adb_test/libbase_test: win32: get some tests working

adb_test:

* Fix adb_utils directory_exists test for Windows. The test actually
fails because directory_exists() is not aware of junctions or symlinks,
but I'm not really sure if that is a bad thing (since these are rare on
Windows to begin with).

* Fix crash during transport tests due to mutex not being initialized.

* io tests fail for various reasons (see adb_io_test.cpp for more info).

libbase_test:

* Get it building on Win32 by implementing mkstemp() and mkdtemp().

* Run StringPrintf %z test on Windows because it passes because we build
with __USE_MINGW_ANSI_STDIO which implements %z.

* I didn't fixup the logging tests: some logging tests fail because when
abort() is called on Windows, by default it pops up UI asking whether a
crash dump should be sent to Microsoft. To some degree this makes sense,
as I think LOG(FATAL) does crash dumping in Chromium. This should be
revisited in the future.

Change-Id: Iaa2433e5294ff162e0b2aa9fe6e4ec09a6893f7a
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
diff --git a/adb/Android.mk b/adb/Android.mk
index 7fcb5ed..46905b3 100644
--- a/adb/Android.mk
+++ b/adb/Android.mk
@@ -123,7 +123,9 @@
 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 @@
     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 0ae21db..f637073 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 309ac02..9c9f85c 100644
--- a/adb/adb_utils_test.cpp
+++ b/adb/adb_utils_test.cpp
@@ -16,6 +16,13 @@
 
 #include "adb_utils.h"
 
+#ifdef _WIN32
+#include <windows.h>
+#include <userenv.h>
+#endif
+
+#include <string>
+
 #include <gtest/gtest.h>
 
 #include <stdlib.h>
@@ -23,12 +30,46 @@
 
 #include "sysdeps.h"
 
+#include <base/macros.h>
 #include <base/test_utils.h>
 
+#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 4b74adf..49deb73 100644
--- a/adb/transport_test.cpp
+++ b/adb/transport_test.cpp
@@ -63,6 +63,27 @@
     }
 };
 
+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 4056684..2158421 100644
--- a/base/file_test.cpp
+++ b/base/file_test.cpp
@@ -34,17 +34,7 @@
   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, 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);