Revert of Move fetching logic out of ApplicationManager, eliminate url mappings....
[chromium-blink-merge.git] / net / base / address_tracker_linux_unittest.cc
blob694c091de37d6526ac2d66fdfa13ce6920536688
1 // Copyright (c) 2012 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.
5 #include "base/memory/scoped_ptr.h"
6 #include "net/base/address_tracker_linux.h"
8 #include <linux/if.h>
10 #include <vector>
12 #include "base/bind.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 #ifndef IFA_F_HOMEADDRESS
16 #define IFA_F_HOMEADDRESS 0x10
17 #endif
19 namespace net {
20 namespace internal {
21 namespace {
23 const int kTestInterfaceEth = 1;
24 const int kTestInterfaceWifi = 2;
25 const int kTestInterfaceTun = 123;
26 const int kTestInterfaceAp = 456;
28 const char kIgnoredInterfaceName[] = "uap0";
30 char* TestGetInterfaceName(int interface_index, char* buf) {
31 if (interface_index == kTestInterfaceEth) {
32 snprintf(buf, IFNAMSIZ, "%s", "eth0");
33 } else if (interface_index == kTestInterfaceTun) {
34 snprintf(buf, IFNAMSIZ, "%s", "tun0");
35 } else if (interface_index == kTestInterfaceAp) {
36 snprintf(buf, IFNAMSIZ, "%s", kIgnoredInterfaceName);
37 } else {
38 snprintf(buf, IFNAMSIZ, "%s", "");
40 return buf;
43 } // namespace
45 typedef std::vector<char> Buffer;
47 class AddressTrackerLinuxTest : public testing::Test {
48 protected:
49 AddressTrackerLinuxTest() {}
51 void InitializeAddressTracker(bool tracking) {
52 if (tracking) {
53 tracker_.reset(new AddressTrackerLinux(
54 base::Bind(&base::DoNothing), base::Bind(&base::DoNothing),
55 base::Bind(&base::DoNothing), ignored_interfaces_));
56 } else {
57 tracker_.reset(new AddressTrackerLinux());
59 original_get_interface_name_ = tracker_->get_interface_name_;
60 tracker_->get_interface_name_ = TestGetInterfaceName;
63 bool HandleAddressMessage(const Buffer& buf) {
64 Buffer writable_buf = buf;
65 bool address_changed = false;
66 bool link_changed = false;
67 bool tunnel_changed = false;
68 tracker_->HandleMessage(&writable_buf[0], buf.size(),
69 &address_changed, &link_changed, &tunnel_changed);
70 EXPECT_FALSE(link_changed);
71 return address_changed;
74 bool HandleLinkMessage(const Buffer& buf) {
75 Buffer writable_buf = buf;
76 bool address_changed = false;
77 bool link_changed = false;
78 bool tunnel_changed = false;
79 tracker_->HandleMessage(&writable_buf[0], buf.size(),
80 &address_changed, &link_changed, &tunnel_changed);
81 EXPECT_FALSE(address_changed);
82 return link_changed;
85 bool HandleTunnelMessage(const Buffer& buf) {
86 Buffer writable_buf = buf;
87 bool address_changed = false;
88 bool link_changed = false;
89 bool tunnel_changed = false;
90 tracker_->HandleMessage(&writable_buf[0], buf.size(),
91 &address_changed, &link_changed, &tunnel_changed);
92 EXPECT_FALSE(address_changed);
93 return tunnel_changed;
96 AddressTrackerLinux::AddressMap GetAddressMap() {
97 return tracker_->GetAddressMap();
100 const base::hash_set<int> GetOnlineLinks() const {
101 return tracker_->GetOnlineLinks();
104 void IgnoreInterface(const std::string& interface_name) {
105 ignored_interfaces_.insert(interface_name);
108 base::hash_set<std::string> ignored_interfaces_;
109 scoped_ptr<AddressTrackerLinux> tracker_;
110 AddressTrackerLinux::GetInterfaceNameFunction original_get_interface_name_;
113 namespace {
115 class NetlinkMessage {
116 public:
117 explicit NetlinkMessage(uint16_t type) : buffer_(NLMSG_HDRLEN) {
118 header()->nlmsg_type = type;
119 Align();
122 void AddPayload(const void* data, size_t length) {
123 CHECK_EQ(static_cast<size_t>(NLMSG_HDRLEN),
124 buffer_.size()) << "Payload must be added first";
125 Append(data, length);
126 Align();
129 void AddAttribute(uint16_t type, const void* data, size_t length) {
130 struct nlattr attr;
131 attr.nla_len = NLA_HDRLEN + length;
132 attr.nla_type = type;
133 Append(&attr, sizeof(attr));
134 Align();
135 Append(data, length);
136 Align();
139 void AppendTo(Buffer* output) const {
140 CHECK_EQ(NLMSG_ALIGN(output->size()), output->size());
141 output->reserve(output->size() + NLMSG_LENGTH(buffer_.size()));
142 output->insert(output->end(), buffer_.begin(), buffer_.end());
145 private:
146 void Append(const void* data, size_t length) {
147 const char* chardata = reinterpret_cast<const char*>(data);
148 buffer_.insert(buffer_.end(), chardata, chardata + length);
151 void Align() {
152 header()->nlmsg_len = buffer_.size();
153 buffer_.insert(buffer_.end(), NLMSG_ALIGN(buffer_.size()) - buffer_.size(),
155 CHECK(NLMSG_OK(header(), buffer_.size()));
158 struct nlmsghdr* header() {
159 return reinterpret_cast<struct nlmsghdr*>(&buffer_[0]);
162 Buffer buffer_;
165 #define INFINITY_LIFE_TIME 0xFFFFFFFF
167 void MakeAddrMessageWithCacheInfo(uint16_t type,
168 uint8_t flags,
169 uint8_t family,
170 int index,
171 const IPAddressNumber& address,
172 const IPAddressNumber& local,
173 uint32_t preferred_lifetime,
174 Buffer* output) {
175 NetlinkMessage nlmsg(type);
176 struct ifaddrmsg msg = {};
177 msg.ifa_family = family;
178 msg.ifa_flags = flags;
179 msg.ifa_index = index;
180 nlmsg.AddPayload(&msg, sizeof(msg));
181 if (address.size())
182 nlmsg.AddAttribute(IFA_ADDRESS, &address[0], address.size());
183 if (local.size())
184 nlmsg.AddAttribute(IFA_LOCAL, &local[0], local.size());
185 struct ifa_cacheinfo cache_info = {};
186 cache_info.ifa_prefered = preferred_lifetime;
187 cache_info.ifa_valid = INFINITY_LIFE_TIME;
188 nlmsg.AddAttribute(IFA_CACHEINFO, &cache_info, sizeof(cache_info));
189 nlmsg.AppendTo(output);
192 void MakeAddrMessage(uint16_t type,
193 uint8_t flags,
194 uint8_t family,
195 int index,
196 const IPAddressNumber& address,
197 const IPAddressNumber& local,
198 Buffer* output) {
199 MakeAddrMessageWithCacheInfo(type, flags, family, index, address, local,
200 INFINITY_LIFE_TIME, output);
203 void MakeLinkMessage(uint16_t type,
204 uint32_t flags,
205 uint32_t index,
206 Buffer* output) {
207 NetlinkMessage nlmsg(type);
208 struct ifinfomsg msg = {};
209 msg.ifi_index = index;
210 msg.ifi_flags = flags;
211 nlmsg.AddPayload(&msg, sizeof(msg));
212 output->clear();
213 nlmsg.AppendTo(output);
216 // Creates a netlink message generated by wireless_send_event. These events
217 // should be ignored.
218 void MakeWirelessLinkMessage(uint16_t type,
219 uint32_t flags,
220 uint32_t index,
221 Buffer* output) {
222 NetlinkMessage nlmsg(type);
223 struct ifinfomsg msg = {};
224 msg.ifi_index = index;
225 msg.ifi_flags = flags;
226 msg.ifi_change = 0;
227 nlmsg.AddPayload(&msg, sizeof(msg));
228 char data[8] = {0};
229 nlmsg.AddAttribute(IFLA_WIRELESS, data, sizeof(data));
230 output->clear();
231 nlmsg.AppendTo(output);
234 const unsigned char kAddress0[] = { 127, 0, 0, 1 };
235 const unsigned char kAddress1[] = { 10, 0, 0, 1 };
236 const unsigned char kAddress2[] = { 192, 168, 0, 1 };
237 const unsigned char kAddress3[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
238 0, 0, 0, 1 };
240 TEST_F(AddressTrackerLinuxTest, NewAddress) {
241 InitializeAddressTracker(true);
243 const IPAddressNumber kEmpty;
244 const IPAddressNumber kAddr0(kAddress0, kAddress0 + arraysize(kAddress0));
245 const IPAddressNumber kAddr1(kAddress1, kAddress1 + arraysize(kAddress1));
246 const IPAddressNumber kAddr2(kAddress2, kAddress2 + arraysize(kAddress2));
247 const IPAddressNumber kAddr3(kAddress3, kAddress3 + arraysize(kAddress3));
249 Buffer buffer;
250 MakeAddrMessage(RTM_NEWADDR, IFA_F_TEMPORARY, AF_INET, kTestInterfaceEth,
251 kAddr0, kEmpty, &buffer);
252 EXPECT_TRUE(HandleAddressMessage(buffer));
253 AddressTrackerLinux::AddressMap map = GetAddressMap();
254 EXPECT_EQ(1u, map.size());
255 EXPECT_EQ(1u, map.count(kAddr0));
256 EXPECT_EQ(IFA_F_TEMPORARY, map[kAddr0].ifa_flags);
258 buffer.clear();
259 MakeAddrMessage(RTM_NEWADDR, IFA_F_HOMEADDRESS, AF_INET, kTestInterfaceEth,
260 kAddr1, kAddr2, &buffer);
261 EXPECT_TRUE(HandleAddressMessage(buffer));
262 map = GetAddressMap();
263 EXPECT_EQ(2u, map.size());
264 EXPECT_EQ(1u, map.count(kAddr0));
265 EXPECT_EQ(1u, map.count(kAddr2));
266 EXPECT_EQ(IFA_F_HOMEADDRESS, map[kAddr2].ifa_flags);
268 buffer.clear();
269 MakeAddrMessage(RTM_NEWADDR, 0, AF_INET6, kTestInterfaceEth, kEmpty, kAddr3,
270 &buffer);
271 EXPECT_TRUE(HandleAddressMessage(buffer));
272 map = GetAddressMap();
273 EXPECT_EQ(3u, map.size());
274 EXPECT_EQ(1u, map.count(kAddr3));
277 TEST_F(AddressTrackerLinuxTest, NewAddressChange) {
278 InitializeAddressTracker(true);
280 const IPAddressNumber kEmpty;
281 const IPAddressNumber kAddr0(kAddress0, kAddress0 + arraysize(kAddress0));
283 Buffer buffer;
284 MakeAddrMessage(RTM_NEWADDR, IFA_F_TEMPORARY, AF_INET, kTestInterfaceEth,
285 kAddr0, kEmpty, &buffer);
286 EXPECT_TRUE(HandleAddressMessage(buffer));
287 AddressTrackerLinux::AddressMap map = GetAddressMap();
288 EXPECT_EQ(1u, map.size());
289 EXPECT_EQ(1u, map.count(kAddr0));
290 EXPECT_EQ(IFA_F_TEMPORARY, map[kAddr0].ifa_flags);
292 buffer.clear();
293 MakeAddrMessage(RTM_NEWADDR, IFA_F_HOMEADDRESS, AF_INET, kTestInterfaceEth,
294 kAddr0, kEmpty, &buffer);
295 EXPECT_TRUE(HandleAddressMessage(buffer));
296 map = GetAddressMap();
297 EXPECT_EQ(1u, map.size());
298 EXPECT_EQ(1u, map.count(kAddr0));
299 EXPECT_EQ(IFA_F_HOMEADDRESS, map[kAddr0].ifa_flags);
301 // Both messages in one buffer.
302 buffer.clear();
303 MakeAddrMessage(RTM_NEWADDR, IFA_F_TEMPORARY, AF_INET, kTestInterfaceEth,
304 kAddr0, kEmpty, &buffer);
305 MakeAddrMessage(RTM_NEWADDR, IFA_F_HOMEADDRESS, AF_INET, kTestInterfaceEth,
306 kAddr0, kEmpty, &buffer);
307 EXPECT_TRUE(HandleAddressMessage(buffer));
308 map = GetAddressMap();
309 EXPECT_EQ(1u, map.size());
310 EXPECT_EQ(IFA_F_HOMEADDRESS, map[kAddr0].ifa_flags);
313 TEST_F(AddressTrackerLinuxTest, NewAddressDuplicate) {
314 InitializeAddressTracker(true);
316 const IPAddressNumber kAddr0(kAddress0, kAddress0 + arraysize(kAddress0));
318 Buffer buffer;
319 MakeAddrMessage(RTM_NEWADDR, IFA_F_TEMPORARY, AF_INET, kTestInterfaceEth,
320 kAddr0, kAddr0, &buffer);
321 EXPECT_TRUE(HandleAddressMessage(buffer));
322 AddressTrackerLinux::AddressMap map = GetAddressMap();
323 EXPECT_EQ(1u, map.size());
324 EXPECT_EQ(1u, map.count(kAddr0));
325 EXPECT_EQ(IFA_F_TEMPORARY, map[kAddr0].ifa_flags);
327 EXPECT_FALSE(HandleAddressMessage(buffer));
328 map = GetAddressMap();
329 EXPECT_EQ(1u, map.size());
330 EXPECT_EQ(IFA_F_TEMPORARY, map[kAddr0].ifa_flags);
333 TEST_F(AddressTrackerLinuxTest, DeleteAddress) {
334 InitializeAddressTracker(true);
336 const IPAddressNumber kEmpty;
337 const IPAddressNumber kAddr0(kAddress0, kAddress0 + arraysize(kAddress0));
338 const IPAddressNumber kAddr1(kAddress1, kAddress1 + arraysize(kAddress1));
339 const IPAddressNumber kAddr2(kAddress2, kAddress2 + arraysize(kAddress2));
341 Buffer buffer;
342 MakeAddrMessage(RTM_NEWADDR, 0, AF_INET, kTestInterfaceEth, kAddr0, kEmpty,
343 &buffer);
344 MakeAddrMessage(RTM_NEWADDR, 0, AF_INET, kTestInterfaceEth, kAddr1, kAddr2,
345 &buffer);
346 EXPECT_TRUE(HandleAddressMessage(buffer));
347 AddressTrackerLinux::AddressMap map = GetAddressMap();
348 EXPECT_EQ(2u, map.size());
350 buffer.clear();
351 MakeAddrMessage(RTM_DELADDR, 0, AF_INET, kTestInterfaceEth, kEmpty, kAddr0,
352 &buffer);
353 EXPECT_TRUE(HandleAddressMessage(buffer));
354 map = GetAddressMap();
355 EXPECT_EQ(1u, map.size());
356 EXPECT_EQ(0u, map.count(kAddr0));
357 EXPECT_EQ(1u, map.count(kAddr2));
359 buffer.clear();
360 MakeAddrMessage(RTM_DELADDR, 0, AF_INET, kTestInterfaceEth, kAddr2, kAddr1,
361 &buffer);
362 // kAddr1 does not exist in the map.
363 EXPECT_FALSE(HandleAddressMessage(buffer));
364 map = GetAddressMap();
365 EXPECT_EQ(1u, map.size());
367 buffer.clear();
368 MakeAddrMessage(RTM_DELADDR, 0, AF_INET, kTestInterfaceEth, kAddr2, kEmpty,
369 &buffer);
370 EXPECT_TRUE(HandleAddressMessage(buffer));
371 map = GetAddressMap();
372 EXPECT_EQ(0u, map.size());
375 TEST_F(AddressTrackerLinuxTest, DeprecatedLifetime) {
376 InitializeAddressTracker(true);
378 const IPAddressNumber kEmpty;
379 const IPAddressNumber kAddr3(kAddress3, kAddress3 + arraysize(kAddress3));
381 Buffer buffer;
382 MakeAddrMessage(RTM_NEWADDR, 0, AF_INET6, kTestInterfaceEth, kEmpty, kAddr3,
383 &buffer);
384 EXPECT_TRUE(HandleAddressMessage(buffer));
385 AddressTrackerLinux::AddressMap map = GetAddressMap();
386 EXPECT_EQ(1u, map.size());
387 EXPECT_EQ(1u, map.count(kAddr3));
388 EXPECT_EQ(0, map[kAddr3].ifa_flags);
390 // Verify 0 preferred lifetime implies deprecated.
391 buffer.clear();
392 MakeAddrMessageWithCacheInfo(RTM_NEWADDR, 0, AF_INET6, kTestInterfaceEth,
393 kEmpty, kAddr3, 0, &buffer);
394 EXPECT_TRUE(HandleAddressMessage(buffer));
395 map = GetAddressMap();
396 EXPECT_EQ(1u, map.size());
397 EXPECT_EQ(IFA_F_DEPRECATED, map[kAddr3].ifa_flags);
399 // Verify properly flagged message doesn't imply change.
400 buffer.clear();
401 MakeAddrMessageWithCacheInfo(RTM_NEWADDR, IFA_F_DEPRECATED, AF_INET6,
402 kTestInterfaceEth, kEmpty, kAddr3, 0, &buffer);
403 EXPECT_FALSE(HandleAddressMessage(buffer));
404 map = GetAddressMap();
405 EXPECT_EQ(1u, map.size());
406 EXPECT_EQ(IFA_F_DEPRECATED, map[kAddr3].ifa_flags);
408 // Verify implied deprecated doesn't imply change.
409 buffer.clear();
410 MakeAddrMessageWithCacheInfo(RTM_NEWADDR, 0, AF_INET6, kTestInterfaceEth,
411 kEmpty, kAddr3, 0, &buffer);
412 EXPECT_FALSE(HandleAddressMessage(buffer));
413 map = GetAddressMap();
414 EXPECT_EQ(1u, map.size());
415 EXPECT_EQ(IFA_F_DEPRECATED, map[kAddr3].ifa_flags);
418 TEST_F(AddressTrackerLinuxTest, IgnoredMessage) {
419 InitializeAddressTracker(true);
421 const IPAddressNumber kEmpty;
422 const IPAddressNumber kAddr0(kAddress0, kAddress0 + arraysize(kAddress0));
423 const IPAddressNumber kAddr3(kAddress3, kAddress3 + arraysize(kAddress3));
425 Buffer buffer;
426 // Ignored family.
427 MakeAddrMessage(RTM_NEWADDR, 0, AF_UNSPEC, kTestInterfaceEth, kAddr3, kAddr0,
428 &buffer);
429 // No address.
430 MakeAddrMessage(RTM_NEWADDR, 0, AF_INET, kTestInterfaceEth, kEmpty, kEmpty,
431 &buffer);
432 // Ignored type.
433 MakeAddrMessage(RTM_DELROUTE, 0, AF_INET6, kTestInterfaceEth, kAddr3, kEmpty,
434 &buffer);
435 EXPECT_FALSE(HandleAddressMessage(buffer));
436 EXPECT_TRUE(GetAddressMap().empty());
438 // Valid message after ignored messages.
439 NetlinkMessage nlmsg(RTM_NEWADDR);
440 struct ifaddrmsg msg = {};
441 msg.ifa_family = AF_INET;
442 nlmsg.AddPayload(&msg, sizeof(msg));
443 // Ignored attribute.
444 struct ifa_cacheinfo cache_info = {};
445 nlmsg.AddAttribute(IFA_CACHEINFO, &cache_info, sizeof(cache_info));
446 nlmsg.AddAttribute(IFA_ADDRESS, &kAddr0[0], kAddr0.size());
447 nlmsg.AppendTo(&buffer);
449 EXPECT_TRUE(HandleAddressMessage(buffer));
450 EXPECT_EQ(1u, GetAddressMap().size());
453 TEST_F(AddressTrackerLinuxTest, AddInterface) {
454 InitializeAddressTracker(true);
456 Buffer buffer;
458 // Ignores loopback.
459 MakeLinkMessage(RTM_NEWLINK,
460 IFF_LOOPBACK | IFF_UP | IFF_LOWER_UP | IFF_RUNNING,
461 kTestInterfaceEth, &buffer);
462 EXPECT_FALSE(HandleLinkMessage(buffer));
463 EXPECT_TRUE(GetOnlineLinks().empty());
465 // Ignores not IFF_LOWER_UP.
466 MakeLinkMessage(RTM_NEWLINK, IFF_UP | IFF_RUNNING, kTestInterfaceEth,
467 &buffer);
468 EXPECT_FALSE(HandleLinkMessage(buffer));
469 EXPECT_TRUE(GetOnlineLinks().empty());
471 // Ignores deletion.
472 MakeLinkMessage(RTM_DELLINK, IFF_UP | IFF_LOWER_UP | IFF_RUNNING,
473 kTestInterfaceEth, &buffer);
474 EXPECT_FALSE(HandleLinkMessage(buffer));
475 EXPECT_TRUE(GetOnlineLinks().empty());
477 // Verify success.
478 MakeLinkMessage(RTM_NEWLINK, IFF_UP | IFF_LOWER_UP | IFF_RUNNING,
479 kTestInterfaceEth, &buffer);
480 EXPECT_TRUE(HandleLinkMessage(buffer));
481 EXPECT_EQ(1u, GetOnlineLinks().count(kTestInterfaceEth));
482 EXPECT_EQ(1u, GetOnlineLinks().size());
484 // Ignores redundant enables.
485 MakeLinkMessage(RTM_NEWLINK, IFF_UP | IFF_LOWER_UP | IFF_RUNNING,
486 kTestInterfaceEth, &buffer);
487 EXPECT_FALSE(HandleLinkMessage(buffer));
488 EXPECT_EQ(1u, GetOnlineLinks().count(kTestInterfaceEth));
489 EXPECT_EQ(1u, GetOnlineLinks().size());
491 // Ignores messages from wireless_send_event.
492 MakeWirelessLinkMessage(RTM_NEWLINK, IFF_UP | IFF_LOWER_UP | IFF_RUNNING,
493 kTestInterfaceWifi, &buffer);
494 EXPECT_FALSE(HandleLinkMessage(buffer));
495 EXPECT_EQ(0u, GetOnlineLinks().count(kTestInterfaceWifi));
496 EXPECT_EQ(1u, GetOnlineLinks().size());
498 // Verify adding another online device (e.g. VPN) is considered a change.
499 MakeLinkMessage(RTM_NEWLINK, IFF_UP | IFF_LOWER_UP | IFF_RUNNING, 2, &buffer);
500 EXPECT_TRUE(HandleLinkMessage(buffer));
501 EXPECT_EQ(1u, GetOnlineLinks().count(kTestInterfaceEth));
502 EXPECT_EQ(1u, GetOnlineLinks().count(2));
503 EXPECT_EQ(2u, GetOnlineLinks().size());
506 TEST_F(AddressTrackerLinuxTest, RemoveInterface) {
507 InitializeAddressTracker(true);
509 Buffer buffer;
511 // Should disappear when not IFF_LOWER_UP.
512 MakeLinkMessage(RTM_NEWLINK, IFF_UP | IFF_LOWER_UP | IFF_RUNNING,
513 kTestInterfaceEth, &buffer);
514 EXPECT_TRUE(HandleLinkMessage(buffer));
515 EXPECT_FALSE(GetOnlineLinks().empty());
516 MakeLinkMessage(RTM_NEWLINK, IFF_UP | IFF_RUNNING, kTestInterfaceEth,
517 &buffer);
518 EXPECT_TRUE(HandleLinkMessage(buffer));
519 EXPECT_TRUE(GetOnlineLinks().empty());
521 // Ignores redundant disables.
522 MakeLinkMessage(RTM_NEWLINK, IFF_UP | IFF_RUNNING, kTestInterfaceEth,
523 &buffer);
524 EXPECT_FALSE(HandleLinkMessage(buffer));
525 EXPECT_TRUE(GetOnlineLinks().empty());
527 // Ignores deleting down interfaces.
528 MakeLinkMessage(RTM_DELLINK, IFF_UP | IFF_RUNNING, kTestInterfaceEth,
529 &buffer);
530 EXPECT_FALSE(HandleLinkMessage(buffer));
531 EXPECT_TRUE(GetOnlineLinks().empty());
533 // Should disappear when deleted.
534 MakeLinkMessage(RTM_NEWLINK, IFF_UP | IFF_LOWER_UP | IFF_RUNNING,
535 kTestInterfaceEth, &buffer);
536 EXPECT_TRUE(HandleLinkMessage(buffer));
537 EXPECT_FALSE(GetOnlineLinks().empty());
538 MakeLinkMessage(RTM_DELLINK, IFF_UP | IFF_LOWER_UP | IFF_RUNNING,
539 kTestInterfaceEth, &buffer);
540 EXPECT_TRUE(HandleLinkMessage(buffer));
541 EXPECT_TRUE(GetOnlineLinks().empty());
543 // Ignores messages from wireless_send_event.
544 MakeLinkMessage(RTM_NEWLINK, IFF_UP | IFF_LOWER_UP | IFF_RUNNING,
545 kTestInterfaceWifi, &buffer);
546 EXPECT_TRUE(HandleLinkMessage(buffer));
547 EXPECT_FALSE(GetOnlineLinks().empty());
548 MakeWirelessLinkMessage(RTM_NEWLINK, IFF_UP | IFF_LOWER_UP,
549 kTestInterfaceWifi, &buffer);
550 EXPECT_FALSE(HandleLinkMessage(buffer));
551 EXPECT_FALSE(GetOnlineLinks().empty());
552 MakeLinkMessage(RTM_NEWLINK, IFF_UP | IFF_RUNNING, kTestInterfaceWifi,
553 &buffer);
554 EXPECT_TRUE(HandleLinkMessage(buffer));
555 EXPECT_TRUE(GetOnlineLinks().empty());
558 TEST_F(AddressTrackerLinuxTest, IgnoreInterface) {
559 IgnoreInterface(kIgnoredInterfaceName);
560 InitializeAddressTracker(true);
562 Buffer buffer;
563 const IPAddressNumber kEmpty;
564 const IPAddressNumber kAddr0(kAddress0, kAddress0 + arraysize(kAddress0));
566 // Verify online links and address map has been not been updated
567 MakeAddrMessage(RTM_NEWADDR, IFA_F_TEMPORARY, AF_INET, kTestInterfaceAp,
568 kAddr0, kEmpty, &buffer);
569 EXPECT_FALSE(HandleAddressMessage(buffer));
570 AddressTrackerLinux::AddressMap map = GetAddressMap();
571 EXPECT_EQ(0u, map.size());
572 EXPECT_EQ(0u, map.count(kAddr0));
573 MakeLinkMessage(RTM_NEWLINK, IFF_UP | IFF_LOWER_UP | IFF_RUNNING,
574 kTestInterfaceAp, &buffer);
575 EXPECT_FALSE(HandleLinkMessage(buffer));
576 EXPECT_EQ(0u, GetOnlineLinks().count(kTestInterfaceAp));
577 EXPECT_EQ(0u, GetOnlineLinks().size());
580 TEST_F(AddressTrackerLinuxTest, IgnoreInterface_NonIgnoredInterface) {
581 IgnoreInterface(kIgnoredInterfaceName);
582 InitializeAddressTracker(true);
584 Buffer buffer;
585 const IPAddressNumber kEmpty;
586 const IPAddressNumber kAddr0(kAddress0, kAddress0 + arraysize(kAddress0));
588 // Verify eth0 is not ignored when only uap0 is ignored
589 MakeAddrMessage(RTM_NEWADDR, IFA_F_TEMPORARY, AF_INET, kTestInterfaceEth,
590 kAddr0, kEmpty, &buffer);
591 EXPECT_TRUE(HandleAddressMessage(buffer));
592 AddressTrackerLinux::AddressMap map = GetAddressMap();
593 EXPECT_EQ(1u, map.size());
594 EXPECT_EQ(1u, map.count(kAddr0));
595 MakeLinkMessage(RTM_NEWLINK, IFF_UP | IFF_LOWER_UP | IFF_RUNNING,
596 kTestInterfaceEth, &buffer);
597 EXPECT_TRUE(HandleLinkMessage(buffer));
598 EXPECT_EQ(1u, GetOnlineLinks().count(kTestInterfaceEth));
599 EXPECT_EQ(1u, GetOnlineLinks().size());
602 TEST_F(AddressTrackerLinuxTest, TunnelInterface) {
603 InitializeAddressTracker(true);
605 Buffer buffer;
607 // Ignores without "tun" prefixed name.
608 MakeLinkMessage(RTM_NEWLINK,
609 IFF_UP | IFF_LOWER_UP | IFF_RUNNING | IFF_POINTOPOINT,
610 kTestInterfaceEth, &buffer);
611 EXPECT_FALSE(HandleTunnelMessage(buffer));
613 // Verify success.
614 MakeLinkMessage(RTM_NEWLINK,
615 IFF_UP | IFF_LOWER_UP | IFF_RUNNING | IFF_POINTOPOINT,
616 kTestInterfaceTun, &buffer);
617 EXPECT_TRUE(HandleTunnelMessage(buffer));
619 // Ignores redundant enables.
620 MakeLinkMessage(RTM_NEWLINK,
621 IFF_UP | IFF_LOWER_UP | IFF_RUNNING | IFF_POINTOPOINT,
622 kTestInterfaceTun, &buffer);
623 EXPECT_FALSE(HandleTunnelMessage(buffer));
625 // Ignores deleting without "tun" prefixed name.
626 MakeLinkMessage(RTM_DELLINK,
627 IFF_UP | IFF_LOWER_UP | IFF_RUNNING | IFF_POINTOPOINT,
628 0, &buffer);
629 EXPECT_FALSE(HandleTunnelMessage(buffer));
631 // Verify successful deletion
632 MakeLinkMessage(RTM_DELLINK,
633 IFF_UP | IFF_LOWER_UP | IFF_RUNNING | IFF_POINTOPOINT,
634 kTestInterfaceTun, &buffer);
635 EXPECT_TRUE(HandleTunnelMessage(buffer));
637 // Ignores redundant deletions.
638 MakeLinkMessage(RTM_DELLINK,
639 IFF_UP | IFF_LOWER_UP | IFF_RUNNING | IFF_POINTOPOINT,
640 kTestInterfaceTun, &buffer);
641 EXPECT_FALSE(HandleTunnelMessage(buffer));
644 // Check AddressTrackerLinux::get_interface_name_ original implementation
645 // doesn't crash or return NULL.
646 TEST_F(AddressTrackerLinuxTest, GetInterfaceName) {
647 InitializeAddressTracker(true);
649 for (int i = 0; i < 10; i++) {
650 char buf[IFNAMSIZ] = {0};
651 EXPECT_NE((const char*)NULL, original_get_interface_name_(i, buf));
655 TEST_F(AddressTrackerLinuxTest, NonTrackingMode) {
656 InitializeAddressTracker(false);
658 const IPAddressNumber kEmpty;
659 const IPAddressNumber kAddr0(kAddress0, kAddress0 + arraysize(kAddress0));
661 Buffer buffer;
662 MakeAddrMessage(RTM_NEWADDR, IFA_F_TEMPORARY, AF_INET, kTestInterfaceEth,
663 kAddr0, kEmpty, &buffer);
664 EXPECT_TRUE(HandleAddressMessage(buffer));
665 AddressTrackerLinux::AddressMap map = GetAddressMap();
666 EXPECT_EQ(1u, map.size());
667 EXPECT_EQ(1u, map.count(kAddr0));
668 EXPECT_EQ(IFA_F_TEMPORARY, map[kAddr0].ifa_flags);
670 MakeLinkMessage(RTM_NEWLINK, IFF_UP | IFF_LOWER_UP | IFF_RUNNING, 1, &buffer);
671 EXPECT_TRUE(HandleLinkMessage(buffer));
672 EXPECT_EQ(1u, GetOnlineLinks().count(1));
673 EXPECT_EQ(1u, GetOnlineLinks().size());
676 TEST_F(AddressTrackerLinuxTest, NonTrackingModeInit) {
677 AddressTrackerLinux tracker;
678 tracker.Init();
681 } // namespace
683 } // namespace internal
684 } // namespace net