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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
content / browser / network / address_map_linux_browsertest.cc [blame]
// 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 <linux/if.h>
#include <linux/if_addr.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <sys/socket.h>
#include <memory>
#include <unordered_set>
#include <vector>
#include "base/functional/bind.h"
#include "base/posix/unix_domain_socket.h"
#include "base/run_loop.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/test/scoped_feature_list.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/browser/network_service_util.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/content_browser_test.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/sync_call_restrictions.h"
#include "net/base/address_map_linux.h"
#include "net/base/address_tracker_linux_test_util.h"
#include "net/base/features.h"
#include "net/base/network_change_notifier.h"
#include "net/base/network_change_notifier_factory.h"
#include "net/base/network_change_notifier_linux.h"
#include "services/network/public/mojom/network_change_manager.mojom.h"
#include "services/network/public/mojom/network_service.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
namespace {
constexpr unsigned char kAddress0[] = {127, 0, 0, 1};
constexpr unsigned char kAddress1[] = {10, 0, 0, 1};
constexpr unsigned char kAddress2[] = {192, 168, 0, 1};
constexpr unsigned char kAddress3[] = {0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1};
class NCNLinuxMockedNetlinkTestUtil {
public:
static constexpr int kTestInterfaceEth = 1;
static constexpr int kTestInterfaceOther = 2;
static inline const net::IPAddress kEmpty;
static inline const net::IPAddress kAddr0{kAddress0};
static inline const net::IPAddress kAddr1{kAddress1};
static inline const net::IPAddress kAddr2{kAddress2};
static inline const net::IPAddress kAddr3{kAddress3};
NCNLinuxMockedNetlinkTestUtil() = default;
~NCNLinuxMockedNetlinkTestUtil() = default;
std::unique_ptr<net::NetworkChangeNotifierLinux> CreateNCNLinux() {
base::ScopedFD netlink_fd_receiver;
base::CreateSocketPair(&fake_netlink_fd_, &netlink_fd_receiver);
auto ncn_linux =
net::NetworkChangeNotifierLinux::CreateWithSocketForTesting(
{}, std::move(netlink_fd_receiver));
base::ThreadPool::PostTaskAndReply(
FROM_HERE, {base::MayBlock()},
base::BindOnce(
&NCNLinuxMockedNetlinkTestUtil::SimulateAddressTrackerLinuxStart,
base::Unretained(this)),
base::BindOnce(&NCNLinuxMockedNetlinkTestUtil::SetInitialized,
base::Unretained(this)));
return ncn_linux;
}
// This should run on a thread pool thread because it blocks.
// Sets up the AddressMap with kAddr0, and kTestInterfaceEth online.
void SimulateAddressTrackerLinuxStart() {
struct {
struct nlmsghdr header;
struct rtgenmsg msg;
} request = {};
// Receive the RTM_GETADDR request.
std::vector<base::ScopedFD> fds;
ssize_t expected_size = NLMSG_LENGTH(sizeof(request.msg));
EXPECT_EQ(base::UnixDomainSocket::RecvMsg(fake_netlink_fd_.get(), &request,
expected_size, &fds),
expected_size);
EXPECT_TRUE(fds.empty());
EXPECT_EQ(request.header.nlmsg_type, RTM_GETADDR);
// Send a response.
net::test::NetlinkBuffer buffer;
net::test::MakeAddrMessage(RTM_NEWADDR, IFA_F_TEMPORARY, AF_INET,
kTestInterfaceEth, kAddr0, kEmpty, &buffer);
base::UnixDomainSocket::SendMsg(fake_netlink_fd_.get(), buffer.data(),
buffer.size(), {});
// Receive the RTM_GETLINK request.
EXPECT_EQ(base::UnixDomainSocket::RecvMsg(fake_netlink_fd_.get(), &request,
expected_size, &fds),
expected_size);
EXPECT_EQ(request.header.nlmsg_type, RTM_GETLINK);
// Send a response.
buffer.clear();
net::test::MakeLinkMessage(RTM_NEWLINK, IFF_UP | IFF_LOWER_UP | IFF_RUNNING,
kTestInterfaceEth, &buffer);
base::UnixDomainSocket::SendMsg(fake_netlink_fd_.get(), buffer.data(),
buffer.size(), {});
}
void BufferAddAddrMsg(const net::IPAddress address,
int interface = kTestInterfaceEth,
uint8_t flags = IFA_F_TEMPORARY) {
net::test::MakeAddrMessage(RTM_NEWADDR, flags,
address.IsIPv4() ? AF_INET : AF_INET6, interface,
address, kEmpty, &buffer_);
}
void BufferDeleteAddrMsg(const net::IPAddress& address,
int interface = kTestInterfaceEth) {
net::test::MakeAddrMessage(RTM_DELADDR, 0,
address.IsIPv4() ? AF_INET : AF_INET6, interface,
address, kEmpty, &buffer_);
}
void BufferAddLinkMsg(int link) {
net::test::MakeLinkMessage(RTM_NEWLINK, IFF_UP | IFF_LOWER_UP | IFF_RUNNING,
link, &buffer_, /*clear_output=*/false);
}
void BufferDeleteLinkMsg(int link) {
net::test::MakeLinkMessage(RTM_DELLINK, 0, link, &buffer_,
/*clear_output=*/false);
}
void SendBuffer() {
base::UnixDomainSocket::SendMsg(fake_netlink_fd_.get(), buffer_.data(),
buffer_.size(), {});
buffer_.clear();
}
void SetInitialized() {
initialized_ = true;
initialize_run_loop_.Quit();
}
void WaitForInit() {
if (initialized_) {
return;
}
initialize_run_loop_.Run();
}
private:
base::ScopedFD fake_netlink_fd_;
bool initialized_ = false;
base::RunLoop initialize_run_loop_;
net::test::NetlinkBuffer buffer_;
};
class NetworkChangeNotifierLinuxMockedNetlinkFactory
: public net::NetworkChangeNotifierFactory {
public:
std::unique_ptr<net::NetworkChangeNotifier> CreateInstanceWithInitialTypes(
net::NetworkChangeNotifier::ConnectionType /*initial_type*/,
net::NetworkChangeNotifier::ConnectionSubtype /*nitial_subtype*/)
override {
// There should only be one called to this factory function.
DCHECK(!ncn_wrapper_);
ncn_wrapper_ = std::make_unique<NCNLinuxMockedNetlinkTestUtil>();
return ncn_wrapper_->CreateNCNLinux();
}
NCNLinuxMockedNetlinkTestUtil* ncn_wrapper() { return ncn_wrapper_.get(); }
private:
std::unique_ptr<NCNLinuxMockedNetlinkTestUtil> ncn_wrapper_;
};
enum class ExpectedConnectionType {
kNone,
kConnected,
};
class AddressMapLinuxBrowserTest : public ContentBrowserTest {
public:
struct ExpectedCachedInfo {
std::vector<net::IPAddress> should_contain_addresses;
std::vector<net::IPAddress> should_not_contain_addresses;
std::vector<int> should_contain_links;
std::vector<int> should_not_contain_links;
};
void SetUp() override {
scoped_feature_list_.InitAndEnableFeature(
net::features::kAddressTrackerLinuxIsProxied);
ForceOutOfProcessNetworkService();
ncn_mocked_factory_ =
std::make_unique<NetworkChangeNotifierLinuxMockedNetlinkFactory>();
net::NetworkChangeNotifier::SetFactory(ncn_mocked_factory_.get());
ContentBrowserTest::SetUp();
}
void SetUpOnMainThread() override {
mojo::Remote<network::mojom::NetworkChangeManager> network_change_manager;
GetNetworkService()->GetNetworkChangeManager(
network_change_manager.BindNewPipeAndPassReceiver());
mojo::PendingReceiver<network::mojom::NetworkChangeManagerClient>
client_receiver;
network_change_manager->RequestNotifications(
client_receiver.InitWithNewPipeAndPassRemote());
notification_listener_ =
std::make_unique<NetworkChangeNotificationListener>(
std::move(client_receiver));
}
void ExpectCorrectInfoInNetworkService(
ExpectedCachedInfo expected_cached_info) {
const net::AddressMapOwnerLinux* address_map_owner =
net::NetworkChangeNotifier::GetAddressMapOwner();
net::AddressMapOwnerLinux::AddressMap network_service_addr_map;
std::unordered_set<int> network_service_links;
{
mojo::ScopedAllowSyncCallForTesting allow_sync_call;
network_service_test()->GetAddressMapCacheLinux(&network_service_addr_map,
&network_service_links);
}
net::AddressMapOwnerLinux::AddressMap browser_process_addr_map =
address_map_owner->GetAddressMap();
std::unordered_set<int> browser_process_links =
address_map_owner->GetOnlineLinks();
EXPECT_EQ(browser_process_addr_map, network_service_addr_map);
EXPECT_EQ(browser_process_links, network_service_links);
for (const net::IPAddress& address :
expected_cached_info.should_contain_addresses) {
SCOPED_TRACE(testing::Message()
<< "Network service AddressMap should include "
<< address.ToString());
EXPECT_TRUE(network_service_addr_map.contains(address));
}
for (const net::IPAddress& address :
expected_cached_info.should_not_contain_addresses) {
SCOPED_TRACE(testing::Message()
<< "Network service AddressMap should not include "
<< address.ToString());
EXPECT_FALSE(network_service_addr_map.contains(address));
}
for (const int link : expected_cached_info.should_contain_links) {
SCOPED_TRACE(testing::Message()
<< "Network service online links should include " << link);
EXPECT_TRUE(network_service_links.contains(link));
}
for (const int link : expected_cached_info.should_not_contain_links) {
SCOPED_TRACE(testing::Message()
<< "Network service online links should not include "
<< link);
EXPECT_FALSE(network_service_links.contains(link));
}
}
void WaitForNetworkChange(ExpectedConnectionType expected_connection_type) {
notification_listener_->WaitForNetworkChange(expected_connection_type);
}
protected:
std::unique_ptr<NetworkChangeNotifierLinuxMockedNetlinkFactory>
ncn_mocked_factory_;
private:
class NetworkChangeNotificationListener
: public network::mojom::NetworkChangeManagerClient {
public:
explicit NetworkChangeNotificationListener(
mojo::PendingReceiver<network::mojom::NetworkChangeManagerClient>
receiver)
: receiver_(this, std::move(receiver)) {}
void OnInitialConnectionType(network::mojom::ConnectionType type) override {
}
void OnNetworkChanged(network::mojom::ConnectionType type) override {
// NetworkChangeNotifier::NetworkChangeObserver will fire a
// CONNECTION_NONE change right before firing a non-CONNECTION_NONE
// change. So if this is a CONNECTION_NONE event, only continue the test
// if the test is expecting a CONNECTION_NONE event.
// TODO(mpdenton): set timeouts to zero in the network process so tests
// run faster.
if ((expected_connection_type_ == ExpectedConnectionType::kNone ||
type != network::mojom::ConnectionType::CONNECTION_NONE) &&
run_loop_.has_value()) {
run_loop_->Quit();
}
}
void WaitForNetworkChange(ExpectedConnectionType expected_connection_type) {
expected_connection_type_ = expected_connection_type;
run_loop_.emplace();
run_loop_->Run();
run_loop_.reset();
}
private:
mojo::Receiver<network::mojom::NetworkChangeManagerClient> receiver_;
std::optional<base::RunLoop> run_loop_;
ExpectedConnectionType expected_connection_type_;
};
base::test::ScopedFeatureList scoped_feature_list_;
std::unique_ptr<NetworkChangeNotificationListener> notification_listener_;
};
} // namespace
IN_PROC_BROWSER_TEST_F(AddressMapLinuxBrowserTest, CheckInitialMapsMatch) {
if (IsInProcessNetworkService()) {
GTEST_SKIP();
}
ncn_mocked_factory_->ncn_wrapper()->WaitForInit();
ExpectCorrectInfoInNetworkService(
{.should_contain_addresses = {NCNLinuxMockedNetlinkTestUtil::kAddr0},
.should_not_contain_addresses = {},
.should_contain_links =
{NCNLinuxMockedNetlinkTestUtil::kTestInterfaceEth},
.should_not_contain_links = {}});
}
IN_PROC_BROWSER_TEST_F(AddressMapLinuxBrowserTest, CheckAddressMapDiffsApply) {
// Delete kAddr0 from the map.
ncn_mocked_factory_->ncn_wrapper()->BufferDeleteAddrMsg(
NCNLinuxMockedNetlinkTestUtil::kAddr0);
ncn_mocked_factory_->ncn_wrapper()->SendBuffer();
WaitForNetworkChange(ExpectedConnectionType::kNone);
ExpectCorrectInfoInNetworkService(
{.should_contain_addresses = {},
.should_not_contain_addresses = {NCNLinuxMockedNetlinkTestUtil::kAddr0},
.should_contain_links =
{NCNLinuxMockedNetlinkTestUtil::kTestInterfaceEth},
.should_not_contain_links = {}});
// Now add kAddr0 back to the map.
ncn_mocked_factory_->ncn_wrapper()->BufferAddAddrMsg(
NCNLinuxMockedNetlinkTestUtil::kAddr0);
ncn_mocked_factory_->ncn_wrapper()->SendBuffer();
WaitForNetworkChange(ExpectedConnectionType::kConnected);
ExpectCorrectInfoInNetworkService(
{.should_contain_addresses = {NCNLinuxMockedNetlinkTestUtil::kAddr0},
.should_not_contain_addresses = {},
.should_contain_links =
{NCNLinuxMockedNetlinkTestUtil::kTestInterfaceEth},
.should_not_contain_links = {}});
// Now change kAddr0's ifaddrmsg. Use flags = IFA_F_HOMEADDRESS rather than
// flags = IFA_F_TEMPORARY.
ncn_mocked_factory_->ncn_wrapper()->BufferAddAddrMsg(
NCNLinuxMockedNetlinkTestUtil::kAddr0,
NCNLinuxMockedNetlinkTestUtil::kTestInterfaceEth, IFA_F_HOMEADDRESS);
ncn_mocked_factory_->ncn_wrapper()->SendBuffer();
WaitForNetworkChange(ExpectedConnectionType::kConnected);
ExpectCorrectInfoInNetworkService(
{.should_contain_addresses = {NCNLinuxMockedNetlinkTestUtil::kAddr0},
.should_not_contain_addresses = {},
.should_contain_links =
{NCNLinuxMockedNetlinkTestUtil::kTestInterfaceEth},
.should_not_contain_links = {}});
// Add the other addresses, and then delete one of them and delete the
// existing address before sending diffs.
ncn_mocked_factory_->ncn_wrapper()->BufferAddAddrMsg(
NCNLinuxMockedNetlinkTestUtil::kAddr1);
ncn_mocked_factory_->ncn_wrapper()->BufferAddAddrMsg(
NCNLinuxMockedNetlinkTestUtil::kAddr2);
ncn_mocked_factory_->ncn_wrapper()->BufferAddAddrMsg(
NCNLinuxMockedNetlinkTestUtil::kAddr3);
ncn_mocked_factory_->ncn_wrapper()->BufferDeleteAddrMsg(
NCNLinuxMockedNetlinkTestUtil::kAddr2);
ncn_mocked_factory_->ncn_wrapper()->BufferDeleteAddrMsg(
NCNLinuxMockedNetlinkTestUtil::kAddr0);
ncn_mocked_factory_->ncn_wrapper()->SendBuffer();
WaitForNetworkChange(ExpectedConnectionType::kConnected);
ExpectCorrectInfoInNetworkService(
{.should_contain_addresses = {NCNLinuxMockedNetlinkTestUtil::kAddr1,
NCNLinuxMockedNetlinkTestUtil::kAddr3},
.should_not_contain_addresses = {NCNLinuxMockedNetlinkTestUtil::kAddr0,
NCNLinuxMockedNetlinkTestUtil::kAddr2},
.should_contain_links =
{NCNLinuxMockedNetlinkTestUtil::kTestInterfaceEth},
.should_not_contain_links = {}});
}
IN_PROC_BROWSER_TEST_F(AddressMapLinuxBrowserTest, CheckOnlineLinksDiffsApply) {
// Delete the link.
ncn_mocked_factory_->ncn_wrapper()->BufferDeleteLinkMsg(
NCNLinuxMockedNetlinkTestUtil::kTestInterfaceEth);
ncn_mocked_factory_->ncn_wrapper()->SendBuffer();
WaitForNetworkChange(ExpectedConnectionType::kNone);
ExpectCorrectInfoInNetworkService(
{.should_contain_addresses = {NCNLinuxMockedNetlinkTestUtil::kAddr0},
.should_not_contain_addresses = {},
.should_contain_links = {},
.should_not_contain_links = {
NCNLinuxMockedNetlinkTestUtil::kTestInterfaceEth}});
// Add the link back.
ncn_mocked_factory_->ncn_wrapper()->BufferAddLinkMsg(
NCNLinuxMockedNetlinkTestUtil::kTestInterfaceEth);
ncn_mocked_factory_->ncn_wrapper()->SendBuffer();
WaitForNetworkChange(ExpectedConnectionType::kConnected);
ExpectCorrectInfoInNetworkService(
{.should_contain_addresses = {NCNLinuxMockedNetlinkTestUtil::kAddr0},
.should_not_contain_addresses = {},
.should_contain_links =
{NCNLinuxMockedNetlinkTestUtil::kTestInterfaceEth},
.should_not_contain_links = {}});
// Delete link 1, add it back, delete it again. Also add link 2, delete it,
// and add it back again.
ncn_mocked_factory_->ncn_wrapper()->BufferDeleteLinkMsg(
NCNLinuxMockedNetlinkTestUtil::kTestInterfaceEth);
ncn_mocked_factory_->ncn_wrapper()->BufferAddLinkMsg(
NCNLinuxMockedNetlinkTestUtil::kTestInterfaceEth);
ncn_mocked_factory_->ncn_wrapper()->BufferDeleteLinkMsg(
NCNLinuxMockedNetlinkTestUtil::kTestInterfaceEth);
ncn_mocked_factory_->ncn_wrapper()->BufferAddLinkMsg(
NCNLinuxMockedNetlinkTestUtil::kTestInterfaceOther);
ncn_mocked_factory_->ncn_wrapper()->BufferDeleteLinkMsg(
NCNLinuxMockedNetlinkTestUtil::kTestInterfaceOther);
ncn_mocked_factory_->ncn_wrapper()->BufferAddLinkMsg(
NCNLinuxMockedNetlinkTestUtil::kTestInterfaceOther);
ncn_mocked_factory_->ncn_wrapper()->SendBuffer();
// Unconnected because kTestInterfaceOther has no addresses.
WaitForNetworkChange(ExpectedConnectionType::kNone);
ExpectCorrectInfoInNetworkService(
{.should_contain_addresses = {NCNLinuxMockedNetlinkTestUtil::kAddr0},
.should_not_contain_addresses = {},
.should_contain_links =
{NCNLinuxMockedNetlinkTestUtil::kTestInterfaceOther},
.should_not_contain_links = {
NCNLinuxMockedNetlinkTestUtil::kTestInterfaceEth}});
}
IN_PROC_BROWSER_TEST_F(AddressMapLinuxBrowserTest, CheckBothDiffsApply) {
// Delete the link, add another.
ncn_mocked_factory_->ncn_wrapper()->BufferDeleteLinkMsg(
NCNLinuxMockedNetlinkTestUtil::kTestInterfaceEth);
ncn_mocked_factory_->ncn_wrapper()->BufferAddLinkMsg(
NCNLinuxMockedNetlinkTestUtil::kTestInterfaceOther);
// Add some addresses
ncn_mocked_factory_->ncn_wrapper()->BufferAddAddrMsg(
NCNLinuxMockedNetlinkTestUtil::kAddr1,
NCNLinuxMockedNetlinkTestUtil::kTestInterfaceOther);
ncn_mocked_factory_->ncn_wrapper()->BufferAddAddrMsg(
NCNLinuxMockedNetlinkTestUtil::kAddr2);
ncn_mocked_factory_->ncn_wrapper()->BufferAddAddrMsg(
NCNLinuxMockedNetlinkTestUtil::kAddr3);
// Delete some addresses.
ncn_mocked_factory_->ncn_wrapper()->BufferDeleteAddrMsg(
NCNLinuxMockedNetlinkTestUtil::kAddr2);
ncn_mocked_factory_->ncn_wrapper()->BufferDeleteAddrMsg(
NCNLinuxMockedNetlinkTestUtil::kAddr0);
ncn_mocked_factory_->ncn_wrapper()->SendBuffer();
// Connected because kAddr1 is associated with kTestInterfaceOther.
WaitForNetworkChange(ExpectedConnectionType::kConnected);
ExpectCorrectInfoInNetworkService(
{.should_contain_addresses = {NCNLinuxMockedNetlinkTestUtil::kAddr1,
NCNLinuxMockedNetlinkTestUtil::kAddr3},
.should_not_contain_addresses = {NCNLinuxMockedNetlinkTestUtil::kAddr0,
NCNLinuxMockedNetlinkTestUtil::kAddr2},
.should_contain_links =
{NCNLinuxMockedNetlinkTestUtil::kTestInterfaceOther},
.should_not_contain_links = {
NCNLinuxMockedNetlinkTestUtil::kTestInterfaceEth}});
}
} // namespace content