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

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

Issue 2093923004: [Cast Channel] Add real SSL tests to CastSocketTest (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 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 <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 674 matching lines...) Expand 10 before | Expand all | Expand 10 after
685 685
686 NET_TRACE(1, " *** ") << " Completing socket write for: " 686 NET_TRACE(1, " *** ") << " Completing socket write for: "
687 << data.sequence_number; 687 << data.sequence_number;
688 socket()->OnWriteComplete(rv); 688 socket()->OnWriteComplete(rv);
689 NET_TRACE(1, " *** ") << "Done"; 689 NET_TRACE(1, " *** ") << "Done";
690 } 690 }
691 691
692 SequencedSocketData::~SequencedSocketData() { 692 SequencedSocketData::~SequencedSocketData() {
693 } 693 }
694 694
695 FakeDataChannel::FakeDataChannel()
696 : read_buf_len_(0),
697 closed_(false),
698 write_called_after_close_(false),
699 weak_factory_(this) {}
700
701 FakeDataChannel::~FakeDataChannel() = default;
702
703 int FakeDataChannel::Read(IOBuffer* buf,
704 int buf_len,
705 const CompletionCallback& callback) {
706 DCHECK(read_callback_.is_null());
707 DCHECK(!read_buf_.get());
708 if (closed_)
709 return 0;
710 if (data_.empty()) {
711 read_callback_ = callback;
712 read_buf_ = buf;
713 read_buf_len_ = buf_len;
714 return ERR_IO_PENDING;
715 }
716 return PropagateData(buf, buf_len);
717 }
718
719 int FakeDataChannel::Write(IOBuffer* buf,
720 int buf_len,
721 const CompletionCallback& callback) {
722 DCHECK(write_callback_.is_null());
723 if (closed_) {
724 if (write_called_after_close_)
725 return ERR_CONNECTION_RESET;
726 write_called_after_close_ = true;
727 write_callback_ = callback;
728 base::ThreadTaskRunnerHandle::Get()->PostTask(
729 FROM_HERE, base::Bind(&FakeDataChannel::DoWriteCallback,
730 weak_factory_.GetWeakPtr()));
731 return ERR_IO_PENDING;
732 }
733 // This function returns synchronously, so make a copy of the buffer.
734 data_.push(new DrainableIOBuffer(
735 new StringIOBuffer(std::string(buf->data(), buf_len)), buf_len));
736 base::ThreadTaskRunnerHandle::Get()->PostTask(
737 FROM_HERE,
738 base::Bind(&FakeDataChannel::DoReadCallback, weak_factory_.GetWeakPtr()));
739 return buf_len;
740 }
741
742 void FakeDataChannel::Close() {
743 closed_ = true;
744 if (!read_callback_.is_null()) {
745 base::ThreadTaskRunnerHandle::Get()->PostTask(
746 FROM_HERE, base::Bind(&FakeDataChannel::DoReadCallback,
747 weak_factory_.GetWeakPtr()));
748 }
749 }
750
751 void FakeDataChannel::DoReadCallback() {
752 if (read_callback_.is_null())
753 return;
754
755 if (closed_) {
756 base::ResetAndReturn(&read_callback_).Run(ERR_CONNECTION_CLOSED);
757 return;
758 }
759
760 if (data_.empty())
761 return;
762
763 int copied = PropagateData(read_buf_, read_buf_len_);
764 CompletionCallback callback = read_callback_;
765 read_callback_.Reset();
766 read_buf_ = NULL;
767 read_buf_len_ = 0;
768 callback.Run(copied);
769 }
770
771 void FakeDataChannel::DoWriteCallback() {
772 if (write_callback_.is_null())
773 return;
774
775 CompletionCallback callback = write_callback_;
776 write_callback_.Reset();
777 callback.Run(ERR_CONNECTION_RESET);
778 }
779
780 int FakeDataChannel::PropagateData(scoped_refptr<IOBuffer> read_buf,
781 int read_buf_len) {
782 scoped_refptr<DrainableIOBuffer> buf = data_.front();
783 int copied = std::min(buf->BytesRemaining(), read_buf_len);
784 memcpy(read_buf->data(), buf->data(), copied);
785 buf->DidConsume(copied);
786
787 if (!buf->BytesRemaining())
788 data_.pop();
789 return copied;
790 }
791
792 FakeSocket::~FakeSocket() {}
793
794 int FakeSocket::Read(IOBuffer* buf,
795 int buf_len,
796 const CompletionCallback& callback) {
797 // Read random number of bytes.
798 buf_len = rand() % buf_len + 1;
799 return incoming_->Read(buf, buf_len, callback);
800 }
801
802 int FakeSocket::Write(IOBuffer* buf,
803 int buf_len,
804 const CompletionCallback& callback) {
805 // Write random number of bytes.
806 buf_len = rand() % buf_len + 1;
807 return outgoing_->Write(buf, buf_len, callback);
808 }
809
810 int FakeSocket::SetReceiveBufferSize(int32_t size) {
811 return OK;
812 }
813
814 int FakeSocket::SetSendBufferSize(int32_t size) {
815 return OK;
816 }
817
818 int FakeSocket::Connect(const CompletionCallback& callback) {
819 return OK;
820 }
821
822 void FakeSocket::Disconnect() {
823 incoming_->Close();
824 outgoing_->Close();
825 }
826
827 bool FakeSocket::IsConnected() const {
828 return true;
829 }
830
831 bool FakeSocket::IsConnectedAndIdle() const {
832 return true;
833 }
834
835 int FakeSocket::GetPeerAddress(IPEndPoint* address) const {
836 *address = IPEndPoint(IPAddress::IPv4AllZeros(), 0 /*port*/);
837 return OK;
838 }
839
840 int FakeSocket::GetLocalAddress(IPEndPoint* address) const {
841 *address = IPEndPoint(IPAddress::IPv4AllZeros(), 0 /*port*/);
842 return OK;
843 }
844
845 const BoundNetLog& FakeSocket::NetLog() const {
846 return net_log_;
847 }
848
849 void FakeSocket::SetSubresourceSpeculation() {}
850 void FakeSocket::SetOmniboxSpeculation() {}
851
852 bool FakeSocket::WasEverUsed() const {
853 return true;
854 }
855
856 bool FakeSocket::WasNpnNegotiated() const {
857 return false;
858 }
859
860 NextProto FakeSocket::GetNegotiatedProtocol() const {
861 return kProtoUnknown;
862 }
863
864 bool FakeSocket::GetSSLInfo(SSLInfo* ssl_info) {
865 return false;
866 }
867
868 void FakeSocket::GetConnectionAttempts(ConnectionAttempts* out) const {
869 out->clear();
870 }
871
872 void FakeSocket::ClearConnectionAttempts() {}
873
874 void FakeSocket::AddConnectionAttempts(const ConnectionAttempts& attempts) {}
875
876 int64_t FakeSocket::GetTotalReceivedBytes() const {
877 NOTIMPLEMENTED();
878 return 0;
879 }
880
695 MockClientSocketFactory::MockClientSocketFactory() {} 881 MockClientSocketFactory::MockClientSocketFactory() {}
696 882
697 MockClientSocketFactory::~MockClientSocketFactory() {} 883 MockClientSocketFactory::~MockClientSocketFactory() {}
698 884
699 void MockClientSocketFactory::AddSocketDataProvider( 885 void MockClientSocketFactory::AddSocketDataProvider(
700 SocketDataProvider* data) { 886 SocketDataProvider* data) {
701 mock_data_.Add(data); 887 mock_data_.Add(data);
702 } 888 }
703 889
704 void MockClientSocketFactory::AddSSLSocketDataProvider( 890 void MockClientSocketFactory::AddSSLSocketDataProvider(
(...skipping 1030 matching lines...) Expand 10 before | Expand all | Expand 10 after
1735 } 1921 }
1736 1922
1737 int64_t CountWriteBytes(const MockWrite writes[], size_t writes_size) { 1923 int64_t CountWriteBytes(const MockWrite writes[], size_t writes_size) {
1738 int64_t total = 0; 1924 int64_t total = 0;
1739 for (const MockWrite* write = writes; write != writes + writes_size; ++write) 1925 for (const MockWrite* write = writes; write != writes + writes_size; ++write)
1740 total += write->data_len; 1926 total += write->data_len;
1741 return total; 1927 return total;
1742 } 1928 }
1743 1929
1744 } // namespace net 1930 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698