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/extension_install_event_log_manager_wrapper.h"
6
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/location.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/run_loop.h"
14 #include "base/sequenced_task_runner.h"
15 #include "chrome/browser/chromeos/policy/extension_install_event_log.h"
16 #include "chrome/common/pref_names.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "components/policy/proto/device_management_backend.pb.h"
19 #include "components/prefs/pref_service.h"
20 #include "content/public/test/browser_task_environment.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23
24 using testing::Mock;
25
26 namespace em = enterprise_management;
27
28 namespace policy {
29
30 namespace {
31
32 constexpr base::FilePath::CharType kLogFileName[] =
33 FILE_PATH_LITERAL("extension_install_log");
34
35 constexpr char kExtensionId[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
36
37 class ExtensionInstallEventLogManagerWrapperTestable
38 : public ExtensionInstallEventLogManagerWrapper {
39 public:
ExtensionInstallEventLogManagerWrapperTestable(Profile * profile)40 explicit ExtensionInstallEventLogManagerWrapperTestable(Profile* profile)
41 : ExtensionInstallEventLogManagerWrapper(profile) {}
42
log_task_runner()43 scoped_refptr<base::SequencedTaskRunner> log_task_runner() {
44 return log_task_runner_->GetTaskRunner();
45 }
46
47 // Make |Init()| visible for testing.
48 using ExtensionInstallEventLogManagerWrapper::Init;
49
50 // ExtensionInstallEventLogManagerWrapper:
51 MOCK_METHOD0(CreateManager, void());
52 MOCK_METHOD0(DestroyManager, void());
53 };
54
55 } // namespace
56
57 class ExtensionInstallEventLogManagerWrapperTest : public testing::Test {
58 protected:
ExtensionInstallEventLogManagerWrapperTest()59 ExtensionInstallEventLogManagerWrapperTest()
60 : log_file_path_(profile_.GetPath().Append(kLogFileName)) {}
61
PopulateLogFile()62 void PopulateLogFile() {
63 ExtensionInstallEventLog log(log_file_path_);
64 em::ExtensionInstallReportLogEvent event;
65 event.set_timestamp(0);
66 event.set_event_type(em::ExtensionInstallReportLogEvent::SUCCESS);
67 log.Add(kExtensionId, event);
68 log.Store();
69 }
70
FlushPendingTasks()71 void FlushPendingTasks() {
72 base::RunLoop run_loop;
73 ASSERT_TRUE(log_task_runner_);
74 log_task_runner_->PostTaskAndReply(FROM_HERE, base::DoNothing(),
75 run_loop.QuitClosure());
76 run_loop.Run();
77 }
78
CreateWrapper()79 void CreateWrapper() {
80 wrapper_ = std::make_unique<ExtensionInstallEventLogManagerWrapperTestable>(
81 &profile_);
82 log_task_runner_ = wrapper_->log_task_runner();
83 }
84
DestroyWrapper()85 void DestroyWrapper() {
86 wrapper_.reset();
87 FlushPendingTasks();
88 log_task_runner_ = nullptr;
89 }
90
InitWrapper()91 void InitWrapper() {
92 ASSERT_TRUE(wrapper_);
93 wrapper_->Init();
94 FlushPendingTasks();
95 }
96
97 content::BrowserTaskEnvironment task_environment_;
98 TestingProfile profile_;
99
100 const base::FilePath log_file_path_;
101
102 std::unique_ptr<ExtensionInstallEventLogManagerWrapperTestable> wrapper_;
103
104 scoped_refptr<base::SequencedTaskRunner> log_task_runner_;
105 };
106
107 // Populate a log file. Enable reporting. Create a wrapper. Verify that a
108 // manager is created and the log file is not cleared. Then, destroy the
109 // wrapper. Verify that the log file is not cleared.
TEST_F(ExtensionInstallEventLogManagerWrapperTest,EnableCreate)110 TEST_F(ExtensionInstallEventLogManagerWrapperTest, EnableCreate) {
111 PopulateLogFile();
112 profile_.GetPrefs()->SetBoolean(prefs::kExtensionInstallEventLoggingEnabled,
113 true);
114
115 CreateWrapper();
116
117 EXPECT_CALL(*wrapper_, CreateManager());
118 EXPECT_CALL(*wrapper_, DestroyManager()).Times(0);
119 InitWrapper();
120 // Verify that the log file is not cleared.
121 EXPECT_TRUE(base::PathExists(log_file_path_));
122 Mock::VerifyAndClearExpectations(&wrapper_);
123
124 DestroyWrapper();
125 // Verify that the log file is not cleared.
126 EXPECT_TRUE(base::PathExists(log_file_path_));
127 }
128
129 // Populate a log file. Disable reporting. Create a wrapper. Verify that no
130 // manager is created and the log file is cleared.
TEST_F(ExtensionInstallEventLogManagerWrapperTest,DisableCreate)131 TEST_F(ExtensionInstallEventLogManagerWrapperTest, DisableCreate) {
132 PopulateLogFile();
133 profile_.GetPrefs()->SetBoolean(prefs::kExtensionInstallEventLoggingEnabled,
134 false);
135
136 CreateWrapper();
137
138 EXPECT_CALL(*wrapper_, CreateManager()).Times(0);
139 EXPECT_CALL(*wrapper_, DestroyManager());
140 InitWrapper();
141 // Verify that the log file is cleared.
142 EXPECT_FALSE(base::PathExists(log_file_path_));
143 }
144
145 // Disable reporting. Create a wrapper. Verify that no manager is created. Then,
146 // enable reporting. Verify that a manager is created. Populate a log file.
147 // Then, destroy the wrapper. Verify that the log file is not cleared.
TEST_F(ExtensionInstallEventLogManagerWrapperTest,CreateEnable)148 TEST_F(ExtensionInstallEventLogManagerWrapperTest, CreateEnable) {
149 profile_.GetPrefs()->SetBoolean(prefs::kExtensionInstallEventLoggingEnabled,
150 false);
151
152 CreateWrapper();
153
154 EXPECT_CALL(*wrapper_, CreateManager()).Times(0);
155 EXPECT_CALL(*wrapper_, DestroyManager());
156 InitWrapper();
157 Mock::VerifyAndClearExpectations(&wrapper_);
158
159 EXPECT_CALL(*wrapper_, CreateManager());
160 EXPECT_CALL(*wrapper_, DestroyManager()).Times(0);
161 profile_.GetPrefs()->SetBoolean(prefs::kExtensionInstallEventLoggingEnabled,
162 true);
163 Mock::VerifyAndClearExpectations(&wrapper_);
164 FlushPendingTasks();
165
166 PopulateLogFile();
167
168 DestroyWrapper();
169 // Verify that the log file is not cleared.
170 EXPECT_TRUE(base::PathExists(log_file_path_));
171 }
172
173 // Populate a log file. Enable reporting. Create a wrapper. Verify that a
174 // manager is created and the log file is not cleared. Then, disable reporting.
175 // Verify that the manager is destroyed and the log file is cleared.
TEST_F(ExtensionInstallEventLogManagerWrapperTest,CreateDisable)176 TEST_F(ExtensionInstallEventLogManagerWrapperTest, CreateDisable) {
177 PopulateLogFile();
178 profile_.GetPrefs()->SetBoolean(prefs::kExtensionInstallEventLoggingEnabled,
179 true);
180
181 CreateWrapper();
182
183 EXPECT_CALL(*wrapper_, CreateManager());
184 EXPECT_CALL(*wrapper_, DestroyManager()).Times(0);
185 InitWrapper();
186 // Verify that the log file is not cleared.
187 EXPECT_TRUE(base::PathExists(log_file_path_));
188 Mock::VerifyAndClearExpectations(&wrapper_);
189
190 EXPECT_CALL(*wrapper_, CreateManager()).Times(0);
191 EXPECT_CALL(*wrapper_, DestroyManager());
192 profile_.GetPrefs()->SetBoolean(prefs::kExtensionInstallEventLoggingEnabled,
193 false);
194 Mock::VerifyAndClearExpectations(&wrapper_);
195 FlushPendingTasks();
196 // Verify that the log file is cleared.
197 EXPECT_FALSE(base::PathExists(log_file_path_));
198 }
199
200 } // namespace policy
201