| Index: net/quic/crypto/strike_register_test.cc
|
| diff --git a/net/quic/crypto/strike_register_test.cc b/net/quic/crypto/strike_register_test.cc
|
| index d805269942d1ce40e0182a0e97141620feea098e..3d3c3c447984681073959d3c5e9597d0dd831ae1 100644
|
| --- a/net/quic/crypto/strike_register_test.cc
|
| +++ b/net/quic/crypto/strike_register_test.cc
|
| @@ -25,12 +25,14 @@ NonceSetTimeAndOrbit(uint8 nonce[32], unsigned time, const uint8 orbit[8]) {
|
| nonce[2] = time >> 8;
|
| nonce[3] = time;
|
| memcpy(nonce + 4, orbit, 8);
|
| + memset(nonce + 12, 0, 20);
|
| }
|
|
|
| TEST(StrikeRegisterTest, SimpleHorizon) {
|
| // The set must reject values created on or before its own creation time.
|
| StrikeRegister set(10 /* max size */, 1000 /* current time */,
|
| - 100 /* window secs */, kOrbit);
|
| + 100 /* window secs */, kOrbit,
|
| + StrikeRegister::DENY_REQUESTS_AT_STARTUP);
|
| uint8 nonce[32];
|
| NonceSetTimeAndOrbit(nonce, 999, kOrbit);
|
| ASSERT_FALSE(set.Insert(nonce, 1000));
|
| @@ -38,10 +40,23 @@ TEST(StrikeRegisterTest, SimpleHorizon) {
|
| ASSERT_FALSE(set.Insert(nonce, 1000));
|
| }
|
|
|
| +TEST(StrikeRegisterTest, NoStartupMode) {
|
| + // Check that a strike register works immediately if NO_STARTUP_PERIOD_NEEDED
|
| + // is specified.
|
| + StrikeRegister set(10 /* max size */, 0 /* current time */,
|
| + 100 /* window secs */, kOrbit,
|
| + StrikeRegister::NO_STARTUP_PERIOD_NEEDED);
|
| + uint8 nonce[32];
|
| + NonceSetTimeAndOrbit(nonce, 0, kOrbit);
|
| + ASSERT_TRUE(set.Insert(nonce, 0));
|
| + ASSERT_FALSE(set.Insert(nonce, 0));
|
| +}
|
| +
|
| TEST(StrikeRegisterTest, WindowFuture) {
|
| // The set must reject values outside the window.
|
| StrikeRegister set(10 /* max size */, 1000 /* current time */,
|
| - 100 /* window secs */, kOrbit);
|
| + 100 /* window secs */, kOrbit,
|
| + StrikeRegister::DENY_REQUESTS_AT_STARTUP);
|
| uint8 nonce[32];
|
| NonceSetTimeAndOrbit(nonce, 1101, kOrbit);
|
| ASSERT_FALSE(set.Insert(nonce, 1000));
|
| @@ -52,7 +67,8 @@ TEST(StrikeRegisterTest, WindowFuture) {
|
| TEST(StrikeRegisterTest, BadOrbit) {
|
| // The set must reject values with the wrong orbit
|
| StrikeRegister set(10 /* max size */, 1000 /* current time */,
|
| - 100 /* window secs */, kOrbit);
|
| + 100 /* window secs */, kOrbit,
|
| + StrikeRegister::DENY_REQUESTS_AT_STARTUP);
|
| uint8 nonce[32];
|
| static const uint8 kBadOrbit[8] = { 0, 0, 0, 0, 1, 1, 1, 1 };
|
| NonceSetTimeAndOrbit(nonce, 1101, kBadOrbit);
|
| @@ -61,7 +77,8 @@ TEST(StrikeRegisterTest, BadOrbit) {
|
|
|
| TEST(StrikeRegisterTest, OneValue) {
|
| StrikeRegister set(10 /* max size */, 1000 /* current time */,
|
| - 100 /* window secs */, kOrbit);
|
| + 100 /* window secs */, kOrbit,
|
| + StrikeRegister::DENY_REQUESTS_AT_STARTUP);
|
| uint8 nonce[32];
|
| NonceSetTimeAndOrbit(nonce, 1101, kOrbit);
|
| ASSERT_TRUE(set.Insert(nonce, 1100));
|
| @@ -70,7 +87,8 @@ TEST(StrikeRegisterTest, OneValue) {
|
| TEST(StrikeRegisterTest, RejectDuplicate) {
|
| // The set must reject values with the wrong orbit
|
| StrikeRegister set(10 /* max size */, 1000 /* current time */,
|
| - 100 /* window secs */, kOrbit);
|
| + 100 /* window secs */, kOrbit,
|
| + StrikeRegister::DENY_REQUESTS_AT_STARTUP);
|
| uint8 nonce[32];
|
| memset(nonce, 0, sizeof(nonce));
|
| NonceSetTimeAndOrbit(nonce, 1101, kOrbit);
|
| @@ -80,7 +98,8 @@ TEST(StrikeRegisterTest, RejectDuplicate) {
|
|
|
| TEST(StrikeRegisterTest, HorizonUpdating) {
|
| StrikeRegister set(5 /* max size */, 1000 /* current time */,
|
| - 100 /* window secs */, kOrbit);
|
| + 100 /* window secs */, kOrbit,
|
| + StrikeRegister::DENY_REQUESTS_AT_STARTUP);
|
| uint8 nonce[6][32];
|
| for (unsigned i = 0; i < 5; i++) {
|
| NonceSetTimeAndOrbit(nonce[i], 1101, kOrbit);
|
| @@ -103,7 +122,8 @@ TEST(StrikeRegisterTest, HorizonUpdating) {
|
|
|
| TEST(StrikeRegisterTest, InsertMany) {
|
| StrikeRegister set(5000 /* max size */, 1000 /* current time */,
|
| - 500 /* window secs */, kOrbit);
|
| + 500 /* window secs */, kOrbit,
|
| + StrikeRegister::DENY_REQUESTS_AT_STARTUP);
|
|
|
| uint8 nonce[32];
|
| NonceSetTimeAndOrbit(nonce, 1101, kOrbit);
|
| @@ -129,24 +149,20 @@ class SlowStrikeRegister {
|
| uint32 window_secs, const uint8 orbit[8])
|
| : max_entries_(max_entries),
|
| window_secs_(window_secs),
|
| - horizon_(current_time + window_secs) {
|
| + creation_time_(current_time),
|
| + horizon_(ExternalTimeToInternal(current_time + window_secs)) {
|
| memcpy(orbit_, orbit, sizeof(orbit_));
|
| }
|
|
|
| - bool Insert(const uint8 nonce_bytes[32], const uint32 current_time) {
|
| - // Same overflow and underflow check as the real StrikeRegister.
|
| - if (current_time < window_secs_ ||
|
| - current_time + window_secs_ < current_time) {
|
| - nonces_.clear();
|
| - horizon_ = current_time;
|
| - return false;
|
| - }
|
| + bool Insert(const uint8 nonce_bytes[32], const uint32 current_time_external) {
|
| + const uint32 current_time = ExternalTimeToInternal(current_time_external);
|
|
|
| // Check to see if the orbit is correct.
|
| if (memcmp(nonce_bytes + 4, orbit_, sizeof(orbit_))) {
|
| return false;
|
| }
|
| - const uint32 nonce_time = TimeFromBytes(nonce_bytes);
|
| + const uint32 nonce_time =
|
| + ExternalTimeToInternal(TimeFromBytes(nonce_bytes));
|
| // We have dropped one or more nonces with a time value of |horizon_|, so
|
| // we have to reject anything with a timestamp less than or equal to that.
|
| if (nonce_time <= horizon_) {
|
| @@ -154,12 +170,19 @@ class SlowStrikeRegister {
|
| }
|
|
|
| // Check that the timestamp is in the current window.
|
| - if (nonce_time < (current_time - window_secs_) ||
|
| + if ((current_time > window_secs_ &&
|
| + nonce_time < (current_time - window_secs_)) ||
|
| nonce_time > (current_time + window_secs_)) {
|
| return false;
|
| }
|
|
|
| - const string nonce(reinterpret_cast<const char*>(nonce_bytes), 32);
|
| + string nonce;
|
| + nonce.reserve(32);
|
| + nonce +=
|
| + string(reinterpret_cast<const char*>(&nonce_time), sizeof(nonce_time));
|
| + nonce +=
|
| + string(reinterpret_cast<const char*>(nonce_bytes) + sizeof(nonce_time),
|
| + 32 - sizeof(nonce_time));
|
|
|
| set<string>::const_iterator it = nonces_.find(nonce);
|
| if (it != nonces_.end()) {
|
| @@ -183,6 +206,16 @@ class SlowStrikeRegister {
|
| static_cast<uint32>(d[3]);
|
| }
|
|
|
| + uint32 ExternalTimeToInternal(uint32 external_time) {
|
| + static const uint32 kCreationTimeFromInternalEpoch = 63115200.0;
|
| + uint32 internal_epoch = 0;
|
| + if (creation_time_ > kCreationTimeFromInternalEpoch) {
|
| + internal_epoch = creation_time_ - kCreationTimeFromInternalEpoch;
|
| + }
|
| +
|
| + return external_time - internal_epoch;
|
| + }
|
| +
|
| void DropOldestEntry() {
|
| set<string>::iterator oldest = nonces_.begin(), it;
|
| uint32 oldest_time =
|
| @@ -203,6 +236,7 @@ class SlowStrikeRegister {
|
|
|
| const unsigned max_entries_;
|
| const unsigned window_secs_;
|
| + const uint32 creation_time_;
|
| uint8 orbit_[8];
|
| uint32 horizon_;
|
|
|
| @@ -215,7 +249,8 @@ TEST(StrikeRegisterStressTest, Stress) {
|
| unsigned max_entries = 64;
|
| uint32 current_time = 10000, window = 200;
|
| scoped_ptr<StrikeRegister> s1(
|
| - new StrikeRegister(max_entries, current_time, window, kOrbit));
|
| + new StrikeRegister(max_entries, current_time, window, kOrbit,
|
| + StrikeRegister::DENY_REQUESTS_AT_STARTUP));
|
| scoped_ptr<SlowStrikeRegister> s2(
|
| new SlowStrikeRegister(max_entries, current_time, window, kOrbit));
|
| uint64 i;
|
| @@ -230,7 +265,8 @@ TEST(StrikeRegisterStressTest, Stress) {
|
| max_entries = rand() % 300 + 2;
|
| current_time = rand() % 10000;
|
| window = rand() % 500;
|
| - s1.reset(new StrikeRegister(max_entries, current_time, window, kOrbit));
|
| + s1.reset(new StrikeRegister(max_entries, current_time, window, kOrbit,
|
| + StrikeRegister::DENY_REQUESTS_AT_STARTUP));
|
| s2.reset(
|
| new SlowStrikeRegister(max_entries, current_time, window, kOrbit));
|
| }
|
|
|