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 #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(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 BattOrConnection::Listener { |
| 40 public: |
| 41 void OnConnectionOpened(bool success) override { open_success_ = success; }; |
| 42 void OnBytesSent(bool success) override { send_success_ = success; } |
| 43 void OnBytesRead(bool success, |
| 44 BattOrMessageType type, |
| 45 scoped_ptr<std::vector<char>> bytes) override { |
| 46 is_read_complete_ = true; |
| 47 read_success_ = success; |
| 48 read_type_ = type; |
| 49 read_bytes_ = bytes.Pass(); |
| 50 } |
| 51 |
| 52 protected: |
| 53 void SetUp() override { |
| 54 connection_.reset(new TestableBattOrConnection(this)); |
| 55 } |
| 56 |
| 57 void OpenConnection() { connection_->Open(); } |
| 58 |
| 59 void ReadBytes(uint16_t bytes_to_read) { |
| 60 is_read_complete_ = false; |
| 61 connection_->ReadBytes(bytes_to_read); |
| 62 } |
| 63 |
| 64 // Reads the specified number of bytes directly from the serial connection. |
| 65 scoped_refptr<net::IOBuffer> ReadBytesRaw(int bytes_to_read) { |
| 66 scoped_refptr<net::IOBuffer> buffer( |
| 67 new net::IOBuffer((size_t)bytes_to_read)); |
| 68 |
| 69 connection_->GetIoHandler()->Read(make_scoped_ptr(new device::ReceiveBuffer( |
| 70 buffer, bytes_to_read, base::Bind(&NullReadCallback)))); |
| 71 |
| 72 return buffer; |
| 73 } |
| 74 |
| 75 void SendControlMessage(BattOrControlMessageType type, |
| 76 uint16_t param1, |
| 77 uint16_t param2) { |
| 78 BattOrControlMessage msg{type, param1, param2}; |
| 79 connection_->SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, |
| 80 reinterpret_cast<char*>(&msg), sizeof(msg)); |
| 81 } |
| 82 |
| 83 // Writes the specified bytes directly to the serial connection. |
| 84 void SendBytesRaw(const char* data, uint16_t bytes_to_send) { |
| 85 std::vector<char> data_vector(data, data + bytes_to_send); |
| 86 connection_->GetIoHandler()->Write(make_scoped_ptr( |
| 87 new device::SendBuffer(data_vector, base::Bind(&NullWriteCallback)))); |
| 88 } |
| 89 |
| 90 bool GetOpenSuccess() { return open_success_; } |
| 91 bool GetSendSuccess() { return send_success_; } |
| 92 bool IsReadComplete() { return is_read_complete_; } |
| 93 bool GetReadSuccess() { return read_success_; } |
| 94 BattOrMessageType GetReadType() { return read_type_; } |
| 95 std::vector<char>* GetReadBytes() { return read_bytes_.get(); } |
| 96 |
| 97 private: |
| 98 scoped_ptr<TestableBattOrConnection> connection_; |
| 99 |
| 100 // Result from the last connect command. |
| 101 bool open_success_; |
| 102 // Result from the last send command. |
| 103 bool send_success_; |
| 104 // Results from the last read command. |
| 105 bool is_read_complete_; |
| 106 bool read_success_; |
| 107 BattOrMessageType read_type_; |
| 108 scoped_ptr<std::vector<char>> read_bytes_; |
| 109 }; |
| 110 |
| 111 TEST_F(BattOrConnectionTest, InitSendsCorrectBytes) { |
| 112 OpenConnection(); |
| 113 ASSERT_TRUE(GetOpenSuccess()); |
| 114 |
| 115 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_INIT, 0, 0); |
| 116 |
| 117 const char expected_data[] = { |
| 118 BATTOR_CONTROL_BYTE_START, BATTOR_MESSAGE_TYPE_CONTROL, |
| 119 BATTOR_CONTROL_BYTE_ESCAPE, BATTOR_CONTROL_MESSAGE_TYPE_INIT, |
| 120 BATTOR_CONTROL_BYTE_ESCAPE, 0x00, |
| 121 BATTOR_CONTROL_BYTE_ESCAPE, 0x00, |
| 122 BATTOR_CONTROL_BYTE_ESCAPE, 0x00, |
| 123 BATTOR_CONTROL_BYTE_ESCAPE, 0x00, |
| 124 BATTOR_CONTROL_BYTE_END, |
| 125 }; |
| 126 |
| 127 ASSERT_TRUE(GetSendSuccess()); |
| 128 ASSERT_EQ(0, std::memcmp(ReadBytesRaw(13)->data(), expected_data, 13)); |
| 129 } |
| 130 |
| 131 TEST_F(BattOrConnectionTest, ResetSendsCorrectBytes) { |
| 132 OpenConnection(); |
| 133 ASSERT_TRUE(GetOpenSuccess()); |
| 134 |
| 135 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0, 0); |
| 136 |
| 137 const char expected_data[] = { |
| 138 BATTOR_CONTROL_BYTE_START, |
| 139 BATTOR_MESSAGE_TYPE_CONTROL, |
| 140 BATTOR_CONTROL_MESSAGE_TYPE_RESET, |
| 141 BATTOR_CONTROL_BYTE_ESCAPE, |
| 142 0x00, |
| 143 BATTOR_CONTROL_BYTE_ESCAPE, |
| 144 0x00, |
| 145 BATTOR_CONTROL_BYTE_ESCAPE, |
| 146 0x00, |
| 147 BATTOR_CONTROL_BYTE_ESCAPE, |
| 148 0x00, |
| 149 BATTOR_CONTROL_BYTE_END, |
| 150 }; |
| 151 |
| 152 ASSERT_TRUE(GetSendSuccess()); |
| 153 ASSERT_EQ(0, std::memcmp(ReadBytesRaw(12)->data(), expected_data, 12)); |
| 154 } |
| 155 |
| 156 TEST_F(BattOrConnectionTest, ReadBytesControlMessage) { |
| 157 OpenConnection(); |
| 158 ASSERT_TRUE(GetOpenSuccess()); |
| 159 |
| 160 const char data[] = { |
| 161 BATTOR_CONTROL_BYTE_START, |
| 162 BATTOR_MESSAGE_TYPE_CONTROL, |
| 163 BATTOR_CONTROL_MESSAGE_TYPE_RESET, |
| 164 0x04, |
| 165 0x04, |
| 166 0x04, |
| 167 0x04, |
| 168 BATTOR_CONTROL_BYTE_END, |
| 169 }; |
| 170 SendBytesRaw(data, 8); |
| 171 ReadBytes(5); |
| 172 |
| 173 const char expected[] = {BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0x04, 0x04, 0x04, |
| 174 0x04}; |
| 175 |
| 176 ASSERT_TRUE(IsReadComplete()); |
| 177 ASSERT_TRUE(GetReadSuccess()); |
| 178 ASSERT_EQ(BATTOR_MESSAGE_TYPE_CONTROL, GetReadType()); |
| 179 ASSERT_EQ(0, std::memcmp(GetReadBytes()->data(), expected, 5)); |
| 180 } |
| 181 |
| 182 TEST_F(BattOrConnectionTest, ReadBytesNotEnoughBytes) { |
| 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(BattOrConnectionTest, ReadBytesInvalidType) { |
| 194 OpenConnection(); |
| 195 ASSERT_TRUE(GetOpenSuccess()); |
| 196 |
| 197 const char data[] = { |
| 198 BATTOR_CONTROL_BYTE_START, |
| 199 UINT8_MAX, |
| 200 BATTOR_CONTROL_MESSAGE_TYPE_INIT, |
| 201 0x04, |
| 202 0x04, |
| 203 BATTOR_CONTROL_BYTE_END, |
| 204 }; |
| 205 SendBytesRaw(data, 8); |
| 206 |
| 207 ReadBytes(3); |
| 208 |
| 209 ASSERT_TRUE(IsReadComplete()); |
| 210 ASSERT_FALSE(GetReadSuccess()); |
| 211 } |
| 212 |
| 213 TEST_F(BattOrConnectionTest, ReadBytesWithEscapeCharacters) { |
| 214 OpenConnection(); |
| 215 ASSERT_TRUE(GetOpenSuccess()); |
| 216 |
| 217 const char data[] = { |
| 218 BATTOR_CONTROL_BYTE_START, |
| 219 BATTOR_MESSAGE_TYPE_CONTROL_ACK, |
| 220 BATTOR_CONTROL_MESSAGE_TYPE_RESET, |
| 221 BATTOR_CONTROL_BYTE_ESCAPE, |
| 222 0x00, |
| 223 BATTOR_CONTROL_BYTE_END, |
| 224 }; |
| 225 SendBytesRaw(data, 6); |
| 226 |
| 227 ReadBytes(2); |
| 228 |
| 229 const char expected[] = {BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0x00}; |
| 230 |
| 231 ASSERT_TRUE(IsReadComplete()); |
| 232 ASSERT_TRUE(GetReadSuccess()); |
| 233 ASSERT_EQ(BATTOR_MESSAGE_TYPE_CONTROL_ACK, GetReadType()); |
| 234 ASSERT_EQ(0, std::memcmp(GetReadBytes()->data(), expected, 2)); |
| 235 } |
| 236 |
| 237 TEST_F(BattOrConnectionTest, ReadBytesWithEscapeCharactersInSubsequentReads) { |
| 238 OpenConnection(); |
| 239 ASSERT_TRUE(GetOpenSuccess()); |
| 240 |
| 241 // The first read should request 7 bytes. Of those 7 bytes, though, 2 of them |
| 242 // are escape bytes, so we'll then do a second read of 2 bytes. In that second |
| 243 // read, we'll see another escape byte, so we'll have to do a third read of 1 |
| 244 // byte. That third read should complete the message. |
| 245 const char data[] = { |
| 246 // These bytes make up the first read. |
| 247 BATTOR_CONTROL_BYTE_START, BATTOR_MESSAGE_TYPE_CONTROL_ACK, |
| 248 BATTOR_CONTROL_MESSAGE_TYPE_RESET, BATTOR_CONTROL_BYTE_ESCAPE, 0x00, |
| 249 BATTOR_CONTROL_BYTE_ESCAPE, 0x00, |
| 250 // These bytes make up the second read. |
| 251 BATTOR_CONTROL_BYTE_ESCAPE, 0x00, |
| 252 // This byte makes up the third read. |
| 253 BATTOR_CONTROL_BYTE_END, |
| 254 }; |
| 255 SendBytesRaw(data, 10); |
| 256 |
| 257 ReadBytes(4); |
| 258 |
| 259 const char expected[] = {BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0x00, 0x00, 0x00}; |
| 260 |
| 261 ASSERT_TRUE(IsReadComplete()); |
| 262 ASSERT_TRUE(GetReadSuccess()); |
| 263 ASSERT_EQ(BATTOR_MESSAGE_TYPE_CONTROL_ACK, GetReadType()); |
| 264 ASSERT_EQ(0, std::memcmp(GetReadBytes()->data(), expected, 4)); |
| 265 } |
| 266 |
| 267 TEST_F(BattOrConnectionTest, ReadControlMessage) { |
| 268 OpenConnection(); |
| 269 ASSERT_TRUE(GetOpenSuccess()); |
| 270 |
| 271 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_RESET, 4, 7); |
| 272 ReadBytes(sizeof(BattOrControlMessage)); |
| 273 |
| 274 ASSERT_TRUE(IsReadComplete()); |
| 275 ASSERT_TRUE(GetReadSuccess()); |
| 276 ASSERT_EQ(BATTOR_MESSAGE_TYPE_CONTROL, GetReadType()); |
| 277 |
| 278 BattOrControlMessage* msg = |
| 279 reinterpret_cast<BattOrControlMessage*>(GetReadBytes()->data()); |
| 280 |
| 281 ASSERT_EQ(BATTOR_CONTROL_MESSAGE_TYPE_RESET, msg->type); |
| 282 ASSERT_EQ(4, msg->param1); |
| 283 ASSERT_EQ(7, msg->param2); |
| 284 } |
| 285 |
| 286 } // namespace battor |
OLD | NEW |