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