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

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: Changed flag value 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/prefs/pref_registry_simple.h"
24 #include "components/prefs/pref_service.h" 27 #include "components/prefs/pref_service.h"
28 #include "components/prefs/pref_service_factory.h"
25 #include "components/prefs/testing_pref_service.h" 29 #include "components/prefs/testing_pref_service.h"
30 #include "components/prefs/testing_pref_store.h"
26 #include "components/signin/core/account_id/account_id.h" 31 #include "components/signin/core/account_id/account_id.h"
27 #include "components/sync_preferences/testing_pref_service_syncable.h" 32 #include "components/sync_preferences/testing_pref_service_syncable.h"
28 #include "components/user_manager/known_user.h" 33 #include "components/user_manager/known_user.h"
29 #include "components/user_manager/user_manager.h" 34 #include "components/user_manager/user_manager.h"
30 #include "components/user_manager/user_names.h" 35 #include "components/user_manager/user_names.h"
31 #include "content/public/test/test_browser_thread_bundle.h" 36 #include "content/public/test/test_browser_thread_bundle.h"
32 #include "testing/gtest/include/gtest/gtest.h" 37 #include "testing/gtest/include/gtest/gtest.h"
33 38
34 namespace arc { 39 namespace arc {
35 namespace util { 40 namespace util {
36 41
37 namespace { 42 namespace {
38 43
39 constexpr char kTestProfileName[] = "user@gmail.com"; 44 constexpr char kTestProfileName[] = "user@gmail.com";
40 constexpr char kTestGaiaId[] = "1234567890"; 45 constexpr char kTestGaiaId[] = "1234567890";
41 46
47 // The constants matching the values from DeviceEcryptfsMigrationStrategy
48 // policy.
49 constexpr int kMigrationAllowedPolicyUnset = 0;
50 constexpr int kMigrationAllowedPolicyDisabled = 1;
51 constexpr int kMigrationAllowedPolicyEnabled = 2;
52
42 class ScopedLogIn { 53 class ScopedLogIn {
43 public: 54 public:
44 ScopedLogIn( 55 ScopedLogIn(
45 chromeos::FakeChromeUserManager* fake_user_manager, 56 chromeos::FakeChromeUserManager* fake_user_manager,
46 const AccountId& account_id, 57 const AccountId& account_id,
47 user_manager::UserType user_type = user_manager::USER_TYPE_REGULAR) 58 user_manager::UserType user_type = user_manager::USER_TYPE_REGULAR)
48 : fake_user_manager_(fake_user_manager), account_id_(account_id) { 59 : fake_user_manager_(fake_user_manager), account_id_(account_id) {
49 switch (user_type) { 60 switch (user_type) {
50 case user_manager::USER_TYPE_REGULAR: // fallthrough 61 case user_manager::USER_TYPE_REGULAR: // fallthrough
51 case user_manager::USER_TYPE_ACTIVE_DIRECTORY: 62 case user_manager::USER_TYPE_ACTIVE_DIRECTORY:
(...skipping 29 matching lines...) Expand all
81 } 92 }
82 93
83 void LogOut() { fake_user_manager_->RemoveUserFromList(account_id_); } 94 void LogOut() { fake_user_manager_->RemoveUserFromList(account_id_); }
84 95
85 chromeos::FakeChromeUserManager* fake_user_manager_; 96 chromeos::FakeChromeUserManager* fake_user_manager_;
86 const AccountId account_id_; 97 const AccountId account_id_;
87 98
88 DISALLOW_COPY_AND_ASSIGN(ScopedLogIn); 99 DISALLOW_COPY_AND_ASSIGN(ScopedLogIn);
89 }; 100 };
90 101
102 class FakeInstallAttributesManaged : public chromeos::InstallAttributes {
103 public:
104 FakeInstallAttributesManaged() : chromeos::InstallAttributes(nullptr) {
105 device_locked_ = true;
106 }
107
108 ~FakeInstallAttributesManaged() {
109 policy::BrowserPolicyConnectorChromeOS::RemoveInstallAttributesForTesting();
110 }
111
112 void SetIsManaged(bool is_managed) {
113 if (is_managed)
hidehiko 2017/06/09 09:40:39 nit/style/optional: I'd recommend ?: for this case
igorcov 2017/06/09 12:55:12 Done.
114 registration_mode_ = policy::DEVICE_MODE_ENTERPRISE;
115 else
116 registration_mode_ = policy::DEVICE_MODE_CONSUMER;
117 }
118 };
119
91 class FakeUserManagerWithLocalState : public chromeos::FakeChromeUserManager { 120 class FakeUserManagerWithLocalState : public chromeos::FakeChromeUserManager {
92 public: 121 public:
93 FakeUserManagerWithLocalState() 122 FakeUserManagerWithLocalState()
94 : test_local_state_(base::MakeUnique<TestingPrefServiceSimple>()) { 123 : test_local_state_(base::MakeUnique<TestingPrefServiceSimple>()) {
95 RegisterPrefs(test_local_state_->registry()); 124 RegisterPrefs(test_local_state_->registry());
96 } 125 }
97 126
98 PrefService* GetLocalState() const override { 127 PrefService* GetLocalState() const override {
99 return test_local_state_.get(); 128 if (pref_service_)
hidehiko 2017/06/09 09:40:39 style: if (pref_service_) return pref_service_;
igorcov 2017/06/09 12:55:12 Done.
129 return pref_service_;
130 else
131 return test_local_state_.get();
132 }
133
134 void SetLocalState(PrefService* pref_service) {
135 pref_service_ = pref_service;
100 } 136 }
101 137
102 private: 138 private:
103 std::unique_ptr<TestingPrefServiceSimple> test_local_state_; 139 std::unique_ptr<TestingPrefServiceSimple> test_local_state_;
140 PrefService* pref_service_ = nullptr;
104 141
105 DISALLOW_COPY_AND_ASSIGN(FakeUserManagerWithLocalState); 142 DISALLOW_COPY_AND_ASSIGN(FakeUserManagerWithLocalState);
106 }; 143 };
107 144
108 } // namespace 145 } // namespace
109 146
110 class ChromeArcUtilTest : public testing::Test { 147 class ChromeArcUtilTest : public testing::Test {
111 public: 148 public:
112 ChromeArcUtilTest() = default; 149 ChromeArcUtilTest() {
150 std::unique_ptr<FakeInstallAttributesManaged> attributes =
hidehiko 2017/06/09 09:40:39 nit/optional: auto may help you to avoid the longe
igorcov 2017/06/09 12:55:12 Done.
151 base::MakeUnique<FakeInstallAttributesManaged>();
152 attributes_ = attributes.get();
153 policy::BrowserPolicyConnectorChromeOS::SetInstallAttributesForTesting(
154 attributes.release());
155 }
156
113 ~ChromeArcUtilTest() override = default; 157 ~ChromeArcUtilTest() override = default;
114 158
115 void SetUp() override { 159 void SetUp() override {
116 command_line_ = base::MakeUnique<base::test::ScopedCommandLine>(); 160 command_line_ = base::MakeUnique<base::test::ScopedCommandLine>();
117 161
118 user_manager_enabler_ = 162 user_manager_enabler_ =
119 base::MakeUnique<chromeos::ScopedUserManagerEnabler>( 163 base::MakeUnique<chromeos::ScopedUserManagerEnabler>(
120 new FakeUserManagerWithLocalState()); 164 new FakeUserManagerWithLocalState());
121 chromeos::WallpaperManager::Initialize(); 165 chromeos::WallpaperManager::Initialize();
122 profile_ = base::MakeUnique<TestingProfile>(); 166 profile_ = base::MakeUnique<TestingProfile>();
123 profile_->set_profile_name(kTestProfileName); 167 profile_->set_profile_name(kTestProfileName);
124 } 168 }
125 169
126 void TearDown() override { 170 void TearDown() override {
127 profile_.reset(); 171 profile_.reset();
128 chromeos::WallpaperManager::Shutdown(); 172 chromeos::WallpaperManager::Shutdown();
129 user_manager_enabler_.reset(); 173 user_manager_enabler_.reset();
130 command_line_.reset(); 174 command_line_.reset();
131 } 175 }
132 176
133 TestingProfile* profile() { return profile_.get(); } 177 TestingProfile* profile() { return profile_.get(); }
134 178
135 chromeos::FakeChromeUserManager* GetFakeUserManager() const { 179 chromeos::FakeChromeUserManager* GetFakeUserManager() const {
136 return static_cast<chromeos::FakeChromeUserManager*>( 180 return static_cast<chromeos::FakeChromeUserManager*>(
137 user_manager::UserManager::Get()); 181 user_manager::UserManager::Get());
138 } 182 }
139 183
184 FakeUserManagerWithLocalState* GetFakeUserManagerWithLocalState() const {
185 return static_cast<FakeUserManagerWithLocalState*>(
186 user_manager::UserManager::Get());
187 }
188
189 void SetDeviceIsEnterpriseManaged(bool is_managed) {
190 attributes_->SetIsManaged(is_managed);
191 }
192
140 void LogIn() { 193 void LogIn() {
141 const auto account_id = AccountId::FromUserEmailGaiaId( 194 const auto account_id = AccountId::FromUserEmailGaiaId(
142 profile()->GetProfileUserName(), kTestGaiaId); 195 profile()->GetProfileUserName(), kTestGaiaId);
143 GetFakeUserManager()->AddUser(account_id); 196 GetFakeUserManager()->AddUser(account_id);
144 GetFakeUserManager()->LoginUser(account_id); 197 GetFakeUserManager()->LoginUser(account_id);
145 } 198 }
146 199
147 private: 200 private:
148 std::unique_ptr<base::test::ScopedCommandLine> command_line_; 201 std::unique_ptr<base::test::ScopedCommandLine> command_line_;
149 content::TestBrowserThreadBundle thread_bundle_; 202 content::TestBrowserThreadBundle thread_bundle_;
150 std::unique_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_; 203 std::unique_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_;
151 std::unique_ptr<TestingProfile> profile_; 204 std::unique_ptr<TestingProfile> profile_;
205 FakeInstallAttributesManaged* attributes_;
152 206
153 DISALLOW_COPY_AND_ASSIGN(ChromeArcUtilTest); 207 DISALLOW_COPY_AND_ASSIGN(ChromeArcUtilTest);
154 }; 208 };
155 209
156 TEST_F(ChromeArcUtilTest, IsArcAllowedForProfile) { 210 TEST_F(ChromeArcUtilTest, IsArcAllowedForProfile) {
157 base::CommandLine::ForCurrentProcess()->InitFromArgv( 211 base::CommandLine::ForCurrentProcess()->InitFromArgv(
158 {"", "--arc-availability=officially-supported"}); 212 {"", "--arc-availability=officially-supported"});
159 ScopedLogIn login(GetFakeUserManager(), 213 ScopedLogIn login(GetFakeUserManager(),
160 AccountId::FromUserEmailGaiaId( 214 AccountId::FromUserEmailGaiaId(
161 profile()->GetProfileUserName(), kTestGaiaId)); 215 profile()->GetProfileUserName(), kTestGaiaId));
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after
467 profile()->GetTestingPrefService()->SetManagedPref( 521 profile()->GetTestingPrefService()->SetManagedPref(
468 prefs::kArcLocationServiceEnabled, base::MakeUnique<base::Value>(false)); 522 prefs::kArcLocationServiceEnabled, base::MakeUnique<base::Value>(false));
469 EXPECT_FALSE(AreArcAllOptInPreferencesManagedForProfile(profile())); 523 EXPECT_FALSE(AreArcAllOptInPreferencesManagedForProfile(profile()));
470 524
471 // Both OptIn prefs are set to managed values, and the function returns true. 525 // Both OptIn prefs are set to managed values, and the function returns true.
472 profile()->GetTestingPrefService()->SetManagedPref( 526 profile()->GetTestingPrefService()->SetManagedPref(
473 prefs::kArcBackupRestoreEnabled, base::MakeUnique<base::Value>(false)); 527 prefs::kArcBackupRestoreEnabled, base::MakeUnique<base::Value>(false));
474 EXPECT_TRUE(AreArcAllOptInPreferencesManagedForProfile(profile())); 528 EXPECT_TRUE(AreArcAllOptInPreferencesManagedForProfile(profile()));
475 } 529 }
476 530
531 TEST_F(ChromeArcUtilTest, IsMigrationAllowedDeviceOwned) {
532 ResetGlobalDataForTesting();
533 auto* command_line = base::CommandLine::ForCurrentProcess();
534 command_line->InitFromArgv({"", "--need-arc-migration-policy-check",
535 "--arc-availability=officially-supported"});
536 SetDeviceIsEnterpriseManaged(false);
537 ScopedLogIn login(GetFakeUserManager(),
538 AccountId::FromUserEmailGaiaId(
539 profile()->GetProfileUserName(), kTestGaiaId));
540 EXPECT_TRUE(IsArcAllowedForProfile(profile()));
541 }
542
543 TEST_F(ChromeArcUtilTest, IsMigrationAllowedNoPolicy) {
544 ResetGlobalDataForTesting();
545 auto* command_line = base::CommandLine::ForCurrentProcess();
546 command_line->InitFromArgv({"", "--need-arc-migration-policy-check",
547 "--arc-availability=officially-supported"});
548 SetDeviceIsEnterpriseManaged(true);
549 ScopedLogIn login(GetFakeUserManager(),
550 AccountId::FromUserEmailGaiaId(
551 profile()->GetProfileUserName(), kTestGaiaId));
552
553 EXPECT_FALSE(IsArcAllowedForProfile(profile()));
554 }
555
556 TEST_F(ChromeArcUtilTest, IsMigrationAllowedPolicyAllowed) {
557 ResetGlobalDataForTesting();
558 auto* command_line = base::CommandLine::ForCurrentProcess();
559 command_line->InitFromArgv({"", "--need-arc-migration-policy-check",
560 "--arc-availability=officially-supported"});
561 SetDeviceIsEnterpriseManaged(true);
562 FakeUserManagerWithLocalState* fake_user_manager =
563 GetFakeUserManagerWithLocalState();
564 PrefServiceFactory pref_service_factory;
565 scoped_refptr<TestingPrefStore> managed_pref_store(new TestingPrefStore());
566 scoped_refptr<TestingPrefStore> user_pref_store(new TestingPrefStore());
567 managed_pref_store->SetInteger(prefs::kDeviceEcryptfsMigrationStrategy,
568 kMigrationAllowedPolicyEnabled);
569 pref_service_factory.set_managed_prefs(managed_pref_store.get());
570 pref_service_factory.set_user_prefs(user_pref_store.get());
571 scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple());
572 registry->RegisterIntegerPref(prefs::kDeviceEcryptfsMigrationStrategy,
573 kMigrationAllowedPolicyUnset);
574 std::unique_ptr<PrefService> pref_service(
575 pref_service_factory.Create(registry.get()));
576 pref_service->SetInteger(prefs::kDeviceEcryptfsMigrationStrategy,
577 kMigrationAllowedPolicyEnabled);
578 fake_user_manager->SetLocalState(pref_service.get());
579 ScopedLogIn login(fake_user_manager,
580 AccountId::FromUserEmailGaiaId(
581 profile()->GetProfileUserName(), kTestGaiaId));
582
583 EXPECT_TRUE(IsArcAllowedForProfile(profile()));
584 }
585
586 TEST_F(ChromeArcUtilTest, IsMigrationAllowedPolicyDisabled) {
587 ResetGlobalDataForTesting();
588 auto* command_line = base::CommandLine::ForCurrentProcess();
589 command_line->InitFromArgv({"", "--need-arc-migration-policy-check",
590 "--arc-availability=officially-supported"});
591 SetDeviceIsEnterpriseManaged(true);
592 FakeUserManagerWithLocalState* fake_user_manager =
593 GetFakeUserManagerWithLocalState();
594 PrefServiceFactory pref_service_factory;
595 scoped_refptr<TestingPrefStore> managed_pref_store(new TestingPrefStore());
596 scoped_refptr<TestingPrefStore> user_pref_store(new TestingPrefStore());
597 managed_pref_store->SetInteger(prefs::kDeviceEcryptfsMigrationStrategy,
598 kMigrationAllowedPolicyDisabled);
599 pref_service_factory.set_managed_prefs(managed_pref_store.get());
600 pref_service_factory.set_user_prefs(user_pref_store.get());
601 scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple());
602 registry->RegisterIntegerPref(prefs::kDeviceEcryptfsMigrationStrategy,
603 kMigrationAllowedPolicyUnset);
604 std::unique_ptr<PrefService> pref_service(
605 pref_service_factory.Create(registry.get()));
606 pref_service->SetInteger(prefs::kDeviceEcryptfsMigrationStrategy,
607 kMigrationAllowedPolicyDisabled);
608 fake_user_manager->SetLocalState(pref_service.get());
609 ScopedLogIn login(fake_user_manager,
610 AccountId::FromUserEmailGaiaId(
611 profile()->GetProfileUserName(), kTestGaiaId));
612
613 EXPECT_FALSE(IsArcAllowedForProfile(profile()));
614 }
615
477 } // namespace util 616 } // namespace util
478 } // namespace arc 617 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698