Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Side by Side Diff: net/ssl/channel_id_service_unittest.cc

Issue 2109503009: Refactor net tests to use GMock matchers for checking net::Error results (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Revert changes to contents.txt files Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/spdy/spdy_test_util_common.cc ('k') | net/ssl/default_channel_id_store_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/ssl/channel_id_service.h" 5 #include "net/ssl/channel_id_service.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/location.h" 12 #include "base/location.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/run_loop.h" 14 #include "base/run_loop.h"
15 #include "base/single_thread_task_runner.h" 15 #include "base/single_thread_task_runner.h"
16 #include "base/strings/string_number_conversions.h" 16 #include "base/strings/string_number_conversions.h"
17 #include "base/task_runner.h" 17 #include "base/task_runner.h"
18 #include "base/threading/thread_task_runner_handle.h" 18 #include "base/threading/thread_task_runner_handle.h"
19 #include "crypto/ec_private_key.h" 19 #include "crypto/ec_private_key.h"
20 #include "net/base/net_errors.h" 20 #include "net/base/net_errors.h"
21 #include "net/base/test_completion_callback.h" 21 #include "net/base/test_completion_callback.h"
22 #include "net/cert/asn1_util.h" 22 #include "net/cert/asn1_util.h"
23 #include "net/cert/x509_certificate.h" 23 #include "net/cert/x509_certificate.h"
24 #include "net/ssl/default_channel_id_store.h" 24 #include "net/ssl/default_channel_id_store.h"
25 #include "net/test/channel_id_test_util.h" 25 #include "net/test/channel_id_test_util.h"
26 #include "net/test/gtest_util.h"
27 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
27 29
30 using net::test::IsError;
31 using net::test::IsOk;
32
28 namespace net { 33 namespace net {
29 34
30 namespace { 35 namespace {
31 36
32 void FailTest(int /* result */) { 37 void FailTest(int /* result */) {
33 FAIL(); 38 FAIL();
34 } 39 }
35 40
36 // Simple task runner that refuses to actually post any tasks. This simulates 41 // Simple task runner that refuses to actually post any tasks. This simulates
37 // a TaskRunner that has been shutdown, by returning false for any attempt to 42 // a TaskRunner that has been shutdown, by returning false for any attempt to
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 std::string host("encrypted.google.com"); 141 std::string host("encrypted.google.com");
137 142
138 int error; 143 int error;
139 TestCompletionCallback callback; 144 TestCompletionCallback callback;
140 ChannelIDService::Request request; 145 ChannelIDService::Request request;
141 146
142 // Synchronous completion, because the store is initialized. 147 // Synchronous completion, because the store is initialized.
143 std::unique_ptr<crypto::ECPrivateKey> key; 148 std::unique_ptr<crypto::ECPrivateKey> key;
144 EXPECT_EQ(0, service_->channel_id_count()); 149 EXPECT_EQ(0, service_->channel_id_count());
145 error = service_->GetChannelID(host, &key, callback.callback(), &request); 150 error = service_->GetChannelID(host, &key, callback.callback(), &request);
146 EXPECT_EQ(ERR_FILE_NOT_FOUND, error); 151 EXPECT_THAT(error, IsError(ERR_FILE_NOT_FOUND));
147 EXPECT_FALSE(request.is_active()); 152 EXPECT_FALSE(request.is_active());
148 EXPECT_EQ(0, service_->channel_id_count()); 153 EXPECT_EQ(0, service_->channel_id_count());
149 EXPECT_FALSE(key); 154 EXPECT_FALSE(key);
150 } 155 }
151 156
152 TEST_F(ChannelIDServiceTest, CacheHit) { 157 TEST_F(ChannelIDServiceTest, CacheHit) {
153 std::string host("encrypted.google.com"); 158 std::string host("encrypted.google.com");
154 159
155 int error; 160 int error;
156 TestCompletionCallback callback; 161 TestCompletionCallback callback;
157 ChannelIDService::Request request; 162 ChannelIDService::Request request;
158 163
159 // Asynchronous completion. 164 // Asynchronous completion.
160 std::unique_ptr<crypto::ECPrivateKey> key1; 165 std::unique_ptr<crypto::ECPrivateKey> key1;
161 EXPECT_EQ(0, service_->channel_id_count()); 166 EXPECT_EQ(0, service_->channel_id_count());
162 error = service_->GetOrCreateChannelID(host, &key1, callback.callback(), 167 error = service_->GetOrCreateChannelID(host, &key1, callback.callback(),
163 &request); 168 &request);
164 EXPECT_EQ(ERR_IO_PENDING, error); 169 EXPECT_THAT(error, IsError(ERR_IO_PENDING));
165 EXPECT_TRUE(request.is_active()); 170 EXPECT_TRUE(request.is_active());
166 error = callback.WaitForResult(); 171 error = callback.WaitForResult();
167 EXPECT_EQ(OK, error); 172 EXPECT_THAT(error, IsOk());
168 EXPECT_EQ(1, service_->channel_id_count()); 173 EXPECT_EQ(1, service_->channel_id_count());
169 EXPECT_TRUE(key1); 174 EXPECT_TRUE(key1);
170 EXPECT_FALSE(request.is_active()); 175 EXPECT_FALSE(request.is_active());
171 176
172 // Synchronous completion. 177 // Synchronous completion.
173 std::unique_ptr<crypto::ECPrivateKey> key2; 178 std::unique_ptr<crypto::ECPrivateKey> key2;
174 error = service_->GetOrCreateChannelID(host, &key2, callback.callback(), 179 error = service_->GetOrCreateChannelID(host, &key2, callback.callback(),
175 &request); 180 &request);
176 EXPECT_FALSE(request.is_active()); 181 EXPECT_FALSE(request.is_active());
177 EXPECT_EQ(OK, error); 182 EXPECT_THAT(error, IsOk());
178 EXPECT_EQ(1, service_->channel_id_count()); 183 EXPECT_EQ(1, service_->channel_id_count());
179 EXPECT_TRUE(KeysEqual(key1.get(), key2.get())); 184 EXPECT_TRUE(KeysEqual(key1.get(), key2.get()));
180 185
181 // Synchronous get. 186 // Synchronous get.
182 std::unique_ptr<crypto::ECPrivateKey> key3; 187 std::unique_ptr<crypto::ECPrivateKey> key3;
183 error = service_->GetChannelID(host, &key3, callback.callback(), &request); 188 error = service_->GetChannelID(host, &key3, callback.callback(), &request);
184 EXPECT_FALSE(request.is_active()); 189 EXPECT_FALSE(request.is_active());
185 EXPECT_EQ(OK, error); 190 EXPECT_THAT(error, IsOk());
186 EXPECT_EQ(1, service_->channel_id_count()); 191 EXPECT_EQ(1, service_->channel_id_count());
187 EXPECT_TRUE(KeysEqual(key1.get(), key3.get())); 192 EXPECT_TRUE(KeysEqual(key1.get(), key3.get()));
188 193
189 EXPECT_EQ(3u, service_->requests()); 194 EXPECT_EQ(3u, service_->requests());
190 EXPECT_EQ(2u, service_->key_store_hits()); 195 EXPECT_EQ(2u, service_->key_store_hits());
191 EXPECT_EQ(0u, service_->inflight_joins()); 196 EXPECT_EQ(0u, service_->inflight_joins());
192 } 197 }
193 198
194 TEST_F(ChannelIDServiceTest, StoreChannelIDs) { 199 TEST_F(ChannelIDServiceTest, StoreChannelIDs) {
195 int error; 200 int error;
196 TestCompletionCallback callback; 201 TestCompletionCallback callback;
197 ChannelIDService::Request request; 202 ChannelIDService::Request request;
198 203
199 std::string host1("encrypted.google.com"); 204 std::string host1("encrypted.google.com");
200 std::unique_ptr<crypto::ECPrivateKey> key1; 205 std::unique_ptr<crypto::ECPrivateKey> key1;
201 EXPECT_EQ(0, service_->channel_id_count()); 206 EXPECT_EQ(0, service_->channel_id_count());
202 error = service_->GetOrCreateChannelID(host1, &key1, callback.callback(), 207 error = service_->GetOrCreateChannelID(host1, &key1, callback.callback(),
203 &request); 208 &request);
204 EXPECT_EQ(ERR_IO_PENDING, error); 209 EXPECT_THAT(error, IsError(ERR_IO_PENDING));
205 EXPECT_TRUE(request.is_active()); 210 EXPECT_TRUE(request.is_active());
206 error = callback.WaitForResult(); 211 error = callback.WaitForResult();
207 EXPECT_EQ(OK, error); 212 EXPECT_THAT(error, IsOk());
208 EXPECT_EQ(1, service_->channel_id_count()); 213 EXPECT_EQ(1, service_->channel_id_count());
209 214
210 std::string host2("www.verisign.com"); 215 std::string host2("www.verisign.com");
211 std::unique_ptr<crypto::ECPrivateKey> key2; 216 std::unique_ptr<crypto::ECPrivateKey> key2;
212 error = service_->GetOrCreateChannelID(host2, &key2, callback.callback(), 217 error = service_->GetOrCreateChannelID(host2, &key2, callback.callback(),
213 &request); 218 &request);
214 EXPECT_EQ(ERR_IO_PENDING, error); 219 EXPECT_THAT(error, IsError(ERR_IO_PENDING));
215 EXPECT_TRUE(request.is_active()); 220 EXPECT_TRUE(request.is_active());
216 error = callback.WaitForResult(); 221 error = callback.WaitForResult();
217 EXPECT_EQ(OK, error); 222 EXPECT_THAT(error, IsOk());
218 EXPECT_EQ(2, service_->channel_id_count()); 223 EXPECT_EQ(2, service_->channel_id_count());
219 224
220 std::string host3("www.twitter.com"); 225 std::string host3("www.twitter.com");
221 std::unique_ptr<crypto::ECPrivateKey> key3; 226 std::unique_ptr<crypto::ECPrivateKey> key3;
222 error = service_->GetOrCreateChannelID(host3, &key3, callback.callback(), 227 error = service_->GetOrCreateChannelID(host3, &key3, callback.callback(),
223 &request); 228 &request);
224 EXPECT_EQ(ERR_IO_PENDING, error); 229 EXPECT_THAT(error, IsError(ERR_IO_PENDING));
225 EXPECT_TRUE(request.is_active()); 230 EXPECT_TRUE(request.is_active());
226 error = callback.WaitForResult(); 231 error = callback.WaitForResult();
227 EXPECT_EQ(OK, error); 232 EXPECT_THAT(error, IsOk());
228 EXPECT_EQ(3, service_->channel_id_count()); 233 EXPECT_EQ(3, service_->channel_id_count());
229 234
230 EXPECT_FALSE(KeysEqual(key1.get(), key2.get())); 235 EXPECT_FALSE(KeysEqual(key1.get(), key2.get()));
231 EXPECT_FALSE(KeysEqual(key1.get(), key3.get())); 236 EXPECT_FALSE(KeysEqual(key1.get(), key3.get()));
232 EXPECT_FALSE(KeysEqual(key2.get(), key3.get())); 237 EXPECT_FALSE(KeysEqual(key2.get(), key3.get()));
233 } 238 }
234 239
235 // Tests an inflight join. 240 // Tests an inflight join.
236 TEST_F(ChannelIDServiceTest, InflightJoin) { 241 TEST_F(ChannelIDServiceTest, InflightJoin) {
237 std::string host("encrypted.google.com"); 242 std::string host("encrypted.google.com");
238 int error; 243 int error;
239 244
240 std::unique_ptr<crypto::ECPrivateKey> key1; 245 std::unique_ptr<crypto::ECPrivateKey> key1;
241 TestCompletionCallback callback1; 246 TestCompletionCallback callback1;
242 ChannelIDService::Request request1; 247 ChannelIDService::Request request1;
243 248
244 std::unique_ptr<crypto::ECPrivateKey> key2; 249 std::unique_ptr<crypto::ECPrivateKey> key2;
245 TestCompletionCallback callback2; 250 TestCompletionCallback callback2;
246 ChannelIDService::Request request2; 251 ChannelIDService::Request request2;
247 252
248 error = service_->GetOrCreateChannelID(host, &key1, callback1.callback(), 253 error = service_->GetOrCreateChannelID(host, &key1, callback1.callback(),
249 &request1); 254 &request1);
250 EXPECT_EQ(ERR_IO_PENDING, error); 255 EXPECT_THAT(error, IsError(ERR_IO_PENDING));
251 EXPECT_TRUE(request1.is_active()); 256 EXPECT_TRUE(request1.is_active());
252 // Should join with the original request. 257 // Should join with the original request.
253 error = service_->GetOrCreateChannelID(host, &key2, callback2.callback(), 258 error = service_->GetOrCreateChannelID(host, &key2, callback2.callback(),
254 &request2); 259 &request2);
255 EXPECT_EQ(ERR_IO_PENDING, error); 260 EXPECT_THAT(error, IsError(ERR_IO_PENDING));
256 EXPECT_TRUE(request2.is_active()); 261 EXPECT_TRUE(request2.is_active());
257 262
258 error = callback1.WaitForResult(); 263 error = callback1.WaitForResult();
259 EXPECT_EQ(OK, error); 264 EXPECT_THAT(error, IsOk());
260 error = callback2.WaitForResult(); 265 error = callback2.WaitForResult();
261 EXPECT_EQ(OK, error); 266 EXPECT_THAT(error, IsOk());
262 267
263 EXPECT_EQ(2u, service_->requests()); 268 EXPECT_EQ(2u, service_->requests());
264 EXPECT_EQ(0u, service_->key_store_hits()); 269 EXPECT_EQ(0u, service_->key_store_hits());
265 EXPECT_EQ(1u, service_->inflight_joins()); 270 EXPECT_EQ(1u, service_->inflight_joins());
266 EXPECT_EQ(1u, service_->workers_created()); 271 EXPECT_EQ(1u, service_->workers_created());
267 } 272 }
268 273
269 // Tests an inflight join of a Get request to a GetOrCreate request. 274 // Tests an inflight join of a Get request to a GetOrCreate request.
270 TEST_F(ChannelIDServiceTest, InflightJoinGetOrCreateAndGet) { 275 TEST_F(ChannelIDServiceTest, InflightJoinGetOrCreateAndGet) {
271 std::string host("encrypted.google.com"); 276 std::string host("encrypted.google.com");
272 int error; 277 int error;
273 278
274 std::unique_ptr<crypto::ECPrivateKey> key1; 279 std::unique_ptr<crypto::ECPrivateKey> key1;
275 TestCompletionCallback callback1; 280 TestCompletionCallback callback1;
276 ChannelIDService::Request request1; 281 ChannelIDService::Request request1;
277 282
278 std::unique_ptr<crypto::ECPrivateKey> key2; 283 std::unique_ptr<crypto::ECPrivateKey> key2;
279 TestCompletionCallback callback2; 284 TestCompletionCallback callback2;
280 ChannelIDService::Request request2; 285 ChannelIDService::Request request2;
281 286
282 error = service_->GetOrCreateChannelID(host, &key1, callback1.callback(), 287 error = service_->GetOrCreateChannelID(host, &key1, callback1.callback(),
283 &request1); 288 &request1);
284 EXPECT_EQ(ERR_IO_PENDING, error); 289 EXPECT_THAT(error, IsError(ERR_IO_PENDING));
285 EXPECT_TRUE(request1.is_active()); 290 EXPECT_TRUE(request1.is_active());
286 // Should join with the original request. 291 // Should join with the original request.
287 error = service_->GetChannelID(host, &key2, callback2.callback(), &request2); 292 error = service_->GetChannelID(host, &key2, callback2.callback(), &request2);
288 EXPECT_EQ(ERR_IO_PENDING, error); 293 EXPECT_THAT(error, IsError(ERR_IO_PENDING));
289 EXPECT_TRUE(request2.is_active()); 294 EXPECT_TRUE(request2.is_active());
290 295
291 error = callback1.WaitForResult(); 296 error = callback1.WaitForResult();
292 EXPECT_EQ(OK, error); 297 EXPECT_THAT(error, IsOk());
293 error = callback2.WaitForResult(); 298 error = callback2.WaitForResult();
294 EXPECT_EQ(OK, error); 299 EXPECT_THAT(error, IsOk());
295 EXPECT_TRUE(KeysEqual(key1.get(), key2.get())); 300 EXPECT_TRUE(KeysEqual(key1.get(), key2.get()));
296 301
297 EXPECT_EQ(2u, service_->requests()); 302 EXPECT_EQ(2u, service_->requests());
298 EXPECT_EQ(0u, service_->key_store_hits()); 303 EXPECT_EQ(0u, service_->key_store_hits());
299 EXPECT_EQ(1u, service_->inflight_joins()); 304 EXPECT_EQ(1u, service_->inflight_joins());
300 EXPECT_EQ(1u, service_->workers_created()); 305 EXPECT_EQ(1u, service_->workers_created());
301 } 306 }
302 307
303 // Tests that the callback of a canceled request is never made. 308 // Tests that the callback of a canceled request is never made.
304 TEST_F(ChannelIDServiceTest, CancelRequest) { 309 TEST_F(ChannelIDServiceTest, CancelRequest) {
305 std::string host("encrypted.google.com"); 310 std::string host("encrypted.google.com");
306 std::unique_ptr<crypto::ECPrivateKey> key; 311 std::unique_ptr<crypto::ECPrivateKey> key;
307 int error; 312 int error;
308 ChannelIDService::Request request; 313 ChannelIDService::Request request;
309 314
310 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), 315 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest),
311 &request); 316 &request);
312 EXPECT_EQ(ERR_IO_PENDING, error); 317 EXPECT_THAT(error, IsError(ERR_IO_PENDING));
313 EXPECT_TRUE(request.is_active()); 318 EXPECT_TRUE(request.is_active());
314 request.Cancel(); 319 request.Cancel();
315 EXPECT_FALSE(request.is_active()); 320 EXPECT_FALSE(request.is_active());
316 321
317 // Wait for reply from ChannelIDServiceWorker to be posted back to the 322 // Wait for reply from ChannelIDServiceWorker to be posted back to the
318 // ChannelIDService. 323 // ChannelIDService.
319 base::RunLoop().RunUntilIdle(); 324 base::RunLoop().RunUntilIdle();
320 325
321 // Even though the original request was cancelled, the service will still 326 // Even though the original request was cancelled, the service will still
322 // store the result, it just doesn't call the callback. 327 // store the result, it just doesn't call the callback.
323 EXPECT_EQ(1, service_->channel_id_count()); 328 EXPECT_EQ(1, service_->channel_id_count());
324 } 329 }
325 330
326 // Tests that destructing the Request cancels the request. 331 // Tests that destructing the Request cancels the request.
327 TEST_F(ChannelIDServiceTest, CancelRequestByHandleDestruction) { 332 TEST_F(ChannelIDServiceTest, CancelRequestByHandleDestruction) {
328 std::string host("encrypted.google.com"); 333 std::string host("encrypted.google.com");
329 std::unique_ptr<crypto::ECPrivateKey> key; 334 std::unique_ptr<crypto::ECPrivateKey> key;
330 int error; 335 int error;
331 std::unique_ptr<ChannelIDService::Request> request( 336 std::unique_ptr<ChannelIDService::Request> request(
332 new ChannelIDService::Request()); 337 new ChannelIDService::Request());
333 338
334 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), 339 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest),
335 request.get()); 340 request.get());
336 EXPECT_EQ(ERR_IO_PENDING, error); 341 EXPECT_THAT(error, IsError(ERR_IO_PENDING));
337 EXPECT_TRUE(request->is_active()); 342 EXPECT_TRUE(request->is_active());
338 343
339 // Delete the Request object. 344 // Delete the Request object.
340 request.reset(); 345 request.reset();
341 346
342 // Wait for reply from ChannelIDServiceWorker to be posted back to the 347 // Wait for reply from ChannelIDServiceWorker to be posted back to the
343 // ChannelIDService. 348 // ChannelIDService.
344 base::RunLoop().RunUntilIdle(); 349 base::RunLoop().RunUntilIdle();
345 350
346 // Even though the original request was cancelled, the service will still 351 // Even though the original request was cancelled, the service will still
347 // store the result, it just doesn't call the callback. 352 // store the result, it just doesn't call the callback.
348 EXPECT_EQ(1, service_->channel_id_count()); 353 EXPECT_EQ(1, service_->channel_id_count());
349 } 354 }
350 355
351 TEST_F(ChannelIDServiceTest, DestructionWithPendingRequest) { 356 TEST_F(ChannelIDServiceTest, DestructionWithPendingRequest) {
352 std::string host("encrypted.google.com"); 357 std::string host("encrypted.google.com");
353 std::unique_ptr<crypto::ECPrivateKey> key; 358 std::unique_ptr<crypto::ECPrivateKey> key;
354 int error; 359 int error;
355 ChannelIDService::Request request; 360 ChannelIDService::Request request;
356 361
357 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), 362 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest),
358 &request); 363 &request);
359 EXPECT_EQ(ERR_IO_PENDING, error); 364 EXPECT_THAT(error, IsError(ERR_IO_PENDING));
360 EXPECT_TRUE(request.is_active()); 365 EXPECT_TRUE(request.is_active());
361 366
362 // Cancel request and destroy the ChannelIDService. 367 // Cancel request and destroy the ChannelIDService.
363 request.Cancel(); 368 request.Cancel();
364 service_.reset(); 369 service_.reset();
365 370
366 // ChannelIDServiceWorker should not post anything back to the 371 // ChannelIDServiceWorker should not post anything back to the
367 // non-existent ChannelIDService, but run the loop just to be sure it 372 // non-existent ChannelIDService, but run the loop just to be sure it
368 // doesn't. 373 // doesn't.
369 base::RunLoop().RunUntilIdle(); 374 base::RunLoop().RunUntilIdle();
(...skipping 11 matching lines...) Expand all
381 386
382 // Make a request that will force synchronous completion. 387 // Make a request that will force synchronous completion.
383 std::string host("encrypted.google.com"); 388 std::string host("encrypted.google.com");
384 std::unique_ptr<crypto::ECPrivateKey> key; 389 std::unique_ptr<crypto::ECPrivateKey> key;
385 int error; 390 int error;
386 ChannelIDService::Request request; 391 ChannelIDService::Request request;
387 392
388 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), 393 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest),
389 &request); 394 &request);
390 // If we got here without crashing or a valgrind error, it worked. 395 // If we got here without crashing or a valgrind error, it worked.
391 ASSERT_EQ(ERR_INSUFFICIENT_RESOURCES, error); 396 ASSERT_THAT(error, IsError(ERR_INSUFFICIENT_RESOURCES));
392 EXPECT_FALSE(request.is_active()); 397 EXPECT_FALSE(request.is_active());
393 } 398 }
394 399
395 // Tests that simultaneous creation of different certs works. 400 // Tests that simultaneous creation of different certs works.
396 TEST_F(ChannelIDServiceTest, SimultaneousCreation) { 401 TEST_F(ChannelIDServiceTest, SimultaneousCreation) {
397 int error; 402 int error;
398 403
399 std::string host1("encrypted.google.com"); 404 std::string host1("encrypted.google.com");
400 std::unique_ptr<crypto::ECPrivateKey> key1; 405 std::unique_ptr<crypto::ECPrivateKey> key1;
401 TestCompletionCallback callback1; 406 TestCompletionCallback callback1;
402 ChannelIDService::Request request1; 407 ChannelIDService::Request request1;
403 408
404 std::string host2("foo.com"); 409 std::string host2("foo.com");
405 std::unique_ptr<crypto::ECPrivateKey> key2; 410 std::unique_ptr<crypto::ECPrivateKey> key2;
406 TestCompletionCallback callback2; 411 TestCompletionCallback callback2;
407 ChannelIDService::Request request2; 412 ChannelIDService::Request request2;
408 413
409 std::string host3("bar.com"); 414 std::string host3("bar.com");
410 std::unique_ptr<crypto::ECPrivateKey> key3; 415 std::unique_ptr<crypto::ECPrivateKey> key3;
411 TestCompletionCallback callback3; 416 TestCompletionCallback callback3;
412 ChannelIDService::Request request3; 417 ChannelIDService::Request request3;
413 418
414 error = service_->GetOrCreateChannelID(host1, &key1, callback1.callback(), 419 error = service_->GetOrCreateChannelID(host1, &key1, callback1.callback(),
415 &request1); 420 &request1);
416 EXPECT_EQ(ERR_IO_PENDING, error); 421 EXPECT_THAT(error, IsError(ERR_IO_PENDING));
417 EXPECT_TRUE(request1.is_active()); 422 EXPECT_TRUE(request1.is_active());
418 423
419 error = service_->GetOrCreateChannelID(host2, &key2, callback2.callback(), 424 error = service_->GetOrCreateChannelID(host2, &key2, callback2.callback(),
420 &request2); 425 &request2);
421 EXPECT_EQ(ERR_IO_PENDING, error); 426 EXPECT_THAT(error, IsError(ERR_IO_PENDING));
422 EXPECT_TRUE(request2.is_active()); 427 EXPECT_TRUE(request2.is_active());
423 428
424 error = service_->GetOrCreateChannelID(host3, &key3, callback3.callback(), 429 error = service_->GetOrCreateChannelID(host3, &key3, callback3.callback(),
425 &request3); 430 &request3);
426 EXPECT_EQ(ERR_IO_PENDING, error); 431 EXPECT_THAT(error, IsError(ERR_IO_PENDING));
427 EXPECT_TRUE(request3.is_active()); 432 EXPECT_TRUE(request3.is_active());
428 433
429 error = callback1.WaitForResult(); 434 error = callback1.WaitForResult();
430 EXPECT_EQ(OK, error); 435 EXPECT_THAT(error, IsOk());
431 EXPECT_TRUE(key1); 436 EXPECT_TRUE(key1);
432 437
433 error = callback2.WaitForResult(); 438 error = callback2.WaitForResult();
434 EXPECT_EQ(OK, error); 439 EXPECT_THAT(error, IsOk());
435 EXPECT_TRUE(key2); 440 EXPECT_TRUE(key2);
436 441
437 error = callback3.WaitForResult(); 442 error = callback3.WaitForResult();
438 EXPECT_EQ(OK, error); 443 EXPECT_THAT(error, IsOk());
439 EXPECT_TRUE(key3); 444 EXPECT_TRUE(key3);
440 445
441 EXPECT_FALSE(KeysEqual(key1.get(), key2.get())); 446 EXPECT_FALSE(KeysEqual(key1.get(), key2.get()));
442 EXPECT_FALSE(KeysEqual(key1.get(), key3.get())); 447 EXPECT_FALSE(KeysEqual(key1.get(), key3.get()));
443 EXPECT_FALSE(KeysEqual(key2.get(), key3.get())); 448 EXPECT_FALSE(KeysEqual(key2.get(), key3.get()));
444 449
445 EXPECT_EQ(3, service_->channel_id_count()); 450 EXPECT_EQ(3, service_->channel_id_count());
446 } 451 }
447 452
448 TEST_F(ChannelIDServiceTest, AsyncStoreGetOrCreateNoChannelIDsInStore) { 453 TEST_F(ChannelIDServiceTest, AsyncStoreGetOrCreateNoChannelIDsInStore) {
449 MockChannelIDStoreWithAsyncGet* mock_store = 454 MockChannelIDStoreWithAsyncGet* mock_store =
450 new MockChannelIDStoreWithAsyncGet(); 455 new MockChannelIDStoreWithAsyncGet();
451 service_ = std::unique_ptr<ChannelIDService>( 456 service_ = std::unique_ptr<ChannelIDService>(
452 new ChannelIDService(mock_store, base::ThreadTaskRunnerHandle::Get())); 457 new ChannelIDService(mock_store, base::ThreadTaskRunnerHandle::Get()));
453 458
454 std::string host("encrypted.google.com"); 459 std::string host("encrypted.google.com");
455 460
456 int error; 461 int error;
457 TestCompletionCallback callback; 462 TestCompletionCallback callback;
458 ChannelIDService::Request request; 463 ChannelIDService::Request request;
459 464
460 // Asynchronous completion with no certs in the store. 465 // Asynchronous completion with no certs in the store.
461 std::unique_ptr<crypto::ECPrivateKey> key; 466 std::unique_ptr<crypto::ECPrivateKey> key;
462 EXPECT_EQ(0, service_->channel_id_count()); 467 EXPECT_EQ(0, service_->channel_id_count());
463 error = 468 error =
464 service_->GetOrCreateChannelID(host, &key, callback.callback(), &request); 469 service_->GetOrCreateChannelID(host, &key, callback.callback(), &request);
465 EXPECT_EQ(ERR_IO_PENDING, error); 470 EXPECT_THAT(error, IsError(ERR_IO_PENDING));
466 EXPECT_TRUE(request.is_active()); 471 EXPECT_TRUE(request.is_active());
467 472
468 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr); 473 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr);
469 474
470 error = callback.WaitForResult(); 475 error = callback.WaitForResult();
471 EXPECT_EQ(OK, error); 476 EXPECT_THAT(error, IsOk());
472 EXPECT_EQ(1, service_->channel_id_count()); 477 EXPECT_EQ(1, service_->channel_id_count());
473 EXPECT_TRUE(key); 478 EXPECT_TRUE(key);
474 EXPECT_FALSE(request.is_active()); 479 EXPECT_FALSE(request.is_active());
475 } 480 }
476 481
477 TEST_F(ChannelIDServiceTest, AsyncStoreGetNoChannelIDsInStore) { 482 TEST_F(ChannelIDServiceTest, AsyncStoreGetNoChannelIDsInStore) {
478 MockChannelIDStoreWithAsyncGet* mock_store = 483 MockChannelIDStoreWithAsyncGet* mock_store =
479 new MockChannelIDStoreWithAsyncGet(); 484 new MockChannelIDStoreWithAsyncGet();
480 service_ = std::unique_ptr<ChannelIDService>( 485 service_ = std::unique_ptr<ChannelIDService>(
481 new ChannelIDService(mock_store, base::ThreadTaskRunnerHandle::Get())); 486 new ChannelIDService(mock_store, base::ThreadTaskRunnerHandle::Get()));
482 487
483 std::string host("encrypted.google.com"); 488 std::string host("encrypted.google.com");
484 489
485 int error; 490 int error;
486 TestCompletionCallback callback; 491 TestCompletionCallback callback;
487 ChannelIDService::Request request; 492 ChannelIDService::Request request;
488 493
489 // Asynchronous completion with no certs in the store. 494 // Asynchronous completion with no certs in the store.
490 std::unique_ptr<crypto::ECPrivateKey> key; 495 std::unique_ptr<crypto::ECPrivateKey> key;
491 EXPECT_EQ(0, service_->channel_id_count()); 496 EXPECT_EQ(0, service_->channel_id_count());
492 error = service_->GetChannelID(host, &key, callback.callback(), &request); 497 error = service_->GetChannelID(host, &key, callback.callback(), &request);
493 EXPECT_EQ(ERR_IO_PENDING, error); 498 EXPECT_THAT(error, IsError(ERR_IO_PENDING));
494 EXPECT_TRUE(request.is_active()); 499 EXPECT_TRUE(request.is_active());
495 500
496 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr); 501 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr);
497 502
498 error = callback.WaitForResult(); 503 error = callback.WaitForResult();
499 EXPECT_EQ(ERR_FILE_NOT_FOUND, error); 504 EXPECT_THAT(error, IsError(ERR_FILE_NOT_FOUND));
500 EXPECT_EQ(0, service_->channel_id_count()); 505 EXPECT_EQ(0, service_->channel_id_count());
501 EXPECT_EQ(0u, service_->workers_created()); 506 EXPECT_EQ(0u, service_->workers_created());
502 EXPECT_FALSE(key); 507 EXPECT_FALSE(key);
503 EXPECT_FALSE(request.is_active()); 508 EXPECT_FALSE(request.is_active());
504 } 509 }
505 510
506 TEST_F(ChannelIDServiceTest, AsyncStoreGetOrCreateOneCertInStore) { 511 TEST_F(ChannelIDServiceTest, AsyncStoreGetOrCreateOneCertInStore) {
507 MockChannelIDStoreWithAsyncGet* mock_store = 512 MockChannelIDStoreWithAsyncGet* mock_store =
508 new MockChannelIDStoreWithAsyncGet(); 513 new MockChannelIDStoreWithAsyncGet();
509 service_ = std::unique_ptr<ChannelIDService>( 514 service_ = std::unique_ptr<ChannelIDService>(
510 new ChannelIDService(mock_store, base::ThreadTaskRunnerHandle::Get())); 515 new ChannelIDService(mock_store, base::ThreadTaskRunnerHandle::Get()));
511 516
512 std::string host("encrypted.google.com"); 517 std::string host("encrypted.google.com");
513 518
514 int error; 519 int error;
515 TestCompletionCallback callback; 520 TestCompletionCallback callback;
516 ChannelIDService::Request request; 521 ChannelIDService::Request request;
517 522
518 // Asynchronous completion with a cert in the store. 523 // Asynchronous completion with a cert in the store.
519 std::unique_ptr<crypto::ECPrivateKey> key; 524 std::unique_ptr<crypto::ECPrivateKey> key;
520 EXPECT_EQ(0, service_->channel_id_count()); 525 EXPECT_EQ(0, service_->channel_id_count());
521 error = 526 error =
522 service_->GetOrCreateChannelID(host, &key, callback.callback(), &request); 527 service_->GetOrCreateChannelID(host, &key, callback.callback(), &request);
523 EXPECT_EQ(ERR_IO_PENDING, error); 528 EXPECT_THAT(error, IsError(ERR_IO_PENDING));
524 EXPECT_TRUE(request.is_active()); 529 EXPECT_TRUE(request.is_active());
525 530
526 std::unique_ptr<crypto::ECPrivateKey> expected_key( 531 std::unique_ptr<crypto::ECPrivateKey> expected_key(
527 crypto::ECPrivateKey::Create()); 532 crypto::ECPrivateKey::Create());
528 mock_store->CallGetChannelIDCallbackWithResult(OK, expected_key.get()); 533 mock_store->CallGetChannelIDCallbackWithResult(OK, expected_key.get());
529 534
530 error = callback.WaitForResult(); 535 error = callback.WaitForResult();
531 EXPECT_EQ(OK, error); 536 EXPECT_THAT(error, IsOk());
532 EXPECT_EQ(1, service_->channel_id_count()); 537 EXPECT_EQ(1, service_->channel_id_count());
533 EXPECT_EQ(1u, service_->requests()); 538 EXPECT_EQ(1u, service_->requests());
534 EXPECT_EQ(1u, service_->key_store_hits()); 539 EXPECT_EQ(1u, service_->key_store_hits());
535 // Because the cert was found in the store, no new workers should have been 540 // Because the cert was found in the store, no new workers should have been
536 // created. 541 // created.
537 EXPECT_EQ(0u, service_->workers_created()); 542 EXPECT_EQ(0u, service_->workers_created());
538 EXPECT_TRUE(key); 543 EXPECT_TRUE(key);
539 EXPECT_TRUE(KeysEqual(expected_key.get(), key.get())); 544 EXPECT_TRUE(KeysEqual(expected_key.get(), key.get()));
540 EXPECT_FALSE(request.is_active()); 545 EXPECT_FALSE(request.is_active());
541 } 546 }
542 547
543 TEST_F(ChannelIDServiceTest, AsyncStoreGetOneCertInStore) { 548 TEST_F(ChannelIDServiceTest, AsyncStoreGetOneCertInStore) {
544 MockChannelIDStoreWithAsyncGet* mock_store = 549 MockChannelIDStoreWithAsyncGet* mock_store =
545 new MockChannelIDStoreWithAsyncGet(); 550 new MockChannelIDStoreWithAsyncGet();
546 service_ = std::unique_ptr<ChannelIDService>( 551 service_ = std::unique_ptr<ChannelIDService>(
547 new ChannelIDService(mock_store, base::ThreadTaskRunnerHandle::Get())); 552 new ChannelIDService(mock_store, base::ThreadTaskRunnerHandle::Get()));
548 553
549 std::string host("encrypted.google.com"); 554 std::string host("encrypted.google.com");
550 555
551 int error; 556 int error;
552 TestCompletionCallback callback; 557 TestCompletionCallback callback;
553 ChannelIDService::Request request; 558 ChannelIDService::Request request;
554 559
555 // Asynchronous completion with a cert in the store. 560 // Asynchronous completion with a cert in the store.
556 std::unique_ptr<crypto::ECPrivateKey> key; 561 std::unique_ptr<crypto::ECPrivateKey> key;
557 std::string private_key, spki; 562 std::string private_key, spki;
558 EXPECT_EQ(0, service_->channel_id_count()); 563 EXPECT_EQ(0, service_->channel_id_count());
559 error = service_->GetChannelID(host, &key, callback.callback(), &request); 564 error = service_->GetChannelID(host, &key, callback.callback(), &request);
560 EXPECT_EQ(ERR_IO_PENDING, error); 565 EXPECT_THAT(error, IsError(ERR_IO_PENDING));
561 EXPECT_TRUE(request.is_active()); 566 EXPECT_TRUE(request.is_active());
562 567
563 std::unique_ptr<crypto::ECPrivateKey> expected_key( 568 std::unique_ptr<crypto::ECPrivateKey> expected_key(
564 crypto::ECPrivateKey::Create()); 569 crypto::ECPrivateKey::Create());
565 mock_store->CallGetChannelIDCallbackWithResult(OK, expected_key.get()); 570 mock_store->CallGetChannelIDCallbackWithResult(OK, expected_key.get());
566 571
567 error = callback.WaitForResult(); 572 error = callback.WaitForResult();
568 EXPECT_EQ(OK, error); 573 EXPECT_THAT(error, IsOk());
569 EXPECT_EQ(1, service_->channel_id_count()); 574 EXPECT_EQ(1, service_->channel_id_count());
570 EXPECT_EQ(1u, service_->requests()); 575 EXPECT_EQ(1u, service_->requests());
571 EXPECT_EQ(1u, service_->key_store_hits()); 576 EXPECT_EQ(1u, service_->key_store_hits());
572 // Because the cert was found in the store, no new workers should have been 577 // Because the cert was found in the store, no new workers should have been
573 // created. 578 // created.
574 EXPECT_EQ(0u, service_->workers_created()); 579 EXPECT_EQ(0u, service_->workers_created());
575 EXPECT_TRUE(KeysEqual(expected_key.get(), key.get())); 580 EXPECT_TRUE(KeysEqual(expected_key.get(), key.get()));
576 EXPECT_FALSE(request.is_active()); 581 EXPECT_FALSE(request.is_active());
577 } 582 }
578 583
579 TEST_F(ChannelIDServiceTest, AsyncStoreGetThenCreateNoCertsInStore) { 584 TEST_F(ChannelIDServiceTest, AsyncStoreGetThenCreateNoCertsInStore) {
580 MockChannelIDStoreWithAsyncGet* mock_store = 585 MockChannelIDStoreWithAsyncGet* mock_store =
581 new MockChannelIDStoreWithAsyncGet(); 586 new MockChannelIDStoreWithAsyncGet();
582 service_ = std::unique_ptr<ChannelIDService>( 587 service_ = std::unique_ptr<ChannelIDService>(
583 new ChannelIDService(mock_store, base::ThreadTaskRunnerHandle::Get())); 588 new ChannelIDService(mock_store, base::ThreadTaskRunnerHandle::Get()));
584 589
585 std::string host("encrypted.google.com"); 590 std::string host("encrypted.google.com");
586 591
587 int error; 592 int error;
588 593
589 // Asynchronous get with no certs in the store. 594 // Asynchronous get with no certs in the store.
590 TestCompletionCallback callback1; 595 TestCompletionCallback callback1;
591 ChannelIDService::Request request1; 596 ChannelIDService::Request request1;
592 std::unique_ptr<crypto::ECPrivateKey> key1; 597 std::unique_ptr<crypto::ECPrivateKey> key1;
593 EXPECT_EQ(0, service_->channel_id_count()); 598 EXPECT_EQ(0, service_->channel_id_count());
594 error = service_->GetChannelID(host, &key1, callback1.callback(), &request1); 599 error = service_->GetChannelID(host, &key1, callback1.callback(), &request1);
595 EXPECT_EQ(ERR_IO_PENDING, error); 600 EXPECT_THAT(error, IsError(ERR_IO_PENDING));
596 EXPECT_TRUE(request1.is_active()); 601 EXPECT_TRUE(request1.is_active());
597 602
598 // Asynchronous get/create with no certs in the store. 603 // Asynchronous get/create with no certs in the store.
599 TestCompletionCallback callback2; 604 TestCompletionCallback callback2;
600 ChannelIDService::Request request2; 605 ChannelIDService::Request request2;
601 std::unique_ptr<crypto::ECPrivateKey> key2; 606 std::unique_ptr<crypto::ECPrivateKey> key2;
602 EXPECT_EQ(0, service_->channel_id_count()); 607 EXPECT_EQ(0, service_->channel_id_count());
603 error = service_->GetOrCreateChannelID(host, &key2, callback2.callback(), 608 error = service_->GetOrCreateChannelID(host, &key2, callback2.callback(),
604 &request2); 609 &request2);
605 EXPECT_EQ(ERR_IO_PENDING, error); 610 EXPECT_THAT(error, IsError(ERR_IO_PENDING));
606 EXPECT_TRUE(request2.is_active()); 611 EXPECT_TRUE(request2.is_active());
607 612
608 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr); 613 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr);
609 614
610 // Even though the first request didn't ask to create a cert, it gets joined 615 // Even though the first request didn't ask to create a cert, it gets joined
611 // by the second, which does, so both succeed. 616 // by the second, which does, so both succeed.
612 error = callback1.WaitForResult(); 617 error = callback1.WaitForResult();
613 EXPECT_EQ(OK, error); 618 EXPECT_THAT(error, IsOk());
614 error = callback2.WaitForResult(); 619 error = callback2.WaitForResult();
615 EXPECT_EQ(OK, error); 620 EXPECT_THAT(error, IsOk());
616 621
617 // One cert is created, one request is joined. 622 // One cert is created, one request is joined.
618 EXPECT_EQ(2U, service_->requests()); 623 EXPECT_EQ(2U, service_->requests());
619 EXPECT_EQ(1, service_->channel_id_count()); 624 EXPECT_EQ(1, service_->channel_id_count());
620 EXPECT_EQ(1u, service_->workers_created()); 625 EXPECT_EQ(1u, service_->workers_created());
621 EXPECT_EQ(1u, service_->inflight_joins()); 626 EXPECT_EQ(1u, service_->inflight_joins());
622 EXPECT_TRUE(key1); 627 EXPECT_TRUE(key1);
623 EXPECT_TRUE(KeysEqual(key1.get(), key2.get())); 628 EXPECT_TRUE(KeysEqual(key1.get(), key2.get()));
624 EXPECT_FALSE(request1.is_active()); 629 EXPECT_FALSE(request1.is_active());
625 EXPECT_FALSE(request2.is_active()); 630 EXPECT_FALSE(request2.is_active());
626 } 631 }
627 632
628 } // namespace 633 } // namespace
629 634
630 } // namespace net 635 } // namespace net
OLDNEW
« no previous file with comments | « net/spdy/spdy_test_util_common.cc ('k') | net/ssl/default_channel_id_store_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698