| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/socket/socket_test_util.h" | 5 #include "net/socket/socket_test_util.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 read_count_(reads_count), | 150 read_count_(reads_count), |
| 151 writes_(writes), | 151 writes_(writes), |
| 152 write_index_(0), | 152 write_index_(0), |
| 153 write_count_(writes_count) { | 153 write_count_(writes_count) { |
| 154 } | 154 } |
| 155 | 155 |
| 156 StaticSocketDataHelper::~StaticSocketDataHelper() { | 156 StaticSocketDataHelper::~StaticSocketDataHelper() { |
| 157 } | 157 } |
| 158 | 158 |
| 159 const MockRead& StaticSocketDataHelper::PeekRead() const { | 159 const MockRead& StaticSocketDataHelper::PeekRead() const { |
| 160 CHECK(!at_read_eof()); | 160 CHECK(!AllReadDataConsumed()); |
| 161 return reads_[read_index_]; | 161 return reads_[read_index_]; |
| 162 } | 162 } |
| 163 | 163 |
| 164 const MockWrite& StaticSocketDataHelper::PeekWrite() const { | 164 const MockWrite& StaticSocketDataHelper::PeekWrite() const { |
| 165 CHECK(!at_write_eof()); | 165 CHECK(!AllWriteDataConsumed()); |
| 166 return writes_[write_index_]; | 166 return writes_[write_index_]; |
| 167 } | 167 } |
| 168 | 168 |
| 169 const MockRead& StaticSocketDataHelper::AdvanceRead() { | 169 const MockRead& StaticSocketDataHelper::AdvanceRead() { |
| 170 CHECK(!at_read_eof()); | 170 CHECK(!AllReadDataConsumed()); |
| 171 return reads_[read_index_++]; | 171 return reads_[read_index_++]; |
| 172 } | 172 } |
| 173 | 173 |
| 174 const MockWrite& StaticSocketDataHelper::AdvanceWrite() { | 174 const MockWrite& StaticSocketDataHelper::AdvanceWrite() { |
| 175 CHECK(!at_write_eof()); | 175 CHECK(!AllWriteDataConsumed()); |
| 176 return writes_[write_index_++]; | 176 return writes_[write_index_++]; |
| 177 } | 177 } |
| 178 | 178 |
| 179 bool StaticSocketDataHelper::VerifyWriteData(const std::string& data) { | 179 bool StaticSocketDataHelper::VerifyWriteData(const std::string& data) { |
| 180 CHECK(!at_write_eof()); | 180 CHECK(!AllWriteDataConsumed()); |
| 181 // Check that what the actual data matches the expectations. | 181 // Check that what the actual data matches the expectations. |
| 182 const MockWrite& next_write = PeekWrite(); | 182 const MockWrite& next_write = PeekWrite(); |
| 183 if (!next_write.data) | 183 if (!next_write.data) |
| 184 return true; | 184 return true; |
| 185 | 185 |
| 186 // Note: Partial writes are supported here. If the expected data | 186 // Note: Partial writes are supported here. If the expected data |
| 187 // is a match, but shorter than the write actually written, that is legal. | 187 // is a match, but shorter than the write actually written, that is legal. |
| 188 // Example: | 188 // Example: |
| 189 // Application writes "foobarbaz" (9 bytes) | 189 // Application writes "foobarbaz" (9 bytes) |
| 190 // Expected write was "foo" (3 bytes) | 190 // Expected write was "foo" (3 bytes) |
| (...skipping 18 matching lines...) Expand all Loading... |
| 209 size_t reads_count, | 209 size_t reads_count, |
| 210 MockWrite* writes, | 210 MockWrite* writes, |
| 211 size_t writes_count) | 211 size_t writes_count) |
| 212 : helper_(reads, reads_count, writes, writes_count) { | 212 : helper_(reads, reads_count, writes, writes_count) { |
| 213 } | 213 } |
| 214 | 214 |
| 215 StaticSocketDataProvider::~StaticSocketDataProvider() { | 215 StaticSocketDataProvider::~StaticSocketDataProvider() { |
| 216 } | 216 } |
| 217 | 217 |
| 218 MockRead StaticSocketDataProvider::OnRead() { | 218 MockRead StaticSocketDataProvider::OnRead() { |
| 219 CHECK(!helper_.at_read_eof()); | 219 CHECK(!helper_.AllReadDataConsumed()); |
| 220 return helper_.AdvanceRead(); | 220 return helper_.AdvanceRead(); |
| 221 } | 221 } |
| 222 | 222 |
| 223 MockWriteResult StaticSocketDataProvider::OnWrite(const std::string& data) { | 223 MockWriteResult StaticSocketDataProvider::OnWrite(const std::string& data) { |
| 224 if (helper_.write_count() == 0) { | 224 if (helper_.write_count() == 0) { |
| 225 // Not using mock writes; succeed synchronously. | 225 // Not using mock writes; succeed synchronously. |
| 226 return MockWriteResult(SYNCHRONOUS, data.length()); | 226 return MockWriteResult(SYNCHRONOUS, data.length()); |
| 227 } | 227 } |
| 228 EXPECT_FALSE(helper_.at_write_eof()); | 228 EXPECT_FALSE(helper_.AllWriteDataConsumed()); |
| 229 if (helper_.at_write_eof()) { | 229 if (helper_.AllWriteDataConsumed()) { |
| 230 // Show what the extra write actually consists of. | 230 // Show what the extra write actually consists of. |
| 231 EXPECT_EQ("<unexpected write>", data); | 231 EXPECT_EQ("<unexpected write>", data); |
| 232 return MockWriteResult(SYNCHRONOUS, ERR_UNEXPECTED); | 232 return MockWriteResult(SYNCHRONOUS, ERR_UNEXPECTED); |
| 233 } | 233 } |
| 234 | 234 |
| 235 // Check that what we are writing matches the expectation. | 235 // Check that what we are writing matches the expectation. |
| 236 // Then give the mocked return value. | 236 // Then give the mocked return value. |
| 237 if (!helper_.VerifyWriteData(data)) | 237 if (!helper_.VerifyWriteData(data)) |
| 238 return MockWriteResult(SYNCHRONOUS, ERR_UNEXPECTED); | 238 return MockWriteResult(SYNCHRONOUS, ERR_UNEXPECTED); |
| 239 | 239 |
| 240 const MockWrite& next_write = helper_.AdvanceWrite(); | 240 const MockWrite& next_write = helper_.AdvanceWrite(); |
| 241 // In the case that the write was successful, return the number of bytes | 241 // In the case that the write was successful, return the number of bytes |
| 242 // written. Otherwise return the error code. | 242 // written. Otherwise return the error code. |
| 243 int result = | 243 int result = |
| 244 next_write.result == OK ? next_write.data_len : next_write.result; | 244 next_write.result == OK ? next_write.data_len : next_write.result; |
| 245 return MockWriteResult(next_write.mode, result); | 245 return MockWriteResult(next_write.mode, result); |
| 246 } | 246 } |
| 247 | 247 |
| 248 void StaticSocketDataProvider::Reset() { | 248 void StaticSocketDataProvider::Reset() { |
| 249 helper_.Reset(); | 249 helper_.Reset(); |
| 250 } | 250 } |
| 251 | 251 |
| 252 bool StaticSocketDataProvider::AllReadDataConsumed() const { | 252 bool StaticSocketDataProvider::AllReadDataConsumed() const { |
| 253 return helper_.at_read_eof(); | 253 return helper_.AllReadDataConsumed(); |
| 254 } | 254 } |
| 255 | 255 |
| 256 bool StaticSocketDataProvider::AllWriteDataConsumed() const { | 256 bool StaticSocketDataProvider::AllWriteDataConsumed() const { |
| 257 return helper_.at_write_eof(); | 257 return helper_.AllWriteDataConsumed(); |
| 258 } | 258 } |
| 259 | 259 |
| 260 DynamicSocketDataProvider::DynamicSocketDataProvider() | 260 DynamicSocketDataProvider::DynamicSocketDataProvider() |
| 261 : short_read_limit_(0), | 261 : short_read_limit_(0), |
| 262 allow_unconsumed_reads_(false) { | 262 allow_unconsumed_reads_(false) { |
| 263 } | 263 } |
| 264 | 264 |
| 265 DynamicSocketDataProvider::~DynamicSocketDataProvider() {} | 265 DynamicSocketDataProvider::~DynamicSocketDataProvider() {} |
| 266 | 266 |
| 267 MockRead DynamicSocketDataProvider::OnRead() { | 267 MockRead DynamicSocketDataProvider::OnRead() { |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 MockRead* reads, | 351 MockRead* reads, |
| 352 size_t reads_count, | 352 size_t reads_count, |
| 353 MockWrite* writes, | 353 MockWrite* writes, |
| 354 size_t writes_count) | 354 size_t writes_count) |
| 355 : SequencedSocketData(reads, reads_count, writes, writes_count) { | 355 : SequencedSocketData(reads, reads_count, writes, writes_count) { |
| 356 set_connect_data(connect); | 356 set_connect_data(connect); |
| 357 } | 357 } |
| 358 | 358 |
| 359 MockRead SequencedSocketData::OnRead() { | 359 MockRead SequencedSocketData::OnRead() { |
| 360 CHECK_EQ(IDLE, read_state_); | 360 CHECK_EQ(IDLE, read_state_); |
| 361 CHECK(!helper_.at_read_eof()); | 361 CHECK(!helper_.AllReadDataConsumed()); |
| 362 | 362 |
| 363 NET_TRACE(1, " *** ") << "sequence_number: " << sequence_number_; | 363 NET_TRACE(1, " *** ") << "sequence_number: " << sequence_number_; |
| 364 const MockRead& next_read = helper_.PeekRead(); | 364 const MockRead& next_read = helper_.PeekRead(); |
| 365 NET_TRACE(1, " *** ") << "next_read: " << next_read.sequence_number; | 365 NET_TRACE(1, " *** ") << "next_read: " << next_read.sequence_number; |
| 366 CHECK_GE(next_read.sequence_number, sequence_number_); | 366 CHECK_GE(next_read.sequence_number, sequence_number_); |
| 367 | 367 |
| 368 if (next_read.sequence_number <= sequence_number_) { | 368 if (next_read.sequence_number <= sequence_number_) { |
| 369 if (next_read.mode == SYNCHRONOUS) { | 369 if (next_read.mode == SYNCHRONOUS) { |
| 370 NET_TRACE(1, " *** ") << "Returning synchronously"; | 370 NET_TRACE(1, " *** ") << "Returning synchronously"; |
| 371 DumpMockReadWrite(next_read); | 371 DumpMockReadWrite(next_read); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 395 } else { | 395 } else { |
| 396 NET_TRACE(1, " *** ") << "Waiting for write to trigger read"; | 396 NET_TRACE(1, " *** ") << "Waiting for write to trigger read"; |
| 397 read_state_ = PENDING; | 397 read_state_ = PENDING; |
| 398 } | 398 } |
| 399 | 399 |
| 400 return MockRead(SYNCHRONOUS, ERR_IO_PENDING); | 400 return MockRead(SYNCHRONOUS, ERR_IO_PENDING); |
| 401 } | 401 } |
| 402 | 402 |
| 403 MockWriteResult SequencedSocketData::OnWrite(const std::string& data) { | 403 MockWriteResult SequencedSocketData::OnWrite(const std::string& data) { |
| 404 CHECK_EQ(IDLE, write_state_); | 404 CHECK_EQ(IDLE, write_state_); |
| 405 CHECK(!helper_.at_write_eof()); | 405 CHECK(!helper_.AllWriteDataConsumed()); |
| 406 | 406 |
| 407 NET_TRACE(1, " *** ") << "sequence_number: " << sequence_number_; | 407 NET_TRACE(1, " *** ") << "sequence_number: " << sequence_number_; |
| 408 const MockWrite& next_write = helper_.PeekWrite(); | 408 const MockWrite& next_write = helper_.PeekWrite(); |
| 409 NET_TRACE(1, " *** ") << "next_write: " << next_write.sequence_number; | 409 NET_TRACE(1, " *** ") << "next_write: " << next_write.sequence_number; |
| 410 CHECK_GE(next_write.sequence_number, sequence_number_); | 410 CHECK_GE(next_write.sequence_number, sequence_number_); |
| 411 | 411 |
| 412 if (!helper_.VerifyWriteData(data)) | 412 if (!helper_.VerifyWriteData(data)) |
| 413 return MockWriteResult(SYNCHRONOUS, ERR_UNEXPECTED); | 413 return MockWriteResult(SYNCHRONOUS, ERR_UNEXPECTED); |
| 414 | 414 |
| 415 if (next_write.sequence_number <= sequence_number_) { | 415 if (next_write.sequence_number <= sequence_number_) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 444 | 444 |
| 445 void SequencedSocketData::Reset() { | 445 void SequencedSocketData::Reset() { |
| 446 helper_.Reset(); | 446 helper_.Reset(); |
| 447 sequence_number_ = 0; | 447 sequence_number_ = 0; |
| 448 read_state_ = IDLE; | 448 read_state_ = IDLE; |
| 449 write_state_ = IDLE; | 449 write_state_ = IDLE; |
| 450 weak_factory_.InvalidateWeakPtrs(); | 450 weak_factory_.InvalidateWeakPtrs(); |
| 451 } | 451 } |
| 452 | 452 |
| 453 bool SequencedSocketData::AllReadDataConsumed() const { | 453 bool SequencedSocketData::AllReadDataConsumed() const { |
| 454 return helper_.at_read_eof(); | 454 return helper_.AllReadDataConsumed(); |
| 455 } | 455 } |
| 456 | 456 |
| 457 bool SequencedSocketData::AllWriteDataConsumed() const { | 457 bool SequencedSocketData::AllWriteDataConsumed() const { |
| 458 return helper_.at_write_eof(); | 458 return helper_.AllWriteDataConsumed(); |
| 459 } | |
| 460 | |
| 461 bool SequencedSocketData::at_read_eof() const { | |
| 462 return helper_.at_read_eof(); | |
| 463 } | |
| 464 | |
| 465 bool SequencedSocketData::at_write_eof() const { | |
| 466 return helper_.at_read_eof(); | |
| 467 } | 459 } |
| 468 | 460 |
| 469 bool SequencedSocketData::IsReadPaused() { | 461 bool SequencedSocketData::IsReadPaused() { |
| 470 return read_state_ == PAUSED; | 462 return read_state_ == PAUSED; |
| 471 } | 463 } |
| 472 | 464 |
| 473 void SequencedSocketData::CompleteRead() { | 465 void SequencedSocketData::CompleteRead() { |
| 474 if (read_state_ != PAUSED) { | 466 if (read_state_ != PAUSED) { |
| 475 ADD_FAILURE() << "Unable to CompleteRead when not paused."; | 467 ADD_FAILURE() << "Unable to CompleteRead when not paused."; |
| 476 return; | 468 return; |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 603 DCHECK(!is_running_); | 595 DCHECK(!is_running_); |
| 604 is_running_ = true; | 596 is_running_ = true; |
| 605 | 597 |
| 606 SetStopped(false); | 598 SetStopped(false); |
| 607 int counter = 0; | 599 int counter = 0; |
| 608 // Continue to consume data until all data has run out, or the stopped_ flag | 600 // Continue to consume data until all data has run out, or the stopped_ flag |
| 609 // has been set. Consuming data requires two separate operations -- running | 601 // has been set. Consuming data requires two separate operations -- running |
| 610 // the tasks in the message loop, and explicitly invoking the read/write | 602 // the tasks in the message loop, and explicitly invoking the read/write |
| 611 // callbacks (simulating network I/O). We check our conditions between each, | 603 // callbacks (simulating network I/O). We check our conditions between each, |
| 612 // since they can change in either. | 604 // since they can change in either. |
| 613 while ((!at_write_eof() || !at_read_eof()) && !stopped()) { | 605 while ((!AllWriteDataConsumed() || !AllReadDataConsumed()) && !stopped()) { |
| 614 if (counter % 2 == 0) | 606 if (counter % 2 == 0) |
| 615 base::RunLoop().RunUntilIdle(); | 607 base::RunLoop().RunUntilIdle(); |
| 616 if (counter % 2 == 1) { | 608 if (counter % 2 == 1) { |
| 617 InvokeCallbacks(); | 609 InvokeCallbacks(); |
| 618 } | 610 } |
| 619 counter++; | 611 counter++; |
| 620 } | 612 } |
| 621 // We're done consuming new data, but it is possible there are still some | 613 // We're done consuming new data, but it is possible there are still some |
| 622 // pending callbacks which we expect to complete before returning. | 614 // pending callbacks which we expect to complete before returning. |
| 623 while (delegate_.get() && | 615 while (delegate_.get() && |
| (...skipping 1464 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2088 | 2080 |
| 2089 const char kSOCKS5OkRequest[] = | 2081 const char kSOCKS5OkRequest[] = |
| 2090 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; | 2082 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; |
| 2091 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); | 2083 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); |
| 2092 | 2084 |
| 2093 const char kSOCKS5OkResponse[] = | 2085 const char kSOCKS5OkResponse[] = |
| 2094 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; | 2086 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; |
| 2095 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); | 2087 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); |
| 2096 | 2088 |
| 2097 } // namespace net | 2089 } // namespace net |
| OLD | NEW |