OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/install_static/install_util.h" | 5 #include "chrome/install_static/install_util.h" |
6 | 6 |
7 #include <tuple> | 7 #include <tuple> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 | 83 |
84 // Whitespace after a switch. | 84 // Whitespace after a switch. |
85 value = | 85 value = |
86 GetSwitchValueFromCommandLine(L"c:\\temp\\bleh.exe --type= ", L"type"); | 86 GetSwitchValueFromCommandLine(L"c:\\temp\\bleh.exe --type= ", L"type"); |
87 EXPECT_TRUE(value.empty()); | 87 EXPECT_TRUE(value.empty()); |
88 | 88 |
89 // Just tabs after a switch. | 89 // Just tabs after a switch. |
90 value = GetSwitchValueFromCommandLine(L"c:\\temp\\bleh.exe --type=\t\t\t", | 90 value = GetSwitchValueFromCommandLine(L"c:\\temp\\bleh.exe --type=\t\t\t", |
91 L"type"); | 91 L"type"); |
92 EXPECT_TRUE(value.empty()); | 92 EXPECT_TRUE(value.empty()); |
| 93 } |
93 | 94 |
94 // Whitespace after the "=" before the value. | 95 TEST(InstallStaticTest, SpacesAndQuotesInCommandLineArguments) { |
95 value = | 96 std::vector<std::wstring> tokenized; |
96 GetSwitchValueFromCommandLine(L"c:\\temp\\bleh.exe --type= bar", L"type"); | |
97 EXPECT_EQ(L"bar", value); | |
98 | 97 |
99 // Tabs after the "=" before the value. | 98 tokenized = TokenizeCommandLineToArray(L"\"C:\\a\\b.exe\""); |
100 value = GetSwitchValueFromCommandLine(L"c:\\temp\\bleh.exe --type=\t\t\tbar", | 99 ASSERT_EQ(1u, tokenized.size()); |
101 L"type"); | 100 EXPECT_EQ(L"C:\\a\\b.exe", tokenized[0]); |
102 EXPECT_EQ(value, L"bar"); | 101 |
| 102 tokenized = TokenizeCommandLineToArray(L"x.exe"); |
| 103 ASSERT_EQ(1u, tokenized.size()); |
| 104 EXPECT_EQ(L"x.exe", tokenized[0]); |
| 105 |
| 106 tokenized = TokenizeCommandLineToArray(L"\"c:\\with space\\something.exe\""); |
| 107 ASSERT_EQ(1u, tokenized.size()); |
| 108 EXPECT_EQ(L"c:\\with space\\something.exe", tokenized[0]); |
| 109 |
| 110 tokenized = TokenizeCommandLineToArray(L"\"C:\\a\\b.exe\" arg"); |
| 111 ASSERT_EQ(2u, tokenized.size()); |
| 112 EXPECT_EQ(L"C:\\a\\b.exe", tokenized[0]); |
| 113 EXPECT_EQ(L"arg", tokenized[1]); |
| 114 |
| 115 tokenized = TokenizeCommandLineToArray(L"\"C:\\with space\\b.exe\" \"arg\""); |
| 116 ASSERT_EQ(2u, tokenized.size()); |
| 117 EXPECT_EQ(L"C:\\with space\\b.exe", tokenized[0]); |
| 118 EXPECT_EQ(L"arg", tokenized[1]); |
| 119 |
| 120 tokenized = TokenizeCommandLineToArray(L"\"C:\\a\\b.exe\" c:\\tmp\\"); |
| 121 ASSERT_EQ(2u, tokenized.size()); |
| 122 EXPECT_EQ(L"C:\\a\\b.exe", tokenized[0]); |
| 123 EXPECT_EQ(L"c:\\tmp\\", tokenized[1]); |
| 124 |
| 125 tokenized = |
| 126 TokenizeCommandLineToArray(L"\"C:\\a\\b.exe\" \"c:\\some file path\\\""); |
| 127 ASSERT_EQ(2u, tokenized.size()); |
| 128 EXPECT_EQ(L"C:\\a\\b.exe", tokenized[0]); |
| 129 EXPECT_EQ(L"c:\\some file path\"", tokenized[1]); |
| 130 |
| 131 tokenized = TokenizeCommandLineToArray( |
| 132 L"\"C:\\with space\\b.exe\" \\\\x\\\\ \\\\y\\\\"); |
| 133 ASSERT_EQ(3u, tokenized.size()); |
| 134 EXPECT_EQ(L"C:\\with space\\b.exe", tokenized[0]); |
| 135 EXPECT_EQ(L"\\\\x\\\\", tokenized[1]); |
| 136 EXPECT_EQ(L"\\\\y\\\\", tokenized[2]); |
| 137 |
| 138 tokenized = TokenizeCommandLineToArray( |
| 139 L"\"C:\\with space\\b.exe\" \"\\\\space quoted\\\\\""); |
| 140 ASSERT_EQ(2u, tokenized.size()); |
| 141 EXPECT_EQ(L"C:\\with space\\b.exe", tokenized[0]); |
| 142 EXPECT_EQ(L"\\\\space quoted\\", tokenized[1]); |
| 143 |
| 144 tokenized = TokenizeCommandLineToArray( |
| 145 L"\"C:\\with space\\b.exe\" --stuff -x -Y \"c:\\some thing\\\" " |
| 146 L"weewaa "); |
| 147 ASSERT_EQ(5u, tokenized.size()); |
| 148 EXPECT_EQ(L"C:\\with space\\b.exe", tokenized[0]); |
| 149 EXPECT_EQ(L"--stuff", tokenized[1]); |
| 150 EXPECT_EQ(L"-x", tokenized[2]); |
| 151 EXPECT_EQ(L"-Y", tokenized[3]); |
| 152 EXPECT_EQ(L"c:\\some thing\" weewaa ", tokenized[4]); |
| 153 |
| 154 tokenized = TokenizeCommandLineToArray( |
| 155 L"\"C:\\with space\\b.exe\" --stuff=\"d:\\stuff and things\""); |
| 156 EXPECT_EQ(2u, tokenized.size()); |
| 157 EXPECT_EQ(L"C:\\with space\\b.exe", tokenized[0]); |
| 158 EXPECT_EQ(L"--stuff=d:\\stuff and things", tokenized[1]); |
| 159 |
| 160 tokenized = TokenizeCommandLineToArray( |
| 161 L"\"C:\\with space\\b.exe\" \\\\\\\"\""); |
| 162 EXPECT_EQ(2u, tokenized.size()); |
| 163 EXPECT_EQ(L"C:\\with space\\b.exe", tokenized[0]); |
| 164 EXPECT_EQ(L"\\\"", tokenized[1]); |
| 165 } |
| 166 |
| 167 // Test cases from |
| 168 // https://blogs.msdn.microsoft.com/oldnewthing/20100917-00/?p=12833. |
| 169 TEST(InstallStaticTest, SpacesAndQuotesOldNewThing) { |
| 170 std::vector<std::wstring> tokenized; |
| 171 |
| 172 tokenized = TokenizeCommandLineToArray(L"program.exe \"hello there.txt\""); |
| 173 ASSERT_EQ(2u, tokenized.size()); |
| 174 EXPECT_EQ(L"program.exe", tokenized[0]); |
| 175 EXPECT_EQ(L"hello there.txt", tokenized[1]); |
| 176 |
| 177 tokenized = |
| 178 TokenizeCommandLineToArray(L"program.exe \"C:\\Hello there.txt\""); |
| 179 ASSERT_EQ(2u, tokenized.size()); |
| 180 EXPECT_EQ(L"program.exe", tokenized[0]); |
| 181 EXPECT_EQ(L"C:\\Hello there.txt", tokenized[1]); |
| 182 |
| 183 tokenized = |
| 184 TokenizeCommandLineToArray(L"program.exe \"hello\\\"there\""); |
| 185 ASSERT_EQ(2u, tokenized.size()); |
| 186 EXPECT_EQ(L"program.exe", tokenized[0]); |
| 187 EXPECT_EQ(L"hello\"there", tokenized[1]); |
| 188 |
| 189 tokenized = |
| 190 TokenizeCommandLineToArray(L"program.exe \"hello\\\\\""); |
| 191 ASSERT_EQ(2u, tokenized.size()); |
| 192 EXPECT_EQ(L"program.exe", tokenized[0]); |
| 193 EXPECT_EQ(L"hello\\", tokenized[1]); |
| 194 } |
| 195 |
| 196 // Test cases from |
| 197 // http://www.windowsinspired.com/how-a-windows-programs-splits-its-command-line
-into-individual-arguments/. |
| 198 // These are mostly about the special handling of argv[0], which uses different |
| 199 // quoting than the rest of the arguments. |
| 200 TEST(InstallStaticTest, SpacesAndQuotesWindowsInspired) { |
| 201 std::vector<std::wstring> tokenized; |
| 202 |
| 203 tokenized = TokenizeCommandLineToArray( |
| 204 L"\"They said \"you can't do this!\", didn't they?\""); |
| 205 ASSERT_EQ(5u, tokenized.size()); |
| 206 EXPECT_EQ(L"They said ", tokenized[0]); |
| 207 EXPECT_EQ(L"you", tokenized[1]); |
| 208 EXPECT_EQ(L"can't", tokenized[2]); |
| 209 EXPECT_EQ(L"do", tokenized[3]); |
| 210 EXPECT_EQ(L"this!, didn't they?", tokenized[4]); |
| 211 |
| 212 tokenized = TokenizeCommandLineToArray( |
| 213 L"test.exe \"c:\\Path With Spaces\\Ending In Backslash\\\" Arg2 Arg3"); |
| 214 ASSERT_EQ(2u, tokenized.size()); |
| 215 EXPECT_EQ(L"test.exe", tokenized[0]); |
| 216 EXPECT_EQ(L"c:\\Path With Spaces\\Ending In Backslash\" Arg2 Arg3", |
| 217 tokenized[1]); |
| 218 |
| 219 tokenized = TokenizeCommandLineToArray( |
| 220 L"FinalProgram.exe \"first second \"\"embedded quote\"\" third\""); |
| 221 ASSERT_EQ(4u, tokenized.size()); |
| 222 EXPECT_EQ(L"FinalProgram.exe", tokenized[0]); |
| 223 EXPECT_EQ(L"first second \"embedded", tokenized[1]); |
| 224 EXPECT_EQ(L"quote", tokenized[2]); |
| 225 EXPECT_EQ(L"third", tokenized[3]); |
| 226 |
| 227 tokenized = TokenizeCommandLineToArray( |
| 228 L"\"F\"i\"r\"s\"t S\"e\"c\"o\"n\"d\" T\"h\"i\"r\"d\""); |
| 229 ASSERT_EQ(2u, tokenized.size()); |
| 230 EXPECT_EQ(L"F", tokenized[0]); |
| 231 EXPECT_EQ(L"irst Second Third", tokenized[1]); |
| 232 |
| 233 tokenized = TokenizeCommandLineToArray(L"F\"\"ir\"s\"\"t \\\"Second Third\""); |
| 234 ASSERT_EQ(3u, tokenized.size()); |
| 235 EXPECT_EQ(L"F\"\"ir\"s\"\"t", tokenized[0]); |
| 236 EXPECT_EQ(L"\"Second", tokenized[1]); |
| 237 EXPECT_EQ(L"Third", tokenized[2]); |
| 238 |
| 239 tokenized = TokenizeCommandLineToArray(L" Something Else"); |
| 240 ASSERT_EQ(3u, tokenized.size()); |
| 241 EXPECT_EQ(L"", tokenized[0]); |
| 242 EXPECT_EQ(L"Something", tokenized[1]); |
| 243 EXPECT_EQ(L"Else", tokenized[2]); |
| 244 |
| 245 tokenized = TokenizeCommandLineToArray(L" Something Else"); |
| 246 ASSERT_EQ(3u, tokenized.size()); |
| 247 EXPECT_EQ(L"", tokenized[0]); |
| 248 EXPECT_EQ(L"Something", tokenized[1]); |
| 249 EXPECT_EQ(L"Else", tokenized[2]); |
| 250 |
| 251 tokenized = TokenizeCommandLineToArray(L"\"123 456\tabc\\def\"ghi"); |
| 252 ASSERT_EQ(2u, tokenized.size()); |
| 253 EXPECT_EQ(L"123 456\tabc\\def", tokenized[0]); |
| 254 EXPECT_EQ(L"ghi", tokenized[1]); |
| 255 |
| 256 tokenized = TokenizeCommandLineToArray(L"123\"456\"\tabc"); |
| 257 ASSERT_EQ(2u, tokenized.size()); |
| 258 EXPECT_EQ(L"123\"456\"", tokenized[0]); |
| 259 EXPECT_EQ(L"abc", tokenized[1]); |
103 } | 260 } |
104 | 261 |
105 TEST(InstallStaticTest, BrowserProcessTest) { | 262 TEST(InstallStaticTest, BrowserProcessTest) { |
106 EXPECT_EQ(ProcessType::UNINITIALIZED, g_process_type); | 263 EXPECT_EQ(ProcessType::UNINITIALIZED, g_process_type); |
107 InitializeProcessType(); | 264 InitializeProcessType(); |
108 EXPECT_FALSE(IsNonBrowserProcess()); | 265 EXPECT_FALSE(IsNonBrowserProcess()); |
109 } | 266 } |
110 | 267 |
111 class InstallStaticUtilTest | 268 class InstallStaticUtilTest |
112 : public ::testing::TestWithParam< | 269 : public ::testing::TestWithParam< |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 #else // GOOGLE_CHROME_BUILD | 450 #else // GOOGLE_CHROME_BUILD |
294 // Chromium supports multi-install at user and system levels. | 451 // Chromium supports multi-install at user and system levels. |
295 INSTANTIATE_TEST_CASE_P(Chromium, | 452 INSTANTIATE_TEST_CASE_P(Chromium, |
296 InstallStaticUtilTest, | 453 InstallStaticUtilTest, |
297 testing::Combine(testing::Values(CHROMIUM_INDEX), | 454 testing::Combine(testing::Values(CHROMIUM_INDEX), |
298 testing::Values("user", "system"), | 455 testing::Values("user", "system"), |
299 testing::Values("single", "multi"))); | 456 testing::Values("single", "multi"))); |
300 #endif // !GOOGLE_CHROME_BUILD | 457 #endif // !GOOGLE_CHROME_BUILD |
301 | 458 |
302 } // namespace install_static | 459 } // namespace install_static |
OLD | NEW |