[email protected] | f163393 | 2010-08-17 23:05:28 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
| 5 | #include <string> |
| 6 | #include <vector> |
| 7 | |
[email protected] | 9d84e1a | 2010-11-15 00:18:30 | [diff] [blame] | 8 | #include "base/basictypes.h" |
[email protected] | e6124ad5 | 2010-11-15 04:17:52 | [diff] [blame] | 9 | #include "base/command_line.h" |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 10 | #include "base/file_path.h" |
[email protected] | f163393 | 2010-08-17 23:05:28 | [diff] [blame] | 11 | #include "base/utf_string_conversions.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 14 | // 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. |
| 18 | #define TRICKY_QUOTED L"q\\\"bs1\\bs2\\\\bs3q\\\\\\\"" |
| 19 | // It should be parsed by Windows as: q"bs1\bs2\\bs3q\" |
| 20 | // Here that is with C-style escapes. |
| 21 | #define TRICKY L"q\"bs1\\bs2\\\\bs3q\\\"" |
| 22 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 23 | TEST(CommandLineTest, CommandLineConstructor) { |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 24 | #if defined(OS_WIN) |
[email protected] | 51343d5a | 2009-10-26 22:39:33 | [diff] [blame] | 25 | CommandLine cl = CommandLine::FromString( |
| 26 | L"program --foo= -bAr /Spaetzel=pierogi /Baz flim " |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 27 | L"--other-switches=\"--dog=canine --cat=feline\" " |
| 28 | L"-spaetzle=Crepe -=loosevalue flan " |
| 29 | L"--input-translation=\"45\"--output-rotation " |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 30 | L"--quotes=" TRICKY_QUOTED L" " |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 31 | L"-- -- --not-a-switch " |
| 32 | L"\"in the time of submarines...\""); |
| 33 | EXPECT_FALSE(cl.command_line_string().empty()); |
[email protected] | 4e44b4d | 2008-08-12 03:12:41 | [diff] [blame] | 34 | #elif defined(OS_POSIX) |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 35 | const char* argv[] = {"program", "--foo=", "-bar", |
| 36 | "-spaetzel=pierogi", "-baz", "flim", |
[email protected] | e63d598 | 2008-08-14 22:09:39 | [diff] [blame] | 37 | "--other-switches=--dog=canine --cat=feline", |
| 38 | "-spaetzle=Crepe", "-=loosevalue", "flan", |
| 39 | "--input-translation=45--output-rotation", |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 40 | "--", "--", "--not-a-switch", |
[email protected] | e63d598 | 2008-08-14 22:09:39 | [diff] [blame] | 41 | "in the time of submarines..."}; |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 42 | CommandLine cl(arraysize(argv), argv); |
| 43 | #endif |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 44 | EXPECT_FALSE(cl.HasSwitch("cruller")); |
| 45 | EXPECT_FALSE(cl.HasSwitch("flim")); |
| 46 | EXPECT_FALSE(cl.HasSwitch("program")); |
| 47 | EXPECT_FALSE(cl.HasSwitch("dog")); |
| 48 | EXPECT_FALSE(cl.HasSwitch("cat")); |
| 49 | EXPECT_FALSE(cl.HasSwitch("output-rotation")); |
| 50 | EXPECT_FALSE(cl.HasSwitch("not-a-switch")); |
| 51 | EXPECT_FALSE(cl.HasSwitch("--")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 52 | |
[email protected] | 78c4c42 | 2010-10-08 00:06:31 | [diff] [blame] | 53 | EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(), |
| 54 | cl.GetProgram().value()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 55 | |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 56 | EXPECT_TRUE(cl.HasSwitch("foo")); |
| 57 | EXPECT_TRUE(cl.HasSwitch("bar")); |
| 58 | EXPECT_TRUE(cl.HasSwitch("baz")); |
| 59 | EXPECT_TRUE(cl.HasSwitch("spaetzle")); |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 60 | #if defined(OS_WIN) |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 61 | EXPECT_TRUE(cl.HasSwitch("SPAETZLE")); |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 62 | #endif |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 63 | EXPECT_TRUE(cl.HasSwitch("other-switches")); |
| 64 | EXPECT_TRUE(cl.HasSwitch("input-translation")); |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 65 | #if defined(OS_WIN) |
| 66 | EXPECT_TRUE(cl.HasSwitch("quotes")); |
| 67 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 68 | |
[email protected] | c4e52f0d | 2009-11-06 19:55:16 | [diff] [blame] | 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")); |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 76 | #if defined(OS_WIN) |
| 77 | EXPECT_EQ(TRICKY, cl.GetSwitchValueNative("quotes")); |
| 78 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 79 | |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 80 | const std::vector<CommandLine::StringType>& args = cl.args(); |
| 81 | ASSERT_EQ(5U, args.size()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 82 | |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 83 | std::vector<CommandLine::StringType>::const_iterator iter = args.begin(); |
| 84 | EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 85 | ++iter; |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 86 | EXPECT_EQ(FILE_PATH_LITERAL("flan"), *iter); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 87 | ++iter; |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 88 | EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter); |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 89 | ++iter; |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 90 | EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter); |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 91 | ++iter; |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 92 | EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *iter); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 93 | ++iter; |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 94 | EXPECT_TRUE(iter == args.end()); |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 95 | #if defined(OS_POSIX) |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 96 | const std::vector<std::string>& argvec = cl.argv(); |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 97 | |
| 98 | for (size_t i = 0; i < argvec.size(); i++) { |
| 99 | EXPECT_EQ(0, argvec[i].compare(argv[i])); |
| 100 | } |
| 101 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 102 | } |
| 103 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 104 | // Tests behavior with an empty input string. |
| 105 | TEST(CommandLineTest, EmptyString) { |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 106 | #if defined(OS_WIN) |
[email protected] | 51343d5a | 2009-10-26 22:39:33 | [diff] [blame] | 107 | CommandLine cl = CommandLine::FromString(L""); |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 108 | EXPECT_TRUE(cl.command_line_string().empty()); |
[email protected] | fc8edf5 | 2010-10-14 20:30:45 | [diff] [blame] | 109 | EXPECT_TRUE(cl.GetProgram().empty()); |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 110 | #elif defined(OS_POSIX) |
[email protected] | 4e44b4d | 2008-08-12 03:12:41 | [diff] [blame] | 111 | CommandLine cl(0, NULL); |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 112 | EXPECT_TRUE(cl.argv().size() == 0); |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 113 | #endif |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 114 | EXPECT_EQ(0U, cl.args().size()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 115 | } |
| 116 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 117 | // Test methods for appending switches to a command line. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 118 | TEST(CommandLineTest, AppendSwitches) { |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 119 | std::string switch1 = "switch1"; |
| 120 | std::string switch2 = "switch2"; |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 121 | std::string value = "value"; |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 122 | std::string switch3 = "switch3"; |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 123 | std::string value3 = "a value with spaces"; |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 124 | std::string switch4 = "switch4"; |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 125 | std::string value4 = "\"a value with quotes\""; |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 126 | std::string switch5 = "quotes"; |
[email protected] | f163393 | 2010-08-17 23:05:28 | [diff] [blame] | 127 | std::string value5 = WideToUTF8(TRICKY); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 128 | |
[email protected] | 51343d5a | 2009-10-26 22:39:33 | [diff] [blame] | 129 | CommandLine cl(FilePath(FILE_PATH_LITERAL("Program"))); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 130 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 131 | cl.AppendSwitch(switch1); |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 132 | cl.AppendSwitchASCII(switch2, value); |
| 133 | cl.AppendSwitchASCII(switch3, value3); |
| 134 | cl.AppendSwitchASCII(switch4, value4); |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 135 | cl.AppendSwitchASCII(switch5, value5); |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 136 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 137 | EXPECT_TRUE(cl.HasSwitch(switch1)); |
| 138 | EXPECT_TRUE(cl.HasSwitch(switch2)); |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 139 | EXPECT_EQ(value, cl.GetSwitchValueASCII(switch2)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 140 | EXPECT_TRUE(cl.HasSwitch(switch3)); |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 141 | EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3)); |
[email protected] | 8c9510d | 2008-10-10 21:38:20 | [diff] [blame] | 142 | EXPECT_TRUE(cl.HasSwitch(switch4)); |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 143 | EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4)); |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 144 | EXPECT_TRUE(cl.HasSwitch(switch5)); |
| 145 | EXPECT_EQ(value5, cl.GetSwitchValueASCII(switch5)); |
| 146 | |
| 147 | #if defined(OS_WIN) |
| 148 | EXPECT_EQ(L"\"Program\" " |
| 149 | L"--switch1 " |
| 150 | L"--switch2=value " |
| 151 | L"--switch3=\"a value with spaces\" " |
| 152 | L"--switch4=\"\\\"a value with quotes\\\"\" " |
| 153 | L"--quotes=\"" TRICKY_QUOTED L"\"", |
| 154 | cl.command_line_string()); |
| 155 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 156 | } |
[email protected] | e6124ad5 | 2010-11-15 04:17:52 | [diff] [blame] | 157 | |
| 158 | // Tests that when AppendArguments is called that the program is set correctly |
| 159 | // on the target CommandLine object and the switches from the source |
| 160 | // CommandLine are added to the target. |
| 161 | TEST(CommandLineTest, AppendArguments) { |
| 162 | CommandLine cl1(FilePath(FILE_PATH_LITERAL("Program"))); |
| 163 | cl1.AppendSwitch("switch1"); |
| 164 | cl1.AppendSwitchASCII("switch2", "foo"); |
| 165 | |
| 166 | CommandLine cl2(CommandLine::NO_PROGRAM); |
| 167 | cl2.AppendArguments(cl1, true); |
| 168 | EXPECT_EQ(cl1.GetProgram().value(), cl2.GetProgram().value()); |
| 169 | EXPECT_EQ(cl1.command_line_string(), cl2.command_line_string()); |
| 170 | |
| 171 | CommandLine c1(FilePath(FILE_PATH_LITERAL("Program1"))); |
| 172 | c1.AppendSwitch("switch1"); |
| 173 | CommandLine c2(FilePath(FILE_PATH_LITERAL("Program2"))); |
| 174 | c2.AppendSwitch("switch2"); |
| 175 | |
| 176 | c1.AppendArguments(c2, true); |
| 177 | EXPECT_EQ(c1.GetProgram().value(), c2.GetProgram().value()); |
| 178 | EXPECT_TRUE(c1.HasSwitch("switch1")); |
| 179 | EXPECT_TRUE(c1.HasSwitch("switch2")); |
| 180 | } |
| 181 | |
[email protected] | 450b34ec | 2010-11-29 21:12:22 | [diff] [blame^] | 182 | #if defined(OS_WIN) |
| 183 | // Make sure that the program part of a command line is always quoted. |
| 184 | // This only makes sense on Windows and the test is basically here to guard |
| 185 | // against regressions. |
| 186 | TEST(CommandLineTest, ProgramQuotes) { |
| 187 | const FilePath kProgram(L"Program"); |
| 188 | |
| 189 | // Check that quotes are not returned from GetProgram(). |
| 190 | CommandLine cl(kProgram); |
| 191 | EXPECT_EQ(kProgram.value(), cl.GetProgram().value()); |
| 192 | |
| 193 | // Verify that in the command line string, the program part is always quoted. |
| 194 | CommandLine::StringType cmd(cl.command_line_string()); |
| 195 | CommandLine::StringType program(cl.GetProgram().value()); |
| 196 | EXPECT_EQ('"', cmd[0]); |
| 197 | EXPECT_EQ(program, cmd.substr(1, program.length())); |
| 198 | EXPECT_EQ('"', cmd[program.length() + 1]); |
| 199 | } |
| 200 | #endif |