blob: 656236e09e35c6a2bb705c4bb2ba14e2beea1798 [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"),
[email protected]1fa39f02011-09-13 15:45:3437 FILE_PATH_LITERAL("a"),
[email protected]a40ca4302011-05-14 01:10:2438 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);
45
[email protected]61a4c6f2011-07-20 04:54:5246 EXPECT_FALSE(cl.GetCommandLineString().empty());
[email protected]a40ca4302011-05-14 01:10:2447 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("--"));
55
56 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(),
57 cl.GetProgram().value());
58
59 EXPECT_TRUE(cl.HasSwitch("foo"));
60 EXPECT_TRUE(cl.HasSwitch("bAr"));
61 EXPECT_TRUE(cl.HasSwitch("baz"));
62 EXPECT_TRUE(cl.HasSwitch("spaetzle"));
63#if defined(OS_WIN)
64 EXPECT_TRUE(cl.HasSwitch("SPAETZLE"));
65#endif
66 EXPECT_TRUE(cl.HasSwitch("other-switches"));
67 EXPECT_TRUE(cl.HasSwitch("input-translation"));
68
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(
74 "other-switches"));
75 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation"));
76
[email protected]75f1c782011-07-13 23:41:2277 const CommandLine::StringVector& args = cl.GetArgs();
[email protected]1fa39f02011-09-13 15:45:3478 ASSERT_EQ(7U, args.size());
[email protected]a40ca4302011-05-14 01:10:2479
80 std::vector<CommandLine::StringType>::const_iterator iter = args.begin();
81 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
82 ++iter;
83 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter);
84 ++iter;
[email protected]1fa39f02011-09-13 15:45:3485 EXPECT_EQ(FILE_PATH_LITERAL("a"), *iter);
86 ++iter;
[email protected]a40ca4302011-05-14 01:10:2487 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter);
88 ++iter;
89 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter);
90 ++iter;
91 EXPECT_EQ(FILE_PATH_LITERAL("\"in the time of submarines...\""), *iter);
92 ++iter;
93 EXPECT_EQ(FILE_PATH_LITERAL("unquoted arg-with-space"), *iter);
94 ++iter;
95 EXPECT_TRUE(iter == args.end());
96}
97
98TEST(CommandLineTest, CommandLineFromString) {
[email protected]bb975362009-01-21 01:00:2299#if defined(OS_WIN)
[email protected]51343d5a2009-10-26 22:39:33100 CommandLine cl = CommandLine::FromString(
[email protected]a40ca4302011-05-14 01:10:24101 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...\"");
108
[email protected]61a4c6f2011-07-20 04:54:52109 EXPECT_FALSE(cl.GetCommandLineString().empty());
[email protected]b7e0a2a2009-10-13 02:07:25110 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("--"));
initial.commitd7cae122008-07-26 21:49:38118
[email protected]78c4c422010-10-08 00:06:31119 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(),
120 cl.GetProgram().value());
initial.commitd7cae122008-07-26 21:49:38121
[email protected]b7e0a2a2009-10-13 02:07:25122 EXPECT_TRUE(cl.HasSwitch("foo"));
123 EXPECT_TRUE(cl.HasSwitch("bar"));
124 EXPECT_TRUE(cl.HasSwitch("baz"));
125 EXPECT_TRUE(cl.HasSwitch("spaetzle"));
[email protected]b7e0a2a2009-10-13 02:07:25126 EXPECT_TRUE(cl.HasSwitch("SPAETZLE"));
[email protected]b7e0a2a2009-10-13 02:07:25127 EXPECT_TRUE(cl.HasSwitch("other-switches"));
128 EXPECT_TRUE(cl.HasSwitch("input-translation"));
[email protected]98a1c2682010-08-10 18:14:19129 EXPECT_TRUE(cl.HasSwitch("quotes"));
initial.commitd7cae122008-07-26 21:49:38130
[email protected]c4e52f0d2009-11-06 19:55:16131 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(
136 "other-switches"));
137 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation"));
[email protected]a40ca4302011-05-14 01:10:24138 EXPECT_EQ(kTricky, cl.GetSwitchValueNative("quotes"));
initial.commitd7cae122008-07-26 21:49:38139
[email protected]75f1c782011-07-13 23:41:22140 const CommandLine::StringVector& args = cl.GetArgs();
[email protected]2e4c50c2010-07-21 15:57:23141 ASSERT_EQ(5U, args.size());
initial.commitd7cae122008-07-26 21:49:38142
[email protected]2e4c50c2010-07-21 15:57:23143 std::vector<CommandLine::StringType>::const_iterator iter = args.begin();
144 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
initial.commitd7cae122008-07-26 21:49:38145 ++iter;
[email protected]a40ca4302011-05-14 01:10:24146 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter);
initial.commitd7cae122008-07-26 21:49:38147 ++iter;
[email protected]2e4c50c2010-07-21 15:57:23148 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter);
[email protected]02c87962008-10-06 10:25:35149 ++iter;
[email protected]2e4c50c2010-07-21 15:57:23150 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter);
[email protected]02c87962008-10-06 10:25:35151 ++iter;
[email protected]2e4c50c2010-07-21 15:57:23152 EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *iter);
initial.commitd7cae122008-07-26 21:49:38153 ++iter;
[email protected]2e4c50c2010-07-21 15:57:23154 EXPECT_TRUE(iter == args.end());
[email protected]10e42bf2008-10-15 21:59:08155
[email protected]a40ca4302011-05-14 01:10:24156 // Check that a generated string produces an equivalent command line.
[email protected]61a4c6f2011-07-20 04:54:52157 CommandLine cl_duplicate = CommandLine::FromString(cl.GetCommandLineString());
158 EXPECT_EQ(cl.GetCommandLineString(), cl_duplicate.GetCommandLineString());
[email protected]10e42bf2008-10-15 21:59:08159#endif
initial.commitd7cae122008-07-26 21:49:38160}
161
initial.commitd7cae122008-07-26 21:49:38162// Tests behavior with an empty input string.
163TEST(CommandLineTest, EmptyString) {
[email protected]f3adb5c2008-08-07 20:07:32164#if defined(OS_WIN)
[email protected]a40ca4302011-05-14 01:10:24165 CommandLine cl_from_string = CommandLine::FromString(L"");
[email protected]61a4c6f2011-07-20 04:54:52166 EXPECT_TRUE(cl_from_string.GetCommandLineString().empty());
[email protected]a40ca4302011-05-14 01:10:24167 EXPECT_TRUE(cl_from_string.GetProgram().empty());
168 EXPECT_EQ(1U, cl_from_string.argv().size());
[email protected]75f1c782011-07-13 23:41:22169 EXPECT_TRUE(cl_from_string.GetArgs().empty());
[email protected]f3adb5c2008-08-07 20:07:32170#endif
[email protected]a40ca4302011-05-14 01:10:24171 CommandLine cl_from_argv(0, NULL);
[email protected]61a4c6f2011-07-20 04:54:52172 EXPECT_TRUE(cl_from_argv.GetCommandLineString().empty());
[email protected]a40ca4302011-05-14 01:10:24173 EXPECT_TRUE(cl_from_argv.GetProgram().empty());
174 EXPECT_EQ(1U, cl_from_argv.argv().size());
[email protected]75f1c782011-07-13 23:41:22175 EXPECT_TRUE(cl_from_argv.GetArgs().empty());
initial.commitd7cae122008-07-26 21:49:38176}
177
[email protected]bb975362009-01-21 01:00:22178// Test methods for appending switches to a command line.
initial.commitd7cae122008-07-26 21:49:38179TEST(CommandLineTest, AppendSwitches) {
[email protected]b7e0a2a2009-10-13 02:07:25180 std::string switch1 = "switch1";
181 std::string switch2 = "switch2";
[email protected]a40ca4302011-05-14 01:10:24182 std::string value2 = "value";
[email protected]b7e0a2a2009-10-13 02:07:25183 std::string switch3 = "switch3";
[email protected]05076ba22010-07-30 05:59:57184 std::string value3 = "a value with spaces";
[email protected]b7e0a2a2009-10-13 02:07:25185 std::string switch4 = "switch4";
[email protected]05076ba22010-07-30 05:59:57186 std::string value4 = "\"a value with quotes\"";
[email protected]98a1c2682010-08-10 18:14:19187 std::string switch5 = "quotes";
[email protected]a40ca4302011-05-14 01:10:24188 CommandLine::StringType value5 = kTricky;
initial.commitd7cae122008-07-26 21:49:38189
[email protected]51343d5a2009-10-26 22:39:33190 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
initial.commitd7cae122008-07-26 21:49:38191
[email protected]bb975362009-01-21 01:00:22192 cl.AppendSwitch(switch1);
[email protected]a40ca4302011-05-14 01:10:24193 cl.AppendSwitchASCII(switch2, value2);
[email protected]05076ba22010-07-30 05:59:57194 cl.AppendSwitchASCII(switch3, value3);
195 cl.AppendSwitchASCII(switch4, value4);
[email protected]a40ca4302011-05-14 01:10:24196 cl.AppendSwitchNative(switch5, value5);
[email protected]bb975362009-01-21 01:00:22197
initial.commitd7cae122008-07-26 21:49:38198 EXPECT_TRUE(cl.HasSwitch(switch1));
199 EXPECT_TRUE(cl.HasSwitch(switch2));
[email protected]a40ca4302011-05-14 01:10:24200 EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2));
initial.commitd7cae122008-07-26 21:49:38201 EXPECT_TRUE(cl.HasSwitch(switch3));
[email protected]05076ba22010-07-30 05:59:57202 EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3));
[email protected]8c9510d2008-10-10 21:38:20203 EXPECT_TRUE(cl.HasSwitch(switch4));
[email protected]05076ba22010-07-30 05:59:57204 EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4));
[email protected]98a1c2682010-08-10 18:14:19205 EXPECT_TRUE(cl.HasSwitch(switch5));
[email protected]a40ca4302011-05-14 01:10:24206 EXPECT_EQ(value5, cl.GetSwitchValueNative(switch5));
[email protected]98a1c2682010-08-10 18:14:19207
208#if defined(OS_WIN)
[email protected]a40ca4302011-05-14 01:10:24209 EXPECT_EQ(L"Program "
[email protected]98a1c2682010-08-10 18:14:19210 L"--switch1 "
211 L"--switch2=value "
212 L"--switch3=\"a value with spaces\" "
213 L"--switch4=\"\\\"a value with quotes\\\"\" "
[email protected]a40ca4302011-05-14 01:10:24214 L"--quotes=\"" + kTrickyQuoted + L"\"",
[email protected]61a4c6f2011-07-20 04:54:52215 cl.GetCommandLineString());
[email protected]98a1c2682010-08-10 18:14:19216#endif
initial.commitd7cae122008-07-26 21:49:38217}
[email protected]e6124ad52010-11-15 04:17:52218
[email protected]a40ca4302011-05-14 01:10:24219TEST(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);
224
225 cl.AppendSwitch("switch1");
226 cl.AppendSwitchASCII("switch2", "foo");
227
228 cl.AppendArg("--arg2");
229
230 EXPECT_EQ(FILE_PATH_LITERAL("prog --switch1 --switch2=foo -- --arg1 --arg2"),
[email protected]61a4c6f2011-07-20 04:54:52231 cl.GetCommandLineString());
[email protected]a40ca4302011-05-14 01:10:24232 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]);
239}
240
[email protected]e6124ad52010-11-15 04:17:52241// 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.
244TEST(CommandLineTest, AppendArguments) {
245 CommandLine cl1(FilePath(FILE_PATH_LITERAL("Program")));
246 cl1.AppendSwitch("switch1");
247 cl1.AppendSwitchASCII("switch2", "foo");
248
249 CommandLine cl2(CommandLine::NO_PROGRAM);
250 cl2.AppendArguments(cl1, true);
251 EXPECT_EQ(cl1.GetProgram().value(), cl2.GetProgram().value());
[email protected]61a4c6f2011-07-20 04:54:52252 EXPECT_EQ(cl1.GetCommandLineString(), cl2.GetCommandLineString());
[email protected]e6124ad52010-11-15 04:17:52253
254 CommandLine c1(FilePath(FILE_PATH_LITERAL("Program1")));
255 c1.AppendSwitch("switch1");
256 CommandLine c2(FilePath(FILE_PATH_LITERAL("Program2")));
257 c2.AppendSwitch("switch2");
258
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"));
263}
264
[email protected]450b34ec2010-11-29 21:12:22265#if defined(OS_WIN)
[email protected]61a4c6f2011-07-20 04:54:52266// Make sure that the command line string program paths are quoted as necessary.
[email protected]450b34ec2010-11-29 21:12:22267// This only makes sense on Windows and the test is basically here to guard
268// against regressions.
269TEST(CommandLineTest, ProgramQuotes) {
[email protected]a40ca4302011-05-14 01:10:24270 // Check that quotes are not added for paths without spaces.
[email protected]450b34ec2010-11-29 21:12:22271 const FilePath kProgram(L"Program");
[email protected]a40ca4302011-05-14 01:10:24272 CommandLine cl_program(kProgram);
273 EXPECT_EQ(kProgram.value(), cl_program.GetProgram().value());
[email protected]61a4c6f2011-07-20 04:54:52274 EXPECT_EQ(kProgram.value(), cl_program.GetCommandLineString());
[email protected]a40ca4302011-05-14 01:10:24275
276 const FilePath kProgramPath(L"Program Path");
[email protected]450b34ec2010-11-29 21:12:22277
278 // Check that quotes are not returned from GetProgram().
[email protected]a40ca4302011-05-14 01:10:24279 CommandLine cl_program_path(kProgramPath);
280 EXPECT_EQ(kProgramPath.value(), cl_program_path.GetProgram().value());
[email protected]450b34ec2010-11-29 21:12:22281
[email protected]a40ca4302011-05-14 01:10:24282 // Check that quotes are added to command line string paths containing spaces.
[email protected]61a4c6f2011-07-20 04:54:52283 CommandLine::StringType cmd_string(cl_program_path.GetCommandLineString());
[email protected]a40ca4302011-05-14 01:10:24284 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]);
[email protected]450b34ec2010-11-29 21:12:22288}
289#endif
[email protected]f96fe2c42011-07-13 18:03:34290
291// Calling Init multiple times should not modify the previous CommandLine.
292TEST(CommandLineTest, Init) {
293 CommandLine* initial = CommandLine::ForCurrentProcess();
294 CommandLine::Init(0, NULL);
295 CommandLine* current = CommandLine::ForCurrentProcess();
296 EXPECT_EQ(initial, current);
297}