diff --git a/adb/Android.mk b/adb/Android.mk index 7fcb5ed2b..46905b335 100644 --- a/adb/Android.mk +++ b/adb/Android.mk @@ -123,7 +123,9 @@ LOCAL_STATIC_LIBRARIES := libadbd LOCAL_SHARED_LIBRARIES := liblog libbase libcutils include $(BUILD_NATIVE_TEST) -ifneq ($(HOST_OS),windows) +# adb_test +# ========================================================= + include $(CLEAR_VARS) LOCAL_CLANG := $(adb_host_clang) LOCAL_MODULE := adb_test @@ -144,9 +146,13 @@ ifeq ($(HOST_OS),darwin) LOCAL_LDLIBS += -framework CoreFoundation -framework IOKit endif -include $(BUILD_HOST_NATIVE_TEST) +ifeq ($(HOST_OS),windows) + LOCAL_LDLIBS += -lws2_32 -luserenv + LOCAL_STATIC_LIBRARIES += AdbWinApi endif +include $(BUILD_HOST_NATIVE_TEST) + # adb device tracker (used by ddms) test tool # ========================================================= diff --git a/adb/adb_io_test.cpp b/adb/adb_io_test.cpp index 0ae21db5f..f637073a7 100644 --- a/adb/adb_io_test.cpp +++ b/adb/adb_io_test.cpp @@ -30,6 +30,12 @@ #include "base/file.h" #include "base/test_utils.h" +// All of these tests fail on Windows because they use the C Runtime open(), +// but the adb_io APIs expect file descriptors from adb_open(). Also, the +// android::base file APIs use the C Runtime which uses CR/LF translation by +// default (changeable with _setmode()), but the adb_io APIs use adb_read() +// and adb_write() which do no translation. + TEST(io, ReadFdExactly_whole) { const char expected[] = "Foobar"; TemporaryFile tf; diff --git a/adb/adb_utils_test.cpp b/adb/adb_utils_test.cpp index 309ac02c9..9c9f85c7d 100644 --- a/adb/adb_utils_test.cpp +++ b/adb/adb_utils_test.cpp @@ -16,6 +16,13 @@ #include "adb_utils.h" +#ifdef _WIN32 +#include +#include +#endif + +#include + #include #include @@ -23,12 +30,46 @@ #include "sysdeps.h" +#include #include +#ifdef _WIN32 +static std::string subdir(const char* parent, const char* child) { + std::string str(parent); + str += OS_PATH_SEPARATOR; + str += child; + return str; +} +#endif + TEST(adb_utils, directory_exists) { +#ifdef _WIN32 + char profiles_dir[MAX_PATH]; + DWORD cch = arraysize(profiles_dir); + + // On typical Windows 7, returns C:\Users + ASSERT_TRUE(GetProfilesDirectory(profiles_dir, &cch)); + + ASSERT_TRUE(directory_exists(profiles_dir)); + + // On modern (English?) Windows, this is a directory symbolic link to + // C:\ProgramData. Symbolic links are rare on Windows and the user requires + // a special permission (by default granted to Administrative users) to + // create symbolic links. + ASSERT_FALSE(directory_exists(subdir(profiles_dir, "All Users"))); + + // On modern (English?) Windows, this is a directory junction to + // C:\Users\Default. Junctions are used throughout user profile directories + // for backwards compatibility and they don't require any special permissions + // to create. + ASSERT_FALSE(directory_exists(subdir(profiles_dir, "Default User"))); + + ASSERT_FALSE(directory_exists(subdir(profiles_dir, "does-not-exist"))); +#else ASSERT_TRUE(directory_exists("/proc")); ASSERT_FALSE(directory_exists("/proc/self")); // Symbolic link. ASSERT_FALSE(directory_exists("/proc/does-not-exist")); +#endif } TEST(adb_utils, escape_arg) { diff --git a/adb/transport_test.cpp b/adb/transport_test.cpp index 4b74adf92..49deb73d2 100644 --- a/adb/transport_test.cpp +++ b/adb/transport_test.cpp @@ -63,6 +63,27 @@ public: } }; +class TransportSetup { +public: + TransportSetup() { +#ifdef _WIN32 + // Use extern instead of including sysdeps.h which brings in various macros + // that conflict with APIs used in this file. + extern void adb_sysdeps_init(void); + adb_sysdeps_init(); +#else + // adb_sysdeps_init() is an inline function that we cannot link against. +#endif + } +}; + +// Static initializer will call adb_sysdeps_init() before main() to initialize +// the transport mutex before it is used in the tests. Alternatives would be to +// use __attribute__((constructor)) here or to use that or a static initializer +// for adb_sysdeps_init() itself in sysdeps_win32.cpp (caveats of unclear +// init order), or to use a test fixture whose SetUp() could do the init once. +static TransportSetup g_TransportSetup; + TEST(transport, kick_transport) { TestTransport t; diff --git a/base/file_test.cpp b/base/file_test.cpp index 4056684ef..2158421e9 100644 --- a/base/file_test.cpp +++ b/base/file_test.cpp @@ -34,17 +34,7 @@ TEST(file, ReadFileToString_ENOENT) { EXPECT_EQ("", s); // s was cleared. } -TEST(file, ReadFileToString_success) { - std::string s("hello"); - ASSERT_TRUE(android::base::ReadFileToString("/proc/version", &s)) - << strerror(errno); - EXPECT_GT(s.length(), 6U); - EXPECT_EQ('\n', s[s.length() - 1]); - s[5] = 0; - EXPECT_STREQ("Linux", s.c_str()); -} - -TEST(file, WriteStringToFile) { +TEST(file, ReadFileToString_WriteStringToFile) { TemporaryFile tf; ASSERT_TRUE(tf.fd != -1); ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path)) @@ -89,13 +79,23 @@ TEST(file, WriteStringToFd) { } TEST(file, ReadFully) { - int fd = open("/proc/version", O_RDONLY); +#ifdef _WIN32 + VersionFile ver; + ASSERT_NE(ver.filename, nullptr); + const char* filename = ver.filename; + // Note that ReadFully() does CR/LF translation, so we expect \n, not \r\n. + const char expected[] = "\nMicrosoft Windows"; +#else + const char* filename = "/proc/version"; + const char expected[] = "Linux"; +#endif + int fd = open(filename, O_RDONLY); ASSERT_NE(-1, fd) << strerror(errno); char buf[1024]; memset(buf, 0, sizeof(buf)); - ASSERT_TRUE(android::base::ReadFully(fd, buf, 5)); - ASSERT_STREQ("Linux", buf); + ASSERT_TRUE(android::base::ReadFully(fd, buf, sizeof(expected) - 1)); + ASSERT_STREQ(expected, buf); ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);