forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_util_unittest.cc
197 lines (158 loc) · 7.15 KB
/
install_util_unittest.cc
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/install_static/install_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::ElementsAre;
namespace install_static {
// Tests the MatchPattern function in the install_static library.
TEST(InstallStaticTest, MatchPattern) {
EXPECT_TRUE(MatchPattern(L"", L""));
EXPECT_TRUE(MatchPattern(L"", L"*"));
EXPECT_FALSE(MatchPattern(L"", L"*a"));
EXPECT_FALSE(MatchPattern(L"", L"abc"));
EXPECT_TRUE(MatchPattern(L"Hello1234", L"He??o*1*"));
EXPECT_TRUE(MatchPattern(L"Foo", L"F*?"));
EXPECT_TRUE(MatchPattern(L"Foo", L"F*"));
EXPECT_FALSE(MatchPattern(L"Foo", L"F*b"));
EXPECT_TRUE(MatchPattern(L"abcd", L"*c*d"));
EXPECT_TRUE(MatchPattern(L"abcd", L"*?c*d"));
EXPECT_FALSE(MatchPattern(L"abcd", L"abcd*efgh"));
EXPECT_TRUE(MatchPattern(L"foobarabc", L"*bar*"));
}
// Tests the TokenizeString function in the install_static library.
TEST(InstallStaticTest, TokenizeString) {
// Test if the string is tokenized correctly with all tokens stripped of
// leading and trailing spaces.
std::vector<std::string> results =
TokenizeString("un |deux\t|trois\n|quatre", '|', true);
ASSERT_EQ(4u, results.size());
EXPECT_THAT(results, ElementsAre("un", "deux", "trois", "quatre"));
// Test if the string is tokenized correctly with all tokens having
// leading and trailing spaces intact.
results = TokenizeString("un |deux\t|trois\n|quatre", '|', false);
ASSERT_EQ(4u, results.size());
EXPECT_THAT(results, ElementsAre("un ", "deux\t", "trois\n", "quatre"));
// Test if tokenize returns the original string if a string containing only
// one token and no delimiters is passed.
results = TokenizeString("un |deux\t|trois\n|quatre", '!', false);
ASSERT_EQ(1u, results.size());
ASSERT_EQ(results[0], "un |deux\t|trois\n|quatre");
// Test if tokenize handles a space character as delimiter.
results = TokenizeString("foo bar bleh blah boo", ' ', false);
ASSERT_EQ(5u, results.size());
EXPECT_THAT(results, ElementsAre("foo", "bar", "bleh", "blah", "boo"));
// Test string with only delimiters.
results = TokenizeString("||||", '|', false);
ASSERT_EQ(4u, results.size());
EXPECT_THAT(results, ElementsAre("", "", "", ""));
// Test string with spaces separated by delimiters.
results = TokenizeString(" | | | |", '|', false);
ASSERT_EQ(4u, results.size());
EXPECT_THAT(results, ElementsAre(" ", " ", " ", " "));
results = TokenizeString("one|two||four", '|', false);
ASSERT_EQ(4u, results.size());
EXPECT_THAT(results, ElementsAre("one", "two", "", "four"));
// TokenizeString16 tests.
// Test if the string is tokenized correctly with all tokens stripped of
// leading and trailing spaces.
std::vector<std::wstring> results16 =
TokenizeString16(L"un |deux\t|trois\n|quatre", L'|', true);
ASSERT_EQ(4u, results16.size());
EXPECT_THAT(results16, ElementsAre(L"un", L"deux", L"trois", L"quatre"));
// Test string with spaces separated by delimiters.
results16 = TokenizeString16(L"one|two||four", L'|', false);
ASSERT_EQ(4u, results16.size());
EXPECT_THAT(results16, ElementsAre(L"one", L"two", L"", L"four"));
}
// Tests the CompareVersionString function in the install_static library.
TEST(InstallStaticTest, CompareVersions) {
// Case 1. Invalid versions.
int result = 0;
EXPECT_FALSE(CompareVersionStrings("", "", &result));
EXPECT_FALSE(CompareVersionStrings("0.0.0.0", "A.B.C.D", &result));
EXPECT_FALSE(CompareVersionStrings("A.0.0.0", "0.0.0.0", &result));
// Case 2. Equal versions.
EXPECT_TRUE(CompareVersionStrings("0.0.0.0", "0.0.0.0", &result));
EXPECT_EQ(0, result);
// Case 3. Version1 > Version 2.
EXPECT_TRUE(CompareVersionStrings("1.0.0.0", "0.0.0.0", &result));
EXPECT_EQ(1, result);
// Case 4. Version1 < Version 2.
EXPECT_TRUE(CompareVersionStrings("0.0.0.0", "1.0.0.0", &result));
EXPECT_EQ(-1, result);
// Case 5. Version1 < Version 2.
EXPECT_TRUE(CompareVersionStrings("0.0", "0.0.1.0", &result));
EXPECT_EQ(-1, result);
// Case 6. Version1 > Version 2.
EXPECT_TRUE(CompareVersionStrings("0.0.0.2", "0.0", &result));
EXPECT_EQ(1, result);
// Case 7. Version1 > Version 2.
EXPECT_TRUE(CompareVersionStrings("1.1.1.2", "1.1.1.1", &result));
EXPECT_EQ(1, result);
// Case 8. Version1 < Version2
EXPECT_TRUE(CompareVersionStrings("0.0.0.2", "0.0.0.3", &result));
EXPECT_EQ(-1, result);
// Case 9. Version1 > Version2
EXPECT_TRUE(CompareVersionStrings("0.0.0.4", "0.0.0.3", &result));
EXPECT_EQ(1, result);
// Case 10. Version1 > Version2. Multiple digit numbers.
EXPECT_TRUE(CompareVersionStrings("0.0.12.1", "0.0.10.3", &result));
EXPECT_EQ(1, result);
// Case 11. Version1 < Version2. Multiple digit number.
EXPECT_TRUE(CompareVersionStrings("10.11.12.13", "12.11.12.13", &result));
EXPECT_EQ(-1, result);
}
// Tests the install_static::GetSwitchValueFromCommandLine function.
TEST(InstallStaticTest, GetSwitchValueFromCommandLineTest) {
// Simple case with one switch.
std::string value =
GetSwitchValueFromCommandLine("c:\\temp\\bleh.exe --type=bar", "type");
EXPECT_EQ("bar", value);
// Multiple switches with trailing spaces between them.
value = GetSwitchValueFromCommandLine(
"c:\\temp\\bleh.exe --type=bar --abc=def bleh", "abc");
EXPECT_EQ("def", value);
// Multiple switches with trailing spaces and tabs between them.
value = GetSwitchValueFromCommandLine(
"c:\\temp\\bleh.exe --type=bar \t\t\t --abc=def bleh", "abc");
EXPECT_EQ("def", value);
// Non existent switch.
value = GetSwitchValueFromCommandLine(
"c:\\temp\\bleh.exe --foo=bar --abc=def bleh", "type");
EXPECT_EQ("", value);
// Non existent switch.
value = GetSwitchValueFromCommandLine("c:\\temp\\bleh.exe", "type");
EXPECT_EQ("", value);
// Non existent switch.
value = GetSwitchValueFromCommandLine("c:\\temp\\bleh.exe type=bar", "type");
EXPECT_EQ("", value);
// Trailing spaces after the switch.
value = GetSwitchValueFromCommandLine(
"c:\\temp\\bleh.exe --type=bar \t\t", "type");
EXPECT_EQ("bar", value);
// Multiple switches with trailing spaces and tabs between them.
value = GetSwitchValueFromCommandLine(
"c:\\temp\\bleh.exe --type=bar \t\t --foo=bleh", "foo");
EXPECT_EQ("bleh", value);
// Nothing after a switch.
value = GetSwitchValueFromCommandLine("c:\\temp\\bleh.exe --type=", "type");
EXPECT_TRUE(value.empty());
// Whitespace after a switch.
value = GetSwitchValueFromCommandLine("c:\\temp\\bleh.exe --type= ", "type");
EXPECT_TRUE(value.empty());
// Just tabs after a switch.
value = GetSwitchValueFromCommandLine("c:\\temp\\bleh.exe --type=\t\t\t",
"type");
EXPECT_TRUE(value.empty());
// Whitespace after the "=" before the value.
value = GetSwitchValueFromCommandLine("c:\\temp\\bleh.exe --type= bar",
"type");
EXPECT_EQ("bar", value);
// Tabs after the "=" before the value.
value = GetSwitchValueFromCommandLine("c:\\temp\\bleh.exe --type=\t\t\tbar",
"type");
EXPECT_EQ(value, "bar");
}
} // namespace install_static