| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/core/crypto/strike_register.h" | 5 #include "net/quic/core/crypto/strike_register.h" |
| 6 | 6 |
| 7 #include <cstdint> | 7 #include <cstdint> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
| 13 #include "net/quic/platform/api/quic_test.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| 15 namespace net { | 16 namespace net { |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 using std::string; | 20 using std::string; |
| 20 | 21 |
| 21 const uint8_t kOrbit[8] = {1, 2, 3, 4, 5, 6, 7, 8}; | 22 const uint8_t kOrbit[8] = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 22 | 23 |
| 23 // StrikeRegisterTests don't look at the random bytes so this function can | 24 // StrikeRegisterTests don't look at the random bytes so this function can |
| 24 // simply set the random bytes to 0. | 25 // simply set the random bytes to 0. |
| 25 void SetNonce(uint8_t nonce[32], unsigned time, const uint8_t orbit[8]) { | 26 void SetNonce(uint8_t nonce[32], unsigned time, const uint8_t orbit[8]) { |
| 26 nonce[0] = time >> 24; | 27 nonce[0] = time >> 24; |
| 27 nonce[1] = time >> 16; | 28 nonce[1] = time >> 16; |
| 28 nonce[2] = time >> 8; | 29 nonce[2] = time >> 8; |
| 29 nonce[3] = time; | 30 nonce[3] = time; |
| 30 memcpy(nonce + 4, orbit, 8); | 31 memcpy(nonce + 4, orbit, 8); |
| 31 memset(nonce + 12, 0, 20); | 32 memset(nonce + 12, 0, 20); |
| 32 } | 33 } |
| 33 | 34 |
| 34 TEST(StrikeRegisterTest, SimpleHorizon) { | 35 class StrikeRegisterTest : public QuicTest {}; |
| 36 |
| 37 TEST_F(StrikeRegisterTest, SimpleHorizon) { |
| 35 // The set must reject values created on or before its own creation time. | 38 // The set must reject values created on or before its own creation time. |
| 36 StrikeRegister set(10 /* max size */, 1000 /* current time */, | 39 StrikeRegister set(10 /* max size */, 1000 /* current time */, |
| 37 100 /* window secs */, kOrbit, | 40 100 /* window secs */, kOrbit, |
| 38 StrikeRegister::DENY_REQUESTS_AT_STARTUP); | 41 StrikeRegister::DENY_REQUESTS_AT_STARTUP); |
| 39 uint8_t nonce[32]; | 42 uint8_t nonce[32]; |
| 40 SetNonce(nonce, 999, kOrbit); | 43 SetNonce(nonce, 999, kOrbit); |
| 41 EXPECT_EQ(NONCE_INVALID_TIME_FAILURE, set.Insert(nonce, 1000)); | 44 EXPECT_EQ(NONCE_INVALID_TIME_FAILURE, set.Insert(nonce, 1000)); |
| 42 SetNonce(nonce, 1000, kOrbit); | 45 SetNonce(nonce, 1000, kOrbit); |
| 43 EXPECT_EQ(NONCE_INVALID_TIME_FAILURE, set.Insert(nonce, 1000)); | 46 EXPECT_EQ(NONCE_INVALID_TIME_FAILURE, set.Insert(nonce, 1000)); |
| 44 | 47 |
| 45 EXPECT_EQ(0u, set.GetCurrentValidWindowSecs(1000 /* current time */)); | 48 EXPECT_EQ(0u, set.GetCurrentValidWindowSecs(1000 /* current time */)); |
| 46 EXPECT_EQ(0u, set.GetCurrentValidWindowSecs(1100 /* current time */)); | 49 EXPECT_EQ(0u, set.GetCurrentValidWindowSecs(1100 /* current time */)); |
| 47 EXPECT_EQ(1u, set.GetCurrentValidWindowSecs(1101 /* current time */)); | 50 EXPECT_EQ(1u, set.GetCurrentValidWindowSecs(1101 /* current time */)); |
| 48 EXPECT_EQ(50u, set.GetCurrentValidWindowSecs(1150 /* current time */)); | 51 EXPECT_EQ(50u, set.GetCurrentValidWindowSecs(1150 /* current time */)); |
| 49 EXPECT_EQ(100u, set.GetCurrentValidWindowSecs(1200 /* current time */)); | 52 EXPECT_EQ(100u, set.GetCurrentValidWindowSecs(1200 /* current time */)); |
| 50 EXPECT_EQ(101u, set.GetCurrentValidWindowSecs(1300 /* current time */)); | 53 EXPECT_EQ(101u, set.GetCurrentValidWindowSecs(1300 /* current time */)); |
| 51 } | 54 } |
| 52 | 55 |
| 53 TEST(StrikeRegisterTest, NoStartupMode) { | 56 TEST_F(StrikeRegisterTest, NoStartupMode) { |
| 54 // Check that a strike register works immediately if NO_STARTUP_PERIOD_NEEDED | 57 // Check that a strike register works immediately if NO_STARTUP_PERIOD_NEEDED |
| 55 // is specified. | 58 // is specified. |
| 56 StrikeRegister set(10 /* max size */, 1000 /* current time */, | 59 StrikeRegister set(10 /* max size */, 1000 /* current time */, |
| 57 100 /* window secs */, kOrbit, | 60 100 /* window secs */, kOrbit, |
| 58 StrikeRegister::NO_STARTUP_PERIOD_NEEDED); | 61 StrikeRegister::NO_STARTUP_PERIOD_NEEDED); |
| 59 uint8_t nonce[32]; | 62 uint8_t nonce[32]; |
| 60 SetNonce(nonce, 1000, kOrbit); | 63 SetNonce(nonce, 1000, kOrbit); |
| 61 EXPECT_EQ(NONCE_OK, set.Insert(nonce, 1000)); | 64 EXPECT_EQ(NONCE_OK, set.Insert(nonce, 1000)); |
| 62 EXPECT_EQ(NONCE_NOT_UNIQUE_FAILURE, set.Insert(nonce, 1000)); | 65 EXPECT_EQ(NONCE_NOT_UNIQUE_FAILURE, set.Insert(nonce, 1000)); |
| 63 | 66 |
| 64 EXPECT_EQ(101u, set.GetCurrentValidWindowSecs(1000 /* current time */)); | 67 EXPECT_EQ(101u, set.GetCurrentValidWindowSecs(1000 /* current time */)); |
| 65 EXPECT_EQ(101u, set.GetCurrentValidWindowSecs(1050 /* current time */)); | 68 EXPECT_EQ(101u, set.GetCurrentValidWindowSecs(1050 /* current time */)); |
| 66 EXPECT_EQ(101u, set.GetCurrentValidWindowSecs(1100 /* current time */)); | 69 EXPECT_EQ(101u, set.GetCurrentValidWindowSecs(1100 /* current time */)); |
| 67 EXPECT_EQ(101u, set.GetCurrentValidWindowSecs(1200 /* current time */)); | 70 EXPECT_EQ(101u, set.GetCurrentValidWindowSecs(1200 /* current time */)); |
| 68 EXPECT_EQ(101u, set.GetCurrentValidWindowSecs(1300 /* current time */)); | 71 EXPECT_EQ(101u, set.GetCurrentValidWindowSecs(1300 /* current time */)); |
| 69 } | 72 } |
| 70 | 73 |
| 71 TEST(StrikeRegisterTest, WindowFuture) { | 74 TEST_F(StrikeRegisterTest, WindowFuture) { |
| 72 // The set must reject values outside the window. | 75 // The set must reject values outside the window. |
| 73 StrikeRegister set(10 /* max size */, 1000 /* current time */, | 76 StrikeRegister set(10 /* max size */, 1000 /* current time */, |
| 74 100 /* window secs */, kOrbit, | 77 100 /* window secs */, kOrbit, |
| 75 StrikeRegister::DENY_REQUESTS_AT_STARTUP); | 78 StrikeRegister::DENY_REQUESTS_AT_STARTUP); |
| 76 uint8_t nonce[32]; | 79 uint8_t nonce[32]; |
| 77 SetNonce(nonce, 1101, kOrbit); | 80 SetNonce(nonce, 1101, kOrbit); |
| 78 EXPECT_EQ(NONCE_INVALID_TIME_FAILURE, set.Insert(nonce, 1000)); | 81 EXPECT_EQ(NONCE_INVALID_TIME_FAILURE, set.Insert(nonce, 1000)); |
| 79 SetNonce(nonce, 999, kOrbit); | 82 SetNonce(nonce, 999, kOrbit); |
| 80 EXPECT_EQ(NONCE_INVALID_TIME_FAILURE, set.Insert(nonce, 1100)); | 83 EXPECT_EQ(NONCE_INVALID_TIME_FAILURE, set.Insert(nonce, 1100)); |
| 81 } | 84 } |
| 82 | 85 |
| 83 TEST(StrikeRegisterTest, BadOrbit) { | 86 TEST_F(StrikeRegisterTest, BadOrbit) { |
| 84 // The set must reject values with the wrong orbit | 87 // The set must reject values with the wrong orbit |
| 85 StrikeRegister set(10 /* max size */, 1000 /* current time */, | 88 StrikeRegister set(10 /* max size */, 1000 /* current time */, |
| 86 100 /* window secs */, kOrbit, | 89 100 /* window secs */, kOrbit, |
| 87 StrikeRegister::DENY_REQUESTS_AT_STARTUP); | 90 StrikeRegister::DENY_REQUESTS_AT_STARTUP); |
| 88 uint8_t nonce[32]; | 91 uint8_t nonce[32]; |
| 89 static const uint8_t kBadOrbit[8] = {0, 0, 0, 0, 1, 1, 1, 1}; | 92 static const uint8_t kBadOrbit[8] = {0, 0, 0, 0, 1, 1, 1, 1}; |
| 90 SetNonce(nonce, 1101, kBadOrbit); | 93 SetNonce(nonce, 1101, kBadOrbit); |
| 91 EXPECT_EQ(NONCE_INVALID_ORBIT_FAILURE, set.Insert(nonce, 1100)); | 94 EXPECT_EQ(NONCE_INVALID_ORBIT_FAILURE, set.Insert(nonce, 1100)); |
| 92 } | 95 } |
| 93 | 96 |
| 94 TEST(StrikeRegisterTest, OneValue) { | 97 TEST_F(StrikeRegisterTest, OneValue) { |
| 95 StrikeRegister set(10 /* max size */, 1000 /* current time */, | 98 StrikeRegister set(10 /* max size */, 1000 /* current time */, |
| 96 100 /* window secs */, kOrbit, | 99 100 /* window secs */, kOrbit, |
| 97 StrikeRegister::DENY_REQUESTS_AT_STARTUP); | 100 StrikeRegister::DENY_REQUESTS_AT_STARTUP); |
| 98 uint8_t nonce[32]; | 101 uint8_t nonce[32]; |
| 99 SetNonce(nonce, 1101, kOrbit); | 102 SetNonce(nonce, 1101, kOrbit); |
| 100 EXPECT_EQ(NONCE_OK, set.Insert(nonce, 1101)); | 103 EXPECT_EQ(NONCE_OK, set.Insert(nonce, 1101)); |
| 101 } | 104 } |
| 102 | 105 |
| 103 TEST(StrikeRegisterTest, RejectDuplicate) { | 106 TEST_F(StrikeRegisterTest, RejectDuplicate) { |
| 104 // The set must reject values with the wrong orbit | 107 // The set must reject values with the wrong orbit |
| 105 StrikeRegister set(10 /* max size */, 1000 /* current time */, | 108 StrikeRegister set(10 /* max size */, 1000 /* current time */, |
| 106 100 /* window secs */, kOrbit, | 109 100 /* window secs */, kOrbit, |
| 107 StrikeRegister::DENY_REQUESTS_AT_STARTUP); | 110 StrikeRegister::DENY_REQUESTS_AT_STARTUP); |
| 108 uint8_t nonce[32]; | 111 uint8_t nonce[32]; |
| 109 SetNonce(nonce, 1101, kOrbit); | 112 SetNonce(nonce, 1101, kOrbit); |
| 110 EXPECT_EQ(NONCE_OK, set.Insert(nonce, 1101)); | 113 EXPECT_EQ(NONCE_OK, set.Insert(nonce, 1101)); |
| 111 EXPECT_EQ(NONCE_NOT_UNIQUE_FAILURE, set.Insert(nonce, 1101)); | 114 EXPECT_EQ(NONCE_NOT_UNIQUE_FAILURE, set.Insert(nonce, 1101)); |
| 112 } | 115 } |
| 113 | 116 |
| 114 TEST(StrikeRegisterTest, HorizonUpdating) { | 117 TEST_F(StrikeRegisterTest, HorizonUpdating) { |
| 115 StrikeRegister::StartupType startup_types[] = { | 118 StrikeRegister::StartupType startup_types[] = { |
| 116 StrikeRegister::DENY_REQUESTS_AT_STARTUP, | 119 StrikeRegister::DENY_REQUESTS_AT_STARTUP, |
| 117 StrikeRegister::NO_STARTUP_PERIOD_NEEDED}; | 120 StrikeRegister::NO_STARTUP_PERIOD_NEEDED}; |
| 118 | 121 |
| 119 for (size_t type_idx = 0; type_idx < arraysize(startup_types); ++type_idx) { | 122 for (size_t type_idx = 0; type_idx < arraysize(startup_types); ++type_idx) { |
| 120 StrikeRegister set(5 /* max size */, 500 /* current time */, | 123 StrikeRegister set(5 /* max size */, 500 /* current time */, |
| 121 100 /* window secs */, kOrbit, startup_types[type_idx]); | 124 100 /* window secs */, kOrbit, startup_types[type_idx]); |
| 122 uint8_t nonce[6][32]; | 125 uint8_t nonce[6][32]; |
| 123 for (unsigned i = 0; i < 5; i++) { | 126 for (unsigned i = 0; i < 5; i++) { |
| 124 SetNonce(nonce[i], 1101 + i, kOrbit); | 127 SetNonce(nonce[i], 1101 + i, kOrbit); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 nonce[5][31] = 1; | 159 nonce[5][31] = 1; |
| 157 EXPECT_EQ(NONCE_OK, set.Insert(nonce[5], 1110)); | 160 EXPECT_EQ(NONCE_OK, set.Insert(nonce[5], 1110)); |
| 158 | 161 |
| 159 // This should be beyond the upper valid range now: | 162 // This should be beyond the upper valid range now: |
| 160 SetNonce(nonce[5], 1116, kOrbit); | 163 SetNonce(nonce[5], 1116, kOrbit); |
| 161 nonce[5][31] = 2; | 164 nonce[5][31] = 2; |
| 162 EXPECT_EQ(NONCE_INVALID_TIME_FAILURE, set.Insert(nonce[5], 1110)); | 165 EXPECT_EQ(NONCE_INVALID_TIME_FAILURE, set.Insert(nonce[5], 1110)); |
| 163 } | 166 } |
| 164 } | 167 } |
| 165 | 168 |
| 166 TEST(StrikeRegisterTest, InsertMany) { | 169 TEST_F(StrikeRegisterTest, InsertMany) { |
| 167 StrikeRegister set(5000 /* max size */, 1000 /* current time */, | 170 StrikeRegister set(5000 /* max size */, 1000 /* current time */, |
| 168 500 /* window secs */, kOrbit, | 171 500 /* window secs */, kOrbit, |
| 169 StrikeRegister::DENY_REQUESTS_AT_STARTUP); | 172 StrikeRegister::DENY_REQUESTS_AT_STARTUP); |
| 170 | 173 |
| 171 uint8_t nonce[32]; | 174 uint8_t nonce[32]; |
| 172 SetNonce(nonce, 1101, kOrbit); | 175 SetNonce(nonce, 1101, kOrbit); |
| 173 for (unsigned i = 0; i < 100000; i++) { | 176 for (unsigned i = 0; i < 100000; i++) { |
| 174 SetNonce(nonce, 1101 + i / 500, kOrbit); | 177 SetNonce(nonce, 1101 + i / 500, kOrbit); |
| 175 memcpy(nonce + 12, &i, sizeof(i)); | 178 memcpy(nonce + 12, &i, sizeof(i)); |
| 176 EXPECT_EQ(NONCE_INVALID_TIME_FAILURE, set.Insert(nonce, 1100)); | 179 EXPECT_EQ(NONCE_INVALID_TIME_FAILURE, set.Insert(nonce, 1100)); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 | 279 |
| 277 const unsigned max_entries_; | 280 const unsigned max_entries_; |
| 278 const unsigned window_secs_; | 281 const unsigned window_secs_; |
| 279 const uint32_t creation_time_; | 282 const uint32_t creation_time_; |
| 280 uint8_t orbit_[8]; | 283 uint8_t orbit_[8]; |
| 281 uint32_t horizon_; | 284 uint32_t horizon_; |
| 282 | 285 |
| 283 std::set<std::pair<uint32_t, string>> nonces_; | 286 std::set<std::pair<uint32_t, string>> nonces_; |
| 284 }; | 287 }; |
| 285 | 288 |
| 286 class StrikeRegisterStressTest : public ::testing::Test {}; | 289 class StrikeRegisterStressTest : public QuicTest {}; |
| 287 | 290 |
| 288 TEST_F(StrikeRegisterStressTest, InOrderInsertion) { | 291 TEST_F(StrikeRegisterStressTest, InOrderInsertion) { |
| 289 // Fixed seed gives reproducibility for this test. | 292 // Fixed seed gives reproducibility for this test. |
| 290 srand(42); | 293 srand(42); |
| 291 | 294 |
| 292 unsigned max_entries = 64; | 295 unsigned max_entries = 64; |
| 293 uint32_t current_time = 10000, window = 200; | 296 uint32_t current_time = 10000, window = 200; |
| 294 std::unique_ptr<StrikeRegister> s1( | 297 std::unique_ptr<StrikeRegister> s1( |
| 295 new StrikeRegister(max_entries, current_time, window, kOrbit, | 298 new StrikeRegister(max_entries, current_time, window, kOrbit, |
| 296 StrikeRegister::DENY_REQUESTS_AT_STARTUP)); | 299 StrikeRegister::DENY_REQUESTS_AT_STARTUP)); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 } | 399 } |
| 397 | 400 |
| 398 if (i != kMaxIterations) { | 401 if (i != kMaxIterations) { |
| 399 FAIL() << "Failed after " << i << " iterations"; | 402 FAIL() << "Failed after " << i << " iterations"; |
| 400 } | 403 } |
| 401 } | 404 } |
| 402 | 405 |
| 403 } // namespace | 406 } // namespace |
| 404 | 407 |
| 405 } // namespace net | 408 } // namespace net |
| OLD | NEW |