Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(232)

Side by Side Diff: util/net/http_multipart_builder_test.cc

Issue 992503002: Locate test data more robustly (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: 2015 Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « util/net/http_body_test.cc ('k') | util/net/http_transport_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Crashpad Authors. All rights reserved. 1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with 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 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and 12 // See the License for the specific language governing permissions and
13 // limitations under the License. 13 // limitations under the License.
14 14
15 #include "util/net/http_multipart_builder.h" 15 #include "util/net/http_multipart_builder.h"
16 16
17 #include <vector> 17 #include <vector>
18 18
19 #include "gtest/gtest.h" 19 #include "gtest/gtest.h"
20 #include "util/net/http_body.h" 20 #include "util/net/http_body.h"
21 #include "util/net/http_body_test_util.h" 21 #include "util/net/http_body_test_util.h"
22 #include "util/test/paths.h"
22 23
23 namespace crashpad { 24 namespace crashpad {
24 namespace test { 25 namespace test {
25 namespace { 26 namespace {
26 27
27 std::vector<std::string> SplitCRLF(const std::string& string) { 28 std::vector<std::string> SplitCRLF(const std::string& string) {
28 std::vector<std::string> lines; 29 std::vector<std::string> lines;
29 size_t last_line = 0; 30 size_t last_line = 0;
30 for (size_t i = 0; i < string.length(); ++i) { 31 for (size_t i = 0; i < string.length(); ++i) {
31 if (string[i] == '\r' && i+1 < string.length() && string[i+1] == '\n') { 32 if (string[i] == '\r' && i+1 < string.length() && string[i+1] == '\n') {
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 EXPECT_EQ("", *lines_it++); 89 EXPECT_EQ("", *lines_it++);
89 EXPECT_EQ(kValue2, *lines_it++); 90 EXPECT_EQ(kValue2, *lines_it++);
90 91
91 EXPECT_EQ(boundary + "--", *lines_it++); 92 EXPECT_EQ(boundary + "--", *lines_it++);
92 93
93 EXPECT_EQ(lines.end(), lines_it); 94 EXPECT_EQ(lines.end(), lines_it);
94 } 95 }
95 96
96 TEST(HTTPMultipartBuilder, ThreeFileAttachments) { 97 TEST(HTTPMultipartBuilder, ThreeFileAttachments) {
97 HTTPMultipartBuilder builder; 98 HTTPMultipartBuilder builder;
98 // TODO(rsesek): Use a more robust mechanism to locate testdata 99 base::FilePath ascii_http_body_path = Paths::TestDataRoot().Append(
99 // <https://code.google.com/p/crashpad/issues/detail?id=4>. 100 FILE_PATH_LITERAL("util/net/testdata/ascii_http_body.txt"));
100 builder.SetFileAttachment("first", 101 builder.SetFileAttachment("first",
101 "minidump.dmp", 102 "minidump.dmp",
102 base::FilePath(FILE_PATH_LITERAL( 103 ascii_http_body_path,
103 "util/net/testdata/ascii_http_body.txt")),
104 ""); 104 "");
105 builder.SetFileAttachment("second", 105 builder.SetFileAttachment("second",
106 "minidump.dmp", 106 "minidump.dmp",
107 base::FilePath(FILE_PATH_LITERAL( 107 ascii_http_body_path,
108 "util/net/testdata/ascii_http_body.txt")),
109 "text/plain"); 108 "text/plain");
110 builder.SetFileAttachment("\"third 50% silly\"", 109 builder.SetFileAttachment("\"third 50% silly\"",
111 "test%foo.txt", 110 "test%foo.txt",
112 base::FilePath(FILE_PATH_LITERAL( 111 ascii_http_body_path,
113 "util/net/testdata/ascii_http_body.txt")),
114 "text/plain"); 112 "text/plain");
115 113
116 const char kFileContents[] = "This is a test.\n"; 114 const char kFileContents[] = "This is a test.\n";
117 115
118 scoped_ptr<HTTPBodyStream> body(builder.GetBodyStream()); 116 scoped_ptr<HTTPBodyStream> body(builder.GetBodyStream());
119 ASSERT_TRUE(body.get()); 117 ASSERT_TRUE(body.get());
120 std::string contents = ReadStreamToString(body.get()); 118 std::string contents = ReadStreamToString(body.get());
121 auto lines = SplitCRLF(contents); 119 auto lines = SplitCRLF(contents);
122 ASSERT_EQ(16u, lines.size()); 120 ASSERT_EQ(16u, lines.size());
123 auto lines_it = lines.begin(); 121 auto lines_it = lines.begin();
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 EXPECT_EQ("", *lines_it++); 173 EXPECT_EQ("", *lines_it++);
176 EXPECT_EQ("overwrite", *lines_it++); 174 EXPECT_EQ("overwrite", *lines_it++);
177 EXPECT_EQ(boundary + "--", *lines_it++); 175 EXPECT_EQ(boundary + "--", *lines_it++);
178 EXPECT_EQ(lines.end(), lines_it); 176 EXPECT_EQ(lines.end(), lines_it);
179 } 177 }
180 178
181 TEST(HTTPMultipartBuilder, OverwriteFileAttachment) { 179 TEST(HTTPMultipartBuilder, OverwriteFileAttachment) {
182 HTTPMultipartBuilder builder; 180 HTTPMultipartBuilder builder;
183 const char kValue[] = "1 2 3 test"; 181 const char kValue[] = "1 2 3 test";
184 builder.SetFormData("a key", kValue); 182 builder.SetFormData("a key", kValue);
185 // TODO(rsesek): Use a more robust mechanism to locate testdata 183 base::FilePath testdata_path =
186 // <https://code.google.com/p/crashpad/issues/detail?id=4>. 184 Paths::TestDataRoot().Append(FILE_PATH_LITERAL("util/net/testdata"));
187 builder.SetFileAttachment("minidump", 185 builder.SetFileAttachment("minidump",
188 "minidump.dmp", 186 "minidump.dmp",
189 base::FilePath(FILE_PATH_LITERAL( 187 testdata_path.Append(FILE_PATH_LITERAL(
190 "util/net/testdata/binary_http_body.dat")), 188 "binary_http_body.dat")),
191 ""); 189 "");
192 builder.SetFileAttachment("minidump2", 190 builder.SetFileAttachment("minidump2",
193 "minidump.dmp", 191 "minidump.dmp",
194 base::FilePath(FILE_PATH_LITERAL( 192 testdata_path.Append(FILE_PATH_LITERAL(
195 "util/net/testdata/binary_http_body.dat")), 193 "binary_http_body.dat")),
196 ""); 194 "");
197 builder.SetFileAttachment("minidump", 195 builder.SetFileAttachment("minidump",
198 "minidump.dmp", 196 "minidump.dmp",
199 base::FilePath(FILE_PATH_LITERAL( 197 testdata_path.Append(FILE_PATH_LITERAL(
200 "util/net/testdata/ascii_http_body.txt")), 198 "ascii_http_body.txt")),
201 "text/plain"); 199 "text/plain");
202 scoped_ptr<HTTPBodyStream> body(builder.GetBodyStream()); 200 scoped_ptr<HTTPBodyStream> body(builder.GetBodyStream());
203 ASSERT_TRUE(body.get()); 201 ASSERT_TRUE(body.get());
204 std::string contents = ReadStreamToString(body.get()); 202 std::string contents = ReadStreamToString(body.get());
205 auto lines = SplitCRLF(contents); 203 auto lines = SplitCRLF(contents);
206 ASSERT_EQ(15u, lines.size()); 204 ASSERT_EQ(15u, lines.size());
207 auto lines_it = lines.begin(); 205 auto lines_it = lines.begin();
208 206
209 const std::string& boundary = *lines_it++; 207 const std::string& boundary = *lines_it++;
210 EXPECT_GE(boundary.length(), 1u); 208 EXPECT_GE(boundary.length(), 1u);
(...skipping 21 matching lines...) Expand all
232 230
233 EXPECT_EQ(boundary + "--", *lines_it++); 231 EXPECT_EQ(boundary + "--", *lines_it++);
234 232
235 EXPECT_EQ(lines.end(), lines_it); 233 EXPECT_EQ(lines.end(), lines_it);
236 } 234 }
237 235
238 TEST(HTTPMultipartBuilder, SharedFormDataAndAttachmentKeyNamespace) { 236 TEST(HTTPMultipartBuilder, SharedFormDataAndAttachmentKeyNamespace) {
239 HTTPMultipartBuilder builder; 237 HTTPMultipartBuilder builder;
240 const char kValue1[] = "11111"; 238 const char kValue1[] = "11111";
241 builder.SetFormData("one", kValue1); 239 builder.SetFormData("one", kValue1);
240 base::FilePath ascii_http_body_path = Paths::TestDataRoot().Append(
241 FILE_PATH_LITERAL("util/net/testdata/ascii_http_body.txt"));
242 builder.SetFileAttachment("minidump", 242 builder.SetFileAttachment("minidump",
243 "minidump.dmp", 243 "minidump.dmp",
244 base::FilePath(FILE_PATH_LITERAL( 244 ascii_http_body_path,
245 "util/net/testdata/ascii_http_body.txt")),
246 ""); 245 "");
247 const char kValue2[] = "this is not a file"; 246 const char kValue2[] = "this is not a file";
248 builder.SetFormData("minidump", kValue2); 247 builder.SetFormData("minidump", kValue2);
249 248
250 scoped_ptr<HTTPBodyStream> body(builder.GetBodyStream()); 249 scoped_ptr<HTTPBodyStream> body(builder.GetBodyStream());
251 ASSERT_TRUE(body.get()); 250 ASSERT_TRUE(body.get());
252 std::string contents = ReadStreamToString(body.get()); 251 std::string contents = ReadStreamToString(body.get());
253 auto lines = SplitCRLF(contents); 252 auto lines = SplitCRLF(contents);
254 auto lines_it = lines.begin(); 253 auto lines_it = lines.begin();
255 254
(...skipping 24 matching lines...) Expand all
280 ASSERT_DEATH(builder.SetFileAttachment("", "", base::FilePath(), "<>"), ""); 279 ASSERT_DEATH(builder.SetFileAttachment("", "", base::FilePath(), "<>"), "");
281 // Invalid but safe: 280 // Invalid but safe:
282 builder.SetFileAttachment("", "", base::FilePath(), "0/totally/-invalid.pdf"); 281 builder.SetFileAttachment("", "", base::FilePath(), "0/totally/-invalid.pdf");
283 // Valid and safe: 282 // Valid and safe:
284 builder.SetFileAttachment("", "", base::FilePath(), "application/xml+xhtml"); 283 builder.SetFileAttachment("", "", base::FilePath(), "application/xml+xhtml");
285 } 284 }
286 285
287 } // namespace 286 } // namespace
288 } // namespace test 287 } // namespace test
289 } // namespace crashpad 288 } // namespace crashpad
OLDNEW
« no previous file with comments | « util/net/http_body_test.cc ('k') | util/net/http_transport_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698