OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "chrome/browser/net/nss_context.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/run_loop.h" | |
9 #include "chrome/browser/chromeos/login/login_manager_test.h" | |
10 #include "chrome/browser/chromeos/login/startup_utils.h" | |
11 #include "chrome/browser/chromeos/login/user.h" | |
12 #include "chrome/browser/chromeos/login/user_adding_screen.h" | |
13 #include "chrome/browser/chromeos/login/user_manager.h" | |
14 #include "content/public/browser/browser_thread.h" | |
15 #include "net/cert/nss_cert_database.h" | |
16 | |
17 namespace { | |
18 | |
19 const char kTestUser1[] = "test-user1@gmail.com"; | |
20 const char kTestUser2[] = "test-user2@gmail.com"; | |
21 | |
22 void NotCalledDbCallback(net::NSSCertDatabase* db) { ASSERT_TRUE(false); } | |
23 | |
24 class DBTester { | |
Ryan Sleevi
2013/12/18 21:28:32
Please add comments throughout here to document, e
mattm
2013/12/19 22:35:00
Done.
| |
25 public: | |
26 explicit DBTester(Profile* profile) : profile_(profile), db_(NULL) {} | |
27 | |
28 void DoGetDBTests() { | |
29 base::RunLoop run_loop; | |
30 content::BrowserThread::PostTask( | |
31 content::BrowserThread::IO, | |
32 FROM_HERE, | |
33 base::Bind(&DBTester::GetDBAndDoTestsOnIOThread, | |
34 base::Unretained(this), | |
35 profile_->GetResourceContext(), | |
36 run_loop.QuitClosure())); | |
37 run_loop.Run(); | |
38 } | |
39 | |
40 void DoGetDBAgainTests() { | |
41 base::RunLoop run_loop; | |
42 content::BrowserThread::PostTask( | |
43 content::BrowserThread::IO, | |
44 FROM_HERE, | |
45 base::Bind(&DBTester::DoGetDBAgainTestsOnIOThread, | |
46 base::Unretained(this), | |
47 profile_->GetResourceContext(), | |
48 run_loop.QuitClosure())); | |
49 run_loop.Run(); | |
50 } | |
51 | |
52 void DoNotEqualsTests(DBTester* other_tester) { | |
53 // The DB and its NSS slots should be different for each profile. | |
54 EXPECT_NE(db_, other_tester->db_); | |
55 EXPECT_NE(db_->GetPublicSlot().get(), | |
56 other_tester->db_->GetPublicSlot().get()); | |
57 } | |
58 | |
59 private: | |
60 void GetDBAndDoTestsOnIOThread(content::ResourceContext* context, | |
61 const base::Closure& done_callback) { | |
62 net::NSSCertDatabase* db = GetNSSCertDatabaseForResourceContext( | |
63 context, | |
64 base::Bind(&DBTester::DoTestsOnIOThread, | |
65 base::Unretained(this), | |
66 done_callback)); | |
67 if (db) { | |
68 DVLOG(1) << "got db synchronously"; | |
69 DoTestsOnIOThread(done_callback, db); | |
70 } else { | |
71 DVLOG(1) << "getting db asynchronously..."; | |
72 } | |
73 } | |
74 | |
75 void DoTestsOnIOThread(const base::Closure& done_callback, | |
76 net::NSSCertDatabase* db) { | |
77 db_ = db; | |
78 EXPECT_TRUE(db); | |
79 if (db) { | |
80 EXPECT_TRUE(db->GetPublicSlot().get()); | |
81 // Public and private slot are the same in tests. | |
82 EXPECT_EQ(db->GetPublicSlot().get(), db->GetPrivateSlot().get()); | |
83 } | |
84 | |
85 content::BrowserThread::PostTask( | |
86 content::BrowserThread::UI, FROM_HERE, done_callback); | |
87 } | |
88 | |
89 void DoGetDBAgainTestsOnIOThread(content::ResourceContext* context, | |
90 const base::Closure& done_callback) { | |
91 net::NSSCertDatabase* db = GetNSSCertDatabaseForResourceContext( | |
92 context, base::Bind(&NotCalledDbCallback)); | |
93 // Should always be synchronous now. | |
94 EXPECT_TRUE(db); | |
95 // Should return the same db as before. | |
96 EXPECT_EQ(db_, db); | |
97 | |
98 content::BrowserThread::PostTask( | |
99 content::BrowserThread::UI, FROM_HERE, done_callback); | |
100 } | |
101 | |
102 Profile* profile_; | |
103 net::NSSCertDatabase* db_; | |
104 }; | |
105 | |
106 } // namespace | |
107 | |
108 class NSSContextChromeOSBrowserTest : public chromeos::LoginManagerTest { | |
109 public: | |
110 NSSContextChromeOSBrowserTest() : LoginManagerTest(true) {} | |
111 virtual ~NSSContextChromeOSBrowserTest() {} | |
112 }; | |
113 | |
114 IN_PROC_BROWSER_TEST_F(NSSContextChromeOSBrowserTest, PRE_TwoUsers) { | |
115 RegisterUser(kTestUser1); | |
116 RegisterUser(kTestUser2); | |
117 chromeos::StartupUtils::MarkOobeCompleted(); | |
118 } | |
119 | |
120 IN_PROC_BROWSER_TEST_F(NSSContextChromeOSBrowserTest, TwoUsers) { | |
121 chromeos::UserManager* user_manager = chromeos::UserManager::Get(); | |
122 | |
123 LoginUser(kTestUser1); | |
124 Profile* profile1 = | |
125 user_manager->GetProfileByUser(user_manager->FindUser(kTestUser1)); | |
126 ASSERT_TRUE(profile1); | |
127 | |
128 DBTester tester1(profile1); | |
129 tester1.DoGetDBTests(); | |
130 | |
131 chromeos::UserAddingScreen::Get()->Start(); | |
132 base::RunLoop().RunUntilIdle(); | |
133 AddUser(kTestUser2); | |
134 | |
135 Profile* profile2 = | |
136 user_manager->GetProfileByUser(user_manager->FindUser(kTestUser2)); | |
137 ASSERT_TRUE(profile2); | |
138 | |
139 DBTester tester2(profile2); | |
140 tester2.DoGetDBTests(); | |
141 | |
142 tester1.DoGetDBAgainTests(); | |
143 tester2.DoGetDBAgainTests(); | |
144 | |
145 tester1.DoNotEqualsTests(&tester2); | |
146 } | |
OLD | NEW |