File: sandboxed_network_change_notifier_win_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (240 lines) | stat: -rw-r--r-- 8,681 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "services/network/public/mojom/network_service.mojom.h"

// clang-format off
#include <windows.h>  // Must be in front of other Windows header files.
#include <initguid.h>  // Must be in front of devpkey.h.
// Must be in front of Windows includes because they define LogSeverity and this
// breaks gmock.
#include "testing/gmock/include/gmock/gmock.h"
// clang-format on

#include <cfgmgr32.h>
#include <devpkey.h>
#include <newdev.h>
#include <ntddser.h>
#include <setupapi.h>
#include <shlobj.h>
#include <stdint.h>

// LogSeverity is both a macro in setupapi.h and in absl, which is used
// indirectly within InProcessBrowserTest.
#undef LogSeverity

#include <optional>

#include "base/base_paths_win.h"
#include "base/files/file_path.h"
#include "base/functional/callback_helpers.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "base/win/scoped_devinfo.h"
#include "base/win/win_util.h"
#include "base/win/windows_version.h"
#include "build/build_config.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_test.h"
#include "net/base/features.h"
#include "sandbox/policy/features.h"
#include "services/network/public/mojom/network_change_manager.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace content {

namespace {

void UninstallAllMatchingDevices(base::win::ScopedDevInfo dev_info) {
  SP_DEVINFO_DATA device_info_data = {};
  device_info_data.cbSize = sizeof(SP_DEVINFO_DATA);

  DWORD member_index = 0;
  while (::SetupDiEnumDeviceInfo(dev_info.get(), member_index,
                                 &device_info_data)) {
    // Explicitly continue on failure, to make sure that all devices are
    // correctly removed.
    std::ignore = ::DiUninstallDevice(/*hwndParent=*/nullptr, dev_info.get(),
                                      &device_info_data, /*Flags=*/0,
                                      /*NeedReboot=*/nullptr);
    member_index++;
  }
}

std::optional<base::ScopedClosureRunner> InstallAdapter(
    const base::FilePath& inf,
    const std::wstring hwid) {
  GUID guid;
  wchar_t className[MAX_CLASS_NAME_LEN];

  if (!::SetupDiGetINFClass(inf.value().c_str(), &guid, className,
                            MAX_CLASS_NAME_LEN, 0)) {
    PLOG(ERROR) << "Unable to create SetupDiGetINFClass.";
    return std::nullopt;
  }

  base::win::ScopedDevInfo dev_info(
      ::SetupDiCreateDeviceInfoList(&guid, nullptr));
  if (!dev_info.is_valid()) {
    PLOG(ERROR) << "Unable to call SetupDiCreateDeviceInfoList.";
    return std::nullopt;
  }

  SP_DEVINFO_DATA deviceInfoData = {};
  deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

  if (!::SetupDiCreateDeviceInfo(dev_info.get(), className, &guid, nullptr,
                                 nullptr, DICD_GENERATE_ID, &deviceInfoData)) {
    PLOG(ERROR) << "Unable to call SetupDiCreateDeviceInfo";
    return std::nullopt;
  }

  if (!::SetupDiSetDeviceRegistryProperty(
          dev_info.get(), &deviceInfoData, SPDRP_HARDWAREID,
          reinterpret_cast<const BYTE*>(hwid.c_str()),
          (hwid.length() + 1) * sizeof(wchar_t))) {
    PLOG(ERROR) << "Unable to call SetupDiSetDeviceRegistryProperty.";
    return std::nullopt;
  }

  if (!::SetupDiCallClassInstaller(DIF_REGISTERDEVICE, dev_info.get(),
                                   &deviceInfoData)) {
    PLOG(ERROR) << "Unable to call SetupDiCallClassInstaller.";
    return std::nullopt;
  }

  BOOL reboot_required = FALSE;
  if (!::UpdateDriverForPlugAndPlayDevices(
          nullptr, hwid.c_str(), inf.value().c_str(), 0, &reboot_required)) {
    PLOG(ERROR) << "Unable to call UpdateDriverForPlugAndPlayDevices.";
    return std::nullopt;
  }

  return base::ScopedClosureRunner(
      base::BindOnce(&UninstallAllMatchingDevices, std::move(dev_info)));
}

class MockNetworkChangeManagerClient
    : public network::mojom::NetworkChangeManagerClient {
 public:
  MockNetworkChangeManagerClient(
      network::mojom::NetworkChangeManager* network_change_manager) {
    mojo::PendingRemote<network::mojom::NetworkChangeManagerClient>
        client_remote;
    receiver_.Bind(client_remote.InitWithNewPipeAndPassReceiver());
    network_change_manager->RequestNotifications(std::move(client_remote));
  }

  MockNetworkChangeManagerClient(const MockNetworkChangeManagerClient&) =
      delete;
  MockNetworkChangeManagerClient& operator=(
      const MockNetworkChangeManagerClient&) = delete;

  ~MockNetworkChangeManagerClient() override = default;

  // NetworkChangeManagerClient implementation:
  MOCK_METHOD(void,
              OnInitialConnectionType,
              (network::mojom::ConnectionType type),
              (override));
  MOCK_METHOD(void,
              OnNetworkChanged,
              (network::mojom::ConnectionType type),
              (override));

 private:
  mojo::Receiver<network::mojom::NetworkChangeManagerClient> receiver_{this};
};

}  // namespace

