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

Side by Side Diff: components/arc/arc_util_unittest.cc

Issue 2885933003: arc: Consolidate IsArcAllowedForUser logic (Closed)
Patch Set: fix #2 Created 3 years, 7 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 "components/arc/arc_util.h" 5 #include "components/arc/arc_util.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "base/test/scoped_feature_list.h" 13 #include "base/test/scoped_feature_list.h"
14 #include "components/signin/core/account_id/account_id.h"
15 #include "components/user_manager/fake_user_manager.h"
16 #include "components/user_manager/user.h"
14 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
15 18
16 namespace arc { 19 namespace arc {
17 namespace { 20 namespace {
18 21
19 // If an instance is created, based on the value passed to the consturctor, 22 // If an instance is created, based on the value passed to the consturctor,
20 // EnableARC feature is enabled/disabled in the scope. 23 // EnableARC feature is enabled/disabled in the scope.
21 class ScopedArcFeature { 24 class ScopedArcFeature {
22 public: 25 public:
23 explicit ScopedArcFeature(bool enabled) { 26 explicit ScopedArcFeature(bool enabled) {
24 constexpr char kArcFeatureName[] = "EnableARC"; 27 constexpr char kArcFeatureName[] = "EnableARC";
25 if (enabled) { 28 if (enabled) {
26 feature_list.InitFromCommandLine(kArcFeatureName, std::string()); 29 feature_list.InitFromCommandLine(kArcFeatureName, std::string());
27 } else { 30 } else {
28 feature_list.InitFromCommandLine(std::string(), kArcFeatureName); 31 feature_list.InitFromCommandLine(std::string(), kArcFeatureName);
29 } 32 }
30 } 33 }
31 ~ScopedArcFeature() = default; 34 ~ScopedArcFeature() = default;
32 35
33 private: 36 private:
34 base::test::ScopedFeatureList feature_list; 37 base::test::ScopedFeatureList feature_list;
35 DISALLOW_COPY_AND_ASSIGN(ScopedArcFeature); 38 DISALLOW_COPY_AND_ASSIGN(ScopedArcFeature);
36 }; 39 };
37 40
41 // Helper to enable user_manager::FakeUserManager while it is in scope.
42 // TODO(xiyuan): Remove after ScopedUserManagerEnabler is moved to user_manager.
43 class ScopedUserManager {
44 public:
45 explicit ScopedUserManager(user_manager::UserManager* user_manager)
46 : user_manager_(user_manager) {
47 DCHECK(!user_manager::UserManager::IsInitialized());
48 user_manager->Initialize();
49 }
50 ~ScopedUserManager() {
51 DCHECK_EQ(user_manager::UserManager::Get(), user_manager_);
52 user_manager_->Shutdown();
53 user_manager_->Destroy();
54 }
55
56 private:
57 user_manager::UserManager* const user_manager_;
58
59 DISALLOW_COPY_AND_ASSIGN(ScopedUserManager);
60 };
61
62 // Fake user that can be created with a specified type.
63 class FakeUser : public user_manager::User {
64 public:
65 explicit FakeUser(user_manager::UserType user_type)
66 : User(AccountId::FromUserEmail("user@test.com")),
67 user_type_(user_type) {}
68 ~FakeUser() override = default;
69
70 // user_manager::User:
71 user_manager::UserType GetType() const override { return user_type_; }
72
73 private:
74 const user_manager::UserType user_type_;
75
76 DISALLOW_COPY_AND_ASSIGN(FakeUser);
77 };
78
38 using ArcUtilTest = testing::Test; 79 using ArcUtilTest = testing::Test;
39 80
40 TEST_F(ArcUtilTest, IsArcAvailable_None) { 81 TEST_F(ArcUtilTest, IsArcAvailable_None) {
41 auto* command_line = base::CommandLine::ForCurrentProcess(); 82 auto* command_line = base::CommandLine::ForCurrentProcess();
42 83
43 command_line->InitFromArgv({"", "--arc-availability=none"}); 84 command_line->InitFromArgv({"", "--arc-availability=none"});
44 EXPECT_FALSE(IsArcAvailable()); 85 EXPECT_FALSE(IsArcAvailable());
45 86
46 // If --arc-availability flag is set to "none", even if Finch experiment is 87 // If --arc-availability flag is set to "none", even if Finch experiment is
47 // turned on, ARC cannot be used. 88 // turned on, ARC cannot be used.
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 197
157 TEST_F(ArcUtilTest, IsArcOptInVerificationDisabled) { 198 TEST_F(ArcUtilTest, IsArcOptInVerificationDisabled) {
158 auto* command_line = base::CommandLine::ForCurrentProcess(); 199 auto* command_line = base::CommandLine::ForCurrentProcess();
159 command_line->InitFromArgv({""}); 200 command_line->InitFromArgv({""});
160 EXPECT_FALSE(IsArcOptInVerificationDisabled()); 201 EXPECT_FALSE(IsArcOptInVerificationDisabled());
161 202
162 command_line->InitFromArgv({"", "--disable-arc-opt-in-verification"}); 203 command_line->InitFromArgv({"", "--disable-arc-opt-in-verification"});
163 EXPECT_TRUE(IsArcOptInVerificationDisabled()); 204 EXPECT_TRUE(IsArcOptInVerificationDisabled());
164 } 205 }
165 206
207 TEST_F(ArcUtilTest, IsArcAllowedForUser) {
208 user_manager::FakeUserManager fake_user_manager;
209 ScopedUserManager scoped_user_manager(&fake_user_manager);
210
211 struct {
212 user_manager::UserType user_type;
213 bool expected_allowed;
214 } const kTestCases[] = {
215 {user_manager::USER_TYPE_REGULAR, true},
216 {user_manager::USER_TYPE_GUEST, false},
217 {user_manager::USER_TYPE_PUBLIC_ACCOUNT, false},
218 {user_manager::USER_TYPE_SUPERVISED, false},
219 {user_manager::USER_TYPE_KIOSK_APP, false},
220 {user_manager::USER_TYPE_CHILD, true},
221 {user_manager::USER_TYPE_ARC_KIOSK_APP, false},
222 {user_manager::USER_TYPE_ACTIVE_DIRECTORY, false},
223 };
224 for (const auto& test_case : kTestCases) {
225 const FakeUser user(test_case.user_type);
226 EXPECT_EQ(test_case.expected_allowed, IsArcAllowedForUser(&user))
227 << "User type=" << test_case.user_type;
228 }
229
230 // Active directory users are allowed when ARC is supported for active
231 // directory managed devices.
232 auto* command_line = base::CommandLine::ForCurrentProcess();
233 command_line->InitFromArgv(
234 {"", "--arc-availability=officially-supported-with-active-directory"});
235 const FakeUser active_directory_user(
236 user_manager::USER_TYPE_ACTIVE_DIRECTORY);
237 EXPECT_TRUE(IsArcAllowedForUser(&active_directory_user));
238
239 // An ephemeral user is a logged in user but unknown to UserManager when
240 // ephemeral policy is set.
241 fake_user_manager.SetEphemeralUsersEnabled(true);
242 fake_user_manager.UserLoggedIn(AccountId::FromUserEmail("test@test.com"),
243 "test@test.com-hash", false);
244 const user_manager::User* ephemeral_user = fake_user_manager.GetActiveUser();
245 ASSERT_TRUE(ephemeral_user && fake_user_manager.IsUserCryptohomeDataEphemeral(
246 ephemeral_user->GetAccountId()));
247
248 // Ephemeral user is not allowed for ARC.
249 EXPECT_FALSE(IsArcAllowedForUser(ephemeral_user));
250 }
251
166 } // namespace 252 } // namespace
167 } // namespace arc 253 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698