OLD | NEW |
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 #ifndef BLIMP_NET_TEST_COMMON_H_ | 5 #ifndef BLIMP_NET_TEST_COMMON_H_ |
6 #define BLIMP_NET_TEST_COMMON_H_ | 6 #define BLIMP_NET_TEST_COMMON_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 // Using this matcher for inequality checks will result in undefined behavior, | 28 // Using this matcher for inequality checks will result in undefined behavior, |
29 // due to IOBuffer's lack of a size field. | 29 // due to IOBuffer's lack of a size field. |
30 // | 30 // |
31 // arg (type: IOBuffer*) The buffer to check. | 31 // arg (type: IOBuffer*) The buffer to check. |
32 // data (type: std::string) The string to compare with |arg|. | 32 // data (type: std::string) The string to compare with |arg|. |
33 MATCHER_P(BufferEquals, expected, "") { | 33 MATCHER_P(BufferEquals, expected, "") { |
34 return expected == std::string(arg->data(), expected.size()); | 34 return expected == std::string(arg->data(), expected.size()); |
35 } | 35 } |
36 | 36 |
37 // Checks if two proto messages are the same. | 37 // Checks if two proto messages are the same. |
38 // TODO(kmarshall): promote to a shared testing library. | |
39 MATCHER_P(EqualsProto, message, "") { | 38 MATCHER_P(EqualsProto, message, "") { |
40 std::string expected_serialized; | 39 std::string expected_serialized; |
41 std::string actual_serialized; | 40 std::string actual_serialized; |
42 message.SerializeToString(&expected_serialized); | 41 message.SerializeToString(&expected_serialized); |
43 arg.SerializeToString(&actual_serialized); | 42 arg.SerializeToString(&actual_serialized); |
44 return expected_serialized == actual_serialized; | 43 return expected_serialized == actual_serialized; |
45 } | 44 } |
46 | 45 |
47 // Checks if the contents of a buffer are an exact match with BlimpMessage. | 46 // Checks if the contents of a buffer are an exact match with BlimpMessage. |
48 // arg (type: net::DrainableIOBuffer*) The buffer to check. | 47 // arg (type: net::DrainableIOBuffer*) The buffer to check. |
49 // message (type: BlimpMessage) The message to compare with |arg|. | 48 // message (type: BlimpMessage) The message to compare with |arg|. |
50 MATCHER_P(BufferEqualsProto, message, "") { | 49 MATCHER_P(BufferEqualsProto, message, "") { |
51 BlimpMessage actual_message; | 50 BlimpMessage actual_message; |
52 actual_message.ParseFromArray(arg->data(), arg->BytesRemaining()); | 51 actual_message.ParseFromArray(arg->data(), message.ByteSize()); |
53 std::string expected_serialized; | 52 std::string expected_serialized; |
54 std::string actual_serialized; | 53 std::string actual_serialized; |
55 message.SerializeToString(&expected_serialized); | 54 message.SerializeToString(&expected_serialized); |
56 actual_message.SerializeToString(&actual_serialized); | 55 actual_message.SerializeToString(&actual_serialized); |
57 return expected_serialized == actual_serialized; | 56 return expected_serialized == actual_serialized; |
58 } | 57 } |
59 | 58 |
60 // GMock action that writes data from a string to an IOBuffer. | 59 // GMock action that writes data from a string to an IOBuffer. |
61 // | 60 // |
62 // buf_idx (template parameter 0): 0-based index of the IOBuffer arg. | 61 // buf_idx (template parameter 0): 0-based index of the IOBuffer arg. |
63 // str: the string containing data to be written to the IOBuffer. | 62 // str: the string containing data to be written to the IOBuffer. |
64 ACTION_TEMPLATE(FillBufferFromString, | 63 ACTION_TEMPLATE(FillBufferFromString, |
65 HAS_1_TEMPLATE_PARAMS(int, buf_idx), | 64 HAS_1_TEMPLATE_PARAMS(int, buf_idx), |
66 AND_1_VALUE_PARAMS(str)) { | 65 AND_1_VALUE_PARAMS(str)) { |
67 memcpy(testing::get<buf_idx>(args)->data(), str.data(), str.size()); | 66 memcpy(testing::get<buf_idx>(args)->data(), str.data(), str.size()); |
68 } | 67 } |
69 | 68 |
70 // GMock action that writes data from a blimp message to an IOBuffer . | 69 // GMock action that writes data from a BlimpMessage to a GrowableIOBuffer. |
| 70 // Advances the buffer's |offset| to the end of the message. |
71 // | 71 // |
72 // buf_idx (template parameter 0): 0-based index of the IOBuffer arg. | 72 // buf_idx (template parameter 0): 0-based index of the IOBuffer arg. |
73 // message: the blimp message containing data to be written to the IOBuffer | 73 // message: the blimp message containing data to be written to the IOBuffer |
74 ACTION_TEMPLATE(FillBufferFromMessage, | 74 ACTION_TEMPLATE(FillBufferFromMessage, |
75 HAS_1_TEMPLATE_PARAMS(int, buf_idx), | 75 HAS_1_TEMPLATE_PARAMS(int, buf_idx), |
76 AND_1_VALUE_PARAMS(message)) { | 76 AND_1_VALUE_PARAMS(message)) { |
77 message->SerializeToArray(testing::get<buf_idx>(args)->data(), | 77 message->SerializeToArray(testing::get<buf_idx>(args)->data(), |
78 message->ByteSize()); | 78 message->ByteSize()); |
| 79 testing::get<buf_idx>(args)->set_offset(message->ByteSize()); |
| 80 } |
| 81 |
| 82 // Calls |set_offset()| for a GrowableIOBuffer. |
| 83 ACTION_TEMPLATE(SetBufferOffset, |
| 84 HAS_1_TEMPLATE_PARAMS(int, buf_idx), |
| 85 AND_1_VALUE_PARAMS(offset)) { |
| 86 testing::get<buf_idx>(args)->set_offset(offset); |
79 } | 87 } |
80 | 88 |
81 // Formats a string-based representation of a BlimpMessage header. | 89 // Formats a string-based representation of a BlimpMessage header. |
82 std::string EncodeHeader(size_t size); | 90 std::string EncodeHeader(size_t size); |
83 | 91 |
84 class MockStreamSocket : public net::StreamSocket { | 92 class MockStreamSocket : public net::StreamSocket { |
85 public: | 93 public: |
86 MockStreamSocket(); | 94 MockStreamSocket(); |
87 virtual ~MockStreamSocket(); | 95 virtual ~MockStreamSocket(); |
88 | 96 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 MOCK_METHOD1(HandleConnectionPtr, void(BlimpConnection* connection)); | 140 MOCK_METHOD1(HandleConnectionPtr, void(BlimpConnection* connection)); |
133 void HandleConnection(scoped_ptr<BlimpConnection> connection) override; | 141 void HandleConnection(scoped_ptr<BlimpConnection> connection) override; |
134 }; | 142 }; |
135 | 143 |
136 class MockPacketReader : public PacketReader { | 144 class MockPacketReader : public PacketReader { |
137 public: | 145 public: |
138 MockPacketReader(); | 146 MockPacketReader(); |
139 ~MockPacketReader() override; | 147 ~MockPacketReader() override; |
140 | 148 |
141 MOCK_METHOD2(ReadPacket, | 149 MOCK_METHOD2(ReadPacket, |
142 int(const scoped_refptr<net::GrowableIOBuffer>&, | 150 void(const scoped_refptr<net::GrowableIOBuffer>&, |
143 const net::CompletionCallback&)); | 151 const net::CompletionCallback&)); |
144 }; | 152 }; |
145 | 153 |
146 class MockPacketWriter : public PacketWriter { | 154 class MockPacketWriter : public PacketWriter { |
147 public: | 155 public: |
148 MockPacketWriter(); | 156 MockPacketWriter(); |
149 ~MockPacketWriter() override; | 157 ~MockPacketWriter() override; |
150 | 158 |
151 MOCK_METHOD2(WritePacket, | 159 MOCK_METHOD2(WritePacket, |
152 int(scoped_refptr<net::DrainableIOBuffer>, | 160 void(scoped_refptr<net::DrainableIOBuffer>, |
153 const net::CompletionCallback&)); | 161 const net::CompletionCallback&)); |
154 }; | 162 }; |
155 | 163 |
156 class MockConnectionErrorObserver : public ConnectionErrorObserver { | 164 class MockConnectionErrorObserver : public ConnectionErrorObserver { |
157 public: | 165 public: |
158 MockConnectionErrorObserver(); | 166 MockConnectionErrorObserver(); |
159 ~MockConnectionErrorObserver() override; | 167 ~MockConnectionErrorObserver() override; |
160 | 168 |
161 MOCK_METHOD1(OnConnectionError, void(int error)); | 169 MOCK_METHOD1(OnConnectionError, void(int error)); |
162 }; | 170 }; |
163 | 171 |
(...skipping 13 matching lines...) Expand all Loading... |
177 const net::CompletionCallback& callback)); | 185 const net::CompletionCallback& callback)); |
178 }; | 186 }; |
179 | 187 |
180 // Returns true if |buf| has a prefix of |str|. | 188 // Returns true if |buf| has a prefix of |str|. |
181 // Behavior is undefined if len(buf) < len(str). | 189 // Behavior is undefined if len(buf) < len(str). |
182 bool BufferStartsWith(net::GrowableIOBuffer* buf, const std::string& str); | 190 bool BufferStartsWith(net::GrowableIOBuffer* buf, const std::string& str); |
183 | 191 |
184 } // namespace blimp | 192 } // namespace blimp |
185 | 193 |
186 #endif // BLIMP_NET_TEST_COMMON_H_ | 194 #endif // BLIMP_NET_TEST_COMMON_H_ |
OLD | NEW |