OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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/net/http_multipart_builder.h" |
| 16 |
| 17 #include <vector> |
| 18 |
| 19 #include "gtest/gtest.h" |
| 20 #include "util/net/http_body.h" |
| 21 #include "util/net/http_body_test_util.h" |
| 22 |
| 23 namespace crashpad { |
| 24 namespace test { |
| 25 namespace { |
| 26 |
| 27 std::vector<std::string> SplitCRLF(const std::string& string) { |
| 28 std::vector<std::string> lines; |
| 29 size_t last_line = 0; |
| 30 for (size_t i = 0; i < string.length(); ++i) { |
| 31 if (string[i] == '\r' && i+1 < string.length() && string[i+1] == '\n') { |
| 32 lines.push_back(string.substr(last_line, i - last_line)); |
| 33 last_line = i + 2; |
| 34 ++i; |
| 35 } |
| 36 } |
| 37 // Append any remainder. |
| 38 if (last_line < string.length()) { |
| 39 lines.push_back(string.substr(last_line)); |
| 40 } |
| 41 return lines; |
| 42 } |
| 43 |
| 44 // In the tests below, the form data pairs don’t appear in the order they were |
| 45 // added. The current implementation uses a std::map which sorts keys, so the |
| 46 // entires appear in alphabetical order. However, this is an implementation |
| 47 // detail, and it’s OK if the writer stops sorting in this order. Testing for |
| 48 // a specific order is just the easiest way to write this test while the writer |
| 49 // will output things in a known order. |
| 50 |
| 51 TEST(HTTPMultipartBuilder, ThreeStringFields) { |
| 52 HTTPMultipartBuilder builder; |
| 53 |
| 54 const char kKey1[] = "key1"; |
| 55 const char kValue1[] = "test"; |
| 56 builder.SetFormData(kKey1, kValue1); |
| 57 |
| 58 const char kKey2[] = "key2"; |
| 59 const char kValue2[] = "This is another test."; |
| 60 builder.SetFormData(kKey2, kValue2); |
| 61 |
| 62 const char kKey3[] = "key-three"; |
| 63 const char kValue3[] = "More tests"; |
| 64 builder.SetFormData(kKey3, kValue3); |
| 65 |
| 66 scoped_ptr<HTTPBodyStream> body(builder.GetBodyStream()); |
| 67 ASSERT_TRUE(body.get()); |
| 68 std::string contents = ReadStreamToString(body.get()); |
| 69 auto lines = SplitCRLF(contents); |
| 70 auto lines_it = lines.begin(); |
| 71 |
| 72 // The first line is the boundary. All subsequent boundaries must match this. |
| 73 const std::string& boundary = *lines_it++; |
| 74 EXPECT_GE(boundary.length(), 1u); |
| 75 EXPECT_LE(boundary.length(), 70u); |
| 76 |
| 77 EXPECT_EQ("Content-Disposition: form-data; name=\"key-three\"", *lines_it++); |
| 78 EXPECT_EQ("", *lines_it++); |
| 79 EXPECT_EQ(kValue3, *lines_it++); |
| 80 |
| 81 EXPECT_EQ(boundary, *lines_it++); |
| 82 EXPECT_EQ("Content-Disposition: form-data; name=\"key1\"", *lines_it++); |
| 83 EXPECT_EQ("", *lines_it++); |
| 84 EXPECT_EQ(kValue1, *lines_it++); |
| 85 |
| 86 EXPECT_EQ(boundary, *lines_it++); |
| 87 EXPECT_EQ("Content-Disposition: form-data; name=\"key2\"", *lines_it++); |
| 88 EXPECT_EQ("", *lines_it++); |
| 89 EXPECT_EQ(kValue2, *lines_it++); |
| 90 |
| 91 EXPECT_EQ(boundary + "--", *lines_it++); |
| 92 |
| 93 EXPECT_EQ(lines.end(), lines_it); |
| 94 } |
| 95 |
| 96 TEST(HTTPMultipartBuilder, ThreeFileAttachments) { |
| 97 HTTPMultipartBuilder builder; |
| 98 // TODO(rsesek): Use a more robust mechanism to locate testdata |
| 99 // <https://code.google.com/p/crashpad/issues/detail?id=4>. |
| 100 builder.SetFileAttachment("first", "minidump.dmp", |
| 101 base::FilePath("util/net/testdata/ascii_http_body.txt"), ""); |
| 102 builder.SetFileAttachment("second", "minidump.dmp", |
| 103 base::FilePath("util/net/testdata/ascii_http_body.txt"), "text/plain"); |
| 104 builder.SetFileAttachment("\"third 50% silly\"", "test%foo.txt", |
| 105 base::FilePath("util/net/testdata/ascii_http_body.txt"), "text/plain"); |
| 106 |
| 107 const char kFileContents[] = "This is a test.\n"; |
| 108 |
| 109 scoped_ptr<HTTPBodyStream> body(builder.GetBodyStream()); |
| 110 ASSERT_TRUE(body.get()); |
| 111 std::string contents = ReadStreamToString(body.get()); |
| 112 auto lines = SplitCRLF(contents); |
| 113 auto lines_it = lines.begin(); |
| 114 |
| 115 const std::string& boundary = *lines_it++; |
| 116 EXPECT_GE(boundary.length(), 1u); |
| 117 EXPECT_LE(boundary.length(), 70u); |
| 118 |
| 119 EXPECT_EQ("Content-Disposition: form-data; " |
| 120 "name=\"%22third 50%25 silly%22\"; filename=\"test%25foo.txt\"", |
| 121 *lines_it++); |
| 122 EXPECT_EQ("Content-Type: text/plain", *lines_it++); |
| 123 EXPECT_EQ("", *lines_it++); |
| 124 EXPECT_EQ(kFileContents, *lines_it++); |
| 125 |
| 126 EXPECT_EQ(boundary, *lines_it++); |
| 127 EXPECT_EQ("Content-Disposition: form-data; " |
| 128 "name=\"first\"; filename=\"minidump.dmp\"", |
| 129 *lines_it++); |
| 130 EXPECT_EQ("Content-Type: application/octet-stream", *lines_it++); |
| 131 EXPECT_EQ("", *lines_it++); |
| 132 EXPECT_EQ(kFileContents, *lines_it++); |
| 133 |
| 134 EXPECT_EQ(boundary, *lines_it++); |
| 135 EXPECT_EQ("Content-Disposition: form-data; " |
| 136 "name=\"second\"; filename=\"minidump.dmp\"", |
| 137 *lines_it++); |
| 138 EXPECT_EQ("Content-Type: text/plain", *lines_it++); |
| 139 EXPECT_EQ("", *lines_it++); |
| 140 EXPECT_EQ(kFileContents, *lines_it++); |
| 141 |
| 142 EXPECT_EQ(boundary + "--", *lines_it++); |
| 143 |
| 144 EXPECT_EQ(lines.end(), lines_it); |
| 145 } |
| 146 |
| 147 TEST(HTTPMultipartBuilder, OverwriteFormDataWithEscapedKey) { |
| 148 HTTPMultipartBuilder builder; |
| 149 const char kKey[] = "a 100% \"silly\"\r\ntest"; |
| 150 builder.SetFormData(kKey, "some dummy value"); |
| 151 builder.SetFormData(kKey, "overwrite"); |
| 152 scoped_ptr<HTTPBodyStream> body(builder.GetBodyStream()); |
| 153 ASSERT_TRUE(body.get()); |
| 154 std::string contents = ReadStreamToString(body.get()); |
| 155 auto lines = SplitCRLF(contents); |
| 156 auto lines_it = lines.begin(); |
| 157 |
| 158 const std::string& boundary = *lines_it++; |
| 159 EXPECT_GE(boundary.length(), 1u); |
| 160 EXPECT_LE(boundary.length(), 70u); |
| 161 |
| 162 EXPECT_EQ( |
| 163 "Content-Disposition: form-data; name=\"a 100%25 %22silly%22%0d%0atest\"", |
| 164 *lines_it++); |
| 165 EXPECT_EQ("", *lines_it++); |
| 166 EXPECT_EQ("overwrite", *lines_it++); |
| 167 EXPECT_EQ(boundary + "--", *lines_it++); |
| 168 EXPECT_EQ(lines.end(), lines_it); |
| 169 } |
| 170 |
| 171 TEST(HTTPMultipartBuilder, OverwriteFileAttachment) { |
| 172 HTTPMultipartBuilder builder; |
| 173 const char kValue[] = "1 2 3 test"; |
| 174 builder.SetFormData("a key", kValue); |
| 175 // TODO(rsesek): Use a more robust mechanism to locate testdata |
| 176 // <https://code.google.com/p/crashpad/issues/detail?id=4>. |
| 177 builder.SetFileAttachment("minidump", "minidump.dmp", |
| 178 base::FilePath("util/net/testdata/binary_http_body.dat"), ""); |
| 179 builder.SetFileAttachment("minidump2", "minidump.dmp", |
| 180 base::FilePath("util/net/testdata/binary_http_body.dat"), ""); |
| 181 builder.SetFileAttachment("minidump", "minidump.dmp", |
| 182 base::FilePath("util/net/testdata/ascii_http_body.txt"), "text/plain"); |
| 183 scoped_ptr<HTTPBodyStream> body(builder.GetBodyStream()); |
| 184 ASSERT_TRUE(body.get()); |
| 185 std::string contents = ReadStreamToString(body.get()); |
| 186 auto lines = SplitCRLF(contents); |
| 187 auto lines_it = lines.begin(); |
| 188 |
| 189 const std::string& boundary = *lines_it++; |
| 190 EXPECT_GE(boundary.length(), 1u); |
| 191 EXPECT_LE(boundary.length(), 70u); |
| 192 |
| 193 EXPECT_EQ("Content-Disposition: form-data; name=\"a key\"", *lines_it++); |
| 194 EXPECT_EQ("", *lines_it++); |
| 195 EXPECT_EQ(kValue, *lines_it++); |
| 196 |
| 197 EXPECT_EQ(boundary, *lines_it++); |
| 198 EXPECT_EQ("Content-Disposition: form-data; " |
| 199 "name=\"minidump\"; filename=\"minidump.dmp\"", |
| 200 *lines_it++); |
| 201 EXPECT_EQ("Content-Type: text/plain", *lines_it++); |
| 202 EXPECT_EQ("", *lines_it++); |
| 203 EXPECT_EQ("This is a test.\n", *lines_it++); |
| 204 |
| 205 EXPECT_EQ(boundary, *lines_it++); |
| 206 EXPECT_EQ("Content-Disposition: form-data; " |
| 207 "name=\"minidump2\"; filename=\"minidump.dmp\"", |
| 208 *lines_it++); |
| 209 EXPECT_EQ("Content-Type: application/octet-stream", *lines_it++); |
| 210 EXPECT_EQ("", *lines_it++); |
| 211 EXPECT_EQ("\xFE\xED\xFA\xCE\xA1\x1A\x15", *lines_it++); |
| 212 |
| 213 EXPECT_EQ(boundary + "--", *lines_it++); |
| 214 |
| 215 EXPECT_EQ(lines.end(), lines_it); |
| 216 } |
| 217 |
| 218 TEST(HTTPMultipartBuilder, SharedFormDataAndAttachmentKeyNamespace) { |
| 219 HTTPMultipartBuilder builder; |
| 220 const char kValue1[] = "11111"; |
| 221 builder.SetFormData("one", kValue1); |
| 222 builder.SetFileAttachment("minidump", "minidump.dmp", |
| 223 base::FilePath("util/net/testdata/ascii_http_body.txt"), ""); |
| 224 const char kValue2[] = "this is not a file"; |
| 225 builder.SetFormData("minidump", kValue2); |
| 226 |
| 227 scoped_ptr<HTTPBodyStream> body(builder.GetBodyStream()); |
| 228 ASSERT_TRUE(body.get()); |
| 229 std::string contents = ReadStreamToString(body.get()); |
| 230 auto lines = SplitCRLF(contents); |
| 231 auto lines_it = lines.begin(); |
| 232 |
| 233 const std::string& boundary = *lines_it++; |
| 234 EXPECT_GE(boundary.length(), 1u); |
| 235 EXPECT_LE(boundary.length(), 70u); |
| 236 |
| 237 EXPECT_EQ("Content-Disposition: form-data; name=\"minidump\"", *lines_it++); |
| 238 EXPECT_EQ("", *lines_it++); |
| 239 EXPECT_EQ(kValue2, *lines_it++); |
| 240 |
| 241 EXPECT_EQ(boundary, *lines_it++); |
| 242 EXPECT_EQ("Content-Disposition: form-data; name=\"one\"", *lines_it++); |
| 243 EXPECT_EQ("", *lines_it++); |
| 244 EXPECT_EQ(kValue1, *lines_it++); |
| 245 |
| 246 EXPECT_EQ(boundary + "--", *lines_it++); |
| 247 |
| 248 EXPECT_EQ(lines.end(), lines_it); |
| 249 } |
| 250 |
| 251 TEST(HTTPMultipartBuilder, AssertUnsafeMIMEType) { |
| 252 HTTPMultipartBuilder builder; |
| 253 // Invalid and potentially dangerous: |
| 254 ASSERT_DEATH(builder.SetFileAttachment("", "", base::FilePath(), "\r\n"), ""); |
| 255 ASSERT_DEATH(builder.SetFileAttachment("", "", base::FilePath(), "\""), ""); |
| 256 ASSERT_DEATH(builder.SetFileAttachment("", "", base::FilePath(), "\x12"), ""); |
| 257 ASSERT_DEATH(builder.SetFileAttachment("", "", base::FilePath(), "<>"), ""); |
| 258 // Invalid but safe: |
| 259 builder.SetFileAttachment("", "", base::FilePath(), "0/totally/-invalid.pdf"); |
| 260 } |
| 261 |
| 262 } // namespace |
| 263 } // namespace test |
| 264 } // namespace crashpad |
OLD | NEW |