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; | |
Mark Mentovai
2014/10/28 22:34:28
You should also ++i again since you don’t want to
Robert Sesek
2014/10/29 19:52:01
Done.
| |
34 } | |
35 } | |
36 return lines; | |
Mark Mentovai
2014/10/28 22:34:28
This should signal somehow if the string didn’t en
Robert Sesek
2014/10/29 19:52:01
I don't think it's an error to not end in CRLF. Th
| |
37 } | |
38 | |
39 // In the tests below, the form data pairs don’t appear in the order they were | |
40 // added. The current implementation uses a std::map which sorts keys, so the | |
41 // entires appear in alphabetical order. However, this is an implementation | |
42 // detail, and it’s OK if the writer stops sorting in this order. Testing for | |
43 // a specific order is just the easiest way to write this test while the writer | |
44 // will output things in a known order. | |
45 | |
46 TEST(HTTPMultipartBuilder, ThreeStringFields) { | |
47 HTTPMultipartBuilder builder; | |
48 | |
49 const char kKey1[] = "key1"; | |
50 const char kValue1[] = "test"; | |
51 builder.SetFormData(kKey1, kValue1); | |
52 | |
53 const char kKey2[] = "key2"; | |
54 const char kValue2[] = "This is another test."; | |
55 builder.SetFormData(kKey2, kValue2); | |
56 | |
57 const char kKey3[] = "key-three"; | |
58 const char kValue3[] = "More tests"; | |
59 builder.SetFormData(kKey3, kValue3); | |
60 | |
61 scoped_ptr<HTTPBodyStream> body(builder.GetBodyStream()); | |
62 ASSERT_TRUE(body.get()); | |
63 std::string contents = ReadStreamToString(body.get()); | |
64 auto lines = SplitCRLF(contents); | |
65 auto lines_it = lines.begin(); | |
66 | |
67 // The first line is the boundary. All subsequent boundaries must match this. | |
68 const std::string& boundary = *lines_it++; | |
69 EXPECT_GT(boundary.length(), 1u); | |
Mark Mentovai
2014/10/28 22:34:28
EXPECT_GE, the standard allows a boundary of one c
Robert Sesek
2014/10/29 19:52:01
Done.
| |
70 EXPECT_LE(boundary.length(), 70u); | |
71 | |
72 EXPECT_EQ("Content-Disposition: form-data; name=\"key-three\"", *lines_it++); | |
73 EXPECT_EQ("", *lines_it++); | |
74 EXPECT_EQ(kValue3, *lines_it++); | |
75 | |
76 EXPECT_EQ(boundary, *lines_it++); | |
77 EXPECT_EQ("Content-Disposition: form-data; name=\"key1\"", *lines_it++); | |
78 EXPECT_EQ("", *lines_it++); | |
79 EXPECT_EQ(kValue1, *lines_it++); | |
80 | |
81 EXPECT_EQ(boundary, *lines_it++); | |
82 EXPECT_EQ("Content-Disposition: form-data; name=\"key2\"", *lines_it++); | |
83 EXPECT_EQ("", *lines_it++); | |
84 EXPECT_EQ(kValue2, *lines_it++); | |
85 | |
86 EXPECT_EQ(lines.end(), lines_it); | |
87 } | |
88 | |
89 TEST(HTTPMultipartBuilder, OverwriteFormDataWithEscapedKey) { | |
90 HTTPMultipartBuilder builder; | |
91 const char kKey[] = "a 100% \"silly\"\r\ntest"; | |
92 builder.SetFormData(kKey, "some dummy value"); | |
93 builder.SetFormData(kKey, "overwrite"); | |
94 scoped_ptr<HTTPBodyStream> body(builder.GetBodyStream()); | |
95 ASSERT_TRUE(body.get()); | |
96 std::string contents = ReadStreamToString(body.get()); | |
97 auto lines = SplitCRLF(contents); | |
98 auto lines_it = lines.begin(); | |
99 | |
100 const std::string& boundary = *lines_it++; | |
101 EXPECT_GT(boundary.length(), 1u); | |
102 EXPECT_LE(boundary.length(), 70u); | |
103 | |
104 EXPECT_EQ( | |
105 "Content-Disposition: form-data; name=\"a 100%25 %22silly%22%0d%0atest\"", | |
106 *lines_it++); | |
107 EXPECT_EQ("", *lines_it++); | |
108 EXPECT_EQ("overwrite", *lines_it++); | |
109 EXPECT_EQ(lines.end(), lines_it); | |
110 } | |
111 | |
112 TEST(HTTPMultipartBuilder, OverwriteFileAttachment) { | |
113 HTTPMultipartBuilder builder; | |
114 const char kValue[] = "1 2 3 test"; | |
115 builder.SetFormData("a key", kValue); | |
116 // TODO(rsesek): Use a more robust mechanism to locate testdata | |
117 // <https://code.google.com/p/crashpad/issues/detail?id=4>. | |
118 builder.SetFileAttachment("minidump", | |
119 base::FilePath("util/net/testdata/binary_http_body.dat")); | |
120 builder.SetFileAttachment("minidump", | |
121 base::FilePath("util/net/testdata/ascii_http_body.txt")); | |
122 scoped_ptr<HTTPBodyStream> body(builder.GetBodyStream()); | |
123 ASSERT_TRUE(body.get()); | |
124 std::string contents = ReadStreamToString(body.get()); | |
125 auto lines = SplitCRLF(contents); | |
126 auto lines_it = lines.begin(); | |
127 | |
128 const std::string& boundary = *lines_it++; | |
129 EXPECT_GT(boundary.length(), 1u); | |
130 EXPECT_LE(boundary.length(), 70u); | |
131 | |
132 EXPECT_EQ("Content-Disposition: form-data; name=\"a key\"", *lines_it++); | |
133 EXPECT_EQ("", *lines_it++); | |
134 EXPECT_EQ(kValue, *lines_it++); | |
135 | |
136 EXPECT_EQ(boundary, *lines_it++); | |
137 EXPECT_EQ("Content-Disposition: form-data; name=\"minidump\"", *lines_it++); | |
138 EXPECT_EQ("", *lines_it++); | |
139 EXPECT_EQ("This is a test.\n", *lines_it++); | |
140 | |
141 EXPECT_EQ(lines.end(), lines_it); | |
142 } | |
143 | |
144 } // namespace | |
145 } // namespace test | |
146 } // namespace crashpad | |
OLD | NEW |