Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / content / browser / media / webrtc_identity_store_unittest.cc
blob9078f1a19ac3d4a408ae97681e8fd39e5254b487
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.
5 #include "base/bind.h"
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"
16 #include "url/gurl.h"
18 namespace content {
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,
28 std::string* out_key,
29 int error,
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);
35 *completed = true;
36 *out_cert = certificate;
37 *out_key = private_key;
40 class WebRTCIdentityStoreTest : public testing::Test {
41 public:
42 WebRTCIdentityStoreTest()
43 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP |
44 TestBrowserThreadBundle::REAL_DB_THREAD),
45 pool_owner_(
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);
58 void RunUntilIdle() {
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,
68 bool* completed,
69 std::string* certificate,
70 std::string* private_key) {
71 base::Closure cancel_callback = webrtc_identity_store_->RequestIdentity(
72 GURL(origin),
73 identity_name,
74 common_name,
75 base::Bind(&OnRequestCompleted, completed, certificate, private_key));
76 EXPECT_FALSE(cancel_callback.is_null());
77 RunUntilIdle();
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());
86 protected:
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;
94 std::string dummy;
95 base::Closure cancel_callback =
96 RequestIdentityAndRunUtilIdle(kFakeOrigin,
97 kFakeIdentityName1,
98 kFakeCommonName1,
99 &completed,
100 &dummy,
101 &dummy);
102 EXPECT_TRUE(completed);
105 TEST_F(WebRTCIdentityStoreTest, CancelRequest) {
106 bool completed = false;
107 std::string dummy;
108 base::Closure cancel_callback = webrtc_identity_store_->RequestIdentity(
109 GURL(kFakeOrigin),
110 kFakeIdentityName1,
111 kFakeCommonName1,
112 base::Bind(&OnRequestCompleted, &completed, &dummy, &dummy));
113 ASSERT_FALSE(cancel_callback.is_null());
114 cancel_callback.Run();
116 RunUntilIdle();
117 EXPECT_FALSE(completed);
120 TEST_F(WebRTCIdentityStoreTest, ConcurrentUniqueRequests) {
121 bool completed_1 = false;
122 bool completed_2 = false;
123 std::string dummy;
124 base::Closure cancel_callback_1 = webrtc_identity_store_->RequestIdentity(
125 GURL(kFakeOrigin),
126 kFakeIdentityName1,
127 kFakeCommonName1,
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(
132 GURL(kFakeOrigin),
133 kFakeIdentityName2,
134 kFakeCommonName1,
135 base::Bind(&OnRequestCompleted, &completed_2, &dummy, &dummy));
136 ASSERT_FALSE(cancel_callback_2.is_null());
138 RunUntilIdle();
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,
150 kFakeIdentityName1,
151 kFakeCommonName1,
152 &completed_1,
153 &cert_1,
154 &key_1);
156 base::Closure cancel_callback_2 =
157 RequestIdentityAndRunUtilIdle(kFakeOrigin,
158 kFakeIdentityName1,
159 kFakeCommonName2,
160 &completed_2,
161 &cert_2,
162 &key_2);
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,
177 kFakeIdentityName1,
178 kFakeCommonName1,
179 &completed_1,
180 &cert_1,
181 &key_1);
183 base::Closure cancel_callback_2 =
184 RequestIdentityAndRunUtilIdle(kFakeOrigin,
185 kFakeIdentityName1,
186 kFakeCommonName1,
187 &completed_2,
188 &cert_2,
189 &key_2);
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(
203 GURL(kFakeOrigin),
204 kFakeIdentityName1,
205 kFakeCommonName1,
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(
210 GURL(kFakeOrigin),
211 kFakeIdentityName1,
212 kFakeCommonName1,
213 base::Bind(&OnRequestCompleted, &completed_2, &cert_2, &key_2));
214 ASSERT_FALSE(cancel_callback_2.is_null());
216 RunUntilIdle();
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(
229 GURL(kFakeOrigin),
230 kFakeIdentityName1,
231 kFakeCommonName1,
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(
236 GURL(kFakeOrigin),
237 kFakeIdentityName1,
238 kFakeCommonName1,
239 base::Bind(&OnRequestCompleted, &completed_2, &cert_2, &key_2));
240 ASSERT_FALSE(cancel_callback_2.is_null());
242 cancel_callback_1.Run();
244 RunUntilIdle();
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,
257 kFakeIdentityName1,
258 kFakeCommonName1,
259 &completed_1,
260 &cert_1,
261 &key_1);
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));
266 RunUntilIdle();
268 base::Closure cancel_callback_2 =
269 RequestIdentityAndRunUtilIdle(kFakeOrigin,
270 kFakeIdentityName1,
271 kFakeCommonName1,
272 &completed_2,
273 &cert_2,
274 &key_2);
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,
292 kFakeIdentityName1,
293 kFakeCommonName1,
294 &completed_1,
295 &cert_1,
296 &key_1);
297 EXPECT_TRUE(completed_1);
299 // Check that the old identity is not returned.
300 base::Closure cancel_callback_2 =
301 RequestIdentityAndRunUtilIdle(kFakeOrigin,
302 kFakeIdentityName1,
303 kFakeCommonName1,
304 &completed_2,
305 &cert_2,
306 &key_2);
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,
324 kFakeIdentityName1,
325 kFakeCommonName1,
326 &completed_1,
327 &cert_1,
328 &key_1);
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,
336 kFakeIdentityName1,
337 kFakeCommonName1,
338 &completed_2,
339 &cert_2,
340 &key_2);
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,
356 kFakeIdentityName1,
357 kFakeCommonName1,
358 &completed_1,
359 &cert_1,
360 &key_1);
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;
369 RunUntilIdle();
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