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

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

Issue 8801005: base::Bind: Convert Socket::Read. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 // This test suite uses SSLClientSocket to test the implementation of 5 // This test suite uses SSLClientSocket to test the implementation of
6 // SSLServerSocket. In order to establish connections between the sockets 6 // SSLServerSocket. In order to establish connections between the sockets
7 // we need two additional classes: 7 // we need two additional classes:
8 // 1. FakeSocket 8 // 1. FakeSocket
9 // Connects SSL socket to FakeDataChannel. This class is just a stub. 9 // Connects SSL socket to FakeDataChannel. This class is just a stub.
10 // 10 //
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 #include "testing/gtest/include/gtest/gtest.h" 44 #include "testing/gtest/include/gtest/gtest.h"
45 #include "testing/platform_test.h" 45 #include "testing/platform_test.h"
46 46
47 namespace net { 47 namespace net {
48 48
49 namespace { 49 namespace {
50 50
51 class FakeDataChannel { 51 class FakeDataChannel {
52 public: 52 public:
53 FakeDataChannel() 53 FakeDataChannel()
54 : read_callback_(NULL), 54 : old_read_callback_(NULL),
55 read_buf_len_(0), 55 read_buf_len_(0),
56 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)) { 56 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)) {
57 } 57 }
58 58
59 virtual int Read(IOBuffer* buf, int buf_len, 59 virtual int Read(IOBuffer* buf, int buf_len,
60 OldCompletionCallback* callback) { 60 OldCompletionCallback* callback) {
61 if (data_.empty()) { 61 if (data_.empty()) {
62 old_read_callback_ = callback;
63 read_buf_ = buf;
64 read_buf_len_ = buf_len;
65 return net::ERR_IO_PENDING;
66 }
67 return PropogateData(buf, buf_len);
68 }
69 virtual int Read(IOBuffer* buf, int buf_len,
70 const CompletionCallback& callback) {
71 if (data_.empty()) {
62 read_callback_ = callback; 72 read_callback_ = callback;
63 read_buf_ = buf; 73 read_buf_ = buf;
64 read_buf_len_ = buf_len; 74 read_buf_len_ = buf_len;
65 return net::ERR_IO_PENDING; 75 return net::ERR_IO_PENDING;
66 } 76 }
67 return PropogateData(buf, buf_len); 77 return PropogateData(buf, buf_len);
68 } 78 }
69 79
70 virtual int Write(IOBuffer* buf, int buf_len, 80 virtual int Write(IOBuffer* buf, int buf_len,
71 OldCompletionCallback* callback) { 81 OldCompletionCallback* callback) {
72 data_.push(new net::DrainableIOBuffer(buf, buf_len)); 82 data_.push(new net::DrainableIOBuffer(buf, buf_len));
73 MessageLoop::current()->PostTask( 83 MessageLoop::current()->PostTask(
74 FROM_HERE, task_factory_.NewRunnableMethod( 84 FROM_HERE, task_factory_.NewRunnableMethod(
75 &FakeDataChannel::DoReadCallback)); 85 &FakeDataChannel::DoReadCallback));
76 return buf_len; 86 return buf_len;
77 } 87 }
78 88
79 private: 89 private:
80 void DoReadCallback() { 90 void DoReadCallback() {
81 if (!read_callback_ || data_.empty()) 91 if ((!old_read_callback_ && read_callback_.is_null()) || data_.empty())
82 return; 92 return;
83 93
84 int copied = PropogateData(read_buf_, read_buf_len_); 94 int copied = PropogateData(read_buf_, read_buf_len_);
85 net::OldCompletionCallback* callback = read_callback_; 95 if (old_read_callback_) {
86 read_callback_ = NULL; 96 net::OldCompletionCallback* callback = old_read_callback_;
87 read_buf_ = NULL; 97 old_read_callback_ = NULL;
88 read_buf_len_ = 0; 98 read_buf_ = NULL;
89 callback->Run(copied); 99 read_buf_len_ = 0;
100 callback->Run(copied);
101 } else {
102 net::CompletionCallback callback = read_callback_;
103 read_callback_.Reset();
104 read_buf_ = NULL;
105 read_buf_len_ = 0;
106 callback.Run(copied);
107 }
90 } 108 }
91 109
92 int PropogateData(scoped_refptr<net::IOBuffer> read_buf, int read_buf_len) { 110 int PropogateData(scoped_refptr<net::IOBuffer> read_buf, int read_buf_len) {
93 scoped_refptr<net::DrainableIOBuffer> buf = data_.front(); 111 scoped_refptr<net::DrainableIOBuffer> buf = data_.front();
94 int copied = std::min(buf->BytesRemaining(), read_buf_len); 112 int copied = std::min(buf->BytesRemaining(), read_buf_len);
95 memcpy(read_buf->data(), buf->data(), copied); 113 memcpy(read_buf->data(), buf->data(), copied);
96 buf->DidConsume(copied); 114 buf->DidConsume(copied);
97 115
98 if (!buf->BytesRemaining()) 116 if (!buf->BytesRemaining())
99 data_.pop(); 117 data_.pop();
100 return copied; 118 return copied;
101 } 119 }
102 120
103 net::OldCompletionCallback* read_callback_; 121 net::OldCompletionCallback* old_read_callback_;
122 net::CompletionCallback read_callback_;
104 scoped_refptr<net::IOBuffer> read_buf_; 123 scoped_refptr<net::IOBuffer> read_buf_;
105 int read_buf_len_; 124 int read_buf_len_;
106 125
107 std::queue<scoped_refptr<net::DrainableIOBuffer> > data_; 126 std::queue<scoped_refptr<net::DrainableIOBuffer> > data_;
108 127
109 ScopedRunnableMethodFactory<FakeDataChannel> task_factory_; 128 ScopedRunnableMethodFactory<FakeDataChannel> task_factory_;
110 129
111 DISALLOW_COPY_AND_ASSIGN(FakeDataChannel); 130 DISALLOW_COPY_AND_ASSIGN(FakeDataChannel);
112 }; 131 };
113 132
114 class FakeSocket : public StreamSocket { 133 class FakeSocket : public StreamSocket {
115 public: 134 public:
116 FakeSocket(FakeDataChannel* incoming_channel, 135 FakeSocket(FakeDataChannel* incoming_channel,
117 FakeDataChannel* outgoing_channel) 136 FakeDataChannel* outgoing_channel)
118 : incoming_(incoming_channel), 137 : incoming_(incoming_channel),
119 outgoing_(outgoing_channel) { 138 outgoing_(outgoing_channel) {
120 } 139 }
121 140
122 virtual ~FakeSocket() { 141 virtual ~FakeSocket() {
123 } 142 }
124 143
125 virtual int Read(IOBuffer* buf, int buf_len, 144 virtual int Read(IOBuffer* buf, int buf_len,
126 OldCompletionCallback* callback) { 145 OldCompletionCallback* callback) {
127 // Read random number of bytes. 146 // Read random number of bytes.
128 buf_len = rand() % buf_len + 1; 147 buf_len = rand() % buf_len + 1;
129 return incoming_->Read(buf, buf_len, callback); 148 return incoming_->Read(buf, buf_len, callback);
130 } 149 }
150 virtual int Read(IOBuffer* buf, int buf_len,
151 const CompletionCallback& callback) {
152 // Read random number of bytes.
153 buf_len = rand() % buf_len + 1;
154 return incoming_->Read(buf, buf_len, callback);
155 }
131 156
132 virtual int Write(IOBuffer* buf, int buf_len, 157 virtual int Write(IOBuffer* buf, int buf_len,
133 OldCompletionCallback* callback) { 158 OldCompletionCallback* callback) {
134 // Write random number of bytes. 159 // Write random number of bytes.
135 buf_len = rand() % buf_len + 1; 160 buf_len = rand() % buf_len + 1;
136 return outgoing_->Write(buf, buf_len, callback); 161 return outgoing_->Write(buf, buf_len, callback);
137 } 162 }
138 163
139 virtual bool SetReceiveBufferSize(int32 size) { 164 virtual bool SetReceiveBufferSize(int32 size) {
140 return true; 165 return true;
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after
471 const char* kKeyingLabelBad = "EXPERIMENTAL-server-socket-test-bad"; 496 const char* kKeyingLabelBad = "EXPERIMENTAL-server-socket-test-bad";
472 unsigned char client_bad[kKeyingMaterialSize]; 497 unsigned char client_bad[kKeyingMaterialSize];
473 rv = client_socket_->ExportKeyingMaterial(kKeyingLabelBad, kKeyingContext, 498 rv = client_socket_->ExportKeyingMaterial(kKeyingLabelBad, kKeyingContext,
474 client_bad, sizeof(client_bad)); 499 client_bad, sizeof(client_bad));
475 ASSERT_EQ(rv, net::OK); 500 ASSERT_EQ(rv, net::OK);
476 EXPECT_TRUE(memcmp(server_out, client_bad, sizeof(server_out)) != 0); 501 EXPECT_TRUE(memcmp(server_out, client_bad, sizeof(server_out)) != 0);
477 } 502 }
478 #endif 503 #endif
479 504
480 } // namespace net 505 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698