OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "net/quic/crypto/quic_crypto_server_config.h" | 5 #include "net/quic/crypto/quic_crypto_server_config.h" |
6 | 6 |
7 #include <stdarg.h> | 7 #include <stdarg.h> |
8 | 8 |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "net/quic/crypto/aes_128_gcm_12_encrypter.h" | 10 #include "net/quic/crypto/aes_128_gcm_12_encrypter.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 return config->source_address_token_boxer == | 49 return config->source_address_token_boxer == |
50 &(server_config_->default_source_address_token_boxer_); | 50 &(server_config_->default_source_address_token_boxer_); |
51 } | 51 } |
52 | 52 |
53 string NewSourceAddressToken( | 53 string NewSourceAddressToken( |
54 string config_id, | 54 string config_id, |
55 IPEndPoint ip, | 55 IPEndPoint ip, |
56 QuicRandom* rand, | 56 QuicRandom* rand, |
57 QuicWallTime now) { | 57 QuicWallTime now) { |
58 return server_config_->NewSourceAddressToken( | 58 return server_config_->NewSourceAddressToken( |
59 *GetConfig(config_id), ip, rand, now, NULL); | 59 *GetConfig(config_id), ip, rand, now, nullptr); |
60 } | 60 } |
61 | 61 |
62 HandshakeFailureReason ValidateSourceAddressToken(string config_id, | 62 HandshakeFailureReason ValidateSourceAddressToken(string config_id, |
63 StringPiece srct, | 63 StringPiece srct, |
64 IPEndPoint ip, | 64 IPEndPoint ip, |
65 QuicWallTime now) { | 65 QuicWallTime now) { |
66 return server_config_->ValidateSourceAddressToken( | 66 return server_config_->ValidateSourceAddressToken( |
67 *GetConfig(config_id), srct, ip, now); | 67 *GetConfig(config_id), srct, ip, now); |
68 } | 68 } |
69 | 69 |
70 string NewServerNonce(QuicRandom* rand, QuicWallTime now) const { | 70 string NewServerNonce(QuicRandom* rand, QuicWallTime now) const { |
71 return server_config_->NewServerNonce(rand, now); | 71 return server_config_->NewServerNonce(rand, now); |
72 } | 72 } |
73 | 73 |
74 HandshakeFailureReason ValidateServerNonce(StringPiece token, | 74 HandshakeFailureReason ValidateServerNonce(StringPiece token, |
75 QuicWallTime now) { | 75 QuicWallTime now) { |
76 return server_config_->ValidateServerNonce(token, now); | 76 return server_config_->ValidateServerNonce(token, now); |
77 } | 77 } |
78 | 78 |
79 base::Lock* GetStrikeRegisterClientLock() { | 79 base::Lock* GetStrikeRegisterClientLock() { |
80 return &server_config_->strike_register_client_lock_; | 80 return &server_config_->strike_register_client_lock_; |
81 } | 81 } |
82 | 82 |
83 // CheckConfigs compares the state of the Configs in |server_config_| to the | 83 // CheckConfigs compares the state of the Configs in |server_config_| to the |
84 // description given as arguments. The arguments are given as NULL-terminated | 84 // description given as arguments. The arguments are given as |
85 // pairs. The first of each pair is the server config ID of a Config. The | 85 // nullptr-terminated pairs. The first of each pair is the server config ID of |
86 // second is a boolean describing whether the config is the primary. For | 86 // a Config. The second is a boolean describing whether the config is the |
87 // example: | 87 // primary. For example: |
88 // CheckConfigs(NULL); // checks that no Configs are loaded. | 88 // CheckConfigs(nullptr); // checks that no Configs are loaded. |
89 // | 89 // |
90 // // Checks that exactly three Configs are loaded with the given IDs and | 90 // // Checks that exactly three Configs are loaded with the given IDs and |
91 // // status. | 91 // // status. |
92 // CheckConfigs( | 92 // CheckConfigs( |
93 // "id1", false, | 93 // "id1", false, |
94 // "id2", true, | 94 // "id2", true, |
95 // "id3", false, | 95 // "id3", false, |
96 // NULL); | 96 // nullptr); |
97 void CheckConfigs(const char* server_config_id1, ...) { | 97 void CheckConfigs(const char* server_config_id1, ...) { |
98 va_list ap; | 98 va_list ap; |
99 va_start(ap, server_config_id1); | 99 va_start(ap, server_config_id1); |
100 | 100 |
101 vector<pair<ServerConfigID, bool> > expected; | 101 vector<pair<ServerConfigID, bool> > expected; |
102 bool first = true; | 102 bool first = true; |
103 for (;;) { | 103 for (;;) { |
104 const char* server_config_id; | 104 const char* server_config_id; |
105 if (first) { | 105 if (first) { |
106 server_config_id = server_config_id1; | 106 server_config_id = server_config_id1; |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 CryptoServerConfigsTest() | 365 CryptoServerConfigsTest() |
366 : rand_(QuicRandom::GetInstance()), | 366 : rand_(QuicRandom::GetInstance()), |
367 config_(QuicCryptoServerConfig::TESTING, rand_), | 367 config_(QuicCryptoServerConfig::TESTING, rand_), |
368 test_peer_(&config_) {} | 368 test_peer_(&config_) {} |
369 | 369 |
370 virtual void SetUp() { | 370 virtual void SetUp() { |
371 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1000)); | 371 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1000)); |
372 } | 372 } |
373 | 373 |
374 // SetConfigs constructs suitable config protobufs and calls SetConfigs on | 374 // SetConfigs constructs suitable config protobufs and calls SetConfigs on |
375 // |config_|. The arguments are given as NULL-terminated pairs. The first of | 375 // |config_|. The arguments are given as nullptr-terminated pairs. The first |
376 // each pair is the server config ID of a Config. The second is the | 376 // of each pair is the server config ID of a Config. The second is the |
377 // |primary_time| of that Config, given in epoch seconds. (Although note | 377 // |primary_time| of that Config, given in epoch seconds. (Although note that, |
378 // that, in these tests, time is set to 1000 seconds since the epoch.) For | 378 // in these tests, time is set to 1000 seconds since the epoch.) For example: |
379 // example: | 379 // SetConfigs(nullptr); // calls |config_.SetConfigs| with no protobufs. |
380 // SetConfigs(NULL); // calls |config_.SetConfigs| with no protobufs. | |
381 // | 380 // |
382 // // Calls |config_.SetConfigs| with two protobufs: one for a Config with | 381 // // Calls |config_.SetConfigs| with two protobufs: one for a Config with |
383 // // a |primary_time| of 900 and priority 1, and another with | 382 // // a |primary_time| of 900 and priority 1, and another with |
384 // // a |primary_time| of 1000 and priority 2. | 383 // // a |primary_time| of 1000 and priority 2. |
385 | 384 |
386 // CheckConfigs( | 385 // CheckConfigs( |
387 // "id1", 900, 1, | 386 // "id1", 900, 1, |
388 // "id2", 1000, 2, | 387 // "id2", 1000, 2, |
389 // NULL); | 388 // nullptr); |
390 // | 389 // |
391 // If the server config id starts with "INVALID" then the generated protobuf | 390 // If the server config id starts with "INVALID" then the generated protobuf |
392 // will be invalid. | 391 // will be invalid. |
393 void SetConfigs(const char* server_config_id1, ...) { | 392 void SetConfigs(const char* server_config_id1, ...) { |
394 const char kOrbit[] = "12345678"; | 393 const char kOrbit[] = "12345678"; |
395 | 394 |
396 va_list ap; | 395 va_list ap; |
397 va_start(ap, server_config_id1); | 396 va_start(ap, server_config_id1); |
398 bool has_invalid = false; | 397 bool has_invalid = false; |
399 bool is_empty = true; | 398 bool is_empty = true; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 } | 436 } |
438 | 437 |
439 protected: | 438 protected: |
440 QuicRandom* const rand_; | 439 QuicRandom* const rand_; |
441 MockClock clock_; | 440 MockClock clock_; |
442 QuicCryptoServerConfig config_; | 441 QuicCryptoServerConfig config_; |
443 QuicCryptoServerConfigPeer test_peer_; | 442 QuicCryptoServerConfigPeer test_peer_; |
444 }; | 443 }; |
445 | 444 |
446 TEST_F(CryptoServerConfigsTest, NoConfigs) { | 445 TEST_F(CryptoServerConfigsTest, NoConfigs) { |
447 test_peer_.CheckConfigs(NULL); | 446 test_peer_.CheckConfigs(nullptr); |
448 } | 447 } |
449 | 448 |
450 TEST_F(CryptoServerConfigsTest, MakePrimaryFirst) { | 449 TEST_F(CryptoServerConfigsTest, MakePrimaryFirst) { |
451 // Make sure that "b" is primary even though "a" comes first. | 450 // Make sure that "b" is primary even though "a" comes first. |
452 SetConfigs("a", 1100, 1, | 451 SetConfigs("a", 1100, 1, |
453 "b", 900, 1, | 452 "b", 900, 1, |
454 NULL); | 453 nullptr); |
455 test_peer_.CheckConfigs( | 454 test_peer_.CheckConfigs( |
456 "a", false, | 455 "a", false, |
457 "b", true, | 456 "b", true, |
458 NULL); | 457 nullptr); |
459 } | 458 } |
460 | 459 |
461 TEST_F(CryptoServerConfigsTest, MakePrimarySecond) { | 460 TEST_F(CryptoServerConfigsTest, MakePrimarySecond) { |
462 // Make sure that a remains primary after b is added. | 461 // Make sure that a remains primary after b is added. |
463 SetConfigs("a", 900, 1, | 462 SetConfigs("a", 900, 1, |
464 "b", 1100, 1, | 463 "b", 1100, 1, |
465 NULL); | 464 nullptr); |
466 test_peer_.CheckConfigs( | 465 test_peer_.CheckConfigs( |
467 "a", true, | 466 "a", true, |
468 "b", false, | 467 "b", false, |
469 NULL); | 468 nullptr); |
470 } | 469 } |
471 | 470 |
472 TEST_F(CryptoServerConfigsTest, Delete) { | 471 TEST_F(CryptoServerConfigsTest, Delete) { |
473 // Ensure that configs get deleted when removed. | 472 // Ensure that configs get deleted when removed. |
474 SetConfigs("a", 800, 1, | 473 SetConfigs("a", 800, 1, |
475 "b", 900, 1, | 474 "b", 900, 1, |
476 "c", 1100, 1, | 475 "c", 1100, 1, |
477 NULL); | 476 nullptr); |
478 test_peer_.CheckConfigs( | 477 test_peer_.CheckConfigs( |
479 "a", false, | 478 "a", false, |
480 "b", true, | 479 "b", true, |
481 "c", false, | 480 "c", false, |
482 NULL); | 481 nullptr); |
483 SetConfigs("b", 900, 1, | 482 SetConfigs("b", 900, 1, |
484 "c", 1100, 1, | 483 "c", 1100, 1, |
485 NULL); | 484 nullptr); |
486 test_peer_.CheckConfigs( | 485 test_peer_.CheckConfigs( |
487 "b", true, | 486 "b", true, |
488 "c", false, | 487 "c", false, |
489 NULL); | 488 nullptr); |
490 } | 489 } |
491 | 490 |
492 TEST_F(CryptoServerConfigsTest, DeletePrimary) { | 491 TEST_F(CryptoServerConfigsTest, DeletePrimary) { |
493 // Ensure that deleting the primary config works. | 492 // Ensure that deleting the primary config works. |
494 SetConfigs("a", 800, 1, | 493 SetConfigs("a", 800, 1, |
495 "b", 900, 1, | 494 "b", 900, 1, |
496 "c", 1100, 1, | 495 "c", 1100, 1, |
497 NULL); | 496 nullptr); |
498 test_peer_.CheckConfigs( | 497 test_peer_.CheckConfigs( |
499 "a", false, | 498 "a", false, |
500 "b", true, | 499 "b", true, |
501 "c", false, | 500 "c", false, |
502 NULL); | 501 nullptr); |
503 SetConfigs("a", 800, 1, | 502 SetConfigs("a", 800, 1, |
504 "c", 1100, 1, | 503 "c", 1100, 1, |
505 NULL); | 504 nullptr); |
506 test_peer_.CheckConfigs( | 505 test_peer_.CheckConfigs( |
507 "a", true, | 506 "a", true, |
508 "c", false, | 507 "c", false, |
509 NULL); | 508 nullptr); |
510 } | 509 } |
511 | 510 |
512 TEST_F(CryptoServerConfigsTest, FailIfDeletingAllConfigs) { | 511 TEST_F(CryptoServerConfigsTest, FailIfDeletingAllConfigs) { |
513 // Ensure that configs get deleted when removed. | 512 // Ensure that configs get deleted when removed. |
514 SetConfigs("a", 800, 1, | 513 SetConfigs("a", 800, 1, |
515 "b", 900, 1, | 514 "b", 900, 1, |
516 NULL); | 515 nullptr); |
517 test_peer_.CheckConfigs( | 516 test_peer_.CheckConfigs( |
518 "a", false, | 517 "a", false, |
519 "b", true, | 518 "b", true, |
520 NULL); | 519 nullptr); |
521 SetConfigs(NULL); | 520 SetConfigs(nullptr); |
522 // Config change is rejected, still using old configs. | 521 // Config change is rejected, still using old configs. |
523 test_peer_.CheckConfigs( | 522 test_peer_.CheckConfigs( |
524 "a", false, | 523 "a", false, |
525 "b", true, | 524 "b", true, |
526 NULL); | 525 nullptr); |
527 } | 526 } |
528 | 527 |
529 TEST_F(CryptoServerConfigsTest, ChangePrimaryTime) { | 528 TEST_F(CryptoServerConfigsTest, ChangePrimaryTime) { |
530 // Check that updates to primary time get picked up. | 529 // Check that updates to primary time get picked up. |
531 SetConfigs("a", 400, 1, | 530 SetConfigs("a", 400, 1, |
532 "b", 800, 1, | 531 "b", 800, 1, |
533 "c", 1200, 1, | 532 "c", 1200, 1, |
534 NULL); | 533 nullptr); |
535 test_peer_.SelectNewPrimaryConfig(500); | 534 test_peer_.SelectNewPrimaryConfig(500); |
536 test_peer_.CheckConfigs( | 535 test_peer_.CheckConfigs( |
537 "a", true, | 536 "a", true, |
538 "b", false, | 537 "b", false, |
539 "c", false, | 538 "c", false, |
540 NULL); | 539 nullptr); |
541 SetConfigs("a", 1200, 1, | 540 SetConfigs("a", 1200, 1, |
542 "b", 800, 1, | 541 "b", 800, 1, |
543 "c", 400, 1, | 542 "c", 400, 1, |
544 NULL); | 543 nullptr); |
545 test_peer_.SelectNewPrimaryConfig(500); | 544 test_peer_.SelectNewPrimaryConfig(500); |
546 test_peer_.CheckConfigs( | 545 test_peer_.CheckConfigs( |
547 "a", false, | 546 "a", false, |
548 "b", false, | 547 "b", false, |
549 "c", true, | 548 "c", true, |
550 NULL); | 549 nullptr); |
551 } | 550 } |
552 | 551 |
553 TEST_F(CryptoServerConfigsTest, AllConfigsInThePast) { | 552 TEST_F(CryptoServerConfigsTest, AllConfigsInThePast) { |
554 // Check that the most recent config is selected. | 553 // Check that the most recent config is selected. |
555 SetConfigs("a", 400, 1, | 554 SetConfigs("a", 400, 1, |
556 "b", 800, 1, | 555 "b", 800, 1, |
557 "c", 1200, 1, | 556 "c", 1200, 1, |
558 NULL); | 557 nullptr); |
559 test_peer_.SelectNewPrimaryConfig(1500); | 558 test_peer_.SelectNewPrimaryConfig(1500); |
560 test_peer_.CheckConfigs( | 559 test_peer_.CheckConfigs( |
561 "a", false, | 560 "a", false, |
562 "b", false, | 561 "b", false, |
563 "c", true, | 562 "c", true, |
564 NULL); | 563 nullptr); |
565 } | 564 } |
566 | 565 |
567 TEST_F(CryptoServerConfigsTest, AllConfigsInTheFuture) { | 566 TEST_F(CryptoServerConfigsTest, AllConfigsInTheFuture) { |
568 // Check that the first config is selected. | 567 // Check that the first config is selected. |
569 SetConfigs("a", 400, 1, | 568 SetConfigs("a", 400, 1, |
570 "b", 800, 1, | 569 "b", 800, 1, |
571 "c", 1200, 1, | 570 "c", 1200, 1, |
572 NULL); | 571 nullptr); |
573 test_peer_.SelectNewPrimaryConfig(100); | 572 test_peer_.SelectNewPrimaryConfig(100); |
574 test_peer_.CheckConfigs( | 573 test_peer_.CheckConfigs( |
575 "a", true, | 574 "a", true, |
576 "b", false, | 575 "b", false, |
577 "c", false, | 576 "c", false, |
578 NULL); | 577 nullptr); |
579 } | 578 } |
580 | 579 |
581 TEST_F(CryptoServerConfigsTest, SortByPriority) { | 580 TEST_F(CryptoServerConfigsTest, SortByPriority) { |
582 // Check that priority is used to decide on a primary config when | 581 // Check that priority is used to decide on a primary config when |
583 // configs have the same primary time. | 582 // configs have the same primary time. |
584 SetConfigs("a", 900, 1, | 583 SetConfigs("a", 900, 1, |
585 "b", 900, 2, | 584 "b", 900, 2, |
586 "c", 900, 3, | 585 "c", 900, 3, |
587 NULL); | 586 nullptr); |
588 test_peer_.CheckConfigs( | 587 test_peer_.CheckConfigs( |
589 "a", true, | 588 "a", true, |
590 "b", false, | 589 "b", false, |
591 "c", false, | 590 "c", false, |
592 NULL); | 591 nullptr); |
593 test_peer_.SelectNewPrimaryConfig(800); | 592 test_peer_.SelectNewPrimaryConfig(800); |
594 test_peer_.CheckConfigs( | 593 test_peer_.CheckConfigs( |
595 "a", true, | 594 "a", true, |
596 "b", false, | 595 "b", false, |
597 "c", false, | 596 "c", false, |
598 NULL); | 597 nullptr); |
599 test_peer_.SelectNewPrimaryConfig(1000); | 598 test_peer_.SelectNewPrimaryConfig(1000); |
600 test_peer_.CheckConfigs( | 599 test_peer_.CheckConfigs( |
601 "a", true, | 600 "a", true, |
602 "b", false, | 601 "b", false, |
603 "c", false, | 602 "c", false, |
604 NULL); | 603 nullptr); |
605 | 604 |
606 // Change priorities and expect sort order to change. | 605 // Change priorities and expect sort order to change. |
607 SetConfigs("a", 900, 2, | 606 SetConfigs("a", 900, 2, |
608 "b", 900, 1, | 607 "b", 900, 1, |
609 "c", 900, 0, | 608 "c", 900, 0, |
610 NULL); | 609 nullptr); |
611 test_peer_.CheckConfigs( | 610 test_peer_.CheckConfigs( |
612 "a", false, | 611 "a", false, |
613 "b", false, | 612 "b", false, |
614 "c", true, | 613 "c", true, |
615 NULL); | 614 nullptr); |
616 test_peer_.SelectNewPrimaryConfig(800); | 615 test_peer_.SelectNewPrimaryConfig(800); |
617 test_peer_.CheckConfigs( | 616 test_peer_.CheckConfigs( |
618 "a", false, | 617 "a", false, |
619 "b", false, | 618 "b", false, |
620 "c", true, | 619 "c", true, |
621 NULL); | 620 nullptr); |
622 test_peer_.SelectNewPrimaryConfig(1000); | 621 test_peer_.SelectNewPrimaryConfig(1000); |
623 test_peer_.CheckConfigs( | 622 test_peer_.CheckConfigs( |
624 "a", false, | 623 "a", false, |
625 "b", false, | 624 "b", false, |
626 "c", true, | 625 "c", true, |
627 NULL); | 626 nullptr); |
628 } | 627 } |
629 | 628 |
630 TEST_F(CryptoServerConfigsTest, AdvancePrimary) { | 629 TEST_F(CryptoServerConfigsTest, AdvancePrimary) { |
631 // Check that a new primary config is enabled at the right time. | 630 // Check that a new primary config is enabled at the right time. |
632 SetConfigs("a", 900, 1, | 631 SetConfigs("a", 900, 1, |
633 "b", 1100, 1, | 632 "b", 1100, 1, |
634 NULL); | 633 nullptr); |
635 test_peer_.SelectNewPrimaryConfig(1000); | 634 test_peer_.SelectNewPrimaryConfig(1000); |
636 test_peer_.CheckConfigs( | 635 test_peer_.CheckConfigs( |
637 "a", true, | 636 "a", true, |
638 "b", false, | 637 "b", false, |
639 NULL); | 638 nullptr); |
640 test_peer_.SelectNewPrimaryConfig(1101); | 639 test_peer_.SelectNewPrimaryConfig(1101); |
641 test_peer_.CheckConfigs( | 640 test_peer_.CheckConfigs( |
642 "a", false, | 641 "a", false, |
643 "b", true, | 642 "b", true, |
644 NULL); | 643 nullptr); |
645 } | 644 } |
646 | 645 |
647 TEST_F(CryptoServerConfigsTest, InvalidConfigs) { | 646 TEST_F(CryptoServerConfigsTest, InvalidConfigs) { |
648 // Ensure that invalid configs don't change anything. | 647 // Ensure that invalid configs don't change anything. |
649 SetConfigs("a", 800, 1, | 648 SetConfigs("a", 800, 1, |
650 "b", 900, 1, | 649 "b", 900, 1, |
651 "c", 1100, 1, | 650 "c", 1100, 1, |
652 NULL); | 651 nullptr); |
653 test_peer_.CheckConfigs( | 652 test_peer_.CheckConfigs( |
654 "a", false, | 653 "a", false, |
655 "b", true, | 654 "b", true, |
656 "c", false, | 655 "c", false, |
657 NULL); | 656 nullptr); |
658 SetConfigs("a", 800, 1, | 657 SetConfigs("a", 800, 1, |
659 "c", 1100, 1, | 658 "c", 1100, 1, |
660 "INVALID1", 1000, 1, | 659 "INVALID1", 1000, 1, |
661 NULL); | 660 nullptr); |
662 test_peer_.CheckConfigs( | 661 test_peer_.CheckConfigs( |
663 "a", false, | 662 "a", false, |
664 "b", true, | 663 "b", true, |
665 "c", false, | 664 "c", false, |
666 NULL); | 665 nullptr); |
667 } | 666 } |
668 | 667 |
669 } // namespace test | 668 } // namespace test |
670 } // namespace net | 669 } // namespace net |
OLD | NEW |