OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/http/http_network_transaction.h" | 5 #include "net/http/http_network_transaction.h" |
6 | 6 |
7 #include <math.h> // ceil | 7 #include <math.h> // ceil |
8 #include <stdarg.h> | 8 #include <stdarg.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
| 11 #include <limits> |
11 #include <string> | 12 #include <string> |
12 #include <vector> | 13 #include <vector> |
13 | 14 |
14 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
15 #include "base/compiler_specific.h" | 16 #include "base/compiler_specific.h" |
16 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
17 #include "base/files/file_util.h" | 18 #include "base/files/file_util.h" |
18 #include "base/json/json_writer.h" | 19 #include "base/json/json_writer.h" |
19 #include "base/logging.h" | 20 #include "base/logging.h" |
20 #include "base/memory/scoped_ptr.h" | 21 #include "base/memory/scoped_ptr.h" |
(...skipping 518 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 DCHECK(sizeof_data >= size); | 540 DCHECK(sizeof_data >= size); |
540 str->reserve(sizeof_data); | 541 str->reserve(sizeof_data); |
541 | 542 |
542 for (int i = 0; i < num_rows; ++i) | 543 for (int i = 0; i < num_rows; ++i) |
543 str->append(row, sizeof_row); | 544 str->append(row, sizeof_row); |
544 } | 545 } |
545 | 546 |
546 #if defined(NTLM_PORTABLE) | 547 #if defined(NTLM_PORTABLE) |
547 // Alternative functions that eliminate randomness and dependency on the local | 548 // Alternative functions that eliminate randomness and dependency on the local |
548 // host name so that the generated NTLM messages are reproducible. | 549 // host name so that the generated NTLM messages are reproducible. |
549 void MockGenerateRandom1(uint8* output, size_t n) { | 550 void MockGenerateRandom1(uint8_t* output, size_t n) { |
550 static const uint8 bytes[] = { | 551 static const uint8_t bytes[] = {0x55, 0x29, 0x66, 0x26, |
551 0x55, 0x29, 0x66, 0x26, 0x6b, 0x9c, 0x73, 0x54 | 552 0x6b, 0x9c, 0x73, 0x54}; |
552 }; | |
553 static size_t current_byte = 0; | 553 static size_t current_byte = 0; |
554 for (size_t i = 0; i < n; ++i) { | 554 for (size_t i = 0; i < n; ++i) { |
555 output[i] = bytes[current_byte++]; | 555 output[i] = bytes[current_byte++]; |
556 current_byte %= arraysize(bytes); | 556 current_byte %= arraysize(bytes); |
557 } | 557 } |
558 } | 558 } |
559 | 559 |
560 void MockGenerateRandom2(uint8* output, size_t n) { | 560 void MockGenerateRandom2(uint8_t* output, size_t n) { |
561 static const uint8 bytes[] = { | 561 static const uint8_t bytes[] = {0x96, 0x79, 0x85, 0xe7, 0x49, 0x93, |
562 0x96, 0x79, 0x85, 0xe7, 0x49, 0x93, 0x70, 0xa1, | 562 0x70, 0xa1, 0x4e, 0xe7, 0x87, 0x45, |
563 0x4e, 0xe7, 0x87, 0x45, 0x31, 0x5b, 0xd3, 0x1f | 563 0x31, 0x5b, 0xd3, 0x1f}; |
564 }; | |
565 static size_t current_byte = 0; | 564 static size_t current_byte = 0; |
566 for (size_t i = 0; i < n; ++i) { | 565 for (size_t i = 0; i < n; ++i) { |
567 output[i] = bytes[current_byte++]; | 566 output[i] = bytes[current_byte++]; |
568 current_byte %= arraysize(bytes); | 567 current_byte %= arraysize(bytes); |
569 } | 568 } |
570 } | 569 } |
571 | 570 |
572 std::string MockGetHostName() { | 571 std::string MockGetHostName() { |
573 return "WTC-WIN7"; | 572 return "WTC-WIN7"; |
574 } | 573 } |
(...skipping 794 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1369 MockRead("world"), | 1368 MockRead("world"), |
1370 MockRead(ASYNC, OK), | 1369 MockRead(ASYNC, OK), |
1371 }; | 1370 }; |
1372 StaticSocketDataProvider data2(data2_reads, arraysize(data2_reads), NULL, 0); | 1371 StaticSocketDataProvider data2(data2_reads, arraysize(data2_reads), NULL, 0); |
1373 session_deps_.socket_factory->AddSocketDataProvider(&data2); | 1372 session_deps_.socket_factory->AddSocketDataProvider(&data2); |
1374 | 1373 |
1375 const char* const kExpectedResponseData[] = { | 1374 const char* const kExpectedResponseData[] = { |
1376 "hello", "world" | 1375 "hello", "world" |
1377 }; | 1376 }; |
1378 | 1377 |
1379 uint32 first_socket_log_id = NetLog::Source::kInvalidId; | 1378 uint32_t first_socket_log_id = NetLog::Source::kInvalidId; |
1380 for (int i = 0; i < 2; ++i) { | 1379 for (int i = 0; i < 2; ++i) { |
1381 TestCompletionCallback callback; | 1380 TestCompletionCallback callback; |
1382 | 1381 |
1383 scoped_ptr<HttpTransaction> trans( | 1382 scoped_ptr<HttpTransaction> trans( |
1384 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | 1383 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); |
1385 | 1384 |
1386 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | 1385 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
1387 EXPECT_EQ(ERR_IO_PENDING, rv); | 1386 EXPECT_EQ(ERR_IO_PENDING, rv); |
1388 | 1387 |
1389 rv = callback.WaitForResult(); | 1388 rv = callback.WaitForResult(); |
(...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1868 | 1867 |
1869 MockRead data2_reads[] = { | 1868 MockRead data2_reads[] = { |
1870 MockRead(SYNCHRONOUS, ERR_UNEXPECTED), // Should not be reached. | 1869 MockRead(SYNCHRONOUS, ERR_UNEXPECTED), // Should not be reached. |
1871 }; | 1870 }; |
1872 StaticSocketDataProvider data2(data2_reads, arraysize(data2_reads), NULL, 0); | 1871 StaticSocketDataProvider data2(data2_reads, arraysize(data2_reads), NULL, 0); |
1873 session_deps_.socket_factory->AddSocketDataProvider(&data2); | 1872 session_deps_.socket_factory->AddSocketDataProvider(&data2); |
1874 | 1873 |
1875 const int kNumUnreadBodies = arraysize(data1_reads) - 2; | 1874 const int kNumUnreadBodies = arraysize(data1_reads) - 2; |
1876 std::string response_lines[kNumUnreadBodies]; | 1875 std::string response_lines[kNumUnreadBodies]; |
1877 | 1876 |
1878 uint32 first_socket_log_id = NetLog::Source::kInvalidId; | 1877 uint32_t first_socket_log_id = NetLog::Source::kInvalidId; |
1879 for (size_t i = 0; i < arraysize(data1_reads) - 2; ++i) { | 1878 for (size_t i = 0; i < arraysize(data1_reads) - 2; ++i) { |
1880 TestCompletionCallback callback; | 1879 TestCompletionCallback callback; |
1881 | 1880 |
1882 scoped_ptr<HttpTransaction> trans( | 1881 scoped_ptr<HttpTransaction> trans( |
1883 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | 1882 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); |
1884 | 1883 |
1885 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | 1884 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
1886 EXPECT_EQ(ERR_IO_PENDING, rv); | 1885 EXPECT_EQ(ERR_IO_PENDING, rv); |
1887 | 1886 |
1888 rv = callback.WaitForResult(); | 1887 rv = callback.WaitForResult(); |
(...skipping 7210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
9099 EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); | 9098 EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
9100 | 9099 |
9101 std::string response_data; | 9100 std::string response_data; |
9102 rv = ReadTransaction(trans.get(), &response_data); | 9101 rv = ReadTransaction(trans.get(), &response_data); |
9103 EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, rv); | 9102 EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, rv); |
9104 } | 9103 } |
9105 | 9104 |
9106 TEST_P(HttpNetworkTransactionTest, UploadFileSmallerThanLength) { | 9105 TEST_P(HttpNetworkTransactionTest, UploadFileSmallerThanLength) { |
9107 base::FilePath temp_file_path; | 9106 base::FilePath temp_file_path; |
9108 ASSERT_TRUE(base::CreateTemporaryFile(&temp_file_path)); | 9107 ASSERT_TRUE(base::CreateTemporaryFile(&temp_file_path)); |
9109 const uint64 kFakeSize = 100000; // file is actually blank | 9108 const uint64_t kFakeSize = 100000; // file is actually blank |
9110 UploadFileElementReader::ScopedOverridingContentLengthForTests | 9109 UploadFileElementReader::ScopedOverridingContentLengthForTests |
9111 overriding_content_length(kFakeSize); | 9110 overriding_content_length(kFakeSize); |
9112 | 9111 |
9113 std::vector<scoped_ptr<UploadElementReader>> element_readers; | 9112 std::vector<scoped_ptr<UploadElementReader>> element_readers; |
9114 element_readers.push_back(make_scoped_ptr(new UploadFileElementReader( | 9113 element_readers.push_back(make_scoped_ptr(new UploadFileElementReader( |
9115 base::ThreadTaskRunnerHandle::Get().get(), temp_file_path, 0, kuint64max, | 9114 base::ThreadTaskRunnerHandle::Get().get(), temp_file_path, 0, |
9116 base::Time()))); | 9115 std::numeric_limits<uint64_t>::max(), base::Time()))); |
9117 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); | 9116 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
9118 | 9117 |
9119 HttpRequestInfo request; | 9118 HttpRequestInfo request; |
9120 request.method = "POST"; | 9119 request.method = "POST"; |
9121 request.url = GURL("http://www.example.org/upload"); | 9120 request.url = GURL("http://www.example.org/upload"); |
9122 request.upload_data_stream = &upload_data_stream; | 9121 request.upload_data_stream = &upload_data_stream; |
9123 request.load_flags = 0; | 9122 request.load_flags = 0; |
9124 | 9123 |
9125 scoped_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | 9124 scoped_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
9126 scoped_ptr<HttpTransaction> trans( | 9125 scoped_ptr<HttpTransaction> trans( |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
9158 | 9157 |
9159 TEST_P(HttpNetworkTransactionTest, UploadUnreadableFile) { | 9158 TEST_P(HttpNetworkTransactionTest, UploadUnreadableFile) { |
9160 base::FilePath temp_file; | 9159 base::FilePath temp_file; |
9161 ASSERT_TRUE(base::CreateTemporaryFile(&temp_file)); | 9160 ASSERT_TRUE(base::CreateTemporaryFile(&temp_file)); |
9162 std::string temp_file_content("Unreadable file."); | 9161 std::string temp_file_content("Unreadable file."); |
9163 ASSERT_TRUE(base::WriteFile(temp_file, temp_file_content.c_str(), | 9162 ASSERT_TRUE(base::WriteFile(temp_file, temp_file_content.c_str(), |
9164 temp_file_content.length())); | 9163 temp_file_content.length())); |
9165 ASSERT_TRUE(base::MakeFileUnreadable(temp_file)); | 9164 ASSERT_TRUE(base::MakeFileUnreadable(temp_file)); |
9166 | 9165 |
9167 std::vector<scoped_ptr<UploadElementReader>> element_readers; | 9166 std::vector<scoped_ptr<UploadElementReader>> element_readers; |
9168 element_readers.push_back(make_scoped_ptr( | 9167 element_readers.push_back(make_scoped_ptr(new UploadFileElementReader( |
9169 new UploadFileElementReader(base::ThreadTaskRunnerHandle::Get().get(), | 9168 base::ThreadTaskRunnerHandle::Get().get(), temp_file, 0, |
9170 temp_file, 0, kuint64max, base::Time()))); | 9169 std::numeric_limits<uint64_t>::max(), base::Time()))); |
9171 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); | 9170 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
9172 | 9171 |
9173 HttpRequestInfo request; | 9172 HttpRequestInfo request; |
9174 request.method = "POST"; | 9173 request.method = "POST"; |
9175 request.url = GURL("http://www.example.org/upload"); | 9174 request.url = GURL("http://www.example.org/upload"); |
9176 request.upload_data_stream = &upload_data_stream; | 9175 request.upload_data_stream = &upload_data_stream; |
9177 request.load_flags = 0; | 9176 request.load_flags = 0; |
9178 | 9177 |
9179 // If we try to upload an unreadable file, the transaction should fail. | 9178 // If we try to upload an unreadable file, the transaction should fail. |
9180 scoped_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | 9179 scoped_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
(...skipping 20 matching lines...) Expand all Loading... |
9201 FakeUploadElementReader() {} | 9200 FakeUploadElementReader() {} |
9202 ~FakeUploadElementReader() override {} | 9201 ~FakeUploadElementReader() override {} |
9203 | 9202 |
9204 const CompletionCallback& callback() const { return callback_; } | 9203 const CompletionCallback& callback() const { return callback_; } |
9205 | 9204 |
9206 // UploadElementReader overrides: | 9205 // UploadElementReader overrides: |
9207 int Init(const CompletionCallback& callback) override { | 9206 int Init(const CompletionCallback& callback) override { |
9208 callback_ = callback; | 9207 callback_ = callback; |
9209 return ERR_IO_PENDING; | 9208 return ERR_IO_PENDING; |
9210 } | 9209 } |
9211 uint64 GetContentLength() const override { return 0; } | 9210 uint64_t GetContentLength() const override { return 0; } |
9212 uint64 BytesRemaining() const override { return 0; } | 9211 uint64_t BytesRemaining() const override { return 0; } |
9213 int Read(IOBuffer* buf, | 9212 int Read(IOBuffer* buf, |
9214 int buf_length, | 9213 int buf_length, |
9215 const CompletionCallback& callback) override { | 9214 const CompletionCallback& callback) override { |
9216 return ERR_FAILED; | 9215 return ERR_FAILED; |
9217 } | 9216 } |
9218 | 9217 |
9219 private: | 9218 private: |
9220 CompletionCallback callback_; | 9219 CompletionCallback callback_; |
9221 }; | 9220 }; |
9222 | 9221 |
(...skipping 6271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
15494 | 15493 |
15495 scoped_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | 15494 scoped_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
15496 HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); | 15495 HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
15497 | 15496 |
15498 EXPECT_THAT(trans.server_ssl_config_.alpn_protos, | 15497 EXPECT_THAT(trans.server_ssl_config_.alpn_protos, |
15499 testing::ElementsAre(kProtoHTTP2, kProtoSPDY31, kProtoHTTP11)); | 15498 testing::ElementsAre(kProtoHTTP2, kProtoSPDY31, kProtoHTTP11)); |
15500 EXPECT_TRUE(trans.server_ssl_config_.npn_protos.empty()); | 15499 EXPECT_TRUE(trans.server_ssl_config_.npn_protos.empty()); |
15501 } | 15500 } |
15502 | 15501 |
15503 } // namespace net | 15502 } // namespace net |
OLD | NEW |