| 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/macros.h" |
| 9 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/strings/string_piece.h" | 11 #include "base/strings/string_piece.h" |
| 11 #include "base/sys_byteorder.h" | 12 #include "base/sys_byteorder.h" |
| 12 #include "net/quic/crypto/crypto_protocol.h" | 13 #include "net/quic/crypto/crypto_protocol.h" |
| 13 #include "net/quic/quic_time.h" | 14 #include "net/quic/quic_time.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 using base::StringPiece; | 17 using base::StringPiece; |
| 17 using std::string; | 18 using std::string; |
| 18 | 19 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 43 } | 44 } |
| 44 | 45 |
| 45 private: | 46 private: |
| 46 bool* called_; | 47 bool* called_; |
| 47 bool* saved_value_; | 48 bool* saved_value_; |
| 48 InsertStatus* saved_nonce_error_; | 49 InsertStatus* saved_nonce_error_; |
| 49 | 50 |
| 50 DISALLOW_COPY_AND_ASSIGN(RecordResultCallback); | 51 DISALLOW_COPY_AND_ASSIGN(RecordResultCallback); |
| 51 }; | 52 }; |
| 52 | 53 |
| 53 const uint8 kOrbit[] = "\x12\x34\x56\x78\x9A\xBC\xDE\xF0"; | 54 const uint8_t kOrbit[] = "\x12\x34\x56\x78\x9A\xBC\xDE\xF0"; |
| 54 const uint32 kCurrentTimeExternalSecs = 12345678; | 55 const uint32_t kCurrentTimeExternalSecs = 12345678; |
| 55 size_t kMaxEntries = 100; | 56 size_t kMaxEntries = 100; |
| 56 uint32 kWindowSecs = 60; | 57 uint32_t kWindowSecs = 60; |
| 57 | 58 |
| 58 class LocalStrikeRegisterClientTest : public ::testing::Test { | 59 class LocalStrikeRegisterClientTest : public ::testing::Test { |
| 59 protected: | 60 protected: |
| 60 LocalStrikeRegisterClientTest() {} | 61 LocalStrikeRegisterClientTest() {} |
| 61 | 62 |
| 62 void SetUp() override { | 63 void SetUp() override { |
| 63 strike_register_.reset(new LocalStrikeRegisterClient( | 64 strike_register_.reset(new LocalStrikeRegisterClient( |
| 64 kMaxEntries, kCurrentTimeExternalSecs, kWindowSecs, kOrbit, | 65 kMaxEntries, kCurrentTimeExternalSecs, kWindowSecs, kOrbit, |
| 65 StrikeRegister::NO_STARTUP_PERIOD_NEEDED)); | 66 StrikeRegister::NO_STARTUP_PERIOD_NEEDED)); |
| 66 } | 67 } |
| 67 | 68 |
| 68 scoped_ptr<LocalStrikeRegisterClient> strike_register_; | 69 scoped_ptr<LocalStrikeRegisterClient> strike_register_; |
| 69 }; | 70 }; |
| 70 | 71 |
| 71 TEST_F(LocalStrikeRegisterClientTest, CheckOrbit) { | 72 TEST_F(LocalStrikeRegisterClientTest, CheckOrbit) { |
| 72 EXPECT_TRUE(strike_register_->IsKnownOrbit( | 73 EXPECT_TRUE(strike_register_->IsKnownOrbit( |
| 73 StringPiece(reinterpret_cast<const char*>(kOrbit), kOrbitSize))); | 74 StringPiece(reinterpret_cast<const char*>(kOrbit), kOrbitSize))); |
| 74 EXPECT_FALSE(strike_register_->IsKnownOrbit( | 75 EXPECT_FALSE(strike_register_->IsKnownOrbit( |
| 75 StringPiece(reinterpret_cast<const char*>(kOrbit), kOrbitSize - 1))); | 76 StringPiece(reinterpret_cast<const char*>(kOrbit), kOrbitSize - 1))); |
| 76 EXPECT_FALSE(strike_register_->IsKnownOrbit( | 77 EXPECT_FALSE(strike_register_->IsKnownOrbit( |
| 77 StringPiece(reinterpret_cast<const char*>(kOrbit), kOrbitSize + 1))); | 78 StringPiece(reinterpret_cast<const char*>(kOrbit), kOrbitSize + 1))); |
| 78 EXPECT_FALSE(strike_register_->IsKnownOrbit( | 79 EXPECT_FALSE(strike_register_->IsKnownOrbit( |
| 79 StringPiece(reinterpret_cast<const char*>(kOrbit) + 1, kOrbitSize))); | 80 StringPiece(reinterpret_cast<const char*>(kOrbit) + 1, kOrbitSize))); |
| 80 } | 81 } |
| 81 | 82 |
| 82 TEST_F(LocalStrikeRegisterClientTest, IncorrectNonceLength) { | 83 TEST_F(LocalStrikeRegisterClientTest, IncorrectNonceLength) { |
| 83 string valid_nonce; | 84 string valid_nonce; |
| 84 uint32 norder = htonl(kCurrentTimeExternalSecs); | 85 uint32_t norder = htonl(kCurrentTimeExternalSecs); |
| 85 valid_nonce.assign(reinterpret_cast<const char*>(&norder), sizeof(norder)); | 86 valid_nonce.assign(reinterpret_cast<const char*>(&norder), sizeof(norder)); |
| 86 valid_nonce.append(string(reinterpret_cast<const char*>(kOrbit), kOrbitSize)); | 87 valid_nonce.append(string(reinterpret_cast<const char*>(kOrbit), kOrbitSize)); |
| 87 valid_nonce.append(string(20, '\x17')); // 20 'random' bytes. | 88 valid_nonce.append(string(20, '\x17')); // 20 'random' bytes. |
| 88 | 89 |
| 89 { | 90 { |
| 90 // Validation fails if you remove a byte from the nonce. | 91 // Validation fails if you remove a byte from the nonce. |
| 91 bool called = false; | 92 bool called = false; |
| 92 bool is_valid = false; | 93 bool is_valid = false; |
| 93 InsertStatus nonce_error = NONCE_UNKNOWN_FAILURE; | 94 InsertStatus nonce_error = NONCE_UNKNOWN_FAILURE; |
| 94 string short_nonce = valid_nonce.substr(0, valid_nonce.length() - 1); | 95 string short_nonce = valid_nonce.substr(0, valid_nonce.length() - 1); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 125 new RecordResultCallback(&called, &is_valid, &nonce_error)); | 126 new RecordResultCallback(&called, &is_valid, &nonce_error)); |
| 126 EXPECT_TRUE(called); | 127 EXPECT_TRUE(called); |
| 127 EXPECT_TRUE(is_valid); | 128 EXPECT_TRUE(is_valid); |
| 128 EXPECT_EQ(NONCE_OK, nonce_error); | 129 EXPECT_EQ(NONCE_OK, nonce_error); |
| 129 } | 130 } |
| 130 } | 131 } |
| 131 | 132 |
| 132 } // namespace | 133 } // namespace |
| 133 } // namespace test | 134 } // namespace test |
| 134 } // namespace net | 135 } // namespace net |
| OLD | NEW |