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

Side by Side Diff: chrome/browser/chromeos/arc/arc_util_unittest.cc

Issue 2890843002: Policy implementation for encryptfs to ext4 migration strategy (Closed)
Patch Set: Fixed review comments Created 3 years, 6 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 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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/chromeos/arc/arc_util.h" 5 #include "chrome/browser/chromeos/arc/arc_util.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
12 #include "base/sys_info.h" 12 #include "base/sys_info.h"
13 #include "base/test/scoped_command_line.h" 13 #include "base/test/scoped_command_line.h"
14 #include "base/values.h" 14 #include "base/values.h"
15 #include "chrome/browser/chromeos/arc/arc_session_manager.h" 15 #include "chrome/browser/chromeos/arc/arc_session_manager.h"
16 #include "chrome/browser/chromeos/login/supervised/supervised_user_creation_flow .h" 16 #include "chrome/browser/chromeos/login/supervised/supervised_user_creation_flow .h"
17 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" 17 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
18 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" 18 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
19 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h" 19 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h"
20 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
20 #include "chrome/browser/chromeos/profiles/profile_helper.h" 21 #include "chrome/browser/chromeos/profiles/profile_helper.h"
22 #include "chrome/browser/chromeos/settings/install_attributes.h"
21 #include "chrome/browser/profiles/profile.h" 23 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/common/pref_names.h" 24 #include "chrome/common/pref_names.h"
23 #include "chrome/test/base/testing_profile.h" 25 #include "chrome/test/base/testing_profile.h"
26 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
27 #include "components/prefs/pref_registry_simple.h"
24 #include "components/prefs/pref_service.h" 28 #include "components/prefs/pref_service.h"
29 #include "components/prefs/pref_service_factory.h"
25 #include "components/prefs/testing_pref_service.h" 30 #include "components/prefs/testing_pref_service.h"
31 #include "components/prefs/testing_pref_store.h"
26 #include "components/signin/core/account_id/account_id.h" 32 #include "components/signin/core/account_id/account_id.h"
27 #include "components/sync_preferences/testing_pref_service_syncable.h" 33 #include "components/sync_preferences/testing_pref_service_syncable.h"
28 #include "components/user_manager/known_user.h" 34 #include "components/user_manager/known_user.h"
29 #include "components/user_manager/user_manager.h" 35 #include "components/user_manager/user_manager.h"
30 #include "components/user_manager/user_names.h" 36 #include "components/user_manager/user_names.h"
31 #include "content/public/test/test_browser_thread_bundle.h" 37 #include "content/public/test/test_browser_thread_bundle.h"
32 #include "testing/gtest/include/gtest/gtest.h" 38 #include "testing/gtest/include/gtest/gtest.h"
33 39
34 namespace arc { 40 namespace arc {
35 namespace util { 41 namespace util {
36 42
37 namespace { 43 namespace {
38 44
39 constexpr char kTestProfileName[] = "user@gmail.com"; 45 constexpr char kTestProfileName[] = "user@gmail.com";
40 constexpr char kTestGaiaId[] = "1234567890"; 46 constexpr char kTestGaiaId[] = "1234567890";
41 47
48 // The constants matching the values from DeviceEcryptfsMigrationStrategy
49 // policy.
50 constexpr int kMigrationAllowedPolicyUnset = 0;
51 constexpr int kMigrationAllowedPolicyDisabled = 1;
52 constexpr int kMigrationAllowedPolicyEnabled = 2;
53
42 class ScopedLogIn { 54 class ScopedLogIn {
43 public: 55 public:
44 ScopedLogIn( 56 ScopedLogIn(
45 chromeos::FakeChromeUserManager* fake_user_manager, 57 chromeos::FakeChromeUserManager* fake_user_manager,
46 const AccountId& account_id, 58 const AccountId& account_id,
47 user_manager::UserType user_type = user_manager::USER_TYPE_REGULAR) 59 user_manager::UserType user_type = user_manager::USER_TYPE_REGULAR)
48 : fake_user_manager_(fake_user_manager), account_id_(account_id) { 60 : fake_user_manager_(fake_user_manager), account_id_(account_id) {
49 switch (user_type) { 61 switch (user_type) {
50 case user_manager::USER_TYPE_REGULAR: // fallthrough 62 case user_manager::USER_TYPE_REGULAR: // fallthrough
51 case user_manager::USER_TYPE_ACTIVE_DIRECTORY: 63 case user_manager::USER_TYPE_ACTIVE_DIRECTORY:
(...skipping 29 matching lines...) Expand all
81 } 93 }
82 94
83 void LogOut() { fake_user_manager_->RemoveUserFromList(account_id_); } 95 void LogOut() { fake_user_manager_->RemoveUserFromList(account_id_); }
84 96
85 chromeos::FakeChromeUserManager* fake_user_manager_; 97 chromeos::FakeChromeUserManager* fake_user_manager_;
86 const AccountId account_id_; 98 const AccountId account_id_;
87 99
88 DISALLOW_COPY_AND_ASSIGN(ScopedLogIn); 100 DISALLOW_COPY_AND_ASSIGN(ScopedLogIn);
89 }; 101 };
90 102
103 class FakeInstallAttributesManaged : public chromeos::InstallAttributes {
104 public:
105 FakeInstallAttributesManaged() : chromeos::InstallAttributes(nullptr) {
106 device_locked_ = true;
107 }
108
109 ~FakeInstallAttributesManaged() {
110 policy::BrowserPolicyConnectorChromeOS::RemoveInstallAttributesForTesting();
111 }
112
113 void SetIsManaged(bool is_managed) {
114 registration_mode_ = is_managed ? policy::DEVICE_MODE_ENTERPRISE
115 : policy::DEVICE_MODE_CONSUMER;
116 }
117 };
118
91 class FakeUserManagerWithLocalState : public chromeos::FakeChromeUserManager { 119 class FakeUserManagerWithLocalState : public chromeos::FakeChromeUserManager {
92 public: 120 public:
93 FakeUserManagerWithLocalState() 121 FakeUserManagerWithLocalState()
94 : test_local_state_(base::MakeUnique<TestingPrefServiceSimple>()) { 122 : test_local_state_(base::MakeUnique<TestingPrefServiceSimple>()) {
95 RegisterPrefs(test_local_state_->registry()); 123 RegisterPrefs(test_local_state_->registry());
96 } 124 }
97 125
98 PrefService* GetLocalState() const override { 126 PrefService* GetLocalState() const override {
127 if (local_state_)
128 return local_state_;
99 return test_local_state_.get(); 129 return test_local_state_.get();
100 } 130 }
101 131
132 void SetLocalState(PrefService* local_state) { local_state_ = local_state; }
133
102 private: 134 private:
103 std::unique_ptr<TestingPrefServiceSimple> test_local_state_; 135 std::unique_ptr<TestingPrefServiceSimple> test_local_state_;
136 PrefService* local_state_ = nullptr;
104 137
105 DISALLOW_COPY_AND_ASSIGN(FakeUserManagerWithLocalState); 138 DISALLOW_COPY_AND_ASSIGN(FakeUserManagerWithLocalState);
106 }; 139 };
107 140
108 } // namespace 141 } // namespace
109 142
110 class ChromeArcUtilTest : public testing::Test { 143 class ChromeArcUtilTest : public testing::Test {
111 public: 144 public:
112 ChromeArcUtilTest() = default; 145 ChromeArcUtilTest() {
146 auto attributes = base::MakeUnique<FakeInstallAttributesManaged>();
147 attributes_ = attributes.get();
148 policy::BrowserPolicyConnectorChromeOS::SetInstallAttributesForTesting(
149 attributes.release());
150 }
151
113 ~ChromeArcUtilTest() override = default; 152 ~ChromeArcUtilTest() override = default;
114 153
115 void SetUp() override { 154 void SetUp() override {
116 command_line_ = base::MakeUnique<base::test::ScopedCommandLine>(); 155 command_line_ = base::MakeUnique<base::test::ScopedCommandLine>();
117 156
118 user_manager_enabler_ = 157 user_manager_enabler_ =
119 base::MakeUnique<chromeos::ScopedUserManagerEnabler>( 158 base::MakeUnique<chromeos::ScopedUserManagerEnabler>(
120 new FakeUserManagerWithLocalState()); 159 new FakeUserManagerWithLocalState());
121 chromeos::WallpaperManager::Initialize(); 160 chromeos::WallpaperManager::Initialize();
122 profile_ = base::MakeUnique<TestingProfile>(); 161 profile_ = base::MakeUnique<TestingProfile>();
123 profile_->set_profile_name(kTestProfileName); 162 profile_->set_profile_name(kTestProfileName);
124 } 163 }
125 164
126 void TearDown() override { 165 void TearDown() override {
127 profile_.reset(); 166 profile_.reset();
128 chromeos::WallpaperManager::Shutdown(); 167 chromeos::WallpaperManager::Shutdown();
129 user_manager_enabler_.reset(); 168 user_manager_enabler_.reset();
130 command_line_.reset(); 169 command_line_.reset();
131 } 170 }
132 171
133 TestingProfile* profile() { return profile_.get(); } 172 TestingProfile* profile() { return profile_.get(); }
134 173
135 chromeos::FakeChromeUserManager* GetFakeUserManager() const { 174 chromeos::FakeChromeUserManager* GetFakeUserManager() const {
136 return static_cast<chromeos::FakeChromeUserManager*>( 175 return static_cast<chromeos::FakeChromeUserManager*>(
137 user_manager::UserManager::Get()); 176 user_manager::UserManager::Get());
138 } 177 }
139 178
179 FakeUserManagerWithLocalState* GetFakeUserManagerWithLocalState() const {
180 return static_cast<FakeUserManagerWithLocalState*>(
181 user_manager::UserManager::Get());
182 }
183
184 void SetDeviceIsEnterpriseManaged(bool is_managed) {
185 attributes_->SetIsManaged(is_managed);
186 }
187
140 void LogIn() { 188 void LogIn() {
141 const auto account_id = AccountId::FromUserEmailGaiaId( 189 const auto account_id = AccountId::FromUserEmailGaiaId(
142 profile()->GetProfileUserName(), kTestGaiaId); 190 profile()->GetProfileUserName(), kTestGaiaId);
143 GetFakeUserManager()->AddUser(account_id); 191 GetFakeUserManager()->AddUser(account_id);
144 GetFakeUserManager()->LoginUser(account_id); 192 GetFakeUserManager()->LoginUser(account_id);
145 } 193 }
146 194
195 void SetMigrationPolicy(bool migration_allowed) {
196 int pref_value = migration_allowed ? kMigrationAllowedPolicyEnabled
bartfab (slow) 2017/06/13 09:56:02 Nit: const
igorcov 2017/06/16 11:13:04 Done.
197 : kMigrationAllowedPolicyDisabled;
198 auto* command_line = base::CommandLine::ForCurrentProcess();
bartfab (slow) 2017/06/13 09:56:02 Nit: const pointer
igorcov 2017/06/16 11:13:04 Done.
199 command_line->InitFromArgv({"", "--need-arc-migration-policy-check",
200 "--arc-availability=officially-supported"});
201 SetDeviceIsEnterpriseManaged(true);
202 FakeUserManagerWithLocalState* fake_user_manager =
203 GetFakeUserManagerWithLocalState();
204 PrefServiceFactory pref_service_factory;
205 managed_pref_store_ =
206 scoped_refptr<TestingPrefStore>(new TestingPrefStore());
207 user_pref_store_ = scoped_refptr<TestingPrefStore>(new TestingPrefStore());
208 managed_pref_store_->SetInteger(prefs::kDeviceEcryptfsMigrationStrategy,
209 pref_value);
210 pref_service_factory.set_managed_prefs(managed_pref_store_.get());
211 pref_service_factory.set_user_prefs(user_pref_store_.get());
212 scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple());
213 registry->RegisterIntegerPref(prefs::kDeviceEcryptfsMigrationStrategy,
214 kMigrationAllowedPolicyUnset);
215 pref_service_ = std::unique_ptr<PrefService>(
216 pref_service_factory.Create(registry.get()));
217 pref_service_->SetInteger(prefs::kDeviceEcryptfsMigrationStrategy,
218 pref_value);
219 fake_user_manager->SetLocalState(pref_service_.get());
220 }
221
147 private: 222 private:
148 std::unique_ptr<base::test::ScopedCommandLine> command_line_; 223 std::unique_ptr<base::test::ScopedCommandLine> command_line_;
149 content::TestBrowserThreadBundle thread_bundle_; 224 content::TestBrowserThreadBundle thread_bundle_;
150 std::unique_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_; 225 std::unique_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_;
151 std::unique_ptr<TestingProfile> profile_; 226 std::unique_ptr<TestingProfile> profile_;
227 FakeInstallAttributesManaged* attributes_;
228 scoped_refptr<TestingPrefStore> managed_pref_store_;
229 scoped_refptr<TestingPrefStore> user_pref_store_;
230 std::unique_ptr<PrefService> pref_service_;
152 231
153 DISALLOW_COPY_AND_ASSIGN(ChromeArcUtilTest); 232 DISALLOW_COPY_AND_ASSIGN(ChromeArcUtilTest);
154 }; 233 };
155 234
156 TEST_F(ChromeArcUtilTest, IsArcAllowedForProfile) { 235 TEST_F(ChromeArcUtilTest, IsArcAllowedForProfile) {
157 base::CommandLine::ForCurrentProcess()->InitFromArgv( 236 base::CommandLine::ForCurrentProcess()->InitFromArgv(
158 {"", "--arc-availability=officially-supported"}); 237 {"", "--arc-availability=officially-supported"});
159 ScopedLogIn login(GetFakeUserManager(), 238 ScopedLogIn login(GetFakeUserManager(),
160 AccountId::FromUserEmailGaiaId( 239 AccountId::FromUserEmailGaiaId(
161 profile()->GetProfileUserName(), kTestGaiaId)); 240 profile()->GetProfileUserName(), kTestGaiaId));
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after
467 profile()->GetTestingPrefService()->SetManagedPref( 546 profile()->GetTestingPrefService()->SetManagedPref(
468 prefs::kArcLocationServiceEnabled, base::MakeUnique<base::Value>(false)); 547 prefs::kArcLocationServiceEnabled, base::MakeUnique<base::Value>(false));
469 EXPECT_FALSE(AreArcAllOptInPreferencesManagedForProfile(profile())); 548 EXPECT_FALSE(AreArcAllOptInPreferencesManagedForProfile(profile()));
470 549
471 // Both OptIn prefs are set to managed values, and the function returns true. 550 // Both OptIn prefs are set to managed values, and the function returns true.
472 profile()->GetTestingPrefService()->SetManagedPref( 551 profile()->GetTestingPrefService()->SetManagedPref(
473 prefs::kArcBackupRestoreEnabled, base::MakeUnique<base::Value>(false)); 552 prefs::kArcBackupRestoreEnabled, base::MakeUnique<base::Value>(false));
474 EXPECT_TRUE(AreArcAllOptInPreferencesManagedForProfile(profile())); 553 EXPECT_TRUE(AreArcAllOptInPreferencesManagedForProfile(profile()));
475 } 554 }
476 555
556 TEST_F(ChromeArcUtilTest, IsMigrationAllowedConsumerOwned) {
557 ResetArcMigrationAllowedForTesting();
558 auto* command_line = base::CommandLine::ForCurrentProcess();
559 command_line->InitFromArgv({"", "--need-arc-migration-policy-check",
560 "--arc-availability=officially-supported"});
561 SetDeviceIsEnterpriseManaged(false);
562 EXPECT_TRUE(IsArcMigrationAllowed());
563 }
564
565 TEST_F(ChromeArcUtilTest, IsMigrationAllowedNoPolicy) {
566 ResetArcMigrationAllowedForTesting();
567 auto* command_line = base::CommandLine::ForCurrentProcess();
568 command_line->InitFromArgv({"", "--need-arc-migration-policy-check",
569 "--arc-availability=officially-supported"});
570 SetDeviceIsEnterpriseManaged(true);
571
572 EXPECT_FALSE(IsArcMigrationAllowed());
573 }
574
575 TEST_F(ChromeArcUtilTest, IsMigrationAllowedPolicyAllowed) {
576 ResetArcMigrationAllowedForTesting();
577 SetMigrationPolicy(true);
578
579 EXPECT_TRUE(IsArcMigrationAllowed());
580 }
581
582 TEST_F(ChromeArcUtilTest, IsMigrationAllowedPolicyDisabled) {
583 ResetArcMigrationAllowedForTesting();
584 SetMigrationPolicy(false);
585
586 EXPECT_FALSE(IsArcMigrationAllowed());
587 }
588
477 } // namespace util 589 } // namespace util
478 } // namespace arc 590 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698