summaryrefslogtreecommitdiffstats
path: root/toolkit/components/aboutthirdparty/tests/gtest/TestAboutThirdParty.cpp
blob: 565e87e0f1dbc941dd634289f51abfca03b1f64e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

#include <windows.h>
#include "gtest/gtest.h"

#include "../../AboutThirdPartyUtils.h"
#include "mozilla/AboutThirdParty.h"
#include "mozilla/ArrayUtils.h"
#include "nsTArray.h"

using namespace mozilla;

#define WEATHER_RU u"\x041F\x043E\x0433\x043E\x0434\x0430"_ns
#define WEATHER_JA u"\x5929\x6C17"_ns

TEST(AboutThirdParty, CompareIgnoreCase)
{
  EXPECT_EQ(CompareIgnoreCase(u""_ns, u""_ns), 0);
  EXPECT_EQ(CompareIgnoreCase(u"abc"_ns, u"aBc"_ns), 0);
  EXPECT_LT(CompareIgnoreCase(u"a"_ns, u"ab"_ns), 0);
  EXPECT_GT(CompareIgnoreCase(u"ab"_ns, u"A"_ns), 0);
  EXPECT_LT(CompareIgnoreCase(u""_ns, u"aB"_ns), 0);
  EXPECT_GT(CompareIgnoreCase(u"ab"_ns, u""_ns), 0);

  // non-ascii testcases
  EXPECT_EQ(CompareIgnoreCase(WEATHER_JA, WEATHER_JA), 0);
  EXPECT_EQ(CompareIgnoreCase(WEATHER_RU, WEATHER_RU), 0);
  EXPECT_LT(CompareIgnoreCase(WEATHER_RU, WEATHER_JA), 0);
  EXPECT_GT(CompareIgnoreCase(WEATHER_JA, WEATHER_RU), 0);
  EXPECT_EQ(CompareIgnoreCase(WEATHER_RU u"x"_ns WEATHER_JA,
                              WEATHER_RU u"X"_ns WEATHER_JA),
            0);
  EXPECT_GT(
      CompareIgnoreCase(WEATHER_RU u"a"_ns WEATHER_JA, WEATHER_RU u"A"_ns), 0);
  EXPECT_LT(CompareIgnoreCase(WEATHER_RU u"a"_ns WEATHER_RU,
                              WEATHER_RU u"A"_ns WEATHER_JA),
            0);
}

TEST(AboutThirdParty, MsiPackGuid)
{
  nsAutoString packedGuid;
  EXPECT_FALSE(
      MsiPackGuid(u"EDA620E3-AA98-3846-B81E-3493CB2E0E02"_ns, packedGuid));
  EXPECT_FALSE(
      MsiPackGuid(u"*EDA620E3-AA98-3846-B81E-3493CB2E0E02*"_ns, packedGuid));
  EXPECT_TRUE(
      MsiPackGuid(u"{EDA620E3-AA98-3846-B81E-3493CB2E0E02}"_ns, packedGuid));
  EXPECT_STREQ(packedGuid.get(), L"3E026ADE89AA64838BE14339BCE2E020");
}

TEST(AboutThirdParty, CorrectMsiComponentPath)
{
  nsAutoString testPath;

  testPath = u""_ns;
  EXPECT_FALSE(CorrectMsiComponentPath(testPath));

  testPath = u"\\\\server\\share"_ns;
  EXPECT_FALSE(CorrectMsiComponentPath(testPath));

  testPath = u"hello"_ns;
  EXPECT_FALSE(CorrectMsiComponentPath(testPath));

  testPath = u"02:\\Software"_ns;
  EXPECT_FALSE(CorrectMsiComponentPath(testPath));

  testPath = u"C:\\path\\"_ns;
  EXPECT_TRUE(CorrectMsiComponentPath(testPath));
  EXPECT_STREQ(testPath.get(), L"C:\\path\\");

  testPath = u"C?\\path\\"_ns;
  EXPECT_TRUE(CorrectMsiComponentPath(testPath));
  EXPECT_STREQ(testPath.get(), L"C:\\path\\");

  testPath = u"C:\\?path\\"_ns;
  EXPECT_TRUE(CorrectMsiComponentPath(testPath));
  EXPECT_STREQ(testPath.get(), L"C:\\path\\");

  testPath = u"\\?path\\"_ns;
  EXPECT_FALSE(CorrectMsiComponentPath(testPath));
}

TEST(AboutThirdParty, InstallLocations)
{
  const nsLiteralString kDirectoriesUnsorted[] = {
      u"C:\\duplicate\\"_ns, u"C:\\duplicate\\"_ns, u"C:\\app1\\"_ns,
      u"C:\\app2\\"_ns,      u"C:\\app11\\"_ns,     u"C:\\app12\\"_ns,
  };

  struct TestCase {
    nsLiteralString mFile;
    nsLiteralString mInstallPath;
  } const kTestCases[] = {
      {u"C:\\app\\sub\\file.dll"_ns, u""_ns},
      {u"C:\\app1\\sub\\file.dll"_ns, u"C:\\app1\\"_ns},
      {u"C:\\app11\\sub\\file.dll"_ns, u"C:\\app11\\"_ns},
      {u"C:\\app12\\sub\\file.dll"_ns, u"C:\\app12\\"_ns},
      {u"C:\\app13\\sub\\file.dll"_ns, u""_ns},
      {u"C:\\duplicate\\sub\\file.dll"_ns, u""_ns},
  };

  nsTArray<InstallLocationT> locations(ArrayLength(kDirectoriesUnsorted));
  for (size_t i = 0; i < ArrayLength(kDirectoriesUnsorted); ++i) {
    locations.EmplaceBack(kDirectoriesUnsorted[i], new InstalledApplication());
  }

  locations.Sort([](const InstallLocationT& aA, const InstallLocationT& aB) {
    return CompareIgnoreCase(aA.first(), aB.first());
  });

  for (const auto& testCase : kTestCases) {
    auto bounds = EqualRange(locations, 0, locations.Length(),
                             InstallLocationComparator(testCase.mFile));
    if (bounds.second - bounds.first != 1) {
      EXPECT_TRUE(testCase.mInstallPath.IsEmpty());
      continue;
    }

    EXPECT_EQ(locations[bounds.first].first(), testCase.mInstallPath);
  }
}