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

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

Issue 14287009: Land Recent QUIC Changes (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge with Tot Created 7 years, 7 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/congestion_control/tcp_cubic_sender.cc ('k') | net/quic/crypto/crypto_handshake.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 <map> 5 #include <map>
6 #include <vector> 6 #include <vector>
7 7
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "net/quic/crypto/crypto_framer.h" 10 #include "net/quic/crypto/crypto_framer.h"
11 #include "net/quic/crypto/crypto_handshake.h" 11 #include "net/quic/crypto/crypto_handshake.h"
12 #include "net/quic/crypto/crypto_protocol.h" 12 #include "net/quic/crypto/crypto_protocol.h"
13 #include "net/quic/quic_protocol.h"
13 #include "net/quic/test_tools/crypto_test_utils.h" 14 #include "net/quic/test_tools/crypto_test_utils.h"
14 #include "net/quic/test_tools/quic_test_utils.h" 15 #include "net/quic/test_tools/quic_test_utils.h"
15 16
16 using base::StringPiece; 17 using base::StringPiece;
17 using std::map; 18 using std::map;
18 using std::string; 19 using std::string;
19 using std::vector; 20 using std::vector;
20 21
21 namespace net { 22 namespace net {
22 23
23 namespace { 24 namespace {
24 25
25 char* AsChars(unsigned char* data) { 26 char* AsChars(unsigned char* data) { return reinterpret_cast<char*>(data); }
26 return reinterpret_cast<char*>(data);
27 }
28 27
29 } // namespace 28 } // namespace
30 29
31 namespace test { 30 namespace test {
32 31
33 class TestCryptoVisitor : public ::net::CryptoFramerVisitorInterface { 32 class TestCryptoVisitor : public ::net::CryptoFramerVisitorInterface {
34 public: 33 public:
35 TestCryptoVisitor() 34 TestCryptoVisitor() : error_count_(0) {}
36 : error_count_(0) {
37 }
38 35
39 virtual void OnError(CryptoFramer* framer) OVERRIDE { 36 virtual void OnError(CryptoFramer* framer) OVERRIDE {
40 DLOG(ERROR) << "CryptoFramer Error: " << framer->error(); 37 DLOG(ERROR) << "CryptoFramer Error: " << framer->error();
41 ++error_count_; 38 ++error_count_;
42 } 39 }
43 40
44 virtual void OnHandshakeMessage( 41 virtual void OnHandshakeMessage(
45 const CryptoHandshakeMessage& message) OVERRIDE { 42 const CryptoHandshakeMessage& message) OVERRIDE {
46 messages_.push_back(message); 43 messages_.push_back(message);
47 } 44 }
48 45
49 // Counters from the visitor callbacks. 46 // Counters from the visitor callbacks.
50 int error_count_; 47 int error_count_;
51 48
52 vector<CryptoHandshakeMessage> messages_; 49 vector<CryptoHandshakeMessage> messages_;
53 }; 50 };
54 51
55 TEST(CryptoFramerTest, MakeCryptoTag) { 52 TEST(CryptoFramerTest, MakeCryptoTag) {
56 CryptoTag tag = MAKE_TAG('A', 'B', 'C', 'D'); 53 CryptoTag tag = MakeQuicTag('A', 'B', 'C', 'D');
57 char bytes[4]; 54 char bytes[4];
58 memcpy(bytes, &tag, 4); 55 memcpy(bytes, &tag, 4);
59 EXPECT_EQ('A', bytes[0]); 56 EXPECT_EQ('A', bytes[0]);
60 EXPECT_EQ('B', bytes[1]); 57 EXPECT_EQ('B', bytes[1]);
61 EXPECT_EQ('C', bytes[2]); 58 EXPECT_EQ('C', bytes[2]);
62 EXPECT_EQ('D', bytes[3]); 59 EXPECT_EQ('D', bytes[3]);
63 } 60 }
64 61
65 TEST(CryptoFramerTest, ConstructHandshakeMessage) { 62 TEST(CryptoFramerTest, ConstructHandshakeMessage) {
66 CryptoHandshakeMessage message; 63 CryptoHandshakeMessage message;
(...skipping 28 matching lines...) Expand all
95 'g', 'h', 'i', 'j', 92 'g', 'h', 'i', 'j',
96 'k', 93 'k',
97 // value 3 94 // value 3
98 'l', 'm', 'n', 'o', 95 'l', 'm', 'n', 'o',
99 'p', 'q', 'r', 96 'p', 'q', 'r',
100 }; 97 };
101 98
102 CryptoFramer framer; 99 CryptoFramer framer;
103 scoped_ptr<QuicData> data(framer.ConstructHandshakeMessage(message)); 100 scoped_ptr<QuicData> data(framer.ConstructHandshakeMessage(message));
104 ASSERT_TRUE(data.get() != NULL); 101 ASSERT_TRUE(data.get() != NULL);
105 test::CompareCharArraysWithHexError("constructed packet", 102 test::CompareCharArraysWithHexError("constructed packet", data->data(),
106 data->data(), data->length(), 103 data->length(), AsChars(packet),
107 AsChars(packet), arraysize(packet)); 104 arraysize(packet));
108 } 105 }
109 106
110 TEST(CryptoFramerTest, ConstructHandshakeMessageWithTwoKeys) { 107 TEST(CryptoFramerTest, ConstructHandshakeMessageWithTwoKeys) {
111 CryptoHandshakeMessage message; 108 CryptoHandshakeMessage message;
112 message.set_tag(0xFFAA7733); 109 message.set_tag(0xFFAA7733);
113 message.SetStringPiece(0x12345678, "abcdef"); 110 message.SetStringPiece(0x12345678, "abcdef");
114 message.SetStringPiece(0x12345679, "ghijk"); 111 message.SetStringPiece(0x12345679, "ghijk");
115 112
116 unsigned char packet[] = { 113 unsigned char packet[] = {
117 // tag 114 // tag
(...skipping 13 matching lines...) Expand all
131 'e', 'f', 128 'e', 'f',
132 // value 2 129 // value 2
133 'g', 'h', 'i', 'j', 130 'g', 'h', 'i', 'j',
134 'k', 131 'k',
135 }; 132 };
136 133
137 CryptoFramer framer; 134 CryptoFramer framer;
138 scoped_ptr<QuicData> data(framer.ConstructHandshakeMessage(message)); 135 scoped_ptr<QuicData> data(framer.ConstructHandshakeMessage(message));
139 ASSERT_TRUE(data.get() != NULL); 136 ASSERT_TRUE(data.get() != NULL);
140 137
141 test::CompareCharArraysWithHexError("constructed packet", 138 test::CompareCharArraysWithHexError("constructed packet", data->data(),
142 data->data(), data->length(), 139 data->length(), AsChars(packet),
143 AsChars(packet), arraysize(packet)); 140 arraysize(packet));
144 } 141 }
145 142
146 TEST(CryptoFramerTest, ConstructHandshakeMessageZeroLength) { 143 TEST(CryptoFramerTest, ConstructHandshakeMessageZeroLength) {
147 CryptoHandshakeMessage message; 144 CryptoHandshakeMessage message;
148 message.set_tag(0xFFAA7733); 145 message.set_tag(0xFFAA7733);
149 message.SetStringPiece(0x12345678, ""); 146 message.SetStringPiece(0x12345678, "");
150 147
151 unsigned char packet[] = { 148 unsigned char packet[] = {
152 // tag 149 // tag
153 0x33, 0x77, 0xAA, 0xFF, 150 0x33, 0x77, 0xAA, 0xFF,
154 // num entries 151 // num entries
155 0x01, 0x00, 152 0x01, 0x00,
156 // tag 1 153 // tag 1
157 0x78, 0x56, 0x34, 0x12, 154 0x78, 0x56, 0x34, 0x12,
158 // len 1 155 // len 1
159 0x00, 0x00, 156 0x00, 0x00,
160 // padding 157 // padding
161 0x00, 0x00, 158 0x00, 0x00,
162 }; 159 };
163 160
164 CryptoFramer framer; 161 CryptoFramer framer;
165 scoped_ptr<QuicData> data(framer.ConstructHandshakeMessage(message)); 162 scoped_ptr<QuicData> data(framer.ConstructHandshakeMessage(message));
166 ASSERT_TRUE(data.get() != NULL); 163 ASSERT_TRUE(data.get() != NULL);
167 164
168 test::CompareCharArraysWithHexError("constructed packet", 165 test::CompareCharArraysWithHexError("constructed packet", data->data(),
169 data->data(), data->length(), 166 data->length(), AsChars(packet),
170 AsChars(packet), arraysize(packet)); 167 arraysize(packet));
171 } 168 }
172 169
173 TEST(CryptoFramerTest, ConstructHandshakeMessageTooManyEntries) { 170 TEST(CryptoFramerTest, ConstructHandshakeMessageTooManyEntries) {
174 CryptoHandshakeMessage message; 171 CryptoHandshakeMessage message;
175 message.set_tag(0xFFAA7733); 172 message.set_tag(0xFFAA7733);
176 for (uint32 key = 1; key <= kMaxEntries + 1; ++key) { 173 for (uint32 key = 1; key <= kMaxEntries + 1; ++key) {
177 message.SetStringPiece(key, "abcdef"); 174 message.SetStringPiece(key, "abcdef");
178 } 175 }
179 176
180 CryptoFramer framer; 177 CryptoFramer framer;
(...skipping 20 matching lines...) Expand all
201 // len 2 198 // len 2
202 0x05, 0x00, 199 0x05, 0x00,
203 // value 1 200 // value 1
204 'a', 'b', 'c', 'd', 201 'a', 'b', 'c', 'd',
205 'e', 'f', 202 'e', 'f',
206 // value 2 203 // value 2
207 'g', 'h', 'i', 'j', 204 'g', 'h', 'i', 'j',
208 'k', 205 'k',
209 }; 206 };
210 207
211 EXPECT_TRUE(framer.ProcessInput(StringPiece(AsChars(input), 208 EXPECT_TRUE(
212 arraysize(input)))); 209 framer.ProcessInput(StringPiece(AsChars(input), arraysize(input))));
213 EXPECT_EQ(0u, framer.InputBytesRemaining()); 210 EXPECT_EQ(0u, framer.InputBytesRemaining());
214 ASSERT_EQ(1u, visitor.messages_.size()); 211 ASSERT_EQ(1u, visitor.messages_.size());
215 const CryptoHandshakeMessage& message = visitor.messages_[0]; 212 const CryptoHandshakeMessage& message = visitor.messages_[0];
216 EXPECT_EQ(0xFFAA7733, message.tag()); 213 EXPECT_EQ(0xFFAA7733, message.tag());
217 EXPECT_EQ(2u, message.tag_value_map().size()); 214 EXPECT_EQ(2u, message.tag_value_map().size());
218 EXPECT_EQ("abcdef", CryptoTestUtils::GetValueForTag(message, 0x12345678)); 215 EXPECT_EQ("abcdef", CryptoTestUtils::GetValueForTag(message, 0x12345678));
219 EXPECT_EQ("ghijk", CryptoTestUtils::GetValueForTag(message, 0x12345679)); 216 EXPECT_EQ("ghijk", CryptoTestUtils::GetValueForTag(message, 0x12345679));
220 } 217 }
221 218
222 TEST(CryptoFramerTest, ProcessInputWithThreeKeys) { 219 TEST(CryptoFramerTest, ProcessInputWithThreeKeys) {
(...skipping 24 matching lines...) Expand all
247 'a', 'b', 'c', 'd', 244 'a', 'b', 'c', 'd',
248 'e', 'f', 245 'e', 'f',
249 // value 2 246 // value 2
250 'g', 'h', 'i', 'j', 247 'g', 'h', 'i', 'j',
251 'k', 248 'k',
252 // value 3 249 // value 3
253 'l', 'm', 'n', 'o', 250 'l', 'm', 'n', 'o',
254 'p', 'q', 'r', 251 'p', 'q', 'r',
255 }; 252 };
256 253
257 EXPECT_TRUE(framer.ProcessInput(StringPiece(AsChars(input), 254 EXPECT_TRUE(
258 arraysize(input)))); 255 framer.ProcessInput(StringPiece(AsChars(input), arraysize(input))));
259 EXPECT_EQ(0u, framer.InputBytesRemaining()); 256 EXPECT_EQ(0u, framer.InputBytesRemaining());
260 ASSERT_EQ(1u, visitor.messages_.size()); 257 ASSERT_EQ(1u, visitor.messages_.size());
261 const CryptoHandshakeMessage& message = visitor.messages_[0]; 258 const CryptoHandshakeMessage& message = visitor.messages_[0];
262 EXPECT_EQ(0xFFAA7733, message.tag()); 259 EXPECT_EQ(0xFFAA7733, message.tag());
263 EXPECT_EQ(3u, message.tag_value_map().size()); 260 EXPECT_EQ(3u, message.tag_value_map().size());
264 EXPECT_EQ("abcdef", CryptoTestUtils::GetValueForTag(message, 0x12345678)); 261 EXPECT_EQ("abcdef", CryptoTestUtils::GetValueForTag(message, 0x12345678));
265 EXPECT_EQ("ghijk", CryptoTestUtils::GetValueForTag(message, 0x12345679)); 262 EXPECT_EQ("ghijk", CryptoTestUtils::GetValueForTag(message, 0x12345679));
266 EXPECT_EQ("lmnopqr", CryptoTestUtils::GetValueForTag(message, 0x1234567A)); 263 EXPECT_EQ("lmnopqr", CryptoTestUtils::GetValueForTag(message, 0x1234567A));
267 } 264 }
268 265
(...skipping 17 matching lines...) Expand all
286 0x05, 0x00, 283 0x05, 0x00,
287 // value 1 284 // value 1
288 'a', 'b', 'c', 'd', 285 'a', 'b', 'c', 'd',
289 'e', 'f', 286 'e', 'f',
290 // value 2 287 // value 2
291 'g', 'h', 'i', 'j', 288 'g', 'h', 'i', 'j',
292 'k', 289 'k',
293 }; 290 };
294 291
295 for (size_t i = 0; i < arraysize(input); i++) { 292 for (size_t i = 0; i < arraysize(input); i++) {
296 EXPECT_TRUE(framer.ProcessInput(StringPiece(AsChars(input)+ i, 1))); 293 EXPECT_TRUE(framer.ProcessInput(StringPiece(AsChars(input) + i, 1)));
297 } 294 }
298 EXPECT_EQ(0u, framer.InputBytesRemaining()); 295 EXPECT_EQ(0u, framer.InputBytesRemaining());
299 ASSERT_EQ(1u, visitor.messages_.size()); 296 ASSERT_EQ(1u, visitor.messages_.size());
300 const CryptoHandshakeMessage& message = visitor.messages_[0]; 297 const CryptoHandshakeMessage& message = visitor.messages_[0];
301 EXPECT_EQ(0xFFAA7733, message.tag()); 298 EXPECT_EQ(0xFFAA7733, message.tag());
302 EXPECT_EQ(2u, message.tag_value_map().size()); 299 EXPECT_EQ(2u, message.tag_value_map().size());
303 EXPECT_EQ("abcdef", CryptoTestUtils::GetValueForTag(message, 0x12345678)); 300 EXPECT_EQ("abcdef", CryptoTestUtils::GetValueForTag(message, 0x12345678));
304 EXPECT_EQ("ghijk", CryptoTestUtils::GetValueForTag(message, 0x12345679)); 301 EXPECT_EQ("ghijk", CryptoTestUtils::GetValueForTag(message, 0x12345679));
305 } 302 }
306 303
307 TEST(CryptoFramerTest, ProcessInputTagsOutOfOrder) { 304 TEST(CryptoFramerTest, ProcessInputTagsOutOfOrder) {
308 test::TestCryptoVisitor visitor; 305 test::TestCryptoVisitor visitor;
309 CryptoFramer framer; 306 CryptoFramer framer;
310 framer.set_visitor(&visitor); 307 framer.set_visitor(&visitor);
311 308
312 unsigned char input[] = { 309 unsigned char input[] = {
313 // tag 310 // tag
314 0x33, 0x77, 0xAA, 0xFF, 311 0x33, 0x77, 0xAA, 0xFF,
315 // num entries 312 // num entries
316 0x02, 0x00, 313 0x02, 0x00,
317 // tag 1 314 // tag 1
318 0x78, 0x56, 0x34, 0x13, 315 0x78, 0x56, 0x34, 0x13,
319 // tag 2 316 // tag 2
320 0x79, 0x56, 0x34, 0x12, 317 0x79, 0x56, 0x34, 0x12,
321 }; 318 };
322 319
323 EXPECT_FALSE(framer.ProcessInput(StringPiece(AsChars(input), 320 EXPECT_FALSE(
324 arraysize(input)))); 321 framer.ProcessInput(StringPiece(AsChars(input), arraysize(input))));
325 EXPECT_EQ(QUIC_CRYPTO_TAGS_OUT_OF_ORDER, framer.error()); 322 EXPECT_EQ(QUIC_CRYPTO_TAGS_OUT_OF_ORDER, framer.error());
326 } 323 }
327 324
328 TEST(CryptoFramerTest, ProcessInputTooManyEntries) { 325 TEST(CryptoFramerTest, ProcessInputTooManyEntries) {
329 test::TestCryptoVisitor visitor; 326 test::TestCryptoVisitor visitor;
330 CryptoFramer framer; 327 CryptoFramer framer;
331 framer.set_visitor(&visitor); 328 framer.set_visitor(&visitor);
332 329
333 unsigned char input[] = { 330 unsigned char input[] = {
334 // tag 331 // tag
335 0x33, 0x77, 0xAA, 0xFF, 332 0x33, 0x77, 0xAA, 0xFF,
336 // num entries 333 // num entries
337 0xA0, 0x00, 334 0xA0, 0x00,
338 }; 335 };
339 336
340 EXPECT_FALSE(framer.ProcessInput(StringPiece(AsChars(input), 337 EXPECT_FALSE(
341 arraysize(input)))); 338 framer.ProcessInput(StringPiece(AsChars(input), arraysize(input))));
342 EXPECT_EQ(QUIC_CRYPTO_TOO_MANY_ENTRIES, framer.error()); 339 EXPECT_EQ(QUIC_CRYPTO_TOO_MANY_ENTRIES, framer.error());
343 } 340 }
344 341
345 TEST(CryptoFramerTest, ProcessInputZeroLength) { 342 TEST(CryptoFramerTest, ProcessInputZeroLength) {
346 test::TestCryptoVisitor visitor; 343 test::TestCryptoVisitor visitor;
347 CryptoFramer framer; 344 CryptoFramer framer;
348 framer.set_visitor(&visitor); 345 framer.set_visitor(&visitor);
349 346
350 unsigned char input[] = { 347 unsigned char input[] = {
351 // tag 348 // tag
352 0x33, 0x77, 0xAA, 0xFF, 349 0x33, 0x77, 0xAA, 0xFF,
353 // num entries 350 // num entries
354 0x02, 0x00, 351 0x02, 0x00,
355 // tag 1 352 // tag 1
356 0x78, 0x56, 0x34, 0x12, 353 0x78, 0x56, 0x34, 0x12,
357 // tag 2 354 // tag 2
358 0x79, 0x56, 0x34, 0x12, 355 0x79, 0x56, 0x34, 0x12,
359 // len 1 356 // len 1
360 0x00, 0x00, 357 0x00, 0x00,
361 // len 2 358 // len 2
362 0x05, 0x00, 359 0x05, 0x00,
363 }; 360 };
364 361
365 EXPECT_TRUE(framer.ProcessInput(StringPiece(AsChars(input), 362 EXPECT_TRUE(
366 arraysize(input)))); 363 framer.ProcessInput(StringPiece(AsChars(input), arraysize(input))));
367 } 364 }
368 365
369 TEST(CryptoFramerTest, ProcessInputInvalidLengthPadding) { 366 TEST(CryptoFramerTest, ProcessInputInvalidLengthPadding) {
370 test::TestCryptoVisitor visitor; 367 test::TestCryptoVisitor visitor;
371 CryptoFramer framer; 368 CryptoFramer framer;
372 framer.set_visitor(&visitor); 369 framer.set_visitor(&visitor);
373 370
374 unsigned char input[] = { 371 unsigned char input[] = {
375 // tag 372 // tag
376 0x33, 0x77, 0xAA, 0xFF, 373 0x33, 0x77, 0xAA, 0xFF,
377 // num entries 374 // num entries
378 0x01, 0x00, 375 0x01, 0x00,
379 // tag 1 376 // tag 1
380 0x78, 0x56, 0x34, 0x12, 377 0x78, 0x56, 0x34, 0x12,
381 // len 1 378 // len 1
382 0x05, 0x00, 379 0x05, 0x00,
383 // padding 380 // padding
384 0x05, 0x00, 381 0x05, 0x00,
385 }; 382 };
386 383
387 EXPECT_FALSE(framer.ProcessInput(StringPiece(AsChars(input), 384 EXPECT_FALSE(
388 arraysize(input)))); 385 framer.ProcessInput(StringPiece(AsChars(input), arraysize(input))));
389 EXPECT_EQ(QUIC_CRYPTO_INVALID_VALUE_LENGTH, framer.error()); 386 EXPECT_EQ(QUIC_CRYPTO_INVALID_VALUE_LENGTH, framer.error());
390 } 387 }
391 388
392 } // namespace test 389 } // namespace test
393 390
394 } // namespace net 391 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/congestion_control/tcp_cubic_sender.cc ('k') | net/quic/crypto/crypto_handshake.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698