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

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

Issue 1876983002: Use Chromium BUILD to approximate Blimp protocol version, and check it. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix Android to use GetVersionNumber() 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
« no previous file with comments | « blimp/net/blimp_connection.cc ('k') | blimp/net/connection_error_observer.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 2015 The Chromium Authors. All rights reserved. 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 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 "blimp/net/blimp_connection.h" 5 #include "blimp/net/blimp_connection.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <string> 9 #include <string>
10 10
(...skipping 19 matching lines...) Expand all
30 30
31 namespace blimp { 31 namespace blimp {
32 namespace { 32 namespace {
33 33
34 class BlimpConnectionTest : public testing::Test { 34 class BlimpConnectionTest : public testing::Test {
35 public: 35 public:
36 BlimpConnectionTest() { 36 BlimpConnectionTest() {
37 std::unique_ptr<testing::StrictMock<MockPacketWriter>> writer( 37 std::unique_ptr<testing::StrictMock<MockPacketWriter>> writer(
38 new testing::StrictMock<MockPacketWriter>); 38 new testing::StrictMock<MockPacketWriter>);
39 writer_ = writer.get(); 39 writer_ = writer.get();
40 connection_.reset(new BlimpConnection( 40 std::unique_ptr<testing::StrictMock<MockPacketReader>> reader(
41 base::WrapUnique(new MockPacketReader), std::move(writer))); 41 new testing::StrictMock<MockPacketReader>);
42 reader_ = reader.get();
43 connection_.reset(
44 new BlimpConnection(std::move(reader), std::move(writer)));
42 connection_->AddConnectionErrorObserver(&error_observer1_); 45 connection_->AddConnectionErrorObserver(&error_observer1_);
43 connection_->AddConnectionErrorObserver(&error_observer2_); 46 connection_->AddConnectionErrorObserver(&error_observer2_);
44 connection_->AddConnectionErrorObserver(&error_observer3_); 47 connection_->AddConnectionErrorObserver(&error_observer3_);
45 connection_->RemoveConnectionErrorObserver(&error_observer3_); 48 connection_->RemoveConnectionErrorObserver(&error_observer3_);
46 } 49 }
47 50
48 ~BlimpConnectionTest() override {} 51 ~BlimpConnectionTest() override {}
49 52
50 void DropConnection() { connection_.reset(); } 53 void DropConnection() { connection_.reset(); }
51 54
52 protected: 55 protected:
53 std::unique_ptr<BlimpMessage> CreateInputMessage() { 56 std::unique_ptr<BlimpMessage> CreateInputMessage() {
54 InputMessage* input; 57 InputMessage* input;
55 return CreateBlimpMessage(&input); 58 return CreateBlimpMessage(&input);
56 } 59 }
57 60
58 std::unique_ptr<BlimpMessage> CreateControlMessage() { 61 std::unique_ptr<BlimpMessage> CreateControlMessage() {
59 TabControlMessage* control; 62 TabControlMessage* control;
60 return CreateBlimpMessage(&control); 63 return CreateBlimpMessage(&control);
61 } 64 }
62 65
63 base::MessageLoop message_loop_; 66 base::MessageLoop message_loop_;
67 testing::StrictMock<MockPacketReader>* reader_;
64 testing::StrictMock<MockPacketWriter>* writer_; 68 testing::StrictMock<MockPacketWriter>* writer_;
65 testing::StrictMock<MockConnectionErrorObserver> error_observer1_; 69 testing::StrictMock<MockConnectionErrorObserver> error_observer1_;
66 testing::StrictMock<MockConnectionErrorObserver> error_observer2_; 70 testing::StrictMock<MockConnectionErrorObserver> error_observer2_;
67 71
68 // This error observer is Removed() immediately after it's added; 72 // This error observer is Removed() immediately after it's added;
69 // it should never be called. 73 // it should never be called.
70 testing::StrictMock<MockConnectionErrorObserver> error_observer3_; 74 testing::StrictMock<MockConnectionErrorObserver> error_observer3_;
71 75
72 testing::StrictMock<MockBlimpMessageProcessor> receiver_; 76 testing::StrictMock<MockBlimpMessageProcessor> receiver_;
73 std::unique_ptr<BlimpConnection> connection_; 77 std::unique_ptr<BlimpConnection> connection_;
74 }; 78 };
75 79
76 // Write completes writing two packets asynchronously. 80 // Write completes writing two packets asynchronously.
77 TEST_F(BlimpConnectionTest, AsyncTwoPacketsWrite) { 81 TEST_F(BlimpConnectionTest, AsyncTwoPacketsWrite) {
78 net::CompletionCallback write_packet_cb; 82 net::CompletionCallback write_packet_cb;
79 83
80 InSequence s; 84 InSequence s;
81 EXPECT_CALL(*writer_, 85 EXPECT_CALL(*writer_,
82 WritePacket(BufferEqualsProto(*CreateInputMessage()), _)) 86 WritePacket(BufferEqualsProto(*CreateInputMessage()), _))
83 .WillOnce(SaveArg<1>(&write_packet_cb)) 87 .WillOnce(SaveArg<1>(&write_packet_cb))
84 .RetiresOnSaturation(); 88 .RetiresOnSaturation();
85 EXPECT_CALL(*writer_, 89 EXPECT_CALL(*writer_,
86 WritePacket(BufferEqualsProto(*CreateControlMessage()), _)) 90 WritePacket(BufferEqualsProto(*CreateControlMessage()), _))
87 .WillOnce(SaveArg<1>(&write_packet_cb)) 91 .WillOnce(SaveArg<1>(&write_packet_cb))
88 .RetiresOnSaturation(); 92 .RetiresOnSaturation();
93 EXPECT_CALL(error_observer1_, OnConnectionError(_)).Times(0);
94 EXPECT_CALL(error_observer2_, OnConnectionError(_)).Times(0);
95 EXPECT_CALL(error_observer3_, OnConnectionError(_)).Times(0);
89 96
90 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor(); 97 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor();
91 net::TestCompletionCallback complete_cb_1; 98 net::TestCompletionCallback complete_cb_1;
92 ASSERT_TRUE(write_packet_cb.is_null()); 99 ASSERT_TRUE(write_packet_cb.is_null());
93 sender->ProcessMessage(CreateInputMessage(), 100 sender->ProcessMessage(CreateInputMessage(),
94 complete_cb_1.callback()); 101 complete_cb_1.callback());
95 ASSERT_FALSE(write_packet_cb.is_null()); 102 ASSERT_FALSE(write_packet_cb.is_null());
96 base::ResetAndReturn(&write_packet_cb).Run(net::OK); 103 base::ResetAndReturn(&write_packet_cb).Run(net::OK);
97 EXPECT_EQ(net::OK, complete_cb_1.WaitForResult()); 104 EXPECT_EQ(net::OK, complete_cb_1.WaitForResult());
98 105
(...skipping 15 matching lines...) Expand all
114 EXPECT_CALL(*writer_, 121 EXPECT_CALL(*writer_,
115 WritePacket(BufferEqualsProto(*CreateInputMessage()), _)) 122 WritePacket(BufferEqualsProto(*CreateInputMessage()), _))
116 .WillOnce(SaveArg<1>(&write_packet_cb)) 123 .WillOnce(SaveArg<1>(&write_packet_cb))
117 .RetiresOnSaturation(); 124 .RetiresOnSaturation();
118 EXPECT_CALL(*writer_, 125 EXPECT_CALL(*writer_,
119 WritePacket(BufferEqualsProto(*CreateControlMessage()), _)) 126 WritePacket(BufferEqualsProto(*CreateControlMessage()), _))
120 .WillOnce(SaveArg<1>(&write_packet_cb)) 127 .WillOnce(SaveArg<1>(&write_packet_cb))
121 .RetiresOnSaturation(); 128 .RetiresOnSaturation();
122 EXPECT_CALL(error_observer1_, OnConnectionError(net::ERR_FAILED)); 129 EXPECT_CALL(error_observer1_, OnConnectionError(net::ERR_FAILED));
123 EXPECT_CALL(error_observer2_, OnConnectionError(net::ERR_FAILED)); 130 EXPECT_CALL(error_observer2_, OnConnectionError(net::ERR_FAILED));
131 EXPECT_CALL(error_observer3_, OnConnectionError(_)).Times(0);
124 132
125 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor(); 133 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor();
126 net::TestCompletionCallback complete_cb_1; 134 net::TestCompletionCallback complete_cb_1;
127 sender->ProcessMessage(CreateInputMessage(), 135 sender->ProcessMessage(CreateInputMessage(),
128 complete_cb_1.callback()); 136 complete_cb_1.callback());
129 base::ResetAndReturn(&write_packet_cb).Run(net::OK); 137 base::ResetAndReturn(&write_packet_cb).Run(net::OK);
130 EXPECT_EQ(net::OK, complete_cb_1.WaitForResult()); 138 EXPECT_EQ(net::OK, complete_cb_1.WaitForResult());
131 139
132 net::TestCompletionCallback complete_cb_2; 140 net::TestCompletionCallback complete_cb_2;
133 sender->ProcessMessage(CreateControlMessage(), 141 sender->ProcessMessage(CreateControlMessage(),
(...skipping 15 matching lines...) Expand all
149 this, &BlimpConnectionTest::DropConnection)); 157 this, &BlimpConnectionTest::DropConnection));
150 158
151 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor(); 159 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor();
152 net::TestCompletionCallback complete_cb_1; 160 net::TestCompletionCallback complete_cb_1;
153 sender->ProcessMessage(CreateInputMessage(), 161 sender->ProcessMessage(CreateInputMessage(),
154 complete_cb_1.callback()); 162 complete_cb_1.callback());
155 base::ResetAndReturn(&write_packet_cb).Run(net::ERR_FAILED); 163 base::ResetAndReturn(&write_packet_cb).Run(net::ERR_FAILED);
156 EXPECT_EQ(net::ERR_FAILED, complete_cb_1.WaitForResult()); 164 EXPECT_EQ(net::ERR_FAILED, complete_cb_1.WaitForResult());
157 } 165 }
158 166
167 // Verifies that a ReadPacket error causes ErrorObservers to be notified.
168 TEST_F(BlimpConnectionTest, ReadPacketErrorInvokesErrorObservers) {
169 scoped_refptr<net::GrowableIOBuffer> read_packet_buffer;
170 net::CompletionCallback read_packet_cb;
171
172 EXPECT_CALL(*reader_, ReadPacket(_, _))
173 .WillOnce(
174 DoAll(SaveArg<0>(&read_packet_buffer), SaveArg<1>(&read_packet_cb)))
175 .RetiresOnSaturation();
176
177 EXPECT_CALL(error_observer1_, OnConnectionError(net::ERR_FAILED));
178 EXPECT_CALL(error_observer2_, OnConnectionError(net::ERR_FAILED));
179 EXPECT_CALL(error_observer3_, OnConnectionError(_)).Times(0);
180
181 EXPECT_CALL(receiver_, MockableProcessMessage(_, _)).Times(0);
182
183 // Trigger the first ReadPacket() call by setting the MessageProcessor.
184 connection_->SetIncomingMessageProcessor(&receiver_);
185 EXPECT_TRUE(read_packet_buffer);
186 EXPECT_FALSE(read_packet_cb.is_null());
187
188 // Signal an error back from the ReadPacket operation.
189 base::ResetAndReturn(&read_packet_cb).Run(net::ERR_FAILED);
190 }
191
192 // Verifies that EndConnection messages received from the peer are
193 // routed through to registered ConnectionErrorObservers as errors.
194 TEST_F(BlimpConnectionTest, EndConnectionInvokesErrorObservers) {
195 scoped_refptr<net::GrowableIOBuffer> read_packet_buffer;
196 net::CompletionCallback read_packet_cb;
197
198 EXPECT_CALL(*reader_, ReadPacket(_, _))
199 .WillOnce(
200 DoAll(SaveArg<0>(&read_packet_buffer), SaveArg<1>(&read_packet_cb)))
201 .WillOnce(Return())
202 .RetiresOnSaturation();
203
204 EXPECT_CALL(error_observer1_,
205 OnConnectionError(EndConnectionMessage::PROTOCOL_MISMATCH));
206 EXPECT_CALL(error_observer2_,
207 OnConnectionError(EndConnectionMessage::PROTOCOL_MISMATCH));
208 EXPECT_CALL(error_observer3_, OnConnectionError(_)).Times(0);
209
210 EXPECT_CALL(receiver_, MockableProcessMessage(_, _)).Times(0);
211
212 // Trigger the first ReadPacket() call by setting the MessageProcessor.
213 connection_->SetIncomingMessageProcessor(&receiver_);
214 EXPECT_TRUE(read_packet_buffer);
215 EXPECT_FALSE(read_packet_cb.is_null());
216
217 // Create an EndConnection message to return from ReadPacket.
218 std::unique_ptr<BlimpMessage> message =
219 CreateEndConnectionMessage(EndConnectionMessage::PROTOCOL_MISMATCH);
220
221 // Put the EndConnection message in the buffer and invoke the read callback.
222 read_packet_buffer->SetCapacity(message->ByteSize());
223 ASSERT_TRUE(message->SerializeToArray(read_packet_buffer->data(),
224 message->GetCachedSize()));
225 base::ResetAndReturn(&read_packet_cb).Run(message->ByteSize());
226 }
227
159 } // namespace 228 } // namespace
160 } // namespace blimp 229 } // namespace blimp
OLDNEW
« no previous file with comments | « blimp/net/blimp_connection.cc ('k') | blimp/net/connection_error_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698