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