tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestNativeNtGTest.cpp (2241B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include <windows.h>
      8 
      9 #include "gtest/gtest.h"
     10 
     11 #include "mozilla/NativeNt.h"
     12 #include "nsHashKeys.h"
     13 #include "nsTHashSet.h"
     14 
     15 TEST(TestNativeNtGTest, GenerateDependentModuleSet)
     16 {
     17  mozilla::nt::PEHeaders executable(::GetModuleHandleW(nullptr));
     18  nsTHashSet<nsStringCaseInsensitiveHashKey> dependentModules;
     19  executable.EnumImportChunks([&](const char* aModule) {
     20    dependentModules.Insert(
     21        mozilla::nt::GetLeafName(NS_ConvertASCIItoUTF16(aModule)));
     22  });
     23 
     24  EXPECT_TRUE(dependentModules.Contains(u"mozglue.dll"_ns));
     25  EXPECT_TRUE(dependentModules.Contains(u"MOZGLUE.dll"_ns));
     26  EXPECT_FALSE(dependentModules.Contains(u"xxx.dll"_ns));
     27 }
     28 
     29 TEST(TestNativeNtGTest, GetLeafName)
     30 {
     31  nsAutoString str;
     32  str = mozilla::nt::GetLeafName(u""_ns);
     33  EXPECT_STREQ(str.get(), L"");
     34  str = mozilla::nt::GetLeafName(u"\\"_ns);
     35  EXPECT_STREQ(str.get(), L"");
     36  str = mozilla::nt::GetLeafName(u"\\\\"_ns);
     37  EXPECT_STREQ(str.get(), L"");
     38  str = mozilla::nt::GetLeafName(u"abc\\def\\ghi"_ns);
     39  EXPECT_STREQ(str.get(), L"ghi");
     40  str = mozilla::nt::GetLeafName(u"abcdef"_ns);
     41  EXPECT_STREQ(str.get(), L"abcdef");
     42  str = mozilla::nt::GetLeafName(u"\\abcdef"_ns);
     43  EXPECT_STREQ(str.get(), L"abcdef");
     44 
     45  const auto kEntireText =
     46      u"\\"_ns
     47      u"\\\\abc"_ns
     48      u"\\x\\y\\z"_ns
     49      u"123\\456\\"_ns
     50      u"789"_ns;
     51  str = mozilla::nt::GetLeafName(Substring(kEntireText, 0, 0));
     52  EXPECT_STREQ(str.get(), L"");
     53  str = mozilla::nt::GetLeafName(Substring(kEntireText, 0, 1));
     54  EXPECT_STREQ(str.get(), L"");
     55  str = mozilla::nt::GetLeafName(Substring(kEntireText, 1, 5));
     56  EXPECT_STREQ(str.get(), L"abc");
     57  str = mozilla::nt::GetLeafName(Substring(kEntireText, 6, 6));
     58  EXPECT_STREQ(str.get(), L"z");
     59  str = mozilla::nt::GetLeafName(Substring(kEntireText, 12, 8));
     60  EXPECT_STREQ(str.get(), L"");
     61  str = mozilla::nt::GetLeafName(Substring(kEntireText, 20));
     62  EXPECT_STREQ(str.get(), L"789");
     63 }