TEST(ExceptionTest, EmptyThree)
{
  ::SetLastError(1);
  WindowsApiError e("", "", "");
  ASSERT_STREQ("<unspecified> returned <unknown> with last error code 1", e.what());
}
Ejemplo n.º 2
0
TEST(WTF_RefPtr, Assignment)
{
    DerivedRefLogger a("a");
    RefLogger b("b");
    DerivedRefLogger c("c");

    {
        RefPtr<RefLogger> p1(&a);
        RefPtr<RefLogger> p2(&b);
        ASSERT_EQ(&a, p1.get());
        ASSERT_EQ(&b, p2.get());
        log() << "| ";
        p1 = p2;
        ASSERT_EQ(&b, p1.get());
        ASSERT_EQ(&b, p2.get());
        log() << "| ";
    }
    ASSERT_STREQ("ref(a) ref(b) | ref(b) deref(a) | deref(b) deref(b) ", takeLogStr().c_str());

    {
        RefPtr<RefLogger> ptr(&a);
        ASSERT_EQ(&a, ptr.get());
        log() << "| ";
        ptr = &b;
        ASSERT_EQ(&b, ptr.get());
        log() << "| ";
    }
    ASSERT_STREQ("ref(a) | ref(b) deref(a) | deref(b) ", takeLogStr().c_str());

    {
        RefPtr<RefLogger> ptr(&a);
        ASSERT_EQ(&a, ptr.get());
        log() << "| ";
        ptr = adoptRef(&b);
        ASSERT_EQ(&b, ptr.get());
        log() << "| ";
    }
    ASSERT_STREQ("ref(a) | deref(a) | deref(b) ", takeLogStr().c_str());

    {
        RefPtr<RefLogger> ptr(&a);
        ASSERT_EQ(&a, ptr.get());
        ptr = nullptr;
        ASSERT_EQ(nullptr, ptr.get());
    }
    ASSERT_STREQ("ref(a) deref(a) ", takeLogStr().c_str());

    {
        RefPtr<RefLogger> p1(&a);
        RefPtr<RefLogger> p2(&b);
        ASSERT_EQ(&a, p1.get());
        ASSERT_EQ(&b, p2.get());
        log() << "| ";
        p1 = WTFMove(p2);
        ASSERT_EQ(&b, p1.get());
        ASSERT_EQ(nullptr, p2.get());
        log() << "| ";
    }
    ASSERT_STREQ("ref(a) ref(b) | deref(a) | deref(b) ", takeLogStr().c_str());

    {
        RefPtr<RefLogger> p1(&a);
        RefPtr<DerivedRefLogger> p2(&c);
        ASSERT_EQ(&a, p1.get());
        ASSERT_EQ(&c, p2.get());
        log() << "| ";
        p1 = p2;
        ASSERT_EQ(&c, p1.get());
        ASSERT_EQ(&c, p2.get());
        log() << "| ";
    }
    ASSERT_STREQ("ref(a) ref(c) | ref(c) deref(a) | deref(c) deref(c) ", takeLogStr().c_str());

    {
        RefPtr<RefLogger> ptr(&a);
        ASSERT_EQ(&a, ptr.get());
        log() << "| ";
        ptr = &c;
        ASSERT_EQ(&c, ptr.get());
        log() << "| ";
    }
    ASSERT_STREQ("ref(a) | ref(c) deref(a) | deref(c) ", takeLogStr().c_str());

    {
        RefPtr<RefLogger> ptr(&a);
        ASSERT_EQ(&a, ptr.get());
        log() << "| ";
        ptr = adoptRef(&c);
        ASSERT_EQ(&c, ptr.get());
        log() << "| ";
    }
    ASSERT_STREQ("ref(a) | deref(a) | deref(c) ", takeLogStr().c_str());

    {
        RefPtr<RefLogger> p1(&a);
        RefPtr<DerivedRefLogger> p2(&c);
        ASSERT_EQ(&a, p1.get());
        ASSERT_EQ(&c, p2.get());
        log() << "| ";
        p1 = WTFMove(p2);
        ASSERT_EQ(&c, p1.get());
        ASSERT_EQ(nullptr, p2.get());
        log() << "| ";
    }
    ASSERT_STREQ("ref(a) ref(c) | deref(a) | deref(c) ", takeLogStr().c_str());

    {
        RefPtr<RefLogger> ptr(&a);
        ASSERT_EQ(&a, ptr.get());
        log() << "| ";
        ptr = ptr;
        ASSERT_EQ(&a, ptr.get());
        log() << "| ";
    }
    ASSERT_STREQ("ref(a) | ref(a) deref(a) | deref(a) ", takeLogStr().c_str());

    {
        RefPtr<RefLogger> ptr(&a);
        ASSERT_EQ(&a, ptr.get());
#if COMPILER(CLANG)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunknown-pragmas"
#pragma clang diagnostic ignored "-Wself-move"
#endif
        ptr = WTFMove(ptr);
#if COMPILER(CLANG)
#pragma clang diagnostic pop
#endif
        ASSERT_EQ(&a, ptr.get());
    }
    ASSERT_STREQ("ref(a) deref(a) ", takeLogStr().c_str());
}
TEST(JsonVariant_As_Tests, RandomStringAsCharPtr) {
  JsonVariant variant = "hello";
  ASSERT_STREQ("hello", variant.as<char*>());
}
TEST_F(ScenarioEngageHumanZeroHumans, ReturnsProperMessage)
{
    ASSERT_STREQ(Game::ENGAGE_HUMAN_TRYING_TO_ENGAGE_ZERO_HUMANS.c_str(), mCommandReply->getMessage().c_str());
}
Ejemplo n.º 5
0
// Tests that we can only have a consistent and correct fpos_t when using
// f*pos functions (i.e. fpos doesn't get inside a multi byte character).
TEST(stdio, consistent_fpos_t) {
  ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
  uselocale(LC_GLOBAL_LOCALE);

  FILE* fp = tmpfile();
  ASSERT_TRUE(fp != NULL);

  wchar_t mb_one_bytes = L'h';
  wchar_t mb_two_bytes = 0x00a2;
  wchar_t mb_three_bytes = 0x20ac;
  wchar_t mb_four_bytes = 0x24b62;

  // Write to file.
  ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp)));
  ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
  ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
  ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));

  rewind(fp);

  // Record each character position.
  fpos_t pos1;
  fpos_t pos2;
  fpos_t pos3;
  fpos_t pos4;
  fpos_t pos5;
  EXPECT_EQ(0, fgetpos(fp, &pos1));
  ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
  EXPECT_EQ(0, fgetpos(fp, &pos2));
  ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
  EXPECT_EQ(0, fgetpos(fp, &pos3));
  ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
  EXPECT_EQ(0, fgetpos(fp, &pos4));
  ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
  EXPECT_EQ(0, fgetpos(fp, &pos5));

