blob: 80874ac5a02d8bb4f44265d8ca45273c4369cbd4 [file] [log] [blame]
[email protected]a40ca4302011-05-14 01:10:241// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
5#include <string>
6#include <vector>
7
[email protected]9d84e1a2010-11-15 00:18:308#include "base/basictypes.h"
[email protected]e6124ad52010-11-15 04:17:529#include "base/command_line.h"
[email protected]5d91c9e2010-07-28 17:25:2810#include "base/file_path.h"
[email protected]f1633932010-08-17 23:05:2811#include "base/utf_string_conversions.h"
initial.commitd7cae122008-07-26 21:49:3812#include "testing/gtest/include/gtest/gtest.h"
13
[email protected]98a1c2682010-08-10 18:14:1914// To test Windows quoting behavior, we use a string that has some backslashes
15// and quotes.
16// Consider the command-line argument: q\"bs1\bs2\\bs3q\\\"
17// Here it is with C-style escapes.
[email protected]a40ca4302011-05-14 01:10:2418static const CommandLine::StringType kTrickyQuoted =
19 FILE_PATH_LITERAL("q\\\"bs1\\bs2\\\\bs3q\\\\\\\"");
[email protected]98a1c2682010-08-10 18:14:1920// It should be parsed by Windows as: q"bs1\bs2\\bs3q\"
21// Here that is with C-style escapes.
[email protected]a40ca4302011-05-14 01:10:2422static const CommandLine::StringType kTricky =
23 FILE_PATH_LITERAL("q\"bs1\\bs2\\\\bs3q\\\"");
[email protected]98a1c2682010-08-10 18:14:1924
initial.commitd7cae122008-07-26 21:49:3825TEST(CommandLineTest, CommandLineConstructor) {
[email protected]a40ca4302011-05-14 01:10:2426 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("--input-translation=45--output-rotation"),
38 FILE_PATH_LITERAL("--"),
39 FILE_PATH_LITERAL("--"),
40 FILE_PATH_LITERAL("--not-a-switch"),
41 FILE_PATH_LITERAL("\"in the time of submarines...\""),
42 FILE_PATH_LITERAL("unquoted arg-with-space")};
43 CommandLine cl(arraysize(argv), argv);
44
[email protected]61a4c6f2011-07-20 04:54:5245 EXPECT_FALSE(cl.GetCommandLineString().empty());
[email protected]a40ca4302011-05-14 01:10:2446 EXPECT_FALSE(cl.HasSwitch("cruller"));
47 EXPECT_FALSE(cl.HasSwitch("flim"));
48 EXPECT_FALSE(cl.HasSwitch("program"));
49 EXPECT_FALSE(cl.HasSwitch("dog"));
50 EXPECT_FALSE(cl.HasSwitch("cat"));
51 EXPECT_FALSE(cl.HasSwitch("output-rotation"));
52 EXPECT_FALSE(cl.HasSwitch("not-a-switch"));
53 EXPECT_FALSE(cl.HasSwitch("--"));
54
55 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(),
56 cl.GetProgram().value());
57
58 EXPECT_TRUE(cl.HasSwitch("foo"));
59 EXPECT_TRUE(cl.HasSwitch("bAr"));
60 EXPECT_TRUE(cl.HasSwitch("baz"));
61 EXPECT_TRUE(cl.HasSwitch("spaetzle"));
62#if defined(OS_WIN)
63 EXPECT_TRUE(cl.HasSwitch("SPAETZLE"));
64#endif
65 EXPECT_TRUE(cl.HasSwitch("other-switches"));
66 EXPECT_TRUE(cl.HasSwitch("input-translation"));
67
68 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle"));
69 EXPECT_EQ("", cl.GetSwitchValueASCII("Foo"));
70 EXPECT_EQ("", cl.GetSwitchValueASCII("bar"));
71 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller"));
72 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII(
73 "other-switches"));
74 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation"));
75
[email protected]75f1c782011-07-13 23:41:2276 const CommandLine::StringVector& args = cl.GetArgs();
[email protected]a40ca4302011-05-14 01:10:2477 ASSERT_EQ(6U, args.size());
78
79 std::vector<CommandLine::StringType>::const_iterator iter = args.begin();
80 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
81 ++iter;
82 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter);
83 ++iter;
84 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter);
85 ++iter;
86 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter);
87 ++iter;
88 EXPECT_EQ(FILE_PATH_LITERAL("\"in the time of submarines...\""), *iter);
89 ++iter;
90 EXPECT_EQ(FILE_PATH_LITERAL("unquoted arg-with-space"), *iter);
91 ++iter;
92 EXPECT_TRUE(iter == args.end());
93}
94
95TEST(CommandLineTest, CommandLineFromString) {
[email protected]bb975362009-01-21 01:00:2296#if defined(OS_WIN)
[email protected]51343d5a2009-10-26 22:39:3397 CommandLine cl = CommandLine::FromString(
[email protected]a40ca4302011-05-14 01:10:2498 L"program --foo= -bAr /Spaetzel=pierogi /Baz flim "
99 L"--other-switches=\"--dog=canine --cat=feline\" "
100 L"-spaetzle=Crepe -=loosevalue FLAN "
101 L"--input-translation=\"45\"--output-rotation "
102 L"--quotes=" + kTrickyQuoted + L" "
103 L"-- -- --not-a-switch "
104 L"\"in the time of submarines...\"");
105
[email protected]61a4c6f2011-07-20 04:54:52106 EXPECT_FALSE(cl.GetCommandLineString().empty());
[email protected]b7e0a2a2009-10-13 02:07:25107 EXPECT_FALSE(cl.HasSwitch("cruller"));
108 EXPECT_FALSE(cl.HasSwitch("flim"));
109 EXPECT_FALSE(cl.HasSwitch("program"));
110 EXPECT_FALSE(cl.HasSwitch("dog"));
111 EXPECT_FALSE(cl.HasSwitch("cat"));
112 EXPECT_FALSE(cl.HasSwitch("output-rotation"));
113 EXPECT_FALSE(cl.HasSwitch("not-a-switch"));
114 EXPECT_FALSE(cl.HasSwitch("--"));
initial.commitd7cae122008-07-26 21:49:38115
[email protected]78c4c422010-10-08 00:06:31116 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(),
117 cl.GetProgram().value());
initial.commitd7cae122008-07-26 21:49:38118
[email protected]b7e0a2a2009-10-13 02:07:25119 EXPECT_TRUE(cl.HasSwitch("foo"));
120 EXPECT_TRUE(cl.HasSwitch("bar"));
121 EXPECT_TRUE(cl.HasSwitch("baz"));
122 EXPECT_TRUE(cl.HasSwitch("spaetzle"));
[email protected]b7e0a2a2009-10-13 02:07:25123 EXPECT_TRUE(cl.HasSwitch("SPAETZLE"));
[email protected]b7e0a2a2009-10-13 02:07:25124 EXPECT_TRUE(cl.HasSwitch("other-switches"));
125 EXPECT_TRUE(cl.HasSwitch("input-translation"));
[email protected]98a1c2682010-08-10 18:14:19126 EXPECT_TRUE(cl.HasSwitch("quotes"));
initial.commitd7cae122008-07-26 21:49:38127
[email protected]c4e52f0d2009-11-06 19:55:16128 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle"));
129 EXPECT_EQ("", cl.GetSwitchValueASCII("Foo"));
130 EXPECT_EQ("", cl.GetSwitchValueASCII("bar"));
131 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller"));
132 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII(
133 "other-switches"));
134 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation"));
[email protected]a40ca4302011-05-14 01:10:24135 EXPECT_EQ(kTricky, cl.GetSwitchValueNative("quotes"));
initial.commitd7cae122008-07-26 21:49:38136
[email protected]75f1c782011-07-13 23:41:22137 const CommandLine::StringVector& args = cl.GetArgs();
[email protected]2e4c50c2010-07-21 15:57:23138 ASSERT_EQ(5U, args.size());
initial.commitd7cae122008-07-26 21:49:38139
[email protected]2e4c50c2010-07-21 15:57:23140 std::vector<CommandLine::StringType>::const_iterator iter = args.begin();
141 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
initial.commitd7cae122008-07-26 21:49:38142 ++iter;
[email protected]a40ca4302011-05-14 01:10:24143 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter);
initial.commitd7cae122008-07-26 21:49:38144 ++iter;
[email protected]2e4c50c2010-07-21 15:57:23145 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter);
[email protected]02c87962008-10-06 10:25:35146 ++iter;
[email protected]2e4c50c2010-07-21 15:57:23147 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter);
[email protected]02c87962008-10-06 10:25:35148 ++iter;
[email protected]2e4c50c2010-07-21 15:57:23149 EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *iter);
initial.commitd7cae122008-07-26 21:49:38150 ++iter;
[email protected]2e4c50c2010-07-21 15:57:23151 EXPECT_TRUE(iter == args.end());
[email protected]10e42bf2008-10-15 21:59:08152
[email protected]a40ca4302011-05-14 01:10:24153 // Check that a generated string produces an equivalent command line.
[email protected]61a4c6f2011-07-20 04:54:52154 CommandLine cl_duplicate = CommandLine::FromString(cl.GetCommandLineString());
155 EXPECT_EQ(cl.GetCommandLineString(), cl_duplicate.GetCommandLineString());
[email protected]10e42bf2008-10-15 21:59:08156#endif
initial.commitd7cae122008-07-26 21:49:38157}
158
initial.commitd7cae122008-07-26 21:49:38159// Tests behavior with an empty input string.
160TEST(CommandLineTest, EmptyString) {
[email protected]f3adb5c2008-08-07 20:07:32161#if defined(OS_WIN)
[email protected]a40ca4302011-05-14 01:10:24162 CommandLine cl_from_string = CommandLine::FromString(L"");
[email protected]61a4c6f2011-07-20 04:54:52163 EXPECT_TRUE(cl_from_string.GetCommandLineString().empty());
[email protected]a40ca4302011-05-14 01:10:24164 EXPECT_TRUE(cl_from_string.GetProgram().empty());
165 EXPECT_EQ(1U, cl_from_string.argv().size());
[email protected]75f1c782011-07-13 23:41:22166 EXPECT_TRUE(cl_from_string.GetArgs().empty());
[email protected]f3adb5c2008-08-07 20:07:32167#endif
[email protected]a40ca4302011-05-14 01:10:24168 CommandLine cl_from_argv(0, NULL);
[email protected]61a4c6f2011-07-20 04:54:52169 EXPECT_TRUE(cl_from_argv.GetCommandLineString().empty());
[email protected]a40ca4302011-05-14 01:10:24170 EXPECT_TRUE(cl_from_argv.GetProgram().empty());
171 EXPECT_EQ(1U, cl_from_argv.argv().size());
[email protected]75f1c782011-07-13 23:41:22172 EXPECT_TRUE(cl_from_argv.GetArgs().empty());
initial.commitd7cae122008-07-26 21:49:38173}
174
[email protected]bb975362009-01-21 01:00:22175// Test methods for appending switches to a command line.
initial.commitd7cae122008-07-26 21:49:38176TEST(CommandLineTest, AppendSwitches) {
[email protected]b7e0a2a2009-10-13 02:07:25177 std::string switch1 = "switch1";
178 std::string switch2 = "switch2";
[email protected]a40ca4302011-05-14 01:10:24179 std::string value2 = "value";
[email protected]b7e0a2a2009-10-13 02:07:25180 std::string switch3 = "switch3";
[email protected]05076ba22010-07-30 05:59:57181 std::string value3 = "a value with spaces";
[email protected]b7e0a2a2009-10-13 02:07:25182 std::string switch4 = "switch4";
[email protected]05076ba22010-07-30 05:59:57183 std::string value4 = "\"a value with quotes\"";
[email protected]98a1c2682010-08-10 18:14:19184 std::string switch5 = "quotes";
[email protected]a40ca4302011-05-14 01:10:24185 CommandLine::StringType value5 = kTricky;
initial.commitd7cae122008-07-26 21:49:38186
[email protected]51343d5a2009-10-26 22:39:33187 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
initial.commitd7cae122008-07-26 21:49:38188
[email protected]bb975362009-01-21 01:00:22189 cl.AppendSwitch(switch1);
[email protected]a40ca4302011-05-14 01:10:24190 cl.AppendSwitchASCII(switch2, value2);
[email protected]05076ba22010-07-30 05:59:57191 cl.AppendSwitchASCII(switch3, value3);
192 cl.AppendSwitchASCII(switch4, value4);
[email protected]a40ca4302011-05-14 01:10:24193 cl.AppendSwitchNative(switch5, value5);
[email protected]bb975362009-01-21 01:00:22194
initial.commitd7cae122008-07-26 21:49:38195 EXPECT_TRUE(cl.HasSwitch(switch1));
196 EXPECT_TRUE(cl.HasSwitch(switch2));
[email protected]a40ca4302011-05-14 01:10:24197 EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2));
initial.commitd7cae122008-07-26 21:49:38198 EXPECT_TRUE(cl.HasSwitch(switch3));
[email protected]05076ba22010-07-30 05:59:57199 EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3));
[email protected]8c9510d2008-10-10 21:38:20200 EXPECT_TRUE(cl.HasSwitch(switch4));
[email protected]05076ba22010-07-30 05:59:57201 EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4));
[email protected]98a1c2682010-08-10 18:14:19202 EXPECT_TRUE(cl.HasSwitch(switch5));
[email protected]a40ca4302011-05-14 01:10:24203 EXPECT_EQ(value5, cl.GetSwitchValueNative(switch5));
[email protected]98a1c2682010-08-10 18:14:19204
205#if defined(OS_WIN)
[email protected]a40ca4302011-05-14 01:10:24206 EXPECT_EQ(L"Program "
[email protected]98a1c2682010-08-10 18:14:19207 L"--switch1 "
208 L"--switch2=value "
209 L"--switch3=\"a value with spaces\" "
210 L"--switch4=\"\\\"a value with quotes\\\"\" "
[email protected]a40ca4302011-05-14 01:10:24211 L"--quotes=\"" + kTrickyQuoted + L"\"",
[email protected]61a4c6f2011-07-20 04:54:52212 cl.GetCommandLineString());
[email protected]98a1c2682010-08-10 18:14:19213#endif
initial.commitd7cae122008-07-26 21:49:38214}
[email protected]e6124ad52010-11-15 04:17:52215
[email protected]a40ca4302011-05-14 01:10:24216TEST(CommandLineTest, AppendSwitchesDashDash) {
217 const CommandLine::CharType* raw_argv[] = { FILE_PATH_LITERAL("prog"),
218 FILE_PATH_LITERAL("--"),
219 FILE_PATH_LITERAL("--arg1") };
220 CommandLine cl(arraysize(raw_argv), raw_argv);
221
222 cl.AppendSwitch("switch1");
223 cl.AppendSwitchASCII("switch2", "foo");
224
225 cl.AppendArg("--arg2");
226
227 EXPECT_EQ(FILE_PATH_LITERAL("prog --switch1 --switch2=foo -- --arg1 --arg2"),
[email protected]61a4c6f2011-07-20 04:54:52228 cl.GetCommandLineString());
[email protected]a40ca4302011-05-14 01:10:24229 CommandLine::StringVector cl_argv = cl.argv();
230 EXPECT_EQ(FILE_PATH_LITERAL("prog"), cl_argv[0]);
231 EXPECT_EQ(FILE_PATH_LITERAL("--switch1"), cl_argv[1]);
232 EXPECT_EQ(FILE_PATH_LITERAL("--switch2=foo"), cl_argv[2]);
233 EXPECT_EQ(FILE_PATH_LITERAL("--"), cl_argv[3]);
234 EXPECT_EQ(FILE_PATH_LITERAL("--arg1"), cl_argv[4]);
235 EXPECT_EQ(FILE_PATH_LITERAL("--arg2"), cl_argv[5]);
236}
237
[email protected]e6124ad52010-11-15 04:17:52238// Tests that when AppendArguments is called that the program is set correctly
239// on the target CommandLine object and the switches from the source
240// CommandLine are added to the target.
241TEST(CommandLineTest, AppendArguments) {
242 CommandLine cl1(FilePath(FILE_PATH_LITERAL("Program")));
243 cl1.AppendSwitch("switch1");
244 cl1.AppendSwitchASCII("switch2", "foo");
245
246 CommandLine cl2(CommandLine::NO_PROGRAM);
247 cl2.AppendArguments(cl1, true);
248 EXPECT_EQ(cl1.GetProgram().value(), cl2.GetProgram().value());
[email protected]61a4c6f2011-07-20 04:54:52249 EXPECT_EQ(cl1.GetCommandLineString(), cl2.GetCommandLineString());
[email protected]e6124ad52010-11-15 04:17:52250
251 CommandLine c1(FilePath(FILE_PATH_LITERAL("Program1")));
252 c1.AppendSwitch("switch1");
253 CommandLine c2(FilePath(FILE_PATH_LITERAL("Program2")));
254 c2.AppendSwitch("switch2");
255
256 c1.AppendArguments(c2, true);
257 EXPECT_EQ(c1.GetProgram().value(), c2.GetProgram().value());
258 EXPECT_TRUE(c1.HasSwitch("switch1"));
259 EXPECT_TRUE(c1.HasSwitch("switch2"));
260}
261
[email protected]450b34ec2010-11-29 21:12:22262#if defined(OS_WIN)
[email protected]61a4c6f2011-07-20 04:54:52263// Make sure that the command line string program paths are quoted as necessary.
[email protected]450b34ec2010-11-29 21:12:22264// This only makes sense on Windows and the test is basically here to guard
265// against regressions.
266TEST(CommandLineTest, ProgramQuotes) {
[email protected]a40ca4302011-05-14 01:10:24267 // Check that quotes are not added for paths without spaces.
[email protected]450b34ec2010-11-29 21:12:22268 const FilePath kProgram(L"Program");
[email protected]a40ca4302011-05-14 01:10:24269 CommandLine cl_program(kProgram);
270 EXPECT_EQ(kProgram.value(), cl_program.GetProgram().value());
[email protected]61a4c6f2011-07-20 04:54:52271 EXPECT_EQ(kProgram.value(), cl_program.GetCommandLineString());
[email protected]a40ca4302011-05-14 01:10:24272
273 const FilePath kProgramPath(L"Program Path");
[email protected]450b34ec2010-11-29 21:12:22274
275 // Check that quotes are not returned from GetProgram().
[email protected]a40ca4302011-05-14 01:10:24276 CommandLine cl_program_path(kProgramPath);
277 EXPECT_EQ(kProgramPath.value(), cl_program_path.GetProgram().value());
[email protected]450b34ec2010-11-29 21:12:22278
[email protected]a40ca4302011-05-14 01:10:24279 // Check that quotes are added to command line string paths containing spaces.
[email protected]61a4c6f2011-07-20 04:54:52280 CommandLine::StringType cmd_string(cl_program_path.GetCommandLineString());
[email protected]a40ca4302011-05-14 01:10:24281 CommandLine::StringType program_string(cl_program_path.GetProgram().value());
282 EXPECT_EQ('"', cmd_string[0]);
283 EXPECT_EQ(program_string, cmd_string.substr(1, program_string.length()));
284 EXPECT_EQ('"', cmd_string[program_string.length() + 1]);
[email protected]450b34ec2010-11-29 21:12:22285}
286#endif
[email protected]f96fe2c42011-07-13 18:03:34287
288// Calling Init multiple times should not modify the previous CommandLine.
289TEST(CommandLineTest, Init) {
290 CommandLine* initial = CommandLine::ForCurrentProcess();
291 CommandLine::Init(0, NULL);
292 CommandLine* current = CommandLine::ForCurrentProcess();
293 EXPECT_EQ(initial, current);
294}