1 // Copyright 2018 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/safe_browsing/test_extension_event_observer.h"
6
7 #include "base/strings/string_number_conversions.h"
8 #include "chrome/browser/extensions/api/safe_browsing_private/safe_browsing_private_event_router.h"
9 #include "chrome/common/extensions/api/safe_browsing_private.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace OnSecurityInterstitialShown =
13 extensions::api::safe_browsing_private::OnSecurityInterstitialShown;
14 namespace OnSecurityInterstitialProceeded =
15 extensions::api::safe_browsing_private::OnSecurityInterstitialProceeded;
16 namespace OnPolicySpecifiedPasswordReuseDetected = extensions::api::
17 safe_browsing_private::OnPolicySpecifiedPasswordReuseDetected;
18 namespace OnPolicySpecifiedPasswordChanged =
19 extensions::api::safe_browsing_private::OnPolicySpecifiedPasswordChanged;
20 namespace OnDangerousDownloadOpened =
21 extensions::api::safe_browsing_private::OnDangerousDownloadOpened;
22
23 namespace safe_browsing {
24
TestExtensionEventObserver(extensions::TestEventRouter * event_router)25 TestExtensionEventObserver::TestExtensionEventObserver(
26 extensions::TestEventRouter* event_router) {
27 event_router->AddEventObserver(this);
28 }
29
PassEventArgs()30 base::Value TestExtensionEventObserver::PassEventArgs() {
31 return std::move(latest_event_args_);
32 }
33
OnBroadcastEvent(const extensions::Event & event)34 void TestExtensionEventObserver::OnBroadcastEvent(
35 const extensions::Event& event) {
36 if (event.event_name == OnSecurityInterstitialProceeded::kEventName ||
37 event.event_name == OnSecurityInterstitialShown::kEventName ||
38 event.event_name == OnPolicySpecifiedPasswordReuseDetected::kEventName ||
39 event.event_name == OnPolicySpecifiedPasswordChanged::kEventName ||
40 event.event_name == OnDangerousDownloadOpened::kEventName) {
41 latest_event_args_ = event.event_args->Clone();
42 latest_event_name_ = event.event_name;
43 }
44 }
45
VerifyLatestSecurityInterstitialEvent(const std::string & expected_event_name,const GURL & expected_page_url,const std::string & expected_reason,const std::string & expected_username,int expected_net_error_code)46 void TestExtensionEventObserver::VerifyLatestSecurityInterstitialEvent(
47 const std::string& expected_event_name,
48 const GURL& expected_page_url,
49 const std::string& expected_reason,
50 const std::string& expected_username,
51 int expected_net_error_code) {
52 EXPECT_EQ(expected_event_name, latest_event_name_);
53 auto captured_args = PassEventArgs().GetList()[0].Clone();
54 EXPECT_EQ(expected_page_url.spec(),
55 captured_args.FindKey("url")->GetString());
56 EXPECT_EQ(expected_reason, captured_args.FindKey("reason")->GetString());
57 if (!expected_username.empty())
58 EXPECT_EQ(expected_username,
59 captured_args.FindKey("userName")->GetString());
60 if (expected_net_error_code == 0)
61 EXPECT_FALSE(captured_args.FindKey("netErrorCode"));
62 else
63 EXPECT_EQ(base::NumberToString(expected_net_error_code),
64 captured_args.FindKey("netErrorCode")->GetString());
65 }
66
BuildSafeBrowsingPrivateEventRouter(content::BrowserContext * context)67 std::unique_ptr<KeyedService> BuildSafeBrowsingPrivateEventRouter(
68 content::BrowserContext* context) {
69 return std::unique_ptr<KeyedService>(
70 new extensions::SafeBrowsingPrivateEventRouter(context));
71 }
72
73 } // namespace safe_browsing
74