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 "base/rand_util.h" | 11 #include "base/rand_util.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
| 16 using net::InsertStatus; |
16 using net::StrikeRegister; | 17 using net::StrikeRegister; |
17 using std::make_pair; | 18 using std::make_pair; |
18 using std::min; | 19 using std::min; |
19 using std::pair; | 20 using std::pair; |
20 using std::set; | 21 using std::set; |
21 using std::string; | 22 using std::string; |
22 | 23 |
23 const uint8 kOrbit[8] = { 1, 2, 3, 4, 5, 6, 7, 8 }; | 24 const uint8 kOrbit[8] = { 1, 2, 3, 4, 5, 6, 7, 8 }; |
24 | 25 |
25 // StrikeRegisterTests don't look at the random bytes so this function can | 26 // StrikeRegisterTests don't look at the random bytes so this function can |
26 // simply set the random bytes to 0. | 27 // simply set the random bytes to 0. |
27 void SetNonce(uint8 nonce[32], unsigned time, const uint8 orbit[8]) { | 28 void SetNonce(uint8 nonce[32], unsigned time, const uint8 orbit[8]) { |
28 nonce[0] = time >> 24; | 29 nonce[0] = time >> 24; |
29 nonce[1] = time >> 16; | 30 nonce[1] = time >> 16; |
30 nonce[2] = time >> 8; | 31 nonce[2] = time >> 8; |
31 nonce[3] = time; | 32 nonce[3] = time; |
32 memcpy(nonce + 4, orbit, 8); | 33 memcpy(nonce + 4, orbit, 8); |
33 memset(nonce + 12, 0, 20); | 34 memset(nonce + 12, 0, 20); |
34 } | 35 } |
35 | 36 |
36 TEST(StrikeRegisterTest, SimpleHorizon) { | 37 TEST(StrikeRegisterTest, SimpleHorizon) { |
37 // 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. |
38 StrikeRegister set(10 /* max size */, 1000 /* current time */, | 39 StrikeRegister set(10 /* max size */, 1000 /* current time */, |
39 100 /* window secs */, kOrbit, | 40 100 /* window secs */, kOrbit, |
40 StrikeRegister::DENY_REQUESTS_AT_STARTUP); | 41 StrikeRegister::DENY_REQUESTS_AT_STARTUP); |
41 uint8 nonce[32]; | 42 uint8 nonce[32]; |
42 SetNonce(nonce, 999, kOrbit); | 43 SetNonce(nonce, 999, kOrbit); |
43 ASSERT_FALSE(set.Insert(nonce, 1000)); | 44 EXPECT_EQ(net::NONCE_INVALID_TIME_FAILURE, set.Insert(nonce, 1000)); |
44 SetNonce(nonce, 1000, kOrbit); | 45 SetNonce(nonce, 1000, kOrbit); |
45 ASSERT_FALSE(set.Insert(nonce, 1000)); | 46 EXPECT_EQ(net::NONCE_INVALID_TIME_FAILURE, set.Insert(nonce, 1000)); |
46 | 47 |
47 EXPECT_EQ(0u, set.GetCurrentValidWindowSecs(1000 /* current time */)); | 48 EXPECT_EQ(0u, set.GetCurrentValidWindowSecs(1000 /* current time */)); |
48 EXPECT_EQ(0u, set.GetCurrentValidWindowSecs(1100 /* current time */)); | 49 EXPECT_EQ(0u, set.GetCurrentValidWindowSecs(1100 /* current time */)); |
49 EXPECT_EQ(1u, set.GetCurrentValidWindowSecs(1101 /* current time */)); | 50 EXPECT_EQ(1u, set.GetCurrentValidWindowSecs(1101 /* current time */)); |
50 EXPECT_EQ(50u, set.GetCurrentValidWindowSecs(1150 /* current time */)); | 51 EXPECT_EQ(50u, set.GetCurrentValidWindowSecs(1150 /* current time */)); |
51 EXPECT_EQ(100u, set.GetCurrentValidWindowSecs(1200 /* current time */)); | 52 EXPECT_EQ(100u, set.GetCurrentValidWindowSecs(1200 /* current time */)); |
52 EXPECT_EQ(101u, set.GetCurrentValidWindowSecs(1300 /* current time */)); | 53 EXPECT_EQ(101u, set.GetCurrentValidWindowSecs(1300 /* current time */)); |
53 } | 54 } |
54 | 55 |
55 TEST(StrikeRegisterTest, NoStartupMode) { | 56 TEST(StrikeRegisterTest, NoStartupMode) { |
56 // 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 |
57 // is specified. | 58 // is specified. |
58 StrikeRegister set(10 /* max size */, 1000 /* current time */, | 59 StrikeRegister set(10 /* max size */, 1000 /* current time */, |
59 100 /* window secs */, kOrbit, | 60 100 /* window secs */, kOrbit, |
60 StrikeRegister::NO_STARTUP_PERIOD_NEEDED); | 61 StrikeRegister::NO_STARTUP_PERIOD_NEEDED); |
61 uint8 nonce[32]; | 62 uint8 nonce[32]; |
62 SetNonce(nonce, 1000, kOrbit); | 63 SetNonce(nonce, 1000, kOrbit); |
63 ASSERT_TRUE(set.Insert(nonce, 1000)); | 64 EXPECT_EQ(net::NONCE_OK, set.Insert(nonce, 1000)); |
64 ASSERT_FALSE(set.Insert(nonce, 1000)); | 65 EXPECT_EQ(net::NONCE_NOT_UNIQUE_FAILURE, set.Insert(nonce, 1000)); |
65 | 66 |
66 EXPECT_EQ(101u, set.GetCurrentValidWindowSecs(1000 /* current time */)); | 67 EXPECT_EQ(101u, set.GetCurrentValidWindowSecs(1000 /* current time */)); |
67 EXPECT_EQ(101u, set.GetCurrentValidWindowSecs(1050 /* current time */)); | 68 EXPECT_EQ(101u, set.GetCurrentValidWindowSecs(1050 /* current time */)); |
68 EXPECT_EQ(101u, set.GetCurrentValidWindowSecs(1100 /* current time */)); | 69 EXPECT_EQ(101u, set.GetCurrentValidWindowSecs(1100 /* current time */)); |
69 EXPECT_EQ(101u, set.GetCurrentValidWindowSecs(1200 /* current time */)); | 70 EXPECT_EQ(101u, set.GetCurrentValidWindowSecs(1200 /* current time */)); |
70 EXPECT_EQ(101u, set.GetCurrentValidWindowSecs(1300 /* current time */)); | 71 EXPECT_EQ(101u, set.GetCurrentValidWindowSecs(1300 /* current time */)); |
71 } | 72 } |
72 | 73 |
73 TEST(StrikeRegisterTest, WindowFuture) { | 74 TEST(StrikeRegisterTest, WindowFuture) { |
74 // The set must reject values outside the window. | 75 // The set must reject values outside the window. |
75 StrikeRegister set(10 /* max size */, 1000 /* current time */, | 76 StrikeRegister set(10 /* max size */, 1000 /* current time */, |
76 100 /* window secs */, kOrbit, | 77 100 /* window secs */, kOrbit, |
77 StrikeRegister::DENY_REQUESTS_AT_STARTUP); | 78 StrikeRegister::DENY_REQUESTS_AT_STARTUP); |
78 uint8 nonce[32]; | 79 uint8 nonce[32]; |
79 SetNonce(nonce, 1101, kOrbit); | 80 SetNonce(nonce, 1101, kOrbit); |
80 ASSERT_FALSE(set.Insert(nonce, 1000)); | 81 EXPECT_EQ(net::NONCE_INVALID_TIME_FAILURE, set.Insert(nonce, 1000)); |
81 SetNonce(nonce, 999, kOrbit); | 82 SetNonce(nonce, 999, kOrbit); |
82 ASSERT_FALSE(set.Insert(nonce, 1100)); | 83 EXPECT_EQ(net::NONCE_INVALID_TIME_FAILURE, set.Insert(nonce, 1100)); |
83 } | 84 } |
84 | 85 |
85 TEST(StrikeRegisterTest, BadOrbit) { | 86 TEST(StrikeRegisterTest, BadOrbit) { |
86 // The set must reject values with the wrong orbit | 87 // The set must reject values with the wrong orbit |
87 StrikeRegister set(10 /* max size */, 1000 /* current time */, | 88 StrikeRegister set(10 /* max size */, 1000 /* current time */, |
88 100 /* window secs */, kOrbit, | 89 100 /* window secs */, kOrbit, |
89 StrikeRegister::DENY_REQUESTS_AT_STARTUP); | 90 StrikeRegister::DENY_REQUESTS_AT_STARTUP); |
90 uint8 nonce[32]; | 91 uint8 nonce[32]; |
91 static const uint8 kBadOrbit[8] = { 0, 0, 0, 0, 1, 1, 1, 1 }; | 92 static const uint8 kBadOrbit[8] = { 0, 0, 0, 0, 1, 1, 1, 1 }; |
92 SetNonce(nonce, 1101, kBadOrbit); | 93 SetNonce(nonce, 1101, kBadOrbit); |
93 ASSERT_FALSE(set.Insert(nonce, 1100)); | 94 EXPECT_EQ(net::NONCE_INVALID_ORBIT_FAILURE, set.Insert(nonce, 1100)); |
94 } | 95 } |
95 | 96 |
96 TEST(StrikeRegisterTest, OneValue) { | 97 TEST(StrikeRegisterTest, OneValue) { |
97 StrikeRegister set(10 /* max size */, 1000 /* current time */, | 98 StrikeRegister set(10 /* max size */, 1000 /* current time */, |
98 100 /* window secs */, kOrbit, | 99 100 /* window secs */, kOrbit, |
99 StrikeRegister::DENY_REQUESTS_AT_STARTUP); | 100 StrikeRegister::DENY_REQUESTS_AT_STARTUP); |
100 uint8 nonce[32]; | 101 uint8 nonce[32]; |
101 SetNonce(nonce, 1101, kOrbit); | 102 SetNonce(nonce, 1101, kOrbit); |
102 ASSERT_TRUE(set.Insert(nonce, 1101)); | 103 EXPECT_EQ(net::NONCE_OK, set.Insert(nonce, 1101)); |
103 } | 104 } |
104 | 105 |
105 TEST(StrikeRegisterTest, RejectDuplicate) { | 106 TEST(StrikeRegisterTest, RejectDuplicate) { |
106 // The set must reject values with the wrong orbit | 107 // The set must reject values with the wrong orbit |
107 StrikeRegister set(10 /* max size */, 1000 /* current time */, | 108 StrikeRegister set(10 /* max size */, 1000 /* current time */, |
108 100 /* window secs */, kOrbit, | 109 100 /* window secs */, kOrbit, |
109 StrikeRegister::DENY_REQUESTS_AT_STARTUP); | 110 StrikeRegister::DENY_REQUESTS_AT_STARTUP); |
110 uint8 nonce[32]; | 111 uint8 nonce[32]; |
111 SetNonce(nonce, 1101, kOrbit); | 112 SetNonce(nonce, 1101, kOrbit); |
112 ASSERT_TRUE(set.Insert(nonce, 1101)); | 113 EXPECT_EQ(net::NONCE_OK, set.Insert(nonce, 1101)); |
113 ASSERT_FALSE(set.Insert(nonce, 1101)); | 114 EXPECT_EQ(net::NONCE_NOT_UNIQUE_FAILURE, set.Insert(nonce, 1101)); |
114 } | 115 } |
115 | 116 |
116 TEST(StrikeRegisterTest, HorizonUpdating) { | 117 TEST(StrikeRegisterTest, HorizonUpdating) { |
117 StrikeRegister::StartupType startup_types[] = { | 118 StrikeRegister::StartupType startup_types[] = { |
118 StrikeRegister::DENY_REQUESTS_AT_STARTUP, | 119 StrikeRegister::DENY_REQUESTS_AT_STARTUP, |
119 StrikeRegister::NO_STARTUP_PERIOD_NEEDED | 120 StrikeRegister::NO_STARTUP_PERIOD_NEEDED |
120 }; | 121 }; |
121 | 122 |
122 for (size_t type_idx = 0; type_idx < arraysize(startup_types); ++type_idx) { | 123 for (size_t type_idx = 0; type_idx < arraysize(startup_types); ++type_idx) { |
123 StrikeRegister set(5 /* max size */, 500 /* current time */, | 124 StrikeRegister set(5 /* max size */, 500 /* current time */, |
124 100 /* window secs */, kOrbit, | 125 100 /* window secs */, kOrbit, |
125 startup_types[type_idx]); | 126 startup_types[type_idx]); |
126 uint8 nonce[6][32]; | 127 uint8 nonce[6][32]; |
127 for (unsigned i = 0; i < 5; i++) { | 128 for (unsigned i = 0; i < 5; i++) { |
128 SetNonce(nonce[i], 1101 + i, kOrbit); | 129 SetNonce(nonce[i], 1101 + i, kOrbit); |
129 nonce[i][31] = i; | 130 nonce[i][31] = i; |
130 ASSERT_TRUE(set.Insert(nonce[i], 1100)); | 131 EXPECT_EQ(net::NONCE_OK, set.Insert(nonce[i], 1100)); |
131 } | 132 } |
132 | 133 |
133 // Valid window is still equal to |window_secs + 1|. | 134 // Valid window is still equal to |window_secs + 1|. |
134 EXPECT_EQ(101u, set.GetCurrentValidWindowSecs(1100)); | 135 EXPECT_EQ(101u, set.GetCurrentValidWindowSecs(1100)); |
135 | 136 |
136 // This should push the oldest value out and force the horizon to | 137 // This should push the oldest value out and force the horizon to |
137 // be updated. | 138 // be updated. |
138 SetNonce(nonce[5], 1110, kOrbit); | 139 SetNonce(nonce[5], 1110, kOrbit); |
139 ASSERT_TRUE(set.Insert(nonce[5], 1110)); | 140 EXPECT_EQ(net::NONCE_OK, set.Insert(nonce[5], 1110)); |
140 // Effective horizon is computed based on the timestamp of the | 141 // Effective horizon is computed based on the timestamp of the |
141 // value that was pushed out. | 142 // value that was pushed out. |
142 EXPECT_EQ(9u, set.GetCurrentValidWindowSecs(1110)); | 143 EXPECT_EQ(9u, set.GetCurrentValidWindowSecs(1110)); |
143 | 144 |
144 SetNonce(nonce[5], 1111, kOrbit); | 145 SetNonce(nonce[5], 1111, kOrbit); |
145 EXPECT_TRUE(set.Insert(nonce[5], 1110)); | 146 EXPECT_EQ(net::NONCE_OK, set.Insert(nonce[5], 1110)); |
146 EXPECT_EQ(8u, set.GetCurrentValidWindowSecs(1110)); | 147 EXPECT_EQ(8u, set.GetCurrentValidWindowSecs(1110)); |
147 | 148 |
148 // This should be behind the horizon now: | 149 // This should be behind the horizon now: |
149 SetNonce(nonce[5], 1101, kOrbit); | 150 SetNonce(nonce[5], 1101, kOrbit); |
150 nonce[5][31] = 10; | 151 nonce[5][31] = 10; |
151 EXPECT_FALSE(set.Insert(nonce[5], 1110)); | 152 EXPECT_EQ(net::NONCE_INVALID_TIME_FAILURE, set.Insert(nonce[5], 1110)); |
152 | 153 |
153 // Insert beyond the valid range. | 154 // Insert beyond the valid range. |
154 SetNonce(nonce[5], 1117, kOrbit); | 155 SetNonce(nonce[5], 1117, kOrbit); |
155 nonce[5][31] = 2; | 156 nonce[5][31] = 2; |
156 EXPECT_FALSE(set.Insert(nonce[5], 1110)); | 157 EXPECT_EQ(net::NONCE_INVALID_TIME_FAILURE, set.Insert(nonce[5], 1110)); |
157 | 158 |
158 // Insert at the upper valid range. | 159 // Insert at the upper valid range. |
159 SetNonce(nonce[5], 1116, kOrbit); | 160 SetNonce(nonce[5], 1116, kOrbit); |
160 nonce[5][31] = 1; | 161 nonce[5][31] = 1; |
161 EXPECT_TRUE(set.Insert(nonce[5], 1110)); | 162 EXPECT_EQ(net::NONCE_OK, set.Insert(nonce[5], 1110)); |
162 | 163 |
163 // This should be beyond the upper valid range now: | 164 // This should be beyond the upper valid range now: |
164 SetNonce(nonce[5], 1116, kOrbit); | 165 SetNonce(nonce[5], 1116, kOrbit); |
165 nonce[5][31] = 2; | 166 nonce[5][31] = 2; |
166 EXPECT_FALSE(set.Insert(nonce[5], 1110)); | 167 EXPECT_EQ(net::NONCE_INVALID_TIME_FAILURE, set.Insert(nonce[5], 1110)); |
167 } | 168 } |
168 } | 169 } |
169 | 170 |
170 TEST(StrikeRegisterTest, InsertMany) { | 171 TEST(StrikeRegisterTest, InsertMany) { |
171 StrikeRegister set(5000 /* max size */, 1000 /* current time */, | 172 StrikeRegister set(5000 /* max size */, 1000 /* current time */, |
172 500 /* window secs */, kOrbit, | 173 500 /* window secs */, kOrbit, |
173 StrikeRegister::DENY_REQUESTS_AT_STARTUP); | 174 StrikeRegister::DENY_REQUESTS_AT_STARTUP); |
174 | 175 |
175 uint8 nonce[32]; | 176 uint8 nonce[32]; |
176 SetNonce(nonce, 1101, kOrbit); | 177 SetNonce(nonce, 1101, kOrbit); |
177 for (unsigned i = 0; i < 100000; i++) { | 178 for (unsigned i = 0; i < 100000; i++) { |
178 SetNonce(nonce, 1101 + i/500, kOrbit); | 179 SetNonce(nonce, 1101 + i/500, kOrbit); |
179 memcpy(nonce + 12, &i, sizeof(i)); | 180 memcpy(nonce + 12, &i, sizeof(i)); |
180 set.Insert(nonce, 1100); | 181 EXPECT_EQ(net::NONCE_INVALID_TIME_FAILURE, set.Insert(nonce, 1100)); |
181 } | 182 } |
182 } | 183 } |
183 | 184 |
184 | 185 |
185 // For the following test we create a slow, but simple, version of a | 186 // For the following test we create a slow, but simple, version of a |
186 // StrikeRegister. The behaviour of this object is much easier to understand | 187 // StrikeRegister. The behaviour of this object is much easier to understand |
187 // than the fully fledged version. We then create a test to show, empirically, | 188 // than the fully fledged version. We then create a test to show, empirically, |
188 // that the two objects have identical behaviour. | 189 // that the two objects have identical behaviour. |
189 | 190 |
190 // A SlowStrikeRegister has the same public interface as a StrikeRegister, but | 191 // A SlowStrikeRegister has the same public interface as a StrikeRegister, but |
191 // is much slower. Hopefully it is also more obviously correct and we can | 192 // is much slower. Hopefully it is also more obviously correct and we can |
192 // empirically test that their behaviours are identical. | 193 // empirically test that their behaviours are identical. |
193 class SlowStrikeRegister { | 194 class SlowStrikeRegister { |
194 public: | 195 public: |
195 SlowStrikeRegister(unsigned max_entries, uint32 current_time, | 196 SlowStrikeRegister(unsigned max_entries, uint32 current_time, |
196 uint32 window_secs, const uint8 orbit[8]) | 197 uint32 window_secs, const uint8 orbit[8]) |
197 : max_entries_(max_entries), | 198 : max_entries_(max_entries), |
198 window_secs_(window_secs), | 199 window_secs_(window_secs), |
199 creation_time_(current_time), | 200 creation_time_(current_time), |
200 horizon_(ExternalTimeToInternal(current_time + window_secs) + 1) { | 201 horizon_(ExternalTimeToInternal(current_time + window_secs) + 1) { |
201 memcpy(orbit_, orbit, sizeof(orbit_)); | 202 memcpy(orbit_, orbit, sizeof(orbit_)); |
202 } | 203 } |
203 | 204 |
204 bool Insert(const uint8 nonce_bytes[32], | 205 InsertStatus Insert(const uint8 nonce_bytes[32], |
205 const uint32 nonce_time_external, | 206 const uint32 nonce_time_external, |
206 const uint32 current_time_external) { | 207 const uint32 current_time_external) { |
207 if (nonces_.size() == max_entries_) { | 208 if (nonces_.size() == max_entries_) { |
208 DropOldestEntry(); | 209 DropOldestEntry(); |
209 } | 210 } |
210 | 211 |
211 const uint32 current_time = ExternalTimeToInternal(current_time_external); | 212 const uint32 current_time = ExternalTimeToInternal(current_time_external); |
212 | 213 |
213 // Check to see if the orbit is correct. | 214 // Check to see if the orbit is correct. |
214 if (memcmp(nonce_bytes + 4, orbit_, sizeof(orbit_))) { | 215 if (memcmp(nonce_bytes + 4, orbit_, sizeof(orbit_))) { |
215 return false; | 216 return net::NONCE_INVALID_ORBIT_FAILURE; |
216 } | 217 } |
217 const uint32 nonce_time = | 218 const uint32 nonce_time = |
218 ExternalTimeToInternal(TimeFromBytes(nonce_bytes)); | 219 ExternalTimeToInternal(TimeFromBytes(nonce_bytes)); |
219 EXPECT_EQ(ExternalTimeToInternal(nonce_time_external), nonce_time); | 220 EXPECT_EQ(ExternalTimeToInternal(nonce_time_external), nonce_time); |
220 // We have dropped one or more nonces with a time value of |horizon_ - 1|, | 221 // We have dropped one or more nonces with a time value of |horizon_ - 1|, |
221 // so we have to reject anything with a timestamp less than or | 222 // so we have to reject anything with a timestamp less than or |
222 // equal to that. | 223 // equal to that. |
223 if (nonce_time < horizon_) { | 224 if (nonce_time < horizon_) { |
224 return false; | 225 return net::NONCE_INVALID_TIME_FAILURE; |
225 } | 226 } |
226 | 227 |
227 // Check that the timestamp is in the current window. | 228 // Check that the timestamp is in the current window. |
228 if ((current_time > window_secs_ && | 229 if ((current_time > window_secs_ && |
229 nonce_time < (current_time - window_secs_)) || | 230 nonce_time < (current_time - window_secs_)) || |
230 nonce_time > (current_time + window_secs_)) { | 231 nonce_time > (current_time + window_secs_)) { |
231 return false; | 232 return net::NONCE_INVALID_TIME_FAILURE; |
232 } | 233 } |
233 | 234 |
234 pair<uint32, string> nonce = make_pair( | 235 pair<uint32, string> nonce = make_pair( |
235 nonce_time, | 236 nonce_time, |
236 string(reinterpret_cast<const char*>(nonce_bytes), 32)); | 237 string(reinterpret_cast<const char*>(nonce_bytes), 32)); |
237 | 238 |
238 set<pair<uint32, string> >::const_iterator it = nonces_.find(nonce); | 239 set<pair<uint32, string> >::const_iterator it = nonces_.find(nonce); |
239 if (it != nonces_.end()) { | 240 if (it != nonces_.end()) { |
240 return false; | 241 return net::NONCE_NOT_UNIQUE_FAILURE; |
241 } | 242 } |
242 | 243 |
243 nonces_.insert(nonce); | 244 nonces_.insert(nonce); |
244 return true; | 245 return net::NONCE_OK; |
245 } | 246 } |
246 | 247 |
247 uint32 GetCurrentValidWindowSecs(const uint32 current_time_external) const { | 248 uint32 GetCurrentValidWindowSecs(const uint32 current_time_external) const { |
248 const uint32 current_time = ExternalTimeToInternal(current_time_external); | 249 const uint32 current_time = ExternalTimeToInternal(current_time_external); |
249 if (horizon_ > current_time) { | 250 if (horizon_ > current_time) { |
250 return 0; | 251 return 0; |
251 } | 252 } |
252 return 1 + min(current_time - horizon_, window_secs_); | 253 return 1 + min(current_time - horizon_, window_secs_); |
253 } | 254 } |
254 | 255 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 const uint32 time = current_time + i; | 308 const uint32 time = current_time + i; |
308 | 309 |
309 uint8 nonce[32]; | 310 uint8 nonce[32]; |
310 SetNonce(nonce, time, kOrbit); | 311 SetNonce(nonce, time, kOrbit); |
311 | 312 |
312 // There are 2048 possible nonce values: | 313 // There are 2048 possible nonce values: |
313 const uint32 v = rand() % 2048; | 314 const uint32 v = rand() % 2048; |
314 nonce[30] = v >> 8; | 315 nonce[30] = v >> 8; |
315 nonce[31] = v; | 316 nonce[31] = v; |
316 | 317 |
317 const bool r2 = s2->Insert(nonce, time, time); | 318 const InsertStatus nonce_error2 = s2->Insert(nonce, time, time); |
318 const bool r1 = s1->Insert(nonce, time); | 319 const InsertStatus nonce_error1 = s1->Insert(nonce, time); |
319 EXPECT_EQ(r1, r2); | 320 EXPECT_EQ(nonce_error1, nonce_error2); |
| 321 |
320 // Inserts succeed after the startup period. | 322 // Inserts succeed after the startup period. |
321 EXPECT_EQ(time > current_time + window, r1); | 323 if (time > current_time + window) { |
| 324 EXPECT_EQ(net::NONCE_OK, nonce_error1); |
| 325 } else { |
| 326 EXPECT_EQ(net::NONCE_INVALID_TIME_FAILURE, nonce_error1); |
| 327 } |
322 EXPECT_EQ(s1->GetCurrentValidWindowSecs(time), | 328 EXPECT_EQ(s1->GetCurrentValidWindowSecs(time), |
323 s2->GetCurrentValidWindowSecs(time)); | 329 s2->GetCurrentValidWindowSecs(time)); |
324 | 330 |
325 if (i % 10 == 0) { | 331 if (i % 10 == 0) { |
326 s1->Validate(); | 332 s1->Validate(); |
327 } | 333 } |
328 | 334 |
329 if (HasFailure()) { | 335 if (HasFailure()) { |
330 break; | 336 break; |
331 } | 337 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 } | 378 } |
373 | 379 |
374 uint8 nonce[32]; | 380 uint8 nonce[32]; |
375 SetNonce(nonce, time, kOrbit); | 381 SetNonce(nonce, time, kOrbit); |
376 | 382 |
377 // There are 2048 possible nonce values: | 383 // There are 2048 possible nonce values: |
378 const uint32 v = rand() % 2048; | 384 const uint32 v = rand() % 2048; |
379 nonce[30] = v >> 8; | 385 nonce[30] = v >> 8; |
380 nonce[31] = v; | 386 nonce[31] = v; |
381 | 387 |
382 const bool r2 = s2->Insert(nonce, time, time); | 388 const InsertStatus nonce_error2 = s2->Insert(nonce, time, time); |
383 const bool r1 = s1->Insert(nonce, time); | 389 const InsertStatus nonce_error1 = s1->Insert(nonce, time); |
384 EXPECT_EQ(r1, r2); | 390 EXPECT_EQ(nonce_error1, nonce_error2); |
385 EXPECT_EQ(s1->GetCurrentValidWindowSecs(time), | 391 EXPECT_EQ(s1->GetCurrentValidWindowSecs(time), |
386 s2->GetCurrentValidWindowSecs(time)); | 392 s2->GetCurrentValidWindowSecs(time)); |
387 | 393 |
388 if (i % 10 == 0) { | 394 if (i % 10 == 0) { |
389 s1->Validate(); | 395 s1->Validate(); |
390 } | 396 } |
391 | 397 |
392 if (HasFailure()) { | 398 if (HasFailure()) { |
393 break; | 399 break; |
394 } | 400 } |
395 } | 401 } |
396 | 402 |
397 if (i != kMaxIterations) { | 403 if (i != kMaxIterations) { |
398 FAIL() << "Failed after " << i << " iterations"; | 404 FAIL() << "Failed after " << i << " iterations"; |
399 } | 405 } |
400 } | 406 } |
401 | 407 |
402 } // anonymous namespace | 408 } // anonymous namespace |
OLD | NEW |