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/crypto/strike_register.h" | 5 #include "net/quic/crypto/strike_register.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 using net::StrikeRegister; | 15 using net::StrikeRegister; |
16 using std::set; | 16 using std::set; |
17 using std::string; | 17 using std::string; |
18 | 18 |
19 const uint8 kOrbit[8] = {1, 2, 3, 4, 5, 6, 7, 8}; | 19 const uint8 kOrbit[8] = { 1, 2, 3, 4, 5, 6, 7, 8 }; |
20 | 20 |
21 void | 21 void |
22 NonceSetTimeAndOrbit(uint8 nonce[32], unsigned time, const uint8 orbit[8]) { | 22 NonceSetTimeAndOrbit(uint8 nonce[32], unsigned time, const uint8 orbit[8]) { |
23 nonce[0] = time >> 24; | 23 nonce[0] = time >> 24; |
24 nonce[1] = time >> 16; | 24 nonce[1] = time >> 16; |
25 nonce[2] = time >> 8; | 25 nonce[2] = time >> 8; |
26 nonce[3] = time; | 26 nonce[3] = time; |
27 memcpy(nonce + 4, orbit, 8); | 27 memcpy(nonce + 4, orbit, 8); |
28 } | 28 } |
29 | 29 |
(...skipping 17 matching lines...) Expand all Loading... |
47 ASSERT_FALSE(set.Insert(nonce, 1000)); | 47 ASSERT_FALSE(set.Insert(nonce, 1000)); |
48 NonceSetTimeAndOrbit(nonce, 999, kOrbit); | 48 NonceSetTimeAndOrbit(nonce, 999, kOrbit); |
49 ASSERT_FALSE(set.Insert(nonce, 1100)); | 49 ASSERT_FALSE(set.Insert(nonce, 1100)); |
50 } | 50 } |
51 | 51 |
52 TEST(StrikeRegisterTest, BadOrbit) { | 52 TEST(StrikeRegisterTest, BadOrbit) { |
53 // The set must reject values with the wrong orbit | 53 // The set must reject values with the wrong orbit |
54 StrikeRegister set(10 /* max size */, 1000 /* current time */, | 54 StrikeRegister set(10 /* max size */, 1000 /* current time */, |
55 100 /* window secs */, kOrbit); | 55 100 /* window secs */, kOrbit); |
56 uint8 nonce[32]; | 56 uint8 nonce[32]; |
57 static const uint8 kBadOrbit[8] = {0, 0, 0, 0, 1, 1, 1, 1}; | 57 static const uint8 kBadOrbit[8] = { 0, 0, 0, 0, 1, 1, 1, 1 }; |
58 NonceSetTimeAndOrbit(nonce, 1101, kBadOrbit); | 58 NonceSetTimeAndOrbit(nonce, 1101, kBadOrbit); |
59 ASSERT_FALSE(set.Insert(nonce, 1100)); | 59 ASSERT_FALSE(set.Insert(nonce, 1100)); |
60 } | 60 } |
61 | 61 |
62 TEST(StrikeRegisterTest, OneValue) { | 62 TEST(StrikeRegisterTest, OneValue) { |
63 StrikeRegister set(10 /* max size */, 1000 /* current time */, | 63 StrikeRegister set(10 /* max size */, 1000 /* current time */, |
64 100 /* window secs */, kOrbit); | 64 100 /* window secs */, kOrbit); |
65 uint8 nonce[32]; | 65 uint8 nonce[32]; |
66 NonceSetTimeAndOrbit(nonce, 1101, kOrbit); | 66 NonceSetTimeAndOrbit(nonce, 1101, kOrbit); |
67 ASSERT_TRUE(set.Insert(nonce, 1100)); | 67 ASSERT_TRUE(set.Insert(nonce, 1100)); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 static uint32 TimeFromBytes(const uint8 d[4]) { | 179 static uint32 TimeFromBytes(const uint8 d[4]) { |
180 return static_cast<uint32>(d[0]) << 24 | | 180 return static_cast<uint32>(d[0]) << 24 | |
181 static_cast<uint32>(d[1]) << 16 | | 181 static_cast<uint32>(d[1]) << 16 | |
182 static_cast<uint32>(d[2]) << 8 | | 182 static_cast<uint32>(d[2]) << 8 | |
183 static_cast<uint32>(d[3]); | 183 static_cast<uint32>(d[3]); |
184 } | 184 } |
185 | 185 |
186 void DropOldestEntry() { | 186 void DropOldestEntry() { |
187 set<string>::iterator oldest = nonces_.begin(), it; | 187 set<string>::iterator oldest = nonces_.begin(), it; |
188 uint32 oldest_time = | 188 uint32 oldest_time = |
189 TimeFromBytes(reinterpret_cast<const uint8*>(oldest->data())); | 189 TimeFromBytes(reinterpret_cast<const uint8*>(oldest->data())); |
190 | 190 |
191 for (it = oldest; it != nonces_.end(); it++) { | 191 for (it = oldest; it != nonces_.end(); it++) { |
192 uint32 t = TimeFromBytes(reinterpret_cast<const uint8*>(it->data())); | 192 uint32 t = TimeFromBytes(reinterpret_cast<const uint8*>(it->data())); |
193 if (t < oldest_time || | 193 if (t < oldest_time || |
194 (t == oldest_time && memcmp(it->data(), oldest->data(), 32) < 0)) { | 194 (t == oldest_time && memcmp(it->data(), oldest->data(), 32) < 0)) { |
195 oldest_time = t; | 195 oldest_time = t; |
196 oldest = it; | 196 oldest = it; |
197 } | 197 } |
198 } | 198 } |
199 | 199 |
200 nonces_.erase(oldest); | 200 nonces_.erase(oldest); |
201 horizon_ = oldest_time; | 201 horizon_ = oldest_time; |
202 } | 202 } |
203 | 203 |
204 const unsigned max_entries_; | 204 const unsigned max_entries_; |
205 const unsigned window_secs_; | 205 const unsigned window_secs_; |
206 uint8 orbit_[8]; | 206 uint8 orbit_[8]; |
207 uint32 horizon_; | 207 uint32 horizon_; |
208 | 208 |
209 set<string> nonces_; | 209 set<string> nonces_; |
210 }; | 210 }; |
211 | 211 |
212 TEST(StrikeRegisterStressTest, Stress) { | 212 TEST(StrikeRegisterStressTest, Stress) { |
213 // Fixed seed gives reproducibility for this test. | 213 // Fixed seed gives reproducibility for this test. |
214 srand(42); | 214 srand(42); |
215 unsigned max_entries = 64; | 215 unsigned max_entries = 64; |
216 uint32 current_time = 10000, window = 200; | 216 uint32 current_time = 10000, window = 200; |
217 scoped_ptr<StrikeRegister> s1(new StrikeRegister( | 217 scoped_ptr<StrikeRegister> s1( |
218 max_entries, current_time, window, kOrbit)); | 218 new StrikeRegister(max_entries, current_time, window, kOrbit)); |
219 scoped_ptr<SlowStrikeRegister> s2(new SlowStrikeRegister( | 219 scoped_ptr<SlowStrikeRegister> s2( |
220 max_entries, current_time, window, kOrbit)); | 220 new SlowStrikeRegister(max_entries, current_time, window, kOrbit)); |
221 uint64 i; | 221 uint64 i; |
222 | 222 |
223 // When making changes it's worth removing the limit on this test and running | 223 // When making changes it's worth removing the limit on this test and running |
224 // it for a while. For the initial development an opt binary was left running | 224 // it for a while. For the initial development an opt binary was left running |
225 // for 10 minutes. | 225 // for 10 minutes. |
226 const uint64 kMaxIterations = 10000; | 226 const uint64 kMaxIterations = 10000; |
227 for (i = 0; i < kMaxIterations; i++) { | 227 for (i = 0; i < kMaxIterations; i++) { |
228 if (rand() % 1000 == 0) { | 228 if (rand() % 1000 == 0) { |
229 // 0.1% chance of resetting the sets. | 229 // 0.1% chance of resetting the sets. |
230 max_entries = rand() % 300 + 2; | 230 max_entries = rand() % 300 + 2; |
231 current_time = rand() % 10000; | 231 current_time = rand() % 10000; |
232 window = rand() % 500; | 232 window = rand() % 500; |
233 s1.reset(new StrikeRegister(max_entries, current_time, window, kOrbit)); | 233 s1.reset(new StrikeRegister(max_entries, current_time, window, kOrbit)); |
234 s2.reset(new SlowStrikeRegister(max_entries, current_time, window, | 234 s2.reset( |
235 kOrbit)); | 235 new SlowStrikeRegister(max_entries, current_time, window, kOrbit)); |
236 } | 236 } |
237 | 237 |
238 int32 time_delta = rand() % (window * 4); | 238 int32 time_delta = rand() % (window * 4); |
239 time_delta -= window * 2; | 239 time_delta -= window * 2; |
240 const uint32 time = current_time + time_delta; | 240 const uint32 time = current_time + time_delta; |
241 if (time_delta < 0 && time > current_time) { | 241 if (time_delta < 0 && time > current_time) { |
242 continue; // overflow | 242 continue; // overflow |
243 } | 243 } |
244 | 244 |
245 uint8 nonce[32]; | 245 uint8 nonce[32]; |
(...skipping 16 matching lines...) Expand all Loading... |
262 s1->Validate(); | 262 s1->Validate(); |
263 } | 263 } |
264 } | 264 } |
265 | 265 |
266 if (i != kMaxIterations) { | 266 if (i != kMaxIterations) { |
267 FAIL() << "Failed after " << i << " iterations"; | 267 FAIL() << "Failed after " << i << " iterations"; |
268 } | 268 } |
269 } | 269 } |
270 | 270 |
271 } // anonymous namespace | 271 } // anonymous namespace |
OLD | NEW |