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 #include "tools/battor_agent/battor_connection_impl.h" | 5 #include "tools/battor_agent/battor_connection_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
10 #include "device/serial/serial.mojom.h" | 10 #include "device/serial/serial.mojom.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 scoped_refptr<device::SerialIoHandler> GetIoHandler() { return io_handler_; } | 33 scoped_refptr<device::SerialIoHandler> GetIoHandler() { return io_handler_; } |
34 }; | 34 }; |
35 | 35 |
36 // BattOrConnectionImplTest provides a BattOrConnection and captures the | 36 // BattOrConnectionImplTest provides a BattOrConnection and captures the |
37 // results of all its commands. | 37 // results of all its commands. |
38 class BattOrConnectionImplTest : public testing::Test, | 38 class BattOrConnectionImplTest : public testing::Test, |
39 public BattOrConnection::Listener { | 39 public BattOrConnection::Listener { |
40 public: | 40 public: |
41 void OnConnectionOpened(bool success) override { open_success_ = success; }; | 41 void OnConnectionOpened(bool success) override { open_success_ = success; }; |
42 void OnBytesSent(bool success) override { send_success_ = success; } | 42 void OnBytesSent(bool success) override { send_success_ = success; } |
43 void OnBytesRead(bool success, | 43 void OnMessageRead(bool success, |
44 BattOrMessageType type, | 44 BattOrMessageType type, |
45 scoped_ptr<std::vector<char>> bytes) override { | 45 scoped_ptr<std::vector<char>> bytes) override { |
46 is_read_complete_ = true; | 46 is_read_complete_ = true; |
47 read_success_ = success; | 47 read_success_ = success; |
48 read_type_ = type; | 48 read_type_ = type; |
49 read_bytes_ = std::move(bytes); | 49 read_bytes_ = std::move(bytes); |
50 } | 50 } |
51 | 51 |
52 protected: | 52 protected: |
53 void SetUp() override { | 53 void SetUp() override { |
54 connection_.reset(new TestableBattOrConnection(this)); | 54 connection_.reset(new TestableBattOrConnection(this)); |
55 } | 55 } |
56 | 56 |
57 void OpenConnection() { connection_->Open(); } | 57 void OpenConnection() { connection_->Open(); } |
58 | 58 |
59 void ReadBytes(uint16_t bytes_to_read) { | 59 void ReadMessage(BattOrMessageType type) { |
60 is_read_complete_ = false; | 60 is_read_complete_ = false; |
61 connection_->ReadBytes(bytes_to_read); | 61 connection_->ReadMessage(type); |
62 } | 62 } |
63 | 63 |
64 // Reads the specified number of bytes directly from the serial connection. | 64 // Reads the specified number of bytes directly from the serial connection. |
65 scoped_refptr<net::IOBuffer> ReadBytesRaw(int bytes_to_read) { | 65 scoped_refptr<net::IOBuffer> ReadMessageRaw(int bytes_to_read) { |
66 scoped_refptr<net::IOBuffer> buffer( | 66 scoped_refptr<net::IOBuffer> buffer( |
67 new net::IOBuffer((size_t)bytes_to_read)); | 67 new net::IOBuffer((size_t)bytes_to_read)); |
68 | 68 |
69 connection_->GetIoHandler()->Read(make_scoped_ptr(new device::ReceiveBuffer( | 69 connection_->GetIoHandler()->Read(make_scoped_ptr(new device::ReceiveBuffer( |
70 buffer, bytes_to_read, base::Bind(&NullReadCallback)))); | 70 buffer, bytes_to_read, base::Bind(&NullReadCallback)))); |
71 | 71 |
72 return buffer; | 72 return buffer; |
73 } | 73 } |
74 | 74 |
75 void SendControlMessage(BattOrControlMessageType type, | 75 void SendControlMessage(BattOrControlMessageType type, |
76 uint16_t param1, | 76 uint16_t param1, |
77 uint16_t param2) { | 77 uint16_t param2) { |
78 BattOrControlMessage msg{type, param1, param2}; | 78 BattOrControlMessage msg{type, param1, param2}; |
79 connection_->SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, | 79 connection_->SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, |
80 reinterpret_cast<char*>(&msg), sizeof(msg)); | 80 reinterpret_cast<char*>(&msg), sizeof(msg)); |
81 } | 81 } |
82 | 82 |
83 // Writes the specified bytes directly to the serial connection. | 83 // Writes the specified bytes directly to the serial connection. |
84 void SendBytesRaw(const char* data, uint16_t bytes_to_send) { | 84 void SendBytesRaw(const char* data, uint16_t bytes_to_send) { |
85 std::vector<char> data_vector(data, data + bytes_to_send); | 85 std::vector<char> data_vector(data, data + bytes_to_send); |
86 connection_->GetIoHandler()->Write(make_scoped_ptr( | 86 connection_->GetIoHandler()->Write(make_scoped_ptr( |
87 new device::SendBuffer(data_vector, base::Bind(&NullWriteCallback)))); | 87 new device::SendBuffer(data_vector, base::Bind(&NullWriteCallback)))); |
88 } | 88 } |
89 | 89 |
90 bool GetOpenSuccess() { return open_success_; } | 90 bool GetOpenSuccess() { return open_success_; } |
91 bool GetSendSuccess() { return send_success_; } | 91 bool GetSendSuccess() { return send_success_; } |
92 bool IsReadComplete() { return is_read_complete_; } | 92 bool IsReadComplete() { return is_read_complete_; } |
93 bool GetReadSuccess() { return read_success_; } | 93 bool GetReadSuccess() { return read_success_; } |
94 BattOrMessageType GetReadType() { return read_type_; } | 94 BattOrMessageType GetReadType() { return read_type_; } |
95 std::vector<char>* GetReadBytes() { return read_bytes_.get(); } | 95 std::vector<char>* GetReadMessage() { return read_bytes_.get(); } |
96 | 96 |
97 private: | 97 private: |
98 scoped_ptr<TestableBattOrConnection> connection_; | 98 scoped_ptr<TestableBattOrConnection> connection_; |
99 | 99 |
100 // Result from the last connect command. | 100 // Result from the last connect command. |
101 bool open_success_; | 101 bool open_success_; |
102 // Result from the last send command. | 102 // Result from the last send command. |
103 bool send_success_; | 103 bool send_success_; |
104 // Results from the last read command. | 104 // Results from the last read command. |
105 bool is_read_complete_; | 105 bool is_read_complete_; |
(...skipping 12 matching lines...) Expand all Loading... |
118 BATTOR_CONTROL_BYTE_START, BATTOR_MESSAGE_TYPE_CONTROL, | 118 BATTOR_CONTROL_BYTE_START, BATTOR_MESSAGE_TYPE_CONTROL, |
119 BATTOR_CONTROL_BYTE_ESCAPE, BATTOR_CONTROL_MESSAGE_TYPE_INIT, | 119 BATTOR_CONTROL_BYTE_ESCAPE, BATTOR_CONTROL_MESSAGE_TYPE_INIT, |
120 BATTOR_CONTROL_BYTE_ESCAPE, 0x00, | 120 BATTOR_CONTROL_BYTE_ESCAPE, 0x00, |
121 BATTOR_CONTROL_BYTE_ESCAPE, 0x00, | 121 BATTOR_CONTROL_BYTE_ESCAPE, 0x00, |
122 BATTOR_CONTROL_BYTE_ESCAPE, 0x00, | 122 BATTOR_CONTROL_BYTE_ESCAPE, 0x00, |
123 BATTOR_CONTROL_BYTE_ESCAPE, 0x00, | 123 BATTOR_CONTROL_BYTE_ESCAPE, 0x00, |
124 BATTOR_CONTROL_BYTE_END, | 124 BATTOR_CONTROL_BYTE_END, |
125 }; | 125 }; |
126 | 126 |
127 ASSERT_TRUE(GetSendSuccess()); | 127 ASSERT_TRUE(GetSendSuccess()); |
128 ASSERT_EQ(0, std::memcmp(ReadBytesRaw(13)->data(), expected_data, 13)); | 128 ASSERT_EQ(0, std::memcmp(ReadMessageRaw(13)->data(), expected_data, 13)); |
129 } | 129 } |
130 | 130 |
131 TEST_F(BattOrConnectionImplTest, ResetSendsCorrectBytes) { | 131 TEST_F(BattOrConnectionImplTest, ResetSendsCorrectBytes) { |
132 OpenConnection(); | 132 OpenConnection(); |
133 ASSERT_TRUE(GetOpenSuccess()); | 133 ASSERT_TRUE(GetOpenSuccess()); |
134 | 134 |
135 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0, 0); | 135 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0, 0); |
136 | 136 |
137 const char expected_data[] = { | 137 const char expected_data[] = { |
138 BATTOR_CONTROL_BYTE_START, | 138 BATTOR_CONTROL_BYTE_START, |
139 BATTOR_MESSAGE_TYPE_CONTROL, | 139 BATTOR_MESSAGE_TYPE_CONTROL, |
140 BATTOR_CONTROL_MESSAGE_TYPE_RESET, | 140 BATTOR_CONTROL_MESSAGE_TYPE_RESET, |
141 BATTOR_CONTROL_BYTE_ESCAPE, | 141 BATTOR_CONTROL_BYTE_ESCAPE, |
142 0x00, | 142 0x00, |
143 BATTOR_CONTROL_BYTE_ESCAPE, | 143 BATTOR_CONTROL_BYTE_ESCAPE, |
144 0x00, | 144 0x00, |
145 BATTOR_CONTROL_BYTE_ESCAPE, | 145 BATTOR_CONTROL_BYTE_ESCAPE, |
146 0x00, | 146 0x00, |
147 BATTOR_CONTROL_BYTE_ESCAPE, | 147 BATTOR_CONTROL_BYTE_ESCAPE, |
148 0x00, | 148 0x00, |
149 BATTOR_CONTROL_BYTE_END, | 149 BATTOR_CONTROL_BYTE_END, |
150 }; | 150 }; |
151 | 151 |
152 ASSERT_TRUE(GetSendSuccess()); | 152 ASSERT_TRUE(GetSendSuccess()); |
153 ASSERT_EQ(0, std::memcmp(ReadBytesRaw(12)->data(), expected_data, 12)); | 153 ASSERT_EQ(0, std::memcmp(ReadMessageRaw(12)->data(), expected_data, 12)); |
154 } | 154 } |
155 | 155 |
156 TEST_F(BattOrConnectionImplTest, ReadBytesControlMessage) { | 156 TEST_F(BattOrConnectionImplTest, ReadMessageControlMessage) { |
157 OpenConnection(); | 157 OpenConnection(); |
158 ASSERT_TRUE(GetOpenSuccess()); | 158 ASSERT_TRUE(GetOpenSuccess()); |
159 | 159 |
160 const char data[] = { | 160 const char data[] = { |
161 BATTOR_CONTROL_BYTE_START, | 161 BATTOR_CONTROL_BYTE_START, |
162 BATTOR_MESSAGE_TYPE_CONTROL, | 162 BATTOR_MESSAGE_TYPE_CONTROL, |
163 BATTOR_CONTROL_MESSAGE_TYPE_RESET, | 163 BATTOR_CONTROL_MESSAGE_TYPE_RESET, |
164 0x04, | 164 0x04, |
165 0x04, | 165 0x04, |
166 0x04, | 166 0x04, |
167 0x04, | 167 0x04, |
168 BATTOR_CONTROL_BYTE_END, | 168 BATTOR_CONTROL_BYTE_END, |
169 }; | 169 }; |
170 SendBytesRaw(data, 8); | 170 SendBytesRaw(data, 8); |
171 ReadBytes(5); | 171 ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL); |
172 | 172 |
173 const char expected[] = {BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0x04, 0x04, 0x04, | 173 const char expected[] = {BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0x04, 0x04, 0x04, |
174 0x04}; | 174 0x04}; |
175 | 175 |
176 ASSERT_TRUE(IsReadComplete()); | 176 ASSERT_TRUE(IsReadComplete()); |
177 ASSERT_TRUE(GetReadSuccess()); | 177 ASSERT_TRUE(GetReadSuccess()); |
178 ASSERT_EQ(BATTOR_MESSAGE_TYPE_CONTROL, GetReadType()); | 178 ASSERT_EQ(BATTOR_MESSAGE_TYPE_CONTROL, GetReadType()); |
179 ASSERT_EQ(0, std::memcmp(GetReadBytes()->data(), expected, 5)); | 179 ASSERT_EQ(0, std::memcmp(GetReadMessage()->data(), expected, 5)); |
180 } | 180 } |
181 | 181 |
182 TEST_F(BattOrConnectionImplTest, ReadBytesNotEnoughBytes) { | 182 TEST_F(BattOrConnectionImplTest, ReadMessageInvalidType) { |
183 OpenConnection(); | |
184 ASSERT_TRUE(GetOpenSuccess()); | |
185 | |
186 // 3 (h/f), 1 (control message type), 4 (data) | |
187 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0, 0); | |
188 ReadBytes(sizeof(BattOrControlMessage) + 1); | |
189 | |
190 ASSERT_FALSE(IsReadComplete()); | |
191 } | |
192 | |
193 TEST_F(BattOrConnectionImplTest, ReadBytesInvalidType) { | |
194 OpenConnection(); | 183 OpenConnection(); |
195 ASSERT_TRUE(GetOpenSuccess()); | 184 ASSERT_TRUE(GetOpenSuccess()); |
196 | 185 |
197 const char data[] = { | 186 const char data[] = { |
198 BATTOR_CONTROL_BYTE_START, | 187 BATTOR_CONTROL_BYTE_START, |
199 UINT8_MAX, | 188 UINT8_MAX, |
200 BATTOR_CONTROL_MESSAGE_TYPE_INIT, | 189 BATTOR_CONTROL_MESSAGE_TYPE_RESET, |
| 190 0x04, |
| 191 0x04, |
201 0x04, | 192 0x04, |
202 0x04, | 193 0x04, |
203 BATTOR_CONTROL_BYTE_END, | 194 BATTOR_CONTROL_BYTE_END, |
204 }; | 195 }; |
205 SendBytesRaw(data, 8); | 196 SendBytesRaw(data, 6); |
206 | 197 ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL); |
207 ReadBytes(3); | |
208 | 198 |
209 ASSERT_TRUE(IsReadComplete()); | 199 ASSERT_TRUE(IsReadComplete()); |
210 ASSERT_FALSE(GetReadSuccess()); | 200 ASSERT_FALSE(GetReadSuccess()); |
211 } | 201 } |
212 | 202 |
213 TEST_F(BattOrConnectionImplTest, ReadBytesWithEscapeCharacters) { | 203 TEST_F(BattOrConnectionImplTest, ReadMessageEndsMidMessageByte) { |
| 204 OpenConnection(); |
| 205 ASSERT_TRUE(GetOpenSuccess()); |
| 206 |
| 207 const char data[] = { |
| 208 BATTOR_CONTROL_BYTE_START, BATTOR_MESSAGE_TYPE_CONTROL, |
| 209 BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0x04, |
| 210 }; |
| 211 SendBytesRaw(data, 4); |
| 212 ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL); |
| 213 |
| 214 ASSERT_TRUE(IsReadComplete()); |
| 215 ASSERT_FALSE(GetReadSuccess()); |
| 216 } |
| 217 |
| 218 TEST_F(BattOrConnectionImplTest, ReadMessageMissingEndByte) { |
214 OpenConnection(); | 219 OpenConnection(); |
215 ASSERT_TRUE(GetOpenSuccess()); | 220 ASSERT_TRUE(GetOpenSuccess()); |
216 | 221 |
217 const char data[] = { | 222 const char data[] = { |
218 BATTOR_CONTROL_BYTE_START, | 223 BATTOR_CONTROL_BYTE_START, |
219 BATTOR_MESSAGE_TYPE_CONTROL_ACK, | 224 BATTOR_MESSAGE_TYPE_CONTROL, |
| 225 BATTOR_CONTROL_MESSAGE_TYPE_RESET, |
| 226 0x04, |
| 227 0x04, |
| 228 0x04, |
| 229 0x04, |
| 230 }; |
| 231 SendBytesRaw(data, 5); |
| 232 ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL); |
| 233 |
| 234 ASSERT_TRUE(IsReadComplete()); |
| 235 ASSERT_FALSE(GetReadSuccess()); |
| 236 } |
| 237 |
| 238 TEST_F(BattOrConnectionImplTest, ReadMessageWithEscapeCharacters) { |
| 239 OpenConnection(); |
| 240 ASSERT_TRUE(GetOpenSuccess()); |
| 241 |
| 242 const char data[] = { |
| 243 BATTOR_CONTROL_BYTE_START, |
| 244 BATTOR_MESSAGE_TYPE_CONTROL, |
220 BATTOR_CONTROL_MESSAGE_TYPE_RESET, | 245 BATTOR_CONTROL_MESSAGE_TYPE_RESET, |
221 BATTOR_CONTROL_BYTE_ESCAPE, | 246 BATTOR_CONTROL_BYTE_ESCAPE, |
222 0x00, | 247 0x00, |
| 248 0x04, |
| 249 0x04, |
| 250 0x04, |
223 BATTOR_CONTROL_BYTE_END, | 251 BATTOR_CONTROL_BYTE_END, |
224 }; | 252 }; |
225 SendBytesRaw(data, 6); | 253 SendBytesRaw(data, 9); |
226 | 254 ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL); |
227 ReadBytes(2); | |
228 | 255 |
229 const char expected[] = {BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0x00}; | 256 const char expected[] = {BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0x00}; |
230 | 257 |
231 ASSERT_TRUE(IsReadComplete()); | 258 ASSERT_TRUE(IsReadComplete()); |
232 ASSERT_TRUE(GetReadSuccess()); | 259 ASSERT_TRUE(GetReadSuccess()); |
233 ASSERT_EQ(BATTOR_MESSAGE_TYPE_CONTROL_ACK, GetReadType()); | 260 ASSERT_EQ(BATTOR_MESSAGE_TYPE_CONTROL, GetReadType()); |
234 ASSERT_EQ(0, std::memcmp(GetReadBytes()->data(), expected, 2)); | 261 ASSERT_EQ(0, std::memcmp(GetReadMessage()->data(), expected, 2)); |
235 } | |
236 | |
237 TEST_F(BattOrConnectionImplTest, | |
238 ReadBytesWithEscapeCharactersInSubsequentReads) { | |
239 OpenConnection(); | |
240 ASSERT_TRUE(GetOpenSuccess()); | |
241 | |
242 // The first read should request 7 bytes. Of those 7 bytes, though, 2 of them | |
243 // are escape bytes, so we'll then do a second read of 2 bytes. In that second | |
244 // read, we'll see another escape byte, so we'll have to do a third read of 1 | |
245 // byte. That third read should complete the message. | |
246 const char data[] = { | |
247 // These bytes make up the first read. | |
248 BATTOR_CONTROL_BYTE_START, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | |
249 BATTOR_CONTROL_MESSAGE_TYPE_RESET, BATTOR_CONTROL_BYTE_ESCAPE, 0x00, | |
250 BATTOR_CONTROL_BYTE_ESCAPE, 0x00, | |
251 // These bytes make up the second read. | |
252 BATTOR_CONTROL_BYTE_ESCAPE, 0x00, | |
253 // This byte makes up the third read. | |
254 BATTOR_CONTROL_BYTE_END, | |
255 }; | |
256 SendBytesRaw(data, 10); | |
257 | |
258 ReadBytes(4); | |
259 | |
260 const char expected[] = {BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0x00, 0x00, 0x00}; | |
261 | |
262 ASSERT_TRUE(IsReadComplete()); | |
263 ASSERT_TRUE(GetReadSuccess()); | |
264 ASSERT_EQ(BATTOR_MESSAGE_TYPE_CONTROL_ACK, GetReadType()); | |
265 ASSERT_EQ(0, std::memcmp(GetReadBytes()->data(), expected, 4)); | |
266 } | 262 } |
267 | 263 |
268 TEST_F(BattOrConnectionImplTest, ReadControlMessage) { | 264 TEST_F(BattOrConnectionImplTest, ReadControlMessage) { |
269 OpenConnection(); | 265 OpenConnection(); |
270 ASSERT_TRUE(GetOpenSuccess()); | 266 ASSERT_TRUE(GetOpenSuccess()); |
271 | 267 |
272 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_RESET, 4, 7); | 268 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_RESET, 4, 7); |
273 ReadBytes(sizeof(BattOrControlMessage)); | 269 ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL); |
274 | 270 |
275 ASSERT_TRUE(IsReadComplete()); | 271 ASSERT_TRUE(IsReadComplete()); |
276 ASSERT_TRUE(GetReadSuccess()); | 272 ASSERT_TRUE(GetReadSuccess()); |
277 ASSERT_EQ(BATTOR_MESSAGE_TYPE_CONTROL, GetReadType()); | 273 ASSERT_EQ(BATTOR_MESSAGE_TYPE_CONTROL, GetReadType()); |
278 | 274 |
279 BattOrControlMessage* msg = | 275 BattOrControlMessage* msg = |
280 reinterpret_cast<BattOrControlMessage*>(GetReadBytes()->data()); | 276 reinterpret_cast<BattOrControlMessage*>(GetReadMessage()->data()); |
281 | 277 |
282 ASSERT_EQ(BATTOR_CONTROL_MESSAGE_TYPE_RESET, msg->type); | 278 ASSERT_EQ(BATTOR_CONTROL_MESSAGE_TYPE_RESET, msg->type); |
283 ASSERT_EQ(4, msg->param1); | 279 ASSERT_EQ(4, msg->param1); |
284 ASSERT_EQ(7, msg->param2); | 280 ASSERT_EQ(7, msg->param2); |
285 } | 281 } |
286 | 282 |
| 283 TEST_F(BattOrConnectionImplTest, ReadMessageExtraBytesStoredBetweenReads) { |
| 284 OpenConnection(); |
| 285 ASSERT_TRUE(GetOpenSuccess()); |
| 286 |
| 287 // Send a samples frame with length and sequence number of zero. |
| 288 const char data[] = { |
| 289 BATTOR_CONTROL_BYTE_START, |
| 290 BATTOR_MESSAGE_TYPE_SAMPLES, |
| 291 0x02, |
| 292 0x00, |
| 293 0x02, |
| 294 0x00, |
| 295 0x02, |
| 296 0x00, |
| 297 BATTOR_CONTROL_BYTE_END, |
| 298 }; |
| 299 SendBytesRaw(data, 9); |
| 300 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_INIT, 5, 8); |
| 301 |
| 302 // When reading sample frames, we're forced to read lots because each frame |
| 303 // could be up to 50kB long. By reading a really short sample frame (like the |
| 304 // zero-length one above), the BattOrConnection is forced to store whatever |
| 305 // extra data it finds in the serial stream - in this case, the init control |
| 306 // message that we sent. |
| 307 ReadMessage(BATTOR_MESSAGE_TYPE_SAMPLES); |
| 308 |
| 309 ASSERT_TRUE(IsReadComplete()); |
| 310 ASSERT_TRUE(GetReadSuccess()); |
| 311 ASSERT_EQ(BATTOR_MESSAGE_TYPE_SAMPLES, GetReadType()); |
| 312 |
| 313 ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL); |
| 314 |
| 315 ASSERT_TRUE(IsReadComplete()); |
| 316 ASSERT_TRUE(GetReadSuccess()); |
| 317 ASSERT_EQ(BATTOR_MESSAGE_TYPE_CONTROL, GetReadType()); |
| 318 |
| 319 BattOrControlMessage* init_msg = |
| 320 reinterpret_cast<BattOrControlMessage*>(GetReadMessage()->data()); |
| 321 |
| 322 ASSERT_EQ(BATTOR_CONTROL_MESSAGE_TYPE_INIT, init_msg->type); |
| 323 ASSERT_EQ(5, init_msg->param1); |
| 324 ASSERT_EQ(8, init_msg->param2); |
| 325 } |
| 326 |
| 327 TEST_F(BattOrConnectionImplTest, ReadMessageFailsWithControlButExpectingAck) { |
| 328 OpenConnection(); |
| 329 ASSERT_TRUE(GetOpenSuccess()); |
| 330 |
| 331 const char data[] = { |
| 332 BATTOR_CONTROL_BYTE_START, BATTOR_MESSAGE_TYPE_CONTROL_ACK, |
| 333 BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0x04, |
| 334 BATTOR_CONTROL_BYTE_END, |
| 335 }; |
| 336 SendBytesRaw(data, 5); |
| 337 ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL); |
| 338 |
| 339 ASSERT_TRUE(IsReadComplete()); |
| 340 ASSERT_FALSE(GetReadSuccess()); |
| 341 } |
| 342 |
| 343 TEST_F(BattOrConnectionImplTest, ReadMessageFailsWithAckButExpectingControl) { |
| 344 OpenConnection(); |
| 345 ASSERT_TRUE(GetOpenSuccess()); |
| 346 |
| 347 const char data[] = { |
| 348 BATTOR_CONTROL_BYTE_START, BATTOR_MESSAGE_TYPE_CONTROL_ACK, |
| 349 BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0x04, |
| 350 BATTOR_CONTROL_BYTE_END, |
| 351 }; |
| 352 SendBytesRaw(data, 5); |
| 353 ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL); |
| 354 |
| 355 ASSERT_TRUE(IsReadComplete()); |
| 356 ASSERT_FALSE(GetReadSuccess()); |
| 357 } |
| 358 |
| 359 TEST_F(BattOrConnectionImplTest, ReadMessageControlTypePrintFails) { |
| 360 OpenConnection(); |
| 361 ASSERT_TRUE(GetOpenSuccess()); |
| 362 |
| 363 const char data[] = { |
| 364 BATTOR_CONTROL_BYTE_START, BATTOR_MESSAGE_TYPE_PRINT, |
| 365 BATTOR_CONTROL_BYTE_END, |
| 366 }; |
| 367 SendBytesRaw(data, 3); |
| 368 ReadMessage(BATTOR_MESSAGE_TYPE_PRINT); |
| 369 |
| 370 ASSERT_TRUE(IsReadComplete()); |
| 371 ASSERT_FALSE(GetReadSuccess()); |
| 372 } |
| 373 |
287 } // namespace battor | 374 } // namespace battor |
OLD | NEW |