1 // Copyright 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.
6 #include "base/files/scoped_temp_dir.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/test/sequenced_worker_pool_owner.h"
9 #include "content/browser/media/webrtc_identity_store.h"
10 #include "content/public/test/test_browser_thread_bundle.h"
11 #include "content/public/test/test_utils.h"
12 #include "net/base/net_errors.h"
13 #include "testing/gtest/include/gtest/gtest.h"
18 static const char* kFakeOrigin
= "http://foo.com";
19 static const char* kFakeIdentityName1
= "name1";
20 static const char* kFakeIdentityName2
= "name2";
21 static const char* kFakeCommonName1
= "cname1";
22 static const char* kFakeCommonName2
= "cname2";
24 static void OnRequestCompleted(bool* completed
,
25 std::string
* out_cert
,
28 const std::string
& certificate
,
29 const std::string
& private_key
) {
30 ASSERT_EQ(net::OK
, error
);
31 ASSERT_NE("", certificate
);
32 ASSERT_NE("", private_key
);
34 *out_cert
= certificate
;
35 *out_key
= private_key
;
38 class WebRTCIdentityStoreTest
: public testing::Test
{
40 WebRTCIdentityStoreTest()
41 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP
|
42 TestBrowserThreadBundle::REAL_DB_THREAD
),
44 new base::SequencedWorkerPoolOwner(3, "WebRTCIdentityStoreTest")),
45 webrtc_identity_store_(
46 new WebRTCIdentityStore(base::FilePath(), NULL
)) {
47 webrtc_identity_store_
->SetTaskRunnerForTesting(pool_owner_
->pool());
50 virtual ~WebRTCIdentityStoreTest() {
51 pool_owner_
->pool()->Shutdown();
54 void SetValidityPeriod(base::TimeDelta validity_period
) {
55 webrtc_identity_store_
->SetValidityPeriodForTesting(validity_period
);
59 RunAllPendingInMessageLoop(BrowserThread::DB
);
60 RunAllPendingInMessageLoop(BrowserThread::IO
);
61 pool_owner_
->pool()->FlushForTesting();
62 base::RunLoop().RunUntilIdle();
65 base::Closure
RequestIdentityAndRunUtilIdle(const std::string
& origin
,
66 const std::string
& identity_name
,
67 const std::string
& common_name
,
69 std::string
* certificate
,
70 std::string
* private_key
) {
71 base::Closure cancel_callback
= webrtc_identity_store_
->RequestIdentity(
75 base::Bind(&OnRequestCompleted
, completed
, certificate
, private_key
));
76 EXPECT_FALSE(cancel_callback
.is_null());
78 return cancel_callback
;
81 void Restart(const base::FilePath
& path
) {
82 webrtc_identity_store_
= new WebRTCIdentityStore(path
, NULL
);
83 webrtc_identity_store_
->SetTaskRunnerForTesting(pool_owner_
->pool());
87 TestBrowserThreadBundle browser_thread_bundle_
;
88 scoped_ptr
<base::SequencedWorkerPoolOwner
> pool_owner_
;
89 scoped_refptr
<WebRTCIdentityStore
> webrtc_identity_store_
;
92 TEST_F(WebRTCIdentityStoreTest
, RequestIdentity
) {
93 bool completed
= false;
95 base::Closure cancel_callback
=
96 RequestIdentityAndRunUtilIdle(kFakeOrigin
,
102 EXPECT_TRUE(completed
);
105 TEST_F(WebRTCIdentityStoreTest
, CancelRequest
) {
106 bool completed
= false;
108 base::Closure cancel_callback
= webrtc_identity_store_
->RequestIdentity(
112 base::Bind(&OnRequestCompleted
, &completed
, &dummy
, &dummy
));
113 ASSERT_FALSE(cancel_callback
.is_null());
114 cancel_callback
.Run();
117 EXPECT_FALSE(completed
);
120 TEST_F(WebRTCIdentityStoreTest
, ConcurrentUniqueRequests
) {
121 bool completed_1
= false;
122 bool completed_2
= false;
124 base::Closure cancel_callback_1
= webrtc_identity_store_
->RequestIdentity(
128 base::Bind(&OnRequestCompleted
, &completed_1
, &dummy
, &dummy
));
129 ASSERT_FALSE(cancel_callback_1
.is_null());
131 base::Closure cancel_callback_2
= webrtc_identity_store_
->RequestIdentity(
135 base::Bind(&OnRequestCompleted
, &completed_2
, &dummy
, &dummy
));
136 ASSERT_FALSE(cancel_callback_2
.is_null());
139 EXPECT_TRUE(completed_1
);
140 EXPECT_TRUE(completed_2
);
143 TEST_F(WebRTCIdentityStoreTest
, DifferentCommonNameReturnNewIdentity
) {
144 bool completed_1
= false;
145 bool completed_2
= false;
146 std::string cert_1
, cert_2
, key_1
, key_2
;
148 base::Closure cancel_callback_1
=
149 RequestIdentityAndRunUtilIdle(kFakeOrigin
,
156 base::Closure cancel_callback_2
=
157 RequestIdentityAndRunUtilIdle(kFakeOrigin
,
164 EXPECT_TRUE(completed_1
);
165 EXPECT_TRUE(completed_2
);
166 EXPECT_NE(cert_1
, cert_2
);
167 EXPECT_NE(key_1
, key_2
);
170 TEST_F(WebRTCIdentityStoreTest
, SerialIdenticalRequests
) {
171 bool completed_1
= false;
172 bool completed_2
= false;
173 std::string cert_1
, cert_2
, key_1
, key_2
;
175 base::Closure cancel_callback_1
=
176 RequestIdentityAndRunUtilIdle(kFakeOrigin
,
183 base::Closure cancel_callback_2
=
184 RequestIdentityAndRunUtilIdle(kFakeOrigin
,
191 EXPECT_TRUE(completed_1
);
192 EXPECT_TRUE(completed_2
);
193 EXPECT_EQ(cert_1
, cert_2
);
194 EXPECT_EQ(key_1
, key_2
);
197 TEST_F(WebRTCIdentityStoreTest
, ConcurrentIdenticalRequestsJoined
) {
198 bool completed_1
= false;
199 bool completed_2
= false;
200 std::string cert_1
, cert_2
, key_1
, key_2
;
202 base::Closure cancel_callback_1
= webrtc_identity_store_
->RequestIdentity(
206 base::Bind(&OnRequestCompleted
, &completed_1
, &cert_1
, &key_1
));
207 ASSERT_FALSE(cancel_callback_1
.is_null());
209 base::Closure cancel_callback_2
= webrtc_identity_store_
->RequestIdentity(
213 base::Bind(&OnRequestCompleted
, &completed_2
, &cert_2
, &key_2
));
214 ASSERT_FALSE(cancel_callback_2
.is_null());
217 EXPECT_TRUE(completed_1
);
218 EXPECT_TRUE(completed_2
);
219 EXPECT_EQ(cert_1
, cert_2
);
220 EXPECT_EQ(key_1
, key_2
);
223 TEST_F(WebRTCIdentityStoreTest
, CancelOneOfIdenticalRequests
) {
224 bool completed_1
= false;
225 bool completed_2
= false;
226 std::string cert_1
, cert_2
, key_1
, key_2
;
228 base::Closure cancel_callback_1
= webrtc_identity_store_
->RequestIdentity(
232 base::Bind(&OnRequestCompleted
, &completed_1
, &cert_1
, &key_1
));
233 ASSERT_FALSE(cancel_callback_1
.is_null());
235 base::Closure cancel_callback_2
= webrtc_identity_store_
->RequestIdentity(
239 base::Bind(&OnRequestCompleted
, &completed_2
, &cert_2
, &key_2
));
240 ASSERT_FALSE(cancel_callback_2
.is_null());
242 cancel_callback_1
.Run();
245 EXPECT_FALSE(completed_1
);
246 EXPECT_TRUE(completed_2
);
249 TEST_F(WebRTCIdentityStoreTest
, DeleteDataAndGenerateNewIdentity
) {
250 bool completed_1
= false;
251 bool completed_2
= false;
252 std::string cert_1
, cert_2
, key_1
, key_2
;
254 // Generate the first identity.
255 base::Closure cancel_callback_1
=
256 RequestIdentityAndRunUtilIdle(kFakeOrigin
,
263 // Clear the data and the second request should return a new identity.
264 webrtc_identity_store_
->DeleteBetween(
265 base::Time(), base::Time::Now(), base::Bind(&base::DoNothing
));
268 base::Closure cancel_callback_2
=
269 RequestIdentityAndRunUtilIdle(kFakeOrigin
,
276 EXPECT_TRUE(completed_1
);
277 EXPECT_TRUE(completed_2
);
278 EXPECT_NE(cert_1
, cert_2
);
279 EXPECT_NE(key_1
, key_2
);
282 TEST_F(WebRTCIdentityStoreTest
, ExpiredIdentityDeleted
) {
283 // The identities will expire immediately after creation.
284 SetValidityPeriod(base::TimeDelta::FromMilliseconds(0));
286 bool completed_1
= false;
287 bool completed_2
= false;
288 std::string cert_1
, cert_2
, key_1
, key_2
;
290 base::Closure cancel_callback_1
=
291 RequestIdentityAndRunUtilIdle(kFakeOrigin
,
297 EXPECT_TRUE(completed_1
);
299 // Check that the old identity is not returned.
300 base::Closure cancel_callback_2
=
301 RequestIdentityAndRunUtilIdle(kFakeOrigin
,
307 EXPECT_TRUE(completed_2
);
308 EXPECT_NE(cert_1
, cert_2
);
309 EXPECT_NE(key_1
, key_2
);
312 TEST_F(WebRTCIdentityStoreTest
, IdentityPersistentAcrossRestart
) {
313 base::ScopedTempDir temp_dir
;
314 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
315 Restart(temp_dir
.path());
317 bool completed_1
= false;
318 bool completed_2
= false;
319 std::string cert_1
, cert_2
, key_1
, key_2
;
321 // Creates an identity.
322 base::Closure cancel_callback_1
=
323 RequestIdentityAndRunUtilIdle(kFakeOrigin
,
329 EXPECT_TRUE(completed_1
);
331 Restart(temp_dir
.path());
333 // Check that the same identity is returned after the restart.
334 base::Closure cancel_callback_2
=
335 RequestIdentityAndRunUtilIdle(kFakeOrigin
,
341 EXPECT_TRUE(completed_2
);
342 EXPECT_EQ(cert_1
, cert_2
);
343 EXPECT_EQ(key_1
, key_2
);
346 } // namespace content