Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8c61e029 authored by Elliott Hughes's avatar Elliott Hughes Committed by Gerrit Code Review
Browse files

Merge "adb_test/libbase_test: win32: get some tests working"

parents bb3b45d2 cf168a82
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -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
# =========================================================

+6 −0
Original line number Diff line number Diff line
@@ -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;
+41 −0
Original line number Diff line number Diff line
@@ -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) {
+21 −0
Original line number Diff line number Diff line
@@ -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;

+14 −14
Original line number Diff line number Diff line
@@ -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);