1 //
2 // Copyright (C) 2012 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "shill/device_info.h"
18
19 #include <memory>
20
21 #include <linux/if.h>
22 #include <linux/if_tun.h>
23 #include <linux/netlink.h> // Needs typedefs from sys/socket.h.
24 #include <linux/rtnetlink.h>
25 #include <linux/sockios.h>
26 #include <net/if_arp.h>
27 #include <sys/socket.h>
28
29 #include <base/bind.h>
30 #include <base/files/file_util.h>
31 #include <base/files/scoped_temp_dir.h>
32 #include <base/memory/ref_counted.h>
33 #include <base/message_loop/message_loop.h>
34 #include <base/stl_util.h>
35 #include <base/strings/string_number_conversions.h>
36 #include <gmock/gmock.h>
37 #include <gtest/gtest.h>
38
39 #include "shill/cellular/mock_modem_info.h"
40 #include "shill/logging.h"
41 #include "shill/manager.h"
42 #include "shill/mock_control.h"
43 #include "shill/mock_device.h"
44 #include "shill/mock_log.h"
45 #include "shill/mock_manager.h"
46 #include "shill/mock_metrics.h"
47 #include "shill/mock_routing_table.h"
48 #include "shill/net/ip_address.h"
49 #include "shill/net/mock_rtnl_handler.h"
50 #include "shill/net/mock_sockets.h"
51 #include "shill/net/mock_time.h"
52 #include "shill/net/rtnl_message.h"
53 #include "shill/vpn/mock_vpn_provider.h"
54 #include "shill/wimax/mock_wimax_provider.h"
55 #include "shill/wimax/wimax.h"
56
57 #if !defined(DISABLE_WIFI)
58 #include "shill/net/mock_netlink_manager.h"
59 #include "shill/net/netlink_attribute.h"
60 #include "shill/net/nl80211_message.h"
61 #endif // DISABLE_WIFI
62
63 using base::Callback;
64 using base::FilePath;
65 using std::map;
66 using std::set;
67 using std::string;
68 using std::unique_ptr;
69 using std::vector;
70 using testing::_;
71 using testing::AnyNumber;
72 using testing::ContainerEq;
73 using testing::DoAll;
74 using testing::ElementsAreArray;
75 using testing::HasSubstr;
76 using testing::Mock;
77 using testing::NotNull;
78 using testing::Return;
79 using testing::SetArgPointee;
80 using testing::StrictMock;
81 using testing::Test;
82
83 namespace shill {
84
85 class TestEventDispatcherForDeviceInfo : public EventDispatcher {
86 public:
CreateInputHandler(int,const IOHandler::InputCallback &,const IOHandler::ErrorCallback &)87 virtual IOHandler* CreateInputHandler(
88 int /*fd*/,
89 const IOHandler::InputCallback& /*input_callback*/,
90 const IOHandler::ErrorCallback& /*error_callback*/) {
91 return nullptr;
92 }
93 MOCK_METHOD2(PostDelayedTask, void(const base::Closure& task,
94 int64_t delay_ms));
95 };
96
97 class DeviceInfoTest : public Test {
98 public:
DeviceInfoTest()99 DeviceInfoTest()
100 : metrics_(&dispatcher_),
101 manager_(&control_interface_, &dispatcher_, &metrics_),
102 device_info_(&control_interface_, &dispatcher_, &metrics_, &manager_) {
103 }
~DeviceInfoTest()104 virtual ~DeviceInfoTest() {}
105
SetUp()106 virtual void SetUp() {
107 device_info_.rtnl_handler_ = &rtnl_handler_;
108 device_info_.routing_table_ = &routing_table_;
109 #if !defined(DISABLE_WIFI)
110 device_info_.netlink_manager_ = &netlink_manager_;
111 #endif // DISABLE_WIFI
112 device_info_.time_ = &time_;
113 manager_.set_mock_device_info(&device_info_);
114 EXPECT_CALL(manager_, FilterPrependDNSServersByFamily(_))
115 .WillRepeatedly(Return(vector<string>()));
116 }
117
CreateInterfaceAddress()118 IPAddress CreateInterfaceAddress() {
119 // Create an IP address entry (as if left-over from a previous connection
120 // manager).
121 IPAddress address(IPAddress::kFamilyIPv4);
122 EXPECT_TRUE(address.SetAddressFromString(kTestIPAddress0));
123 address.set_prefix(kTestIPAddressPrefix0);
124 vector<DeviceInfo::AddressData>& addresses =
125 device_info_.infos_[kTestDeviceIndex].ip_addresses;
126 addresses.push_back(DeviceInfo::AddressData(address, 0, RT_SCOPE_UNIVERSE));
127 EXPECT_EQ(1, addresses.size());
128 return address;
129 }
130
CreateDevice(const std::string & link_name,const std::string & address,int interface_index,Technology::Identifier technology)131 DeviceRefPtr CreateDevice(const std::string& link_name,
132 const std::string& address,
133 int interface_index,
134 Technology::Identifier technology) {
135 return device_info_.CreateDevice(link_name, address, interface_index,
136 technology);
137 }
138
GetDelayedDevices()139 virtual std::set<int>& GetDelayedDevices() {
140 return device_info_.delayed_devices_;
141 }
142
GetDelayedDeviceCreationMilliseconds()143 int GetDelayedDeviceCreationMilliseconds() {
144 return DeviceInfo::kDelayedDeviceCreationSeconds * 1000;
145 }
146
SetSockets()147 void SetSockets() {
148 mock_sockets_ = new MockSockets();
149 device_info_.set_sockets(mock_sockets_);
150 }
151
152 // Takes ownership of |provider|.
SetVPNProvider(VPNProvider * provider)153 void SetVPNProvider(VPNProvider* provider) {
154 manager_.vpn_provider_.reset(provider);
155 manager_.UpdateProviderMapping();
156 }
157
SetManagerRunning(bool running)158 void SetManagerRunning(bool running) {
159 manager_.running_ = running;
160 }
161
162 protected:
163 static const int kTestDeviceIndex;
164 static const char kTestDeviceName[];
165 static const uint8_t kTestMACAddress[];
166 static const char kTestIPAddress0[];
167 static const int kTestIPAddressPrefix0;
168 static const char kTestIPAddress1[];
169 static const int kTestIPAddressPrefix1;
170 static const char kTestIPAddress2[];
171 static const char kTestIPAddress3[];
172 static const char kTestIPAddress4[];
173 static const char kTestIPAddress5[];
174 static const char kTestIPAddress6[];
175 static const char kTestIPAddress7[];
176 static const int kReceiveByteCount;
177 static const int kTransmitByteCount;
178
179 RTNLMessage* BuildLinkMessage(RTNLMessage::Mode mode);
180 RTNLMessage* BuildLinkMessageWithInterfaceName(RTNLMessage::Mode mode,
181 const string& interface_name);
182 RTNLMessage* BuildAddressMessage(RTNLMessage::Mode mode,
183 const IPAddress& address,
184 unsigned char flags,
185 unsigned char scope);
186 RTNLMessage* BuildRdnssMessage(RTNLMessage::Mode mode,
187 uint32_t lifetime,
188 const vector<IPAddress>& dns_servers);
189 void SendMessageToDeviceInfo(const RTNLMessage& message);
190
191 MockControl control_interface_;
192 MockMetrics metrics_;
193 StrictMock<MockManager> manager_;
194 DeviceInfo device_info_;
195 TestEventDispatcherForDeviceInfo dispatcher_;
196 MockRoutingTable routing_table_;
197 #if !defined(DISABLE_WIFI)
198 MockNetlinkManager netlink_manager_;
199 #endif // DISABLE_WIFI
200 StrictMock<MockRTNLHandler> rtnl_handler_;
201 MockSockets* mock_sockets_; // Owned by DeviceInfo.
202 MockTime time_;
203 };
204
205 const int DeviceInfoTest::kTestDeviceIndex = 123456;
206 const char DeviceInfoTest::kTestDeviceName[] = "test-device";
207 const uint8_t DeviceInfoTest::kTestMACAddress[] = {
208 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
209 const char DeviceInfoTest::kTestIPAddress0[] = "192.168.1.1";
210 const int DeviceInfoTest::kTestIPAddressPrefix0 = 24;
211 const char DeviceInfoTest::kTestIPAddress1[] = "fe80::1aa9:5ff:abcd:1234";
212 const int DeviceInfoTest::kTestIPAddressPrefix1 = 64;
213 const char DeviceInfoTest::kTestIPAddress2[] = "fe80::1aa9:5ff:abcd:1235";
214 const char DeviceInfoTest::kTestIPAddress3[] = "fe80::1aa9:5ff:abcd:1236";
215 const char DeviceInfoTest::kTestIPAddress4[] = "fe80::1aa9:5ff:abcd:1237";
216 const char DeviceInfoTest::kTestIPAddress5[] = "192.168.1.2";
217 const char DeviceInfoTest::kTestIPAddress6[] = "192.168.2.2";
218 const char DeviceInfoTest::kTestIPAddress7[] = "fe80::1aa9:5ff:abcd:1238";
219 const int DeviceInfoTest::kReceiveByteCount = 1234;
220 const int DeviceInfoTest::kTransmitByteCount = 5678;
221
BuildLinkMessageWithInterfaceName(RTNLMessage::Mode mode,const string & interface_name)222 RTNLMessage* DeviceInfoTest::BuildLinkMessageWithInterfaceName(
223 RTNLMessage::Mode mode, const string& interface_name) {
224 RTNLMessage* message = new RTNLMessage(
225 RTNLMessage::kTypeLink,
226 mode,
227 0,
228 0,
229 0,
230 kTestDeviceIndex,
231 IPAddress::kFamilyIPv4);
232 message->SetAttribute(static_cast<uint16_t>(IFLA_IFNAME),
233 ByteString(interface_name, true));
234 ByteString test_address(kTestMACAddress, sizeof(kTestMACAddress));
235 message->SetAttribute(IFLA_ADDRESS, test_address);
236 return message;
237 }
238
BuildLinkMessage(RTNLMessage::Mode mode)239 RTNLMessage* DeviceInfoTest::BuildLinkMessage(RTNLMessage::Mode mode) {
240 return BuildLinkMessageWithInterfaceName(mode, kTestDeviceName);
241 }
242
BuildAddressMessage(RTNLMessage::Mode mode,const IPAddress & address,unsigned char flags,unsigned char scope)243 RTNLMessage* DeviceInfoTest::BuildAddressMessage(RTNLMessage::Mode mode,
244 const IPAddress& address,
245 unsigned char flags,
246 unsigned char scope) {
247 RTNLMessage* message = new RTNLMessage(
248 RTNLMessage::kTypeAddress,
249 mode,
250 0,
251 0,
252 0,
253 kTestDeviceIndex,
254 address.family());
255 message->SetAttribute(IFA_ADDRESS, address.address());
256 message->set_address_status(
257 RTNLMessage::AddressStatus(address.prefix(), flags, scope));
258 return message;
259 }
260
BuildRdnssMessage(RTNLMessage::Mode mode,uint32_t lifetime,const vector<IPAddress> & dns_servers)261 RTNLMessage* DeviceInfoTest::BuildRdnssMessage(RTNLMessage::Mode mode,
262 uint32_t lifetime, const vector<IPAddress>& dns_servers) {
263 RTNLMessage* message = new RTNLMessage(
264 RTNLMessage::kTypeRdnss,
265 mode,
266 0,
267 0,
268 0,
269 kTestDeviceIndex,
270 IPAddress::kFamilyIPv6);
271 message->set_rdnss_option(
272 RTNLMessage::RdnssOption(lifetime, dns_servers));
273 return message;
274 }
275
SendMessageToDeviceInfo(const RTNLMessage & message)276 void DeviceInfoTest::SendMessageToDeviceInfo(const RTNLMessage& message) {
277 if (message.type() == RTNLMessage::kTypeLink) {
278 device_info_.LinkMsgHandler(message);
279 } else if (message.type() == RTNLMessage::kTypeAddress) {
280 device_info_.AddressMsgHandler(message);
281 } else if (message.type() == RTNLMessage::kTypeRdnss) {
282 device_info_.RdnssMsgHandler(message);
283 } else {
284 NOTREACHED();
285 }
286 }
287
288 MATCHER_P(IsIPAddress, address, "") {
289 // NB: IPAddress objects don't support the "==" operator as per style, so
290 // we need a custom matcher.
291 return address.Equals(arg);
292 }
293
TEST_F(DeviceInfoTest,StartStop)294 TEST_F(DeviceInfoTest, StartStop) {
295 EXPECT_FALSE(device_info_.link_listener_.get());
296 EXPECT_FALSE(device_info_.address_listener_.get());
297 EXPECT_TRUE(device_info_.infos_.empty());
298
299 EXPECT_CALL(rtnl_handler_, RequestDump(RTNLHandler::kRequestLink |
300 RTNLHandler::kRequestAddr));
301 EXPECT_CALL(dispatcher_, PostDelayedTask(
302 _, DeviceInfo::kRequestLinkStatisticsIntervalMilliseconds));
303 device_info_.Start();
304 EXPECT_TRUE(device_info_.link_listener_.get());
305 EXPECT_TRUE(device_info_.address_listener_.get());
306 EXPECT_TRUE(device_info_.infos_.empty());
307 Mock::VerifyAndClearExpectations(&rtnl_handler_);
308
309 CreateInterfaceAddress();
310 EXPECT_FALSE(device_info_.infos_.empty());
311
312 device_info_.Stop();
313 EXPECT_FALSE(device_info_.link_listener_.get());
314 EXPECT_FALSE(device_info_.address_listener_.get());
315 EXPECT_TRUE(device_info_.infos_.empty());
316 }
317
TEST_F(DeviceInfoTest,RegisterDevice)318 TEST_F(DeviceInfoTest, RegisterDevice) {
319 scoped_refptr<MockDevice> device0(new MockDevice(
320 &control_interface_, &dispatcher_, &metrics_, &manager_,
321 "null0", "addr0", kTestDeviceIndex));
322
323 EXPECT_CALL(*device0, Initialize());
324 device_info_.RegisterDevice(device0);
325 }
326
TEST_F(DeviceInfoTest,RequestLinkStatistics)327 TEST_F(DeviceInfoTest, RequestLinkStatistics) {
328 EXPECT_CALL(rtnl_handler_, RequestDump(RTNLHandler::kRequestLink));
329 EXPECT_CALL(dispatcher_, PostDelayedTask(
330 _, DeviceInfo::kRequestLinkStatisticsIntervalMilliseconds));
331 device_info_.RequestLinkStatistics();
332 }
333
TEST_F(DeviceInfoTest,DeviceEnumeration)334 TEST_F(DeviceInfoTest, DeviceEnumeration) {
335 unique_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd));
336 message->set_link_status(RTNLMessage::LinkStatus(0, IFF_LOWER_UP, 0));
337 EXPECT_FALSE(device_info_.GetDevice(kTestDeviceIndex).get());
338 EXPECT_EQ(-1, device_info_.GetIndex(kTestDeviceName));
339 SendMessageToDeviceInfo(*message);
340 EXPECT_TRUE(device_info_.GetDevice(kTestDeviceIndex).get());
341 unsigned int flags = 0;
342 EXPECT_TRUE(device_info_.GetFlags(kTestDeviceIndex, &flags));
343 EXPECT_EQ(IFF_LOWER_UP, flags);
344 ByteString address;
345 EXPECT_TRUE(device_info_.GetMACAddress(kTestDeviceIndex, &address));
346 EXPECT_FALSE(address.IsEmpty());
347 EXPECT_TRUE(address.Equals(ByteString(kTestMACAddress,
348 sizeof(kTestMACAddress))));
349 EXPECT_EQ(kTestDeviceIndex, device_info_.GetIndex(kTestDeviceName));
350
351 message.reset(BuildLinkMessage(RTNLMessage::kModeAdd));
352 message->set_link_status(RTNLMessage::LinkStatus(0, IFF_UP | IFF_RUNNING, 0));
353 SendMessageToDeviceInfo(*message);
354 EXPECT_TRUE(device_info_.GetFlags(kTestDeviceIndex, &flags));
355 EXPECT_EQ(IFF_UP | IFF_RUNNING, flags);
356
357 message.reset(BuildLinkMessage(RTNLMessage::kModeDelete));
358 EXPECT_CALL(manager_, DeregisterDevice(_)).Times(1);
359 SendMessageToDeviceInfo(*message);
360 EXPECT_FALSE(device_info_.GetDevice(kTestDeviceIndex).get());
361 EXPECT_FALSE(device_info_.GetFlags(kTestDeviceIndex, nullptr));
362 EXPECT_EQ(-1, device_info_.GetIndex(kTestDeviceName));
363 }
364
TEST_F(DeviceInfoTest,DeviceRemovedEvent)365 TEST_F(DeviceInfoTest, DeviceRemovedEvent) {
366 // Remove a Wifi device.
367 scoped_refptr<MockDevice> device0(new MockDevice(
368 &control_interface_, &dispatcher_, &metrics_, &manager_,
369 "null0", "addr0", kTestDeviceIndex));
370 device_info_.infos_[kTestDeviceIndex].device = device0;
371 unique_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeDelete));
372 EXPECT_CALL(*device0, technology()).WillRepeatedly(Return(Technology::kWifi));
373 EXPECT_CALL(manager_, DeregisterDevice(_)).Times(1);
374 EXPECT_CALL(metrics_, DeregisterDevice(kTestDeviceIndex)).Times(1);
375 SendMessageToDeviceInfo(*message);
376 Mock::VerifyAndClearExpectations(device0.get());
377
378 // Deregister a Cellular device.
379 scoped_refptr<MockDevice> device1(new MockDevice(
380 &control_interface_, &dispatcher_, &metrics_, &manager_,
381 "null0", "addr0", kTestDeviceIndex));
382 device_info_.infos_[kTestDeviceIndex].device = device1;
383 EXPECT_CALL(*device1, technology()).
384 WillRepeatedly(Return(Technology::kCellular));
385 EXPECT_CALL(manager_, DeregisterDevice(_)).Times(1);
386 EXPECT_CALL(metrics_, DeregisterDevice(kTestDeviceIndex)).Times(1);
387 device_info_.DeregisterDevice(device1);
388 }
389
TEST_F(DeviceInfoTest,GetUninitializedTechnologies)390 TEST_F(DeviceInfoTest, GetUninitializedTechnologies) {
391 vector<string> technologies = device_info_.GetUninitializedTechnologies();
392 set<string> expected_technologies;
393
394 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
395 ContainerEq(expected_technologies));
396
397 device_info_.infos_[0].technology = Technology::kUnknown;
398 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
399 ContainerEq(expected_technologies));
400
401 device_info_.infos_[1].technology = Technology::kCellular;
402 technologies = device_info_.GetUninitializedTechnologies();
403 expected_technologies.insert(Technology::NameFromIdentifier(
404 Technology::kCellular));
405 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
406 ContainerEq(expected_technologies));
407
408 device_info_.infos_[2].technology = Technology::kWiMax;
409 technologies = device_info_.GetUninitializedTechnologies();
410 expected_technologies.insert(Technology::NameFromIdentifier(
411 Technology::kWiMax));
412 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
413 ContainerEq(expected_technologies));
414
415 scoped_refptr<MockDevice> device(new MockDevice(
416 &control_interface_, &dispatcher_, &metrics_, &manager_,
417 "null0", "addr0", 1));
418 device_info_.infos_[1].device = device;
419 technologies = device_info_.GetUninitializedTechnologies();
420 expected_technologies.erase(Technology::NameFromIdentifier(
421 Technology::kCellular));
422 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
423 ContainerEq(expected_technologies));
424
425 device_info_.infos_[3].technology = Technology::kCellular;
426 technologies = device_info_.GetUninitializedTechnologies();
427 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
428 ContainerEq(expected_technologies));
429
430 device_info_.infos_[3].device = device;
431 device_info_.infos_[1].device = nullptr;
432 technologies = device_info_.GetUninitializedTechnologies();
433 EXPECT_THAT(set<string>(technologies.begin(), technologies.end()),
434 ContainerEq(expected_technologies));
435 }
436
TEST_F(DeviceInfoTest,GetByteCounts)437 TEST_F(DeviceInfoTest, GetByteCounts) {
438 uint64_t rx_bytes, tx_bytes;
439 EXPECT_FALSE(device_info_.GetByteCounts(
440 kTestDeviceIndex, &rx_bytes, &tx_bytes));
441
442 // No link statistics in the message.
443 unique_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd));
444 SendMessageToDeviceInfo(*message);
445 EXPECT_TRUE(device_info_.GetByteCounts(
446 kTestDeviceIndex, &rx_bytes, &tx_bytes));
447 EXPECT_EQ(0, rx_bytes);
448 EXPECT_EQ(0, tx_bytes);
449
450 // Short link statistics message.
451 message.reset(BuildLinkMessage(RTNLMessage::kModeAdd));
452 struct rtnl_link_stats64 stats;
453 memset(&stats, 0, sizeof(stats));
454 stats.rx_bytes = kReceiveByteCount;
455 stats.tx_bytes = kTransmitByteCount;
456 ByteString stats_bytes0(reinterpret_cast<const unsigned char*>(&stats),
457 sizeof(stats) - 1);
458 message->SetAttribute(IFLA_STATS64, stats_bytes0);
459 SendMessageToDeviceInfo(*message);
460 EXPECT_TRUE(device_info_.GetByteCounts(
461 kTestDeviceIndex, &rx_bytes, &tx_bytes));
462 EXPECT_EQ(0, rx_bytes);
463 EXPECT_EQ(0, tx_bytes);
464
465 // Correctly sized link statistics message.
466 message.reset(BuildLinkMessage(RTNLMessage::kModeAdd));
467 ByteString stats_bytes1(reinterpret_cast<const unsigned char*>(&stats),
468 sizeof(stats));
469 message->SetAttribute(IFLA_STATS64, stats_bytes1);
470 SendMessageToDeviceInfo(*message);
471 EXPECT_TRUE(device_info_.GetByteCounts(
472 kTestDeviceIndex, &rx_bytes, &tx_bytes));
473 EXPECT_EQ(kReceiveByteCount, rx_bytes);
474 EXPECT_EQ(kTransmitByteCount, tx_bytes);
475 }
476
477 #if !defined(DISABLE_CELLULAR)
478
TEST_F(DeviceInfoTest,CreateDeviceCellular)479 TEST_F(DeviceInfoTest, CreateDeviceCellular) {
480 IPAddress address = CreateInterfaceAddress();
481
482 // A cellular device should be offered to ModemInfo.
483 StrictMock<MockModemInfo> modem_info;
484 EXPECT_CALL(manager_, modem_info()).WillOnce(Return(&modem_info));
485 EXPECT_CALL(modem_info, OnDeviceInfoAvailable(kTestDeviceName)).Times(1);
486 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceIndex)).Times(1);
487 EXPECT_CALL(rtnl_handler_, RemoveInterfaceAddress(kTestDeviceIndex,
488 IsIPAddress(address)));
489 EXPECT_FALSE(CreateDevice(
490 kTestDeviceName, "address", kTestDeviceIndex, Technology::kCellular));
491 }
492
493 #endif // DISABLE_CELLULAR
494
495 #if !defined(DISABLE_WIMAX)
496
TEST_F(DeviceInfoTest,CreateDeviceWiMax)497 TEST_F(DeviceInfoTest, CreateDeviceWiMax) {
498 IPAddress address = CreateInterfaceAddress();
499
500 // A WiMax device should be offered to WiMaxProvider.
501 StrictMock<MockWiMaxProvider> wimax_provider;
502 EXPECT_CALL(manager_, wimax_provider()).WillOnce(Return(&wimax_provider));
503 EXPECT_CALL(wimax_provider, OnDeviceInfoAvailable(kTestDeviceName)).Times(1);
504 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceIndex)).Times(1);
505 EXPECT_CALL(rtnl_handler_, RemoveInterfaceAddress(kTestDeviceIndex,
506 IsIPAddress(address)));
507 device_info_.infos_[kTestDeviceIndex].mac_address =
508 ByteString(kTestMACAddress, sizeof(kTestMACAddress));
509 EXPECT_FALSE(CreateDevice(
510 kTestDeviceName, "address", kTestDeviceIndex, Technology::kWiMax));
511 // The MAC address is clear such that it is obtained via
512 // GetMACAddressFromKernel() instead.
513 EXPECT_TRUE(device_info_.infos_[kTestDeviceIndex].mac_address.IsEmpty());
514 }
515
516 #endif // DISABLE_WIMAX
517
TEST_F(DeviceInfoTest,CreateDeviceEthernet)518 TEST_F(DeviceInfoTest, CreateDeviceEthernet) {
519 IPAddress address = CreateInterfaceAddress();
520
521 // An Ethernet device should cause routes and addresses to be flushed.
522 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceIndex)).Times(1);
523 EXPECT_CALL(rtnl_handler_, RemoveInterfaceAddress(kTestDeviceIndex,
524 IsIPAddress(address)));
525 DeviceRefPtr device = CreateDevice(
526 kTestDeviceName, "address", kTestDeviceIndex, Technology::kEthernet);
527 EXPECT_TRUE(device);
528 Mock::VerifyAndClearExpectations(&routing_table_);
529 Mock::VerifyAndClearExpectations(&rtnl_handler_);
530
531 // The Ethernet device destructor should not call DeregisterService()
532 // while being destructed, since the Manager may itself be partially
533 // destructed at this time.
534 EXPECT_CALL(manager_, DeregisterService(_)).Times(0);
535 device = nullptr;
536 }
537
TEST_F(DeviceInfoTest,CreateDeviceVirtioEthernet)538 TEST_F(DeviceInfoTest, CreateDeviceVirtioEthernet) {
539 IPAddress address = CreateInterfaceAddress();
540
541 // VirtioEthernet is identical to Ethernet from the perspective of this test.
542 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceIndex)).Times(1);
543 EXPECT_CALL(rtnl_handler_, RemoveInterfaceAddress(kTestDeviceIndex,
544 IsIPAddress(address)));
545 DeviceRefPtr device = CreateDevice(
546 kTestDeviceName, "address", kTestDeviceIndex,
547 Technology::kVirtioEthernet);
548 EXPECT_TRUE(device);
549 Mock::VerifyAndClearExpectations(&routing_table_);
550 Mock::VerifyAndClearExpectations(&rtnl_handler_);
551 }
552
553 #if !defined(DISABLE_WIFI)
554 MATCHER_P(IsGetInterfaceMessage, index, "") {
555 if (arg->message_type() != Nl80211Message::GetMessageType()) {
556 return false;
557 }
558 const Nl80211Message* msg = reinterpret_cast<const Nl80211Message*>(arg);
559 if (msg->command() != NL80211_CMD_GET_INTERFACE) {
560 return false;
561 }
562 uint32_t interface_index;
563 if (!msg->const_attributes()->GetU32AttributeValue(NL80211_ATTR_IFINDEX,
564 &interface_index)) {
565 return false;
566 }
567 // kInterfaceIndex is signed, but the attribute as handed from the kernel
568 // is unsigned. We're silently casting it away with this assignment.
569 uint32_t test_interface_index = index;
570 return interface_index == test_interface_index;
571 }
572
TEST_F(DeviceInfoTest,CreateDeviceWiFi)573 TEST_F(DeviceInfoTest, CreateDeviceWiFi) {
574 IPAddress address = CreateInterfaceAddress();
575
576 // WiFi looks a lot like Ethernet too.
577 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceIndex));
578 EXPECT_CALL(rtnl_handler_, RemoveInterfaceAddress(kTestDeviceIndex,
579 IsIPAddress(address)));
580
581 // Set the nl80211 message type to some non-default value.
582 Nl80211Message::SetMessageType(1234);
583
584 EXPECT_CALL(
585 netlink_manager_,
586 SendNl80211Message(IsGetInterfaceMessage(kTestDeviceIndex), _, _, _));
587 EXPECT_FALSE(CreateDevice(
588 kTestDeviceName, "address", kTestDeviceIndex, Technology::kWifi));
589 }
590 #endif // DISABLE_WIFI
591
TEST_F(DeviceInfoTest,CreateDeviceTunnelAccepted)592 TEST_F(DeviceInfoTest, CreateDeviceTunnelAccepted) {
593 IPAddress address = CreateInterfaceAddress();
594
595 // A VPN device should be offered to VPNProvider.
596 MockVPNProvider* vpn_provider = new StrictMock<MockVPNProvider>;
597 SetVPNProvider(vpn_provider);
598 EXPECT_CALL(*vpn_provider,
599 OnDeviceInfoAvailable(kTestDeviceName, kTestDeviceIndex))
600 .WillOnce(Return(true));
601 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceIndex)).Times(1);
602 EXPECT_CALL(rtnl_handler_, RemoveInterfaceAddress(kTestDeviceIndex,
603 IsIPAddress(address)));
604 EXPECT_CALL(rtnl_handler_, RemoveInterface(_)).Times(0);
605 EXPECT_FALSE(CreateDevice(
606 kTestDeviceName, "address", kTestDeviceIndex, Technology::kTunnel));
607 }
608
TEST_F(DeviceInfoTest,CreateDeviceTunnelRejected)609 TEST_F(DeviceInfoTest, CreateDeviceTunnelRejected) {
610 IPAddress address = CreateInterfaceAddress();
611
612 // A VPN device should be offered to VPNProvider.
613 MockVPNProvider* vpn_provider = new StrictMock<MockVPNProvider>;
614 SetVPNProvider(vpn_provider);
615 EXPECT_CALL(*vpn_provider,
616 OnDeviceInfoAvailable(kTestDeviceName, kTestDeviceIndex))
617 .WillOnce(Return(false));
618 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceIndex)).Times(1);
619 EXPECT_CALL(rtnl_handler_, RemoveInterfaceAddress(kTestDeviceIndex,
620 IsIPAddress(address)));
621 // Since the device was rejected by the VPNProvider, DeviceInfo will
622 // remove the interface.
623 EXPECT_CALL(rtnl_handler_, RemoveInterface(kTestDeviceIndex)).Times(1);
624 EXPECT_FALSE(CreateDevice(
625 kTestDeviceName, "address", kTestDeviceIndex, Technology::kTunnel));
626 }
627
TEST_F(DeviceInfoTest,CreateDevicePPP)628 TEST_F(DeviceInfoTest, CreateDevicePPP) {
629 IPAddress address = CreateInterfaceAddress();
630
631 // A VPN device should be offered to VPNProvider.
632 MockVPNProvider* vpn_provider = new StrictMock<MockVPNProvider>;
633 SetVPNProvider(vpn_provider);
634 EXPECT_CALL(*vpn_provider,
635 OnDeviceInfoAvailable(kTestDeviceName, kTestDeviceIndex))
636 .WillOnce(Return(false));
637 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceIndex)).Times(1);
638 EXPECT_CALL(rtnl_handler_, RemoveInterfaceAddress(kTestDeviceIndex,
639 IsIPAddress(address)));
640 // We do not remove PPP interfaces even if the provider does not accept it.
641 EXPECT_CALL(rtnl_handler_, RemoveInterface(_)).Times(0);
642 EXPECT_FALSE(CreateDevice(
643 kTestDeviceName, "address", kTestDeviceIndex, Technology::kPPP));
644 }
645
TEST_F(DeviceInfoTest,CreateDeviceLoopback)646 TEST_F(DeviceInfoTest, CreateDeviceLoopback) {
647 // A loopback device should be brought up, and nothing else done to it.
648 EXPECT_CALL(routing_table_, FlushRoutes(_)).Times(0);
649 EXPECT_CALL(rtnl_handler_, RemoveInterfaceAddress(_, _)).Times(0);
650 EXPECT_CALL(rtnl_handler_,
651 SetInterfaceFlags(kTestDeviceIndex, IFF_UP, IFF_UP)).Times(1);
652 EXPECT_FALSE(CreateDevice(
653 kTestDeviceName, "address", kTestDeviceIndex, Technology::kLoopback));
654 }
655
TEST_F(DeviceInfoTest,CreateDeviceCDCEthernet)656 TEST_F(DeviceInfoTest, CreateDeviceCDCEthernet) {
657 // A cdc_ether / cdc_ncm device should be postponed to a task.
658 EXPECT_CALL(manager_, modem_info()).Times(0);
659 EXPECT_CALL(routing_table_, FlushRoutes(_)).Times(0);
660 EXPECT_CALL(rtnl_handler_, RemoveInterfaceAddress(_, _)).Times(0);
661 EXPECT_CALL(dispatcher_,
662 PostDelayedTask(_, GetDelayedDeviceCreationMilliseconds()));
663 EXPECT_TRUE(GetDelayedDevices().empty());
664 EXPECT_FALSE(CreateDevice(
665 kTestDeviceName, "address", kTestDeviceIndex, Technology::kCDCEthernet));
666 EXPECT_FALSE(GetDelayedDevices().empty());
667 EXPECT_EQ(1, GetDelayedDevices().size());
668 EXPECT_EQ(kTestDeviceIndex, *GetDelayedDevices().begin());
669 }
670
TEST_F(DeviceInfoTest,CreateDeviceUnknown)671 TEST_F(DeviceInfoTest, CreateDeviceUnknown) {
672 IPAddress address = CreateInterfaceAddress();
673
674 // An unknown (blacklisted, unhandled, etc) device won't be flushed or
675 // registered.
676 EXPECT_CALL(routing_table_, FlushRoutes(_)).Times(0);
677 EXPECT_CALL(rtnl_handler_, RemoveInterfaceAddress(_, _)).Times(0);
678 EXPECT_TRUE(CreateDevice(
679 kTestDeviceName, "address", kTestDeviceIndex, Technology::kUnknown));
680 }
681
TEST_F(DeviceInfoTest,DeviceBlackList)682 TEST_F(DeviceInfoTest, DeviceBlackList) {
683 // Manager is not running by default.
684 EXPECT_CALL(rtnl_handler_, RequestDump(RTNLHandler::kRequestLink)).Times(0);
685 device_info_.AddDeviceToBlackList(kTestDeviceName);
686 unique_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd));
687 SendMessageToDeviceInfo(*message);
688
689 DeviceRefPtr device = device_info_.GetDevice(kTestDeviceIndex);
690 ASSERT_TRUE(device.get());
691 EXPECT_TRUE(device->technology() == Technology::kBlacklisted);
692 }
693
TEST_F(DeviceInfoTest,AddDeviceToBlackListWithManagerRunning)694 TEST_F(DeviceInfoTest, AddDeviceToBlackListWithManagerRunning) {
695 SetManagerRunning(true);
696 EXPECT_CALL(rtnl_handler_, RequestDump(RTNLHandler::kRequestLink)).Times(1);
697 device_info_.AddDeviceToBlackList(kTestDeviceName);
698 unique_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd));
699 SendMessageToDeviceInfo(*message);
700
701 DeviceRefPtr device = device_info_.GetDevice(kTestDeviceIndex);
702 ASSERT_TRUE(device.get());
703 EXPECT_TRUE(device->technology() == Technology::kBlacklisted);
704 }
705
TEST_F(DeviceInfoTest,RenamedBlacklistedDevice)706 TEST_F(DeviceInfoTest, RenamedBlacklistedDevice) {
707 device_info_.AddDeviceToBlackList(kTestDeviceName);
708 unique_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd));
709 SendMessageToDeviceInfo(*message);
710
711 DeviceRefPtr device = device_info_.GetDevice(kTestDeviceIndex);
712 ASSERT_TRUE(device.get());
713 EXPECT_TRUE(device->technology() == Technology::kBlacklisted);
714
715 // Rename the test device.
716 const char kRenamedDeviceName[] = "renamed-device";
717 unique_ptr<RTNLMessage> rename_message(
718 BuildLinkMessageWithInterfaceName(
719 RTNLMessage::kModeAdd, kRenamedDeviceName));
720 EXPECT_CALL(manager_, DeregisterDevice(_));
721 EXPECT_CALL(metrics_, DeregisterDevice(kTestDeviceIndex));
722 SendMessageToDeviceInfo(*rename_message);
723
724 DeviceRefPtr renamed_device = device_info_.GetDevice(kTestDeviceIndex);
725 ASSERT_TRUE(renamed_device.get());
726
727 // Expect that a different device has been created.
728 EXPECT_NE(device.get(), renamed_device.get());
729
730 // Since we didn't create a uevent file for kRenamedDeviceName, its
731 // technology should be unknown.
732 EXPECT_TRUE(renamed_device->technology() == Technology::kUnknown);
733 }
734
TEST_F(DeviceInfoTest,RenamedNonBlacklistedDevice)735 TEST_F(DeviceInfoTest, RenamedNonBlacklistedDevice) {
736 const char kInitialDeviceName[] = "initial-device";
737 unique_ptr<RTNLMessage> initial_message(
738 BuildLinkMessageWithInterfaceName(
739 RTNLMessage::kModeAdd, kInitialDeviceName));
740 SendMessageToDeviceInfo(*initial_message);
741 unique_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd));
742
743 DeviceRefPtr initial_device = device_info_.GetDevice(kTestDeviceIndex);
744 ASSERT_TRUE(initial_device.get());
745
746 // Since we didn't create a uevent file for kInitialDeviceName, its
747 // technology should be unknown.
748 EXPECT_TRUE(initial_device->technology() == Technology::kUnknown);
749
750 // Rename the test device.
751 const char kRenamedDeviceName[] = "renamed-device";
752 device_info_.AddDeviceToBlackList(kRenamedDeviceName);
753 unique_ptr<RTNLMessage> rename_message(
754 BuildLinkMessageWithInterfaceName(
755 RTNLMessage::kModeAdd, kRenamedDeviceName));
756 EXPECT_CALL(manager_, DeregisterDevice(_)).Times(0);
757 EXPECT_CALL(metrics_, DeregisterDevice(kTestDeviceIndex)).Times(0);
758 SendMessageToDeviceInfo(*rename_message);
759
760 DeviceRefPtr renamed_device = device_info_.GetDevice(kTestDeviceIndex);
761 ASSERT_TRUE(renamed_device.get());
762
763 // Expect that the the presence of a renamed device does not cause a new
764 // Device entry to be created if the initial device was not blacklisted.
765 EXPECT_EQ(initial_device.get(), renamed_device.get());
766 EXPECT_TRUE(initial_device->technology() == Technology::kUnknown);
767 }
768
TEST_F(DeviceInfoTest,DeviceAddressList)769 TEST_F(DeviceInfoTest, DeviceAddressList) {
770 unique_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd));
771 SendMessageToDeviceInfo(*message);
772
773 vector<DeviceInfo::AddressData> addresses;
774 EXPECT_TRUE(device_info_.GetAddresses(kTestDeviceIndex, &addresses));
775 EXPECT_TRUE(addresses.empty());
776
777 // Add an address to the device address list
778 IPAddress ip_address0(IPAddress::kFamilyIPv4);
779 EXPECT_TRUE(ip_address0.SetAddressFromString(kTestIPAddress0));
780 ip_address0.set_prefix(kTestIPAddressPrefix0);
781 message.reset(BuildAddressMessage(RTNLMessage::kModeAdd, ip_address0, 0, 0));
782 SendMessageToDeviceInfo(*message);
783 EXPECT_TRUE(device_info_.GetAddresses(kTestDeviceIndex, &addresses));
784 EXPECT_EQ(1, addresses.size());
785 EXPECT_TRUE(ip_address0.Equals(addresses[0].address));
786
787 // Re-adding the same address shouldn't cause the address list to change
788 SendMessageToDeviceInfo(*message);
789 EXPECT_TRUE(device_info_.GetAddresses(kTestDeviceIndex, &addresses));
790 EXPECT_EQ(1, addresses.size());
791 EXPECT_TRUE(ip_address0.Equals(addresses[0].address));
792
793 // Adding a new address should expand the list
794 IPAddress ip_address1(IPAddress::kFamilyIPv6);
795 EXPECT_TRUE(ip_address1.SetAddressFromString(kTestIPAddress1));
796 ip_address1.set_prefix(kTestIPAddressPrefix1);
797 message.reset(BuildAddressMessage(RTNLMessage::kModeAdd, ip_address1, 0, 0));
798 SendMessageToDeviceInfo(*message);
799 EXPECT_TRUE(device_info_.GetAddresses(kTestDeviceIndex, &addresses));
800 EXPECT_EQ(2, addresses.size());
801 EXPECT_TRUE(ip_address0.Equals(addresses[0].address));
802 EXPECT_TRUE(ip_address1.Equals(addresses[1].address));
803
804 // Deleting an address should reduce the list
805 message.reset(BuildAddressMessage(RTNLMessage::kModeDelete,
806 ip_address0,
807 0,
808 0));
809 SendMessageToDeviceInfo(*message);
810 EXPECT_TRUE(device_info_.GetAddresses(kTestDeviceIndex, &addresses));
811 EXPECT_EQ(1, addresses.size());
812 EXPECT_TRUE(ip_address1.Equals(addresses[0].address));
813
814 // Delete last item
815 message.reset(BuildAddressMessage(RTNLMessage::kModeDelete,
816 ip_address1,
817 0,
818 0));
819 SendMessageToDeviceInfo(*message);
820 EXPECT_TRUE(device_info_.GetAddresses(kTestDeviceIndex, &addresses));
821 EXPECT_TRUE(addresses.empty());
822
823 // Delete device
824 message.reset(BuildLinkMessage(RTNLMessage::kModeDelete));
825 EXPECT_CALL(manager_, DeregisterDevice(_)).Times(1);
826 SendMessageToDeviceInfo(*message);
827
828 // Should be able to handle message for interface that doesn't exist
829 message.reset(BuildAddressMessage(RTNLMessage::kModeAdd, ip_address0, 0, 0));
830 SendMessageToDeviceInfo(*message);
831 EXPECT_FALSE(device_info_.GetDevice(kTestDeviceIndex).get());
832 }
833
TEST_F(DeviceInfoTest,FlushAddressList)834 TEST_F(DeviceInfoTest, FlushAddressList) {
835 unique_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd));
836 SendMessageToDeviceInfo(*message);
837
838 IPAddress address1(IPAddress::kFamilyIPv6);
839 EXPECT_TRUE(address1.SetAddressFromString(kTestIPAddress1));
840 address1.set_prefix(kTestIPAddressPrefix1);
841 message.reset(BuildAddressMessage(RTNLMessage::kModeAdd,
842 address1,
843 0,
844 RT_SCOPE_UNIVERSE));
845 SendMessageToDeviceInfo(*message);
846 IPAddress address2(IPAddress::kFamilyIPv6);
847 EXPECT_TRUE(address2.SetAddressFromString(kTestIPAddress2));
848 message.reset(BuildAddressMessage(RTNLMessage::kModeAdd,
849 address2,
850 IFA_F_TEMPORARY,
851 RT_SCOPE_UNIVERSE));
852 SendMessageToDeviceInfo(*message);
853 IPAddress address3(IPAddress::kFamilyIPv6);
854 EXPECT_TRUE(address3.SetAddressFromString(kTestIPAddress3));
855 message.reset(BuildAddressMessage(RTNLMessage::kModeAdd,
856 address3,
857 0,
858 RT_SCOPE_LINK));
859 SendMessageToDeviceInfo(*message);
860 IPAddress address4(IPAddress::kFamilyIPv6);
861 EXPECT_TRUE(address4.SetAddressFromString(kTestIPAddress4));
862 message.reset(BuildAddressMessage(RTNLMessage::kModeAdd,
863 address4,
864 IFA_F_PERMANENT,
865 RT_SCOPE_UNIVERSE));
866 SendMessageToDeviceInfo(*message);
867
868 // DeviceInfo now has 4 addresses associated with it, but only two of
869 // them are valid for flush.
870 EXPECT_CALL(rtnl_handler_, RemoveInterfaceAddress(kTestDeviceIndex,
871 IsIPAddress(address1)));
872 EXPECT_CALL(rtnl_handler_, RemoveInterfaceAddress(kTestDeviceIndex,
873 IsIPAddress(address2)));
874 device_info_.FlushAddresses(kTestDeviceIndex);
875 }
876
TEST_F(DeviceInfoTest,HasOtherAddress)877 TEST_F(DeviceInfoTest, HasOtherAddress) {
878 unique_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd));
879 SendMessageToDeviceInfo(*message);
880
881 IPAddress address0(IPAddress::kFamilyIPv4);
882 EXPECT_TRUE(address0.SetAddressFromString(kTestIPAddress0));
883
884 // There are no addresses on this interface.
885 EXPECT_FALSE(device_info_.HasOtherAddress(kTestDeviceIndex, address0));
886
887 message.reset(BuildAddressMessage(RTNLMessage::kModeAdd,
888 address0,
889 0,
890 RT_SCOPE_UNIVERSE));
891 SendMessageToDeviceInfo(*message);
892
893 IPAddress address1(IPAddress::kFamilyIPv6);
894 EXPECT_TRUE(address1.SetAddressFromString(kTestIPAddress1));
895 address1.set_prefix(kTestIPAddressPrefix1);
896 message.reset(BuildAddressMessage(RTNLMessage::kModeAdd,
897 address1,
898 0,
899 RT_SCOPE_LINK));
900 SendMessageToDeviceInfo(*message);
901
902 IPAddress address2(IPAddress::kFamilyIPv6);
903 EXPECT_TRUE(address2.SetAddressFromString(kTestIPAddress2));
904 message.reset(BuildAddressMessage(RTNLMessage::kModeAdd,
905 address2,
906 IFA_F_TEMPORARY,
907 RT_SCOPE_UNIVERSE));
908 SendMessageToDeviceInfo(*message);
909
910 IPAddress address3(IPAddress::kFamilyIPv6);
911 EXPECT_TRUE(address3.SetAddressFromString(kTestIPAddress3));
912
913 // The only IPv6 addresses on this interface are either flagged as
914 // temporary, or they are not universally scoped.
915 EXPECT_FALSE(device_info_.HasOtherAddress(kTestDeviceIndex, address3));
916
917
918 message.reset(BuildAddressMessage(RTNLMessage::kModeAdd,
919 address3,
920 0,
921 RT_SCOPE_UNIVERSE));
922 SendMessageToDeviceInfo(*message);
923
924 // address0 is on this interface.
925 EXPECT_FALSE(device_info_.HasOtherAddress(kTestDeviceIndex, address0));
926 // address1 is on this interface.
927 EXPECT_FALSE(device_info_.HasOtherAddress(kTestDeviceIndex, address1));
928 // address2 is on this interface.
929 EXPECT_FALSE(device_info_.HasOtherAddress(kTestDeviceIndex, address2));
930 // address3 is on this interface.
931 EXPECT_FALSE(device_info_.HasOtherAddress(kTestDeviceIndex, address3));
932
933 IPAddress address4(IPAddress::kFamilyIPv6);
934 EXPECT_TRUE(address4.SetAddressFromString(kTestIPAddress4));
935
936 // address4 is not on this interface, but address3 is, and is a qualified
937 // IPv6 address.
938 EXPECT_TRUE(device_info_.HasOtherAddress(kTestDeviceIndex, address4));
939
940 message.reset(BuildAddressMessage(RTNLMessage::kModeAdd,
941 address4,
942 IFA_F_PERMANENT,
943 RT_SCOPE_UNIVERSE));
944 SendMessageToDeviceInfo(*message);
945
946 // address4 is now on this interface.
947 EXPECT_FALSE(device_info_.HasOtherAddress(kTestDeviceIndex, address4));
948
949 IPAddress address5(IPAddress::kFamilyIPv4);
950 EXPECT_TRUE(address5.SetAddressFromString(kTestIPAddress5));
951 // address5 is not on this interface, but address0 is.
952 EXPECT_TRUE(device_info_.HasOtherAddress(kTestDeviceIndex, address5));
953
954 message.reset(BuildAddressMessage(RTNLMessage::kModeAdd,
955 address5,
956 IFA_F_PERMANENT,
957 RT_SCOPE_UNIVERSE));
958 SendMessageToDeviceInfo(*message);
959
960 // address5 is now on this interface.
961 EXPECT_FALSE(device_info_.HasOtherAddress(kTestDeviceIndex, address5));
962 }
963
TEST_F(DeviceInfoTest,HasDirectConnectivityTo)964 TEST_F(DeviceInfoTest, HasDirectConnectivityTo) {
965 unique_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd));
966 SendMessageToDeviceInfo(*message);
967
968 IPAddress address0(IPAddress::kFamilyIPv4);
969 EXPECT_TRUE(address0.SetAddressFromString(kTestIPAddress0));
970
971 // There are no addresses on this interface.
972 EXPECT_FALSE(device_info_.HasDirectConnectivityTo(
973 kTestDeviceIndex, address0));
974
975 IPAddress address1(IPAddress::kFamilyIPv6);
976 EXPECT_TRUE(address1.SetAddressFromString(kTestIPAddress1));
977 message.reset(BuildAddressMessage(RTNLMessage::kModeAdd,
978 address1,
979 IFA_F_PERMANENT,
980 RT_SCOPE_UNIVERSE));
981 SendMessageToDeviceInfo(*message);
982
983 // No current addresses are of the same family as |address0|.
984 EXPECT_FALSE(device_info_.HasDirectConnectivityTo(
985 kTestDeviceIndex, address0));
986
987 IPAddress address6(IPAddress::kFamilyIPv4);
988 EXPECT_TRUE(address6.SetAddressFromString(kTestIPAddress6));
989 address6.set_prefix(kTestIPAddressPrefix0);
990 message.reset(BuildAddressMessage(RTNLMessage::kModeAdd,
991 address6,
992 IFA_F_PERMANENT,
993 RT_SCOPE_UNIVERSE));
994 SendMessageToDeviceInfo(*message);
995
996 // |address0| is not reachable from |address6|.
997 EXPECT_FALSE(device_info_.HasDirectConnectivityTo(
998 kTestDeviceIndex, address0));
999
1000 IPAddress address5(IPAddress::kFamilyIPv4);
1001 EXPECT_TRUE(address5.SetAddressFromString(kTestIPAddress5));
1002 address5.set_prefix(kTestIPAddressPrefix0);
1003 message.reset(BuildAddressMessage(RTNLMessage::kModeAdd,
1004 address5,
1005 IFA_F_PERMANENT,
1006 RT_SCOPE_UNIVERSE));
1007 SendMessageToDeviceInfo(*message);
1008
1009 // |address0| is reachable from |address5| which is associated with the
1010 // interface.
1011 EXPECT_TRUE(device_info_.HasDirectConnectivityTo(
1012 kTestDeviceIndex, address0));
1013 }
1014
TEST_F(DeviceInfoTest,HasSubdir)1015 TEST_F(DeviceInfoTest, HasSubdir) {
1016 base::ScopedTempDir temp_dir;
1017 EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
1018 EXPECT_TRUE(base::CreateDirectory(temp_dir.path().Append("child1")));
1019 FilePath child2 = temp_dir.path().Append("child2");
1020 EXPECT_TRUE(base::CreateDirectory(child2));
1021 FilePath grandchild = child2.Append("grandchild");
1022 EXPECT_TRUE(base::CreateDirectory(grandchild));
1023 EXPECT_TRUE(base::CreateDirectory(grandchild.Append("greatgrandchild")));
1024 EXPECT_TRUE(DeviceInfo::HasSubdir(temp_dir.path(),
1025 FilePath("grandchild")));
1026 EXPECT_TRUE(DeviceInfo::HasSubdir(temp_dir.path(),
1027 FilePath("greatgrandchild")));
1028 EXPECT_FALSE(DeviceInfo::HasSubdir(temp_dir.path(),
1029 FilePath("nonexistent")));
1030 }
1031
TEST_F(DeviceInfoTest,GetMACAddressFromKernelUnknownDevice)1032 TEST_F(DeviceInfoTest, GetMACAddressFromKernelUnknownDevice) {
1033 SetSockets();
1034 EXPECT_CALL(*mock_sockets_, Socket(PF_INET, SOCK_DGRAM, 0)).Times(0);
1035 ByteString mac_address =
1036 device_info_.GetMACAddressFromKernel(kTestDeviceIndex);
1037 EXPECT_TRUE(mac_address.IsEmpty());
1038 }
1039
TEST_F(DeviceInfoTest,GetMACAddressFromKernelUnableToOpenSocket)1040 TEST_F(DeviceInfoTest, GetMACAddressFromKernelUnableToOpenSocket) {
1041 SetSockets();
1042 EXPECT_CALL(*mock_sockets_, Socket(PF_INET, SOCK_DGRAM, 0))
1043 .WillOnce(Return(-1));
1044 unique_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd));
1045 message->set_link_status(RTNLMessage::LinkStatus(0, IFF_LOWER_UP, 0));
1046 SendMessageToDeviceInfo(*message);
1047 EXPECT_TRUE(device_info_.GetDevice(kTestDeviceIndex).get());
1048 ByteString mac_address =
1049 device_info_.GetMACAddressFromKernel(kTestDeviceIndex);
1050 EXPECT_TRUE(mac_address.IsEmpty());
1051 }
1052
TEST_F(DeviceInfoTest,GetMACAddressFromKernelIoctlFails)1053 TEST_F(DeviceInfoTest, GetMACAddressFromKernelIoctlFails) {
1054 SetSockets();
1055 const int kFd = 99;
1056 EXPECT_CALL(*mock_sockets_, Socket(PF_INET, SOCK_DGRAM, 0))
1057 .WillOnce(Return(kFd));
1058 EXPECT_CALL(*mock_sockets_, Ioctl(kFd, SIOCGIFHWADDR, NotNull()))
1059 .WillOnce(Return(-1));
1060 EXPECT_CALL(*mock_sockets_, Close(kFd));
1061
1062 unique_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd));
1063 message->set_link_status(RTNLMessage::LinkStatus(0, IFF_LOWER_UP, 0));
1064 SendMessageToDeviceInfo(*message);
1065 EXPECT_TRUE(device_info_.GetDevice(kTestDeviceIndex).get());
1066
1067 ByteString mac_address =
1068 device_info_.GetMACAddressFromKernel(kTestDeviceIndex);
1069 EXPECT_TRUE(mac_address.IsEmpty());
1070 }
1071
1072 MATCHER_P2(IfreqEquals, ifindex, ifname, "") {
1073 const struct ifreq* const ifr = static_cast<struct ifreq*>(arg);
1074 return (ifr != nullptr) &&
1075 (ifr->ifr_ifindex == ifindex) &&
1076 (strcmp(ifname, ifr->ifr_name) == 0);
1077 }
1078
ACTION_P(SetIfreq,ifr)1079 ACTION_P(SetIfreq, ifr) {
1080 struct ifreq* const ifr_arg = static_cast<struct ifreq*>(arg2);
1081 *ifr_arg = ifr;
1082 }
1083
TEST_F(DeviceInfoTest,GetMACAddressFromKernel)1084 TEST_F(DeviceInfoTest, GetMACAddressFromKernel) {
1085 SetSockets();
1086 const int kFd = 99;
1087 struct ifreq ifr;
1088 static uint8_t kMacAddress[] = {0x00, 0x01, 0x02, 0xaa, 0xbb, 0xcc};
1089 memcpy(ifr.ifr_hwaddr.sa_data, kMacAddress, sizeof(kMacAddress));
1090 EXPECT_CALL(*mock_sockets_, Socket(PF_INET, SOCK_DGRAM, 0))
1091 .WillOnce(Return(kFd));
1092 EXPECT_CALL(*mock_sockets_,
1093 Ioctl(kFd, SIOCGIFHWADDR,
1094 IfreqEquals(kTestDeviceIndex, kTestDeviceName)))
1095 .WillOnce(DoAll(SetIfreq(ifr), Return(0)));
1096 EXPECT_CALL(*mock_sockets_, Close(kFd));
1097
1098 unique_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd));
1099 message->set_link_status(RTNLMessage::LinkStatus(0, IFF_LOWER_UP, 0));
1100 SendMessageToDeviceInfo(*message);
1101 EXPECT_TRUE(device_info_.GetDevice(kTestDeviceIndex).get());
1102
1103 ByteString mac_address =
1104 device_info_.GetMACAddressFromKernel(kTestDeviceIndex);
1105 EXPECT_THAT(kMacAddress,
1106 ElementsAreArray(mac_address.GetData(), sizeof(kMacAddress)));
1107 }
1108
TEST_F(DeviceInfoTest,GetMACAddressOfPeerUnknownDevice)1109 TEST_F(DeviceInfoTest, GetMACAddressOfPeerUnknownDevice) {
1110 SetSockets();
1111 EXPECT_CALL(*mock_sockets_, Socket(PF_INET, SOCK_DGRAM, 0)).Times(0);
1112 IPAddress address(IPAddress::kFamilyIPv4);
1113 EXPECT_TRUE(address.SetAddressFromString(kTestIPAddress0));
1114 ByteString mac_address;
1115 EXPECT_FALSE(device_info_.GetDevice(kTestDeviceIndex).get());
1116 EXPECT_FALSE(device_info_.GetMACAddressOfPeer(
1117 kTestDeviceIndex, address, &mac_address));
1118 }
1119
TEST_F(DeviceInfoTest,GetMACAddressOfPeerBadAddress)1120 TEST_F(DeviceInfoTest, GetMACAddressOfPeerBadAddress) {
1121 SetSockets();
1122 unique_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd));
1123 message->set_link_status(RTNLMessage::LinkStatus(0, IFF_LOWER_UP, 0));
1124 SendMessageToDeviceInfo(*message);
1125 EXPECT_TRUE(device_info_.GetDevice(kTestDeviceIndex).get());
1126
1127 EXPECT_CALL(*mock_sockets_, Socket(PF_INET, SOCK_DGRAM, 0)).Times(0);
1128
1129 // An improperly formatted IPv4 address should fail.
1130 IPAddress empty_ipv4_address(IPAddress::kFamilyIPv4);
1131 ByteString mac_address;
1132 EXPECT_FALSE(device_info_.GetMACAddressOfPeer(
1133 kTestDeviceIndex, empty_ipv4_address, &mac_address));
1134
1135 // IPv6 addresses are not supported.
1136 IPAddress valid_ipv6_address(IPAddress::kFamilyIPv6);
1137 EXPECT_TRUE(valid_ipv6_address.SetAddressFromString(kTestIPAddress1));
1138 EXPECT_FALSE(device_info_.GetMACAddressOfPeer(
1139 kTestDeviceIndex, valid_ipv6_address, &mac_address));
1140 }
1141
TEST_F(DeviceInfoTest,GetMACAddressOfPeerUnableToOpenSocket)1142 TEST_F(DeviceInfoTest, GetMACAddressOfPeerUnableToOpenSocket) {
1143 SetSockets();
1144 EXPECT_CALL(*mock_sockets_, Socket(PF_INET, SOCK_DGRAM, 0))
1145 .WillOnce(Return(-1));
1146 unique_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd));
1147 message->set_link_status(RTNLMessage::LinkStatus(0, IFF_LOWER_UP, 0));
1148 SendMessageToDeviceInfo(*message);
1149 IPAddress ip_address(IPAddress::kFamilyIPv4);
1150 EXPECT_TRUE(ip_address.SetAddressFromString(kTestIPAddress0));
1151 ByteString mac_address;
1152 EXPECT_FALSE(device_info_.GetMACAddressOfPeer(
1153 kTestDeviceIndex, ip_address, &mac_address));
1154 }
1155
TEST_F(DeviceInfoTest,GetMACAddressOfPeerIoctlFails)1156 TEST_F(DeviceInfoTest, GetMACAddressOfPeerIoctlFails) {
1157 SetSockets();
1158 const int kFd = 99;
1159 EXPECT_CALL(*mock_sockets_, Socket(PF_INET, SOCK_DGRAM, 0))
1160 .WillOnce(Return(kFd));
1161 EXPECT_CALL(*mock_sockets_, Ioctl(kFd, SIOCGARP, NotNull()))
1162 .WillOnce(Return(-1));
1163 unique_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd));
1164 message->set_link_status(RTNLMessage::LinkStatus(0, IFF_LOWER_UP, 0));
1165 SendMessageToDeviceInfo(*message);
1166 IPAddress ip_address(IPAddress::kFamilyIPv4);
1167 EXPECT_TRUE(ip_address.SetAddressFromString(kTestIPAddress0));
1168 ByteString mac_address;
1169 EXPECT_FALSE(device_info_.GetMACAddressOfPeer(
1170 kTestDeviceIndex, ip_address, &mac_address));
1171 }
1172
1173 MATCHER_P2(ArpreqEquals, ifname, peer, "") {
1174 const struct arpreq* const areq = static_cast<struct arpreq*>(arg);
1175 if (areq == nullptr) {
1176 return false;
1177 }
1178
1179 const struct sockaddr_in* const protocol_address =
1180 reinterpret_cast<const struct sockaddr_in*>(&areq->arp_pa);
1181 const struct sockaddr_in* const hardware_address =
1182 reinterpret_cast<const struct sockaddr_in*>(&areq->arp_ha);
1183
1184 return
1185 strcmp(ifname, areq->arp_dev) == 0 &&
1186 protocol_address->sin_family == AF_INET &&
1187 memcmp(&protocol_address->sin_addr.s_addr,
1188 peer.address().GetConstData(),
1189 peer.address().GetLength()) == 0 &&
1190 hardware_address->sin_family == ARPHRD_ETHER;
1191 }
1192
ACTION_P(SetArpreq,areq)1193 ACTION_P(SetArpreq, areq) {
1194 struct arpreq* const areq_arg = static_cast<struct arpreq*>(arg2);
1195 *areq_arg = areq;
1196 }
1197
TEST_F(DeviceInfoTest,GetMACAddressOfPeer)1198 TEST_F(DeviceInfoTest, GetMACAddressOfPeer) {
1199 unique_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd));
1200 message->set_link_status(RTNLMessage::LinkStatus(0, IFF_LOWER_UP, 0));
1201 SendMessageToDeviceInfo(*message);
1202
1203 SetSockets();
1204
1205 const int kFd = 99;
1206 EXPECT_CALL(*mock_sockets_, Socket(PF_INET, SOCK_DGRAM, 0))
1207 .WillRepeatedly(Return(kFd));
1208
1209 IPAddress ip_address(IPAddress::kFamilyIPv4);
1210 EXPECT_TRUE(ip_address.SetAddressFromString(kTestIPAddress0));
1211
1212 static uint8_t kZeroMacAddress[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1213 struct arpreq zero_areq_response;
1214 memcpy(zero_areq_response.arp_ha.sa_data, kZeroMacAddress,
1215 sizeof(kZeroMacAddress));
1216
1217 static uint8_t kMacAddress[] = {0x01, 0x02, 0x03, 0xaa, 0xbb, 0xcc};
1218 struct arpreq areq_response;
1219 memcpy(areq_response.arp_ha.sa_data, kMacAddress, sizeof(kMacAddress));
1220
1221 EXPECT_CALL(*mock_sockets_, Ioctl(
1222 kFd, SIOCGARP, ArpreqEquals(kTestDeviceName, ip_address)))
1223 .WillOnce(DoAll(SetArpreq(zero_areq_response), Return(0)))
1224 .WillOnce(DoAll(SetArpreq(areq_response), Return(0)));
1225
1226 ByteString mac_address;
1227 EXPECT_FALSE(device_info_.GetMACAddressOfPeer(
1228 kTestDeviceIndex, ip_address, &mac_address));
1229 EXPECT_TRUE(device_info_.GetMACAddressOfPeer(
1230 kTestDeviceIndex, ip_address, &mac_address));
1231 EXPECT_THAT(kMacAddress,
1232 ElementsAreArray(mac_address.GetData(), sizeof(kMacAddress)));
1233 }
1234
TEST_F(DeviceInfoTest,IPv6AddressChanged)1235 TEST_F(DeviceInfoTest, IPv6AddressChanged) {
1236 scoped_refptr<MockDevice> device(new MockDevice(
1237 &control_interface_, &dispatcher_, &metrics_, &manager_,
1238 "null0", "addr0", kTestDeviceIndex));
1239
1240 // Device info entry does not exist.
1241 EXPECT_FALSE(device_info_.GetPrimaryIPv6Address(kTestDeviceIndex, nullptr));
1242
1243 device_info_.infos_[kTestDeviceIndex].device = device;
1244
1245 // Device info entry contains no addresses.
1246 EXPECT_FALSE(device_info_.GetPrimaryIPv6Address(kTestDeviceIndex, nullptr));
1247
1248 IPAddress ipv4_address(IPAddress::kFamilyIPv4);
1249 EXPECT_TRUE(ipv4_address.SetAddressFromString(kTestIPAddress0));
1250 unique_ptr<RTNLMessage> message(
1251 BuildAddressMessage(RTNLMessage::kModeAdd, ipv4_address, 0, 0));
1252
1253 EXPECT_CALL(*device, OnIPv6AddressChanged()).Times(0);
1254
1255 // We should ignore IPv4 addresses.
1256 SendMessageToDeviceInfo(*message);
1257 EXPECT_FALSE(device_info_.GetPrimaryIPv6Address(kTestDeviceIndex, nullptr));
1258
1259 IPAddress ipv6_address1(IPAddress::kFamilyIPv6);
1260 EXPECT_TRUE(ipv6_address1.SetAddressFromString(kTestIPAddress1));
1261 message.reset(BuildAddressMessage(
1262 RTNLMessage::kModeAdd, ipv6_address1, 0, RT_SCOPE_LINK));
1263
1264 // We should ignore non-SCOPE_UNIVERSE messages for IPv6.
1265 SendMessageToDeviceInfo(*message);
1266 EXPECT_FALSE(device_info_.GetPrimaryIPv6Address(kTestDeviceIndex, nullptr));
1267
1268 Mock::VerifyAndClearExpectations(device.get());
1269 IPAddress ipv6_address2(IPAddress::kFamilyIPv6);
1270 EXPECT_TRUE(ipv6_address2.SetAddressFromString(kTestIPAddress2));
1271 message.reset(BuildAddressMessage(
1272 RTNLMessage::kModeAdd, ipv6_address2, IFA_F_TEMPORARY,
1273 RT_SCOPE_UNIVERSE));
1274
1275 // Add a temporary address.
1276 EXPECT_CALL(*device, OnIPv6AddressChanged());
1277 SendMessageToDeviceInfo(*message);
1278 IPAddress address0(IPAddress::kFamilyUnknown);
1279 EXPECT_TRUE(device_info_.GetPrimaryIPv6Address(kTestDeviceIndex, &address0));
1280 EXPECT_TRUE(address0.Equals(ipv6_address2));
1281 Mock::VerifyAndClearExpectations(device.get());
1282
1283 IPAddress ipv6_address3(IPAddress::kFamilyIPv6);
1284 EXPECT_TRUE(ipv6_address3.SetAddressFromString(kTestIPAddress3));
1285 message.reset(BuildAddressMessage(
1286 RTNLMessage::kModeAdd, ipv6_address3, 0, RT_SCOPE_UNIVERSE));
1287
1288 // Adding a non-temporary address alerts the Device, but does not override
1289 // the primary address since the previous one was temporary.
1290 EXPECT_CALL(*device, OnIPv6AddressChanged());
1291 SendMessageToDeviceInfo(*message);
1292 IPAddress address1(IPAddress::kFamilyUnknown);
1293 EXPECT_TRUE(device_info_.GetPrimaryIPv6Address(kTestDeviceIndex, &address1));
1294 EXPECT_TRUE(address1.Equals(ipv6_address2));
1295 Mock::VerifyAndClearExpectations(device.get());
1296
1297 IPAddress ipv6_address4(IPAddress::kFamilyIPv6);
1298 EXPECT_TRUE(ipv6_address4.SetAddressFromString(kTestIPAddress4));
1299 message.reset(BuildAddressMessage(
1300 RTNLMessage::kModeAdd, ipv6_address4, IFA_F_TEMPORARY | IFA_F_DEPRECATED,
1301 RT_SCOPE_UNIVERSE));
1302
1303 // Adding a temporary deprecated address alerts the Device, but does not
1304 // override the primary address since the previous one was non-deprecated.
1305 EXPECT_CALL(*device, OnIPv6AddressChanged());
1306 SendMessageToDeviceInfo(*message);
1307 IPAddress address2(IPAddress::kFamilyUnknown);
1308 EXPECT_TRUE(device_info_.GetPrimaryIPv6Address(kTestDeviceIndex, &address2));
1309 EXPECT_TRUE(address2.Equals(ipv6_address2));
1310 Mock::VerifyAndClearExpectations(device.get());
1311
1312 IPAddress ipv6_address7(IPAddress::kFamilyIPv6);
1313 EXPECT_TRUE(ipv6_address7.SetAddressFromString(kTestIPAddress7));
1314 message.reset(BuildAddressMessage(
1315 RTNLMessage::kModeAdd, ipv6_address7, IFA_F_TEMPORARY,
1316 RT_SCOPE_UNIVERSE));
1317
1318 // Another temporary (non-deprecated) address alerts the Device, and will
1319 // override the previous primary address.
1320 EXPECT_CALL(*device, OnIPv6AddressChanged());
1321 SendMessageToDeviceInfo(*message);
1322 IPAddress address3(IPAddress::kFamilyUnknown);
1323 EXPECT_TRUE(device_info_.GetPrimaryIPv6Address(kTestDeviceIndex, &address3));
1324 EXPECT_TRUE(address3.Equals(ipv6_address7));
1325 }
1326
1327
TEST_F(DeviceInfoTest,IPv6DnsServerAddressesChanged)1328 TEST_F(DeviceInfoTest, IPv6DnsServerAddressesChanged) {
1329 scoped_refptr<MockDevice> device(new MockDevice(
1330 &control_interface_, &dispatcher_, &metrics_, &manager_,
1331 "null0", "addr0", kTestDeviceIndex));
1332 device_info_.time_ = &time_;
1333 vector<IPAddress> dns_server_addresses_out;
1334 uint32_t lifetime_out;
1335
1336 // Device info entry does not exist.
1337 EXPECT_FALSE(device_info_.GetIPv6DnsServerAddresses(
1338 kTestDeviceIndex, &dns_server_addresses_out, &lifetime_out));
1339
1340 device_info_.infos_[kTestDeviceIndex].device = device;
1341
1342 // Device info entry contains no IPv6 dns server addresses.
1343 EXPECT_FALSE(device_info_.GetIPv6DnsServerAddresses(
1344 kTestDeviceIndex, &dns_server_addresses_out, &lifetime_out));
1345
1346 // Setup IPv6 dns server addresses.
1347 IPAddress ipv6_address1(IPAddress::kFamilyIPv6);
1348 IPAddress ipv6_address2(IPAddress::kFamilyIPv6);
1349 EXPECT_TRUE(ipv6_address1.SetAddressFromString(kTestIPAddress1));
1350 EXPECT_TRUE(ipv6_address2.SetAddressFromString(kTestIPAddress2));
1351 vector<IPAddress> dns_server_addresses_in;
1352 dns_server_addresses_in.push_back(ipv6_address1);
1353 dns_server_addresses_in.push_back(ipv6_address2);
1354
1355 // Infinite lifetime
1356 const uint32_t kInfiniteLifetime = 0xffffffff;
1357 unique_ptr<RTNLMessage> message(BuildRdnssMessage(
1358 RTNLMessage::kModeAdd, kInfiniteLifetime, dns_server_addresses_in));
1359 EXPECT_CALL(time_, GetSecondsBoottime(_)).
1360 WillOnce(DoAll(SetArgPointee<0>(0), Return(true)));
1361 EXPECT_CALL(*device, OnIPv6DnsServerAddressesChanged()).Times(1);
1362 SendMessageToDeviceInfo(*message);
1363 EXPECT_CALL(time_, GetSecondsBoottime(_)).Times(0);
1364 EXPECT_TRUE(device_info_.GetIPv6DnsServerAddresses(
1365 kTestDeviceIndex, &dns_server_addresses_out, &lifetime_out));
1366 // Verify addresses and lifetime.
1367 EXPECT_EQ(kInfiniteLifetime, lifetime_out);
1368 EXPECT_EQ(2, dns_server_addresses_out.size());
1369 EXPECT_EQ(kTestIPAddress1, dns_server_addresses_out.at(0).ToString());
1370 EXPECT_EQ(kTestIPAddress2, dns_server_addresses_out.at(1).ToString());
1371
1372 // Lifetime of 120, retrieve DNS server addresses after 10 seconds.
1373 const uint32_t kLifetime120 = 120;
1374 const uint32_t kElapseTime10 = 10;
1375 unique_ptr<RTNLMessage> message1(BuildRdnssMessage(
1376 RTNLMessage::kModeAdd, kLifetime120, dns_server_addresses_in));
1377 EXPECT_CALL(time_, GetSecondsBoottime(_)).
1378 WillOnce(DoAll(SetArgPointee<0>(0), Return(true)));
1379 EXPECT_CALL(*device, OnIPv6DnsServerAddressesChanged()).Times(1);
1380 SendMessageToDeviceInfo(*message1);
1381 // 10 seconds passed when GetIPv6DnsServerAddreses is called.
1382 EXPECT_CALL(time_, GetSecondsBoottime(_)).
1383 WillOnce(DoAll(SetArgPointee<0>(kElapseTime10), Return(true)));
1384 EXPECT_TRUE(device_info_.GetIPv6DnsServerAddresses(
1385 kTestDeviceIndex, &dns_server_addresses_out, &lifetime_out));
1386 // Verify addresses and lifetime.
1387 EXPECT_EQ(kLifetime120 - kElapseTime10, lifetime_out);
1388 EXPECT_EQ(2, dns_server_addresses_out.size());
1389 EXPECT_EQ(kTestIPAddress1, dns_server_addresses_out.at(0).ToString());
1390 EXPECT_EQ(kTestIPAddress2, dns_server_addresses_out.at(1).ToString());
1391
1392 // Lifetime of 120, retrieve DNS server addresses after lifetime expired.
1393 EXPECT_CALL(time_, GetSecondsBoottime(_)).
1394 WillOnce(DoAll(SetArgPointee<0>(0), Return(true)));
1395 EXPECT_CALL(*device, OnIPv6DnsServerAddressesChanged()).Times(1);
1396 SendMessageToDeviceInfo(*message1);
1397 // 120 seconds passed when GetIPv6DnsServerAddreses is called.
1398 EXPECT_CALL(time_, GetSecondsBoottime(_)).
1399 WillOnce(DoAll(SetArgPointee<0>(kLifetime120), Return(true)));
1400 EXPECT_TRUE(device_info_.GetIPv6DnsServerAddresses(
1401 kTestDeviceIndex, &dns_server_addresses_out, &lifetime_out));
1402 // Verify addresses and lifetime.
1403 EXPECT_EQ(0, lifetime_out);
1404 EXPECT_EQ(2, dns_server_addresses_out.size());
1405 EXPECT_EQ(kTestIPAddress1, dns_server_addresses_out.at(0).ToString());
1406 EXPECT_EQ(kTestIPAddress2, dns_server_addresses_out.at(1).ToString());
1407 }
1408
1409 class DeviceInfoTechnologyTest : public DeviceInfoTest {
1410 public:
DeviceInfoTechnologyTest()1411 DeviceInfoTechnologyTest()
1412 : DeviceInfoTest(),
1413 test_device_name_(kTestDeviceName) {}
~DeviceInfoTechnologyTest()1414 virtual ~DeviceInfoTechnologyTest() {}
1415
SetUp()1416 virtual void SetUp() {
1417 CHECK(temp_dir_.CreateUniqueTempDir());
1418 device_info_root_ = temp_dir_.path().Append("sys/class/net");
1419 device_info_.device_info_root_ = device_info_root_;
1420 // Most tests require that the uevent file exist.
1421 CreateInfoFile("uevent", "xxx");
1422 }
1423
GetDeviceTechnology()1424 Technology::Identifier GetDeviceTechnology() {
1425 return device_info_.GetDeviceTechnology(test_device_name_);
1426 }
1427 FilePath GetInfoPath(const string& name);
1428 void CreateInfoFile(const string& name, const string& contents);
1429 void CreateInfoSymLink(const string& name, const string& contents);
SetDeviceName(const string & name)1430 void SetDeviceName(const string& name) {
1431 test_device_name_ = name;
1432 EXPECT_TRUE(temp_dir_.Delete()); // nuke old temp dir
1433 SetUp();
1434 }
1435
1436 protected:
1437 base::ScopedTempDir temp_dir_;
1438 FilePath device_info_root_;
1439 string test_device_name_;
1440 };
1441
GetInfoPath(const string & name)1442 FilePath DeviceInfoTechnologyTest::GetInfoPath(const string& name) {
1443 return device_info_root_.Append(test_device_name_).Append(name);
1444 }
1445
CreateInfoFile(const string & name,const string & contents)1446 void DeviceInfoTechnologyTest::CreateInfoFile(const string& name,
1447 const string& contents) {
1448 FilePath info_path = GetInfoPath(name);
1449 EXPECT_TRUE(base::CreateDirectory(info_path.DirName()));
1450 string contents_newline(contents + "\n");
1451 EXPECT_TRUE(base::WriteFile(info_path, contents_newline.c_str(),
1452 contents_newline.size()));
1453 }
1454
CreateInfoSymLink(const string & name,const string & contents)1455 void DeviceInfoTechnologyTest::CreateInfoSymLink(const string& name,
1456 const string& contents) {
1457 FilePath info_path = GetInfoPath(name);
1458 EXPECT_TRUE(base::CreateDirectory(info_path.DirName()));
1459 EXPECT_TRUE(base::CreateSymbolicLink(FilePath(contents), info_path));
1460 }
1461
TEST_F(DeviceInfoTechnologyTest,Unknown)1462 TEST_F(DeviceInfoTechnologyTest, Unknown) {
1463 // With a uevent file but no driver symlink, we should get a pseudo-technology
1464 // which specifies this condition explicitly.
1465 EXPECT_EQ(Technology::kNoDeviceSymlink, GetDeviceTechnology());
1466
1467 // Should be unknown without a uevent file.
1468 EXPECT_TRUE(base::DeleteFile(GetInfoPath("uevent"), false));
1469 EXPECT_EQ(Technology::kUnknown, GetDeviceTechnology());
1470 }
1471
TEST_F(DeviceInfoTechnologyTest,IgnoredPrefix)1472 TEST_F(DeviceInfoTechnologyTest, IgnoredPrefix) {
1473 test_device_name_ = "veth0";
1474 // A new uevent file is needed since the device name has changed.
1475 CreateInfoFile("uevent", "xxx");
1476 // A device with a "veth" prefix should be ignored.
1477 EXPECT_EQ(Technology::kUnknown, GetDeviceTechnology());
1478 }
1479
TEST_F(DeviceInfoTechnologyTest,Loopback)1480 TEST_F(DeviceInfoTechnologyTest, Loopback) {
1481 CreateInfoFile("type", base::IntToString(ARPHRD_LOOPBACK));
1482 EXPECT_EQ(Technology::kLoopback, GetDeviceTechnology());
1483 }
1484
TEST_F(DeviceInfoTechnologyTest,PPP)1485 TEST_F(DeviceInfoTechnologyTest, PPP) {
1486 CreateInfoFile("type", base::IntToString(ARPHRD_PPP));
1487 EXPECT_EQ(Technology::kPPP, GetDeviceTechnology());
1488 }
1489
TEST_F(DeviceInfoTechnologyTest,Tunnel)1490 TEST_F(DeviceInfoTechnologyTest, Tunnel) {
1491 CreateInfoFile("tun_flags", base::IntToString(IFF_TUN));
1492 EXPECT_EQ(Technology::kTunnel, GetDeviceTechnology());
1493 }
1494
TEST_F(DeviceInfoTechnologyTest,WiFi)1495 TEST_F(DeviceInfoTechnologyTest, WiFi) {
1496 CreateInfoFile("uevent", "DEVTYPE=wlan");
1497 EXPECT_EQ(Technology::kWifi, GetDeviceTechnology());
1498 CreateInfoFile("uevent", "foo\nDEVTYPE=wlan");
1499 EXPECT_EQ(Technology::kWifi, GetDeviceTechnology());
1500 CreateInfoFile("type", base::IntToString(ARPHRD_IEEE80211_RADIOTAP));
1501 EXPECT_EQ(Technology::kWiFiMonitor, GetDeviceTechnology());
1502 }
1503
TEST_F(DeviceInfoTechnologyTest,Ethernet)1504 TEST_F(DeviceInfoTechnologyTest, Ethernet) {
1505 CreateInfoSymLink("device/driver", "xxx");
1506 EXPECT_EQ(Technology::kEthernet, GetDeviceTechnology());
1507 }
1508
TEST_F(DeviceInfoTechnologyTest,WiMax)1509 TEST_F(DeviceInfoTechnologyTest, WiMax) {
1510 CreateInfoSymLink("device/driver", "gdm_wimax");
1511 EXPECT_EQ(Technology::kWiMax, GetDeviceTechnology());
1512 }
1513
TEST_F(DeviceInfoTechnologyTest,CellularGobi1)1514 TEST_F(DeviceInfoTechnologyTest, CellularGobi1) {
1515 CreateInfoSymLink("device/driver", "blah/foo/gobi");
1516 EXPECT_EQ(Technology::kCellular, GetDeviceTechnology());
1517 }
1518
TEST_F(DeviceInfoTechnologyTest,CellularGobi2)1519 TEST_F(DeviceInfoTechnologyTest, CellularGobi2) {
1520 CreateInfoSymLink("device/driver", "../GobiNet");
1521 EXPECT_EQ(Technology::kCellular, GetDeviceTechnology());
1522 }
1523
TEST_F(DeviceInfoTechnologyTest,QCUSB)1524 TEST_F(DeviceInfoTechnologyTest, QCUSB) {
1525 CreateInfoSymLink("device/driver", "QCUSBNet2k");
1526 EXPECT_EQ(Technology::kCellular, GetDeviceTechnology());
1527 }
1528
TEST_F(DeviceInfoTechnologyTest,CellularCdcMbim)1529 TEST_F(DeviceInfoTechnologyTest, CellularCdcMbim) {
1530 CreateInfoSymLink("device/driver", "cdc_mbim");
1531 EXPECT_EQ(Technology::kCellular, GetDeviceTechnology());
1532 }
1533
TEST_F(DeviceInfoTechnologyTest,CellularQmiWwan)1534 TEST_F(DeviceInfoTechnologyTest, CellularQmiWwan) {
1535 CreateInfoSymLink("device/driver", "qmi_wwan");
1536 EXPECT_EQ(Technology::kCellular, GetDeviceTechnology());
1537 }
1538
1539 // Modem with absolute driver path with top-level tty file:
1540 // /sys/class/net/dev0/device -> /sys/devices/virtual/0/00
1541 // /sys/devices/virtual/0/00/driver -> /drivers/cdc_ether or /drivers/cdc_ncm
1542 // /sys/devices/virtual/0/01/tty [empty directory]
TEST_F(DeviceInfoTechnologyTest,CDCEthernetModem1)1543 TEST_F(DeviceInfoTechnologyTest, CDCEthernetModem1) {
1544 FilePath device_root(temp_dir_.path().Append("sys/devices/virtual/0"));
1545 FilePath device_path(device_root.Append("00"));
1546 FilePath driver_symlink(device_path.Append("driver"));
1547 EXPECT_TRUE(base::CreateDirectory(device_path));
1548 CreateInfoSymLink("device", device_path.value());
1549 EXPECT_TRUE(base::CreateSymbolicLink(FilePath("/drivers/cdc_ether"),
1550 driver_symlink));
1551 EXPECT_TRUE(base::CreateDirectory(device_root.Append("01/tty")));
1552 EXPECT_EQ(Technology::kCellular, GetDeviceTechnology());
1553
1554 EXPECT_TRUE(base::DeleteFile(driver_symlink, false));
1555 EXPECT_TRUE(base::CreateSymbolicLink(FilePath("/drivers/cdc_ncm"),
1556 driver_symlink));
1557 EXPECT_EQ(Technology::kCellular, GetDeviceTechnology());
1558 }
1559
1560 // Modem with relative driver path with top-level tty file.
1561 // /sys/class/net/dev0/device -> ../../../device_dir/0/00
1562 // /sys/device_dir/0/00/driver -> /drivers/cdc_ether or /drivers/cdc_ncm
1563 // /sys/device_dir/0/01/tty [empty directory]
TEST_F(DeviceInfoTechnologyTest,CDCEthernetModem2)1564 TEST_F(DeviceInfoTechnologyTest, CDCEthernetModem2) {
1565 CreateInfoSymLink("device", "../../../device_dir/0/00");
1566 FilePath device_root(temp_dir_.path().Append("sys/device_dir/0"));
1567 FilePath device_path(device_root.Append("00"));
1568 FilePath driver_symlink(device_path.Append("driver"));
1569 EXPECT_TRUE(base::CreateDirectory(device_path));
1570 EXPECT_TRUE(base::CreateSymbolicLink(FilePath("/drivers/cdc_ether"),
1571 driver_symlink));
1572 EXPECT_TRUE(base::CreateDirectory(device_root.Append("01/tty")));
1573 EXPECT_EQ(Technology::kCellular, GetDeviceTechnology());
1574
1575 EXPECT_TRUE(base::DeleteFile(driver_symlink, false));
1576 EXPECT_TRUE(base::CreateSymbolicLink(FilePath("/drivers/cdc_ncm"),
1577 driver_symlink));
1578 EXPECT_EQ(Technology::kCellular, GetDeviceTechnology());
1579 }
1580
1581 // Modem with relative driver path with lower-level tty file.
1582 // /sys/class/net/dev0/device -> ../../../device_dir/0/00
1583 // /sys/device_dir/0/00/driver -> /drivers/cdc_ether or /drivers/cdc_ncm
1584 // /sys/device_dir/0/01/yyy/tty [empty directory]
TEST_F(DeviceInfoTechnologyTest,CDCEthernetModem3)1585 TEST_F(DeviceInfoTechnologyTest, CDCEthernetModem3) {
1586 CreateInfoSymLink("device", "../../../device_dir/0/00");
1587 FilePath device_root(temp_dir_.path().Append("sys/device_dir/0"));
1588 FilePath device_path(device_root.Append("00"));
1589 FilePath driver_symlink(device_path.Append("driver"));
1590 EXPECT_TRUE(base::CreateDirectory(device_path));
1591 EXPECT_TRUE(base::CreateSymbolicLink(FilePath("/drivers/cdc_ether"),
1592 driver_symlink));
1593 EXPECT_TRUE(base::CreateDirectory(device_root.Append("01/yyy/tty")));
1594 EXPECT_EQ(Technology::kCellular, GetDeviceTechnology());
1595
1596 EXPECT_TRUE(base::DeleteFile(driver_symlink, false));
1597 EXPECT_TRUE(base::CreateSymbolicLink(FilePath("/drivers/cdc_ncm"),
1598 driver_symlink));
1599 EXPECT_EQ(Technology::kCellular, GetDeviceTechnology());
1600 }
1601
TEST_F(DeviceInfoTechnologyTest,CDCEtherNonModem)1602 TEST_F(DeviceInfoTechnologyTest, CDCEtherNonModem) {
1603 CreateInfoSymLink("device", "device_dir");
1604 CreateInfoSymLink("device_dir/driver", "cdc_ether");
1605 EXPECT_EQ(Technology::kCDCEthernet, GetDeviceTechnology());
1606 }
1607
TEST_F(DeviceInfoTechnologyTest,CDCNcmNonModem)1608 TEST_F(DeviceInfoTechnologyTest, CDCNcmNonModem) {
1609 CreateInfoSymLink("device", "device_dir");
1610 CreateInfoSymLink("device_dir/driver", "cdc_ncm");
1611 EXPECT_EQ(Technology::kCDCEthernet, GetDeviceTechnology());
1612 }
1613
TEST_F(DeviceInfoTechnologyTest,PseudoModem)1614 TEST_F(DeviceInfoTechnologyTest, PseudoModem) {
1615 SetDeviceName("pseudomodem");
1616 CreateInfoSymLink("device", "device_dir");
1617 CreateInfoSymLink("device_dir/driver", "cdc_ether");
1618 EXPECT_EQ(Technology::kCellular, GetDeviceTechnology());
1619
1620 SetDeviceName("pseudomodem9");
1621 CreateInfoSymLink("device", "device_dir");
1622 CreateInfoSymLink("device_dir/driver", "cdc_ether");
1623 EXPECT_EQ(Technology::kCellular, GetDeviceTechnology());
1624 }
1625
1626 class DeviceInfoForDelayedCreationTest : public DeviceInfo {
1627 public:
DeviceInfoForDelayedCreationTest(ControlInterface * control_interface,EventDispatcher * dispatcher,Metrics * metrics,Manager * manager)1628 DeviceInfoForDelayedCreationTest(ControlInterface* control_interface,
1629 EventDispatcher* dispatcher,
1630 Metrics* metrics,
1631 Manager* manager)
1632 : DeviceInfo(control_interface, dispatcher, metrics, manager) {}
1633 MOCK_METHOD4(CreateDevice, DeviceRefPtr(const std::string& link_name,
1634 const std::string& address,
1635 int interface_index,
1636 Technology::Identifier technology));
1637 MOCK_METHOD1(GetDeviceTechnology,
1638 Technology::Identifier(const string& iface_name));
1639 };
1640
1641 class DeviceInfoDelayedCreationTest : public DeviceInfoTest {
1642 public:
DeviceInfoDelayedCreationTest()1643 DeviceInfoDelayedCreationTest()
1644 : DeviceInfoTest(),
1645 test_device_info_(
1646 &control_interface_, &dispatcher_, &metrics_, &manager_) {}
~DeviceInfoDelayedCreationTest()1647 virtual ~DeviceInfoDelayedCreationTest() {}
1648
GetDelayedDevices()1649 virtual std::set<int>& GetDelayedDevices() {
1650 return test_device_info_.delayed_devices_;
1651 }
1652
DelayedDeviceCreationTask()1653 void DelayedDeviceCreationTask() {
1654 test_device_info_.DelayedDeviceCreationTask();
1655 }
1656
AddDelayedDevice(Technology::Identifier delayed_technology)1657 void AddDelayedDevice(Technology::Identifier delayed_technology) {
1658 unique_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd));
1659 EXPECT_CALL(test_device_info_, GetDeviceTechnology(kTestDeviceName))
1660 .WillOnce(Return(delayed_technology));
1661 EXPECT_CALL(test_device_info_, CreateDevice(
1662 kTestDeviceName, _, kTestDeviceIndex, delayed_technology))
1663 .WillOnce(Return(DeviceRefPtr()));
1664 test_device_info_.AddLinkMsgHandler(*message);
1665 Mock::VerifyAndClearExpectations(&test_device_info_);
1666 // We need to insert the device index ourselves since we have mocked
1667 // out CreateDevice. This insertion is tested in CreateDeviceCDCEthernet
1668 // above.
1669 GetDelayedDevices().insert(kTestDeviceIndex);
1670 }
1671
EnsureDelayedDevice(Technology::Identifier reported_device_technology,Technology::Identifier created_device_technology)1672 void EnsureDelayedDevice(Technology::Identifier reported_device_technology,
1673 Technology::Identifier created_device_technology) {
1674 EXPECT_CALL(test_device_info_, GetDeviceTechnology(_))
1675 .WillOnce(Return(reported_device_technology));
1676 EXPECT_CALL(test_device_info_, CreateDevice(
1677 kTestDeviceName, _, kTestDeviceIndex, created_device_technology))
1678 .WillOnce(Return(DeviceRefPtr()));
1679 DelayedDeviceCreationTask();
1680 EXPECT_TRUE(GetDelayedDevices().empty());
1681 }
1682
1683 #if !defined(DISABLE_WIFI)
TriggerOnWiFiInterfaceInfoReceived(const Nl80211Message & message)1684 void TriggerOnWiFiInterfaceInfoReceived(const Nl80211Message& message) {
1685 test_device_info_.OnWiFiInterfaceInfoReceived(message);
1686 }
1687 #endif // DISABLE_WIFI
1688
1689 protected:
1690 DeviceInfoForDelayedCreationTest test_device_info_;
1691 };
1692
TEST_F(DeviceInfoDelayedCreationTest,NoDevices)1693 TEST_F(DeviceInfoDelayedCreationTest, NoDevices) {
1694 EXPECT_TRUE(GetDelayedDevices().empty());
1695 EXPECT_CALL(test_device_info_, GetDeviceTechnology(_)).Times(0);
1696 DelayedDeviceCreationTask();
1697 }
1698
TEST_F(DeviceInfoDelayedCreationTest,CDCEthernetDevice)1699 TEST_F(DeviceInfoDelayedCreationTest, CDCEthernetDevice) {
1700 AddDelayedDevice(Technology::kCDCEthernet);
1701 EnsureDelayedDevice(Technology::kCDCEthernet, Technology::kEthernet);
1702 }
1703
TEST_F(DeviceInfoDelayedCreationTest,CellularDevice)1704 TEST_F(DeviceInfoDelayedCreationTest, CellularDevice) {
1705 AddDelayedDevice(Technology::kCDCEthernet);
1706 EnsureDelayedDevice(Technology::kCellular, Technology::kCellular);
1707 }
1708
TEST_F(DeviceInfoDelayedCreationTest,TunnelDevice)1709 TEST_F(DeviceInfoDelayedCreationTest, TunnelDevice) {
1710 AddDelayedDevice(Technology::kNoDeviceSymlink);
1711 EnsureDelayedDevice(Technology::kTunnel, Technology::kTunnel);
1712 }
1713
TEST_F(DeviceInfoDelayedCreationTest,NoDeviceSymlinkEthernet)1714 TEST_F(DeviceInfoDelayedCreationTest, NoDeviceSymlinkEthernet) {
1715 AddDelayedDevice(Technology::kNoDeviceSymlink);
1716 EXPECT_CALL(manager_, ignore_unknown_ethernet())
1717 .WillOnce(Return(false));
1718 EnsureDelayedDevice(Technology::kNoDeviceSymlink, Technology::kEthernet);
1719 }
1720
TEST_F(DeviceInfoDelayedCreationTest,NoDeviceSymlinkIgnored)1721 TEST_F(DeviceInfoDelayedCreationTest, NoDeviceSymlinkIgnored) {
1722 AddDelayedDevice(Technology::kNoDeviceSymlink);
1723 EXPECT_CALL(manager_, ignore_unknown_ethernet())
1724 .WillOnce(Return(true));
1725 EnsureDelayedDevice(Technology::kNoDeviceSymlink, Technology::kUnknown);
1726 }
1727
1728 #if !defined(DISABLE_WIFI)
TEST_F(DeviceInfoDelayedCreationTest,WiFiDevice)1729 TEST_F(DeviceInfoDelayedCreationTest, WiFiDevice) {
1730 ScopedMockLog log;
1731 EXPECT_CALL(log, Log(logging::LOG_ERROR, _,
1732 HasSubstr("Message is not a new interface response")));
1733 GetInterfaceMessage non_interface_response_message;
1734 TriggerOnWiFiInterfaceInfoReceived(non_interface_response_message);
1735 Mock::VerifyAndClearExpectations(&log);
1736
1737 EXPECT_CALL(log, Log(logging::LOG_ERROR, _,
1738 HasSubstr("Message contains no interface index")));
1739 NewInterfaceMessage message;
1740 TriggerOnWiFiInterfaceInfoReceived(message);
1741 Mock::VerifyAndClearExpectations(&log);
1742
1743 message.attributes()->CreateNl80211Attribute(
1744 NL80211_ATTR_IFINDEX, NetlinkMessage::MessageContext());
1745 message.attributes()->SetU32AttributeValue(NL80211_ATTR_IFINDEX,
1746 kTestDeviceIndex);
1747 EXPECT_CALL(log, Log(logging::LOG_ERROR, _,
1748 HasSubstr("Message contains no interface type")));
1749 TriggerOnWiFiInterfaceInfoReceived(message);
1750 Mock::VerifyAndClearExpectations(&log);
1751
1752 message.attributes()->CreateNl80211Attribute(
1753 NL80211_ATTR_IFTYPE, NetlinkMessage::MessageContext());
1754 message.attributes()->SetU32AttributeValue(NL80211_ATTR_IFTYPE,
1755 NL80211_IFTYPE_AP);
1756 EXPECT_CALL(log, Log(logging::LOG_ERROR, _,
1757 HasSubstr("Could not find device info for interface")));
1758 TriggerOnWiFiInterfaceInfoReceived(message);
1759 Mock::VerifyAndClearExpectations(&log);
1760
1761 // Use the AddDelayedDevice() method to create a device info entry with no
1762 // associated device.
1763 AddDelayedDevice(Technology::kNoDeviceSymlink);
1764
1765 EXPECT_CALL(log, Log(logging::LOG_INFO, _,
1766 HasSubstr("it is not in station mode")));
1767 TriggerOnWiFiInterfaceInfoReceived(message);
1768 Mock::VerifyAndClearExpectations(&log);
1769 Mock::VerifyAndClearExpectations(&manager_);
1770
1771 message.attributes()->SetU32AttributeValue(NL80211_ATTR_IFTYPE,
1772 NL80211_IFTYPE_STATION);
1773 EXPECT_CALL(manager_, RegisterDevice(_));
1774 EXPECT_CALL(manager_, device_info())
1775 .WillRepeatedly(Return(&test_device_info_));
1776 EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
1777 EXPECT_CALL(log, Log(logging::LOG_INFO, _,
1778 HasSubstr("Creating WiFi device")));
1779 TriggerOnWiFiInterfaceInfoReceived(message);
1780 Mock::VerifyAndClearExpectations(&log);
1781 Mock::VerifyAndClearExpectations(&manager_);
1782
1783 EXPECT_CALL(manager_, RegisterDevice(_)).Times(0);
1784 EXPECT_CALL(log, Log(logging::LOG_ERROR, _,
1785 HasSubstr("Device already created for interface")));
1786 TriggerOnWiFiInterfaceInfoReceived(message);
1787 }
1788 #endif // DISABLE_WIFI
1789
1790 } // namespace shill
1791