OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/bind.h" |
| 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/message_loop.h" |
| 8 #include "base/run_loop.h" |
| 9 #include "base/threading/sequenced_worker_pool.h" |
| 10 #include "content/browser/media/dtls_identity_store.h" |
| 11 #include "content/public/test/test_browser_thread_bundle.h" |
| 12 #include "googleurl/src/gurl.h" |
| 13 #include "net/base/net_errors.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace content { |
| 17 namespace { |
| 18 |
| 19 class DTLSIdentityStoreForTest : public DTLSIdentityStore { |
| 20 public: |
| 21 DTLSIdentityStoreForTest(const scoped_refptr<base::TaskRunner>& task_runner) |
| 22 : DTLSIdentityStore(task_runner) {} |
| 23 |
| 24 virtual ~DTLSIdentityStoreForTest() {} |
| 25 }; |
| 26 |
| 27 class DTLSIdentityStoreTest : public testing::Test { |
| 28 public: |
| 29 DTLSIdentityStoreTest() |
| 30 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
| 31 sequenced_worker_pool_( |
| 32 new base::SequencedWorkerPool(3, "ServerBoundCertServiceTest")) { |
| 33 dtls_identity_store_.reset( |
| 34 new DTLSIdentityStoreForTest(sequenced_worker_pool_)); |
| 35 } |
| 36 |
| 37 virtual ~DTLSIdentityStoreTest() { sequenced_worker_pool_->Shutdown(); } |
| 38 |
| 39 protected: |
| 40 TestBrowserThreadBundle browser_thread_bundle_; |
| 41 scoped_refptr<base::SequencedWorkerPool> sequenced_worker_pool_; |
| 42 scoped_ptr<DTLSIdentityStoreForTest> dtls_identity_store_; |
| 43 }; |
| 44 |
| 45 void OnRequestCompleted(bool* completed, |
| 46 int error, |
| 47 const std::string& certificate, |
| 48 const std::string& private_key) { |
| 49 ASSERT_EQ(net::OK, error); |
| 50 ASSERT_NE("", certificate); |
| 51 ASSERT_NE("", private_key); |
| 52 *completed = true; |
| 53 } |
| 54 |
| 55 TEST_F(DTLSIdentityStoreTest, RequestIdentity) { |
| 56 DTLSIdentityStore::RequestHandle request_handle; |
| 57 scoped_ptr<bool> completed; |
| 58 completed.reset(new bool(false)); |
| 59 dtls_identity_store_->RequestIdentity( |
| 60 GURL("http://google.com"), |
| 61 "a", |
| 62 "b", |
| 63 base::Bind(&OnRequestCompleted, base::Unretained(completed.get())), |
| 64 &request_handle); |
| 65 sequenced_worker_pool_->FlushForTesting(); |
| 66 base::RunLoop().RunUntilIdle(); |
| 67 EXPECT_EQ(true, *completed); |
| 68 } |
| 69 |
| 70 TEST_F(DTLSIdentityStoreTest, CancelRequest) { |
| 71 DTLSIdentityStore::RequestHandle request_handle; |
| 72 scoped_ptr<bool> completed; |
| 73 completed.reset(new bool(false)); |
| 74 dtls_identity_store_->RequestIdentity( |
| 75 GURL("http://google.com"), |
| 76 "a", |
| 77 "b", |
| 78 base::Bind(&OnRequestCompleted, base::Unretained(completed.get())), |
| 79 &request_handle); |
| 80 request_handle.Cancel(); |
| 81 sequenced_worker_pool_->FlushForTesting(); |
| 82 base::RunLoop().RunUntilIdle(); |
| 83 EXPECT_EQ(false, *completed); |
| 84 } |
| 85 |
| 86 TEST_F(DTLSIdentityStoreTest, MultipleRequests) { |
| 87 scoped_ptr<bool> completed_1; |
| 88 completed_1.reset(new bool(false)); |
| 89 scoped_ptr<bool> completed_2; |
| 90 completed_2.reset(new bool(false)); |
| 91 |
| 92 DTLSIdentityStore::RequestHandle request_handle_1; |
| 93 DTLSIdentityStore::RequestHandle request_handle_2; |
| 94 dtls_identity_store_->RequestIdentity( |
| 95 GURL("http://foo.com"), |
| 96 "a", |
| 97 "b", |
| 98 base::Bind(&OnRequestCompleted, base::Unretained(completed_1.get())), |
| 99 &request_handle_1); |
| 100 |
| 101 dtls_identity_store_->RequestIdentity( |
| 102 GURL("http://bar.com"), |
| 103 "a", |
| 104 "b", |
| 105 base::Bind(&OnRequestCompleted, base::Unretained(completed_2.get())), |
| 106 &request_handle_2); |
| 107 |
| 108 sequenced_worker_pool_->FlushForTesting(); |
| 109 base::RunLoop().RunUntilIdle(); |
| 110 EXPECT_EQ(true, *completed_1); |
| 111 EXPECT_EQ(true, *completed_2); |
| 112 } |
| 113 |
| 114 } // namespace |
| 115 } // namespace content |
OLD | NEW |