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_collector.h"
6
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/run_loop.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
12 #include "chrome/browser/chromeos/profiles/profile_helper.h"
13 #include "chrome/browser/extensions/forced_extensions/force_installed_tracker.h"
14 #include "chrome/browser/extensions/test_extension_system.h"
15 #include "chrome/browser/prefs/browser_prefs.h"
16 #include "chrome/common/pref_names.h"
17 #include "chrome/test/base/testing_browser_process.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "chromeos/constants/chromeos_switches.h"
20 #include "chromeos/dbus/dbus_thread_manager.h"
21 #include "chromeos/dbus/power/fake_power_manager_client.h"
22 #include "chromeos/dbus/shill/shill_service_client.h"
23 #include "chromeos/network/network_handler.h"
24 #include "components/policy/proto/device_management_backend.pb.h"
25 #include "components/prefs/testing_pref_service.h"
26 #include "components/user_manager/scoped_user_manager.h"
27 #include "components/user_manager/user_names.h"
28 #include "content/public/test/browser_task_environment.h"
29 #include "extensions/browser/extension_system.h"
30 #include "extensions/common/extension_builder.h"
31 #include "net/base/net_errors.h"
32 #include "testing/gtest/include/gtest/gtest.h"
33 #include "third_party/cros_system_api/dbus/service_constants.h"
34
35 namespace em = enterprise_management;
36
37 namespace policy {
38
39 namespace {
40
41 constexpr char kEthernetServicePath[] = "/service/eth1";
42 constexpr char kWifiServicePath[] = "/service/wifi1";
43
44 constexpr char kExtensionId1[] = "abcdefghijklmnopabcdefghijklmnop";
45
46 constexpr char kExtensionName1[] = "name1";
47
48 constexpr char kEmailId[] = "test@example.com";
49 constexpr char kGaiaId[] = "12345";
50
51 const int kFetchTries = 5;
52 // HTTP_UNAUTHORIZED
53 const int kResponseCode = 401;
54
55 class FakeExtensionInstallEventLogCollectorDelegate
56 : public ExtensionInstallEventLogCollector::Delegate {
57 public:
58 FakeExtensionInstallEventLogCollectorDelegate() = default;
59 ~FakeExtensionInstallEventLogCollectorDelegate() override = default;
60
61 struct Request {
Requestpolicy::__anone0683a210111::FakeExtensionInstallEventLogCollectorDelegate::Request62 Request(bool for_all,
63 bool add_disk_space_info,
64 const extensions::ExtensionId& extension_id,
65 const em::ExtensionInstallReportLogEvent& event)
66 : for_all(for_all),
67 add_disk_space_info(add_disk_space_info),
68 extension_id(extension_id),
69 event(event) {}
70 const bool for_all;
71 const bool add_disk_space_info;
72 const extensions::ExtensionId extension_id;
73 const em::ExtensionInstallReportLogEvent event;
74 };
75
76 // AppInstallEventLogCollector::Delegate:
AddForAllExtensions(std::unique_ptr<em::ExtensionInstallReportLogEvent> event)77 void AddForAllExtensions(
78 std::unique_ptr<em::ExtensionInstallReportLogEvent> event) override {
79 ++add_for_all_count_;
80 requests_.emplace_back(true /* for_all */, false /* add_disk_space_info */,
81 std::string() /* extension_id */, *event);
82 }
83
Add(const extensions::ExtensionId & extension_id,bool add_disk_space_info,std::unique_ptr<em::ExtensionInstallReportLogEvent> event)84 void Add(const extensions::ExtensionId& extension_id,
85 bool add_disk_space_info,
86 std::unique_ptr<em::ExtensionInstallReportLogEvent> event) override {
87 ++add_count_;
88 requests_.emplace_back(false /* for_all */, add_disk_space_info,
89 extension_id, *event);
90 }
91
OnExtensionInstallationFinished(const extensions::ExtensionId & extension_id)92 void OnExtensionInstallationFinished(
93 const extensions::ExtensionId& extension_id) override {
94 extensions_.erase(extension_id);
95 }
96
IsExtensionPending(const extensions::ExtensionId & extension_id)97 bool IsExtensionPending(
98 const extensions::ExtensionId& extension_id) override {
99 return extensions_.find(extension_id) != extensions_.end();
100 }
101
add_for_all_count() const102 int add_for_all_count() const { return add_for_all_count_; }
103
add_count() const104 int add_count() const { return add_count_; }
105
last_event() const106 const em::ExtensionInstallReportLogEvent& last_event() const {
107 return last_request().event;
108 }
last_request() const109 const Request& last_request() const { return requests_.back(); }
requests() const110 const std::vector<Request>& requests() const { return requests_; }
111
112 private:
113 std::set<extensions::ExtensionId> extensions_ = {kExtensionId1};
114 int add_for_all_count_ = 0;
115 int add_count_ = 0;
116 std::vector<Request> requests_;
117 };
118
119 } // namespace
120
121 class ExtensionInstallEventLogCollectorTest : public testing::Test {
122 protected:
123 ExtensionInstallEventLogCollectorTest() = default;
124 ~ExtensionInstallEventLogCollectorTest() override = default;
125
SetUp()126 void SetUp() override {
127 RegisterLocalState(pref_service_.registry());
128 TestingBrowserProcess::GetGlobal()->SetLocalState(&pref_service_);
129
130 chromeos::DBusThreadManager::Initialize();
131 chromeos::PowerManagerClient::InitializeFake();
132 chromeos::NetworkHandler::Initialize();
133 profile_ = std::make_unique<TestingProfile>();
134 registry_ = extensions::ExtensionRegistry::Get(profile_.get());
135 install_stage_tracker_ =
136 extensions::InstallStageTracker::Get(profile_.get());
137 InitExtensionSystem();
138 service_test_ = chromeos::DBusThreadManager::Get()
139 ->GetShillServiceClient()
140 ->GetTestInterface();
141 service_test_->AddService(kEthernetServicePath, "eth1_guid", "eth1",
142 shill::kTypeEthernet, shill::kStateOffline,
143 true /* visible */);
144 service_test_->AddService(kWifiServicePath, "wifi1_guid", "wifi1",
145 shill::kTypeEthernet, shill::kStateOffline,
146 true /* visible */);
147 base::RunLoop().RunUntilIdle();
148 }
149
TearDown()150 void TearDown() override {
151 profile_.reset();
152 chromeos::NetworkHandler::Shutdown();
153 chromeos::PowerManagerClient::Shutdown();
154 chromeos::DBusThreadManager::Shutdown();
155 TestingBrowserProcess::GetGlobal()->SetLocalState(nullptr);
156 }
157
InitExtensionSystem()158 void InitExtensionSystem() {
159 extensions::TestExtensionSystem* extension_system =
160 static_cast<extensions::TestExtensionSystem*>(
161 extensions::ExtensionSystem::Get(profile_.get()));
162 extension_system->CreateExtensionService(
163 base::CommandLine::ForCurrentProcess(),
164 base::FilePath() /* install_directory */,
165 false /* autoupdate_enabled */);
166 }
167
SetNetworkState(network::NetworkConnectionTracker::NetworkConnectionObserver * observer,const std::string & service_path,const std::string & state)168 void SetNetworkState(
169 network::NetworkConnectionTracker::NetworkConnectionObserver* observer,
170 const std::string& service_path,
171 const std::string& state) {
172 service_test_->SetServiceProperty(service_path, shill::kStateProperty,
173 base::Value(state));
174 base::RunLoop().RunUntilIdle();
175
176 network::mojom::ConnectionType connection_type =
177 network::mojom::ConnectionType::CONNECTION_NONE;
178 const std::string* network_state =
179 service_test_->GetServiceProperties(kWifiServicePath)
180 ->FindStringKey(shill::kStateProperty);
181 if (network_state && *network_state == shill::kStateOnline) {
182 connection_type = network::mojom::ConnectionType::CONNECTION_WIFI;
183 }
184 network_state = service_test_->GetServiceProperties(kEthernetServicePath)
185 ->FindStringKey(shill::kStateProperty);
186 if (network_state && *network_state == shill::kStateOnline) {
187 connection_type = network::mojom::ConnectionType::CONNECTION_ETHERNET;
188 }
189 if (observer)
190 observer->OnConnectionChanged(connection_type);
191 base::RunLoop().RunUntilIdle();
192 }
193
VerifyEventAddedSuccessfully(int expected_add_count,int expected_add_all_count)194 testing::AssertionResult VerifyEventAddedSuccessfully(
195 int expected_add_count,
196 int expected_add_all_count) {
197 if (expected_add_count != delegate()->add_count()) {
198 return testing::AssertionFailure()
199 << "Expected add count: " << expected_add_count
200 << " but actual add count: " << delegate()->add_count();
201 }
202 if (expected_add_all_count != delegate()->add_for_all_count()) {
203 return testing::AssertionFailure()
204 << "Expected add all count: " << expected_add_all_count
205 << " but actual add all count: "
206 << delegate()->add_for_all_count();
207 }
208 if (delegate()->last_request().extension_id != kExtensionId1) {
209 return testing::AssertionFailure()
210 << "Expected extension id: " << kExtensionId1
211 << " but actual extension id: "
212 << delegate()->last_request().extension_id;
213 }
214 return testing::AssertionSuccess();
215 }
216
profile()217 TestingProfile* profile() { return profile_.get(); }
registry()218 extensions::ExtensionRegistry* registry() { return registry_; }
delegate()219 FakeExtensionInstallEventLogCollectorDelegate* delegate() {
220 return &delegate_;
221 }
install_stage_tracker()222 extensions::InstallStageTracker* install_stage_tracker() {
223 return install_stage_tracker_;
224 }
225
226 chromeos::ShillServiceClient::TestInterface* service_test_ = nullptr;
227
228 private:
229 content::BrowserTaskEnvironment task_environment_;
230 std::unique_ptr<TestingProfile> profile_;
231 extensions::ExtensionRegistry* registry_;
232 extensions::InstallStageTracker* install_stage_tracker_;
233 FakeExtensionInstallEventLogCollectorDelegate delegate_;
234 TestingPrefServiceSimple pref_service_;
235 };
236
237 // Test the case when collector is created and destroyed inside the one user
238 // session. In this case no event is generated. This happens for example when
239 // all extensions are installed in context of the same user session.
TEST_F(ExtensionInstallEventLogCollectorTest,NoEventsByDefault)240 TEST_F(ExtensionInstallEventLogCollectorTest, NoEventsByDefault) {
241 auto collector = std::make_unique<ExtensionInstallEventLogCollector>(
242 registry(), delegate(), profile());
243 collector.reset();
244
245 EXPECT_EQ(0, delegate()->add_count());
246 EXPECT_EQ(0, delegate()->add_for_all_count());
247 }
248
TEST_F(ExtensionInstallEventLogCollectorTest,LoginLogout)249 TEST_F(ExtensionInstallEventLogCollectorTest, LoginLogout) {
250 chromeos::FakeChromeUserManager* fake_user_manager =
251 new chromeos::FakeChromeUserManager();
252 user_manager::ScopedUserManager scoped_user_manager(
253 base::WrapUnique(fake_user_manager));
254 AccountId account_id = AccountId::FromUserEmailGaiaId(kEmailId, kGaiaId);
255 user_manager::User* user =
256 fake_user_manager->AddUserWithAffiliationAndTypeAndProfile(
257 account_id, false /*is_affiliated*/, user_manager::USER_TYPE_REGULAR,
258 profile());
259 fake_user_manager->UserLoggedIn(account_id, user->username_hash(),
260 false /* browser_restart */,
261 false /* is_child */);
262 auto collector = std::make_unique<ExtensionInstallEventLogCollector>(
263 registry(), delegate(), profile());
264
265 EXPECT_EQ(0, delegate()->add_for_all_count());
266
267 collector->AddLoginEvent();
268 EXPECT_EQ(1, delegate()->add_for_all_count());
269 EXPECT_EQ(em::ExtensionInstallReportLogEvent::SESSION_STATE_CHANGE,
270 delegate()->last_event().event_type());
271 EXPECT_EQ(em::ExtensionInstallReportLogEvent::LOGIN,
272 delegate()->last_event().session_state_change_type());
273 EXPECT_EQ(em::ExtensionInstallReportLogEvent::USER_TYPE_REGULAR,
274 delegate()->last_event().user_type());
275 EXPECT_EQ(true, delegate()->last_event().is_new_user());
276 EXPECT_TRUE(delegate()->last_event().has_online());
277 EXPECT_FALSE(delegate()->last_event().online());
278 }
279
TEST_F(ExtensionInstallEventLogCollectorTest,LoginTypes)280 TEST_F(ExtensionInstallEventLogCollectorTest, LoginTypes) {
281 chromeos::FakeChromeUserManager* fake_user_manager =
282 new chromeos::FakeChromeUserManager();
283 user_manager::ScopedUserManager scoped_user_manager(
284 base::WrapUnique(fake_user_manager));
285 AccountId account_id = AccountId::FromUserEmailGaiaId(kEmailId, kGaiaId);
286 user_manager::User* user =
287 fake_user_manager->AddUserWithAffiliationAndTypeAndProfile(
288 account_id, false /*is_affiliated*/, user_manager::USER_TYPE_REGULAR,
289 profile());
290 fake_user_manager->UserLoggedIn(account_id, user->username_hash(),
291 false /* browser_restart */,
292 false /* is_child */);
293 {
294 ExtensionInstallEventLogCollector collector(registry(), delegate(),
295 profile());
296 collector.AddLoginEvent();
297 EXPECT_EQ(1, delegate()->add_for_all_count());
298 EXPECT_EQ(em::ExtensionInstallReportLogEvent::SESSION_STATE_CHANGE,
299 delegate()->last_event().event_type());
300 EXPECT_EQ(em::ExtensionInstallReportLogEvent::LOGIN,
301 delegate()->last_event().session_state_change_type());
302 EXPECT_EQ(em::ExtensionInstallReportLogEvent::USER_TYPE_REGULAR,
303 delegate()->last_event().user_type());
304 EXPECT_EQ(true, delegate()->last_event().is_new_user());
305 EXPECT_TRUE(delegate()->last_event().has_online());
306 EXPECT_FALSE(delegate()->last_event().online());
307 }
308
309 {
310 // Check login after restart. No log is expected.
311 ExtensionInstallEventLogCollector collector(registry(), delegate(),
312 profile());
313 base::CommandLine::ForCurrentProcess()->AppendSwitch(
314 chromeos::switches::kLoginUser);
315 collector.AddLoginEvent();
316 EXPECT_EQ(1, delegate()->add_for_all_count());
317 }
318
319 {
320 // Check logout on restart. No log is expected.
321 ExtensionInstallEventLogCollector collector(registry(), delegate(),
322 profile());
323 g_browser_process->local_state()->SetBoolean(prefs::kWasRestarted, true);
324 collector.AddLogoutEvent();
325 EXPECT_EQ(1, delegate()->add_for_all_count());
326 }
327
328 EXPECT_EQ(0, delegate()->add_count());
329 }
330
TEST_F(ExtensionInstallEventLogCollectorTest,SuspendResume)331 TEST_F(ExtensionInstallEventLogCollectorTest, SuspendResume) {
332 auto collector = std::make_unique<ExtensionInstallEventLogCollector>(
333 registry(), delegate(), profile());
334
335 chromeos::FakePowerManagerClient::Get()->SendSuspendImminent(
336 power_manager::SuspendImminent_Reason_OTHER);
337 EXPECT_EQ(1, delegate()->add_for_all_count());
338 EXPECT_EQ(em::ExtensionInstallReportLogEvent::SESSION_STATE_CHANGE,
339 delegate()->last_event().event_type());
340 EXPECT_EQ(em::ExtensionInstallReportLogEvent::SUSPEND,
341 delegate()->last_event().session_state_change_type());
342 EXPECT_FALSE(delegate()->last_event().online());
343
344 chromeos::FakePowerManagerClient::Get()->SendSuspendDone();
345 EXPECT_EQ(2, delegate()->add_for_all_count());
346 EXPECT_EQ(em::ExtensionInstallReportLogEvent::SESSION_STATE_CHANGE,
347 delegate()->last_event().event_type());
348 EXPECT_EQ(em::ExtensionInstallReportLogEvent::RESUME,
349 delegate()->last_event().session_state_change_type());
350 EXPECT_FALSE(delegate()->last_event().online());
351
352 collector.reset();
353
354 EXPECT_EQ(0, delegate()->add_count());
355 }
356
357 // Connect to Ethernet. Start log collector. Verify that a login event with
358 // network state online is recorded. Then, connect to WiFi and disconnect from
359 // Ethernet, in this order. Verify that no event is recorded. Then, disconnect
360 // from WiFi. Verify that a connectivity change event is recorded. Then, connect
361 // to WiFi with a pending captive portal. Verify that no event is recorded.
362 // Then, pass the captive portal. Verify that a connectivity change is recorded.
TEST_F(ExtensionInstallEventLogCollectorTest,ConnectivityChanges)363 TEST_F(ExtensionInstallEventLogCollectorTest, ConnectivityChanges) {
364 SetNetworkState(nullptr, kEthernetServicePath, shill::kStateOnline);
365 chromeos::FakeChromeUserManager* fake_user_manager =
366 new chromeos::FakeChromeUserManager();
367 user_manager::ScopedUserManager scoped_user_manager(
368 base::WrapUnique(fake_user_manager));
369 AccountId account_id = AccountId::FromUserEmailGaiaId(kEmailId, kGaiaId);
370 user_manager::User* user =
371 fake_user_manager->AddUserWithAffiliationAndTypeAndProfile(
372 account_id, false /*is_affiliated*/, user_manager::USER_TYPE_REGULAR,
373 profile());
374 fake_user_manager->UserLoggedIn(account_id, user->username_hash(),
375 false /* browser_restart */,
376 false /* is_child */);
377
378 auto collector = std::make_unique<ExtensionInstallEventLogCollector>(
379 registry(), delegate(), profile());
380
381 EXPECT_EQ(0, delegate()->add_for_all_count());
382
383 collector->AddLoginEvent();
384 EXPECT_EQ(1, delegate()->add_for_all_count());
385 EXPECT_EQ(em::ExtensionInstallReportLogEvent::SESSION_STATE_CHANGE,
386 delegate()->last_event().event_type());
387 EXPECT_EQ(em::ExtensionInstallReportLogEvent::LOGIN,
388 delegate()->last_event().session_state_change_type());
389 EXPECT_EQ(em::ExtensionInstallReportLogEvent::USER_TYPE_REGULAR,
390 delegate()->last_event().user_type());
391 EXPECT_EQ(true, delegate()->last_event().is_new_user());
392 EXPECT_TRUE(delegate()->last_event().online());
393
394 SetNetworkState(collector.get(), kWifiServicePath, shill::kStateOnline);
395 EXPECT_EQ(1, delegate()->add_for_all_count());
396
397 SetNetworkState(collector.get(), kEthernetServicePath, shill::kStateOffline);
398 EXPECT_EQ(1, delegate()->add_for_all_count());
399
400 SetNetworkState(collector.get(), kWifiServicePath, shill::kStateOffline);
401 EXPECT_EQ(2, delegate()->add_for_all_count());
402 EXPECT_EQ(em::ExtensionInstallReportLogEvent::CONNECTIVITY_CHANGE,
403 delegate()->last_event().event_type());
404 EXPECT_FALSE(delegate()->last_event().online());
405
406 SetNetworkState(collector.get(), kWifiServicePath,
407 shill::kStateNoConnectivity);
408 EXPECT_EQ(2, delegate()->add_for_all_count());
409
410 SetNetworkState(collector.get(), kWifiServicePath, shill::kStateOnline);
411 EXPECT_EQ(3, delegate()->add_for_all_count());
412 EXPECT_EQ(em::ExtensionInstallReportLogEvent::CONNECTIVITY_CHANGE,
413 delegate()->last_event().event_type());
414 EXPECT_TRUE(delegate()->last_event().online());
415
416 collector.reset();
417
418 EXPECT_EQ(3, delegate()->add_for_all_count());
419 EXPECT_EQ(0, delegate()->add_count());
420 }
421
TEST_F(ExtensionInstallEventLogCollectorTest,ExtensionInstallFailedWithoutMisconfiguration)422 TEST_F(ExtensionInstallEventLogCollectorTest,
423 ExtensionInstallFailedWithoutMisconfiguration) {
424 auto collector = std::make_unique<ExtensionInstallEventLogCollector>(
425 registry(), delegate(), profile());
426
427 // One extension failed.
428 install_stage_tracker()->ReportInfoOnNoUpdatesFailure(kExtensionId1,
429 "rate limit");
430 install_stage_tracker()->ReportFailure(
431 kExtensionId1,
432 extensions::InstallStageTracker::FailureReason::CRX_FETCH_URL_EMPTY);
433 ASSERT_TRUE(VerifyEventAddedSuccessfully(1 /*expected_add_count*/,
434 0 /*expected_add_all_count*/));
435 EXPECT_EQ(em::ExtensionInstallReportLogEvent::INSTALLATION_FAILED,
436 delegate()->last_request().event.event_type());
437 EXPECT_EQ(em::ExtensionInstallReportLogEvent::CRX_FETCH_URL_EMPTY,
438 delegate()->last_request().event.failure_reason());
439 EXPECT_FALSE(delegate()->last_request().event.is_misconfiguration_failure());
440 }
441
TEST_F(ExtensionInstallEventLogCollectorTest,ExtensionInstallFailedWithMisconfiguration)442 TEST_F(ExtensionInstallEventLogCollectorTest,
443 ExtensionInstallFailedWithMisconfiguration) {
444 auto collector = std::make_unique<ExtensionInstallEventLogCollector>(
445 registry(), delegate(), profile());
446
447 // One extension failed.
448 install_stage_tracker()->ReportInfoOnNoUpdatesFailure(kExtensionId1, "");
449 install_stage_tracker()->ReportFailure(
450 kExtensionId1,
451 extensions::InstallStageTracker::FailureReason::CRX_FETCH_URL_EMPTY);
452 ASSERT_TRUE(VerifyEventAddedSuccessfully(1 /*expected_add_count*/,
453 0 /*expected_add_all_count*/));
454 EXPECT_EQ(em::ExtensionInstallReportLogEvent::INSTALLATION_FAILED,
455 delegate()->last_request().event.event_type());
456 EXPECT_EQ(em::ExtensionInstallReportLogEvent::CRX_FETCH_URL_EMPTY,
457 delegate()->last_request().event.failure_reason());
458 EXPECT_TRUE(delegate()->last_request().event.is_misconfiguration_failure());
459 }
460
461 // Verifies that a new event is created when the extension installation fails
462 // due to invalid ID supplied in the force list policy.
TEST_F(ExtensionInstallEventLogCollectorTest,ExtensionInstallFailed)463 TEST_F(ExtensionInstallEventLogCollectorTest, ExtensionInstallFailed) {
464 auto collector = std::make_unique<ExtensionInstallEventLogCollector>(
465 registry(), delegate(), profile());
466 extensions::InstallStageTracker* tracker =
467 extensions::InstallStageTracker::Get(profile());
468
469 // One extension failed.
470 tracker->ReportFailure(
471 kExtensionId1,
472 extensions::InstallStageTracker::FailureReason::INVALID_ID);
473
474 ASSERT_TRUE(VerifyEventAddedSuccessfully(1 /*expected_add_count*/,
475 0 /*expected_add_all_count*/));
476 EXPECT_EQ(em::ExtensionInstallReportLogEvent::INSTALLATION_FAILED,
477 delegate()->last_request().event.event_type());
478 EXPECT_EQ(em::ExtensionInstallReportLogEvent::INVALID_ID,
479 delegate()->last_request().event.failure_reason());
480 EXPECT_FALSE(delegate()->last_request().event.is_misconfiguration_failure());
481 }
482
483 // Verifies that a new event is created when the extension installation fails
484 // due to crx install error. as failure occurs due during crx installation, crx
485 // install error and event type should be reported.
TEST_F(ExtensionInstallEventLogCollectorTest,ExtensionInstallFailedWithCrxError)486 TEST_F(ExtensionInstallEventLogCollectorTest,
487 ExtensionInstallFailedWithCrxError) {
488 auto collector = std::make_unique<ExtensionInstallEventLogCollector>(
489 registry(), delegate(), profile());
490 extensions::InstallStageTracker* tracker =
491 extensions::InstallStageTracker::Get(profile());
492
493 // One extension failed.
494 tracker->ReportExtensionType(kExtensionId1,
495 extensions::Manifest::TYPE_LEGACY_PACKAGED_APP);
496 tracker->ReportCrxInstallError(
497 kExtensionId1,
498 extensions::InstallStageTracker::FailureReason::
499 CRX_INSTALL_ERROR_DECLINED,
500 extensions::CrxInstallErrorDetail::KIOSK_MODE_ONLY);
501
502 ASSERT_TRUE(VerifyEventAddedSuccessfully(1 /*expected_add_count*/,
503 0 /*expected_add_all_count*/));
504 EXPECT_EQ(em::ExtensionInstallReportLogEvent::INSTALLATION_FAILED,
505 delegate()->last_request().event.event_type());
506 EXPECT_EQ(em::ExtensionInstallReportLogEvent::CRX_INSTALL_ERROR_DECLINED,
507 delegate()->last_request().event.failure_reason());
508 EXPECT_EQ(em::ExtensionInstallReportLogEvent::KIOSK_MODE_ONLY,
509 delegate()->last_request().event.crx_install_error_detail());
510 EXPECT_EQ(em::Extension::TYPE_LEGACY_PACKAGED_APP,
511 delegate()->last_request().event.extension_type());
512 EXPECT_TRUE(delegate()->last_request().event.is_misconfiguration_failure());
513 }
514
515 // Verifies that a new event is created when the extension failed to unpack.
TEST_F(ExtensionInstallEventLogCollectorTest,ExtensionInstallFailedWithUnpackFailure)516 TEST_F(ExtensionInstallEventLogCollectorTest,
517 ExtensionInstallFailedWithUnpackFailure) {
518 auto collector = std::make_unique<ExtensionInstallEventLogCollector>(
519 registry(), delegate(), profile());
520
521 extensions::InstallStageTracker* tracker =
522 extensions::InstallStageTracker::Get(profile());
523
524 // One extension failed.
525 tracker->ReportSandboxedUnpackerFailureReason(
526 kExtensionId1,
527 extensions::SandboxedUnpackerFailureReason::CRX_HEADER_INVALID);
528 ASSERT_TRUE(VerifyEventAddedSuccessfully(1 /*expected_add_count*/,
529 0 /*expected_add_all_count*/));
530 EXPECT_EQ(em::ExtensionInstallReportLogEvent::INSTALLATION_FAILED,
531 delegate()->last_request().event.event_type());
532 EXPECT_EQ(em::ExtensionInstallReportLogEvent::
533 CRX_INSTALL_ERROR_SANDBOXED_UNPACKER_FAILURE,
534 delegate()->last_request().event.failure_reason());
535 EXPECT_EQ(em::ExtensionInstallReportLogEvent::CRX_HEADER_INVALID,
536 delegate()->last_request().event.unpacker_failure_reason());
537 }
538
539 // Verifies that a new event is created when the extension failed due to
540 // MANIFEST_INVALID failure reason.
TEST_F(ExtensionInstallEventLogCollectorTest,ExtensionInstallFailedWitManifestInvalid)541 TEST_F(ExtensionInstallEventLogCollectorTest,
542 ExtensionInstallFailedWitManifestInvalid) {
543 auto collector = std::make_unique<ExtensionInstallEventLogCollector>(
544 registry(), delegate(), profile());
545
546 extensions::InstallStageTracker* tracker =
547 extensions::InstallStageTracker::Get(profile());
548
549 // One extension failed.
550 extensions::ExtensionDownloaderDelegate::FailureData data(
551 extensions::ManifestInvalidError::MISSING_APP_ID);
552 tracker->ReportManifestInvalidFailure(kExtensionId1, data);
553 ASSERT_TRUE(VerifyEventAddedSuccessfully(1 /*expected_add_count*/,
554 0 /*expected_add_all_count*/));
555 EXPECT_EQ(em::ExtensionInstallReportLogEvent::INSTALLATION_FAILED,
556 delegate()->last_request().event.event_type());
557 EXPECT_EQ(em::ExtensionInstallReportLogEvent::MANIFEST_INVALID,
558 delegate()->last_request().event.failure_reason());
559 EXPECT_EQ(em::ExtensionInstallReportLogEvent::MISSING_APP_ID,
560 delegate()->last_request().event.manifest_invalid_error());
561 }
562
563 // Verifies that a new event with error codes and number of fetch tries is
564 // created when the extension failed with error MANFIEST_FETCH_FAILED.
TEST_F(ExtensionInstallEventLogCollectorTest,ExtensionInstallFailedWithManifestFetchFailed)565 TEST_F(ExtensionInstallEventLogCollectorTest,
566 ExtensionInstallFailedWithManifestFetchFailed) {
567 auto collector = std::make_unique<ExtensionInstallEventLogCollector>(
568 registry(), delegate(), profile());
569
570 extensions::InstallStageTracker* tracker =
571 extensions::InstallStageTracker::Get(profile());
572
573 // One extension failed.
574 extensions::ExtensionDownloaderDelegate::FailureData data(
575 net::Error::OK, kResponseCode, kFetchTries);
576 tracker->ReportFetchError(
577 kExtensionId1,
578 extensions::InstallStageTracker::FailureReason::MANIFEST_FETCH_FAILED,
579 data);
580 ASSERT_TRUE(VerifyEventAddedSuccessfully(1 /*expected_add_count*/,
581 0 /*expected_add_all_count*/));
582 EXPECT_EQ(em::ExtensionInstallReportLogEvent::INSTALLATION_FAILED,
583 delegate()->last_request().event.event_type());
584 EXPECT_EQ(em::ExtensionInstallReportLogEvent::MANIFEST_FETCH_FAILED,
585 delegate()->last_request().event.failure_reason());
586 EXPECT_EQ(kResponseCode, delegate()->last_request().event.fetch_error_code());
587 EXPECT_EQ(kFetchTries, delegate()->last_request().event.fetch_tries());
588 }
589
590 // Verifies that a new event with fetch error code and number of fetch tries is
591 // created when the extension failed with error CRX_FETCH_FAILED.
TEST_F(ExtensionInstallEventLogCollectorTest,ExtensionInstallFailedWithCrxFetchFailed)592 TEST_F(ExtensionInstallEventLogCollectorTest,
593 ExtensionInstallFailedWithCrxFetchFailed) {
594 auto collector = std::make_unique<ExtensionInstallEventLogCollector>(
595 registry(), delegate(), profile());
596
597 extensions::InstallStageTracker* tracker =
598 extensions::InstallStageTracker::Get(profile());
599
600 // One extension failed.
601 extensions::ExtensionDownloaderDelegate::FailureData data(
602 net::Error::OK, kResponseCode, kFetchTries);
603 tracker->ReportFetchError(
604 kExtensionId1,
605 extensions::InstallStageTracker::FailureReason::CRX_FETCH_FAILED, data);
606 ASSERT_TRUE(VerifyEventAddedSuccessfully(1 /*expected_add_count*/,
607 0 /*expected_add_all_count*/));
608 EXPECT_EQ(em::ExtensionInstallReportLogEvent::INSTALLATION_FAILED,
609 delegate()->last_request().event.event_type());
610 EXPECT_EQ(em::ExtensionInstallReportLogEvent::CRX_FETCH_FAILED,
611 delegate()->last_request().event.failure_reason());
612 EXPECT_EQ(kResponseCode, delegate()->last_request().event.fetch_error_code());
613 EXPECT_EQ(kFetchTries, delegate()->last_request().event.fetch_tries());
614 }
615
616 // Verifies that a new event is created when an extension is successfully
617 // installed.
TEST_F(ExtensionInstallEventLogCollectorTest,InstallExtension)618 TEST_F(ExtensionInstallEventLogCollectorTest, InstallExtension) {
619 auto collector = std::make_unique<ExtensionInstallEventLogCollector>(
620 registry(), delegate(), profile());
621
622 // One extension installation succeeded.
623 auto ext = extensions::ExtensionBuilder(kExtensionName1)
624 .SetID(kExtensionId1)
625 .Build();
626 collector->OnExtensionLoaded(profile(), ext.get());
627 ASSERT_TRUE(VerifyEventAddedSuccessfully(1 /*expected_add_count*/,
628 0 /*expected_add_all_count*/));
629 EXPECT_EQ(em::ExtensionInstallReportLogEvent::SUCCESS,
630 delegate()->last_request().event.event_type());
631 EXPECT_EQ(em::Extension::TYPE_EXTENSION,
632 delegate()->last_request().event.extension_type());
633 }
634
635 // Verifies that a new event is created when the installation stage is changed
636 // during the installation process.
TEST_F(ExtensionInstallEventLogCollectorTest,InstallationStageChanged)637 TEST_F(ExtensionInstallEventLogCollectorTest, InstallationStageChanged) {
638 auto collector = std::make_unique<ExtensionInstallEventLogCollector>(
639 registry(), delegate(), profile());
640
641 // One extension installation succeeded.
642 auto ext = extensions::ExtensionBuilder(kExtensionName1)
643 .SetID(kExtensionId1)
644 .Build();
645 collector->OnExtensionInstallationStageChanged(
646 kExtensionId1, extensions::InstallStageTracker::Stage::DOWNLOADING);
647 ASSERT_TRUE(VerifyEventAddedSuccessfully(1 /*expected_add_count*/,
648 0 /*expected_add_all_count*/));
649 EXPECT_EQ(em::ExtensionInstallReportLogEvent::DOWNLOADING,
650 delegate()->last_request().event.installation_stage());
651 collector->OnExtensionInstallationStageChanged(
652 kExtensionId1, extensions::InstallStageTracker::Stage::INSTALLING);
653 EXPECT_EQ(em::ExtensionInstallReportLogEvent::INSTALLING,
654 delegate()->last_request().event.installation_stage());
655 ASSERT_TRUE(VerifyEventAddedSuccessfully(2 /*expected_add_count*/,
656 0 /*expected_add_all_count*/));
657 }
658
659 // Verifies that a new event is created when the downloading stage is changed
660 // during the downloading process.
TEST_F(ExtensionInstallEventLogCollectorTest,DownloadingStageChanged)661 TEST_F(ExtensionInstallEventLogCollectorTest, DownloadingStageChanged) {
662 auto collector = std::make_unique<ExtensionInstallEventLogCollector>(
663 registry(), delegate(), profile());
664
665 auto ext = extensions::ExtensionBuilder(kExtensionName1)
666 .SetID(kExtensionId1)
667 .Build();
668 collector->OnExtensionDownloadingStageChanged(
669 kExtensionId1, extensions::ExtensionDownloaderDelegate::Stage::PENDING);
670 ASSERT_TRUE(VerifyEventAddedSuccessfully(1 /*expected_add_count*/,
671 0 /*expected_add_all_count*/));
672 EXPECT_EQ(em::ExtensionInstallReportLogEvent::DOWNLOAD_PENDING,
673 delegate()->last_request().event.downloading_stage());
674 collector->OnExtensionDownloadingStageChanged(
675 kExtensionId1,
676 extensions::ExtensionDownloaderDelegate::Stage::DOWNLOADING_MANIFEST);
677 ASSERT_TRUE(VerifyEventAddedSuccessfully(2 /*expected_add_count*/,
678 0 /*expected_add_all_count*/));
679 EXPECT_EQ(em::ExtensionInstallReportLogEvent::DOWNLOADING_MANIFEST,
680 delegate()->last_request().event.downloading_stage());
681 collector->OnExtensionDownloadingStageChanged(
682 kExtensionId1,
683 extensions::ExtensionDownloaderDelegate::Stage::DOWNLOADING_CRX);
684 ASSERT_TRUE(VerifyEventAddedSuccessfully(3 /*expected_add_count*/,
685 0 /*expected_add_all_count*/));
686 EXPECT_EQ(em::ExtensionInstallReportLogEvent::DOWNLOADING_CRX,
687 delegate()->last_request().event.downloading_stage());
688 collector->OnExtensionDownloadingStageChanged(
689 kExtensionId1, extensions::ExtensionDownloaderDelegate::Stage::FINISHED);
690 ASSERT_TRUE(VerifyEventAddedSuccessfully(4 /*expected_add_count*/,
691 0 /*expected_add_all_count*/));
692 EXPECT_EQ(em::ExtensionInstallReportLogEvent::FINISHED,
693 delegate()->last_request().event.downloading_stage());
694 }
695
696 // Verifies that a new event is created when the install creation stage is
697 // changed.
TEST_F(ExtensionInstallEventLogCollectorTest,InstallCreationStageChanged)698 TEST_F(ExtensionInstallEventLogCollectorTest, InstallCreationStageChanged) {
699 auto collector = std::make_unique<ExtensionInstallEventLogCollector>(
700 registry(), delegate(), profile());
701
702 auto ext = extensions::ExtensionBuilder(kExtensionName1)
703 .SetID(kExtensionId1)
704 .Build();
705 collector->OnExtensionInstallCreationStageChanged(
706 kExtensionId1, extensions::InstallStageTracker::InstallCreationStage::
707 CREATION_INITIATED);
708 ASSERT_TRUE(VerifyEventAddedSuccessfully(1 /*expected_add_count*/,
709 0 /*expected_add_all_count*/));
710 EXPECT_EQ(em::ExtensionInstallReportLogEvent::CREATION_INITIATED,
711 delegate()->last_request().event.install_creation_stage());
712 collector->OnExtensionInstallCreationStageChanged(
713 kExtensionId1, extensions::InstallStageTracker::InstallCreationStage::
714 NOTIFIED_FROM_MANAGEMENT);
715 ASSERT_TRUE(VerifyEventAddedSuccessfully(2 /*expected_add_count*/,
716 0 /*expected_add_all_count*/));
717 EXPECT_EQ(em::ExtensionInstallReportLogEvent::NOTIFIED_FROM_MANAGEMENT,
718 delegate()->last_request().event.install_creation_stage());
719 collector->OnExtensionInstallCreationStageChanged(
720 kExtensionId1, extensions::InstallStageTracker::InstallCreationStage::
721 SEEN_BY_POLICY_LOADER);
722 ASSERT_TRUE(VerifyEventAddedSuccessfully(3 /*expected_add_count*/,
723 0 /*expected_add_all_count*/));
724 EXPECT_EQ(em::ExtensionInstallReportLogEvent::SEEN_BY_POLICY_LOADER,
725 delegate()->last_request().event.install_creation_stage());
726 collector->OnExtensionInstallCreationStageChanged(
727 kExtensionId1, extensions::InstallStageTracker::InstallCreationStage::
728 SEEN_BY_EXTERNAL_PROVIDER);
729 ASSERT_TRUE(VerifyEventAddedSuccessfully(4 /*expected_add_count*/,
730 0 /*expected_add_all_count*/));
731 EXPECT_EQ(em::ExtensionInstallReportLogEvent::SEEN_BY_EXTERNAL_PROVIDER,
732 delegate()->last_request().event.install_creation_stage());
733 }
734
735 // Verifies that a new event is created when the cache status is retrieved
736 // during the extension downloading process.
TEST_F(ExtensionInstallEventLogCollectorTest,DownloadCacheStatusRetrieved)737 TEST_F(ExtensionInstallEventLogCollectorTest, DownloadCacheStatusRetrieved) {
738 auto collector = std::make_unique<ExtensionInstallEventLogCollector>(
739 registry(), delegate(), profile());
740
741 auto ext = extensions::ExtensionBuilder(kExtensionName1)
742 .SetID(kExtensionId1)
743 .Build();
744 collector->OnExtensionDownloadCacheStatusRetrieved(
745 kExtensionId1,
746 extensions::ExtensionDownloaderDelegate::CacheStatus::CACHE_MISS);
747 ASSERT_TRUE(VerifyEventAddedSuccessfully(1 /*expected_add_count*/,
748 0 /*expected_add_all_count*/));
749 }
750
751 } // namespace policy
752