class SandboxedNetworkChangeNotifierBrowserTest
    : public InProcessBrowserTest,
      public ::testing::WithParamInterface</*sandboxed=*/bool> {
 public:
  SandboxedNetworkChangeNotifierBrowserTest() {
    if (GetParam()) {
      scoped_feature_list_.InitWithFeatures(
          {sandbox::policy::features::kNetworkServiceSandbox,
           // When running inside the sandbox, the GetNetworkConnectivityHint
           // API must be used.
           net::features::kEnableGetNetworkConnectivityHintAPI},
          {features::kNetworkServiceInProcess});
    } else {
      scoped_feature_list_.InitWithFeatures(
          {}, {features::kNetworkServiceInProcess,
               sandbox::policy::features::kNetworkServiceSandbox});
    }
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

// Test that dynamically adds a new adapter to the host, and verifies that a
// network change notification is sent from the network service to the browser
// process.
// The network service is able to see these network adapter changes, as it is
// created with the LPAC "internetClient" capability. See
// https://learn.microsoft.com/en-us/windows/uwp/packaging/app-capability-declarations
IN_PROC_BROWSER_TEST_P(SandboxedNetworkChangeNotifierBrowserTest,
                       AddNetworkAdapter) {
  if (!::IsUserAnAdmin()) {
    GTEST_SKIP() << "This test requires running elevated.";
  }
#if defined(ARCH_CPU_X86)
  if (!base::win::OSInfo::GetInstance()->IsWowDisabled()) {
    GTEST_SKIP()
        << "SetupDiCallClassInstaller can't be called from a 32 bit app"
        << " running in a 64 bit environment";
  }
#endif  // defined(ARCH_CPU_X86)

  mojo::Remote<network::mojom::NetworkChangeManager> network_change_manager;
  GetNetworkService()->GetNetworkChangeManager(
      network_change_manager.BindNewPipeAndPassReceiver());

  ::testing::StrictMock<MockNetworkChangeManagerClient> mock(
      network_change_manager.get());
  ::testing::InSequence order;

  // First obtain the initial connection type. Before manipulating any network
  // interfaces.
  base::test::TestFuture<network::mojom::ConnectionType> connection_future;
  EXPECT_CALL(mock, OnInitialConnectionType)
      .WillOnce(base::test::InvokeFuture(connection_future));
  network::mojom::ConnectionType connection = connection_future.Get();

  // NetworkChangeManager sends two notifications, the first is always
  // CONNECTION_NONE, followed by the actual ConnectionType. See
  // `network_change_manager.mojom`.
  base::RunLoop run_loop;
  EXPECT_CALL(
      mock, OnNetworkChanged(network::mojom::ConnectionType::CONNECTION_NONE));
  EXPECT_CALL(mock, OnNetworkChanged(connection))
      .WillOnce(base::test::RunClosure(run_loop.QuitClosure()));

  // Install a new network card.
  base::FilePath dir_windows;
  ASSERT_TRUE(base::PathService::Get(base::DIR_WINDOWS, &dir_windows));
  auto inst = InstallAdapter(
      dir_windows.AppendASCII("Inf").AppendASCII("netloop.inf"), L"*MSLOOP");
  ASSERT_TRUE(inst);

  run_loop.Run();
}

INSTANTIATE_TEST_SUITE_P(,
                         SandboxedNetworkChangeNotifierBrowserTest,
                         ::testing::Bool(),
                         [](const auto& info) {
                           return info.param ? "Sandboxed" : "Unsandboxed";
                         });
}  // namespace content