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

Side by Side Diff: device/u2f/u2f_message_unittest.cc

Issue 2771673002: Use vectors instead of IOBuffer for U2fPackets (Closed)
Patch Set: Use vectors instead of IOBuffer for U2fPackets Created 3 years, 9 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 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(static_cast<size_t>(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(static_cast<size_t>(65),
27 continuation_packet->GetSerializedData().size());
27 } 28 }
28 29
29 /* 30 /*
30 * U2f Init Packets are of the format: 31 * U2f Init Packets are of the format:
31 * Byte 0: 0 32 * Byte 0: 0
32 * Byte 1-4: Channel ID 33 * Byte 1-4: Channel ID
33 * Byte 5: Command byte 34 * Byte 5: Command byte
34 * Byte 6-7: Big Endian size of data 35 * Byte 6-7: Big Endian size of data
35 * Byte 8-n: Data block 36 * Byte 8-n: Data block
36 * 37 *
37 * Remaining buffer is padded with 0 38 * Remaining buffer is padded with 0
38 */ 39 */
39 TEST_F(U2fMessageTest, TestPacketData) { 40 TEST_F(U2fMessageTest, TestPacketData) {
40 uint32_t channel_id = 0xF5060708; 41 uint32_t channel_id = 0xF5060708;
41 std::vector<uint8_t> data{10, 11}; 42 std::vector<uint8_t> data{10, 11};
42 uint8_t cmd = static_cast<uint8_t>(U2fMessage::Type::CMD_WINK); 43 uint8_t cmd = static_cast<uint8_t>(U2fMessage::Type::CMD_WINK);
43 auto init_packet = 44 auto init_packet =
44 base::MakeUnique<U2fInitPacket>(channel_id, cmd, data, data.size()); 45 base::MakeUnique<U2fInitPacket>(channel_id, cmd, data, data.size());
45 int index = 0; 46 size_t index = 0;
46 47
47 EXPECT_EQ(0, init_packet->GetSerializedBuffer()->data()[index++]); 48 std::vector<uint8_t> serialized = init_packet->GetSerializedData();
48 EXPECT_EQ((channel_id >> 24) & 0xff, 49 EXPECT_EQ(0, serialized[index++]);
49 static_cast<uint8_t>( 50 EXPECT_EQ((channel_id >> 24) & 0xff, serialized[index++]);
50 init_packet->GetSerializedBuffer()->data()[index++])); 51 EXPECT_EQ((channel_id >> 16) & 0xff, serialized[index++]);
51 EXPECT_EQ((channel_id >> 16) & 0xff, 52 EXPECT_EQ((channel_id >> 8) & 0xff, serialized[index++]);
52 static_cast<uint8_t>( 53 EXPECT_EQ(channel_id & 0xff, serialized[index++]);
53 init_packet->GetSerializedBuffer()->data()[index++])); 54 EXPECT_EQ(cmd, serialized[index++]);
54 EXPECT_EQ((channel_id >> 8) & 0xff,
55 static_cast<uint8_t>(
56 init_packet->GetSerializedBuffer()->data()[index++]));
57 EXPECT_EQ(channel_id & 0xff,
58 static_cast<uint8_t>(
59 init_packet->GetSerializedBuffer()->data()[index++]));
60 EXPECT_EQ(cmd, static_cast<uint8_t>(
61 init_packet->GetSerializedBuffer()->data()[index++]));
62 55
63 EXPECT_EQ(data.size() >> 8, 56 EXPECT_EQ(data.size() >> 8, serialized[index++]);
64 static_cast<uint8_t>( 57 EXPECT_EQ(data.size() & 0xff, serialized[index++]);
65 init_packet->GetSerializedBuffer()->data()[index++])); 58 EXPECT_EQ(data.at(0), serialized[index++]);
66 EXPECT_EQ(data.size() & 0xff, 59 EXPECT_EQ(data.at(1), serialized[index++]);
67 static_cast<uint8_t>( 60 for (; index < serialized.size(); index++) {
68 init_packet->GetSerializedBuffer()->data()[index++])); 61 EXPECT_EQ(0, serialized[index]) << "mismatch at index " << index;
69 EXPECT_EQ(data.at(0), init_packet->GetSerializedBuffer()->data()[index++]);
70 EXPECT_EQ(data.at(1), init_packet->GetSerializedBuffer()->data()[index++]);
71 for (; index < init_packet->GetSerializedBuffer()->size(); index++) {
72 EXPECT_EQ(0, init_packet->GetSerializedBuffer()->data()[index])
73 << "mismatch at index " << index;
74 } 62 }
75 } 63 }
76 64
77 TEST_F(U2fMessageTest, TestPacketConstructors) { 65 TEST_F(U2fMessageTest, TestPacketConstructors) {
78 uint32_t channel_id = 0x05060708; 66 uint32_t channel_id = 0x05060708;
79 std::vector<uint8_t> data{10, 11}; 67 std::vector<uint8_t> data{10, 11};
80 uint8_t cmd = static_cast<uint8_t>(U2fMessage::Type::CMD_WINK); 68 uint8_t cmd = static_cast<uint8_t>(U2fMessage::Type::CMD_WINK);
81 auto orig_packet = 69 auto orig_packet =
82 base::MakeUnique<U2fInitPacket>(channel_id, cmd, data, data.size()); 70 base::MakeUnique<U2fInitPacket>(channel_id, cmd, data, data.size());
83 71
84 size_t payload_length = static_cast<size_t>(orig_packet->payload_length()); 72 size_t payload_length = static_cast<size_t>(orig_packet->payload_length());
85 std::unique_ptr<U2fInitPacket> reconstructed_packet = 73 std::unique_ptr<U2fInitPacket> reconstructed_packet =
86 U2fInitPacket::CreateFromSerializedData( 74 U2fInitPacket::CreateFromSerializedData(orig_packet->GetSerializedData(),
87 orig_packet->GetSerializedBuffer(), &payload_length); 75 &payload_length);
88 EXPECT_EQ(orig_packet->command(), reconstructed_packet->command()); 76 EXPECT_EQ(orig_packet->command(), reconstructed_packet->command());
89 EXPECT_EQ(orig_packet->payload_length(), 77 EXPECT_EQ(orig_packet->payload_length(),
90 reconstructed_packet->payload_length()); 78 reconstructed_packet->payload_length());
91 EXPECT_THAT(orig_packet->GetPacketPayload(), 79 EXPECT_THAT(orig_packet->GetPacketPayload(),
92 testing::ContainerEq(reconstructed_packet->GetPacketPayload())); 80 testing::ContainerEq(reconstructed_packet->GetPacketPayload()));
93 81
94 EXPECT_EQ(channel_id, reconstructed_packet->channel_id()); 82 EXPECT_EQ(channel_id, reconstructed_packet->channel_id());
95 83
96 ASSERT_EQ(orig_packet->GetSerializedBuffer()->size(), 84 EXPECT_THAT(orig_packet->GetSerializedData(),
97 reconstructed_packet->GetSerializedBuffer()->size()); 85 testing::ContainerEq(reconstructed_packet->GetSerializedData()));
98 for (size_t i = 0;
99 i < static_cast<size_t>(orig_packet->GetSerializedBuffer()->size());
100 ++i) {
101 EXPECT_EQ(orig_packet->GetSerializedBuffer()->data()[i],
102 reconstructed_packet->GetSerializedBuffer()->data()[i]);
103 }
104 } 86 }
105 87
106 TEST_F(U2fMessageTest, TestMaxLengthPacketConstructors) { 88 TEST_F(U2fMessageTest, TestMaxLengthPacketConstructors) {
107 uint32_t channel_id = 0xAAABACAD; 89 uint32_t channel_id = 0xAAABACAD;
108 std::vector<uint8_t> data; 90 std::vector<uint8_t> data;
109 for (size_t i = 0; i < U2fMessage::kMaxMessageSize; ++i) 91 for (size_t i = 0; i < U2fMessage::kMaxMessageSize; ++i)
110 data.push_back(static_cast<uint8_t>(i % 0xff)); 92 data.push_back(static_cast<uint8_t>(i % 0xff));
111 93
112 U2fMessage::Type cmd = U2fMessage::Type::CMD_MSG; 94 U2fMessage::Type cmd = U2fMessage::Type::CMD_MSG;
113 std::unique_ptr<U2fMessage> orig_msg = 95 std::unique_ptr<U2fMessage> orig_msg =
114 U2fMessage::Create(channel_id, cmd, data); 96 U2fMessage::Create(channel_id, cmd, data);
115 auto it = orig_msg->begin(); 97 auto it = orig_msg->begin();
98 std::vector<uint8_t> serialized = (*it)->GetSerializedData();
99 auto serialized_buffer =
100 make_scoped_refptr(new net::IOBufferWithSize(serialized.size()));
101 memcpy(serialized_buffer->data(), serialized.data(), serialized.size());
116 std::unique_ptr<U2fMessage> new_msg = 102 std::unique_ptr<U2fMessage> new_msg =
117 U2fMessage::CreateFromSerializedData((*it)->GetSerializedBuffer()); 103 U2fMessage::CreateFromSerializedData(serialized_buffer);
118 it++; 104 it++;
119 105
120 for (; it != orig_msg->end(); ++it) 106 for (; it != orig_msg->end(); ++it) {
121 new_msg->AddContinuationPacket((*it)->GetSerializedBuffer()); 107 serialized = (*it)->GetSerializedData();
108 serialized_buffer =
109 make_scoped_refptr(new net::IOBufferWithSize(serialized.size()));
110 new_msg->AddContinuationPacket(serialized_buffer);
111 }
122 112
123 auto orig_it = orig_msg->begin(); 113 auto orig_it = orig_msg->begin();
124 auto new_it = new_msg->begin(); 114 auto new_it = new_msg->begin();
125 115
126 for (; orig_it != orig_msg->end() && new_it != new_msg->end(); 116 for (; orig_it != orig_msg->end() && new_it != new_msg->end();
127 ++orig_it, ++new_it) { 117 ++orig_it, ++new_it) {
128 EXPECT_THAT((*orig_it)->GetPacketPayload(), 118 EXPECT_THAT((*orig_it)->GetPacketPayload(),
129 testing::ContainerEq((*new_it)->GetPacketPayload())); 119 testing::ContainerEq((*new_it)->GetPacketPayload()));
130 120
131 EXPECT_EQ((*orig_it)->channel_id(), (*new_it)->channel_id()); 121 EXPECT_EQ((*orig_it)->channel_id(), (*new_it)->channel_id());
132 122
133 ASSERT_EQ((*orig_it)->GetSerializedBuffer()->size(), 123 EXPECT_THAT((*orig_it)->GetSerializedData(),
134 (*new_it)->GetSerializedBuffer()->size()); 124 testing::ContainerEq((*new_it)->GetSerializedData()));
135 for (size_t i = 0;
136 i < static_cast<size_t>((*new_it)->GetSerializedBuffer()->size());
137 ++i) {
138 EXPECT_EQ((*orig_it)->GetSerializedBuffer()->data()[i],
139 (*new_it)->GetSerializedBuffer()->data()[i]);
140 }
141 } 125 }
142 } 126 }
143 127
144 TEST_F(U2fMessageTest, TestMessagePartitoning) { 128 TEST_F(U2fMessageTest, TestMessagePartitoning) {
145 uint32_t channel_id = 0x01010203; 129 uint32_t channel_id = 0x01010203;
146 std::vector<uint8_t> data(U2fMessage::kInitPacketDataSize + 1); 130 std::vector<uint8_t> data(U2fMessage::kInitPacketDataSize + 1);
147 std::unique_ptr<U2fMessage> two_packet_message = 131 std::unique_ptr<U2fMessage> two_packet_message =
148 U2fMessage::Create(channel_id, U2fMessage::Type::CMD_PING, data); 132 U2fMessage::Create(channel_id, U2fMessage::Type::CMD_PING, data);
149 EXPECT_EQ(2U, two_packet_message->NumPackets()); 133 EXPECT_EQ(2U, two_packet_message->NumPackets());
150 134
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 new_message->AddContinuationPacket(buf); 179 new_message->AddContinuationPacket(buf);
196 } 180 }
197 181
198 while ((buf = new_message->PopNextPacket())) { 182 while ((buf = new_message->PopNextPacket())) {
199 ASSERT_EQ(buf->size(), orig_list.front()->size()); 183 ASSERT_EQ(buf->size(), orig_list.front()->size());
200 EXPECT_EQ(0, memcmp(buf->data(), orig_list.front()->data(), buf->size())); 184 EXPECT_EQ(0, memcmp(buf->data(), orig_list.front()->data(), buf->size()));
201 orig_list.pop_front(); 185 orig_list.pop_front();
202 } 186 }
203 } 187 }
204 } // namespace device 188 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698