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.
8 #include "base/basictypes.h"
9 #include "base/command_line.h"
10 #include "base/file_path.h"
11 #include "base/utf_string_conversions.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 // To test Windows quoting behavior, we use a string that has some backslashes
16 // Consider the command-line argument: q\"bs1\bs2\\bs3q\\\"
17 // Here it is with C-style escapes.
18 static const CommandLine::StringType kTrickyQuoted
=
19 FILE_PATH_LITERAL("q\\\"bs1\\bs2\\\\bs3q\\\\\\\"");
20 // It should be parsed by Windows as: q"bs1\bs2\\bs3q\"
21 // Here that is with C-style escapes.
22 static const CommandLine::StringType kTricky
=
23 FILE_PATH_LITERAL("q\"bs1\\bs2\\\\bs3q\\\"");
25 TEST(CommandLineTest
, CommandLineConstructor
) {
26 const CommandLine::CharType
* argv
[] = {
27 FILE_PATH_LITERAL("program"),
28 FILE_PATH_LITERAL("--foo="),
29 FILE_PATH_LITERAL("-bAr"),
30 FILE_PATH_LITERAL("-spaetzel=pierogi"),
31 FILE_PATH_LITERAL("-baz"),
32 FILE_PATH_LITERAL("flim"),
33 FILE_PATH_LITERAL("--other-switches=--dog=canine --cat=feline"),
34 FILE_PATH_LITERAL("-spaetzle=Crepe"),
35 FILE_PATH_LITERAL("-=loosevalue"),
36 FILE_PATH_LITERAL("FLAN"),
37 FILE_PATH_LITERAL("a"),
38 FILE_PATH_LITERAL("--input-translation=45--output-rotation"),
39 FILE_PATH_LITERAL("--"),
40 FILE_PATH_LITERAL("--"),
41 FILE_PATH_LITERAL("--not-a-switch"),
42 FILE_PATH_LITERAL("\"in the time of submarines...\""),
43 FILE_PATH_LITERAL("unquoted arg-with-space")};
44 CommandLine
cl(arraysize(argv
), argv
);
46 EXPECT_FALSE(cl
.GetCommandLineString().empty());
47 EXPECT_FALSE(cl
.HasSwitch("cruller"));
48 EXPECT_FALSE(cl
.HasSwitch("flim"));
49 EXPECT_FALSE(cl
.HasSwitch("program"));
50 EXPECT_FALSE(cl
.HasSwitch("dog"));
51 EXPECT_FALSE(cl
.HasSwitch("cat"));
52 EXPECT_FALSE(cl
.HasSwitch("output-rotation"));
53 EXPECT_FALSE(cl
.HasSwitch("not-a-switch"));
54 EXPECT_FALSE(cl
.HasSwitch("--"));
56 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(),
57 cl
.GetProgram().value());
59 EXPECT_TRUE(cl
.HasSwitch("foo"));
60 EXPECT_TRUE(cl
.HasSwitch("bAr"));
61 EXPECT_TRUE(cl
.HasSwitch("baz"));
62 EXPECT_TRUE(cl
.HasSwitch("spaetzle"));
64 EXPECT_TRUE(cl
.HasSwitch("SPAETZLE"));
66 EXPECT_TRUE(cl
.HasSwitch("other-switches"));
67 EXPECT_TRUE(cl
.HasSwitch("input-translation"));
69 EXPECT_EQ("Crepe", cl
.GetSwitchValueASCII("spaetzle"));
70 EXPECT_EQ("", cl
.GetSwitchValueASCII("Foo"));
71 EXPECT_EQ("", cl
.GetSwitchValueASCII("bar"));
72 EXPECT_EQ("", cl
.GetSwitchValueASCII("cruller"));
73 EXPECT_EQ("--dog=canine --cat=feline", cl
.GetSwitchValueASCII(
75 EXPECT_EQ("45--output-rotation", cl
.GetSwitchValueASCII("input-translation"));
77 const CommandLine::StringVector
& args
= cl
.GetArgs();
78 ASSERT_EQ(7U, args
.size());
80 std::vector
<CommandLine::StringType
>::const_iterator iter
= args
.begin();
81 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter
);
83 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter
);
85 EXPECT_EQ(FILE_PATH_LITERAL("a"), *iter
);
87 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter
);
89 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter
);
91 EXPECT_EQ(FILE_PATH_LITERAL("\"in the time of submarines...\""), *iter
);
93 EXPECT_EQ(FILE_PATH_LITERAL("unquoted arg-with-space"), *iter
);
95 EXPECT_TRUE(iter
== args
.end());
98 TEST(CommandLineTest
, CommandLineFromString
) {
100 CommandLine cl
= CommandLine::FromString(
101 L
"program --foo= -bAr /Spaetzel=pierogi /Baz flim "
102 L
"--other-switches=\"--dog=canine --cat=feline\" "
103 L
"-spaetzle=Crepe -=loosevalue FLAN "
104 L
"--input-translation=\"45\"--output-rotation "
105 L
"--quotes=" + kTrickyQuoted
+ L
" "
106 L
"-- -- --not-a-switch "
107 L
"\"in the time of submarines...\"");
109 EXPECT_FALSE(cl
.GetCommandLineString().empty());
110 EXPECT_FALSE(cl
.HasSwitch("cruller"));
111 EXPECT_FALSE(cl
.HasSwitch("flim"));
112 EXPECT_FALSE(cl
.HasSwitch("program"));
113 EXPECT_FALSE(cl
.HasSwitch("dog"));
114 EXPECT_FALSE(cl
.HasSwitch("cat"));
115 EXPECT_FALSE(cl
.HasSwitch("output-rotation"));
116 EXPECT_FALSE(cl
.HasSwitch("not-a-switch"));
117 EXPECT_FALSE(cl
.HasSwitch("--"));
119 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(),
120 cl
.GetProgram().value());
122 EXPECT_TRUE(cl
.HasSwitch("foo"));
123 EXPECT_TRUE(cl
.HasSwitch("bar"));
124 EXPECT_TRUE(cl
.HasSwitch("baz"));
125 EXPECT_TRUE(cl
.HasSwitch("spaetzle"));
126 EXPECT_TRUE(cl
.HasSwitch("SPAETZLE"));
127 EXPECT_TRUE(cl
.HasSwitch("other-switches"));
128 EXPECT_TRUE(cl
.HasSwitch("input-translation"));
129 EXPECT_TRUE(cl
.HasSwitch("quotes"));
131 EXPECT_EQ("Crepe", cl
.GetSwitchValueASCII("spaetzle"));
132 EXPECT_EQ("", cl
.GetSwitchValueASCII("Foo"));
133 EXPECT_EQ("", cl
.GetSwitchValueASCII("bar"));
134 EXPECT_EQ("", cl
.GetSwitchValueASCII("cruller"));
135 EXPECT_EQ("--dog=canine --cat=feline", cl
.GetSwitchValueASCII(
137 EXPECT_EQ("45--output-rotation", cl
.GetSwitchValueASCII("input-translation"));
138 EXPECT_EQ(kTricky
, cl
.GetSwitchValueNative("quotes"));
140 const CommandLine::StringVector
& args
= cl
.GetArgs();
141 ASSERT_EQ(5U, args
.size());
143 std::vector
<CommandLine::StringType
>::const_iterator iter
= args
.begin();
144 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter
);
146 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter
);
148 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter
);
150 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter
);
152 EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *iter
);
154 EXPECT_TRUE(iter
== args
.end());
156 // Check that a generated string produces an equivalent command line.
157 CommandLine cl_duplicate
= CommandLine::FromString(cl
.GetCommandLineString());
158 EXPECT_EQ(cl
.GetCommandLineString(), cl_duplicate
.GetCommandLineString());
162 // Tests behavior with an empty input string.
163 TEST(CommandLineTest
, EmptyString
) {
165 CommandLine cl_from_string
= CommandLine::FromString(L
"");
166 EXPECT_TRUE(cl_from_string
.GetCommandLineString().empty());
167 EXPECT_TRUE(cl_from_string
.GetProgram().empty());
168 EXPECT_EQ(1U, cl_from_string
.argv().size());
169 EXPECT_TRUE(cl_from_string
.GetArgs().empty());
171 CommandLine
cl_from_argv(0, NULL
);
172 EXPECT_TRUE(cl_from_argv
.GetCommandLineString().empty());
173 EXPECT_TRUE(cl_from_argv
.GetProgram().empty());
174 EXPECT_EQ(1U, cl_from_argv
.argv().size());
175 EXPECT_TRUE(cl_from_argv
.GetArgs().empty());
178 // Test methods for appending switches to a command line.
179 TEST(CommandLineTest
, AppendSwitches
) {
180 std::string switch1
= "switch1";
181 std::string switch2
= "switch2";
182 std::string value2
= "value";
183 std::string switch3
= "switch3";
184 std::string value3
= "a value with spaces";
185 std::string switch4
= "switch4";
186 std::string value4
= "\"a value with quotes\"";
187 std::string switch5
= "quotes";
188 CommandLine::StringType value5
= kTricky
;
190 CommandLine
cl(FilePath(FILE_PATH_LITERAL("Program")));
192 cl
.AppendSwitch(switch1
);
193 cl
.AppendSwitchASCII(switch2
, value2
);
194 cl
.AppendSwitchASCII(switch3
, value3
);
195 cl
.AppendSwitchASCII(switch4
, value4
);
196 cl
.AppendSwitchNative(switch5
, value5
);
198 EXPECT_TRUE(cl
.HasSwitch(switch1
));
199 EXPECT_TRUE(cl
.HasSwitch(switch2
));
200 EXPECT_EQ(value2
, cl
.GetSwitchValueASCII(switch2
));
201 EXPECT_TRUE(cl
.HasSwitch(switch3
));
202 EXPECT_EQ(value3
, cl
.GetSwitchValueASCII(switch3
));
203 EXPECT_TRUE(cl
.HasSwitch(switch4
));
204 EXPECT_EQ(value4
, cl
.GetSwitchValueASCII(switch4
));
205 EXPECT_TRUE(cl
.HasSwitch(switch5
));
206 EXPECT_EQ(value5
, cl
.GetSwitchValueNative(switch5
));
209 EXPECT_EQ(L
"Program "
212 L
"--switch3=\"a value with spaces\" "
213 L
"--switch4=\"\\\"a value with quotes\\\"\" "
214 L
"--quotes=\"" + kTrickyQuoted
+ L
"\"",
215 cl
.GetCommandLineString());
219 TEST(CommandLineTest
, AppendSwitchesDashDash
) {
220 const CommandLine::CharType
* raw_argv
[] = { FILE_PATH_LITERAL("prog"),
221 FILE_PATH_LITERAL("--"),
222 FILE_PATH_LITERAL("--arg1") };
223 CommandLine
cl(arraysize(raw_argv
), raw_argv
);
225 cl
.AppendSwitch("switch1");
226 cl
.AppendSwitchASCII("switch2", "foo");
228 cl
.AppendArg("--arg2");
230 EXPECT_EQ(FILE_PATH_LITERAL("prog --switch1 --switch2=foo -- --arg1 --arg2"),
231 cl
.GetCommandLineString());
232 CommandLine::StringVector cl_argv
= cl
.argv();
233 EXPECT_EQ(FILE_PATH_LITERAL("prog"), cl_argv
[0]);
234 EXPECT_EQ(FILE_PATH_LITERAL("--switch1"), cl_argv
[1]);
235 EXPECT_EQ(FILE_PATH_LITERAL("--switch2=foo"), cl_argv
[2]);
236 EXPECT_EQ(FILE_PATH_LITERAL("--"), cl_argv
[3]);
237 EXPECT_EQ(FILE_PATH_LITERAL("--arg1"), cl_argv
[4]);
238 EXPECT_EQ(FILE_PATH_LITERAL("--arg2"), cl_argv
[5]);
241 // Tests that when AppendArguments is called that the program is set correctly
242 // on the target CommandLine object and the switches from the source
243 // CommandLine are added to the target.
244 TEST(CommandLineTest
, AppendArguments
) {
245 CommandLine
cl1(FilePath(FILE_PATH_LITERAL("Program")));
246 cl1
.AppendSwitch("switch1");
247 cl1
.AppendSwitchASCII("switch2", "foo");
249 CommandLine
cl2(CommandLine::NO_PROGRAM
);
250 cl2
.AppendArguments(cl1
, true);
251 EXPECT_EQ(cl1
.GetProgram().value(), cl2
.GetProgram().value());
252 EXPECT_EQ(cl1
.GetCommandLineString(), cl2
.GetCommandLineString());
254 CommandLine
c1(FilePath(FILE_PATH_LITERAL("Program1")));
255 c1
.AppendSwitch("switch1");
256 CommandLine
c2(FilePath(FILE_PATH_LITERAL("Program2")));
257 c2
.AppendSwitch("switch2");
259 c1
.AppendArguments(c2
, true);
260 EXPECT_EQ(c1
.GetProgram().value(), c2
.GetProgram().value());
261 EXPECT_TRUE(c1
.HasSwitch("switch1"));
262 EXPECT_TRUE(c1
.HasSwitch("switch2"));
266 // Make sure that the command line string program paths are quoted as necessary.
267 // This only makes sense on Windows and the test is basically here to guard
268 // against regressions.
269 TEST(CommandLineTest
, ProgramQuotes
) {
270 // Check that quotes are not added for paths without spaces.
271 const FilePath
kProgram(L
"Program");
272 CommandLine
cl_program(kProgram
);
273 EXPECT_EQ(kProgram
.value(), cl_program
.GetProgram().value());
274 EXPECT_EQ(kProgram
.value(), cl_program
.GetCommandLineString());
276 const FilePath
kProgramPath(L
"Program Path");
278 // Check that quotes are not returned from GetProgram().
279 CommandLine
cl_program_path(kProgramPath
);
280 EXPECT_EQ(kProgramPath
.value(), cl_program_path
.GetProgram().value());
282 // Check that quotes are added to command line string paths containing spaces.
283 CommandLine::StringType
cmd_string(cl_program_path
.GetCommandLineString());
284 CommandLine::StringType
program_string(cl_program_path
.GetProgram().value());
285 EXPECT_EQ('"', cmd_string
[0]);
286 EXPECT_EQ(program_string
, cmd_string
.substr(1, program_string
.length()));
287 EXPECT_EQ('"', cmd_string
[program_string
.length() + 1]);
291 // Calling Init multiple times should not modify the previous CommandLine.
292 TEST(CommandLineTest
, Init
) {
293 CommandLine
* initial
= CommandLine::ForCurrentProcess();
294 EXPECT_FALSE(CommandLine::Init(0, NULL
));
295 CommandLine
* current
= CommandLine::ForCurrentProcess();
296 EXPECT_EQ(initial
, current
);