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

Side by Side Diff: net/socket/socket_test_util.cc

Issue 1114383003: Add AllReadDataConsumed and AllWriteDataConsumed methods to SocketDataProvider (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 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
OLDNEW
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 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 read_count_(reads_count), 149 read_count_(reads_count),
150 writes_(writes), 150 writes_(writes),
151 write_index_(0), 151 write_index_(0),
152 write_count_(writes_count) { 152 write_count_(writes_count) {
153 } 153 }
154 154
155 StaticSocketDataHelper::~StaticSocketDataHelper() { 155 StaticSocketDataHelper::~StaticSocketDataHelper() {
156 } 156 }
157 157
158 const MockRead& StaticSocketDataHelper::PeekRead() const { 158 const MockRead& StaticSocketDataHelper::PeekRead() const {
159 CHECK(!at_read_eof()); 159 CHECK(!AllReadDataConsumed());
160 return reads_[read_index_]; 160 return reads_[read_index_];
161 } 161 }
162 162
163 const MockWrite& StaticSocketDataHelper::PeekWrite() const { 163 const MockWrite& StaticSocketDataHelper::PeekWrite() const {
164 CHECK(!at_write_eof()); 164 CHECK(!AllWriteDataConsumed());
165 return writes_[write_index_]; 165 return writes_[write_index_];
166 } 166 }
167 167
168 const MockRead& StaticSocketDataHelper::AdvanceRead() { 168 const MockRead& StaticSocketDataHelper::AdvanceRead() {
169 CHECK(!at_read_eof()); 169 CHECK(!AllReadDataConsumed());
170 return reads_[read_index_++]; 170 return reads_[read_index_++];
171 } 171 }
172 172
173 const MockWrite& StaticSocketDataHelper::AdvanceWrite() { 173 const MockWrite& StaticSocketDataHelper::AdvanceWrite() {
174 CHECK(!at_write_eof()); 174 CHECK(!AllWriteDataConsumed());
175 return writes_[write_index_++]; 175 return writes_[write_index_++];
176 } 176 }
177 177
178 bool StaticSocketDataHelper::VerifyWriteData(const std::string& data) { 178 bool StaticSocketDataHelper::VerifyWriteData(const std::string& data) {
179 CHECK(!at_write_eof()); 179 CHECK(!AllWriteDataConsumed());
180 // Check that what the actual data matches the expectations. 180 // Check that what the actual data matches the expectations.
181 const MockWrite& next_write = PeekWrite(); 181 const MockWrite& next_write = PeekWrite();
182 if (!next_write.data) 182 if (!next_write.data)
183 return true; 183 return true;
184 184
185 // Note: Partial writes are supported here. If the expected data 185 // Note: Partial writes are supported here. If the expected data
186 // is a match, but shorter than the write actually written, that is legal. 186 // is a match, but shorter than the write actually written, that is legal.
187 // Example: 187 // Example:
188 // Application writes "foobarbaz" (9 bytes) 188 // Application writes "foobarbaz" (9 bytes)
189 // Expected write was "foo" (3 bytes) 189 // Expected write was "foo" (3 bytes)
190 // This is a success, and the function returns true. 190 // This is a success, and the function returns true.
191 std::string expected_data(next_write.data, next_write.data_len); 191 std::string expected_data(next_write.data, next_write.data_len);
192 std::string actual_data(data.substr(0, next_write.data_len)); 192 std::string actual_data(data.substr(0, next_write.data_len));
193 EXPECT_GE(data.length(), expected_data.length()); 193 EXPECT_GE(data.length(), expected_data.length());
194 EXPECT_EQ(expected_data, actual_data); 194 EXPECT_EQ(expected_data, actual_data);
195 return expected_data == actual_data; 195 return expected_data == actual_data;
196 } 196 }
197 197
198 bool StaticSocketDataHelper::AllReadDataConsumed() const {
199 return read_index_ >= read_count_;
200 }
201
202 bool StaticSocketDataHelper::AllWriteDataConsumed() const {
203 return write_index_ >= write_count_;
204 }
205
198 void StaticSocketDataHelper::Reset() { 206 void StaticSocketDataHelper::Reset() {
199 read_index_ = 0; 207 read_index_ = 0;
200 write_index_ = 0; 208 write_index_ = 0;
201 } 209 }
202 210
203 StaticSocketDataProvider::StaticSocketDataProvider() 211 StaticSocketDataProvider::StaticSocketDataProvider()
204 : StaticSocketDataProvider(nullptr, 0, nullptr, 0) { 212 : StaticSocketDataProvider(nullptr, 0, nullptr, 0) {
205 } 213 }
206 214
207 StaticSocketDataProvider::StaticSocketDataProvider(MockRead* reads, 215 StaticSocketDataProvider::StaticSocketDataProvider(MockRead* reads,
208 size_t reads_count, 216 size_t reads_count,
209 MockWrite* writes, 217 MockWrite* writes,
210 size_t writes_count) 218 size_t writes_count)
211 : helper_(reads, reads_count, writes, writes_count) { 219 : helper_(reads, reads_count, writes, writes_count) {
212 } 220 }
213 221
214 StaticSocketDataProvider::~StaticSocketDataProvider() { 222 StaticSocketDataProvider::~StaticSocketDataProvider() {
215 } 223 }
216 224
217 MockRead StaticSocketDataProvider::OnRead() { 225 MockRead StaticSocketDataProvider::OnRead() {
218 CHECK(!helper_.at_read_eof()); 226 CHECK(!helper_.AllReadDataConsumed());
219 return helper_.AdvanceRead(); 227 return helper_.AdvanceRead();
220 } 228 }
221 229
222 MockWriteResult StaticSocketDataProvider::OnWrite(const std::string& data) { 230 MockWriteResult StaticSocketDataProvider::OnWrite(const std::string& data) {
223 if (helper_.write_count() == 0) { 231 if (helper_.write_count() == 0) {
224 // Not using mock writes; succeed synchronously. 232 // Not using mock writes; succeed synchronously.
225 return MockWriteResult(SYNCHRONOUS, data.length()); 233 return MockWriteResult(SYNCHRONOUS, data.length());
226 } 234 }
227 EXPECT_FALSE(helper_.at_write_eof()); 235 EXPECT_FALSE(helper_.AllWriteDataConsumed());
228 if (helper_.at_write_eof()) { 236 if (helper_.AllWriteDataConsumed()) {
229 // Show what the extra write actually consists of. 237 // Show what the extra write actually consists of.
230 EXPECT_EQ("<unexpected write>", data); 238 EXPECT_EQ("<unexpected write>", data);
231 return MockWriteResult(SYNCHRONOUS, ERR_UNEXPECTED); 239 return MockWriteResult(SYNCHRONOUS, ERR_UNEXPECTED);
232 } 240 }
233 241
234 // Check that what we are writing matches the expectation. 242 // Check that what we are writing matches the expectation.
235 // Then give the mocked return value. 243 // Then give the mocked return value.
236 if (!helper_.VerifyWriteData(data)) 244 if (!helper_.VerifyWriteData(data))
237 return MockWriteResult(SYNCHRONOUS, ERR_UNEXPECTED); 245 return MockWriteResult(SYNCHRONOUS, ERR_UNEXPECTED);
238 246
239 const MockWrite& next_write = helper_.AdvanceWrite(); 247 const MockWrite& next_write = helper_.AdvanceWrite();
240 // In the case that the write was successful, return the number of bytes 248 // In the case that the write was successful, return the number of bytes
241 // written. Otherwise return the error code. 249 // written. Otherwise return the error code.
242 int result = 250 int result =
243 next_write.result == OK ? next_write.data_len : next_write.result; 251 next_write.result == OK ? next_write.data_len : next_write.result;
244 return MockWriteResult(next_write.mode, result); 252 return MockWriteResult(next_write.mode, result);
245 } 253 }
246 254
247 void StaticSocketDataProvider::Reset() { 255 void StaticSocketDataProvider::Reset() {
248 helper_.Reset(); 256 helper_.Reset();
249 } 257 }
250 258
259 bool StaticSocketDataProvider::AllReadDataConsumed() const {
260 return helper_.AllReadDataConsumed();
261 }
262
263 bool StaticSocketDataProvider::AllWriteDataConsumed() const {
264 return helper_.AllWriteDataConsumed();
265 }
266
251 DynamicSocketDataProvider::DynamicSocketDataProvider() 267 DynamicSocketDataProvider::DynamicSocketDataProvider()
252 : short_read_limit_(0), 268 : short_read_limit_(0),
253 allow_unconsumed_reads_(false) { 269 allow_unconsumed_reads_(false) {
254 } 270 }
255 271
256 DynamicSocketDataProvider::~DynamicSocketDataProvider() {} 272 DynamicSocketDataProvider::~DynamicSocketDataProvider() {}
257 273
258 MockRead DynamicSocketDataProvider::OnRead() { 274 MockRead DynamicSocketDataProvider::OnRead() {
259 if (reads_.empty()) 275 if (reads_.empty())
260 return MockRead(SYNCHRONOUS, ERR_UNEXPECTED); 276 return MockRead(SYNCHRONOUS, ERR_UNEXPECTED);
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
501 MockRead* reads, 517 MockRead* reads,
502 size_t reads_count, 518 size_t reads_count,
503 MockWrite* writes, 519 MockWrite* writes,
504 size_t writes_count) 520 size_t writes_count)
505 : SequencedSocketData(reads, reads_count, writes, writes_count) { 521 : SequencedSocketData(reads, reads_count, writes, writes_count) {
506 set_connect_data(connect); 522 set_connect_data(connect);
507 } 523 }
508 524
509 MockRead SequencedSocketData::OnRead() { 525 MockRead SequencedSocketData::OnRead() {
510 CHECK_EQ(IDLE, read_state_); 526 CHECK_EQ(IDLE, read_state_);
511 CHECK(!helper_.at_read_eof()); 527 CHECK(!helper_.AllReadDataConsumed());
512 528
513 NET_TRACE(1, " *** ") << "sequence_number: " << sequence_number_; 529 NET_TRACE(1, " *** ") << "sequence_number: " << sequence_number_;
514 const MockRead& next_read = helper_.PeekRead(); 530 const MockRead& next_read = helper_.PeekRead();
515 NET_TRACE(1, " *** ") << "next_read: " << next_read.sequence_number; 531 NET_TRACE(1, " *** ") << "next_read: " << next_read.sequence_number;
516 CHECK_GE(next_read.sequence_number, sequence_number_); 532 CHECK_GE(next_read.sequence_number, sequence_number_);
517 533
518 // Special case handling for hanging reads. 534 // Special case handling for hanging reads.
519 if (next_read.mode == ASYNC && next_read.result == ERR_IO_PENDING) { 535 if (next_read.mode == ASYNC && next_read.result == ERR_IO_PENDING) {
520 NET_TRACE(1, " *** ") << "Hanging read"; 536 NET_TRACE(1, " *** ") << "Hanging read";
521 helper_.AdvanceRead(); 537 helper_.AdvanceRead();
522 ++sequence_number_; 538 ++sequence_number_;
523 CHECK(helper_.at_read_eof()); 539 CHECK(helper_.AllReadDataConsumed());
524 return MockRead(SYNCHRONOUS, ERR_IO_PENDING); 540 return MockRead(SYNCHRONOUS, ERR_IO_PENDING);
525 } 541 }
526 542
527 if (next_read.sequence_number <= sequence_number_) { 543 if (next_read.sequence_number <= sequence_number_) {
528 if (next_read.mode == SYNCHRONOUS) { 544 if (next_read.mode == SYNCHRONOUS) {
529 NET_TRACE(1, " *** ") << "Returning synchronously"; 545 NET_TRACE(1, " *** ") << "Returning synchronously";
530 DumpMockReadWrite(next_read); 546 DumpMockReadWrite(next_read);
531 helper_.AdvanceRead(); 547 helper_.AdvanceRead();
532 ++sequence_number_; 548 ++sequence_number_;
533 MaybePostWriteCompleteTask(); 549 MaybePostWriteCompleteTask();
(...skipping 11 matching lines...) Expand all
545 } else { 561 } else {
546 NET_TRACE(1, " *** ") << "Waiting for write to trigger read"; 562 NET_TRACE(1, " *** ") << "Waiting for write to trigger read";
547 read_state_ = PENDING; 563 read_state_ = PENDING;
548 } 564 }
549 565
550 return MockRead(SYNCHRONOUS, ERR_IO_PENDING); 566 return MockRead(SYNCHRONOUS, ERR_IO_PENDING);
551 } 567 }
552 568
553 MockWriteResult SequencedSocketData::OnWrite(const std::string& data) { 569 MockWriteResult SequencedSocketData::OnWrite(const std::string& data) {
554 CHECK_EQ(IDLE, write_state_); 570 CHECK_EQ(IDLE, write_state_);
555 CHECK(!helper_.at_write_eof()); 571 CHECK(!helper_.AllWriteDataConsumed());
556 572
557 NET_TRACE(1, " *** ") << "sequence_number: " << sequence_number_; 573 NET_TRACE(1, " *** ") << "sequence_number: " << sequence_number_;
558 const MockWrite& next_write = helper_.PeekWrite(); 574 const MockWrite& next_write = helper_.PeekWrite();
559 NET_TRACE(1, " *** ") << "next_write: " << next_write.sequence_number; 575 NET_TRACE(1, " *** ") << "next_write: " << next_write.sequence_number;
560 CHECK_GE(next_write.sequence_number, sequence_number_); 576 CHECK_GE(next_write.sequence_number, sequence_number_);
561 577
562 if (!helper_.VerifyWriteData(data)) 578 if (!helper_.VerifyWriteData(data))
563 return MockWriteResult(SYNCHRONOUS, ERR_UNEXPECTED); 579 return MockWriteResult(SYNCHRONOUS, ERR_UNEXPECTED);
564 580
565 if (next_write.sequence_number <= sequence_number_) { 581 if (next_write.sequence_number <= sequence_number_) {
(...skipping 27 matching lines...) Expand all
593 } 609 }
594 610
595 void SequencedSocketData::Reset() { 611 void SequencedSocketData::Reset() {
596 helper_.Reset(); 612 helper_.Reset();
597 sequence_number_ = 0; 613 sequence_number_ = 0;
598 read_state_ = IDLE; 614 read_state_ = IDLE;
599 write_state_ = IDLE; 615 write_state_ = IDLE;
600 weak_factory_.InvalidateWeakPtrs(); 616 weak_factory_.InvalidateWeakPtrs();
601 } 617 }
602 618
603 bool SequencedSocketData::at_read_eof() const { 619 bool SequencedSocketData::AllReadDataConsumed() const {
604 return helper_.at_read_eof(); 620 return helper_.AllReadDataConsumed();
605 } 621 }
606 622
607 bool SequencedSocketData::at_write_eof() const { 623 bool SequencedSocketData::AllWriteDataConsumed() const {
608 return helper_.at_read_eof(); 624 return helper_.AllWriteDataConsumed();
609 } 625 }
610 626
611 void SequencedSocketData::MaybePostReadCompleteTask() { 627 void SequencedSocketData::MaybePostReadCompleteTask() {
612 NET_TRACE(1, " ****** ") << " current: " << sequence_number_; 628 NET_TRACE(1, " ****** ") << " current: " << sequence_number_;
613 // Only trigger the next read to complete if there is already a read pending 629 // Only trigger the next read to complete if there is already a read pending
614 // which should complete at the current sequence number. 630 // which should complete at the current sequence number.
615 if (read_state_ != PENDING || 631 if (read_state_ != PENDING ||
616 helper_.PeekRead().sequence_number != sequence_number_) { 632 helper_.PeekRead().sequence_number != sequence_number_) {
617 return; 633 return;
618 } 634 }
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
718 DCHECK(!is_running_); 734 DCHECK(!is_running_);
719 is_running_ = true; 735 is_running_ = true;
720 736
721 SetStopped(false); 737 SetStopped(false);
722 int counter = 0; 738 int counter = 0;
723 // Continue to consume data until all data has run out, or the stopped_ flag 739 // Continue to consume data until all data has run out, or the stopped_ flag
724 // has been set. Consuming data requires two separate operations -- running 740 // has been set. Consuming data requires two separate operations -- running
725 // the tasks in the message loop, and explicitly invoking the read/write 741 // the tasks in the message loop, and explicitly invoking the read/write
726 // callbacks (simulating network I/O). We check our conditions between each, 742 // callbacks (simulating network I/O). We check our conditions between each,
727 // since they can change in either. 743 // since they can change in either.
728 while ((!at_write_eof() || !at_read_eof()) && !stopped()) { 744 while ((!AllWriteDataConsumed() || !AllReadDataConsumed()) && !stopped()) {
729 if (counter % 2 == 0) 745 if (counter % 2 == 0)
730 base::RunLoop().RunUntilIdle(); 746 base::RunLoop().RunUntilIdle();
731 if (counter % 2 == 1) { 747 if (counter % 2 == 1) {
732 InvokeCallbacks(); 748 InvokeCallbacks();
733 } 749 }
734 counter++; 750 counter++;
735 } 751 }
736 // We're done consuming new data, but it is possible there are still some 752 // We're done consuming new data, but it is possible there are still some
737 // pending callbacks which we expect to complete before returning. 753 // pending callbacks which we expect to complete before returning.
738 while (delegate_.get() && 754 while (delegate_.get() &&
(...skipping 1440 matching lines...) Expand 10 before | Expand all | Expand 10 after
2179 2195
2180 const char kSOCKS5OkRequest[] = 2196 const char kSOCKS5OkRequest[] =
2181 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; 2197 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 };
2182 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); 2198 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest);
2183 2199
2184 const char kSOCKS5OkResponse[] = 2200 const char kSOCKS5OkResponse[] =
2185 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; 2201 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 };
2186 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); 2202 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse);
2187 2203
2188 } // namespace net 2204 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698