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 #ifndef BLIMP_NET_TEST_COMMON_H_ | |
6 #define BLIMP_NET_TEST_COMMON_H_ | |
7 | |
8 #include <stddef.h> | |
9 #include <stdint.h> | |
10 | |
11 #include <memory> | |
12 #include <string> | |
13 | |
14 #include "blimp/common/proto/blimp_message.pb.h" | |
15 #include "blimp/net/blimp_connection.h" | |
16 #include "blimp/net/blimp_message_processor.h" | |
17 #include "blimp/net/blimp_transport.h" | |
18 #include "blimp/net/connection_error_observer.h" | |
19 #include "blimp/net/connection_handler.h" | |
20 #include "blimp/net/message_port.h" | |
21 #include "blimp/net/packet_reader.h" | |
22 #include "blimp/net/packet_writer.h" | |
23 #include "net/log/net_log_with_source.h" | |
24 #include "net/socket/stream_socket.h" | |
25 #include "testing/gmock/include/gmock/gmock.h" | |
26 | |
27 namespace net { | |
28 class GrowableIOBuffer; | |
29 } // namespace net | |
30 | |
31 namespace blimp { | |
32 | |
33 // Checks if the contents of a buffer are an exact match with std::string. | |
34 // Using this matcher for inequality checks will result in undefined behavior, | |
35 // due to IOBuffer's lack of a size field. | |
36 // | |
37 // arg (type: IOBuffer*) The buffer to check. | |
38 // data (type: std::string) The string to compare with |arg|. | |
39 MATCHER_P(BufferEquals, expected, "") { | |
40 return expected == std::string(arg->data(), expected.size()); | |
41 } | |
42 | |
43 // Checks if two proto messages are the same. | |
44 MATCHER_P(EqualsProto, message, "") { | |
45 std::string expected_serialized; | |
46 std::string actual_serialized; | |
47 message.SerializeToString(&expected_serialized); | |
48 arg.SerializeToString(&actual_serialized); | |
49 return expected_serialized == actual_serialized; | |
50 } | |
51 | |
52 // Checks if the contents of a buffer are an exact match with BlimpMessage. | |
53 // arg (type: net::DrainableIOBuffer*) The buffer to check. | |
54 // message (type: BlimpMessage) The message to compare with |arg|. | |
55 MATCHER_P(BufferEqualsProto, message, "") { | |
56 BlimpMessage actual_message; | |
57 actual_message.ParseFromArray(arg->data(), message.ByteSize()); | |
58 std::string expected_serialized; | |
59 std::string actual_serialized; | |
60 message.SerializeToString(&expected_serialized); | |
61 actual_message.SerializeToString(&actual_serialized); | |
62 return expected_serialized == actual_serialized; | |
63 } | |
64 | |
65 // Checks if the contents of a BlobDataPtr match the string |expected|. | |
66 MATCHER_P(BlobDataPtrEqualsString, expected, "") { | |
67 return expected == arg->data; | |
68 } | |
69 | |
70 // GMock action that writes data from a string to an IOBuffer. | |
71 // | |
72 // buf_idx (template parameter 0): 0-based index of the IOBuffer arg. | |
73 // str: the string containing data to be written to the IOBuffer. | |
74 ACTION_TEMPLATE(FillBufferFromString, | |
75 HAS_1_TEMPLATE_PARAMS(int, buf_idx), | |
76 AND_1_VALUE_PARAMS(str)) { | |
77 memcpy(testing::get<buf_idx>(args)->data(), str.data(), str.size()); | |
78 } | |
79 | |
80 // Returns true if |buf| is prefixed by |str|. | |
81 bool BufferStartsWith(net::GrowableIOBuffer* buf, | |
82 size_t buf_size, | |
83 const std::string& str); | |
84 | |
85 // GMock action that writes data from a BlimpMessage to a GrowableIOBuffer. | |
86 // Advances the buffer's |offset| to the end of the message. | |
87 // | |
88 // buf_idx (template parameter 0): 0-based index of the IOBuffer arg. | |
89 // message: the blimp message containing data to be written to the IOBuffer | |
90 ACTION_TEMPLATE(FillBufferFromMessage, | |
91 HAS_1_TEMPLATE_PARAMS(int, buf_idx), | |
92 AND_1_VALUE_PARAMS(message)) { | |
93 message->SerializeToArray(testing::get<buf_idx>(args)->data(), | |
94 message->ByteSize()); | |
95 } | |
96 | |
97 // Calls |set_offset()| for a GrowableIOBuffer. | |
98 ACTION_TEMPLATE(SetBufferOffset, | |
99 HAS_1_TEMPLATE_PARAMS(int, buf_idx), | |
100 AND_1_VALUE_PARAMS(offset)) { | |
101 testing::get<buf_idx>(args)->set_offset(offset); | |
102 } | |
103 | |
104 // Formats a string-based representation of a BlimpMessage header. | |
105 std::string EncodeHeader(size_t size); | |
106 | |
107 class MockStreamSocket : public net::StreamSocket { | |
108 public: | |
109 MockStreamSocket(); | |
110 virtual ~MockStreamSocket(); | |
111 | |
112 MOCK_METHOD3(Read, int(net::IOBuffer*, int, const net::CompletionCallback&)); | |
113 MOCK_METHOD3(Write, int(net::IOBuffer*, int, const net::CompletionCallback&)); | |
114 MOCK_METHOD1(SetReceiveBufferSize, int(int32_t)); | |
115 MOCK_METHOD1(SetSendBufferSize, int(int32_t)); | |
116 MOCK_METHOD1(Connect, int(const net::CompletionCallback&)); | |
117 MOCK_METHOD0(Disconnect, void()); | |
118 MOCK_CONST_METHOD0(IsConnected, bool()); | |
119 MOCK_CONST_METHOD0(IsConnectedAndIdle, bool()); | |
120 MOCK_CONST_METHOD1(GetPeerAddress, int(net::IPEndPoint*)); | |
121 MOCK_CONST_METHOD1(GetLocalAddress, int(net::IPEndPoint*)); | |
122 MOCK_CONST_METHOD0(NetLog, const net::NetLogWithSource&()); | |
123 MOCK_METHOD0(SetSubresourceSpeculation, void()); | |
124 MOCK_METHOD0(SetOmniboxSpeculation, void()); | |
125 MOCK_CONST_METHOD0(WasEverUsed, bool()); | |
126 MOCK_CONST_METHOD0(UsingTCPFastOpen, bool()); | |
127 MOCK_CONST_METHOD0(NumBytesRead, int64_t()); | |
128 MOCK_CONST_METHOD0(GetConnectTimeMicros, base::TimeDelta()); | |
129 MOCK_CONST_METHOD0(WasAlpnNegotiated, bool()); | |
130 MOCK_CONST_METHOD0(GetNegotiatedProtocol, net::NextProto()); | |
131 MOCK_METHOD1(GetSSLInfo, bool(net::SSLInfo*)); | |
132 MOCK_CONST_METHOD1(GetConnectionAttempts, void(net::ConnectionAttempts*)); | |
133 MOCK_METHOD0(ClearConnectionAttempts, void()); | |
134 MOCK_METHOD1(AddConnectionAttempts, void(const net::ConnectionAttempts&)); | |
135 MOCK_CONST_METHOD0(GetTotalReceivedBytes, int64_t()); | |
136 }; | |
137 | |
138 class MockBlimpConnection; | |
139 | |
140 class MockTransport : public BlimpTransport { | |
141 public: | |
142 MockTransport(); | |
143 ~MockTransport() override; | |
144 | |
145 MOCK_METHOD1(Connect, void(const net::CompletionCallback& callback)); | |
146 MOCK_METHOD0(TakeMessagePortPtr, MessagePort*()); | |
147 std::unique_ptr<BlimpConnection> MakeConnection() override; | |
148 | |
149 const char* GetName() const override; | |
150 | |
151 void SetMockConnection(std::unique_ptr<MockBlimpConnection> connection); | |
152 | |
153 private: | |
154 std::unique_ptr<MockBlimpConnection> connection_; | |
155 }; | |
156 | |
157 class MockConnectionHandler : public ConnectionHandler { | |
158 public: | |
159 MockConnectionHandler(); | |
160 ~MockConnectionHandler() override; | |
161 | |
162 MOCK_METHOD1(HandleConnectionPtr, void(BlimpConnection* connection)); | |
163 void HandleConnection(std::unique_ptr<BlimpConnection> connection) override; | |
164 }; | |
165 | |
166 class MockPacketReader : public PacketReader { | |
167 public: | |
168 MockPacketReader(); | |
169 ~MockPacketReader() override; | |
170 | |
171 MOCK_METHOD2(ReadPacket, | |
172 void(const scoped_refptr<net::GrowableIOBuffer>&, | |
173 const net::CompletionCallback&)); | |
174 }; | |
175 | |
176 class MockPacketWriter : public PacketWriter { | |
177 public: | |
178 MockPacketWriter(); | |
179 ~MockPacketWriter() override; | |
180 | |
181 MOCK_METHOD2(WritePacket, | |
182 void(const scoped_refptr<net::DrainableIOBuffer>&, | |
183 const net::CompletionCallback&)); | |
184 }; | |
185 | |
186 class MockBlimpConnection : public BlimpConnection { | |
187 public: | |
188 MockBlimpConnection(); | |
189 ~MockBlimpConnection() override; | |
190 | |
191 MOCK_METHOD1(SetConnectionErrorObserver, | |
192 void(ConnectionErrorObserver* observer)); | |
193 | |
194 MOCK_METHOD1(SetIncomingMessageProcessor, | |
195 void(BlimpMessageProcessor* processor)); | |
196 | |
197 MOCK_METHOD1(AddConnectionErrorObserver, void(ConnectionErrorObserver*)); | |
198 | |
199 MOCK_METHOD1(RemoveConnectionErrorObserver, void(ConnectionErrorObserver*)); | |
200 | |
201 MOCK_METHOD0(GetOutgoingMessageProcessor, BlimpMessageProcessor*(void)); | |
202 }; | |
203 | |
204 class MockConnectionErrorObserver : public ConnectionErrorObserver { | |
205 public: | |
206 MockConnectionErrorObserver(); | |
207 ~MockConnectionErrorObserver() override; | |
208 | |
209 MOCK_METHOD1(OnConnectionError, void(int error)); | |
210 }; | |
211 | |
212 class MockBlimpMessageProcessor : public BlimpMessageProcessor { | |
213 public: | |
214 MockBlimpMessageProcessor(); | |
215 | |
216 ~MockBlimpMessageProcessor() override; | |
217 | |
218 // Adapts calls from ProcessMessage to MockableProcessMessage by | |
219 // unboxing the |message| std::unique_ptr for GMock compatibility. | |
220 void ProcessMessage(std::unique_ptr<BlimpMessage> message, | |
221 const net::CompletionCallback& callback) override; | |
222 | |
223 MOCK_METHOD2(MockableProcessMessage, | |
224 void(const BlimpMessage& message, | |
225 const net::CompletionCallback& callback)); | |
226 }; | |
227 | |
228 } // namespace blimp | |
229 | |
230 #endif // BLIMP_NET_TEST_COMMON_H_ | |
OLD | NEW |