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

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

Issue 667923003: Standardize usage of virtual/override/final in net/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 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
« no previous file with comments | « net/socket/ssl_client_socket_pool.h ('k') | net/socket/ssl_server_socket.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/ssl_client_socket.h" 5 #include "net/socket/ssl_client_socket.h"
6 6
7 #include "base/callback_helpers.h" 7 #include "base/callback_helpers.h"
8 #include "base/memory/ref_counted.h" 8 #include "base/memory/ref_counted.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "base/time/time.h" 10 #include "base/time/time.h"
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 // WrappedStreamSocket is a base class that wraps an existing StreamSocket, 50 // WrappedStreamSocket is a base class that wraps an existing StreamSocket,
51 // forwarding the Socket and StreamSocket interfaces to the underlying 51 // forwarding the Socket and StreamSocket interfaces to the underlying
52 // transport. 52 // transport.
53 // This is to provide a common base class for subclasses to override specific 53 // This is to provide a common base class for subclasses to override specific
54 // StreamSocket methods for testing, while still communicating with a 'real' 54 // StreamSocket methods for testing, while still communicating with a 'real'
55 // StreamSocket. 55 // StreamSocket.
56 class WrappedStreamSocket : public StreamSocket { 56 class WrappedStreamSocket : public StreamSocket {
57 public: 57 public:
58 explicit WrappedStreamSocket(scoped_ptr<StreamSocket> transport) 58 explicit WrappedStreamSocket(scoped_ptr<StreamSocket> transport)
59 : transport_(transport.Pass()) {} 59 : transport_(transport.Pass()) {}
60 virtual ~WrappedStreamSocket() {} 60 ~WrappedStreamSocket() override {}
61 61
62 // StreamSocket implementation: 62 // StreamSocket implementation:
63 virtual int Connect(const CompletionCallback& callback) override { 63 int Connect(const CompletionCallback& callback) override {
64 return transport_->Connect(callback); 64 return transport_->Connect(callback);
65 } 65 }
66 virtual void Disconnect() override { transport_->Disconnect(); } 66 void Disconnect() override { transport_->Disconnect(); }
67 virtual bool IsConnected() const override { 67 bool IsConnected() const override { return transport_->IsConnected(); }
68 return transport_->IsConnected(); 68 bool IsConnectedAndIdle() const override {
69 }
70 virtual bool IsConnectedAndIdle() const override {
71 return transport_->IsConnectedAndIdle(); 69 return transport_->IsConnectedAndIdle();
72 } 70 }
73 virtual int GetPeerAddress(IPEndPoint* address) const override { 71 int GetPeerAddress(IPEndPoint* address) const override {
74 return transport_->GetPeerAddress(address); 72 return transport_->GetPeerAddress(address);
75 } 73 }
76 virtual int GetLocalAddress(IPEndPoint* address) const override { 74 int GetLocalAddress(IPEndPoint* address) const override {
77 return transport_->GetLocalAddress(address); 75 return transport_->GetLocalAddress(address);
78 } 76 }
79 virtual const BoundNetLog& NetLog() const override { 77 const BoundNetLog& NetLog() const override { return transport_->NetLog(); }
80 return transport_->NetLog(); 78 void SetSubresourceSpeculation() override {
81 }
82 virtual void SetSubresourceSpeculation() override {
83 transport_->SetSubresourceSpeculation(); 79 transport_->SetSubresourceSpeculation();
84 } 80 }
85 virtual void SetOmniboxSpeculation() override { 81 void SetOmniboxSpeculation() override { transport_->SetOmniboxSpeculation(); }
86 transport_->SetOmniboxSpeculation(); 82 bool WasEverUsed() const override { return transport_->WasEverUsed(); }
87 } 83 bool UsingTCPFastOpen() const override {
88 virtual bool WasEverUsed() const override {
89 return transport_->WasEverUsed();
90 }
91 virtual bool UsingTCPFastOpen() const override {
92 return transport_->UsingTCPFastOpen(); 84 return transport_->UsingTCPFastOpen();
93 } 85 }
94 virtual bool WasNpnNegotiated() const override { 86 bool WasNpnNegotiated() const override {
95 return transport_->WasNpnNegotiated(); 87 return transport_->WasNpnNegotiated();
96 } 88 }
97 virtual NextProto GetNegotiatedProtocol() const override { 89 NextProto GetNegotiatedProtocol() const override {
98 return transport_->GetNegotiatedProtocol(); 90 return transport_->GetNegotiatedProtocol();
99 } 91 }
100 virtual bool GetSSLInfo(SSLInfo* ssl_info) override { 92 bool GetSSLInfo(SSLInfo* ssl_info) override {
101 return transport_->GetSSLInfo(ssl_info); 93 return transport_->GetSSLInfo(ssl_info);
102 } 94 }
103 95
104 // Socket implementation: 96 // Socket implementation:
105 virtual int Read(IOBuffer* buf, 97 int Read(IOBuffer* buf,
106 int buf_len, 98 int buf_len,
107 const CompletionCallback& callback) override { 99 const CompletionCallback& callback) override {
108 return transport_->Read(buf, buf_len, callback); 100 return transport_->Read(buf, buf_len, callback);
109 } 101 }
110 virtual int Write(IOBuffer* buf, 102 int Write(IOBuffer* buf,
111 int buf_len, 103 int buf_len,
112 const CompletionCallback& callback) override { 104 const CompletionCallback& callback) override {
113 return transport_->Write(buf, buf_len, callback); 105 return transport_->Write(buf, buf_len, callback);
114 } 106 }
115 virtual int SetReceiveBufferSize(int32 size) override { 107 int SetReceiveBufferSize(int32 size) override {
116 return transport_->SetReceiveBufferSize(size); 108 return transport_->SetReceiveBufferSize(size);
117 } 109 }
118 virtual int SetSendBufferSize(int32 size) override { 110 int SetSendBufferSize(int32 size) override {
119 return transport_->SetSendBufferSize(size); 111 return transport_->SetSendBufferSize(size);
120 } 112 }
121 113
122 protected: 114 protected:
123 scoped_ptr<StreamSocket> transport_; 115 scoped_ptr<StreamSocket> transport_;
124 }; 116 };
125 117
126 // ReadBufferingStreamSocket is a wrapper for an existing StreamSocket that 118 // ReadBufferingStreamSocket is a wrapper for an existing StreamSocket that
127 // will ensure a certain amount of data is internally buffered before 119 // will ensure a certain amount of data is internally buffered before
128 // satisfying a Read() request. It exists to mimic OS-level internal 120 // satisfying a Read() request. It exists to mimic OS-level internal
129 // buffering, but in a way to guarantee that X number of bytes will be 121 // buffering, but in a way to guarantee that X number of bytes will be
130 // returned to callers of Read(), regardless of how quickly the OS receives 122 // returned to callers of Read(), regardless of how quickly the OS receives
131 // them from the TestServer. 123 // them from the TestServer.
132 class ReadBufferingStreamSocket : public WrappedStreamSocket { 124 class ReadBufferingStreamSocket : public WrappedStreamSocket {
133 public: 125 public:
134 explicit ReadBufferingStreamSocket(scoped_ptr<StreamSocket> transport); 126 explicit ReadBufferingStreamSocket(scoped_ptr<StreamSocket> transport);
135 virtual ~ReadBufferingStreamSocket() {} 127 ~ReadBufferingStreamSocket() override {}
136 128
137 // Socket implementation: 129 // Socket implementation:
138 virtual int Read(IOBuffer* buf, 130 int Read(IOBuffer* buf,
139 int buf_len, 131 int buf_len,
140 const CompletionCallback& callback) override; 132 const CompletionCallback& callback) override;
141 133
142 // Sets the internal buffer to |size|. This must not be greater than 134 // Sets the internal buffer to |size|. This must not be greater than
143 // the largest value supplied to Read() - that is, it does not handle 135 // the largest value supplied to Read() - that is, it does not handle
144 // having "leftovers" at the end of Read(). 136 // having "leftovers" at the end of Read().
145 // Each call to Read() will be prevented from completion until at least 137 // Each call to Read() will be prevented from completion until at least
146 // |size| data has been read. 138 // |size| data has been read.
147 // Set to 0 to turn off buffering, causing Read() to transparently 139 // Set to 0 to turn off buffering, causing Read() to transparently
148 // read via the underlying transport. 140 // read via the underlying transport.
149 void SetBufferSize(int size); 141 void SetBufferSize(int size);
150 142
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 return; 247 return;
256 248
257 user_read_buf_ = NULL; 249 user_read_buf_ = NULL;
258 base::ResetAndReturn(&user_read_callback_).Run(result); 250 base::ResetAndReturn(&user_read_callback_).Run(result);
259 } 251 }
260 252
261 // Simulates synchronously receiving an error during Read() or Write() 253 // Simulates synchronously receiving an error during Read() or Write()
262 class SynchronousErrorStreamSocket : public WrappedStreamSocket { 254 class SynchronousErrorStreamSocket : public WrappedStreamSocket {
263 public: 255 public:
264 explicit SynchronousErrorStreamSocket(scoped_ptr<StreamSocket> transport); 256 explicit SynchronousErrorStreamSocket(scoped_ptr<StreamSocket> transport);
265 virtual ~SynchronousErrorStreamSocket() {} 257 ~SynchronousErrorStreamSocket() override {}
266 258
267 // Socket implementation: 259 // Socket implementation:
268 virtual int Read(IOBuffer* buf, 260 int Read(IOBuffer* buf,
269 int buf_len, 261 int buf_len,
270 const CompletionCallback& callback) override; 262 const CompletionCallback& callback) override;
271 virtual int Write(IOBuffer* buf, 263 int Write(IOBuffer* buf,
272 int buf_len, 264 int buf_len,
273 const CompletionCallback& callback) override; 265 const CompletionCallback& callback) override;
274 266
275 // Sets the next Read() call and all future calls to return |error|. 267 // Sets the next Read() call and all future calls to return |error|.
276 // If there is already a pending asynchronous read, the configured error 268 // If there is already a pending asynchronous read, the configured error
277 // will not be returned until that asynchronous read has completed and Read() 269 // will not be returned until that asynchronous read has completed and Read()
278 // is called again. 270 // is called again.
279 void SetNextReadError(int error) { 271 void SetNextReadError(int error) {
280 DCHECK_GE(0, error); 272 DCHECK_GE(0, error);
281 have_read_error_ = true; 273 have_read_error_ = true;
282 pending_read_error_ = error; 274 pending_read_error_ = error;
283 } 275 }
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 return transport_->Write(buf, buf_len, callback); 318 return transport_->Write(buf, buf_len, callback);
327 } 319 }
328 320
329 // FakeBlockingStreamSocket wraps an existing StreamSocket and simulates the 321 // FakeBlockingStreamSocket wraps an existing StreamSocket and simulates the
330 // underlying transport needing to complete things asynchronously in a 322 // underlying transport needing to complete things asynchronously in a
331 // deterministic manner (e.g.: independent of the TestServer and the OS's 323 // deterministic manner (e.g.: independent of the TestServer and the OS's
332 // semantics). 324 // semantics).
333 class FakeBlockingStreamSocket : public WrappedStreamSocket { 325 class FakeBlockingStreamSocket : public WrappedStreamSocket {
334 public: 326 public:
335 explicit FakeBlockingStreamSocket(scoped_ptr<StreamSocket> transport); 327 explicit FakeBlockingStreamSocket(scoped_ptr<StreamSocket> transport);
336 virtual ~FakeBlockingStreamSocket() {} 328 ~FakeBlockingStreamSocket() override {}
337 329
338 // Socket implementation: 330 // Socket implementation:
339 virtual int Read(IOBuffer* buf, 331 int Read(IOBuffer* buf,
340 int buf_len, 332 int buf_len,
341 const CompletionCallback& callback) override; 333 const CompletionCallback& callback) override;
342 virtual int Write(IOBuffer* buf, 334 int Write(IOBuffer* buf,
343 int buf_len, 335 int buf_len,
344 const CompletionCallback& callback) override; 336 const CompletionCallback& callback) override;
345 337
346 // Blocks read results on the socket. Reads will not complete until 338 // Blocks read results on the socket. Reads will not complete until
347 // UnblockReadResult() has been called and a result is ready from the 339 // UnblockReadResult() has been called and a result is ready from the
348 // underlying transport. Note: if BlockReadResult() is called while there is a 340 // underlying transport. Note: if BlockReadResult() is called while there is a
349 // hanging asynchronous Read(), that Read is blocked. 341 // hanging asynchronous Read(), that Read is blocked.
350 void BlockReadResult(); 342 void BlockReadResult();
351 void UnblockReadResult(); 343 void UnblockReadResult();
352 344
353 // Waits for the blocked Read() call to be complete at the underlying 345 // Waits for the blocked Read() call to be complete at the underlying
354 // transport. 346 // transport.
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
542 } 534 }
543 535
544 // CountingStreamSocket wraps an existing StreamSocket and maintains a count of 536 // CountingStreamSocket wraps an existing StreamSocket and maintains a count of
545 // reads and writes on the socket. 537 // reads and writes on the socket.
546 class CountingStreamSocket : public WrappedStreamSocket { 538 class CountingStreamSocket : public WrappedStreamSocket {
547 public: 539 public:
548 explicit CountingStreamSocket(scoped_ptr<StreamSocket> transport) 540 explicit CountingStreamSocket(scoped_ptr<StreamSocket> transport)
549 : WrappedStreamSocket(transport.Pass()), 541 : WrappedStreamSocket(transport.Pass()),
550 read_count_(0), 542 read_count_(0),
551 write_count_(0) {} 543 write_count_(0) {}
552 virtual ~CountingStreamSocket() {} 544 ~CountingStreamSocket() override {}
553 545
554 // Socket implementation: 546 // Socket implementation:
555 virtual int Read(IOBuffer* buf, 547 int Read(IOBuffer* buf,
556 int buf_len, 548 int buf_len,
557 const CompletionCallback& callback) override { 549 const CompletionCallback& callback) override {
558 read_count_++; 550 read_count_++;
559 return transport_->Read(buf, buf_len, callback); 551 return transport_->Read(buf, buf_len, callback);
560 } 552 }
561 virtual int Write(IOBuffer* buf, 553 int Write(IOBuffer* buf,
562 int buf_len, 554 int buf_len,
563 const CompletionCallback& callback) override { 555 const CompletionCallback& callback) override {
564 write_count_++; 556 write_count_++;
565 return transport_->Write(buf, buf_len, callback); 557 return transport_->Write(buf, buf_len, callback);
566 } 558 }
567 559
568 int read_count() const { return read_count_; } 560 int read_count() const { return read_count_; }
569 int write_count() const { return write_count_; } 561 int write_count() const { return write_count_; }
570 562
571 private: 563 private:
572 int read_count_; 564 int read_count_;
573 int write_count_; 565 int write_count_;
574 }; 566 };
575 567
576 // CompletionCallback that will delete the associated StreamSocket when 568 // CompletionCallback that will delete the associated StreamSocket when
577 // the callback is invoked. 569 // the callback is invoked.
578 class DeleteSocketCallback : public TestCompletionCallbackBase { 570 class DeleteSocketCallback : public TestCompletionCallbackBase {
579 public: 571 public:
580 explicit DeleteSocketCallback(StreamSocket* socket) 572 explicit DeleteSocketCallback(StreamSocket* socket)
581 : socket_(socket), 573 : socket_(socket),
582 callback_(base::Bind(&DeleteSocketCallback::OnComplete, 574 callback_(base::Bind(&DeleteSocketCallback::OnComplete,
583 base::Unretained(this))) {} 575 base::Unretained(this))) {}
584 virtual ~DeleteSocketCallback() {} 576 ~DeleteSocketCallback() override {}
585 577
586 const CompletionCallback& callback() const { return callback_; } 578 const CompletionCallback& callback() const { return callback_; }
587 579
588 private: 580 private:
589 void OnComplete(int result) { 581 void OnComplete(int result) {
590 if (socket_) { 582 if (socket_) {
591 delete socket_; 583 delete socket_;
592 socket_ = NULL; 584 socket_ = NULL;
593 } else { 585 } else {
594 ADD_FAILURE() << "Deleting socket twice"; 586 ADD_FAILURE() << "Deleting socket twice";
595 } 587 }
596 SetResult(result); 588 SetResult(result);
597 } 589 }
598 590
599 StreamSocket* socket_; 591 StreamSocket* socket_;
600 CompletionCallback callback_; 592 CompletionCallback callback_;
601 593
602 DISALLOW_COPY_AND_ASSIGN(DeleteSocketCallback); 594 DISALLOW_COPY_AND_ASSIGN(DeleteSocketCallback);
603 }; 595 };
604 596
605 // A ChannelIDStore that always returns an error when asked for a 597 // A ChannelIDStore that always returns an error when asked for a
606 // channel id. 598 // channel id.
607 class FailingChannelIDStore : public ChannelIDStore { 599 class FailingChannelIDStore : public ChannelIDStore {
608 virtual int GetChannelID(const std::string& server_identifier, 600 int GetChannelID(const std::string& server_identifier,
609 base::Time* expiration_time, 601 base::Time* expiration_time,
610 std::string* private_key_result, 602 std::string* private_key_result,
611 std::string* cert_result, 603 std::string* cert_result,
612 const GetChannelIDCallback& callback) override { 604 const GetChannelIDCallback& callback) override {
613 return ERR_UNEXPECTED; 605 return ERR_UNEXPECTED;
614 } 606 }
615 virtual void SetChannelID(const std::string& server_identifier, 607 void SetChannelID(const std::string& server_identifier,
616 base::Time creation_time, 608 base::Time creation_time,
617 base::Time expiration_time, 609 base::Time expiration_time,
618 const std::string& private_key, 610 const std::string& private_key,
619 const std::string& cert) override {} 611 const std::string& cert) override {}
620 virtual void DeleteChannelID(const std::string& server_identifier, 612 void DeleteChannelID(const std::string& server_identifier,
621 const base::Closure& completion_callback) 613 const base::Closure& completion_callback) override {}
622 override {} 614 void DeleteAllCreatedBetween(
623 virtual void DeleteAllCreatedBetween(base::Time delete_begin, 615 base::Time delete_begin,
624 base::Time delete_end, 616 base::Time delete_end,
625 const base::Closure& completion_callback) 617 const base::Closure& completion_callback) override {}
626 override {} 618 void DeleteAll(const base::Closure& completion_callback) override {}
627 virtual void DeleteAll(const base::Closure& completion_callback) override {} 619 void GetAllChannelIDs(const GetChannelIDListCallback& callback) override {}
628 virtual void GetAllChannelIDs(const GetChannelIDListCallback& callback) 620 int GetChannelIDCount() override { return 0; }
629 override {} 621 void SetForceKeepSessionState() override {}
630 virtual int GetChannelIDCount() override { return 0; }
631 virtual void SetForceKeepSessionState() override {}
632 }; 622 };
633 623
634 // A ChannelIDStore that asynchronously returns an error when asked for a 624 // A ChannelIDStore that asynchronously returns an error when asked for a
635 // channel id. 625 // channel id.
636 class AsyncFailingChannelIDStore : public ChannelIDStore { 626 class AsyncFailingChannelIDStore : public ChannelIDStore {
637 virtual int GetChannelID(const std::string& server_identifier, 627 int GetChannelID(const std::string& server_identifier,
638 base::Time* expiration_time, 628 base::Time* expiration_time,
639 std::string* private_key_result, 629 std::string* private_key_result,
640 std::string* cert_result, 630 std::string* cert_result,
641 const GetChannelIDCallback& callback) override { 631 const GetChannelIDCallback& callback) override {
642 base::MessageLoop::current()->PostTask( 632 base::MessageLoop::current()->PostTask(
643 FROM_HERE, base::Bind(callback, ERR_UNEXPECTED, 633 FROM_HERE, base::Bind(callback, ERR_UNEXPECTED,
644 server_identifier, base::Time(), "", "")); 634 server_identifier, base::Time(), "", ""));
645 return ERR_IO_PENDING; 635 return ERR_IO_PENDING;
646 } 636 }
647 virtual void SetChannelID(const std::string& server_identifier, 637 void SetChannelID(const std::string& server_identifier,
648 base::Time creation_time, 638 base::Time creation_time,
649 base::Time expiration_time, 639 base::Time expiration_time,
650 const std::string& private_key, 640 const std::string& private_key,
651 const std::string& cert) override {} 641 const std::string& cert) override {}
652 virtual void DeleteChannelID(const std::string& server_identifier, 642 void DeleteChannelID(const std::string& server_identifier,
653 const base::Closure& completion_callback) 643 const base::Closure& completion_callback) override {}
654 override {} 644 void DeleteAllCreatedBetween(
655 virtual void DeleteAllCreatedBetween(base::Time delete_begin, 645 base::Time delete_begin,
656 base::Time delete_end, 646 base::Time delete_end,
657 const base::Closure& completion_callback) 647 const base::Closure& completion_callback) override {}
658 override {} 648 void DeleteAll(const base::Closure& completion_callback) override {}
659 virtual void DeleteAll(const base::Closure& completion_callback) override {} 649 void GetAllChannelIDs(const GetChannelIDListCallback& callback) override {}
660 virtual void GetAllChannelIDs(const GetChannelIDListCallback& callback) 650 int GetChannelIDCount() override { return 0; }
661 override {} 651 void SetForceKeepSessionState() override {}
662 virtual int GetChannelIDCount() override { return 0; }
663 virtual void SetForceKeepSessionState() override {}
664 }; 652 };
665 653
666 // A mock CTVerifier that records every call to Verify but doesn't verify 654 // A mock CTVerifier that records every call to Verify but doesn't verify
667 // anything. 655 // anything.
668 class MockCTVerifier : public CTVerifier { 656 class MockCTVerifier : public CTVerifier {
669 public: 657 public:
670 MOCK_METHOD5(Verify, int(X509Certificate*, 658 MOCK_METHOD5(Verify, int(X509Certificate*,
671 const std::string&, 659 const std::string&,
672 const std::string&, 660 const std::string&,
673 ct::CTVerifyResult*, 661 ct::CTVerifyResult*,
(...skipping 2363 matching lines...) Expand 10 before | Expand all | Expand 10 after
3037 ssl_config.channel_id_enabled = true; 3025 ssl_config.channel_id_enabled = true;
3038 3026
3039 int rv; 3027 int rv;
3040 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); 3028 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv));
3041 3029
3042 EXPECT_EQ(ERR_UNEXPECTED, rv); 3030 EXPECT_EQ(ERR_UNEXPECTED, rv);
3043 EXPECT_FALSE(sock_->IsConnected()); 3031 EXPECT_FALSE(sock_->IsConnected());
3044 } 3032 }
3045 3033
3046 } // namespace net 3034 } // namespace net
OLDNEW
« no previous file with comments | « net/socket/ssl_client_socket_pool.h ('k') | net/socket/ssl_server_socket.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698