1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "blimp/net/common.h"
#include "blimp/net/stream_packet_writer.h"
#include "blimp/net/test_common.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
#include "net/socket/socket.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
using testing::DoAll;
using testing::InSequence;
using testing::Mock;
using testing::NotNull;
using testing::Return;
using testing::SaveArg;
namespace blimp {
namespace {
class StreamPacketWriterTest : public testing::Test {
public:
StreamPacketWriterTest()
: test_data_(
new net::DrainableIOBuffer(new net::StringIOBuffer(test_data_str_),
test_data_str_.size())),
message_writer_(&socket_) {}
protected:
const std::string test_data_str_ = "U WOT M8";
scoped_refptr<net::DrainableIOBuffer> test_data_;
base::MessageLoop message_loop_;
MockStreamSocket socket_;
StreamPacketWriter message_writer_;
testing::InSequence mock_sequence_;
private:
DISALLOW_COPY_AND_ASSIGN(StreamPacketWriterTest);
};
// Successful write with 1 async header write and 1 async payload write.
TEST_F(StreamPacketWriterTest, TestWriteAsync) {
net::TestCompletionCallback writer_cb;
net::CompletionCallback header_cb;
net::CompletionCallback payload_cb;
EXPECT_CALL(socket_, Write(BufferEquals(EncodeHeader(test_data_str_.size())),
kPacketHeaderSizeBytes, _))
.WillOnce(DoAll(SaveArg<2>(&header_cb), Return(net::ERR_IO_PENDING)));
message_writer_.WritePacket(test_data_, writer_cb.callback());
EXPECT_CALL(socket_,
Write(BufferEquals(test_data_str_), test_data_str_.size(), _))
.WillOnce(DoAll(SaveArg<2>(&payload_cb), Return(net::ERR_IO_PENDING)));
header_cb.Run(kPacketHeaderSizeBytes);
payload_cb.Run(test_data_str_.size());
EXPECT_EQ(net::OK, writer_cb.WaitForResult());
}
// Successful write with 2 async header writes and 2 async payload writes.
TEST_F(StreamPacketWriterTest, TestPartialWriteAsync) {
net::TestCompletionCallback writer_cb;
net::CompletionCallback header_cb;
net::CompletionCallback payload_cb;
std::string header = EncodeHeader(test_data_str_.size());
std::string payload = test_data_str_;
EXPECT_CALL(socket_, Write(BufferEquals(header), kPacketHeaderSizeBytes, _))
.WillOnce(DoAll(SaveArg<2>(&header_cb), Return(net::ERR_IO_PENDING)))
.RetiresOnSaturation();
EXPECT_CALL(socket_, Write(BufferEquals(header.substr(1, header.size())),
header.size() - 1, _))
.WillOnce(DoAll(SaveArg<2>(&header_cb), Return(net::ERR_IO_PENDING)))
.RetiresOnSaturation();
EXPECT_CALL(socket_, Write(BufferEquals(payload), payload.size(), _))
.WillOnce(DoAll(SaveArg<2>(&payload_cb), Return(net::ERR_IO_PENDING)))
.RetiresOnSaturation();
EXPECT_CALL(socket_,
Write(BufferEquals(payload.substr(1, payload.size() - 1)),
payload.size() - 1, _))
.WillOnce(DoAll(SaveArg<2>(&payload_cb), Return(net::ERR_IO_PENDING)))
.RetiresOnSaturation();
message_writer_.WritePacket(test_data_, writer_cb.callback());
// Header is written - first one byte, then the remainder.
header_cb.Run(1);
header_cb.Run(header.size() - 1);
// Payload is written - first one byte, then the remainder.
payload_cb.Run(1);
payload_cb.Run(payload.size() - 1);
EXPECT_EQ(net::OK, writer_cb.WaitForResult());
}
// Async socket error while writing data.
TEST_F(StreamPacketWriterTest, TestWriteErrorAsync) {
net::TestCompletionCallback writer_cb;
net::CompletionCallback header_cb;
net::CompletionCallback payload_cb;
EXPECT_CALL(socket_, Write(BufferEquals(EncodeHeader(test_data_str_.size())),
kPacketHeaderSizeBytes, _))
.WillOnce(DoAll(SaveArg<2>(&header_cb), Return(net::ERR_IO_PENDING)));
EXPECT_CALL(socket_,
Write(BufferEquals(test_data_str_), test_data_str_.size(), _))
.WillOnce(DoAll(SaveArg<2>(&payload_cb), Return(net::ERR_IO_PENDING)));
message_writer_.WritePacket(test_data_, writer_cb.callback());
header_cb.Run(kPacketHeaderSizeBytes);
payload_cb.Run(net::ERR_CONNECTION_RESET);
EXPECT_EQ(net::ERR_CONNECTION_RESET, writer_cb.WaitForResult());
}
// Successful write with 1 sync header write and 1 sync payload write.
TEST_F(StreamPacketWriterTest, TestWriteSync) {
net::TestCompletionCallback writer_cb;
EXPECT_CALL(socket_, Write(BufferEquals(EncodeHeader(test_data_str_.size())),
kPacketHeaderSizeBytes, _))
.WillOnce(Return(kPacketHeaderSizeBytes));
EXPECT_CALL(socket_,
Write(BufferEquals(test_data_str_), test_data_str_.size(), _))
.WillOnce(Return(test_data_str_.size()));
message_writer_.WritePacket(test_data_, writer_cb.callback());
EXPECT_EQ(net::OK, writer_cb.WaitForResult());
}
// Successful write with 2 sync header writes and 2 sync payload writes.
TEST_F(StreamPacketWriterTest, TestPartialWriteSync) {
net::TestCompletionCallback writer_cb;
std::string header = EncodeHeader(test_data_str_.size());
std::string payload = test_data_str_;
EXPECT_CALL(socket_, Write(BufferEquals(header), header.size(), _))
.WillOnce(Return(1));
EXPECT_CALL(socket_, Write(BufferEquals(header.substr(1, header.size() - 1)),
header.size() - 1, _))
.WillOnce(Return(header.size() - 1));
EXPECT_CALL(socket_, Write(BufferEquals(payload), payload.size(), _))
.WillOnce(Return(1));
EXPECT_CALL(socket_, Write(BufferEquals(payload.substr(1, payload.size())),
payload.size() - 1, _))
.WillOnce(Return(payload.size() - 1));
message_writer_.WritePacket(test_data_, writer_cb.callback());
EXPECT_EQ(net::OK, writer_cb.WaitForResult());
}
// Sync socket error while writing header data.
TEST_F(StreamPacketWriterTest, TestWriteHeaderErrorSync) {
net::TestCompletionCallback writer_cb;
EXPECT_CALL(socket_, Write(BufferEquals(EncodeHeader(test_data_str_.size())),
kPacketHeaderSizeBytes, _))
.WillOnce(Return(net::ERR_FAILED));
message_writer_.WritePacket(test_data_, writer_cb.callback());
EXPECT_EQ(net::ERR_FAILED, writer_cb.WaitForResult());
EXPECT_EQ(net::ERR_EMPTY_RESPONSE,
writer_cb.GetResult(net::ERR_EMPTY_RESPONSE));
EXPECT_FALSE(writer_cb.have_result());
}
// Sync socket error while writing payload data.
TEST_F(StreamPacketWriterTest, TestWritePayloadErrorSync) {
net::TestCompletionCallback writer_cb;
EXPECT_CALL(socket_, Write(BufferEquals(EncodeHeader(test_data_str_.size())),
kPacketHeaderSizeBytes, _))
.WillOnce(Return(kPacketHeaderSizeBytes));
EXPECT_CALL(socket_,
Write(BufferEquals(test_data_str_), test_data_str_.size(), _))
.WillOnce(Return(net::ERR_FAILED));
message_writer_.WritePacket(test_data_, writer_cb.callback());
EXPECT_EQ(net::ERR_FAILED, writer_cb.WaitForResult());
}
// Verify that asynchronous header write completions don't cause a
// use-after-free error if the writer object is deleted.
TEST_F(StreamPacketWriterTest, DeletedDuringHeaderWrite) {
net::TestCompletionCallback writer_cb;
net::CompletionCallback header_cb;
net::CompletionCallback payload_cb;
scoped_ptr<StreamPacketWriter> writer(new StreamPacketWriter(&socket_));
// Write header.
EXPECT_CALL(socket_, Write(BufferEquals(EncodeHeader(test_data_str_.size())),
kPacketHeaderSizeBytes, _))
.WillOnce(DoAll(SaveArg<2>(&header_cb), Return(net::ERR_IO_PENDING)));
writer->WritePacket(test_data_, writer_cb.callback());
Mock::VerifyAndClearExpectations(&socket_);
// Header write completion callback is invoked after the writer died.
writer.reset();
header_cb.Run(kPacketHeaderSizeBytes);
}
// Verify that asynchronous payload write completions don't cause a
// use-after-free error if the writer object is deleted.
TEST_F(StreamPacketWriterTest, DeletedDuringPayloadWrite) {
net::TestCompletionCallback writer_cb;
net::CompletionCallback header_cb;
net::CompletionCallback payload_cb;
scoped_ptr<StreamPacketWriter> writer(new StreamPacketWriter(&socket_));
EXPECT_CALL(socket_, Write(BufferEquals(EncodeHeader(test_data_str_.size())),
kPacketHeaderSizeBytes, _))
.WillOnce(DoAll(SaveArg<2>(&header_cb), Return(net::ERR_IO_PENDING)));
EXPECT_CALL(socket_,
Write(BufferEquals(test_data_str_), test_data_str_.size(), _))
.WillOnce(DoAll(SaveArg<2>(&payload_cb), Return(net::ERR_IO_PENDING)));
writer->WritePacket(test_data_, writer_cb.callback());
// Header write completes successfully.
header_cb.Run(kPacketHeaderSizeBytes);
// Payload write completion callback is invoked after the writer died.
writer.reset();
payload_cb.Run(test_data_str_.size());
}
} // namespace
} // namespace blimp
|