diff options
author | evan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-10 18:14:19 +0000 |
---|---|---|
committer | evan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-10 18:14:19 +0000 |
commit | 98a1c268c3cf3ad6cc34e994ca3c50bbfdf6ba69 (patch) | |
tree | 12ada13b88bb567f4c4d1622f5eaa453d1a0c5ec /base/command_line_unittest.cc | |
parent | 4a47d7612c2f80b6dd760fdea9ba48d9e3704057 (diff) | |
download | chromium_src-98a1c268c3cf3ad6cc34e994ca3c50bbfdf6ba69.zip chromium_src-98a1c268c3cf3ad6cc34e994ca3c50bbfdf6ba69.tar.gz chromium_src-98a1c268c3cf3ad6cc34e994ca3c50bbfdf6ba69.tar.bz2 |
Factor out command-line quoting code on Windows.
Our command line code is not very good with respect to getting
quoting right. Because of this, callers will sometimes themselves
attempt to quote arguments, though some of them get it wrong(!) by
just surrounding with quotes.
The first step is to add a quoting function that we think is correct
and update the unit test to test it.
(Note that most of this is Windows-specific, because on POSIX
we can pass arguments to commands as a vector -- trying to do
quoting on POSIX is usually wrong.)
Review URL: http://codereview.chromium.org/3007038
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@55591 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/command_line_unittest.cc')
-rw-r--r-- | base/command_line_unittest.cc | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/base/command_line_unittest.cc b/base/command_line_unittest.cc index e44f240..8c8803c 100644 --- a/base/command_line_unittest.cc +++ b/base/command_line_unittest.cc @@ -11,6 +11,15 @@ #include "base/string_util.h" #include "testing/gtest/include/gtest/gtest.h" +// To test Windows quoting behavior, we use a string that has some backslashes +// and quotes. +// Consider the command-line argument: q\"bs1\bs2\\bs3q\\\" +// Here it is with C-style escapes. +#define TRICKY_QUOTED L"q\\\"bs1\\bs2\\\\bs3q\\\\\\\"" +// It should be parsed by Windows as: q"bs1\bs2\\bs3q\" +// Here that is with C-style escapes. +#define TRICKY L"q\"bs1\\bs2\\\\bs3q\\\"" + TEST(CommandLineTest, CommandLineConstructor) { #if defined(OS_WIN) CommandLine cl = CommandLine::FromString( @@ -18,6 +27,7 @@ TEST(CommandLineTest, CommandLineConstructor) { L"--other-switches=\"--dog=canine --cat=feline\" " L"-spaetzle=Crepe -=loosevalue flan " L"--input-translation=\"45\"--output-rotation " + L"--quotes=" TRICKY_QUOTED L" " L"-- -- --not-a-switch " L"\"in the time of submarines...\""); EXPECT_FALSE(cl.command_line_string().empty()); @@ -51,6 +61,9 @@ TEST(CommandLineTest, CommandLineConstructor) { #endif EXPECT_TRUE(cl.HasSwitch("other-switches")); EXPECT_TRUE(cl.HasSwitch("input-translation")); +#if defined(OS_WIN) + EXPECT_TRUE(cl.HasSwitch("quotes")); +#endif EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle")); EXPECT_EQ("", cl.GetSwitchValueASCII("Foo")); @@ -59,6 +72,9 @@ TEST(CommandLineTest, CommandLineConstructor) { EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII( "other-switches")); EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation")); +#if defined(OS_WIN) + EXPECT_EQ(TRICKY, cl.GetSwitchValueNative("quotes")); +#endif const std::vector<CommandLine::StringType>& args = cl.args(); ASSERT_EQ(5U, args.size()); @@ -106,6 +122,8 @@ TEST(CommandLineTest, AppendSwitches) { std::string value3 = "a value with spaces"; std::string switch4 = "switch4"; std::string value4 = "\"a value with quotes\""; + std::string switch5 = "quotes"; + std::string value5 = WideToASCII(TRICKY); CommandLine cl(FilePath(FILE_PATH_LITERAL("Program"))); @@ -113,6 +131,7 @@ TEST(CommandLineTest, AppendSwitches) { cl.AppendSwitchASCII(switch2, value); cl.AppendSwitchASCII(switch3, value3); cl.AppendSwitchASCII(switch4, value4); + cl.AppendSwitchASCII(switch5, value5); EXPECT_TRUE(cl.HasSwitch(switch1)); EXPECT_TRUE(cl.HasSwitch(switch2)); @@ -121,4 +140,16 @@ TEST(CommandLineTest, AppendSwitches) { EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3)); EXPECT_TRUE(cl.HasSwitch(switch4)); EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4)); + EXPECT_TRUE(cl.HasSwitch(switch5)); + EXPECT_EQ(value5, cl.GetSwitchValueASCII(switch5)); + +#if defined(OS_WIN) + EXPECT_EQ(L"\"Program\" " + L"--switch1 " + L"--switch2=value " + L"--switch3=\"a value with spaces\" " + L"--switch4=\"\\\"a value with quotes\\\"\" " + L"--quotes=\"" TRICKY_QUOTED L"\"", + cl.command_line_string()); +#endif } |