| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/quic/crypto/local_strike_register_client.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/strings/string_piece.h" | |
| 11 #include "base/sys_byteorder.h" | |
| 12 #include "net/quic/crypto/crypto_protocol.h" | |
| 13 #include "net/quic/quic_time.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 | |
| 16 using base::StringPiece; | |
| 17 using std::string; | |
| 18 | |
| 19 namespace net { | |
| 20 namespace test { | |
| 21 namespace { | |
| 22 | |
| 23 class RecordResultCallback : public StrikeRegisterClient::ResultCallback { | |
| 24 public: | |
| 25 // RecordResultCallback stores the argument to RunImpl in | |
| 26 // |*saved_value| and sets |*called| to true. The callback is self | |
| 27 // deleting. | |
| 28 RecordResultCallback(bool* called, | |
| 29 bool* saved_value, | |
| 30 InsertStatus* saved_nonce_error) | |
| 31 : called_(called), | |
| 32 saved_value_(saved_value), | |
| 33 saved_nonce_error_(saved_nonce_error) { | |
| 34 *called_ = false; | |
| 35 } | |
| 36 | |
| 37 protected: | |
| 38 void RunImpl(bool nonce_is_valid_and_unique, | |
| 39 InsertStatus nonce_error) override { | |
| 40 *called_ = true; | |
| 41 *saved_value_ = nonce_is_valid_and_unique; | |
| 42 *saved_nonce_error_ = nonce_error; | |
| 43 } | |
| 44 | |
| 45 private: | |
| 46 bool* called_; | |
| 47 bool* saved_value_; | |
| 48 InsertStatus* saved_nonce_error_; | |
| 49 | |
| 50 DISALLOW_COPY_AND_ASSIGN(RecordResultCallback); | |
| 51 }; | |
| 52 | |
| 53 const uint8 kOrbit[] = "\x12\x34\x56\x78\x9A\xBC\xDE\xF0"; | |
| 54 const uint32 kCurrentTimeExternalSecs = 12345678; | |
| 55 size_t kMaxEntries = 100; | |
| 56 uint32 kWindowSecs = 60; | |
| 57 | |
| 58 class LocalStrikeRegisterClientTest : public ::testing::Test { | |
| 59 protected: | |
| 60 LocalStrikeRegisterClientTest() { | |
| 61 } | |
| 62 | |
| 63 void SetUp() override { | |
| 64 strike_register_.reset(new LocalStrikeRegisterClient( | |
| 65 kMaxEntries, kCurrentTimeExternalSecs, kWindowSecs, kOrbit, | |
| 66 net::StrikeRegister::NO_STARTUP_PERIOD_NEEDED)); | |
| 67 } | |
| 68 | |
| 69 scoped_ptr<LocalStrikeRegisterClient> strike_register_; | |
| 70 }; | |
| 71 | |
| 72 TEST_F(LocalStrikeRegisterClientTest, CheckOrbit) { | |
| 73 EXPECT_TRUE(strike_register_->IsKnownOrbit( | |
| 74 StringPiece(reinterpret_cast<const char*>(kOrbit), kOrbitSize))); | |
| 75 EXPECT_FALSE(strike_register_->IsKnownOrbit( | |
| 76 StringPiece(reinterpret_cast<const char*>(kOrbit), kOrbitSize - 1))); | |
| 77 EXPECT_FALSE(strike_register_->IsKnownOrbit( | |
| 78 StringPiece(reinterpret_cast<const char*>(kOrbit), kOrbitSize + 1))); | |
| 79 EXPECT_FALSE(strike_register_->IsKnownOrbit( | |
| 80 StringPiece(reinterpret_cast<const char*>(kOrbit) + 1, kOrbitSize))); | |
| 81 } | |
| 82 | |
| 83 TEST_F(LocalStrikeRegisterClientTest, IncorrectNonceLength) { | |
| 84 string valid_nonce; | |
| 85 uint32 norder = htonl(kCurrentTimeExternalSecs); | |
| 86 valid_nonce.assign(reinterpret_cast<const char*>(&norder), sizeof(norder)); | |
| 87 valid_nonce.append(string(reinterpret_cast<const char*>(kOrbit), kOrbitSize)); | |
| 88 valid_nonce.append(string(20, '\x17')); // 20 'random' bytes. | |
| 89 | |
| 90 { | |
| 91 // Validation fails if you remove a byte from the nonce. | |
| 92 bool called = false; | |
| 93 bool is_valid = false; | |
| 94 InsertStatus nonce_error = NONCE_UNKNOWN_FAILURE; | |
| 95 string short_nonce = valid_nonce.substr(0, valid_nonce.length() - 1); | |
| 96 strike_register_->VerifyNonceIsValidAndUnique( | |
| 97 short_nonce, | |
| 98 QuicWallTime::FromUNIXSeconds(kCurrentTimeExternalSecs), | |
| 99 new RecordResultCallback(&called, &is_valid, &nonce_error)); | |
| 100 EXPECT_TRUE(called); | |
| 101 EXPECT_FALSE(is_valid); | |
| 102 EXPECT_EQ(NONCE_INVALID_FAILURE, nonce_error); | |
| 103 } | |
| 104 | |
| 105 { | |
| 106 // Validation fails if you add a byte to the nonce. | |
| 107 bool called = false; | |
| 108 bool is_valid = false; | |
| 109 InsertStatus nonce_error = NONCE_UNKNOWN_FAILURE; | |
| 110 string long_nonce(valid_nonce); | |
| 111 long_nonce.append("a"); | |
| 112 strike_register_->VerifyNonceIsValidAndUnique( | |
| 113 long_nonce, | |
| 114 QuicWallTime::FromUNIXSeconds(kCurrentTimeExternalSecs), | |
| 115 new RecordResultCallback(&called, &is_valid, &nonce_error)); | |
| 116 EXPECT_TRUE(called); | |
| 117 EXPECT_FALSE(is_valid); | |
| 118 EXPECT_EQ(NONCE_INVALID_FAILURE, nonce_error); | |
| 119 } | |
| 120 | |
| 121 { | |
| 122 // Verify that the base nonce validates was valid. | |
| 123 bool called = false; | |
| 124 bool is_valid = false; | |
| 125 InsertStatus nonce_error = NONCE_UNKNOWN_FAILURE; | |
| 126 strike_register_->VerifyNonceIsValidAndUnique( | |
| 127 valid_nonce, | |
| 128 QuicWallTime::FromUNIXSeconds(kCurrentTimeExternalSecs), | |
| 129 new RecordResultCallback(&called, &is_valid, &nonce_error)); | |
| 130 EXPECT_TRUE(called); | |
| 131 EXPECT_TRUE(is_valid); | |
| 132 EXPECT_EQ(NONCE_OK, nonce_error); | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 } // namespace | |
| 137 } // namespace test | |
| 138 } // namespace net | |
| OLD | NEW |