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

Side by Side Diff: net/quic/crypto/crypto_framer_test.cc

Issue 11125002: Add QuicFramer and friends. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix final comments. Created 8 years, 2 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 | Annotate | Revision Log
« no previous file with comments | « net/quic/crypto/crypto_framer.cc ('k') | net/quic/crypto/crypto_protocol.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 <map>
6 #include <vector>
7
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "net/quic/crypto/crypto_framer.h"
11 #include "net/quic/crypto/crypto_protocol.h"
12 #include "net/quic/test_tools/quic_test_utils.h"
13
14 using base::StringPiece;
15 using std::map;
16 using std::string;
17 using std::vector;
18
19 namespace net {
20
21 namespace {
22
23 char* AsChars(unsigned char* data) {
24 return reinterpret_cast<char*>(data);
25 }
26
27 } // namespace
28
29 namespace test {
30
31 class TestCryptoVisitor : public ::net::CryptoFramerVisitorInterface {
32 public:
33 TestCryptoVisitor()
34 : error_count_(0) {
35 }
36
37 ~TestCryptoVisitor() {}
38
39 virtual void OnError(CryptoFramer* f) {
40 LOG(INFO) << "CryptoFramer Error: " << f->error();
41 error_count_++;
42 }
43
44 virtual void OnHandshakeMessage(const CryptoHandshakeMessage& message) {
45 message_tags_.push_back(message.tag);
46 message_maps_.push_back(map<CryptoTag, string>());
47 CryptoTagValueMap::const_iterator it = message.tag_value_map.begin();
48 while (it != message.tag_value_map.end()) {
49 message_maps_.back()[it->first] = it->second.as_string();
50 ++it;
51 }
52 }
53
54 CryptoFramer framer_;
55
56 // Counters from the visitor callbacks.
57 int error_count_;
58
59 vector<CryptoTag> message_tags_;
60 vector<map<CryptoTag, string> > message_maps_;
61 };
62
63 } // namespace test
64
65 TEST(CryptoFramerTest, ConstructHandshakeMessage) {
66 CryptoHandshakeMessage message;
67 message.tag = 0xFFAA7733;
68 message.tag_value_map[0x12345678] = "abcdef";
69 message.tag_value_map[0x12345679] = "ghijk";
70 message.tag_value_map[0x1234567A] = "lmnopqr";
71
72 unsigned char packet[] = {
73 // tag
74 0x33, 0x77, 0xAA, 0xFF,
75 // num entires
76 0x03, 0x00,
77 // tag 1
78 0x78, 0x56, 0x34, 0x12,
79 // tag 2
80 0x79, 0x56, 0x34, 0x12,
81 // tag 3
82 0x7A, 0x56, 0x34, 0x12,
83 // len 1
84 0x06, 0x00,
85 // len 2
86 0x05, 0x00,
87 // len 3
88 0x07, 0x00,
89 // padding
90 0xAB, 0xAB,
91 // value 1
92 'a', 'b', 'c', 'd',
93 'e', 'f',
94 // value 2
95 'g', 'h', 'i', 'j',
96 'k',
97 // value 3
98 'l', 'm', 'n', 'o',
99 'p', 'q', 'r',
100 };
101
102 CryptoFramer framer;
103 QuicData* data;
104 EXPECT_TRUE(framer.ConstructHandshakeMessage(message, &data));
105
106 test::CompareCharArraysWithHexError("constructed packet",
107 data->data(), data->length(),
108 AsChars(packet), arraysize(packet));
109
110 delete data;
111 }
112
113 TEST(CryptoFramerTest, ConstructHandshakeMessageWithTwoKeys) {
114 CryptoHandshakeMessage message;
115 message.tag = 0xFFAA7733;
116 message.tag_value_map[0x12345678] = "abcdef";
117 message.tag_value_map[0x12345679] = "ghijk";
118
119 unsigned char packet[] = {
120 // tag
121 0x33, 0x77, 0xAA, 0xFF,
122 // num entires
123 0x02, 0x00,
124 // tag 1
125 0x78, 0x56, 0x34, 0x12,
126 // tag 2
127 0x79, 0x56, 0x34, 0x12,
128 // len 1
129 0x06, 0x00,
130 // len 2
131 0x05, 0x00,
132 // value 1
133 'a', 'b', 'c', 'd',
134 'e', 'f',
135 // value 2
136 'g', 'h', 'i', 'j',
137 'k',
138 };
139
140 CryptoFramer framer;
141 QuicData* data;
142 EXPECT_TRUE(framer.ConstructHandshakeMessage(message, &data));
143
144 test::CompareCharArraysWithHexError("constructed packet",
145 data->data(), data->length(),
146 AsChars(packet), arraysize(packet));
147
148 delete data;
149 }
150
151 TEST(CryptoFramerTest, ConstructHandshakeMessageTooManyEntries) {
152 CryptoHandshakeMessage message;
153 message.tag = 0xFFAA7733;
154 for (uint32 key = 1; key <= kMaxEntries + 1; key++) {
155 message.tag_value_map[key] = "abcdef";
156 }
157
158 CryptoFramer framer;
159 QuicData* data = NULL;
160 EXPECT_FALSE(framer.ConstructHandshakeMessage(message, &data));
161 delete data;
162 }
163
164
165 TEST(CryptoFramerTest, ConstructHandshakeMessageInvalidLength) {
166 CryptoHandshakeMessage message;
167 message.tag = 0xFFAA7733;
168 message.tag_value_map[0x12345678] = "";
169
170 CryptoFramer framer;
171 QuicData* data = NULL;
172 EXPECT_FALSE(framer.ConstructHandshakeMessage(message, &data));
173 delete data;
174 }
175
176 TEST(CryptoFramerTest, EmptyPacket) {
177 test::TestCryptoVisitor visitor;
178 CryptoFramer framer;
179 framer.set_visitor(&visitor);
180 }
181
182 TEST(CryptoFramerTest, ProcessInput) {
183 test::TestCryptoVisitor visitor;
184 CryptoFramer framer;
185 framer.set_visitor(&visitor);
186
187 unsigned char input[] = {
188 // tag
189 0x33, 0x77, 0xAA, 0xFF,
190 // num entires
191 0x02, 0x00,
192 // tag 1
193 0x78, 0x56, 0x34, 0x12,
194 // tag 2
195 0x79, 0x56, 0x34, 0x12,
196 // len 1
197 0x06, 0x00,
198 // len 2
199 0x05, 0x00,
200 // value 1
201 'a', 'b', 'c', 'd',
202 'e', 'f',
203 // value 2
204 'g', 'h', 'i', 'j',
205 'k',
206 };
207
208 EXPECT_TRUE(framer.ProcessInput(StringPiece(AsChars(input),
209 arraysize(input))));
210 ASSERT_EQ(1u, visitor.message_tags_.size());
211 EXPECT_EQ(0xFFAA7733, visitor.message_tags_[0]);
212 EXPECT_EQ(2u, visitor.message_maps_[0].size());
213 EXPECT_EQ("abcdef",visitor.message_maps_[0][0x12345678]);
214 EXPECT_EQ("ghijk", visitor.message_maps_[0][0x12345679]);
215 }
216
217 TEST(CryptoFramerTest, ProcessInputIncrementally) {
218 test::TestCryptoVisitor visitor;
219 CryptoFramer framer;
220 framer.set_visitor(&visitor);
221
222 unsigned char input[] = {
223 // tag
224 0x33, 0x77, 0xAA, 0xFF,
225 // num entires
226 0x02, 0x00,
227 // tag 1
228 0x78, 0x56, 0x34, 0x12,
229 // tag 2
230 0x79, 0x56, 0x34, 0x12,
231 // len 1
232 0x06, 0x00,
233 // len 2
234 0x05, 0x00,
235 // value 1
236 'a', 'b', 'c', 'd',
237 'e', 'f',
238 // value 2
239 'g', 'h', 'i', 'j',
240 'k',
241 };
242
243 for (size_t i = 0; i < arraysize(input); i++) {
244 EXPECT_TRUE(framer.ProcessInput(StringPiece(AsChars(input)+ i, 1)));
245 }
246 ASSERT_EQ(1u, visitor.message_tags_.size());
247 EXPECT_EQ(0xFFAA7733, visitor.message_tags_[0]);
248 EXPECT_EQ(2u, visitor.message_maps_[0].size());
249 EXPECT_EQ("abcdef",visitor.message_maps_[0][0x12345678]);
250 EXPECT_EQ("ghijk", visitor.message_maps_[0][0x12345679]);
251 }
252
253 TEST(CryptoFramerTest, ProcessInputTagsOutOfOrder) {
254 test::TestCryptoVisitor visitor;
255 CryptoFramer framer;
256 framer.set_visitor(&visitor);
257
258 unsigned char input[] = {
259 // tag
260 0x33, 0x77, 0xAA, 0xFF,
261 // num entires
262 0x02, 0x00,
263 // tag 1
264 0x79, 0x56, 0x34, 0x12,
265 // tag 2
266 0x78, 0x56, 0x34, 0x12,
267 };
268
269 EXPECT_FALSE(framer.ProcessInput(StringPiece(AsChars(input),
270 arraysize(input))));
271 EXPECT_EQ(QUIC_CRYPTO_TAGS_OUT_OF_ORDER, framer.error());
272 }
273
274 TEST(CryptoFramerTest, ProcessInputTooManyEntries) {
275 test::TestCryptoVisitor visitor;
276 CryptoFramer framer;
277 framer.set_visitor(&visitor);
278
279 unsigned char input[] = {
280 // tag
281 0x33, 0x77, 0xAA, 0xFF,
282 // num entires
283 0xA0, 0x00,
284 };
285
286 EXPECT_FALSE(framer.ProcessInput(StringPiece(AsChars(input),
287 arraysize(input))));
288 EXPECT_EQ(QUIC_CRYPTO_TOO_MANY_ENTRIES, framer.error());
289 }
290
291 TEST(CryptoFramerTest, ProcessInputInvalidLength) {
292 test::TestCryptoVisitor visitor;
293 CryptoFramer framer;
294 framer.set_visitor(&visitor);
295
296 unsigned char input[] = {
297 // tag
298 0x33, 0x77, 0xAA, 0xFF,
299 // num entires
300 0x02, 0x00,
301 // tag 1
302 0x78, 0x56, 0x34, 0x12,
303 // tag 2
304 0x79, 0x56, 0x34, 0x12,
305 // len 1
306 0x00, 0x00,
307 // len 2
308 0x05, 0x00,
309 };
310
311 EXPECT_FALSE(framer.ProcessInput(StringPiece(AsChars(input),
312 arraysize(input))));
313 EXPECT_EQ(QUIC_CRYPTO_INVALID_VALUE_LENGTH, framer.error());
314 }
315
316 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/crypto/crypto_framer.cc ('k') | net/quic/crypto/crypto_protocol.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698