OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "device/u2f/u2f_message.h" | 5 #include "device/u2f/u2f_message.h" |
6 #include "base/memory/ptr_util.h" | 6 #include "base/memory/ptr_util.h" |
7 #include "net/base/io_buffer.h" | 7 #include "net/base/io_buffer.h" |
8 #include "testing/gmock/include/gmock/gmock.h" | 8 #include "testing/gmock/include/gmock/gmock.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
11 namespace device { | 11 namespace device { |
12 | 12 |
13 class U2fMessageTest : public testing::Test {}; | 13 class U2fMessageTest : public testing::Test {}; |
14 | 14 |
15 // Packets should be 64 bytes + 1 report ID byte | 15 // Packets should be 64 bytes + 1 report ID byte |
16 TEST_F(U2fMessageTest, TestPacketSize) { | 16 TEST_F(U2fMessageTest, TestPacketSize) { |
17 uint32_t channel_id = 0x05060708; | 17 uint32_t channel_id = 0x05060708; |
18 std::vector<uint8_t> data; | 18 std::vector<uint8_t> data; |
19 | 19 |
20 auto init_packet = | 20 auto init_packet = |
21 base::MakeUnique<U2fInitPacket>(channel_id, 0, data, data.size()); | 21 base::MakeUnique<U2fInitPacket>(channel_id, 0, data, data.size()); |
22 EXPECT_EQ(65, init_packet->GetSerializedBuffer()->size()); | 22 EXPECT_EQ(65, init_packet->GetSerializedData()->size()); |
23 | 23 |
24 auto continuation_packet = | 24 auto continuation_packet = |
25 base::MakeUnique<U2fContinuationPacket>(channel_id, 0, data); | 25 base::MakeUnique<U2fContinuationPacket>(channel_id, 0, data); |
26 EXPECT_EQ(65, continuation_packet->GetSerializedBuffer()->size()); | 26 EXPECT_EQ(65, continuation_packet->GetSerializedData()->size()); |
27 } | 27 } |
28 | 28 |
29 /* | 29 /* |
30 * U2f Init Packets are of the format: | 30 * U2f Init Packets are of the format: |
31 * Byte 0: 0 | 31 * Byte 0: 0 |
32 * Byte 1-4: Channel ID | 32 * Byte 1-4: Channel ID |
33 * Byte 5: Command byte | 33 * Byte 5: Command byte |
34 * Byte 6-7: Big Endian size of data | 34 * Byte 6-7: Big Endian size of data |
35 * Byte 8-n: Data block | 35 * Byte 8-n: Data block |
36 * | 36 * |
37 * Remaining buffer is padded with 0 | 37 * Remaining buffer is padded with 0 |
38 */ | 38 */ |
39 TEST_F(U2fMessageTest, TestPacketData) { | 39 TEST_F(U2fMessageTest, TestPacketData) { |
40 uint32_t channel_id = 0xF5060708; | 40 uint32_t channel_id = 0xF5060708; |
41 std::vector<uint8_t> data{10, 11}; | 41 std::vector<uint8_t> data{10, 11}; |
42 uint8_t cmd = static_cast<uint8_t>(U2fMessage::Type::CMD_WINK); | 42 uint8_t cmd = static_cast<uint8_t>(U2fMessage::Type::CMD_WINK); |
43 auto init_packet = | 43 auto init_packet = |
44 base::MakeUnique<U2fInitPacket>(channel_id, cmd, data, data.size()); | 44 base::MakeUnique<U2fInitPacket>(channel_id, cmd, data, data.size()); |
45 int index = 0; | 45 int index = 0; |
46 | 46 |
47 EXPECT_EQ(0, init_packet->GetSerializedBuffer()->data()[index++]); | 47 scoped_refptr<net::IOBufferWithSize> serialized = |
| 48 init_packet->GetSerializedData(); |
| 49 EXPECT_EQ(0, serialized->data()[index++]); |
48 EXPECT_EQ((channel_id >> 24) & 0xff, | 50 EXPECT_EQ((channel_id >> 24) & 0xff, |
49 static_cast<uint8_t>( | 51 static_cast<uint8_t>(serialized->data()[index++])); |
50 init_packet->GetSerializedBuffer()->data()[index++])); | |
51 EXPECT_EQ((channel_id >> 16) & 0xff, | 52 EXPECT_EQ((channel_id >> 16) & 0xff, |
52 static_cast<uint8_t>( | 53 static_cast<uint8_t>(serialized->data()[index++])); |
53 init_packet->GetSerializedBuffer()->data()[index++])); | |
54 EXPECT_EQ((channel_id >> 8) & 0xff, | 54 EXPECT_EQ((channel_id >> 8) & 0xff, |
55 static_cast<uint8_t>( | 55 static_cast<uint8_t>(serialized->data()[index++])); |
56 init_packet->GetSerializedBuffer()->data()[index++])); | |
57 EXPECT_EQ(channel_id & 0xff, | 56 EXPECT_EQ(channel_id & 0xff, |
58 static_cast<uint8_t>( | 57 static_cast<uint8_t>(serialized->data()[index++])); |
59 init_packet->GetSerializedBuffer()->data()[index++])); | 58 EXPECT_EQ(cmd, static_cast<uint8_t>(serialized->data()[index++])); |
60 EXPECT_EQ(cmd, static_cast<uint8_t>( | |
61 init_packet->GetSerializedBuffer()->data()[index++])); | |
62 | 59 |
63 EXPECT_EQ(data.size() >> 8, | 60 EXPECT_EQ(data.size() >> 8, |
64 static_cast<uint8_t>( | 61 static_cast<uint8_t>(serialized->data()[index++])); |
65 init_packet->GetSerializedBuffer()->data()[index++])); | |
66 EXPECT_EQ(data.size() & 0xff, | 62 EXPECT_EQ(data.size() & 0xff, |
67 static_cast<uint8_t>( | 63 static_cast<uint8_t>(serialized->data()[index++])); |
68 init_packet->GetSerializedBuffer()->data()[index++])); | 64 EXPECT_EQ(data[0], serialized->data()[index++]); |
69 EXPECT_EQ(data.at(0), init_packet->GetSerializedBuffer()->data()[index++]); | 65 EXPECT_EQ(data[1], serialized->data()[index++]); |
70 EXPECT_EQ(data.at(1), init_packet->GetSerializedBuffer()->data()[index++]); | 66 for (; index < serialized->size(); index++) |
71 for (; index < init_packet->GetSerializedBuffer()->size(); index++) { | 67 EXPECT_EQ(0, serialized->data()[index]) << "mismatch at index " << index; |
72 EXPECT_EQ(0, init_packet->GetSerializedBuffer()->data()[index]) | |
73 << "mismatch at index " << index; | |
74 } | |
75 } | 68 } |
76 | 69 |
77 TEST_F(U2fMessageTest, TestPacketConstructors) { | 70 TEST_F(U2fMessageTest, TestPacketConstructors) { |
78 uint32_t channel_id = 0x05060708; | 71 uint32_t channel_id = 0x05060708; |
79 std::vector<uint8_t> data{10, 11}; | 72 std::vector<uint8_t> data{10, 11}; |
80 uint8_t cmd = static_cast<uint8_t>(U2fMessage::Type::CMD_WINK); | 73 uint8_t cmd = static_cast<uint8_t>(U2fMessage::Type::CMD_WINK); |
81 auto orig_packet = | 74 auto orig_packet = |
82 base::MakeUnique<U2fInitPacket>(channel_id, cmd, data, data.size()); | 75 base::MakeUnique<U2fInitPacket>(channel_id, cmd, data, data.size()); |
83 | 76 |
84 size_t payload_length = static_cast<size_t>(orig_packet->payload_length()); | 77 size_t payload_length = static_cast<size_t>(orig_packet->payload_length()); |
| 78 scoped_refptr<net::IOBufferWithSize> buffer = |
| 79 orig_packet->GetSerializedData(); |
| 80 std::vector<uint8_t> orig_data(buffer->data(), |
| 81 buffer->data() + buffer->size()); |
85 std::unique_ptr<U2fInitPacket> reconstructed_packet = | 82 std::unique_ptr<U2fInitPacket> reconstructed_packet = |
86 U2fInitPacket::CreateFromSerializedData( | 83 U2fInitPacket::CreateFromSerializedData(orig_data, &payload_length); |
87 orig_packet->GetSerializedBuffer(), &payload_length); | |
88 EXPECT_EQ(orig_packet->command(), reconstructed_packet->command()); | 84 EXPECT_EQ(orig_packet->command(), reconstructed_packet->command()); |
89 EXPECT_EQ(orig_packet->payload_length(), | 85 EXPECT_EQ(orig_packet->payload_length(), |
90 reconstructed_packet->payload_length()); | 86 reconstructed_packet->payload_length()); |
91 EXPECT_THAT(orig_packet->GetPacketPayload(), | 87 EXPECT_THAT(orig_packet->GetPacketPayload(), |
92 testing::ContainerEq(reconstructed_packet->GetPacketPayload())); | 88 testing::ContainerEq(reconstructed_packet->GetPacketPayload())); |
93 | 89 |
94 EXPECT_EQ(channel_id, reconstructed_packet->channel_id()); | 90 EXPECT_EQ(channel_id, reconstructed_packet->channel_id()); |
95 | 91 |
96 ASSERT_EQ(orig_packet->GetSerializedBuffer()->size(), | 92 ASSERT_EQ(orig_packet->GetSerializedData()->size(), |
97 reconstructed_packet->GetSerializedBuffer()->size()); | 93 reconstructed_packet->GetSerializedData()->size()); |
98 for (size_t i = 0; | 94 for (int index = 0; index < orig_packet->GetSerializedData()->size(); |
99 i < static_cast<size_t>(orig_packet->GetSerializedBuffer()->size()); | 95 ++index) { |
100 ++i) { | 96 EXPECT_EQ(orig_packet->GetSerializedData()->data()[index], |
101 EXPECT_EQ(orig_packet->GetSerializedBuffer()->data()[i], | 97 reconstructed_packet->GetSerializedData()->data()[index]) |
102 reconstructed_packet->GetSerializedBuffer()->data()[i]); | 98 << "mismatch at index " << index; |
103 } | 99 } |
104 } | 100 } |
105 | 101 |
106 TEST_F(U2fMessageTest, TestMaxLengthPacketConstructors) { | 102 TEST_F(U2fMessageTest, TestMaxLengthPacketConstructors) { |
107 uint32_t channel_id = 0xAAABACAD; | 103 uint32_t channel_id = 0xAAABACAD; |
108 std::vector<uint8_t> data; | 104 std::vector<uint8_t> data; |
109 for (size_t i = 0; i < U2fMessage::kMaxMessageSize; ++i) | 105 for (size_t i = 0; i < U2fMessage::kMaxMessageSize; ++i) |
110 data.push_back(static_cast<uint8_t>(i % 0xff)); | 106 data.push_back(static_cast<uint8_t>(i % 0xff)); |
111 | 107 |
112 U2fMessage::Type cmd = U2fMessage::Type::CMD_MSG; | 108 U2fMessage::Type cmd = U2fMessage::Type::CMD_MSG; |
113 std::unique_ptr<U2fMessage> orig_msg = | 109 std::unique_ptr<U2fMessage> orig_msg = |
114 U2fMessage::Create(channel_id, cmd, data); | 110 U2fMessage::Create(channel_id, cmd, data); |
115 auto it = orig_msg->begin(); | 111 auto it = orig_msg->begin(); |
| 112 |
| 113 scoped_refptr<net::IOBufferWithSize> buffer = (*it)->GetSerializedData(); |
| 114 std::vector<uint8_t> msg_data(buffer->data(), |
| 115 buffer->data() + buffer->size()); |
116 std::unique_ptr<U2fMessage> new_msg = | 116 std::unique_ptr<U2fMessage> new_msg = |
117 U2fMessage::CreateFromSerializedData((*it)->GetSerializedBuffer()); | 117 U2fMessage::CreateFromSerializedData(msg_data); |
118 it++; | 118 it++; |
119 | 119 for (; it != orig_msg->end(); ++it) { |
120 for (; it != orig_msg->end(); ++it) | 120 buffer = (*it)->GetSerializedData(); |
121 new_msg->AddContinuationPacket((*it)->GetSerializedBuffer()); | 121 msg_data.assign(buffer->data(), buffer->data() + buffer->size()); |
| 122 new_msg->AddContinuationPacket(msg_data); |
| 123 } |
122 | 124 |
123 auto orig_it = orig_msg->begin(); | 125 auto orig_it = orig_msg->begin(); |
124 auto new_it = new_msg->begin(); | 126 auto new_it = new_msg->begin(); |
125 | 127 |
126 for (; orig_it != orig_msg->end() && new_it != new_msg->end(); | 128 for (; orig_it != orig_msg->end() && new_it != new_msg->end(); |
127 ++orig_it, ++new_it) { | 129 ++orig_it, ++new_it) { |
128 EXPECT_THAT((*orig_it)->GetPacketPayload(), | 130 EXPECT_THAT((*orig_it)->GetPacketPayload(), |
129 testing::ContainerEq((*new_it)->GetPacketPayload())); | 131 testing::ContainerEq((*new_it)->GetPacketPayload())); |
130 | 132 |
131 EXPECT_EQ((*orig_it)->channel_id(), (*new_it)->channel_id()); | 133 EXPECT_EQ((*orig_it)->channel_id(), (*new_it)->channel_id()); |
132 | 134 |
133 ASSERT_EQ((*orig_it)->GetSerializedBuffer()->size(), | 135 ASSERT_EQ((*orig_it)->GetSerializedData()->size(), |
134 (*new_it)->GetSerializedBuffer()->size()); | 136 (*new_it)->GetSerializedData()->size()); |
135 for (size_t i = 0; | 137 for (int index = 0; index < (*orig_it)->GetSerializedData()->size(); |
136 i < static_cast<size_t>((*new_it)->GetSerializedBuffer()->size()); | 138 ++index) { |
137 ++i) { | 139 EXPECT_EQ((*orig_it)->GetSerializedData()->data()[index], |
138 EXPECT_EQ((*orig_it)->GetSerializedBuffer()->data()[i], | 140 (*new_it)->GetSerializedData()->data()[index]) |
139 (*new_it)->GetSerializedBuffer()->data()[i]); | 141 << "mismatch at index " << index; |
140 } | 142 } |
141 } | 143 } |
142 } | 144 } |
143 | 145 |
144 TEST_F(U2fMessageTest, TestMessagePartitoning) { | 146 TEST_F(U2fMessageTest, TestMessagePartitoning) { |
145 uint32_t channel_id = 0x01010203; | 147 uint32_t channel_id = 0x01010203; |
146 std::vector<uint8_t> data(U2fMessage::kInitPacketDataSize + 1); | 148 std::vector<uint8_t> data(U2fMessage::kInitPacketDataSize + 1); |
147 std::unique_ptr<U2fMessage> two_packet_message = | 149 std::unique_ptr<U2fMessage> two_packet_message = |
148 U2fMessage::Create(channel_id, U2fMessage::Type::CMD_PING, data); | 150 U2fMessage::Create(channel_id, U2fMessage::Type::CMD_PING, data); |
149 EXPECT_EQ(2U, two_packet_message->NumPackets()); | 151 EXPECT_EQ(2U, two_packet_message->NumPackets()); |
(...skipping 30 matching lines...) Expand all Loading... |
180 TEST_F(U2fMessageTest, TestDeserialize) { | 182 TEST_F(U2fMessageTest, TestDeserialize) { |
181 uint32_t channel_id = 0x0A0B0C0D; | 183 uint32_t channel_id = 0x0A0B0C0D; |
182 std::vector<uint8_t> data(U2fMessage::kMaxMessageSize); | 184 std::vector<uint8_t> data(U2fMessage::kMaxMessageSize); |
183 | 185 |
184 std::unique_ptr<U2fMessage> orig_message = | 186 std::unique_ptr<U2fMessage> orig_message = |
185 U2fMessage::Create(channel_id, U2fMessage::Type::CMD_PING, data); | 187 U2fMessage::Create(channel_id, U2fMessage::Type::CMD_PING, data); |
186 std::list<scoped_refptr<net::IOBufferWithSize>> orig_list; | 188 std::list<scoped_refptr<net::IOBufferWithSize>> orig_list; |
187 scoped_refptr<net::IOBufferWithSize> buf = orig_message->PopNextPacket(); | 189 scoped_refptr<net::IOBufferWithSize> buf = orig_message->PopNextPacket(); |
188 orig_list.push_back(buf); | 190 orig_list.push_back(buf); |
189 | 191 |
| 192 std::vector<uint8_t> message_data(buf->data(), buf->data() + buf->size()); |
190 std::unique_ptr<U2fMessage> new_message = | 193 std::unique_ptr<U2fMessage> new_message = |
191 U2fMessage::CreateFromSerializedData(buf); | 194 U2fMessage::CreateFromSerializedData(message_data); |
192 while (!new_message->MessageComplete()) { | 195 while (!new_message->MessageComplete()) { |
193 buf = orig_message->PopNextPacket(); | 196 buf = orig_message->PopNextPacket(); |
194 orig_list.push_back(buf); | 197 orig_list.push_back(buf); |
195 new_message->AddContinuationPacket(buf); | 198 message_data.assign(buf->data(), buf->data() + buf->size()); |
| 199 new_message->AddContinuationPacket(message_data); |
196 } | 200 } |
197 | 201 |
198 while ((buf = new_message->PopNextPacket())) { | 202 while ((buf = new_message->PopNextPacket())) { |
199 ASSERT_EQ(buf->size(), orig_list.front()->size()); | 203 ASSERT_EQ(buf->size(), orig_list.front()->size()); |
200 EXPECT_EQ(0, memcmp(buf->data(), orig_list.front()->data(), buf->size())); | 204 EXPECT_EQ(0, memcmp(buf->data(), orig_list.front()->data(), buf->size())); |
201 orig_list.pop_front(); | 205 orig_list.pop_front(); |
202 } | 206 } |
203 } | 207 } |
204 } // namespace device | 208 } // namespace device |
OLD | NEW |