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 "sql/connection.h"
14 #include "sql/test/test_helpers.h"
15 #include "testing/gtest/include/gtest/gtest.h"
20 static const char kFakeOrigin
[] = "http://foo.com";
21 static const char kFakeIdentityName1
[] = "name1";
22 static const char kFakeIdentityName2
[] = "name2";
23 static const char kFakeCommonName1
[] = "cname1";
24 static const char kFakeCommonName2
[] = "cname2";
26 static void OnRequestCompleted(bool* completed
,
27 std::string
* out_cert
,
30 const std::string
& certificate
,
31 const std::string
& private_key
) {
32 ASSERT_EQ(net::OK
, error
);
33 ASSERT_NE("", certificate
);
34 ASSERT_NE("", private_key
);
36 *out_cert
= certificate
;
37 *out_key
= private_key
;
40 class WebRTCIdentityStoreTest
: public testing::Test
{
42 WebRTCIdentityStoreTest()
43 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP
|
44 TestBrowserThreadBundle::REAL_DB_THREAD
),
46 new base::SequencedWorkerPoolOwner(3, "WebRTCIdentityStoreTest")),
47 webrtc_identity_store_(
48 new WebRTCIdentityStore(base::FilePath(), NULL
)) {
49 webrtc_identity_store_
->SetTaskRunnerForTesting(pool_owner_
->pool());
52 ~WebRTCIdentityStoreTest() override
{ 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 TEST_F(WebRTCIdentityStoreTest
, HandleDBErrors
) {
347 base::ScopedTempDir temp_dir
;
348 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
349 Restart(temp_dir
.path());
351 bool completed_1
= false;
352 std::string cert_1
, key_1
;
354 // Creates an identity.
355 RequestIdentityAndRunUtilIdle(kFakeOrigin
,
362 // Make the table corrupted.
363 base::FilePath db_path
=
364 temp_dir
.path().Append(FILE_PATH_LITERAL("WebRTCIdentityStore"));
365 EXPECT_TRUE(sql::test::CorruptSizeInHeader(db_path
));
367 // Reset to commit the DB changes, which should fail and not crash.
368 webrtc_identity_store_
= NULL
;
371 // Verifies the corrupted table was razed.
372 scoped_ptr
<sql::Connection
> db(new sql::Connection());
373 EXPECT_TRUE(db
->Open(db_path
));
374 EXPECT_EQ(0U, sql::test::CountSQLTables(db
.get()));
377 } // namespace content