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

Side by Side Diff: blimp/net/stream_packet_writer_unittest.cc

Issue 2632803002: Remove all blimp network code. (Closed)
Patch Set: merge from origin/master for good measure Created 3 years, 11 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 | « blimp/net/stream_packet_writer.cc ('k') | blimp/net/stream_socket_connection.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <memory>
6 #include <string>
7
8 #include "base/at_exit.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "blimp/net/blimp_stats.h"
12 #include "blimp/net/common.h"
13 #include "blimp/net/stream_packet_writer.h"
14 #include "blimp/net/test_common.h"
15 #include "net/base/io_buffer.h"
16 #include "net/base/net_errors.h"
17 #include "net/base/test_completion_callback.h"
18 #include "net/socket/socket.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 using testing::_;
23 using testing::DoAll;
24 using testing::InSequence;
25 using testing::Mock;
26 using testing::NotNull;
27 using testing::Return;
28 using testing::SaveArg;
29
30 namespace blimp {
31 namespace {
32
33 class StreamPacketWriterTest : public testing::Test {
34 public:
35 StreamPacketWriterTest()
36 : test_data_(
37 new net::DrainableIOBuffer(new net::StringIOBuffer(test_data_str_),
38 test_data_str_.size())),
39 message_writer_(&socket_) {}
40
41 protected:
42 const std::string test_data_str_ = "U WOT M8";
43
44 base::ShadowingAtExitManager at_exit_manager_;
45 base::MessageLoop message_loop_;
46 MockStreamSocket socket_;
47 scoped_refptr<net::DrainableIOBuffer> test_data_;
48 StreamPacketWriter message_writer_;
49 testing::InSequence mock_sequence_;
50
51 private:
52 DISALLOW_COPY_AND_ASSIGN(StreamPacketWriterTest);
53 };
54
55 // Successful write with 1 async header write and 1 async payload write.
56 TEST_F(StreamPacketWriterTest, TestWriteAsync) {
57 net::TestCompletionCallback writer_cb;
58 net::CompletionCallback header_cb;
59 net::CompletionCallback payload_cb;
60
61 EXPECT_CALL(socket_, Write(BufferEquals(EncodeHeader(test_data_str_.size())),
62 kPacketHeaderSizeBytes, _))
63 .WillOnce(DoAll(SaveArg<2>(&header_cb), Return(net::ERR_IO_PENDING)));
64 message_writer_.WritePacket(test_data_, writer_cb.callback());
65 EXPECT_CALL(socket_,
66 Write(BufferEquals(test_data_str_), test_data_str_.size(), _))
67 .WillOnce(DoAll(SaveArg<2>(&payload_cb), Return(net::ERR_IO_PENDING)));
68
69 header_cb.Run(kPacketHeaderSizeBytes);
70 payload_cb.Run(test_data_str_.size());
71 EXPECT_EQ(net::OK, writer_cb.WaitForResult());
72 }
73
74 // Successful write with 2 async header writes and 2 async payload writes.
75 TEST_F(StreamPacketWriterTest, TestPartialWriteAsync) {
76 net::TestCompletionCallback writer_cb;
77 net::CompletionCallback header_cb;
78 net::CompletionCallback payload_cb;
79
80 std::string header = EncodeHeader(test_data_str_.size());
81 std::string payload = test_data_str_;
82
83 EXPECT_CALL(socket_, Write(BufferEquals(header), kPacketHeaderSizeBytes, _))
84 .WillOnce(DoAll(SaveArg<2>(&header_cb), Return(net::ERR_IO_PENDING)))
85 .RetiresOnSaturation();
86 EXPECT_CALL(socket_, Write(BufferEquals(header.substr(1, header.size())),
87 header.size() - 1, _))
88 .WillOnce(DoAll(SaveArg<2>(&header_cb), Return(net::ERR_IO_PENDING)))
89 .RetiresOnSaturation();
90 EXPECT_CALL(socket_, Write(BufferEquals(payload), payload.size(), _))
91 .WillOnce(DoAll(SaveArg<2>(&payload_cb), Return(net::ERR_IO_PENDING)))
92 .RetiresOnSaturation();
93 EXPECT_CALL(socket_,
94 Write(BufferEquals(payload.substr(1, payload.size() - 1)),
95 payload.size() - 1, _))
96 .WillOnce(DoAll(SaveArg<2>(&payload_cb), Return(net::ERR_IO_PENDING)))
97 .RetiresOnSaturation();
98
99 message_writer_.WritePacket(test_data_, writer_cb.callback());
100
101 EXPECT_EQ(static_cast<int>(payload.size()),
102 BlimpStats::GetInstance()->Get(BlimpStats::BYTES_SENT));
103
104 // Header is written - first one byte, then the remainder.
105 header_cb.Run(1);
106 header_cb.Run(header.size() - 1);
107
108 // Payload is written - first one byte, then the remainder.
109 payload_cb.Run(1);
110 payload_cb.Run(payload.size() - 1);
111
112 EXPECT_EQ(net::OK, writer_cb.WaitForResult());
113 }
114
115 // Async socket error while writing data.
116 TEST_F(StreamPacketWriterTest, TestWriteErrorAsync) {
117 net::TestCompletionCallback writer_cb;
118 net::CompletionCallback header_cb;
119 net::CompletionCallback payload_cb;
120
121 EXPECT_CALL(socket_, Write(BufferEquals(EncodeHeader(test_data_str_.size())),
122 kPacketHeaderSizeBytes, _))
123 .WillOnce(DoAll(SaveArg<2>(&header_cb), Return(net::ERR_IO_PENDING)));
124 EXPECT_CALL(socket_,
125 Write(BufferEquals(test_data_str_), test_data_str_.size(), _))
126 .WillOnce(DoAll(SaveArg<2>(&payload_cb), Return(net::ERR_IO_PENDING)));
127
128 message_writer_.WritePacket(test_data_, writer_cb.callback());
129 header_cb.Run(kPacketHeaderSizeBytes);
130 payload_cb.Run(net::ERR_CONNECTION_RESET);
131
132 EXPECT_EQ(net::ERR_CONNECTION_RESET, writer_cb.WaitForResult());
133 }
134
135 // Successful write with 1 sync header write and 1 sync payload write.
136 TEST_F(StreamPacketWriterTest, TestWriteSync) {
137 net::TestCompletionCallback writer_cb;
138
139 EXPECT_CALL(socket_, Write(BufferEquals(EncodeHeader(test_data_str_.size())),
140 kPacketHeaderSizeBytes, _))
141 .WillOnce(Return(kPacketHeaderSizeBytes));
142 EXPECT_CALL(socket_,
143 Write(BufferEquals(test_data_str_), test_data_str_.size(), _))
144 .WillOnce(Return(test_data_str_.size()));
145
146 message_writer_.WritePacket(test_data_, writer_cb.callback());
147 EXPECT_EQ(net::OK, writer_cb.WaitForResult());
148 }
149
150 // Successful write with 2 sync header writes and 2 sync payload writes.
151 TEST_F(StreamPacketWriterTest, TestPartialWriteSync) {
152 net::TestCompletionCallback writer_cb;
153
154 std::string header = EncodeHeader(test_data_str_.size());
155 std::string payload = test_data_str_;
156
157 EXPECT_CALL(socket_, Write(BufferEquals(header), header.size(), _))
158 .WillOnce(Return(1));
159 EXPECT_CALL(socket_, Write(BufferEquals(header.substr(1, header.size() - 1)),
160 header.size() - 1, _))
161 .WillOnce(Return(header.size() - 1));
162 EXPECT_CALL(socket_, Write(BufferEquals(payload), payload.size(), _))
163 .WillOnce(Return(1));
164 EXPECT_CALL(socket_, Write(BufferEquals(payload.substr(1, payload.size())),
165 payload.size() - 1, _))
166 .WillOnce(Return(payload.size() - 1));
167
168 message_writer_.WritePacket(test_data_, writer_cb.callback());
169 EXPECT_EQ(net::OK, writer_cb.WaitForResult());
170 }
171
172 // Sync socket error while writing header data.
173 TEST_F(StreamPacketWriterTest, TestWriteHeaderErrorSync) {
174 net::TestCompletionCallback writer_cb;
175
176 EXPECT_CALL(socket_, Write(BufferEquals(EncodeHeader(test_data_str_.size())),
177 kPacketHeaderSizeBytes, _))
178 .WillOnce(Return(net::ERR_FAILED));
179
180 message_writer_.WritePacket(test_data_, writer_cb.callback());
181 EXPECT_EQ(net::ERR_FAILED, writer_cb.WaitForResult());
182 EXPECT_EQ(net::ERR_EMPTY_RESPONSE,
183 writer_cb.GetResult(net::ERR_EMPTY_RESPONSE));
184 EXPECT_FALSE(writer_cb.have_result());
185 }
186
187 // Sync socket error while writing payload data.
188 TEST_F(StreamPacketWriterTest, TestWritePayloadErrorSync) {
189 net::TestCompletionCallback writer_cb;
190
191 EXPECT_CALL(socket_, Write(BufferEquals(EncodeHeader(test_data_str_.size())),
192 kPacketHeaderSizeBytes, _))
193 .WillOnce(Return(kPacketHeaderSizeBytes));
194 EXPECT_CALL(socket_,
195 Write(BufferEquals(test_data_str_), test_data_str_.size(), _))
196 .WillOnce(Return(net::ERR_FAILED));
197
198 message_writer_.WritePacket(test_data_, writer_cb.callback());
199 EXPECT_EQ(net::ERR_FAILED, writer_cb.WaitForResult());
200 }
201
202 // Verify that asynchronous header write completions don't cause a
203 // use-after-free error if the writer object is deleted.
204 TEST_F(StreamPacketWriterTest, DeletedDuringHeaderWrite) {
205 net::TestCompletionCallback writer_cb;
206 net::CompletionCallback header_cb;
207 net::CompletionCallback payload_cb;
208 std::unique_ptr<StreamPacketWriter> writer(new StreamPacketWriter(&socket_));
209
210 // Write header.
211 EXPECT_CALL(socket_, Write(BufferEquals(EncodeHeader(test_data_str_.size())),
212 kPacketHeaderSizeBytes, _))
213 .WillOnce(DoAll(SaveArg<2>(&header_cb), Return(net::ERR_IO_PENDING)));
214 writer->WritePacket(test_data_, writer_cb.callback());
215 Mock::VerifyAndClearExpectations(&socket_);
216
217 // Header write completion callback is invoked after the writer died.
218 writer.reset();
219 header_cb.Run(kPacketHeaderSizeBytes);
220 }
221
222 // Verify that asynchronous payload write completions don't cause a
223 // use-after-free error if the writer object is deleted.
224 TEST_F(StreamPacketWriterTest, DeletedDuringPayloadWrite) {
225 net::TestCompletionCallback writer_cb;
226 net::CompletionCallback header_cb;
227 net::CompletionCallback payload_cb;
228 std::unique_ptr<StreamPacketWriter> writer(new StreamPacketWriter(&socket_));
229
230 EXPECT_CALL(socket_, Write(BufferEquals(EncodeHeader(test_data_str_.size())),
231 kPacketHeaderSizeBytes, _))
232 .WillOnce(DoAll(SaveArg<2>(&header_cb), Return(net::ERR_IO_PENDING)));
233 EXPECT_CALL(socket_,
234 Write(BufferEquals(test_data_str_), test_data_str_.size(), _))
235 .WillOnce(DoAll(SaveArg<2>(&payload_cb), Return(net::ERR_IO_PENDING)));
236
237 writer->WritePacket(test_data_, writer_cb.callback());
238
239 // Header write completes successfully.
240 header_cb.Run(kPacketHeaderSizeBytes);
241
242 // Payload write completion callback is invoked after the writer died.
243 writer.reset();
244 payload_cb.Run(test_data_str_.size());
245 }
246
247 TEST_F(StreamPacketWriterTest, TestWriteHeaderEOFSync) {
248 net::TestCompletionCallback writer_cb;
249
250 EXPECT_CALL(socket_, Write(BufferEquals(EncodeHeader(test_data_str_.size())),
251 kPacketHeaderSizeBytes, _))
252 .WillOnce(Return(net::OK));
253 message_writer_.WritePacket(test_data_, writer_cb.callback());
254
255 EXPECT_EQ(net::ERR_CONNECTION_CLOSED, writer_cb.WaitForResult());
256 }
257
258 TEST_F(StreamPacketWriterTest, TestWritePayloadEOFSync) {
259 net::TestCompletionCallback writer_cb;
260
261 EXPECT_CALL(socket_, Write(BufferEquals(EncodeHeader(test_data_str_.size())),
262 kPacketHeaderSizeBytes, _))
263 .WillOnce(Return(kPacketHeaderSizeBytes));
264 EXPECT_CALL(socket_,
265 Write(BufferEquals(test_data_str_), test_data_str_.size(), _))
266 .WillOnce(Return(0));
267 message_writer_.WritePacket(test_data_, writer_cb.callback());
268
269 EXPECT_EQ(net::ERR_CONNECTION_CLOSED, writer_cb.WaitForResult());
270 }
271
272 TEST_F(StreamPacketWriterTest, TestWriteHeaderEOFAsync) {
273 net::TestCompletionCallback writer_cb;
274 net::CompletionCallback header_cb;
275
276 EXPECT_CALL(socket_, Write(BufferEquals(EncodeHeader(test_data_str_.size())),
277 kPacketHeaderSizeBytes, _))
278 .WillOnce(DoAll(SaveArg<2>(&header_cb), Return(net::ERR_IO_PENDING)));
279 message_writer_.WritePacket(test_data_, writer_cb.callback());
280 header_cb.Run(0);
281
282 EXPECT_EQ(net::ERR_CONNECTION_CLOSED, writer_cb.WaitForResult());
283 }
284
285 TEST_F(StreamPacketWriterTest, TestWritePayloadEOFAsync) {
286 net::TestCompletionCallback writer_cb;
287 net::CompletionCallback header_cb;
288 net::CompletionCallback payload_cb;
289
290 EXPECT_CALL(socket_, Write(BufferEquals(EncodeHeader(test_data_str_.size())),
291 kPacketHeaderSizeBytes, _))
292 .WillOnce(DoAll(SaveArg<2>(&header_cb), Return(net::ERR_IO_PENDING)));
293 EXPECT_CALL(socket_,
294 Write(BufferEquals(test_data_str_), test_data_str_.size(), _))
295 .WillOnce(DoAll(SaveArg<2>(&payload_cb), Return(net::ERR_IO_PENDING)));
296 message_writer_.WritePacket(test_data_, writer_cb.callback());
297 header_cb.Run(kPacketHeaderSizeBytes);
298 payload_cb.Run(0);
299
300 EXPECT_EQ(net::ERR_CONNECTION_CLOSED, writer_cb.WaitForResult());
301 }
302
303 } // namespace
304 } // namespace blimp
OLDNEW
« no previous file with comments | « blimp/net/stream_packet_writer.cc ('k') | blimp/net/stream_socket_connection.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698