#if defined(__BIONIC__)
  // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD
  // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In
  // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE
  // structure.
  ASSERT_EQ(0, static_cast<off_t>(pos1));
  ASSERT_EQ(1, static_cast<off_t>(pos2));
  ASSERT_EQ(3, static_cast<off_t>(pos3));
  ASSERT_EQ(6, static_cast<off_t>(pos4));
  ASSERT_EQ(10, static_cast<off_t>(pos5));
#endif

  // Exercise back and forth movements of the position.
  ASSERT_EQ(0, fsetpos(fp, &pos2));
  ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
  ASSERT_EQ(0, fsetpos(fp, &pos1));
  ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
  ASSERT_EQ(0, fsetpos(fp, &pos4));
  ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
  ASSERT_EQ(0, fsetpos(fp, &pos3));
  ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
  ASSERT_EQ(0, fsetpos(fp, &pos5));
  ASSERT_EQ(WEOF, fgetwc(fp));

  fclose(fp);
}
Ejemplo n.º 6
0
TEST(dlext, ns_shared) {
  static const char* root_lib = "libnstest_root_not_isolated.so";
  static const char* root_lib_isolated = "libnstest_root.so";
  std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;

  const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
  const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
  void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
  ASSERT_TRUE(handle_public != nullptr) << dlerror();

  android_set_application_target_sdk_version(42U); // something > 23

  ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();

  // preload this library to the default namespace to check if it
  // is shared later on.
  void* handle_dlopened =
          dlopen((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
  ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();

  android_namespace_t* ns_not_isolated =
          android_create_namespace("private", nullptr,
                                   (lib_path + "/private_namespace_libs").c_str(),
                                   ANDROID_NAMESPACE_TYPE_REGULAR, nullptr);
  ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();

  android_namespace_t* ns_isolated_shared =
          android_create_namespace("private_isolated_shared", nullptr,
                                   (lib_path + "/private_namespace_libs").c_str(),
                                   ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
                                   nullptr);
  ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();

  ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
  ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());

  std::string lib_private_external_path =
      lib_path + "/private_namespace_libs_external/libnstest_private_external.so";

  // Load lib_private_external_path to default namespace
  // (it should remain invisible for the isolated namespaces after this)
  void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
  ASSERT_TRUE(handle != nullptr) << dlerror();

  android_dlextinfo extinfo;
  extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
  extinfo.library_namespace = ns_not_isolated;

  void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
  ASSERT_TRUE(handle1 != nullptr) << dlerror();

  extinfo.library_namespace = ns_isolated_shared;

  void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
  ASSERT_TRUE(handle2 == nullptr);
  ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());

  // Check dlopen by absolute path
  handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
  ASSERT_TRUE(handle2 == nullptr);
  ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed"
            " or dlopened by \"" + get_executable_name() + "\" is not accessible"
            " for the namespace \"private_isolated_shared\"", dlerror());

  // load libnstest_root.so to shared namespace in order to check that everything is different
  // except shared libnstest_dlopened.so

  handle2 = android_dlopen_ext(root_lib_isolated, RTLD_NOW, &extinfo);

  typedef const char* (*fn_t)();
  fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
  ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
  fn_t ns_get_local_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
  ASSERT_TRUE(ns_get_local_string_shared != nullptr) << dlerror();

  ASSERT_STREQ("This string is local to root library", ns_get_local_string());
  ASSERT_STREQ("This string is local to root library", ns_get_local_string_shared());
  ASSERT_TRUE(ns_get_local_string() != ns_get_local_string_shared());

  fn_t ns_get_private_extern_string =
          reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
  ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
  fn_t ns_get_private_extern_string_shared =
          reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
  ASSERT_TRUE(ns_get_private_extern_string_shared() != nullptr) << dlerror();

  ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
  ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string_shared());
  ASSERT_TRUE(ns_get_private_extern_string() != ns_get_private_extern_string_shared());

  fn_t ns_get_public_extern_string =
          reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
  ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
  fn_t ns_get_public_extern_string_shared =
          reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
  ASSERT_TRUE(ns_get_public_extern_string_shared != nullptr) << dlerror();

  ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
  ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string_shared());
  ASSERT_TRUE(ns_get_public_extern_string() == ns_get_public_extern_string_shared());

  fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
  ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
  fn_t ns_get_dlopened_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
  ASSERT_TRUE(ns_get_dlopened_string_shared != nullptr) << dlerror();
  const char** ns_dlopened_string = static_cast<const char**>(dlsym(handle_dlopened, "g_private_dlopened_string"));
  ASSERT_TRUE(ns_dlopened_string != nullptr) << dlerror();

  ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
  ASSERT_STREQ("This string is from private namespace (dlopened library)", *ns_dlopened_string);
  ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string_shared());
  ASSERT_TRUE(ns_get_dlopened_string() != ns_get_dlopened_string_shared());
  ASSERT_TRUE(*ns_dlopened_string == ns_get_dlopened_string_shared());

  dlclose(handle1);
  dlclose(handle2);
}
TEST_F(ScenarioEngageHumanNotEnoughBuildings, ReturnsProperMessage)
{
    ASSERT_STREQ(Game::ENGAGE_HUMAN_NOT_ENOUGH_BUILDINGS.c_str(), mCommandReply->getMessage().c_str());
}
TEST_F(ScenarioDismissHumanNotEnoughEngaged, ReturnsProperMessage)
{
    ASSERT_STREQ(Game::DISMISS_HUMAN_NOT_ENOUGH_ENGAGED.c_str(), mCommandReply->getMessage().c_str());
}
TEST_F(ScenarioDismissHumanLastOneHuman, ReturnsProperMessage)
{
    ASSERT_STREQ(Game::DISMISS_HUMAN_HUMAN_HAS_BEEN_DISMISSED.c_str(), mCommandReply->getMessage().c_str());
}
Ejemplo n.º 10
0
TEST(ExceptionTest, StringStringMessage)
{
  ::SetLastError(6);
  WindowsApiError e("GetErrorMode", "SEM_FAILCRITICALERRORS", "message");
  ASSERT_STREQ("GetErrorMode returned SEM_FAILCRITICALERRORS with last error code 6: message", e.what());
}
Ejemplo n.º 11
0
TEST(ReadSysFile, Main) {
  /* Use a different file name for each test since different tests could be
  executed concurrently. */
  static const char* fn = "TestReadSysFileMain";
  /* If we have a file which contains "abcd" and we read it with ReadSysFile(),
  providing a buffer of size 10 bytes, we would expect 5 bytes to be written
  to that buffer: "abcd\0". */
  struct {
    /* input (file contents), e.g. "abcd" */
    const char* input;
    /* pretended output buffer size, e.g. 10; the actual buffer is larger
    and we check if anything was written past the end of the allowed length */
    size_t output_size;
    /* expected number of bytes written to the output buffer, including the
    terminating '\0', e.g. 5 */
    size_t output_len;
    /* expected output buffer contents, e.g. "abcd\0", the first output_len
    bytes of the output buffer should match the first 'output_len' bytes from
    'output', the rest of the output buffer should be untouched. */
    const char* output;
  } tests[] = {
    /* No new lines */
    {"", 0, 0, ""},
    {"", 1, 1, "\0"}, /* \0 is redundant, but we write it for clarity */
    {"", 9, 1, "\0"},

    {"a", 0, 0, ""},
    {"a", 1, 1, "\0"},
    {"a", 2, 2, "a\0"},
    {"a", 9, 2, "a\0"},

    {"abcd", 0, 0, ""},
    {"abcd", 1, 1, "\0"},
    {"abcd", 2, 2, "a\0"},
    {"abcd", 3, 3, "ab\0"},
    {"abcd", 4, 4, "abc\0"},
    {"abcd", 5, 5, "abcd\0"},
    {"abcd", 9, 5, "abcd\0"},

    /* A single trailing new line */
    {"\n", 0, 0, ""},
    {"\n", 1, 1, "\0"},
    {"\n", 2, 1, "\0"},
    {"\n", 9, 1, "\0"},

    {"a\n", 0, 0, ""},
    {"a\n", 1, 1, "\0"},
    {"a\n", 2, 2, "a\0"},
    {"a\n", 3, 2, "a\0"},
    {"a\n", 9, 2, "a\0"},

    {"abcd\n", 0, 0, ""},
    {"abcd\n", 1, 1, "\0"},
    {"abcd\n", 2, 2, "a\0"},
    {"abcd\n", 3, 3, "ab\0"},
    {"abcd\n", 4, 4, "abc\0"},
    {"abcd\n", 5, 5, "abcd\0"},
    {"abcd\n", 6, 5, "abcd\0"},
    {"abcd\n", 9, 5, "abcd\0"},

    /* Multiple trailing new lines */
    {"\n\n", 0, 0, ""},
    {"\n\n", 1, 1, "\0"},
    {"\n\n", 2, 2, "\n\0"},
    {"\n\n", 3, 2, "\n\0"},
    {"\n\n", 9, 2, "\n\0"},

    {"a\n\n", 0, 0, ""},
    {"a\n\n", 1, 1, "\0"},
    {"a\n\n", 2, 2, "a\0"},
    {"a\n\n", 3, 3, "a\n\0"},
    {"a\n\n", 4, 3, "a\n\0"},
    {"a\n\n", 9, 3, "a\n\0"},

    {"abcd\n\n", 0, 0, ""},
    {"abcd\n\n", 1, 1, "\0"},
    {"abcd\n\n", 2, 2, "a\0"},
    {"abcd\n\n", 3, 3, "ab\0"},
    {"abcd\n\n", 4, 4, "abc\0"},
    {"abcd\n\n", 5, 5, "abcd\0"},
    {"abcd\n\n", 6, 6, "abcd\n\0"},
    {"abcd\n\n", 7, 6, "abcd\n\0"},
    {"abcd\n\n", 9, 6, "abcd\n\0"},

    /* New line in the middle */
    {"ab\ncd", 9, 6, "ab\ncd\0"},
  };

  for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
    ASSERT_TRUE(WriteFile(fn, tests[i].input, strlen(tests[i].input)));
    /* Leave the file to exist if some of the assertions fail. */

    char buf[128];
    static const char unmodified = 'X';

    memset(buf, unmodified, sizeof(buf));

    ASSERT_TRUE(ReadSysFile(fn, buf, tests[i].output_size));

    if (tests[i].output_size == 0) {
      /* The buffer must be unmodified. We check only the first byte. */
      ASSERT_EQ(unmodified, buf[0]);
    } else {
      ASSERT_EQ(tests[i].output_len, strlen(buf) + 1);
      ASSERT_STREQ(tests[i].output, buf);
      /* Check that the first byte after the trailing '\0' has not been
      modified. */
      ASSERT_EQ(unmodified, buf[tests[i].output_len]);
    }
  }

  unlink(fn);
}
Ejemplo n.º 12
0
TEST(ExceptionTest, StringNumberMessage)
{
  ::SetLastError(4);
  WindowsApiError e("Beep", 1, "message");
  ASSERT_STREQ("Beep returned 1 with last error code 4: message", e.what());
}
Ejemplo n.º 13
0
TEST(ExceptionTest, StringNumber)
{
  ::SetLastError(3);
  WindowsApiError e("Beep", 1);
  ASSERT_STREQ("Beep returned 1 with last error code 3", e.what());
}
Ejemplo n.º 14
0
TEST(ExceptionTest, EmptyEmptyMessage)
{
  ::SetLastError(2);
  WindowsApiError e("", "", "message");
  ASSERT_STREQ("<unspecified> returned <unknown> with last error code 2: message", e.what());
}
Ejemplo n.º 15
0
TEST(dlext, ns_anonymous) {
  static const char* root_lib = "libnstest_root.so";
  std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;

  const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;

  const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
  void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);

  ASSERT_TRUE(handle_public != nullptr) << dlerror();

  ASSERT_TRUE(android_init_namespaces(path.c_str(), (lib_path + "/private_namespace_libs").c_str()))
      << dlerror();

  android_namespace_t* ns = android_create_namespace(
                                "private", nullptr,
                                (lib_path + "/private_namespace_libs").c_str(),
                                ANDROID_NAMESPACE_TYPE_REGULAR, nullptr);

  ASSERT_TRUE(ns != nullptr) << dlerror();

  std::string private_library_absolute_path = lib_path + "/private_namespace_libs/" + root_lib;

  android_dlextinfo extinfo;
  extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
  extinfo.library_namespace = ns;

  // we are going to copy this library to anonymous mmap and call the copy of ns_get_dlopened_string
  void* handle = android_dlopen_ext(private_library_absolute_path.c_str(), RTLD_NOW, &extinfo);
  ASSERT_TRUE(handle != nullptr) << dlerror();

  uintptr_t ns_get_dlopened_string_addr =
      reinterpret_cast<uintptr_t>(dlsym(handle, "ns_get_dlopened_string"));
  ASSERT_TRUE(ns_get_dlopened_string_addr != 0) << dlerror();
  typedef const char* (*fn_t)();
  fn_t ns_get_dlopened_string_private = reinterpret_cast<fn_t>(ns_get_dlopened_string_addr);

  std::vector<map_record> maps;
  Maps::parse_maps(&maps);

  uintptr_t addr_start = 0;
  uintptr_t addr_end = 0;
  std::vector<map_record> maps_to_copy;

  for (const auto& rec : maps) {
    if (rec.pathname == private_library_absolute_path) {
      if (addr_start == 0) {
        addr_start = rec.addr_start;
      }
      addr_end = rec.addr_end;

      maps_to_copy.push_back(rec);
    }
  }

  // some sanity checks..
  ASSERT_TRUE(addr_start > 0);
  ASSERT_TRUE(addr_end > 0);
  ASSERT_EQ(3U, maps_to_copy.size());
  ASSERT_TRUE(ns_get_dlopened_string_addr > addr_start);
  ASSERT_TRUE(ns_get_dlopened_string_addr < addr_end);

  // copy
  uintptr_t reserved_addr = reinterpret_cast<uintptr_t>(mmap(nullptr, addr_end - addr_start,
                                                             PROT_NONE, MAP_ANON | MAP_PRIVATE,
                                                             -1, 0));
  ASSERT_TRUE(reinterpret_cast<void*>(reserved_addr) != MAP_FAILED);

  for (const auto& rec : maps_to_copy) {
    uintptr_t offset = rec.addr_start - addr_start;
    size_t size = rec.addr_end - rec.addr_start;
    void* addr = reinterpret_cast<void*>(reserved_addr + offset);
    void* map = mmap(addr, size, PROT_READ | PROT_WRITE,
                     MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0);
    ASSERT_TRUE(map != MAP_FAILED);
    memcpy(map, reinterpret_cast<void*>(rec.addr_start), size);
    mprotect(map, size, rec.perms);
  }

  // call the function copy
  uintptr_t ns_get_dlopened_string_offset  = ns_get_dlopened_string_addr - addr_start;
  fn_t ns_get_dlopened_string_anon = reinterpret_cast<fn_t>(reserved_addr + ns_get_dlopened_string_offset);
  ASSERT_STREQ("This string is from private namespace (dlopened library)",
               ns_get_dlopened_string_anon());

  // They should belong to different namespaces (private and anonymous)
  ASSERT_STREQ("This string is from private namespace (dlopened library)",
               ns_get_dlopened_string_private());

  ASSERT_TRUE(ns_get_dlopened_string_anon() != ns_get_dlopened_string_private());
}
TEST_F(ScenarioDismissHumanLastOneHumanDoubleDismiss, ReturnsProperMessage)
{
    ASSERT_STREQ(Game::DISMISS_HUMAN_NOT_ENOUGH_ENGAGED.c_str(), mCommandReply->getMessage().c_str());
}
Ejemplo n.º 17
0
TEST(dlext, ns_smoke) {
  static const char* root_lib = "libnstest_root.so";
  std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;

  ASSERT_FALSE(android_init_namespaces(path.c_str(), nullptr));
  ASSERT_STREQ("android_init_namespaces failed: error initializing public namespace: "
               "\"libnstest_public.so\" was not found in the default namespace", dlerror());

  ASSERT_FALSE(android_init_namespaces("", nullptr));
  ASSERT_STREQ("android_init_namespaces failed: error initializing public namespace: "
               "the list of public libraries is empty.", dlerror());

  const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;

  const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
  void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
  ASSERT_TRUE(handle_public != nullptr) << dlerror();

  ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();

  // Check that libraries added to public namespace are NODELETE
  dlclose(handle_public);
  handle_public = dlopen((lib_path + "/public_namespace_libs/" + g_public_lib).c_str(),
                         RTLD_NOW | RTLD_NOLOAD);

  ASSERT_TRUE(handle_public != nullptr) << dlerror();

  android_namespace_t* ns1 =
          android_create_namespace("private", nullptr,
                                   (lib_path + "/private_namespace_libs").c_str(),
                                   ANDROID_NAMESPACE_TYPE_REGULAR, nullptr);
  ASSERT_TRUE(ns1 != nullptr) << dlerror();

  android_namespace_t* ns2 =
          android_create_namespace("private_isolated", nullptr,
                                   (lib_path + "/private_namespace_libs").c_str(),
                                   ANDROID_NAMESPACE_TYPE_ISOLATED, nullptr);
  ASSERT_TRUE(ns2 != nullptr) << dlerror();

  // This should not have affect search path for default namespace:
  ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
  void* handle = dlopen(g_public_lib, RTLD_NOW);
  ASSERT_TRUE(handle != nullptr) << dlerror();
  dlclose(handle);

  android_dlextinfo extinfo;
  extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
  extinfo.library_namespace = ns1;

  void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
  ASSERT_TRUE(handle1 != nullptr) << dlerror();

  extinfo.library_namespace = ns2;
  void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
  ASSERT_TRUE(handle2 != nullptr) << dlerror();

  ASSERT_TRUE(handle1 != handle2);

  // dlopen for a public library using an absolute path should work for isolated namespaces
  extinfo.library_namespace = ns2;
  handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
  ASSERT_TRUE(handle != nullptr) << dlerror();
  ASSERT_TRUE(handle == handle_public);

  dlclose(handle);

  typedef const char* (*fn_t)();

  fn_t ns_get_local_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
  ASSERT_TRUE(ns_get_local_string1 != nullptr) << dlerror();
  fn_t ns_get_local_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
  ASSERT_TRUE(ns_get_local_string2 != nullptr) << dlerror();

  EXPECT_STREQ("This string is local to root library", ns_get_local_string1());
  EXPECT_STREQ("This string is local to root library", ns_get_local_string2());

  ASSERT_TRUE(ns_get_local_string1() != ns_get_local_string2());

  fn_t ns_get_private_extern_string1 =
          reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
  ASSERT_TRUE(ns_get_private_extern_string1 != nullptr) << dlerror();
  fn_t ns_get_private_extern_string2 =
          reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
  ASSERT_TRUE(ns_get_private_extern_string2 != nullptr) << dlerror();

  EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string1());
  EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string2());

  ASSERT_TRUE(ns_get_private_extern_string1() != ns_get_private_extern_string2());

  fn_t ns_get_public_extern_string1 =
          reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
  ASSERT_TRUE(ns_get_public_extern_string1 != nullptr) << dlerror();
  fn_t ns_get_public_extern_string2 =
          reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
  ASSERT_TRUE(ns_get_public_extern_string2 != nullptr) << dlerror();

  EXPECT_STREQ("This string is from public namespace", ns_get_public_extern_string1());
  ASSERT_TRUE(ns_get_public_extern_string1() == ns_get_public_extern_string2());

  // and now check that dlopen() does the right thing in terms of preserving namespace
  fn_t ns_get_dlopened_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
  ASSERT_TRUE(ns_get_dlopened_string1 != nullptr) << dlerror();
  fn_t ns_get_dlopened_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
  ASSERT_TRUE(ns_get_dlopened_string2 != nullptr) << dlerror();

  EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string1());
  EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());

  ASSERT_TRUE(ns_get_dlopened_string1() != ns_get_dlopened_string2());

  dlclose(handle1);

  // Check if handle2 is still alive (and well)
  ASSERT_STREQ("This string is local to root library", ns_get_local_string2());
  ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
  ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string2());
  ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());

  dlclose(handle2);
}
TEST_F(ScenarioDismissHumanZeroHumans, ReturnsProperMessage)
{
    ASSERT_STREQ(Game::DISMISS_HUMAN_TRYING_TO_DISMISS_ZERO_HUMANS.c_str(), mCommandReply->getMessage().c_str());
}
TEST_F(ScenarioEngageHumanNotEnoughResources, ReturnsProperMessage)
{
    ASSERT_STREQ(Game::ENGAGE_HUMAN_NOT_ENOUGH_RESOURCES.c_str(), mCommandReply->getMessage().c_str());
}
TEST_F(ScenarioDismissHumanNotDismissable, ReturnsProperMessage)
{
    ASSERT_STREQ(Game::DISMISS_HUMAN_HUMAN_IS_NOT_DISMISSABLE.c_str(), mCommandReply->getMessage().c_str());
}
TEST_F(ScenarioEngageHumanOneHuman, ReturnsProperMessage)
{
    ASSERT_STREQ(Game::ENGAGE_HUMAN_HUMAN_HAS_BEEN_ENGAGED.c_str(), mCommandReply->getMessage().c_str());
}
Ejemplo n.º 22
0
TEST(HelloTest, NameIsNull) {
    ASSERT_STREQ("Hello ",hello(""));
    ASSERT_STREQ("Hello ",hello(NULL));
}
TEST_F(ScenarioEngageHumanNotEngageable, ReturnsProperMessage)
{
    ASSERT_STREQ(Game::ENGAGE_HUMAN_HUMAN_IS_NOT_ENGAGEABLE.c_str(), mCommandReply->getMessage().c_str());
}
Ejemplo n.º 24
0
TEST(HelloTest, NameNotNull) { 
    ASSERT_STREQ("Hello World",hello("World"));

    ASSERT_STREQ("Hello GooooooooooooooooooooooooooooooooooooooD", 
    	hello("GooooooooooooooooooooooooooooooooooooooD"));    
}
Ejemplo n.º 25
0
TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
  dlerror(); // Clear any pending errors.
  void* handle = dlopen("no-elf-hash-table-library.so", RTLD_NOW);
  ASSERT_TRUE(handle == NULL);
  ASSERT_STREQ("dlopen failed: empty/missing DT_HASH in \"no-elf-hash-table-library.so\" (built with --hash-style=gnu?)", dlerror());
}
static void testNumberToStringECMAScript(double number, const char* reference)
{
    CString numberString = String::numberToStringECMAScript(number).latin1();
    ASSERT_STREQ(reference, numberString.data());
}
Ejemplo n.º 27
0
TEST(WTF_RefPtr, Basic)
{
    DerivedRefLogger a("a");

    RefPtr<RefLogger> empty;
    ASSERT_EQ(nullptr, empty.get());

    {
        RefPtr<RefLogger> ptr(&a);
        ASSERT_EQ(&a, ptr.get());
        ASSERT_EQ(&a, &*ptr);
        ASSERT_EQ(&a.name, &ptr->name);
    }
    ASSERT_STREQ("ref(a) deref(a) ", takeLogStr().c_str());

    {
        RefPtr<RefLogger> ptr = &a;
        ASSERT_EQ(&a, ptr.get());
    }
    ASSERT_STREQ("ref(a) deref(a) ", takeLogStr().c_str());

    {
        RefPtr<RefLogger> p1 = &a;
        RefPtr<RefLogger> p2(p1);
        ASSERT_EQ(&a, p1.get());
        ASSERT_EQ(&a, p2.get());
    }
    ASSERT_STREQ("ref(a) ref(a) deref(a) deref(a) ", takeLogStr().c_str());

    {
        RefPtr<RefLogger> p1 = &a;
        RefPtr<RefLogger> p2 = p1;
        ASSERT_EQ(&a, p1.get());
        ASSERT_EQ(&a, p2.get());
    }
    ASSERT_STREQ("ref(a) ref(a) deref(a) deref(a) ", takeLogStr().c_str());

    {
        RefPtr<RefLogger> p1 = &a;
        RefPtr<RefLogger> p2 = WTFMove(p1);
        ASSERT_EQ(nullptr, p1.get());
        ASSERT_EQ(&a, p2.get());
    }
    ASSERT_STREQ("ref(a) deref(a) ", takeLogStr().c_str());

    {
        RefPtr<RefLogger> p1 = &a;
        RefPtr<RefLogger> p2(WTFMove(p1));
        ASSERT_EQ(nullptr, p1.get());
        ASSERT_EQ(&a, p2.get());
    }
    ASSERT_STREQ("ref(a) deref(a) ", takeLogStr().c_str());

    {
        RefPtr<DerivedRefLogger> p1 = &a;
        RefPtr<RefLogger> p2 = p1;
        ASSERT_EQ(&a, p1.get());
        ASSERT_EQ(&a, p2.get());
    }
    ASSERT_STREQ("ref(a) ref(a) deref(a) deref(a) ", takeLogStr().c_str());

    {
        RefPtr<DerivedRefLogger> p1 = &a;
        RefPtr<RefLogger> p2 = WTFMove(p1);
        ASSERT_EQ(nullptr, p1.get());
        ASSERT_EQ(&a, p2.get());
    }
    ASSERT_STREQ("ref(a) deref(a) ", takeLogStr().c_str());

    {
        RefPtr<RefLogger> ptr(&a);
        ASSERT_EQ(&a, ptr.get());
        ptr = nullptr;
        ASSERT_EQ(nullptr, ptr.get());
    }
    ASSERT_STREQ("ref(a) deref(a) ", takeLogStr().c_str());

    {
        RefPtr<RefLogger> ptr(&a);
        ASSERT_EQ(&a, ptr.get());
        ptr.release();
        ASSERT_EQ(nullptr, ptr.get());
    }
    ASSERT_STREQ("ref(a) deref(a) ", takeLogStr().c_str());
}
Ejemplo n.º 28
0
TEST(ConfigParserTest, ValidParsing) {
  taylortrack::utils::ConfigParser parser("../Testdata/taylortrack.conf");
  taylortrack::utils::GeneralOptions general = parser.get_general_configuration();
  taylortrack::utils::AudioSettings audio = parser.get_audio_configuration();
  taylortrack::utils::VideoSettings video = parser.get_video_configuration();
  taylortrack::utils::CombinationSettings combination = parser.get_combination_configuration();
  taylortrack::utils::CommunicationSettings audio_in = parser.get_audio_communication_in();
  taylortrack::utils::CommunicationSettings audio_out = parser.get_audio_communication_out();
  taylortrack::utils::CommunicationSettings video_in = parser.get_video_communication_in();
  taylortrack::utils::CommunicationSettings video_out = parser.get_video_communication_out();
  taylortrack::utils::CommunicationSettings combination_audio_in = parser.get_audio_communication_in();
  taylortrack::utils::CommunicationSettings combination_video_in = parser.get_video_communication_in();

  taylortrack::utils::CommunicationSettings combination_out = parser.get_combination_communication_out();
  taylortrack::utils::CommunicationSettings visualizer_in = parser.get_visualizer_communication_in();
  taylortrack::utils::CommunicationSettings input_out = parser.get_input_communication_out();

  std::valarray<double> mic_x = {4, 8.2, 2, 9.123, 0, 3, 5, 5.123};
  std::valarray<double> mic_y = {0.1, 0.12, 0.123, 0.1245, 1234.1, 1234.128, 52, 6123.41234};
  bool mic_x_eq = false, mic_y_eq = false;

  for (unsigned int i = 0; i < audio.mic_x.size(); ++i) {
    mic_x_eq = mic_x[i] == audio.mic_x[i];
    if (!mic_x_eq)
      break;
  }

  for (unsigned int i = 0; i < audio.mic_x.size(); ++i) {
    mic_y_eq = mic_y[i] == audio.mic_y[i];
    if (!mic_y_eq)
      break;
  }

  ASSERT_TRUE(parser.is_valid());
  ASSERT_FALSE(general.console_output);

  // Old deprecated method
  ASSERT_STREQ("/test_audio_inport", audio.inport.c_str());
  ASSERT_STREQ("/test_audio_outport", audio.outport.c_str());

  // New not yet deprecated method
  ASSERT_STREQ("/test_audio_inport", audio_in.port.c_str());
  ASSERT_STREQ("/test_audio_outport", audio_out.port.c_str());

  ASSERT_EQ(53242342, audio.sample_rate);
  ASSERT_TRUE(mic_x_eq);
  ASSERT_TRUE(mic_y_eq);
  ASSERT_EQ(3.1472637, audio.beta);
  ASSERT_EQ(12, audio.grid_x);
  ASSERT_EQ(12, audio.grid_y);
  ASSERT_EQ(0.98765543123, audio.interval);
  ASSERT_EQ(8765, audio.frame_size);

  // Old deprecated method
  ASSERT_STREQ("/test_video_inport", video.inport.c_str());
  ASSERT_STREQ("/test_video_outport", video.outport.c_str());

  // New not yet deprecated method
  ASSERT_STREQ("/test_video_inport", video_in.port.c_str());
  ASSERT_STREQ("/test_video_outport", video_out.port.c_str());

  ASSERT_STREQ("/test_audio_inport", combination_audio_in.port.c_str());
  ASSERT_STREQ("/test_video_inport", combination_video_in.port.c_str());
  ASSERT_STREQ("/test_combination_outport", combination_out.port.c_str());

  ASSERT_STREQ("/test_visualizer_inport", visualizer_in.port.c_str());

  ASSERT_STREQ("/test_input_outport", input_out.port.c_str());
}
Ejemplo n.º 29
0
TEST(signal, sys_siglist) {
  ASSERT_TRUE(sys_siglist[0] == NULL);
  ASSERT_STREQ("Hangup", sys_siglist[SIGHUP]);
}
Ejemplo n.º 30
0
TEST_F (MsgIterateTestC, CreateIterator)
{   
    mama_fid_t      fid         = 0;
    mamaMsgIterator iterator    = NULL;
    mamaMsgField    field       = NULL;
    mamaFieldType   type        = MAMA_FIELD_TYPE_UNKNOWN;    

    /* Create a mama message. */
    mamaMsg msg = NULL;
    mamaMsg_create (&msg);
    
    /* add a fields to the message. */
    mamaMsg_addString (msg, "string", 101, "This is an iteration test.");
    mamaMsg_addU8     (msg, "u8", 102, 8);
    mamaMsg_addU16    (msg, "u16", 103, 16);
    mamaMsg_addU32    (msg, "u32", 104, 32);
    mamaMsg_addU64    (msg, "u64", 105, 64);
   
    /* Create iterator and message field required.*/
    ASSERT_EQ (MAMA_STATUS_OK, 
               mamaMsgIterator_create (&iterator,NULL));
    
    mamaMsgIterator_associate (iterator,msg);
    
    while ((field = mamaMsgIterator_next(iterator)) != NULL)
    {
        /* Operate on contents of each field. */
        mamaMsgField_getFid (field, &fid);
        mamaMsgField_getType (field, &type);
        switch(type)
        {
            case MAMA_FIELD_TYPE_STRING:
            {
                char buffer[MAX_FIELD_STR_LEN];
                ASSERT_EQ (101, fid);
                mamaMsgField_getAsString (field, buffer, MAX_FIELD_STR_LEN);
                ASSERT_STREQ ("This is an iteration test.", buffer);
                break;
            }
            case MAMA_FIELD_TYPE_U8:
            {
                mama_u8_t buffer = 0;
                ASSERT_EQ (102, fid);
                mamaMsgField_getU8 (field, &buffer);
                ASSERT_EQ (8, buffer);
                break;
            }
            case MAMA_FIELD_TYPE_U16:
            {
                mama_u16_t buffer = 0;
                ASSERT_EQ (103, fid);
                mamaMsgField_getU16 (field, &buffer);
                ASSERT_EQ (16, buffer);
                break;
            }
            case MAMA_FIELD_TYPE_U32:
            {
                mama_u32_t buffer = 0;
                ASSERT_EQ (104, fid);
                mamaMsgField_getU32 (field, &buffer);
                ASSERT_EQ (32, buffer);
                break;
            }
            case MAMA_FIELD_TYPE_U64:
            {
                mama_u64_t buffer = 0;
                ASSERT_EQ (105, fid);
                mamaMsgField_getU64 (field, &buffer);
                ASSERT_EQ (64, buffer);
                break;
            }
            default:
                break;
        }
    }

    /* destroy the message. */
    mamaMsg_destroy (msg);
}