OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "util/win/command_line.h" |
| 16 |
| 17 #include <windows.h> |
| 18 #include <shellapi.h> |
| 19 |
| 20 #include "base/basictypes.h" |
| 21 #include "base/logging.h" |
| 22 #include "base/scoped_generic.h" |
| 23 #include "gtest/gtest.h" |
| 24 #include "test/errors.h" |
| 25 |
| 26 namespace crashpad { |
| 27 namespace test { |
| 28 namespace { |
| 29 |
| 30 struct LocalAllocTraits { |
| 31 static HLOCAL InvalidValue() { |
| 32 return nullptr; |
| 33 } |
| 34 |
| 35 static void Free(HLOCAL memory) { |
| 36 PLOG_IF(ERROR, LocalFree(memory) != nullptr) << "LocalFree"; |
| 37 } |
| 38 }; |
| 39 using ScopedLocalAlloc = base::ScopedGeneric<HLOCAL, LocalAllocTraits>; |
| 40 |
| 41 // Calls AppendCommandLineArgument() for every argument in argv, then calls |
| 42 // CommandLineToArgvW() to decode the string into a vector again, and compares |
| 43 // the input and output. |
| 44 void AppendCommandLineArgumentTest(size_t argc, const wchar_t* argv[]) { |
| 45 std::wstring command_line; |
| 46 for (size_t index = 0; index < argc; ++index) { |
| 47 AppendCommandLineArgument(argv[index], &command_line); |
| 48 } |
| 49 |
| 50 int test_argc; |
| 51 wchar_t** test_argv = CommandLineToArgvW(command_line.c_str(), &test_argc); |
| 52 |
| 53 ASSERT_TRUE(test_argv) << ErrorMessage("CommandLineToArgvW"); |
| 54 ScopedLocalAlloc test_argv_owner(test_argv); |
| 55 ASSERT_EQ(argc, test_argc); |
| 56 |
| 57 for (size_t index = 0; index < argc; ++index) { |
| 58 EXPECT_STREQ(argv[index], test_argv[index]) << "index " << index; |
| 59 } |
| 60 EXPECT_FALSE(test_argv[argc]); |
| 61 } |
| 62 |
| 63 TEST(CommandLine, AppendCommandLineArgument) { |
| 64 // Most of these test cases come from |
| 65 // http://blogs.msdn.com/b/twistylittlepassagesallalike/archive/2011/04/23/eve
ryone-quotes-arguments-the-wrong-way.aspx, |
| 66 // which was also a reference for the implementation of |
| 67 // AppendCommandLineArgument(). |
| 68 |
| 69 { |
| 70 SCOPED_TRACE("simple"); |
| 71 |
| 72 const wchar_t* kArguments[] = { |
| 73 L"child.exe", |
| 74 L"argument 1", |
| 75 L"argument 2", |
| 76 }; |
| 77 AppendCommandLineArgumentTest(arraysize(kArguments), kArguments); |
| 78 } |
| 79 |
| 80 { |
| 81 SCOPED_TRACE("path with spaces"); |
| 82 |
| 83 const wchar_t* kArguments[] = { |
| 84 L"child.exe", |
| 85 L"argument1", |
| 86 L"argument 2", |
| 87 L"\\some\\path with\\spaces", |
| 88 }; |
| 89 AppendCommandLineArgumentTest(arraysize(kArguments), kArguments); |
| 90 } |
| 91 |
| 92 { |
| 93 SCOPED_TRACE("argument with embedded quotation marks"); |
| 94 |
| 95 const wchar_t* kArguments[] = { |
| 96 L"child.exe", |
| 97 L"argument1", |
| 98 L"she said, \"you had me at hello\"", |
| 99 L"\\some\\path with\\spaces", |
| 100 }; |
| 101 AppendCommandLineArgumentTest(arraysize(kArguments), kArguments); |
| 102 } |
| 103 |
| 104 { |
| 105 SCOPED_TRACE("argument with unbalanced quotation marks"); |
| 106 |
| 107 const wchar_t* kArguments[] = { |
| 108 L"child.exe", |
| 109 L"argument1", |
| 110 L"argument\"2", |
| 111 L"argument3", |
| 112 L"argument4", |
| 113 }; |
| 114 AppendCommandLineArgumentTest(arraysize(kArguments), kArguments); |
| 115 } |
| 116 |
| 117 { |
| 118 SCOPED_TRACE("argument ending with backslash"); |
| 119 |
| 120 const wchar_t* kArguments[] = { |
| 121 L"child.exe", |
| 122 L"\\some\\directory with\\spaces\\", |
| 123 L"argument2", |
| 124 }; |
| 125 AppendCommandLineArgumentTest(arraysize(kArguments), kArguments); |
| 126 } |
| 127 |
| 128 { |
| 129 SCOPED_TRACE("empty argument"); |
| 130 |
| 131 const wchar_t* kArguments[] = { |
| 132 L"child.exe", |
| 133 L"", |
| 134 L"argument2", |
| 135 }; |
| 136 AppendCommandLineArgumentTest(arraysize(kArguments), kArguments); |
| 137 } |
| 138 |
| 139 { |
| 140 SCOPED_TRACE("funny nonprintable characters"); |
| 141 |
| 142 const wchar_t* kArguments[] = { |
| 143 L"child.exe", |
| 144 L"argument 1", |
| 145 L"argument\t2", |
| 146 L"argument\n3", |
| 147 L"argument\v4", |
| 148 L"argument\"5", |
| 149 L" ", |
| 150 L"\t", |
| 151 L"\n", |
| 152 L"\v", |
| 153 L"\"", |
| 154 L" x", |
| 155 L"\tx", |
| 156 L"\nx", |
| 157 L"\vx", |
| 158 L"\"x", |
| 159 L"x ", |
| 160 L"x\t", |
| 161 L"x\n", |
| 162 L"x\v", |
| 163 L"x\"", |
| 164 L" ", |
| 165 L"\t\t", |
| 166 L"\n\n", |
| 167 L"\v\v", |
| 168 L"\"\"", |
| 169 L" \t\n\v\"", |
| 170 }; |
| 171 AppendCommandLineArgumentTest(arraysize(kArguments), kArguments); |
| 172 } |
| 173 } |
| 174 |
| 175 } // namespace |
| 176 } // namespace test |
| 177 } // namespace crashpad |
OLD | NEW |