1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/command_line.h"
6 
7 #include <memory>
8 #include <string>
9 #include <vector>
10 
11 #include "base/files/file_path.h"
12 #include "base/stl_util.h"
13 #include "base/strings/strcat.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "build/build_config.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 
19 namespace base {
20 
21 // To test Windows quoting behavior, we use a string that has some backslashes
22 // and quotes.
23 // Consider the command-line argument: q\"bs1\bs2\\bs3q\\\"
24 // Here it is with C-style escapes.
25 static const CommandLine::StringType kTrickyQuoted =
26     FILE_PATH_LITERAL("q\\\"bs1\\bs2\\\\bs3q\\\\\\\"");
27 // It should be parsed by Windows as: q"bs1\bs2\\bs3q\"
28 // Here that is with C-style escapes.
29 static const CommandLine::StringType kTricky =
30     FILE_PATH_LITERAL("q\"bs1\\bs2\\\\bs3q\\\"");
31 
TEST(CommandLineTest,CommandLineConstructor)32 TEST(CommandLineTest, CommandLineConstructor) {
33   const CommandLine::CharType* argv[] = {
34       FILE_PATH_LITERAL("program"),
35       FILE_PATH_LITERAL("--foo="),
36       FILE_PATH_LITERAL("-bAr"),
37       FILE_PATH_LITERAL("-spaetzel=pierogi"),
38       FILE_PATH_LITERAL("-baz"),
39       FILE_PATH_LITERAL("flim"),
40       FILE_PATH_LITERAL("--other-switches=--dog=canine --cat=feline"),
41       FILE_PATH_LITERAL("-spaetzle=Crepe"),
42       FILE_PATH_LITERAL("-=loosevalue"),
43       FILE_PATH_LITERAL("-"),
44       FILE_PATH_LITERAL("FLAN"),
45       FILE_PATH_LITERAL("a"),
46       FILE_PATH_LITERAL("--input-translation=45--output-rotation"),
47       FILE_PATH_LITERAL("--"),
48       FILE_PATH_LITERAL("--"),
49       FILE_PATH_LITERAL("--not-a-switch"),
50       FILE_PATH_LITERAL("\"in the time of submarines...\""),
51       FILE_PATH_LITERAL("unquoted arg-with-space")};
52   CommandLine cl(size(argv), argv);
53 
54   EXPECT_FALSE(cl.GetCommandLineString().empty());
55   EXPECT_FALSE(cl.HasSwitch("cruller"));
56   EXPECT_FALSE(cl.HasSwitch("flim"));
57   EXPECT_FALSE(cl.HasSwitch("program"));
58   EXPECT_FALSE(cl.HasSwitch("dog"));
59   EXPECT_FALSE(cl.HasSwitch("cat"));
60   EXPECT_FALSE(cl.HasSwitch("output-rotation"));
61   EXPECT_FALSE(cl.HasSwitch("not-a-switch"));
62   EXPECT_FALSE(cl.HasSwitch("--"));
63 
64   EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(),
65             cl.GetProgram().value());
66 
67   EXPECT_TRUE(cl.HasSwitch("foo"));
68 #if defined(OS_WIN)
69   EXPECT_TRUE(cl.HasSwitch("bar"));
70 #else
71   EXPECT_FALSE(cl.HasSwitch("bar"));
72 #endif
73   EXPECT_TRUE(cl.HasSwitch("baz"));
74   EXPECT_TRUE(cl.HasSwitch("spaetzle"));
75   EXPECT_TRUE(cl.HasSwitch("other-switches"));
76   EXPECT_TRUE(cl.HasSwitch("input-translation"));
77 
78   EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle"));
79   EXPECT_EQ("", cl.GetSwitchValueASCII("foo"));
80   EXPECT_EQ("", cl.GetSwitchValueASCII("bar"));
81   EXPECT_EQ("", cl.GetSwitchValueASCII("cruller"));
82   EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII(
83       "other-switches"));
84   EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation"));
85 
86   const CommandLine::StringVector& args = cl.GetArgs();
87   ASSERT_EQ(8U, args.size());
88 
89   auto iter = args.begin();
90   EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
91   ++iter;
92   EXPECT_EQ(FILE_PATH_LITERAL("-"), *iter);
93   ++iter;
94   EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter);
95   ++iter;
96   EXPECT_EQ(FILE_PATH_LITERAL("a"), *iter);
97   ++iter;
98   EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter);
99   ++iter;
100   EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter);
101   ++iter;
102   EXPECT_EQ(FILE_PATH_LITERAL("\"in the time of submarines...\""), *iter);
103   ++iter;
104   EXPECT_EQ(FILE_PATH_LITERAL("unquoted arg-with-space"), *iter);
105   ++iter;
106   EXPECT_TRUE(iter == args.end());
107 }
108 
TEST(CommandLineTest,CommandLineFromString)109 TEST(CommandLineTest, CommandLineFromString) {
110 #if defined(OS_WIN)
111   CommandLine cl = CommandLine::FromString(
112       L"program --foo= -bAr  /Spaetzel=pierogi /Baz flim "
113       L"--other-switches=\"--dog=canine --cat=feline\" "
114       L"-spaetzle=Crepe   -=loosevalue  FLAN "
115       L"--input-translation=\"45\"--output-rotation "
116       L"--quotes=" +
117       kTrickyQuoted +
118       L" -- -- --not-a-switch \"in the time of submarines...\"");
119 
120   EXPECT_FALSE(cl.GetCommandLineString().empty());
121   EXPECT_FALSE(cl.HasSwitch("cruller"));
122   EXPECT_FALSE(cl.HasSwitch("flim"));
123   EXPECT_FALSE(cl.HasSwitch("program"));
124   EXPECT_FALSE(cl.HasSwitch("dog"));
125   EXPECT_FALSE(cl.HasSwitch("cat"));
126   EXPECT_FALSE(cl.HasSwitch("output-rotation"));
127   EXPECT_FALSE(cl.HasSwitch("not-a-switch"));
128   EXPECT_FALSE(cl.HasSwitch("--"));
129 
130   EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(),
131             cl.GetProgram().value());
132 
133   EXPECT_TRUE(cl.HasSwitch("foo"));
134   EXPECT_TRUE(cl.HasSwitch("bar"));
135   EXPECT_TRUE(cl.HasSwitch("baz"));
136   EXPECT_TRUE(cl.HasSwitch("spaetzle"));
137   EXPECT_TRUE(cl.HasSwitch("other-switches"));
138   EXPECT_TRUE(cl.HasSwitch("input-translation"));
139   EXPECT_TRUE(cl.HasSwitch("quotes"));
140 
141   EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle"));
142   EXPECT_EQ("", cl.GetSwitchValueASCII("foo"));
143   EXPECT_EQ("", cl.GetSwitchValueASCII("bar"));
144   EXPECT_EQ("", cl.GetSwitchValueASCII("cruller"));
145   EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII(
146       "other-switches"));
147   EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation"));
148   EXPECT_EQ(kTricky, cl.GetSwitchValueNative("quotes"));
149 
150   const CommandLine::StringVector& args = cl.GetArgs();
151   ASSERT_EQ(5U, args.size());
152 
153   std::vector<CommandLine::StringType>::const_iterator iter = args.begin();
154   EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
155   ++iter;
156   EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter);
157   ++iter;
158   EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter);
159   ++iter;
160   EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter);
161   ++iter;
162   EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *iter);
163   ++iter;
164   EXPECT_TRUE(iter == args.end());
165 
166   // Check that a generated string produces an equivalent command line.
167   CommandLine cl_duplicate = CommandLine::FromString(cl.GetCommandLineString());
168   EXPECT_EQ(cl.GetCommandLineString(), cl_duplicate.GetCommandLineString());
169 #endif
170 }
171 
172 // Tests behavior with an empty input string.
TEST(CommandLineTest,EmptyString)173 TEST(CommandLineTest, EmptyString) {
174 #if defined(OS_WIN)
175   CommandLine cl_from_string = CommandLine::FromString(std::wstring());
176   EXPECT_TRUE(cl_from_string.GetCommandLineString().empty());
177   EXPECT_TRUE(cl_from_string.GetProgram().empty());
178   EXPECT_EQ(1U, cl_from_string.argv().size());
179   EXPECT_TRUE(cl_from_string.GetArgs().empty());
180 #endif
181   CommandLine cl_from_argv(0, nullptr);
182   EXPECT_TRUE(cl_from_argv.GetCommandLineString().empty());
183   EXPECT_TRUE(cl_from_argv.GetProgram().empty());
184   EXPECT_EQ(1U, cl_from_argv.argv().size());
185   EXPECT_TRUE(cl_from_argv.GetArgs().empty());
186 }
187 
TEST(CommandLineTest,GetArgumentsString)188 TEST(CommandLineTest, GetArgumentsString) {
189   static const FilePath::CharType kPath1[] =
190       FILE_PATH_LITERAL("C:\\Some File\\With Spaces.ggg");
191   static const FilePath::CharType kPath2[] =
192       FILE_PATH_LITERAL("C:\\no\\spaces.ggg");
193 
194   static const char kFirstArgName[] = "first-arg";
195   static const char kSecondArgName[] = "arg2";
196   static const char kThirdArgName[] = "arg with space";
197   static const char kFourthArgName[] = "nospace";
198   static const char kFifthArgName[] = "%1";
199 
200   CommandLine cl(CommandLine::NO_PROGRAM);
201   cl.AppendSwitchPath(kFirstArgName, FilePath(kPath1));
202   cl.AppendSwitchPath(kSecondArgName, FilePath(kPath2));
203   cl.AppendArg(kThirdArgName);
204   cl.AppendArg(kFourthArgName);
205   cl.AppendArg(kFifthArgName);
206 
207 #if defined(OS_WIN)
208   CommandLine::StringType expected_first_arg(UTF8ToWide(kFirstArgName));
209   CommandLine::StringType expected_second_arg(UTF8ToWide(kSecondArgName));
210   CommandLine::StringType expected_third_arg(UTF8ToWide(kThirdArgName));
211   CommandLine::StringType expected_fourth_arg(UTF8ToWide(kFourthArgName));
212   CommandLine::StringType expected_fifth_arg(UTF8ToWide(kFifthArgName));
213 #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
214   CommandLine::StringType expected_first_arg(kFirstArgName);
215   CommandLine::StringType expected_second_arg(kSecondArgName);
216   CommandLine::StringType expected_third_arg(kThirdArgName);
217   CommandLine::StringType expected_fourth_arg(kFourthArgName);
218   CommandLine::StringType expected_fifth_arg(kFifthArgName);
219 #endif
220 
221 #if defined(OS_WIN)
222 #define QUOTE_ON_WIN FILE_PATH_LITERAL("\"")
223 #else
224 #define QUOTE_ON_WIN FILE_PATH_LITERAL("")
225 #endif  // OS_WIN
226 
227   CommandLine::StringType expected_str;
228   expected_str.append(FILE_PATH_LITERAL("--"))
229               .append(expected_first_arg)
230               .append(FILE_PATH_LITERAL("="))
231               .append(QUOTE_ON_WIN)
232               .append(kPath1)
233               .append(QUOTE_ON_WIN)
234               .append(FILE_PATH_LITERAL(" "))
235               .append(FILE_PATH_LITERAL("--"))
236               .append(expected_second_arg)
237               .append(FILE_PATH_LITERAL("="))
238               .append(QUOTE_ON_WIN)
239               .append(kPath2)
240               .append(QUOTE_ON_WIN)
241               .append(FILE_PATH_LITERAL(" "))
242               .append(QUOTE_ON_WIN)
243               .append(expected_third_arg)
244               .append(QUOTE_ON_WIN)
245               .append(FILE_PATH_LITERAL(" "))
246               .append(expected_fourth_arg)
247               .append(FILE_PATH_LITERAL(" "));
248 
249   CommandLine::StringType expected_str_no_quote_placeholders(expected_str);
250   expected_str_no_quote_placeholders.append(expected_fifth_arg);
251   EXPECT_EQ(expected_str_no_quote_placeholders, cl.GetArgumentsString());
252 
253 #if defined(OS_WIN)
254   CommandLine::StringType expected_str_quote_placeholders(expected_str);
255   expected_str_quote_placeholders.append(QUOTE_ON_WIN)
256                                  .append(expected_fifth_arg)
257                                  .append(QUOTE_ON_WIN);
258   EXPECT_EQ(expected_str_quote_placeholders,
259             cl.GetArgumentsStringWithPlaceholders());
260 #endif
261 }
262 
263 // Test methods for appending switches to a command line.
TEST(CommandLineTest,AppendSwitches)264 TEST(CommandLineTest, AppendSwitches) {
265   std::string switch1 = "switch1";
266   std::string switch2 = "switch2";
267   std::string value2 = "value";
268   std::string switch3 = "switch3";
269   std::string value3 = "a value with spaces";
270   std::string switch4 = "switch4";
271   std::string value4 = "\"a value with quotes\"";
272   std::string switch5 = "quotes";
273   CommandLine::StringType value5 = kTricky;
274 
275   CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
276 
277   cl.AppendSwitch(switch1);
278   cl.AppendSwitchASCII(switch2, value2);
279   cl.AppendSwitchASCII(switch3, value3);
280   cl.AppendSwitchASCII(switch4, value4);
281   cl.AppendSwitchASCII(switch5, value4);
282   cl.AppendSwitchNative(switch5, value5);
283 
284   EXPECT_TRUE(cl.HasSwitch(switch1));
285   EXPECT_TRUE(cl.HasSwitch(switch2));
286   EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2));
287   EXPECT_TRUE(cl.HasSwitch(switch3));
288   EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3));
289   EXPECT_TRUE(cl.HasSwitch(switch4));
290   EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4));
291   EXPECT_TRUE(cl.HasSwitch(switch5));
292   EXPECT_EQ(value5, cl.GetSwitchValueNative(switch5));
293 
294 #if defined(OS_WIN)
295   EXPECT_EQ(
296       L"Program "
297       L"--switch1 "
298       L"--switch2=value "
299       L"--switch3=\"a value with spaces\" "
300       L"--switch4=\"\\\"a value with quotes\\\"\" "
301       // Even though the switches are unique, appending can add repeat
302       // switches to argv.
303       L"--quotes=\"\\\"a value with quotes\\\"\" "
304       L"--quotes=\"" +
305           kTrickyQuoted + L"\"",
306       cl.GetCommandLineString());
307 #endif
308 }
309 
TEST(CommandLineTest,AppendSwitchesDashDash)310 TEST(CommandLineTest, AppendSwitchesDashDash) {
311  const CommandLine::CharType* raw_argv[] = { FILE_PATH_LITERAL("prog"),
312                                              FILE_PATH_LITERAL("--"),
313                                              FILE_PATH_LITERAL("--arg1") };
314  CommandLine cl(size(raw_argv), raw_argv);
315 
316  cl.AppendSwitch("switch1");
317  cl.AppendSwitchASCII("switch2", "foo");
318 
319  cl.AppendArg("--arg2");
320 
321  EXPECT_EQ(FILE_PATH_LITERAL("prog --switch1 --switch2=foo -- --arg1 --arg2"),
322            cl.GetCommandLineString());
323  CommandLine::StringVector cl_argv = cl.argv();
324  EXPECT_EQ(FILE_PATH_LITERAL("prog"), cl_argv[0]);
325  EXPECT_EQ(FILE_PATH_LITERAL("--switch1"), cl_argv[1]);
326  EXPECT_EQ(FILE_PATH_LITERAL("--switch2=foo"), cl_argv[2]);
327  EXPECT_EQ(FILE_PATH_LITERAL("--"), cl_argv[3]);
328  EXPECT_EQ(FILE_PATH_LITERAL("--arg1"), cl_argv[4]);
329  EXPECT_EQ(FILE_PATH_LITERAL("--arg2"), cl_argv[5]);
330 }
331 
332 // Tests that when AppendArguments is called that the program is set correctly
333 // on the target CommandLine object and the switches from the source
334 // CommandLine are added to the target.
TEST(CommandLineTest,AppendArguments)335 TEST(CommandLineTest, AppendArguments) {
336   CommandLine cl1(FilePath(FILE_PATH_LITERAL("Program")));
337   cl1.AppendSwitch("switch1");
338   cl1.AppendSwitchASCII("switch2", "foo");
339 
340   CommandLine cl2(CommandLine::NO_PROGRAM);
341   cl2.AppendArguments(cl1, true);
342   EXPECT_EQ(cl1.GetProgram().value(), cl2.GetProgram().value());
343   EXPECT_EQ(cl1.GetCommandLineString(), cl2.GetCommandLineString());
344 
345   CommandLine c1(FilePath(FILE_PATH_LITERAL("Program1")));
346   c1.AppendSwitch("switch1");
347   CommandLine c2(FilePath(FILE_PATH_LITERAL("Program2")));
348   c2.AppendSwitch("switch2");
349 
350   c1.AppendArguments(c2, true);
351   EXPECT_EQ(c1.GetProgram().value(), c2.GetProgram().value());
352   EXPECT_TRUE(c1.HasSwitch("switch1"));
353   EXPECT_TRUE(c1.HasSwitch("switch2"));
354 }
355 
356 #if defined(OS_WIN)
357 // Make sure that the command line string program paths are quoted as necessary.
358 // This only makes sense on Windows and the test is basically here to guard
359 // against regressions.
TEST(CommandLineTest,ProgramQuotes)360 TEST(CommandLineTest, ProgramQuotes) {
361   // Check that quotes are not added for paths without spaces.
362   const FilePath kProgram(L"Program");
363   CommandLine cl_program(kProgram);
364   EXPECT_EQ(kProgram.value(), cl_program.GetProgram().value());
365   EXPECT_EQ(kProgram.value(), cl_program.GetCommandLineString());
366 
367   const FilePath kProgramPath(L"Program Path");
368 
369   // Check that quotes are not returned from GetProgram().
370   CommandLine cl_program_path(kProgramPath);
371   EXPECT_EQ(kProgramPath.value(), cl_program_path.GetProgram().value());
372 
373   // Check that quotes are added to command line string paths containing spaces.
374   CommandLine::StringType cmd_string(cl_program_path.GetCommandLineString());
375   EXPECT_EQ(L"\"Program Path\"", cmd_string);
376 
377   // Check the optional quoting of placeholders in programs.
378   CommandLine cl_quote_placeholder(FilePath(L"%1"));
379   EXPECT_EQ(L"%1", cl_quote_placeholder.GetCommandLineString());
380   EXPECT_EQ(L"\"%1\"",
381             cl_quote_placeholder.GetCommandLineStringWithPlaceholders());
382 }
383 #endif
384 
385 // Calling Init multiple times should not modify the previous CommandLine.
TEST(CommandLineTest,Init)386 TEST(CommandLineTest, Init) {
387   // Call Init without checking output once so we know it's been called
388   // whether or not the test runner does so.
389   CommandLine::Init(0, nullptr);
390   CommandLine* initial = CommandLine::ForCurrentProcess();
391   EXPECT_FALSE(CommandLine::Init(0, nullptr));
392   CommandLine* current = CommandLine::ForCurrentProcess();
393   EXPECT_EQ(initial, current);
394 }
395 
396 // Test that copies of CommandLine have a valid StringPiece map.
TEST(CommandLineTest,Copy)397 TEST(CommandLineTest, Copy) {
398   std::unique_ptr<CommandLine> initial(
399       new CommandLine(CommandLine::NO_PROGRAM));
400   initial->AppendSwitch("a");
401   initial->AppendSwitch("bbbbbbbbbbbbbbb");
402   initial->AppendSwitch("c");
403   CommandLine copy_constructed(*initial);
404   CommandLine assigned = *initial;
405   CommandLine::SwitchMap switch_map = initial->GetSwitches();
406   initial.reset();
407   for (const auto& pair : switch_map)
408     EXPECT_TRUE(copy_constructed.HasSwitch(pair.first));
409   for (const auto& pair : switch_map)
410     EXPECT_TRUE(assigned.HasSwitch(pair.first));
411 }
412 
TEST(CommandLineTest,PrependSimpleWrapper)413 TEST(CommandLineTest, PrependSimpleWrapper) {
414   CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
415   cl.AppendSwitch("a");
416   cl.AppendSwitch("b");
417   cl.PrependWrapper(FILE_PATH_LITERAL("wrapper --foo --bar"));
418 
419   EXPECT_EQ(6u, cl.argv().size());
420   EXPECT_EQ(FILE_PATH_LITERAL("wrapper"), cl.argv()[0]);
421   EXPECT_EQ(FILE_PATH_LITERAL("--foo"), cl.argv()[1]);
422   EXPECT_EQ(FILE_PATH_LITERAL("--bar"), cl.argv()[2]);
423   EXPECT_EQ(FILE_PATH_LITERAL("Program"), cl.argv()[3]);
424   EXPECT_EQ(FILE_PATH_LITERAL("--a"), cl.argv()[4]);
425   EXPECT_EQ(FILE_PATH_LITERAL("--b"), cl.argv()[5]);
426 }
427 
TEST(CommandLineTest,PrependComplexWrapper)428 TEST(CommandLineTest, PrependComplexWrapper) {
429   CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
430   cl.AppendSwitch("a");
431   cl.AppendSwitch("b");
432   cl.PrependWrapper(
433       FILE_PATH_LITERAL("wrapper --foo='hello world' --bar=\"let's go\""));
434 
435   EXPECT_EQ(6u, cl.argv().size());
436   EXPECT_EQ(FILE_PATH_LITERAL("wrapper"), cl.argv()[0]);
437   EXPECT_EQ(FILE_PATH_LITERAL("--foo='hello world'"), cl.argv()[1]);
438   EXPECT_EQ(FILE_PATH_LITERAL("--bar=\"let's go\""), cl.argv()[2]);
439   EXPECT_EQ(FILE_PATH_LITERAL("Program"), cl.argv()[3]);
440   EXPECT_EQ(FILE_PATH_LITERAL("--a"), cl.argv()[4]);
441   EXPECT_EQ(FILE_PATH_LITERAL("--b"), cl.argv()[5]);
442 }
443 
TEST(CommandLineTest,RemoveSwitch)444 TEST(CommandLineTest, RemoveSwitch) {
445   const std::string switch1 = "switch1";
446   const std::string switch2 = "switch2";
447   const std::string value2 = "value";
448 
449   CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
450 
451   cl.AppendSwitch(switch1);
452   cl.AppendSwitchASCII(switch2, value2);
453 
454   EXPECT_TRUE(cl.HasSwitch(switch1));
455   EXPECT_TRUE(cl.HasSwitch(switch2));
456   EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2));
457   EXPECT_THAT(cl.argv(),
458               testing::ElementsAre(FILE_PATH_LITERAL("Program"),
459                                    FILE_PATH_LITERAL("--switch1"),
460                                    FILE_PATH_LITERAL("--switch2=value")));
461 
462   cl.RemoveSwitch(switch1);
463 
464   EXPECT_FALSE(cl.HasSwitch(switch1));
465   EXPECT_TRUE(cl.HasSwitch(switch2));
466   EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2));
467   EXPECT_THAT(cl.argv(),
468               testing::ElementsAre(FILE_PATH_LITERAL("Program"),
469                                    FILE_PATH_LITERAL("--switch2=value")));
470 }
471 
TEST(CommandLineTest,RemoveSwitchWithValue)472 TEST(CommandLineTest, RemoveSwitchWithValue) {
473   const std::string switch1 = "switch1";
474   const std::string switch2 = "switch2";
475   const std::string value2 = "value";
476 
477   CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
478 
479   cl.AppendSwitch(switch1);
480   cl.AppendSwitchASCII(switch2, value2);
481 
482   EXPECT_TRUE(cl.HasSwitch(switch1));
483   EXPECT_TRUE(cl.HasSwitch(switch2));
484   EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2));
485   EXPECT_THAT(cl.argv(),
486               testing::ElementsAre(FILE_PATH_LITERAL("Program"),
487                                    FILE_PATH_LITERAL("--switch1"),
488                                    FILE_PATH_LITERAL("--switch2=value")));
489 
490   cl.RemoveSwitch(switch2);
491 
492   EXPECT_TRUE(cl.HasSwitch(switch1));
493   EXPECT_FALSE(cl.HasSwitch(switch2));
494   EXPECT_THAT(cl.argv(), testing::ElementsAre(FILE_PATH_LITERAL("Program"),
495                                               FILE_PATH_LITERAL("--switch1")));
496 }
497 
TEST(CommandLineTest,RemoveSwitchDropsMultipleSameSwitches)498 TEST(CommandLineTest, RemoveSwitchDropsMultipleSameSwitches) {
499   const std::string switch1 = "switch1";
500   const std::string value2 = "value2";
501 
502   CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
503 
504   cl.AppendSwitch(switch1);
505   cl.AppendSwitchASCII(switch1, value2);
506 
507   EXPECT_TRUE(cl.HasSwitch(switch1));
508   EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch1));
509   EXPECT_THAT(cl.argv(),
510               testing::ElementsAre(FILE_PATH_LITERAL("Program"),
511                                    FILE_PATH_LITERAL("--switch1"),
512                                    FILE_PATH_LITERAL("--switch1=value2")));
513 
514   cl.RemoveSwitch(switch1);
515 
516   EXPECT_FALSE(cl.HasSwitch(switch1));
517   EXPECT_THAT(cl.argv(), testing::ElementsAre(FILE_PATH_LITERAL("Program")));
518 }
519 
TEST(CommandLineTest,AppendAndRemoveSwitchWithDefaultPrefix)520 TEST(CommandLineTest, AppendAndRemoveSwitchWithDefaultPrefix) {
521   CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
522 
523   cl.AppendSwitch("foo");
524   EXPECT_THAT(cl.argv(), testing::ElementsAre(FILE_PATH_LITERAL("Program"),
525                                               FILE_PATH_LITERAL("--foo")));
526   EXPECT_EQ(0u, cl.GetArgs().size());
527 
528   cl.RemoveSwitch("foo");
529   EXPECT_THAT(cl.argv(), testing::ElementsAre(FILE_PATH_LITERAL("Program")));
530   EXPECT_EQ(0u, cl.GetArgs().size());
531 }
532 
TEST(CommandLineTest,AppendAndRemoveSwitchWithAlternativePrefix)533 TEST(CommandLineTest, AppendAndRemoveSwitchWithAlternativePrefix) {
534   CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
535 
536   cl.AppendSwitch("-foo");
537   EXPECT_THAT(cl.argv(), testing::ElementsAre(FILE_PATH_LITERAL("Program"),
538                                               FILE_PATH_LITERAL("-foo")));
539   EXPECT_EQ(0u, cl.GetArgs().size());
540 
541   cl.RemoveSwitch("foo");
542   EXPECT_THAT(cl.argv(), testing::ElementsAre(FILE_PATH_LITERAL("Program")));
543   EXPECT_EQ(0u, cl.GetArgs().size());
544 }
545 
TEST(CommandLineTest,AppendAndRemoveSwitchPreservesOtherSwitchesAndArgs)546 TEST(CommandLineTest, AppendAndRemoveSwitchPreservesOtherSwitchesAndArgs) {
547   CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
548 
549   cl.AppendSwitch("foo");
550   cl.AppendSwitch("bar");
551   cl.AppendArg("arg");
552   EXPECT_THAT(cl.argv(), testing::ElementsAre(FILE_PATH_LITERAL("Program"),
553                                               FILE_PATH_LITERAL("--foo"),
554                                               FILE_PATH_LITERAL("--bar"),
555                                               FILE_PATH_LITERAL("arg")));
556   EXPECT_THAT(cl.GetArgs(), testing::ElementsAre(FILE_PATH_LITERAL("arg")));
557 
558   cl.RemoveSwitch("foo");
559   EXPECT_THAT(cl.argv(), testing::ElementsAre(FILE_PATH_LITERAL("Program"),
560                                               FILE_PATH_LITERAL("--bar"),
561                                               FILE_PATH_LITERAL("arg")));
562   EXPECT_THAT(cl.GetArgs(), testing::ElementsAre(FILE_PATH_LITERAL("arg")));
563 }
564 
TEST(CommandLineTest,MultipleSameSwitch)565 TEST(CommandLineTest, MultipleSameSwitch) {
566   const CommandLine::CharType* argv[] = {
567       FILE_PATH_LITERAL("program"),
568       FILE_PATH_LITERAL("--foo=one"),  // --foo first time
569       FILE_PATH_LITERAL("-baz"),
570       FILE_PATH_LITERAL("--foo=two")  // --foo second time
571   };
572   CommandLine cl(size(argv), argv);
573 
574   EXPECT_TRUE(cl.HasSwitch("foo"));
575   EXPECT_TRUE(cl.HasSwitch("baz"));
576 
577   EXPECT_EQ("two", cl.GetSwitchValueASCII("foo"));
578 }
579 
580 } // namespace base
581