Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(40)

Side by Side Diff: chrome/browser/ui/ash/session_state_delegate_chromeos_unittest.cc

Issue 374853002: Providing more information on why certain users can't be added to multi-profile (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added tests. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/ash/session_state_delegate_chromeos.h" 5 #include "chrome/browser/ui/ash/session_state_delegate_chromeos.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "chrome/browser/chromeos/login/users/fake_user_manager.h" 10 #include "chrome/browser/chromeos/login/users/fake_user_manager.h"
11 #include "chrome/browser/chromeos/login/users/multi_profile_user_controller.h"
12 #include "chrome/browser/chromeos/policy/policy_cert_service.h"
13 #include "chrome/browser/chromeos/policy/policy_cert_service_factory.h"
14 #include "chrome/browser/chromeos/policy/policy_cert_verifier.h"
15 #include "chrome/browser/chromeos/profiles/profile_helper.h"
16 #include "chrome/common/pref_names.h"
17 #include "chrome/test/base/testing_browser_process.h"
11 #include "chrome/test/base/testing_profile_manager.h" 18 #include "chrome/test/base/testing_profile_manager.h"
19 #include "net/cert/x509_certificate.h"
12 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
13 21
22 namespace {
23
24 const char* kUser = "user@test.com";
25
26 // Weak ptr to PolicyCertVerifier - object is freed in test destructor once
27 // we've ensured the profile has been shut down.
28 policy::PolicyCertVerifier* g_policy_cert_verifier_for_factory = NULL;
29
30 KeyedService* TestPolicyCertServiceFactory(content::BrowserContext* context) {
James Cook 2014/08/12 20:08:30 nit: I would name this CreatePolicyCertServiceForT
Roman Sorokin (ftl) 2014/08/13 09:11:52 Done.
31 return policy::PolicyCertService::CreateForTesting(
32 kUser,
33 g_policy_cert_verifier_for_factory,
34 chromeos::UserManager::Get()).release();
35 }
36
37 } // namespace
14 38
15 class SessionStateDelegateChromeOSTest : public testing::Test { 39 class SessionStateDelegateChromeOSTest : public testing::Test {
16 protected: 40 protected:
17 SessionStateDelegateChromeOSTest() : user_manager_(NULL) { 41 SessionStateDelegateChromeOSTest() : user_manager_(NULL) {
18 } 42 }
19 43
20 virtual ~SessionStateDelegateChromeOSTest() { 44 virtual ~SessionStateDelegateChromeOSTest() {
21 } 45 }
22 46
23 virtual void SetUp() OVERRIDE { 47 virtual void SetUp() OVERRIDE {
24 // Initialize the UserManager singleton to a fresh FakeUserManager instance. 48 // Initialize the UserManager singleton to a fresh FakeUserManager instance.
25 user_manager_ = new chromeos::FakeUserManager; 49 user_manager_ = new chromeos::FakeUserManager;
26 user_manager_enabler_.reset( 50 user_manager_enabler_.reset(
27 new chromeos::ScopedUserManagerEnabler(user_manager_)); 51 new chromeos::ScopedUserManagerEnabler(user_manager_));
28 52
29 // Create our SessionStateDelegate to experiment with. 53 // Create our SessionStateDelegate to experiment with.
30 session_state_delegate_.reset(new SessionStateDelegateChromeos()); 54 session_state_delegate_.reset(new SessionStateDelegateChromeos());
31 testing::Test::SetUp(); 55 testing::Test::SetUp();
32 } 56 }
33 57
34 virtual void TearDown() OVERRIDE { 58 virtual void TearDown() OVERRIDE {
35 testing::Test::TearDown(); 59 testing::Test::TearDown();
36 session_state_delegate_.reset(); 60 session_state_delegate_.reset();
37 user_manager_enabler_.reset(); 61 user_manager_enabler_.reset();
38 user_manager_ = NULL; 62 user_manager_ = NULL;
63 // Clear our cached pointer to the PolicyCertVerifier.
64 g_policy_cert_verifier_for_factory = NULL;
39 } 65 }
40 66
41 // Add and log in a user to the session. 67 // Add and log in a user to the session.
42 void UserAddedToSession(std::string user) { 68 void UserAddedToSession(std::string user) {
43 user_manager()->AddUser(user); 69 user_manager()->AddUser(user);
44 user_manager()->LoginUser(user); 70 user_manager()->LoginUser(user);
45 } 71 }
46 72
47 // Get the active user. 73 // Get the active user.
48 const std::string& GetActiveUser() { 74 const std::string& GetActiveUser() {
49 return chromeos::UserManager::Get()->GetActiveUser()->email(); 75 return chromeos::UserManager::Get()->GetActiveUser()->email();
50 } 76 }
51 77
52 chromeos::FakeUserManager* user_manager() { return user_manager_; } 78 chromeos::FakeUserManager* user_manager() { return user_manager_; }
53 SessionStateDelegateChromeos* session_state_delegate() { 79 SessionStateDelegateChromeos* session_state_delegate() {
54 return session_state_delegate_.get(); 80 return session_state_delegate_.get();
55 } 81 }
56 82
83 void InitForMultiProfile() {
84 profile_manager_.reset(
85 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
86 ASSERT_TRUE(profile_manager_->SetUp());
87
88 const std::string user_email(kUser);
89 const user_manager::User* user = user_manager()->AddUser(user_email);
90
91 // Note that user profiles are created after user login in reality.
92 user_profile_ = profile_manager_->CreateTestingProfile(user_email);
93 user_profile_->set_profile_name(user_email);
94 chromeos::ProfileHelper::Get()->SetUserToProfileMappingForTesting(
95 user, user_profile_);
96 }
97
98 TestingProfile* user_profile() { return user_profile_; }
99
57 private: 100 private:
58 scoped_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_; 101 scoped_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_;
59 scoped_ptr<SessionStateDelegateChromeos> session_state_delegate_; 102 scoped_ptr<SessionStateDelegateChromeos> session_state_delegate_;
60 103
61 // Not owned. 104 // Not owned.
62 chromeos::FakeUserManager* user_manager_; 105 chromeos::FakeUserManager* user_manager_;
63 106
107 scoped_ptr<TestingProfileManager> profile_manager_;
108 TestingProfile* user_profile_;
109
64 DISALLOW_COPY_AND_ASSIGN(SessionStateDelegateChromeOSTest); 110 DISALLOW_COPY_AND_ASSIGN(SessionStateDelegateChromeOSTest);
65 }; 111 };
66 112
67 // Make sure that cycling one user does not cause any harm. 113 // Make sure that cycling one user does not cause any harm.
68 TEST_F(SessionStateDelegateChromeOSTest, CyclingOneUser) { 114 TEST_F(SessionStateDelegateChromeOSTest, CyclingOneUser) {
69 UserAddedToSession("firstuser@test.com"); 115 UserAddedToSession("firstuser@test.com");
70 116
71 EXPECT_EQ("firstuser@test.com", GetActiveUser()); 117 EXPECT_EQ("firstuser@test.com", GetActiveUser());
72 session_state_delegate()->CycleActiveUser( 118 session_state_delegate()->CycleActiveUser(
73 ash::SessionStateDelegate::CYCLE_TO_NEXT_USER); 119 ash::SessionStateDelegate::CYCLE_TO_NEXT_USER);
(...skipping 23 matching lines...) Expand all
97 // Cycle backwards. 143 // Cycle backwards.
98 const ash::SessionStateDelegate::CycleUser backward = 144 const ash::SessionStateDelegate::CycleUser backward =
99 ash::SessionStateDelegate::CYCLE_TO_PREVIOUS_USER; 145 ash::SessionStateDelegate::CYCLE_TO_PREVIOUS_USER;
100 session_state_delegate()->CycleActiveUser(backward); 146 session_state_delegate()->CycleActiveUser(backward);
101 EXPECT_EQ("thirduser@test.com", GetActiveUser()); 147 EXPECT_EQ("thirduser@test.com", GetActiveUser());
102 session_state_delegate()->CycleActiveUser(backward); 148 session_state_delegate()->CycleActiveUser(backward);
103 EXPECT_EQ("seconduser@test.com", GetActiveUser()); 149 EXPECT_EQ("seconduser@test.com", GetActiveUser());
104 session_state_delegate()->CycleActiveUser(backward); 150 session_state_delegate()->CycleActiveUser(backward);
105 EXPECT_EQ("firstuser@test.com", GetActiveUser()); 151 EXPECT_EQ("firstuser@test.com", GetActiveUser());
106 } 152 }
153
154 // Make sure MultiProfile disabled by primary user policy.
155 TEST_F(SessionStateDelegateChromeOSTest, MultiProfileDisallowedByUserPolicy) {
156 InitForMultiProfile();
157 EXPECT_TRUE(
158 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy());
159 const std::string user_email(kUser);
160 user_manager()->LoginUser(user_email);
161 EXPECT_TRUE(
162 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy());
163
164 user_profile()->GetPrefs()->SetString(
165 prefs::kMultiProfileUserBehavior,
166 chromeos::MultiProfileUserController::kBehaviorNotAllowed);
167 EXPECT_FALSE(
168 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy());
169 }
170
171 // Make sure MultiProfile disabled by primary user policy certificates.
172 TEST_F(SessionStateDelegateChromeOSTest,
173 MultiProfileDisallowedByPolicyCertificates) {
174 InitForMultiProfile();
175 const std::string user_email(kUser);
176 user_manager()->LoginUser(user_email);
177 EXPECT_TRUE(
178 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy());
179 policy::PolicyCertServiceFactory::SetUsedPolicyCertificates(user_email);
180 EXPECT_FALSE(
181 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy());
182 }
183
184 // Make sure MultiProfile disabled by primary user certificates in memory.
185 TEST_F(SessionStateDelegateChromeOSTest,
186 MultiProfileDisallowedByPrimaryUserCertificatesInMemory) {
187 InitForMultiProfile();
188 const std::string user_email(kUser);
189 user_manager()->LoginUser(user_email);
190 EXPECT_TRUE(
191 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy());
192 scoped_ptr<policy::PolicyCertVerifier> cert_verifier;
193 cert_verifier.reset(new policy::PolicyCertVerifier(base::Closure()));
194 g_policy_cert_verifier_for_factory = cert_verifier.get();
195 ASSERT_TRUE(
196 policy::PolicyCertServiceFactory::GetInstance()->SetTestingFactoryAndUse(
197 user_profile(), TestPolicyCertServiceFactory));
198 policy::PolicyCertService* service =
199 policy::PolicyCertServiceFactory::GetForProfile(user_profile());
200 ASSERT_TRUE(service);
201
202 EXPECT_FALSE(service->has_policy_certificates());
203 net::CertificateList certificates;
204 certificates.push_back(new net::X509Certificate(
205 "subject", "issuer", base::Time(), base::Time()));
206 service->OnTrustAnchorsChanged(certificates);
207 EXPECT_TRUE(service->has_policy_certificates());
208 EXPECT_FALSE(
209 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy());
210 }
James Cook 2014/08/12 20:08:30 Nice test suite. Good coverage, and helps me as a
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698