OLD | NEW |
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, |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include "base/format_macros.h" | 24 #include "base/format_macros.h" |
25 #include "base/logging.h" | 25 #include "base/logging.h" |
26 #include "base/memory/scoped_ptr.h" | 26 #include "base/memory/scoped_ptr.h" |
27 #include "base/strings/stringprintf.h" | 27 #include "base/strings/stringprintf.h" |
28 #include "base/strings/utf_string_conversions.h" | 28 #include "base/strings/utf_string_conversions.h" |
29 #include "build/build_config.h" | 29 #include "build/build_config.h" |
30 #include "gtest/gtest.h" | 30 #include "gtest/gtest.h" |
31 #include "test/multiprocess_exec.h" | 31 #include "test/multiprocess_exec.h" |
32 #include "test/paths.h" | 32 #include "test/paths.h" |
33 #include "util/file/file_io.h" | 33 #include "util/file/file_io.h" |
| 34 #include "util/stdlib/move.h" |
34 #include "util/misc/random_string.h" | 35 #include "util/misc/random_string.h" |
35 #include "util/net/http_body.h" | 36 #include "util/net/http_body.h" |
36 #include "util/net/http_headers.h" | 37 #include "util/net/http_headers.h" |
37 #include "util/net/http_multipart_builder.h" | 38 #include "util/net/http_multipart_builder.h" |
38 | 39 |
39 namespace crashpad { | 40 namespace crashpad { |
40 namespace test { | 41 namespace test { |
41 namespace { | 42 namespace { |
42 | 43 |
43 class HTTPTransportTestFixture : public MultiprocessExec { | 44 class HTTPTransportTestFixture : public MultiprocessExec { |
44 public: | 45 public: |
45 using RequestValidator = | 46 using RequestValidator = |
46 void(*)(HTTPTransportTestFixture*, const std::string&); | 47 void(*)(HTTPTransportTestFixture*, const std::string&); |
47 | 48 |
48 HTTPTransportTestFixture(const HTTPHeaders& headers, | 49 HTTPTransportTestFixture(const HTTPHeaders& headers, |
49 scoped_ptr<HTTPBodyStream> body_stream, | 50 scoped_ptr<HTTPBodyStream> body_stream, |
50 uint16_t http_response_code, | 51 uint16_t http_response_code, |
51 RequestValidator request_validator) | 52 RequestValidator request_validator) |
52 : MultiprocessExec(), | 53 : MultiprocessExec(), |
53 headers_(headers), | 54 headers_(headers), |
54 body_stream_(body_stream.Pass()), | 55 body_stream_(crashpad::move(body_stream)), |
55 response_code_(http_response_code), | 56 response_code_(http_response_code), |
56 request_validator_(request_validator) { | 57 request_validator_(request_validator) { |
57 base::FilePath server_path = Paths::TestDataRoot().Append( | 58 base::FilePath server_path = Paths::TestDataRoot().Append( |
58 FILE_PATH_LITERAL("util/net/http_transport_test_server.py")); | 59 FILE_PATH_LITERAL("util/net/http_transport_test_server.py")); |
59 #if defined(OS_POSIX) | 60 #if defined(OS_POSIX) |
60 SetChildCommand(server_path.value(), nullptr); | 61 SetChildCommand(server_path.value(), nullptr); |
61 #elif defined(OS_WIN) | 62 #elif defined(OS_WIN) |
62 // Explicitly invoke a shell and python so that python can be found in the | 63 // Explicitly invoke a shell and python so that python can be found in the |
63 // path, and run the test script. | 64 // path, and run the test script. |
64 std::vector<std::string> args; | 65 std::vector<std::string> args; |
(...skipping 30 matching lines...) Expand all Loading... |
95 random_string.c_str(), | 96 random_string.c_str(), |
96 random_string.size())); | 97 random_string.size())); |
97 | 98 |
98 // Now execute the HTTP request. | 99 // Now execute the HTTP request. |
99 scoped_ptr<HTTPTransport> transport(HTTPTransport::Create()); | 100 scoped_ptr<HTTPTransport> transport(HTTPTransport::Create()); |
100 transport->SetMethod("POST"); | 101 transport->SetMethod("POST"); |
101 transport->SetURL(base::StringPrintf("http://127.0.0.1:%d/upload", port)); | 102 transport->SetURL(base::StringPrintf("http://127.0.0.1:%d/upload", port)); |
102 for (const auto& pair : headers_) { | 103 for (const auto& pair : headers_) { |
103 transport->SetHeader(pair.first, pair.second); | 104 transport->SetHeader(pair.first, pair.second); |
104 } | 105 } |
105 transport->SetBodyStream(body_stream_.Pass()); | 106 transport->SetBodyStream(crashpad::move(body_stream_)); |
106 | 107 |
107 std::string response_body; | 108 std::string response_body; |
108 bool success = transport->ExecuteSynchronously(&response_body); | 109 bool success = transport->ExecuteSynchronously(&response_body); |
109 if (response_code_ == 200) { | 110 if (response_code_ == 200) { |
110 EXPECT_TRUE(success); | 111 EXPECT_TRUE(success); |
111 std::string expect_response_body = random_string + "\r\n"; | 112 std::string expect_response_body = random_string + "\r\n"; |
112 EXPECT_EQ(expect_response_body, response_body); | 113 EXPECT_EQ(expect_response_body, response_body); |
113 } else { | 114 } else { |
114 EXPECT_FALSE(success); | 115 EXPECT_FALSE(success); |
115 EXPECT_TRUE(response_body.empty()); | 116 EXPECT_TRUE(response_body.empty()); |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 EXPECT_EQ(kTextBody, request.substr(body_start + 2)); | 264 EXPECT_EQ(kTextBody, request.substr(body_start + 2)); |
264 } | 265 } |
265 | 266 |
266 TEST(HTTPTransport, UnchunkedPlainText) { | 267 TEST(HTTPTransport, UnchunkedPlainText) { |
267 scoped_ptr<HTTPBodyStream> body_stream(new StringHTTPBodyStream(kTextBody)); | 268 scoped_ptr<HTTPBodyStream> body_stream(new StringHTTPBodyStream(kTextBody)); |
268 | 269 |
269 HTTPHeaders headers; | 270 HTTPHeaders headers; |
270 headers[kContentType] = kTextPlain; | 271 headers[kContentType] = kTextPlain; |
271 headers[kContentLength] = base::StringPrintf("%" PRIuS, strlen(kTextBody)); | 272 headers[kContentLength] = base::StringPrintf("%" PRIuS, strlen(kTextBody)); |
272 | 273 |
273 HTTPTransportTestFixture test(headers, body_stream.Pass(), 200, | 274 HTTPTransportTestFixture test(headers, crashpad::move(body_stream), 200, |
274 &UnchunkedPlainText); | 275 &UnchunkedPlainText); |
275 test.Run(); | 276 test.Run(); |
276 } | 277 } |
277 | 278 |
278 void RunUpload33k(bool has_content_length) { | 279 void RunUpload33k(bool has_content_length) { |
279 // On OS X, NSMutableURLRequest winds up calling into a CFReadStream’s Read() | 280 // On OS X, NSMutableURLRequest winds up calling into a CFReadStream’s Read() |
280 // callback with a 32kB buffer. Make sure that it’s able to get everything | 281 // callback with a 32kB buffer. Make sure that it’s able to get everything |
281 // when enough is available to fill this buffer, requiring more than one | 282 // when enough is available to fill this buffer, requiring more than one |
282 // Read(). | 283 // Read(). |
283 | 284 |
284 std::string request_string(33 * 1024, 'a'); | 285 std::string request_string(33 * 1024, 'a'); |
285 scoped_ptr<HTTPBodyStream> body_stream( | 286 scoped_ptr<HTTPBodyStream> body_stream( |
286 new StringHTTPBodyStream(request_string)); | 287 new StringHTTPBodyStream(request_string)); |
287 | 288 |
288 HTTPHeaders headers; | 289 HTTPHeaders headers; |
289 headers[kContentType] = "application/octet-stream"; | 290 headers[kContentType] = "application/octet-stream"; |
290 if (has_content_length) { | 291 if (has_content_length) { |
291 headers[kContentLength] = | 292 headers[kContentLength] = |
292 base::StringPrintf("%" PRIuS, request_string.size()); | 293 base::StringPrintf("%" PRIuS, request_string.size()); |
293 } | 294 } |
294 HTTPTransportTestFixture test(headers, body_stream.Pass(), 200, | 295 HTTPTransportTestFixture test(headers, crashpad::move(body_stream), 200, |
295 [](HTTPTransportTestFixture* fixture, const std::string& request) { | 296 [](HTTPTransportTestFixture* fixture, const std::string& request) { |
296 size_t body_start = request.rfind("\r\n"); | 297 size_t body_start = request.rfind("\r\n"); |
297 EXPECT_EQ(33 * 1024u + 2, request.size() - body_start); | 298 EXPECT_EQ(33 * 1024u + 2, request.size() - body_start); |
298 }); | 299 }); |
299 test.Run(); | 300 test.Run(); |
300 } | 301 } |
301 | 302 |
302 TEST(HTTPTransport, Upload33k) { | 303 TEST(HTTPTransport, Upload33k) { |
303 RunUpload33k(true); | 304 RunUpload33k(true); |
304 } | 305 } |
305 | 306 |
306 TEST(HTTPTransport, Upload33k_LengthUnknown) { | 307 TEST(HTTPTransport, Upload33k_LengthUnknown) { |
307 // The same as Upload33k, but without declaring Content-Length ahead of time. | 308 // The same as Upload33k, but without declaring Content-Length ahead of time. |
308 RunUpload33k(false); | 309 RunUpload33k(false); |
309 } | 310 } |
310 | 311 |
311 } // namespace | 312 } // namespace |
312 } // namespace test | 313 } // namespace test |
313 } // namespace crashpad | 314 } // namespace crashpad |
OLD | NEW |