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

Side by Side Diff: tools/battor_agent/battor_connection_impl_unittest.cc

Issue 1567683002: Makes the BattOrConnection read messages instead of bytes (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 11 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
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 "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
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(uint16_t bytes_to_read) {
60 is_read_complete_ = false; 60 is_read_complete_ = false;
61 connection_->ReadBytes(bytes_to_read); 61 connection_->ReadMessage(bytes_to_read);
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
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(5);
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,
201 0x04, 190 0x04,
202 0x04, 191 0x04,
203 BATTOR_CONTROL_BYTE_END, 192 BATTOR_CONTROL_BYTE_END,
204 }; 193 };
205 SendBytesRaw(data, 8); 194 SendBytesRaw(data, 6);
206 195
207 ReadBytes(3); 196 ReadMessage(3);
208 197
209 ASSERT_TRUE(IsReadComplete()); 198 ASSERT_TRUE(IsReadComplete());
210 ASSERT_FALSE(GetReadSuccess()); 199 ASSERT_FALSE(GetReadSuccess());
211 } 200 }
212 201
213 TEST_F(BattOrConnectionImplTest, ReadBytesWithEscapeCharacters) { 202 TEST_F(BattOrConnectionImplTest, ReadMessageEndsMidMessageByte) {
214 OpenConnection(); 203 OpenConnection();
215 ASSERT_TRUE(GetOpenSuccess()); 204 ASSERT_TRUE(GetOpenSuccess());
216 205
206 const char data[] = {
207 BATTOR_CONTROL_BYTE_START, BATTOR_MESSAGE_TYPE_CONTROL,
208 BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0x04,
209 };
210 SendBytesRaw(data, 4);
211 ReadMessage(2);
212
213 ASSERT_TRUE(IsReadComplete());
214 ASSERT_FALSE(GetReadSuccess());
215 }
216
217 TEST_F(BattOrConnectionImplTest, ReadMessageMissingEndByte) {
218 OpenConnection();
219 ASSERT_TRUE(GetOpenSuccess());
220
221 const char data[] = {
222 BATTOR_CONTROL_BYTE_START,
223 BATTOR_MESSAGE_TYPE_CONTROL,
224 BATTOR_CONTROL_MESSAGE_TYPE_RESET,
225 0x04,
226 0x04,
227 };
228 SendBytesRaw(data, 5);
229 ReadMessage(3);
230
231 ASSERT_TRUE(IsReadComplete());
232 ASSERT_FALSE(GetReadSuccess());
233 }
234
235 TEST_F(BattOrConnectionImplTest, ReadMessageWithEscapeCharacters) {
236 OpenConnection();
237 ASSERT_TRUE(GetOpenSuccess());
238
217 const char data[] = { 239 const char data[] = {
218 BATTOR_CONTROL_BYTE_START, 240 BATTOR_CONTROL_BYTE_START,
219 BATTOR_MESSAGE_TYPE_CONTROL_ACK, 241 BATTOR_MESSAGE_TYPE_CONTROL_ACK,
220 BATTOR_CONTROL_MESSAGE_TYPE_RESET, 242 BATTOR_CONTROL_MESSAGE_TYPE_RESET,
221 BATTOR_CONTROL_BYTE_ESCAPE, 243 BATTOR_CONTROL_BYTE_ESCAPE,
222 0x00, 244 0x00,
223 BATTOR_CONTROL_BYTE_END, 245 BATTOR_CONTROL_BYTE_END,
224 }; 246 };
225 SendBytesRaw(data, 6); 247 SendBytesRaw(data, 6);
226 248
227 ReadBytes(2); 249 ReadMessage(2);
228 250
229 const char expected[] = {BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0x00}; 251 const char expected[] = {BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0x00};
230 252
231 ASSERT_TRUE(IsReadComplete()); 253 ASSERT_TRUE(IsReadComplete());
232 ASSERT_TRUE(GetReadSuccess()); 254 ASSERT_TRUE(GetReadSuccess());
233 ASSERT_EQ(BATTOR_MESSAGE_TYPE_CONTROL_ACK, GetReadType()); 255 ASSERT_EQ(BATTOR_MESSAGE_TYPE_CONTROL_ACK, GetReadType());
234 ASSERT_EQ(0, std::memcmp(GetReadBytes()->data(), expected, 2)); 256 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 } 257 }
267 258
268 TEST_F(BattOrConnectionImplTest, ReadControlMessage) { 259 TEST_F(BattOrConnectionImplTest, ReadControlMessage) {
269 OpenConnection(); 260 OpenConnection();
270 ASSERT_TRUE(GetOpenSuccess()); 261 ASSERT_TRUE(GetOpenSuccess());
271 262
272 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_RESET, 4, 7); 263 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_RESET, 4, 7);
273 ReadBytes(sizeof(BattOrControlMessage)); 264 ReadMessage(sizeof(BattOrControlMessage));
274 265
275 ASSERT_TRUE(IsReadComplete()); 266 ASSERT_TRUE(IsReadComplete());
276 ASSERT_TRUE(GetReadSuccess()); 267 ASSERT_TRUE(GetReadSuccess());
277 ASSERT_EQ(BATTOR_MESSAGE_TYPE_CONTROL, GetReadType()); 268 ASSERT_EQ(BATTOR_MESSAGE_TYPE_CONTROL, GetReadType());
278 269
279 BattOrControlMessage* msg = 270 BattOrControlMessage* msg =
280 reinterpret_cast<BattOrControlMessage*>(GetReadBytes()->data()); 271 reinterpret_cast<BattOrControlMessage*>(GetReadMessage()->data());
281 272
282 ASSERT_EQ(BATTOR_CONTROL_MESSAGE_TYPE_RESET, msg->type); 273 ASSERT_EQ(BATTOR_CONTROL_MESSAGE_TYPE_RESET, msg->type);
283 ASSERT_EQ(4, msg->param1); 274 ASSERT_EQ(4, msg->param1);
284 ASSERT_EQ(7, msg->param2); 275 ASSERT_EQ(7, msg->param2);
285 } 276 }
286 277
278 TEST_F(BattOrConnectionImplTest, ExtraBytesStoredBetweenReads) {
279 OpenConnection();
280 ASSERT_TRUE(GetOpenSuccess());
281
282 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_RESET, 4, 7);
283 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_INIT, 5, 8);
284
285 // Here we intentionally overread to make sure that the overread bytes are
286 // stored for the next read.
287 ReadMessage(sizeof(BattOrControlMessage) * 2);
288
289 ASSERT_TRUE(IsReadComplete());
290 ASSERT_TRUE(GetReadSuccess());
291 ASSERT_EQ(BATTOR_MESSAGE_TYPE_CONTROL, GetReadType());
292
293 BattOrControlMessage* reset_msg =
294 reinterpret_cast<BattOrControlMessage*>(GetReadMessage()->data());
295
296 ASSERT_EQ(BATTOR_CONTROL_MESSAGE_TYPE_RESET, reset_msg->type);
297 ASSERT_EQ(4, reset_msg->param1);
298 ASSERT_EQ(7, reset_msg->param2);
299
300 ReadMessage(sizeof(BattOrControlMessage));
301
302 ASSERT_TRUE(IsReadComplete());
303 ASSERT_TRUE(GetReadSuccess());
304 ASSERT_EQ(BATTOR_MESSAGE_TYPE_CONTROL, GetReadType());
305
306 BattOrControlMessage* init_msg =
307 reinterpret_cast<BattOrControlMessage*>(GetReadMessage()->data());
308
309 ASSERT_EQ(BATTOR_CONTROL_MESSAGE_TYPE_INIT, init_msg->type);
310 ASSERT_EQ(5, init_msg->param1);
311 ASSERT_EQ(8, init_msg->param2);
312 }
313
287 } // namespace battor 314 } // namespace battor
OLDNEW
« tools/battor_agent/battor_connection_impl.cc ('K') | « tools/battor_agent/battor_connection_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698