OLD | NEW |
| (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 <stddef.h> | |
6 #include <string> | |
7 | |
8 #include "base/callback_helpers.h" | |
9 #include "blimp/common/create_blimp_message.h" | |
10 #include "blimp/common/proto/blimp_message.pb.h" | |
11 #include "blimp/net/blimp_message_pump.h" | |
12 #include "blimp/net/common.h" | |
13 #include "blimp/net/connection_error_observer.h" | |
14 #include "blimp/net/test_common.h" | |
15 #include "net/base/completion_callback.h" | |
16 #include "net/base/io_buffer.h" | |
17 #include "net/base/net_errors.h" | |
18 #include "net/base/test_completion_callback.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::NotNull; | |
26 using testing::Return; | |
27 using testing::SaveArg; | |
28 | |
29 namespace blimp { | |
30 namespace { | |
31 | |
32 class BlimpMessagePumpTest : public testing::Test { | |
33 public: | |
34 BlimpMessagePumpTest() { | |
35 TabControlMessage* tab_control = nullptr; | |
36 message1_ = CreateBlimpMessage(&tab_control); | |
37 InputMessage* input = nullptr; | |
38 message2_ = CreateBlimpMessage(&input); | |
39 message_pump_.reset(new BlimpMessagePump(&reader_)); | |
40 message_pump_->set_error_observer(&error_observer_); | |
41 } | |
42 | |
43 ~BlimpMessagePumpTest() override {} | |
44 | |
45 void NullMessageProcessor() { message_pump_->SetMessageProcessor(nullptr); } | |
46 | |
47 protected: | |
48 std::unique_ptr<BlimpMessage> message1_; | |
49 std::unique_ptr<BlimpMessage> message2_; | |
50 | |
51 testing::StrictMock<MockPacketReader> reader_; | |
52 testing::StrictMock<MockConnectionErrorObserver> error_observer_; | |
53 testing::StrictMock<MockBlimpMessageProcessor> receiver_; | |
54 std::unique_ptr<BlimpMessagePump> message_pump_; | |
55 }; | |
56 | |
57 // Reader completes reading one packet asynchronously. | |
58 TEST_F(BlimpMessagePumpTest, ReadPacket) { | |
59 net::CompletionCallback read_packet_cb; | |
60 EXPECT_CALL(reader_, ReadPacket(NotNull(), _)); | |
61 EXPECT_CALL(reader_, ReadPacket(NotNull(), _)) | |
62 .WillOnce(DoAll(FillBufferFromMessage<0>(message1_.get()), | |
63 SaveArg<1>(&read_packet_cb))) | |
64 .RetiresOnSaturation(); | |
65 net::CompletionCallback process_msg_cb; | |
66 EXPECT_CALL(receiver_, MockableProcessMessage(EqualsProto(*message1_), _)) | |
67 .WillOnce(SaveArg<1>(&process_msg_cb)); | |
68 message_pump_->SetMessageProcessor(&receiver_); | |
69 ASSERT_FALSE(read_packet_cb.is_null()); | |
70 base::ResetAndReturn(&read_packet_cb).Run(message1_->ByteSize()); | |
71 process_msg_cb.Run(net::OK); | |
72 } | |
73 | |
74 // Reader completes reading two packets asynchronously. | |
75 TEST_F(BlimpMessagePumpTest, ReadTwoPackets) { | |
76 net::CompletionCallback read_packet_cb; | |
77 EXPECT_CALL(reader_, ReadPacket(NotNull(), _)) | |
78 .WillOnce(DoAll(FillBufferFromMessage<0>(message1_.get()), | |
79 SaveArg<1>(&read_packet_cb))) | |
80 .WillOnce(DoAll(FillBufferFromMessage<0>(message2_.get()), | |
81 SaveArg<1>(&read_packet_cb))); | |
82 net::CompletionCallback process_msg_cb; | |
83 { | |
84 InSequence s; | |
85 EXPECT_CALL(receiver_, MockableProcessMessage(EqualsProto(*message1_), _)) | |
86 .WillOnce(SaveArg<1>(&process_msg_cb)) | |
87 .RetiresOnSaturation(); | |
88 EXPECT_CALL(receiver_, MockableProcessMessage(EqualsProto(*message2_), _)); | |
89 } | |
90 message_pump_->SetMessageProcessor(&receiver_); | |
91 ASSERT_FALSE(read_packet_cb.is_null()); | |
92 base::ResetAndReturn(&read_packet_cb).Run(message1_->ByteSize()); | |
93 | |
94 // Trigger next packet read | |
95 process_msg_cb.Run(net::OK); | |
96 ASSERT_FALSE(read_packet_cb.is_null()); | |
97 base::ResetAndReturn(&read_packet_cb).Run(message2_->ByteSize()); | |
98 } | |
99 | |
100 // Reader completes reading two packets asynchronously. | |
101 // The first read succeeds, and the second fails. | |
102 TEST_F(BlimpMessagePumpTest, ReadTwoPacketsWithError) { | |
103 net::CompletionCallback process_msg_cb; | |
104 net::CompletionCallback read_packet_cb; | |
105 EXPECT_CALL(reader_, ReadPacket(NotNull(), _)) | |
106 .WillOnce(DoAll(FillBufferFromMessage<0>(message1_.get()), | |
107 SaveArg<1>(&read_packet_cb))) | |
108 .WillOnce(DoAll(FillBufferFromMessage<0>(message2_.get()), | |
109 SaveArg<1>(&read_packet_cb))); | |
110 EXPECT_CALL(receiver_, MockableProcessMessage(EqualsProto(*message1_), _)) | |
111 .WillOnce(SaveArg<1>(&process_msg_cb)); | |
112 EXPECT_CALL(error_observer_, OnConnectionError(net::ERR_FAILED)); | |
113 | |
114 message_pump_->SetMessageProcessor(&receiver_); | |
115 ASSERT_FALSE(read_packet_cb.is_null()); | |
116 base::ResetAndReturn(&read_packet_cb).Run(message1_->ByteSize()); | |
117 | |
118 // Trigger next packet read | |
119 process_msg_cb.Run(net::OK); | |
120 ASSERT_FALSE(read_packet_cb.is_null()); | |
121 base::ResetAndReturn(&read_packet_cb).Run(net::ERR_FAILED); | |
122 } | |
123 | |
124 // Reader completes reading one packet synchronously, but packet is invalid | |
125 TEST_F(BlimpMessagePumpTest, InvalidPacket) { | |
126 net::CompletionCallback read_packet_cb; | |
127 std::string test_msg("msg"); | |
128 EXPECT_CALL(reader_, ReadPacket(NotNull(), _)) | |
129 .WillOnce(DoAll(FillBufferFromString<0>(test_msg), | |
130 SaveArg<1>(&read_packet_cb))); | |
131 EXPECT_CALL(error_observer_, OnConnectionError(net::ERR_FAILED)); | |
132 | |
133 message_pump_->SetMessageProcessor(&receiver_); | |
134 ASSERT_FALSE(read_packet_cb.is_null()); | |
135 base::ResetAndReturn(&read_packet_cb).Run(message1_->ByteSize()); | |
136 } | |
137 | |
138 // Outgoing MessageProcessor can be set to NULL if no read is pending. | |
139 // This test NULLs the outgoing processor from within ProcessMessage(). | |
140 TEST_F(BlimpMessagePumpTest, NullMessageProcessor) { | |
141 // Set up a ReadPacket expectation to return one message to process. | |
142 net::CompletionCallback read_packet_cb; | |
143 EXPECT_CALL(reader_, ReadPacket(NotNull(), _)) | |
144 .WillOnce(DoAll(FillBufferFromMessage<0>(message1_.get()), | |
145 SaveArg<1>(&read_packet_cb))) | |
146 .RetiresOnSaturation(); | |
147 | |
148 // Set up a ProcessMessage expectation to NULL the outgoing processor. | |
149 net::CompletionCallback process_msg_cb; | |
150 EXPECT_CALL(receiver_, MockableProcessMessage(EqualsProto(*message1_), _)) | |
151 .WillOnce(DoAll( | |
152 InvokeWithoutArgs(this, &BlimpMessagePumpTest::NullMessageProcessor), | |
153 SaveArg<1>(&process_msg_cb))); | |
154 | |
155 // Set the outgoing processor to start the MessagePump. | |
156 message_pump_->SetMessageProcessor(&receiver_); | |
157 ASSERT_FALSE(read_packet_cb.is_null()); | |
158 base::ResetAndReturn(&read_packet_cb).Run(message1_->ByteSize()); | |
159 process_msg_cb.Run(net::OK); | |
160 // Running |process_msg_cb| should NOT trigger another ReadPacket call. | |
161 } | |
162 | |
163 } // namespace | |
164 | |
165 } // namespace blimp | |
OLD | NEW |