command_line_unittest.cc revision 4a5e2dc747d50c653511c68ccb2cfbfb740bd5a7
13345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Copyright (c) 2010 The Chromium Authors. All rights reserved. 2c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Use of this source code is governed by a BSD-style license that can be 3c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// found in the LICENSE file. 4c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 5c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <string> 6c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <vector> 7c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 8c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/basictypes.h" 94a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#include "base/command_line.h" 103345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#include "base/file_path.h" 113345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#include "base/utf_string_conversions.h" 12c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "testing/gtest/include/gtest/gtest.h" 13c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 143345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// To test Windows quoting behavior, we use a string that has some backslashes 153345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// and quotes. 163345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Consider the command-line argument: q\"bs1\bs2\\bs3q\\\" 173345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Here it is with C-style escapes. 183345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#define TRICKY_QUOTED L"q\\\"bs1\\bs2\\\\bs3q\\\\\\\"" 193345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// It should be parsed by Windows as: q"bs1\bs2\\bs3q\" 203345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Here that is with C-style escapes. 213345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#define TRICKY L"q\"bs1\\bs2\\\\bs3q\\\"" 223345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick 23c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottTEST(CommandLineTest, CommandLineConstructor) { 24c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_WIN) 25c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CommandLine cl = CommandLine::FromString( 26c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott L"program --foo= -bAr /Spaetzel=pierogi /Baz flim " 27c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott L"--other-switches=\"--dog=canine --cat=feline\" " 28c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott L"-spaetzle=Crepe -=loosevalue flan " 29c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott L"--input-translation=\"45\"--output-rotation " 303345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick L"--quotes=" TRICKY_QUOTED L" " 31c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott L"-- -- --not-a-switch " 32c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott L"\"in the time of submarines...\""); 33c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_FALSE(cl.command_line_string().empty()); 34c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#elif defined(OS_POSIX) 35c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const char* argv[] = {"program", "--foo=", "-bar", 36c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "-spaetzel=pierogi", "-baz", "flim", 37c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "--other-switches=--dog=canine --cat=feline", 38c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "-spaetzle=Crepe", "-=loosevalue", "flan", 39c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "--input-translation=45--output-rotation", 40c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "--", "--", "--not-a-switch", 41c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "in the time of submarines..."}; 42c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CommandLine cl(arraysize(argv), argv); 43c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif 44c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_FALSE(cl.HasSwitch("cruller")); 45c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_FALSE(cl.HasSwitch("flim")); 46c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_FALSE(cl.HasSwitch("program")); 47c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_FALSE(cl.HasSwitch("dog")); 48c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_FALSE(cl.HasSwitch("cat")); 49c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_FALSE(cl.HasSwitch("output-rotation")); 50c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_FALSE(cl.HasSwitch("not-a-switch")); 51c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_FALSE(cl.HasSwitch("--")); 52c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 53731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(), 54731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick cl.GetProgram().value()); 55c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 56c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_TRUE(cl.HasSwitch("foo")); 57c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_TRUE(cl.HasSwitch("bar")); 58c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_TRUE(cl.HasSwitch("baz")); 59c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_TRUE(cl.HasSwitch("spaetzle")); 60c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_WIN) 61c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_TRUE(cl.HasSwitch("SPAETZLE")); 62c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif 63c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_TRUE(cl.HasSwitch("other-switches")); 64c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_TRUE(cl.HasSwitch("input-translation")); 653345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#if defined(OS_WIN) 663345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick EXPECT_TRUE(cl.HasSwitch("quotes")); 673345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#endif 68c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 69c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle")); 70c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ("", cl.GetSwitchValueASCII("Foo")); 71c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ("", cl.GetSwitchValueASCII("bar")); 72c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ("", cl.GetSwitchValueASCII("cruller")); 73c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII( 74c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "other-switches")); 75c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation")); 763345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#if defined(OS_WIN) 773345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick EXPECT_EQ(TRICKY, cl.GetSwitchValueNative("quotes")); 783345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#endif 79c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 80c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const std::vector<CommandLine::StringType>& args = cl.args(); 81c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ASSERT_EQ(5U, args.size()); 82c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 83c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch std::vector<CommandLine::StringType>::const_iterator iter = args.begin(); 84c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter); 85c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ++iter; 86c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(FILE_PATH_LITERAL("flan"), *iter); 87c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ++iter; 88c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter); 89c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ++iter; 90c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter); 91c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ++iter; 92c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *iter); 93c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ++iter; 94c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_TRUE(iter == args.end()); 95c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_POSIX) 96c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const std::vector<std::string>& argvec = cl.argv(); 97c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 98c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott for (size_t i = 0; i < argvec.size(); i++) { 99c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ(0, argvec[i].compare(argv[i])); 100c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 101c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif 102c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 103c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 104c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Tests behavior with an empty input string. 105c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottTEST(CommandLineTest, EmptyString) { 106c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_WIN) 107c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CommandLine cl = CommandLine::FromString(L""); 108c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_TRUE(cl.command_line_string().empty()); 109731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick EXPECT_TRUE(cl.GetProgram().empty()); 110c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#elif defined(OS_POSIX) 111c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CommandLine cl(0, NULL); 112c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_TRUE(cl.argv().size() == 0); 113c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif 114c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(0U, cl.args().size()); 115c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 116c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 117c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Test methods for appending switches to a command line. 118c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottTEST(CommandLineTest, AppendSwitches) { 119c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott std::string switch1 = "switch1"; 120c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott std::string switch2 = "switch2"; 1213345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick std::string value = "value"; 122c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott std::string switch3 = "switch3"; 1233345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick std::string value3 = "a value with spaces"; 124c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott std::string switch4 = "switch4"; 1253345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick std::string value4 = "\"a value with quotes\""; 1263345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick std::string switch5 = "quotes"; 1273345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick std::string value5 = WideToUTF8(TRICKY); 128c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 129c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CommandLine cl(FilePath(FILE_PATH_LITERAL("Program"))); 130c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 131c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott cl.AppendSwitch(switch1); 1323345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick cl.AppendSwitchASCII(switch2, value); 1333345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick cl.AppendSwitchASCII(switch3, value3); 1343345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick cl.AppendSwitchASCII(switch4, value4); 1353345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick cl.AppendSwitchASCII(switch5, value5); 136c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 137c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_TRUE(cl.HasSwitch(switch1)); 138c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_TRUE(cl.HasSwitch(switch2)); 1393345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick EXPECT_EQ(value, cl.GetSwitchValueASCII(switch2)); 140c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_TRUE(cl.HasSwitch(switch3)); 1413345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3)); 142c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_TRUE(cl.HasSwitch(switch4)); 1433345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4)); 1443345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick EXPECT_TRUE(cl.HasSwitch(switch5)); 1453345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick EXPECT_EQ(value5, cl.GetSwitchValueASCII(switch5)); 1463345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick 1473345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#if defined(OS_WIN) 1483345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick EXPECT_EQ(L"\"Program\" " 1493345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick L"--switch1 " 1503345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick L"--switch2=value " 1513345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick L"--switch3=\"a value with spaces\" " 1523345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick L"--switch4=\"\\\"a value with quotes\\\"\" " 1533345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick L"--quotes=\"" TRICKY_QUOTED L"\"", 1543345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick cl.command_line_string()); 1553345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#endif 156c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 1574a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch 1584a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch// Tests that when AppendArguments is called that the program is set correctly 1594a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch// on the target CommandLine object and the switches from the source 1604a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch// CommandLine are added to the target. 1614a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben MurdochTEST(CommandLineTest, AppendArguments) { 1624a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch CommandLine cl1(FilePath(FILE_PATH_LITERAL("Program"))); 1634a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch cl1.AppendSwitch("switch1"); 1644a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch cl1.AppendSwitchASCII("switch2", "foo"); 1654a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch 1664a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch CommandLine cl2(CommandLine::NO_PROGRAM); 1674a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch cl2.AppendArguments(cl1, true); 1684a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch EXPECT_EQ(cl1.GetProgram().value(), cl2.GetProgram().value()); 1694a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch EXPECT_EQ(cl1.command_line_string(), cl2.command_line_string()); 1704a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch 1714a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch CommandLine c1(FilePath(FILE_PATH_LITERAL("Program1"))); 1724a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch c1.AppendSwitch("switch1"); 1734a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch CommandLine c2(FilePath(FILE_PATH_LITERAL("Program2"))); 1744a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch c2.AppendSwitch("switch2"); 1754a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch 1764a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch c1.AppendArguments(c2, true); 1774a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch EXPECT_EQ(c1.GetProgram().value(), c2.GetProgram().value()); 1784a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch EXPECT_TRUE(c1.HasSwitch("switch1")); 1794a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch EXPECT_TRUE(c1.HasSwitch("switch2")); 1804a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch} 1814a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch 182