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

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

Issue 1524873002: Creates a BattOrConnection for communicating with the BattOr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Code review Created 5 years 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
(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 "tools/battor_agent/battor_connection.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/memory/weak_ptr.h"
10 #include "device/serial/serial.mojom.h"
11 #include "device/serial/test_serial_io_handler.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "tools/battor_agent/battor_protocol_types.h"
14
15 namespace {
16
17 void NullWriteCallback(int, device::serial::SendError) {}
18 void NullReadCallback(int, device::serial::ReceiveError) {}
19
20 } // namespace
21
22 namespace battor {
23
24 // TestableBattOrConnection uses a fake serial connection be testable.
25 class TestableBattOrConnection : public BattOrConnection {
26 public:
27 TestableBattOrConnection(base::WeakPtr<BattOrConnection::Listener> listener)
28 : BattOrConnection("/dev/test", listener, nullptr, nullptr) {}
29 scoped_refptr<device::SerialIoHandler> CreateIoHandler() override {
30 return device::TestSerialIoHandler::Create();
31 }
32
33 scoped_refptr<device::SerialIoHandler> GetIoHandler() { return io_handler_; }
34 };
35
36 // BattOrConnectionTest provides a BattOrConnection and captures the
37 // results of all its commands.
38 class BattOrConnectionTest : public testing::Test,
39 public base::SupportsWeakPtr<BattOrConnectionTest>,
40 public BattOrConnection::Listener {
41 public:
42 void OnConnectionOpened(bool success) override { open_success_ = success; };
43 void OnBytesSent(bool success) override { send_success_ = success; }
44 void OnBytesRead(bool success,
45 BattOrMessageType type,
46 scoped_ptr<std::vector<char>> bytes) override {
47 is_read_complete_ = true;
48 read_success_ = success;
49 read_type_ = type;
50 read_bytes_ = bytes.Pass();
51 }
52
53 protected:
54 void SetUp() override {
55 connection_.reset(new TestableBattOrConnection(AsWeakPtr()));
56 }
57
58 void OpenConnection() { connection_->Open(); }
59
60 void ReadBytes(uint16_t bytes_to_read) {
61 is_read_complete_ = false;
62 connection_->ReadBytes(bytes_to_read);
63 }
64
65 // Reads the specified number of bytes directly from the serial connection.
66 scoped_refptr<net::IOBuffer> ReadBytesRaw(int bytes_to_read) {
67 scoped_refptr<net::IOBuffer> buffer(
68 new net::IOBuffer((size_t)bytes_to_read));
69
70 connection_->GetIoHandler()->Read(make_scoped_ptr(new device::ReceiveBuffer(
71 buffer, bytes_to_read, base::Bind(&NullReadCallback))));
72
73 return buffer;
74 }
75
76 void SendControlMessage(BattOrControlMessageType type,
77 uint16_t param1,
78 uint16_t param2) {
79 BattOrControlMessage msg{type, param1, param2};
80 connection_->SendBytes(BATTOR_MESSAGE_TYPE_CONTROL,
81 reinterpret_cast<char*>(&msg), sizeof(msg));
82 }
83
84 // Writes the specified bytes directly to the serial connection.
85 void SendBytesRaw(const char* data, uint16_t bytes_to_send) {
86 std::vector<char> data_vector(data, data + bytes_to_send);
87 connection_->GetIoHandler()->Write(make_scoped_ptr(
88 new device::SendBuffer(data_vector, base::Bind(&NullWriteCallback))));
89 }
90
91 bool GetOpenSuccess() { return open_success_; }
92 bool GetSendSuccess() { return send_success_; }
93 bool IsReadComplete() { return is_read_complete_; }
94 bool GetReadSuccess() { return read_success_; }
95 BattOrMessageType GetReadType() { return read_type_; }
96 std::vector<char>* GetReadBytes() { return read_bytes_.get(); }
97
98 private:
99 scoped_ptr<TestableBattOrConnection> connection_;
100
101 // Result from the last connect command.
102 bool open_success_;
103 // Result from the last send command.
104 bool send_success_;
105 // Results from the last read command.
106 bool is_read_complete_;
107 bool read_success_;
108 BattOrMessageType read_type_;
109 scoped_ptr<std::vector<char>> read_bytes_;
110 };
111
112 TEST_F(BattOrConnectionTest, InitSendsCorrectBytes) {
113 OpenConnection();
114 ASSERT_TRUE(GetOpenSuccess());
115
116 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_INIT, 0, 0);
117
118 const char expected_data[] = {
119 BATTOR_CONTROL_BYTE_START, BATTOR_MESSAGE_TYPE_CONTROL,
120 BATTOR_CONTROL_BYTE_ESCAPE, BATTOR_CONTROL_MESSAGE_TYPE_INIT,
121 BATTOR_CONTROL_BYTE_ESCAPE, 0x00,
122 BATTOR_CONTROL_BYTE_ESCAPE, 0x00,
123 BATTOR_CONTROL_BYTE_ESCAPE, 0x00,
124 BATTOR_CONTROL_BYTE_ESCAPE, 0x00,
125 BATTOR_CONTROL_BYTE_END,
126 };
127
128 ASSERT_TRUE(GetSendSuccess());
129 ASSERT_EQ(0, std::memcmp(ReadBytesRaw(13)->data(), expected_data, 13));
130 }
131
132 TEST_F(BattOrConnectionTest, ResetSendsCorrectBytes) {
133 OpenConnection();
134 ASSERT_TRUE(GetOpenSuccess());
135
136 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0, 0);
137
138 const char expected_data[] = {
139 BATTOR_CONTROL_BYTE_START,
140 BATTOR_MESSAGE_TYPE_CONTROL,
141 BATTOR_CONTROL_MESSAGE_TYPE_RESET,
142 BATTOR_CONTROL_BYTE_ESCAPE,
143 0x00,
144 BATTOR_CONTROL_BYTE_ESCAPE,
145 0x00,
146 BATTOR_CONTROL_BYTE_ESCAPE,
147 0x00,
148 BATTOR_CONTROL_BYTE_ESCAPE,
149 0x00,
150 BATTOR_CONTROL_BYTE_END,
151 };
152
153 ASSERT_TRUE(GetSendSuccess());
154 ASSERT_EQ(0, std::memcmp(ReadBytesRaw(12)->data(), expected_data, 12));
155 }
156
157 TEST_F(BattOrConnectionTest, ReadBytesControlMessage) {
158 OpenConnection();
159 ASSERT_TRUE(GetOpenSuccess());
160
161 const char data[] = {
162 BATTOR_CONTROL_BYTE_START,
163 BATTOR_MESSAGE_TYPE_CONTROL,
164 BATTOR_CONTROL_MESSAGE_TYPE_RESET,
165 0x04,
166 0x04,
167 0x04,
168 0x04,
169 BATTOR_CONTROL_BYTE_END,
170 };
171 SendBytesRaw(data, 8);
172 ReadBytes(5);
173
174 const char expected[] = {BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0x04, 0x04, 0x04,
175 0x04};
176
177 ASSERT_TRUE(IsReadComplete());
178 ASSERT_TRUE(GetReadSuccess());
179 ASSERT_EQ(BATTOR_MESSAGE_TYPE_CONTROL, GetReadType());
180 ASSERT_EQ(0, std::memcmp(GetReadBytes()->data(), expected, 5));
181 }
182
183 TEST_F(BattOrConnectionTest, ReadBytesNotEnoughBytes) {
184 OpenConnection();
185 ASSERT_TRUE(GetOpenSuccess());
186
187 // 3 (h/f), 1 (control message type), 4 (data)
188 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0, 0);
189 ReadBytes(sizeof(BattOrControlMessage) + 1);
190
191 ASSERT_FALSE(IsReadComplete());
192 }
193
194 TEST_F(BattOrConnectionTest, ReadBytesInvalidType) {
195 OpenConnection();
196 ASSERT_TRUE(GetOpenSuccess());
197
198 const char data[] = {
199 BATTOR_CONTROL_BYTE_START,
200 UINT8_MAX,
201 BATTOR_CONTROL_MESSAGE_TYPE_INIT,
202 0x04,
203 0x04,
204 BATTOR_CONTROL_BYTE_END,
205 };
206 SendBytesRaw(data, 8);
207
208 ReadBytes(3);
209
210 ASSERT_TRUE(IsReadComplete());
211 ASSERT_FALSE(GetReadSuccess());
212 }
213
214 TEST_F(BattOrConnectionTest, ReadBytesWithEscapeCharacters) {
215 OpenConnection();
216 ASSERT_TRUE(GetOpenSuccess());
217
218 const char data[] = {
219 BATTOR_CONTROL_BYTE_START,
220 BATTOR_MESSAGE_TYPE_CONTROL_ACK,
221 BATTOR_CONTROL_MESSAGE_TYPE_RESET,
222 BATTOR_CONTROL_BYTE_ESCAPE,
223 0x00,
224 BATTOR_CONTROL_BYTE_END,
225 };
226 SendBytesRaw(data, 6);
227
228 ReadBytes(2);
229
230 const char expected[] = {BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0x00};
231
232 ASSERT_TRUE(IsReadComplete());
233 ASSERT_TRUE(GetReadSuccess());
234 ASSERT_EQ(BATTOR_MESSAGE_TYPE_CONTROL_ACK, GetReadType());
235 ASSERT_EQ(0, std::memcmp(GetReadBytes()->data(), expected, 2));
236 }
237
238 TEST_F(BattOrConnectionTest, 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 }
267
268 TEST_F(BattOrConnectionTest, ReadControlMessage) {
269 OpenConnection();
270 ASSERT_TRUE(GetOpenSuccess());
271
272 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_RESET, 4, 7);
273 ReadBytes(sizeof(BattOrControlMessage));
274
275 ASSERT_TRUE(IsReadComplete());
276 ASSERT_TRUE(GetReadSuccess());
277 ASSERT_EQ(BATTOR_MESSAGE_TYPE_CONTROL, GetReadType());
278
279 BattOrControlMessage* msg =
280 reinterpret_cast<BattOrControlMessage*>(GetReadBytes()->data());
281
282 ASSERT_EQ(BATTOR_CONTROL_MESSAGE_TYPE_RESET, msg->type);
283 ASSERT_EQ(4, msg->param1);
284 ASSERT_EQ(7, msg->param2);
285 }
286
287 } // namespace battor
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698