1 // Copyright 2020 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/chromeos/policy/status_collector/affiliated_session_service.h"
6 
7 #include "base/test/simple_test_clock.h"
8 #include "chrome/browser/chromeos/login/users/chrome_user_manager.h"
9 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
10 #include "chrome/browser/chromeos/login/users/mock_user_manager.h"
11 #include "chrome/browser/chromeos/profiles/profile_helper.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "chromeos/dbus/power/fake_power_manager_client.h"
14 #include "components/session_manager/core/session_manager.h"
15 #include "components/user_manager/scoped_user_manager.h"
16 #include "content/public/test/browser_task_environment.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 
19 namespace policy {
20 
21 class AffiliatedSessionServiceTest
22     : public ::testing::Test,
23       public policy::AffiliatedSessionService::Observer {
24  protected:
25   using SessionState = session_manager::SessionState;
26 
SetUp()27   void SetUp() override {
28     chromeos::PowerManagerClient::InitializeFake();
29     auto user_manager = std::make_unique<chromeos::FakeChromeUserManager>();
30     user_manager_ = user_manager.get();
31     user_manager_enabler_ = std::make_unique<user_manager::ScopedUserManager>(
32         std::move(user_manager));
33 
34     affiliated_session_service_ =
35         std::make_unique<AffiliatedSessionService>(&test_clock_);
36   }
37 
TearDown()38   void TearDown() override {
39     affiliated_session_service_.reset();
40     chromeos::PowerManagerClient::Shutdown();
41   }
42 
CreateProfile(AccountId account_id,bool is_affiliated,bool login)43   std::unique_ptr<TestingProfile> CreateProfile(AccountId account_id,
44                                                 bool is_affiliated,
45                                                 bool login) {
46     TestingProfile::Builder profile_builder;
47     profile_builder.SetProfileName(account_id.GetUserEmail());
48     auto profile = profile_builder.Build();
49     user_manager_->AddUserWithAffiliationAndTypeAndProfile(
50         account_id, is_affiliated, user_manager::UserType::USER_TYPE_REGULAR,
51         profile.get());
52     if (login) {
53       user_manager_->LoginUser(account_id, true);
54     }
55     return profile;
56   }
57 
affiliated_session_service()58   AffiliatedSessionService* affiliated_session_service() {
59     return affiliated_session_service_.get();
60   }
61 
session_manager()62   session_manager::SessionManager* session_manager() {
63     return &session_manager_;
64   }
65 
power_manager_client()66   chromeos::FakePowerManagerClient* power_manager_client() {
67     return chromeos::FakePowerManagerClient::Get();
68   }
69 
test_clock()70   base::SimpleTestClock* test_clock() { return &test_clock_; }
71 
OnAffiliatedLogin(Profile * profile)72   void OnAffiliatedLogin(Profile* profile) override { logged_in_ = profile; }
OnAffiliatedLogout(Profile * profile)73   void OnAffiliatedLogout(Profile* profile) override { logged_out_ = profile; }
OnLocked()74   void OnLocked() override { locked_ = true; }
OnUnlocked()75   void OnUnlocked() override { unlocked_ = true; }
OnResumeActive(base::Time time)76   void OnResumeActive(base::Time time) override {
77     suspend_time_ = std::make_unique<base::Time>(time);
78   }
79 
80   Profile* logged_in_ = nullptr;
81   Profile* logged_out_ = nullptr;
82   bool locked_ = false;
83   bool unlocked_ = false;
84   std::unique_ptr<base::Time> suspend_time_;
85 
86  private:
87   content::BrowserTaskEnvironment task_environment_;
88 
89   chromeos::FakeChromeUserManager* user_manager_;
90   std::unique_ptr<user_manager::ScopedUserManager> user_manager_enabler_;
91 
92   session_manager::SessionManager session_manager_;
93 
94   base::SimpleTestClock test_clock_;
95 
96   std::unique_ptr<AffiliatedSessionService> affiliated_session_service_;
97 };
98 
TEST_F(AffiliatedSessionServiceTest,OnSessionStateChanged)99 TEST_F(AffiliatedSessionServiceTest, OnSessionStateChanged) {
100   affiliated_session_service()->AddObserver(this);
101 
102   session_manager()->SetSessionState(SessionState::LOCKED);
103   session_manager()->SetSessionState(SessionState::ACTIVE);
104 
105   EXPECT_TRUE(locked_);
106   EXPECT_TRUE(unlocked_);
107 
108   session_manager()->SetSessionState(SessionState::LOCKED);
109 
110   EXPECT_TRUE(locked_);
111 
112   locked_ = false;
113   unlocked_ = false;
114 
115   session_manager()->SetSessionState(SessionState::LOCKED);
116   session_manager()->SetSessionState(SessionState::LOGIN_PRIMARY);
117 
118   EXPECT_FALSE(locked_);
119   EXPECT_TRUE(unlocked_);
120 
121   session_manager()->SetSessionState(SessionState::LOCKED);
122 
123   EXPECT_TRUE(locked_);
124 
125   locked_ = false;
126   unlocked_ = false;
127 
128   session_manager()->SetSessionState(SessionState::ACTIVE);
129 
130   EXPECT_TRUE(unlocked_);
131 }
132 
TEST_F(AffiliatedSessionServiceTest,OnUserProfileLoadedAffiliatedAndPrimary)133 TEST_F(AffiliatedSessionServiceTest, OnUserProfileLoadedAffiliatedAndPrimary) {
134   AccountId affiliated_account_id =
135       AccountId::FromUserEmail("user0@managed.com");
136   std::unique_ptr<TestingProfile> affiliated_profile = CreateProfile(
137       affiliated_account_id, true /* affiliated */, true /* login */);
138   affiliated_session_service()->AddObserver(this);
139 
140   session_manager()->NotifyUserProfileLoaded(affiliated_account_id);
141 
142   EXPECT_TRUE(affiliated_profile->IsSameOrParent(logged_in_));
143 }
144 
TEST_F(AffiliatedSessionServiceTest,OnUserProfileLoadedAffiliated)145 TEST_F(AffiliatedSessionServiceTest, OnUserProfileLoadedAffiliated) {
146   AccountId secondary_account_id =
147       AccountId::FromUserEmail("user3@managed.com");
148   std::unique_ptr<TestingProfile> secondary_profile = CreateProfile(
149       secondary_account_id, true /* affiliated */, false /* login */);
150   affiliated_session_service()->AddObserver(this);
151 
152   session_manager()->NotifyUserProfileLoaded(secondary_account_id);
153 
154   EXPECT_EQ(logged_in_, nullptr);
155 }
156 
TEST_F(AffiliatedSessionServiceTest,OnUserProfileLoadedPrimary)157 TEST_F(AffiliatedSessionServiceTest, OnUserProfileLoadedPrimary) {
158   AccountId unaffiliated_account_id =
159       AccountId::FromUserEmail("user2@managed.com");
160   std::unique_ptr<TestingProfile> unaffiliated_profile = CreateProfile(
161       unaffiliated_account_id, false /* affiliated */, true /* login */);
162   affiliated_session_service()->AddObserver(this);
163 
164   session_manager()->NotifyUserProfileLoaded(unaffiliated_account_id);
165 
166   EXPECT_EQ(logged_in_, nullptr);
167 }
168 
TEST_F(AffiliatedSessionServiceTest,OnProfileWillBeDestroyedAffiliatedAndPrimary)169 TEST_F(AffiliatedSessionServiceTest,
170        OnProfileWillBeDestroyedAffiliatedAndPrimary) {
171   AccountId affiliated_account_id =
172       AccountId::FromUserEmail("user0@managed.com");
173   std::unique_ptr<TestingProfile> affiliated_profile = CreateProfile(
174       affiliated_account_id, true /* affiliated */, true /* login */);
175   affiliated_session_service()->AddObserver(this);
176 
177   session_manager()->NotifyUserProfileLoaded(affiliated_account_id);
178   affiliated_profile->MaybeSendDestroyedNotification();
179 
180   EXPECT_TRUE(affiliated_profile->IsSameOrParent(logged_out_));
181 }
182 
TEST_F(AffiliatedSessionServiceTest,OnProfileWillBeDestroyedAffiliated)183 TEST_F(AffiliatedSessionServiceTest, OnProfileWillBeDestroyedAffiliated) {
184   AccountId secondary_account_id =
185       AccountId::FromUserEmail("user3@managed.com");
186   std::unique_ptr<TestingProfile> secondary_profile = CreateProfile(
187       secondary_account_id, true /* affiliated */, false /* login */);
188   affiliated_session_service()->AddObserver(this);
189 
190   session_manager()->NotifyUserProfileLoaded(secondary_account_id);
191   secondary_profile->MaybeSendDestroyedNotification();
192 
193   EXPECT_EQ(logged_out_, nullptr);
194 }
195 
TEST_F(AffiliatedSessionServiceTest,OnProfileWillBeDestroyedPrimary)196 TEST_F(AffiliatedSessionServiceTest, OnProfileWillBeDestroyedPrimary) {
197   AccountId unaffiliated_account_id =
198       AccountId::FromUserEmail("user2@managed.com");
199   std::unique_ptr<TestingProfile> unaffiliated_profile = CreateProfile(
200       unaffiliated_account_id, false /* affiliated */, true /* login */);
201   affiliated_session_service()->AddObserver(this);
202 
203   session_manager()->NotifyUserProfileLoaded(unaffiliated_account_id);
204   unaffiliated_profile->MaybeSendDestroyedNotification();
205 
206   EXPECT_EQ(logged_out_, nullptr);
207 }
208 
TEST_F(AffiliatedSessionServiceTest,SuspendDone)209 TEST_F(AffiliatedSessionServiceTest, SuspendDone) {
210   affiliated_session_service()->AddObserver(this);
211   test_clock()->SetNow(base::Time::Now());
212   base::TimeDelta sleep_duration = base::TimeDelta::FromHours(2);
213 
214   power_manager_client()->SendSuspendDone(sleep_duration);
215 
216   EXPECT_EQ(*suspend_time_, test_clock()->Now() - sleep_duration);
217 }
218 
TEST_F(AffiliatedSessionServiceTest,RemoveObserver)219 TEST_F(AffiliatedSessionServiceTest, RemoveObserver) {
220   AccountId account_id = AccountId::FromUserEmail("user0@managed.com");
221   std::unique_ptr<TestingProfile> profile =
222       CreateProfile(account_id, true /* affiliated */, true /* login */);
223   affiliated_session_service()->AddObserver(this);
224 
225   affiliated_session_service()->RemoveObserver(this);
226 
227   session_manager()->SetSessionState(SessionState::LOCKED);
228   session_manager()->SetSessionState(SessionState::ACTIVE);
229   session_manager()->SetSessionState(SessionState::LOCKED);
230   EXPECT_FALSE(locked_);
231   EXPECT_FALSE(unlocked_);
232 
233   session_manager()->NotifyUserProfileLoaded(account_id);
234   EXPECT_FALSE(profile->IsSameOrParent(logged_in_));
235 
236   profile->MaybeSendDestroyedNotification();
237   EXPECT_FALSE(profile->IsSameOrParent(logged_out_));
238 }
239 
240 }  // namespace policy
241