1 // Copyright 2015 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 <memory>
6 #include <string>
7 
8 #include "base/location.h"
9 #include "base/macros.h"
10 #include "base/run_loop.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/threading/thread_task_runner_handle.h"
13 #include "base/values.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/chromeos/net/network_portal_detector_impl.h"
16 #include "chrome/browser/extensions/extension_apitest.h"
17 #include "chrome/browser/notifications/notification_display_service_tester.h"
18 #include "chrome/browser/ui/ash/network/network_portal_notification_controller.h"
19 #include "chromeos/dbus/dbus_thread_manager.h"
20 #include "chromeos/dbus/shill/shill_device_client.h"
21 #include "chromeos/dbus/shill/shill_profile_client.h"
22 #include "chromeos/dbus/shill/shill_service_client.h"
23 #include "components/captive_portal/core/captive_portal_testing_utils.h"
24 #include "content/public/test/test_utils.h"
25 #include "extensions/test/result_catcher.h"
26 #include "net/base/net_errors.h"
27 #include "third_party/cros_system_api/dbus/service_constants.h"
28 #include "ui/message_center/public/cpp/notification.h"
29 #include "ui/message_center/public/cpp/notification_delegate.h"
30 
31 using chromeos::DBusThreadManager;
32 using chromeos::NetworkPortalDetector;
33 using chromeos::NetworkPortalDetectorImpl;
34 using chromeos::NetworkPortalNotificationController;
35 using chromeos::ShillDeviceClient;
36 using chromeos::ShillProfileClient;
37 using chromeos::ShillServiceClient;
38 
39 namespace {
40 
41 const char kWifiDevicePath[] = "/device/stub_wifi_device1";
42 const char kWifi1ServicePath[] = "stub_wifi1";
43 const char kWifi1ServiceGUID[] = "wifi1_guid";
44 
45 }  // namespace
46 
47 class NetworkingConfigTest
48     : public extensions::ExtensionApiTest,
49       public captive_portal::CaptivePortalDetectorTestBase {
50  public:
NetworkingConfigTest()51   NetworkingConfigTest() : network_portal_detector_(nullptr) {}
52   ~NetworkingConfigTest() override = default;
53 
SetUpOnMainThread()54   void SetUpOnMainThread() override {
55     extensions::ExtensionApiTest::SetUpOnMainThread();
56     content::RunAllPendingInMessageLoop();
57 
58     display_service_ = std::make_unique<NotificationDisplayServiceTester>(
59         nullptr /* profile */);
60 
61     DBusThreadManager* const dbus_manager = DBusThreadManager::Get();
62     ShillServiceClient::TestInterface* const service_test =
63         dbus_manager->GetShillServiceClient()->GetTestInterface();
64     ShillDeviceClient::TestInterface* const device_test =
65         dbus_manager->GetShillDeviceClient()->GetTestInterface();
66     ShillProfileClient::TestInterface* const profile_test =
67         dbus_manager->GetShillProfileClient()->GetTestInterface();
68 
69     device_test->ClearDevices();
70     service_test->ClearServices();
71 
72     device_test->AddDevice(kWifiDevicePath, shill::kTypeWifi,
73                            "stub_wifi_device1");
74 
75     service_test->AddService(kWifi1ServicePath, kWifi1ServiceGUID, "wifi1",
76                              shill::kTypeWifi, shill::kStateOnline,
77                              true /* add_to_visible */);
78     service_test->SetServiceProperty(kWifi1ServicePath, shill::kWifiBSsid,
79                                      base::Value("01:02:ab:7f:90:00"));
80     service_test->SetServiceProperty(
81         kWifi1ServicePath, shill::kSignalStrengthProperty, base::Value(40));
82     profile_test->AddService(ShillProfileClient::GetSharedProfilePath(),
83                              kWifi1ServicePath);
84 
85     content::RunAllPendingInMessageLoop();
86 
87     network_portal_detector_ =
88         new NetworkPortalDetectorImpl(test_loader_factory());
89     // Takes ownership of |network_portal_detector_|:
90     chromeos::network_portal_detector::InitializeForTesting(
91         network_portal_detector_);
92     network_portal_detector_->Enable(false /* start_detection */);
93     set_detector(network_portal_detector_->captive_portal_detector_.get());
94     network_portal_notification_controller_ =
95         std::make_unique<NetworkPortalNotificationController>(
96             network_portal_detector_);
97   }
98 
TearDownOnMainThread()99   void TearDownOnMainThread() override {
100     network_portal_notification_controller_.reset();
101   }
102 
LoadTestExtension()103   void LoadTestExtension() {
104     extension_ = LoadExtension(test_data_dir_.AppendASCII("networking_config"));
105   }
106 
RunExtensionTest(const std::string & path)107   bool RunExtensionTest(const std::string& path) {
108     return RunExtensionSubtest("networking_config",
109                                extension_->GetResourceURL(path).spec());
110   }
111 
SimulateCaptivePortal()112   void SimulateCaptivePortal() {
113     network_portal_detector_->StartDetection();
114     content::RunAllPendingInMessageLoop();
115 
116     // Simulate a captive portal reply.
117     CompleteURLFetch(net::OK, 200, nullptr);
118   }
119 
SimulateSuccessfulCaptivePortalAuth()120   void SimulateSuccessfulCaptivePortalAuth() {
121     content::RunAllPendingInMessageLoop();
122     CompleteURLFetch(net::OK, 204, nullptr);
123   }
124 
GetCaptivePortalStatus(const std::string & guid)125   NetworkPortalDetector::CaptivePortalStatus GetCaptivePortalStatus(
126       const std::string& guid) {
127     return network_portal_detector_->GetCaptivePortalState(kWifi1ServiceGUID)
128         .status;
129   }
130 
131  protected:
132   NetworkPortalDetectorImpl* network_portal_detector_;
133   std::unique_ptr<NetworkPortalNotificationController>
134       network_portal_notification_controller_;
135   const extensions::Extension* extension_ = nullptr;
136   std::unique_ptr<NotificationDisplayServiceTester> display_service_;
137 };
138 
IN_PROC_BROWSER_TEST_F(NetworkingConfigTest,ApiAvailability)139 IN_PROC_BROWSER_TEST_F(NetworkingConfigTest, ApiAvailability) {
140   ASSERT_TRUE(RunExtensionSubtest("networking_config", "api_availability.html"))
141       << message_;
142 }
143 
IN_PROC_BROWSER_TEST_F(NetworkingConfigTest,RegisterNetworks)144 IN_PROC_BROWSER_TEST_F(NetworkingConfigTest, RegisterNetworks) {
145   ASSERT_TRUE(
146       RunExtensionSubtest("networking_config", "register_networks.html"))
147       << message_;
148 }
149 
150 // Test the full, positive flow starting with the extension registration and
151 // ending with the captive portal being authenticated.
IN_PROC_BROWSER_TEST_F(NetworkingConfigTest,FullTest)152 IN_PROC_BROWSER_TEST_F(NetworkingConfigTest, FullTest) {
153   LoadTestExtension();
154   // This will cause the extension to register for wifi1.
155   ASSERT_TRUE(RunExtensionTest("full_test.html")) << message_;
156 
157   SimulateCaptivePortal();
158 
159   // Wait until a captive portal notification is displayed and verify that it is
160   // the expected captive portal notification.
161   auto notification = display_service_->GetNotification(
162       NetworkPortalNotificationController::kNotificationId);
163   ASSERT_TRUE(notification);
164 
165   // Simulate the user click which leads to the extension being notified.
166   notification->delegate()->Click(
167       NetworkPortalNotificationController::kUseExtensionButtonIndex,
168       base::nullopt);
169 
170   extensions::ResultCatcher catcher;
171   EXPECT_TRUE(catcher.GetNextResult());
172 
173   // Simulate the captive portal vanishing.
174   SimulateSuccessfulCaptivePortalAuth();
175 
176   ASSERT_EQ(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE,
177             GetCaptivePortalStatus(kWifi1ServiceGUID));
178 }
179