Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 4 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 5 | #include "shill/device_info.h" |
| 6 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 7 | #include <glib.h> |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 8 | #include <linux/if.h> |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 9 | #include <linux/if_tun.h> |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 10 | #include <linux/netlink.h> // Needs typedefs from sys/socket.h. |
| 11 | #include <linux/rtnetlink.h> |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 12 | #include <linux/sockios.h> |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 13 | #include <net/if_arp.h> |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 14 | #include <sys/socket.h> |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 15 | |
Paul Stewart | 2ddf2c6 | 2013-04-16 09:47:34 -0700 | [diff] [blame] | 16 | #include <base/bind.h> |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 17 | #include <base/file_util.h> |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 18 | #include <base/files/scoped_temp_dir.h> |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 19 | #include <base/memory/ref_counted.h> |
| 20 | #include <base/message_loop.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 21 | #include <base/stl_util.h> |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 22 | #include <base/string_number_conversions.h> |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 23 | #include <gmock/gmock.h> |
Ben Chan | 5086b97 | 2013-01-15 21:51:38 -0800 | [diff] [blame] | 24 | #include <gtest/gtest.h> |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 25 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 26 | #include "shill/ip_address.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 27 | #include "shill/logging.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 28 | #include "shill/manager.h" |
Chris Masone | 46eaaf5 | 2011-05-24 13:08:30 -0700 | [diff] [blame] | 29 | #include "shill/mock_control.h" |
Ben Chan | 5086b97 | 2013-01-15 21:51:38 -0800 | [diff] [blame] | 30 | #include "shill/mock_device.h" |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 31 | #include "shill/mock_glib.h" |
Paul Stewart | 2ddf2c6 | 2013-04-16 09:47:34 -0700 | [diff] [blame] | 32 | #include "shill/mock_log.h" |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 33 | #include "shill/mock_manager.h" |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 34 | #include "shill/mock_metrics.h" |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 35 | #include "shill/mock_modem_info.h" |
Paul Stewart | 2ddf2c6 | 2013-04-16 09:47:34 -0700 | [diff] [blame] | 36 | #include "shill/mock_netlink_manager.h" |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 37 | #include "shill/mock_routing_table.h" |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 38 | #include "shill/mock_rtnl_handler.h" |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 39 | #include "shill/mock_sockets.h" |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 40 | #include "shill/mock_vpn_provider.h" |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 41 | #include "shill/mock_wimax_provider.h" |
Paul Stewart | 2ddf2c6 | 2013-04-16 09:47:34 -0700 | [diff] [blame] | 42 | #include "shill/netlink_attribute.h" |
| 43 | #include "shill/nl80211_message.h" |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 44 | #include "shill/rtnl_message.h" |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 45 | #include "shill/wimax.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 46 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 47 | using base::Callback; |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 48 | using base::FilePath; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 49 | using std::map; |
Ben Chan | 5086b97 | 2013-01-15 21:51:38 -0800 | [diff] [blame] | 50 | using std::set; |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 51 | using std::string; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 52 | using std::vector; |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 53 | using testing::_; |
Paul Stewart | 2ddf2c6 | 2013-04-16 09:47:34 -0700 | [diff] [blame] | 54 | using testing::AnyNumber; |
Ben Chan | 5086b97 | 2013-01-15 21:51:38 -0800 | [diff] [blame] | 55 | using testing::ContainerEq; |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 56 | using testing::DoAll; |
| 57 | using testing::ElementsAreArray; |
Paul Stewart | 2ddf2c6 | 2013-04-16 09:47:34 -0700 | [diff] [blame] | 58 | using testing::HasSubstr; |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 59 | using testing::Mock; |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 60 | using testing::NotNull; |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 61 | using testing::Return; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 62 | using testing::StrictMock; |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 63 | using testing::Test; |
| 64 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 65 | namespace shill { |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 66 | |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 67 | class TestEventDispatcherForDeviceInfo : public EventDispatcher { |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 68 | public: |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 69 | virtual IOHandler *CreateInputHandler( |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 70 | int /*fd*/, |
Paul Stewart | 5f06a0e | 2012-12-20 11:11:33 -0800 | [diff] [blame] | 71 | const IOHandler::InputCallback &/*input_callback*/, |
| 72 | const IOHandler::ErrorCallback &/*error_callback*/) { |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 73 | return NULL; |
| 74 | } |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 75 | MOCK_METHOD2(PostDelayedTask, bool(const base::Closure &task, |
| 76 | int64 delay_ms)); |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 77 | }; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 78 | |
| 79 | class DeviceInfoTest : public Test { |
| 80 | public: |
| 81 | DeviceInfoTest() |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 82 | : metrics_(&dispatcher_), |
| 83 | manager_(&control_interface_, &dispatcher_, &metrics_, &glib_), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 84 | device_info_(&control_interface_, &dispatcher_, &metrics_, &manager_) { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 85 | } |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 86 | virtual ~DeviceInfoTest() {} |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 87 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 88 | virtual void SetUp() { |
| 89 | device_info_.rtnl_handler_ = &rtnl_handler_; |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 90 | device_info_.routing_table_ = &routing_table_; |
Paul Stewart | 2ddf2c6 | 2013-04-16 09:47:34 -0700 | [diff] [blame] | 91 | device_info_.netlink_manager_ = &netlink_manager_; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 94 | IPAddress CreateInterfaceAddress() { |
| 95 | // Create an IP address entry (as if left-over from a previous connection |
| 96 | // manager). |
| 97 | IPAddress address(IPAddress::kFamilyIPv4); |
| 98 | EXPECT_TRUE(address.SetAddressFromString(kTestIPAddress0)); |
| 99 | address.set_prefix(kTestIPAddressPrefix0); |
| 100 | vector<DeviceInfo::AddressData> &addresses = |
| 101 | device_info_.infos_[kTestDeviceIndex].ip_addresses; |
| 102 | addresses.push_back(DeviceInfo::AddressData(address, 0, RT_SCOPE_UNIVERSE)); |
| 103 | EXPECT_EQ(1, addresses.size()); |
| 104 | return address; |
| 105 | } |
| 106 | |
| 107 | DeviceRefPtr CreateDevice(const std::string &link_name, |
| 108 | const std::string &address, |
| 109 | int interface_index, |
| 110 | Technology::Identifier technology) { |
| 111 | return device_info_.CreateDevice(link_name, address, interface_index, |
| 112 | technology); |
| 113 | } |
| 114 | |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 115 | virtual std::set<int> &GetDelayedDevices() { |
| 116 | return device_info_.delayed_devices_; |
| 117 | } |
| 118 | |
Paul Stewart | 1ac4e84 | 2012-07-10 12:58:12 -0700 | [diff] [blame] | 119 | int GetDelayedDeviceCreationMilliseconds() { |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 120 | return DeviceInfo::kDelayedDeviceCreationSeconds * 1000; |
| 121 | } |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 122 | |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 123 | void SetSockets() { |
| 124 | mock_sockets_ = new MockSockets(); |
| 125 | device_info_.set_sockets(mock_sockets_); |
| 126 | } |
| 127 | |
Darin Petkov | c3505a5 | 2013-03-18 15:13:29 +0100 | [diff] [blame] | 128 | // Takes ownership of |provider|. |
| 129 | void SetVPNProvider(VPNProvider *provider) { |
| 130 | manager_.vpn_provider_.reset(provider); |
Paul Stewart | b87d22b | 2013-07-29 11:11:37 -0700 | [diff] [blame] | 131 | manager_.UpdateProviderMapping(); |
Darin Petkov | c3505a5 | 2013-03-18 15:13:29 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 134 | protected: |
Chris Masone | b2e326b | 2011-07-12 13:28:51 -0700 | [diff] [blame] | 135 | static const int kTestDeviceIndex; |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 136 | static const char kTestDeviceName[]; |
Han Shen | fc34925 | 2012-08-30 11:36:04 -0700 | [diff] [blame] | 137 | static const uint8_t kTestMACAddress[]; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 138 | static const char kTestIPAddress0[]; |
| 139 | static const int kTestIPAddressPrefix0; |
| 140 | static const char kTestIPAddress1[]; |
| 141 | static const int kTestIPAddressPrefix1; |
| 142 | static const char kTestIPAddress2[]; |
| 143 | static const char kTestIPAddress3[]; |
| 144 | static const char kTestIPAddress4[]; |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 145 | static const char kTestIPAddress5[]; |
Paul Stewart | 1ac4e84 | 2012-07-10 12:58:12 -0700 | [diff] [blame] | 146 | static const int kReceiveByteCount; |
| 147 | static const int kTransmitByteCount; |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 148 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 149 | RTNLMessage *BuildLinkMessage(RTNLMessage::Mode mode); |
Paul Stewart | e81eb70 | 2012-04-11 15:04:53 -0700 | [diff] [blame] | 150 | RTNLMessage *BuildLinkMessageWithInterfaceName(RTNLMessage::Mode mode, |
| 151 | const string &interface_name); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 152 | RTNLMessage *BuildAddressMessage(RTNLMessage::Mode mode, |
| 153 | const IPAddress &address, |
| 154 | unsigned char flags, |
| 155 | unsigned char scope); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 156 | void SendMessageToDeviceInfo(const RTNLMessage &message); |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 157 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 158 | MockGLib glib_; |
Chris Masone | 46eaaf5 | 2011-05-24 13:08:30 -0700 | [diff] [blame] | 159 | MockControl control_interface_; |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 160 | MockMetrics metrics_; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 161 | StrictMock<MockManager> manager_; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 162 | DeviceInfo device_info_; |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 163 | TestEventDispatcherForDeviceInfo dispatcher_; |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 164 | MockRoutingTable routing_table_; |
Paul Stewart | 2ddf2c6 | 2013-04-16 09:47:34 -0700 | [diff] [blame] | 165 | MockNetlinkManager netlink_manager_; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 166 | StrictMock<MockRTNLHandler> rtnl_handler_; |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 167 | MockSockets *mock_sockets_; // Owned by DeviceInfo. |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 168 | }; |
| 169 | |
Chris Masone | b2e326b | 2011-07-12 13:28:51 -0700 | [diff] [blame] | 170 | const int DeviceInfoTest::kTestDeviceIndex = 123456; |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 171 | const char DeviceInfoTest::kTestDeviceName[] = "test-device"; |
Han Shen | fc34925 | 2012-08-30 11:36:04 -0700 | [diff] [blame] | 172 | const uint8_t DeviceInfoTest::kTestMACAddress[] = { |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 173 | 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 174 | const char DeviceInfoTest::kTestIPAddress0[] = "192.168.1.1"; |
| 175 | const int DeviceInfoTest::kTestIPAddressPrefix0 = 24; |
| 176 | const char DeviceInfoTest::kTestIPAddress1[] = "fe80::1aa9:5ff:abcd:1234"; |
| 177 | const int DeviceInfoTest::kTestIPAddressPrefix1 = 64; |
| 178 | const char DeviceInfoTest::kTestIPAddress2[] = "fe80::1aa9:5ff:abcd:1235"; |
| 179 | const char DeviceInfoTest::kTestIPAddress3[] = "fe80::1aa9:5ff:abcd:1236"; |
| 180 | const char DeviceInfoTest::kTestIPAddress4[] = "fe80::1aa9:5ff:abcd:1237"; |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 181 | const char DeviceInfoTest::kTestIPAddress5[] = "192.168.1.2"; |
Paul Stewart | 1ac4e84 | 2012-07-10 12:58:12 -0700 | [diff] [blame] | 182 | const int DeviceInfoTest::kReceiveByteCount = 1234; |
| 183 | const int DeviceInfoTest::kTransmitByteCount = 5678; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 184 | |
Paul Stewart | e81eb70 | 2012-04-11 15:04:53 -0700 | [diff] [blame] | 185 | RTNLMessage *DeviceInfoTest::BuildLinkMessageWithInterfaceName( |
| 186 | RTNLMessage::Mode mode, const string &interface_name) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 187 | RTNLMessage *message = new RTNLMessage( |
| 188 | RTNLMessage::kTypeLink, |
| 189 | mode, |
| 190 | 0, |
| 191 | 0, |
| 192 | 0, |
| 193 | kTestDeviceIndex, |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 194 | IPAddress::kFamilyIPv4); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 195 | message->SetAttribute(static_cast<uint16>(IFLA_IFNAME), |
Paul Stewart | e81eb70 | 2012-04-11 15:04:53 -0700 | [diff] [blame] | 196 | ByteString(interface_name, true)); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 197 | ByteString test_address(kTestMACAddress, sizeof(kTestMACAddress)); |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 198 | message->SetAttribute(IFLA_ADDRESS, test_address); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 199 | return message; |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Paul Stewart | e81eb70 | 2012-04-11 15:04:53 -0700 | [diff] [blame] | 202 | RTNLMessage *DeviceInfoTest::BuildLinkMessage(RTNLMessage::Mode mode) { |
| 203 | return BuildLinkMessageWithInterfaceName(mode, kTestDeviceName); |
| 204 | } |
| 205 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 206 | RTNLMessage *DeviceInfoTest::BuildAddressMessage(RTNLMessage::Mode mode, |
| 207 | const IPAddress &address, |
| 208 | unsigned char flags, |
| 209 | unsigned char scope) { |
| 210 | RTNLMessage *message = new RTNLMessage( |
| 211 | RTNLMessage::kTypeAddress, |
| 212 | mode, |
| 213 | 0, |
| 214 | 0, |
| 215 | 0, |
| 216 | kTestDeviceIndex, |
| 217 | address.family()); |
| 218 | message->SetAttribute(IFA_ADDRESS, address.address()); |
| 219 | message->set_address_status( |
| 220 | RTNLMessage::AddressStatus(address.prefix(), flags, scope)); |
| 221 | return message; |
| 222 | } |
| 223 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 224 | void DeviceInfoTest::SendMessageToDeviceInfo(const RTNLMessage &message) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 225 | if (message.type() == RTNLMessage::kTypeLink) { |
| 226 | device_info_.LinkMsgHandler(message); |
| 227 | } else if (message.type() == RTNLMessage::kTypeAddress) { |
| 228 | device_info_.AddressMsgHandler(message); |
| 229 | } else { |
| 230 | NOTREACHED(); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | MATCHER_P(IsIPAddress, address, "") { |
| 235 | // NB: IPAddress objects don't support the "==" operator as per style, so |
| 236 | // we need a custom matcher. |
| 237 | return address.Equals(arg); |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 240 | TEST_F(DeviceInfoTest, StartStop) { |
| 241 | EXPECT_FALSE(device_info_.link_listener_.get()); |
| 242 | EXPECT_FALSE(device_info_.address_listener_.get()); |
| 243 | EXPECT_TRUE(device_info_.infos_.empty()); |
| 244 | |
| 245 | EXPECT_CALL(rtnl_handler_, RequestDump(RTNLHandler::kRequestLink | |
| 246 | RTNLHandler::kRequestAddr)); |
Paul Stewart | 1ac4e84 | 2012-07-10 12:58:12 -0700 | [diff] [blame] | 247 | EXPECT_CALL(dispatcher_, PostDelayedTask( |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 248 | _, DeviceInfo::kRequestLinkStatisticsIntervalMilliseconds)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 249 | device_info_.Start(); |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 250 | EXPECT_TRUE(device_info_.link_listener_.get()); |
| 251 | EXPECT_TRUE(device_info_.address_listener_.get()); |
| 252 | EXPECT_TRUE(device_info_.infos_.empty()); |
| 253 | Mock::VerifyAndClearExpectations(&rtnl_handler_); |
| 254 | |
| 255 | CreateInterfaceAddress(); |
| 256 | EXPECT_FALSE(device_info_.infos_.empty()); |
| 257 | |
| 258 | device_info_.Stop(); |
| 259 | EXPECT_FALSE(device_info_.link_listener_.get()); |
| 260 | EXPECT_FALSE(device_info_.address_listener_.get()); |
| 261 | EXPECT_TRUE(device_info_.infos_.empty()); |
| 262 | } |
| 263 | |
Paul Stewart | 1ac4e84 | 2012-07-10 12:58:12 -0700 | [diff] [blame] | 264 | TEST_F(DeviceInfoTest, RequestLinkStatistics) { |
| 265 | EXPECT_CALL(rtnl_handler_, RequestDump(RTNLHandler::kRequestLink)); |
| 266 | EXPECT_CALL(dispatcher_, PostDelayedTask( |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 267 | _, DeviceInfo::kRequestLinkStatisticsIntervalMilliseconds)); |
Paul Stewart | 1ac4e84 | 2012-07-10 12:58:12 -0700 | [diff] [blame] | 268 | device_info_.RequestLinkStatistics(); |
| 269 | } |
| 270 | |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 271 | TEST_F(DeviceInfoTest, DeviceEnumeration) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 272 | scoped_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd)); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 273 | message->set_link_status(RTNLMessage::LinkStatus(0, IFF_LOWER_UP, 0)); |
| 274 | EXPECT_FALSE(device_info_.GetDevice(kTestDeviceIndex).get()); |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 275 | EXPECT_EQ(-1, device_info_.GetIndex(kTestDeviceName)); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 276 | SendMessageToDeviceInfo(*message); |
| 277 | EXPECT_TRUE(device_info_.GetDevice(kTestDeviceIndex).get()); |
| 278 | unsigned int flags = 0; |
| 279 | EXPECT_TRUE(device_info_.GetFlags(kTestDeviceIndex, &flags)); |
| 280 | EXPECT_EQ(IFF_LOWER_UP, flags); |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 281 | ByteString address; |
Paul Stewart | 3285296 | 2011-08-30 14:06:53 -0700 | [diff] [blame] | 282 | EXPECT_TRUE(device_info_.GetMACAddress(kTestDeviceIndex, &address)); |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 283 | EXPECT_FALSE(address.IsEmpty()); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 284 | EXPECT_TRUE(address.Equals(ByteString(kTestMACAddress, |
| 285 | sizeof(kTestMACAddress)))); |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 286 | EXPECT_EQ(kTestDeviceIndex, device_info_.GetIndex(kTestDeviceName)); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 287 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 288 | message.reset(BuildLinkMessage(RTNLMessage::kModeAdd)); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 289 | message->set_link_status(RTNLMessage::LinkStatus(0, IFF_UP | IFF_RUNNING, 0)); |
| 290 | SendMessageToDeviceInfo(*message); |
| 291 | EXPECT_TRUE(device_info_.GetFlags(kTestDeviceIndex, &flags)); |
| 292 | EXPECT_EQ(IFF_UP | IFF_RUNNING, flags); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 293 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 294 | message.reset(BuildLinkMessage(RTNLMessage::kModeDelete)); |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 295 | EXPECT_CALL(manager_, DeregisterDevice(_)).Times(1); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 296 | SendMessageToDeviceInfo(*message); |
| 297 | EXPECT_FALSE(device_info_.GetDevice(kTestDeviceIndex).get()); |
| 298 | EXPECT_FALSE(device_info_.GetFlags(kTestDeviceIndex, NULL)); |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 299 | EXPECT_EQ(-1, device_info_.GetIndex(kTestDeviceName)); |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 300 | } |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 301 | |
Ben Chan | 5086b97 | 2013-01-15 21:51:38 -0800 | [diff] [blame] | 302 | TEST_F(DeviceInfoTest, GetUninitializedTechnologies) { |
| 303 | vector<string> technologies = device_info_.GetUninitializedTechnologies(); |
| 304 | set<string> expected_technologies; |
| 305 | |
| 306 | EXPECT_THAT(set<string>(technologies.begin(), technologies.end()), |
| 307 | ContainerEq(expected_technologies)); |
| 308 | |
| 309 | device_info_.infos_[0].technology = Technology::kUnknown; |
| 310 | EXPECT_THAT(set<string>(technologies.begin(), technologies.end()), |
| 311 | ContainerEq(expected_technologies)); |
| 312 | |
| 313 | device_info_.infos_[1].technology = Technology::kCellular; |
| 314 | technologies = device_info_.GetUninitializedTechnologies(); |
| 315 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 316 | Technology::kCellular)); |
| 317 | EXPECT_THAT(set<string>(technologies.begin(), technologies.end()), |
| 318 | ContainerEq(expected_technologies)); |
| 319 | |
| 320 | device_info_.infos_[2].technology = Technology::kWiMax; |
| 321 | technologies = device_info_.GetUninitializedTechnologies(); |
| 322 | expected_technologies.insert(Technology::NameFromIdentifier( |
| 323 | Technology::kWiMax)); |
| 324 | EXPECT_THAT(set<string>(technologies.begin(), technologies.end()), |
| 325 | ContainerEq(expected_technologies)); |
| 326 | |
| 327 | scoped_refptr<MockDevice> device(new MockDevice( |
| 328 | &control_interface_, &dispatcher_, &metrics_, &manager_, |
| 329 | "null0", "addr0", 1)); |
| 330 | device_info_.infos_[1].device = device; |
| 331 | technologies = device_info_.GetUninitializedTechnologies(); |
| 332 | expected_technologies.erase(Technology::NameFromIdentifier( |
| 333 | Technology::kCellular)); |
| 334 | EXPECT_THAT(set<string>(technologies.begin(), technologies.end()), |
| 335 | ContainerEq(expected_technologies)); |
| 336 | |
| 337 | device_info_.infos_[3].technology = Technology::kCellular; |
| 338 | technologies = device_info_.GetUninitializedTechnologies(); |
Arman Uguray | b00c13d | 2013-07-29 18:20:52 -0700 | [diff] [blame] | 339 | EXPECT_THAT(set<string>(technologies.begin(), technologies.end()), |
| 340 | ContainerEq(expected_technologies)); |
| 341 | |
| 342 | device_info_.infos_[3].device = device; |
| 343 | device_info_.infos_[1].device = NULL; |
| 344 | technologies = device_info_.GetUninitializedTechnologies(); |
Ben Chan | 5086b97 | 2013-01-15 21:51:38 -0800 | [diff] [blame] | 345 | EXPECT_THAT(set<string>(technologies.begin(), technologies.end()), |
| 346 | ContainerEq(expected_technologies)); |
| 347 | } |
| 348 | |
Paul Stewart | 1ac4e84 | 2012-07-10 12:58:12 -0700 | [diff] [blame] | 349 | TEST_F(DeviceInfoTest, GetByteCounts) { |
| 350 | uint64 rx_bytes, tx_bytes; |
| 351 | EXPECT_FALSE(device_info_.GetByteCounts( |
| 352 | kTestDeviceIndex, &rx_bytes, &tx_bytes)); |
| 353 | |
| 354 | // No link statistics in the message. |
| 355 | scoped_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd)); |
| 356 | SendMessageToDeviceInfo(*message); |
| 357 | EXPECT_TRUE(device_info_.GetByteCounts( |
| 358 | kTestDeviceIndex, &rx_bytes, &tx_bytes)); |
| 359 | EXPECT_EQ(0, rx_bytes); |
| 360 | EXPECT_EQ(0, tx_bytes); |
| 361 | |
| 362 | // Short link statistics message. |
| 363 | message.reset(BuildLinkMessage(RTNLMessage::kModeAdd)); |
| 364 | struct rtnl_link_stats64 stats; |
| 365 | memset(&stats, 0, sizeof(stats)); |
| 366 | stats.rx_bytes = kReceiveByteCount; |
| 367 | stats.tx_bytes = kTransmitByteCount; |
| 368 | ByteString stats_bytes0(reinterpret_cast<const unsigned char*>(&stats), |
| 369 | sizeof(stats) - 1); |
| 370 | message->SetAttribute(IFLA_STATS64, stats_bytes0); |
| 371 | SendMessageToDeviceInfo(*message); |
| 372 | EXPECT_TRUE(device_info_.GetByteCounts( |
| 373 | kTestDeviceIndex, &rx_bytes, &tx_bytes)); |
| 374 | EXPECT_EQ(0, rx_bytes); |
| 375 | EXPECT_EQ(0, tx_bytes); |
| 376 | |
| 377 | // Correctly sized link statistics message. |
| 378 | message.reset(BuildLinkMessage(RTNLMessage::kModeAdd)); |
| 379 | ByteString stats_bytes1(reinterpret_cast<const unsigned char*>(&stats), |
| 380 | sizeof(stats)); |
| 381 | message->SetAttribute(IFLA_STATS64, stats_bytes1); |
| 382 | SendMessageToDeviceInfo(*message); |
| 383 | EXPECT_TRUE(device_info_.GetByteCounts( |
| 384 | kTestDeviceIndex, &rx_bytes, &tx_bytes)); |
| 385 | EXPECT_EQ(kReceiveByteCount, rx_bytes); |
| 386 | EXPECT_EQ(kTransmitByteCount, tx_bytes); |
| 387 | } |
| 388 | |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 389 | TEST_F(DeviceInfoTest, CreateDeviceCellular) { |
| 390 | IPAddress address = CreateInterfaceAddress(); |
| 391 | |
| 392 | // A cellular device should be offered to ModemInfo. |
| 393 | StrictMock<MockModemInfo> modem_info; |
| 394 | EXPECT_CALL(manager_, modem_info()).WillOnce(Return(&modem_info)); |
| 395 | EXPECT_CALL(modem_info, OnDeviceInfoAvailable(kTestDeviceName)).Times(1); |
| 396 | EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceIndex)).Times(1); |
| 397 | EXPECT_CALL(rtnl_handler_, RemoveInterfaceAddress(kTestDeviceIndex, |
| 398 | IsIPAddress(address))); |
| 399 | EXPECT_FALSE(CreateDevice( |
| 400 | kTestDeviceName, "address", kTestDeviceIndex, Technology::kCellular)); |
| 401 | } |
| 402 | |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 403 | TEST_F(DeviceInfoTest, CreateDeviceWiMax) { |
| 404 | IPAddress address = CreateInterfaceAddress(); |
| 405 | |
| 406 | // A WiMax device should be offered to WiMaxProvider. |
| 407 | StrictMock<MockWiMaxProvider> wimax_provider; |
| 408 | EXPECT_CALL(manager_, wimax_provider()).WillOnce(Return(&wimax_provider)); |
| 409 | EXPECT_CALL(wimax_provider, OnDeviceInfoAvailable(kTestDeviceName)).Times(1); |
| 410 | EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceIndex)).Times(1); |
| 411 | EXPECT_CALL(rtnl_handler_, RemoveInterfaceAddress(kTestDeviceIndex, |
| 412 | IsIPAddress(address))); |
Ben Chan | 4b28586 | 2012-10-10 22:52:16 -0700 | [diff] [blame] | 413 | device_info_.infos_[kTestDeviceIndex].mac_address = |
| 414 | ByteString(kTestMACAddress, sizeof(kTestMACAddress)); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 415 | EXPECT_FALSE(CreateDevice( |
| 416 | kTestDeviceName, "address", kTestDeviceIndex, Technology::kWiMax)); |
Ben Chan | 4b28586 | 2012-10-10 22:52:16 -0700 | [diff] [blame] | 417 | // The MAC address is clear such that it is obtained via |
| 418 | // GetMACAddressFromKernel() instead. |
| 419 | EXPECT_TRUE(device_info_.infos_[kTestDeviceIndex].mac_address.IsEmpty()); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 420 | } |
| 421 | |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 422 | TEST_F(DeviceInfoTest, CreateDeviceEthernet) { |
| 423 | IPAddress address = CreateInterfaceAddress(); |
| 424 | |
| 425 | // An Ethernet device should cause routes and addresses to be flushed. |
| 426 | EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceIndex)).Times(1); |
| 427 | EXPECT_CALL(rtnl_handler_, RemoveInterfaceAddress(kTestDeviceIndex, |
| 428 | IsIPAddress(address))); |
| 429 | DeviceRefPtr device = CreateDevice( |
| 430 | kTestDeviceName, "address", kTestDeviceIndex, Technology::kEthernet); |
| 431 | EXPECT_TRUE(device); |
| 432 | Mock::VerifyAndClearExpectations(&routing_table_); |
| 433 | Mock::VerifyAndClearExpectations(&rtnl_handler_); |
| 434 | |
| 435 | // The Ethernet device destructor notifies the manager. |
| 436 | EXPECT_CALL(manager_, UpdateEnabledTechnologies()).Times(1); |
| 437 | device = NULL; |
| 438 | } |
| 439 | |
| 440 | TEST_F(DeviceInfoTest, CreateDeviceVirtioEthernet) { |
| 441 | IPAddress address = CreateInterfaceAddress(); |
| 442 | |
| 443 | // VirtioEthernet is identical to Ethernet from the perspective of this test. |
| 444 | EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceIndex)).Times(1); |
| 445 | EXPECT_CALL(rtnl_handler_, RemoveInterfaceAddress(kTestDeviceIndex, |
| 446 | IsIPAddress(address))); |
| 447 | DeviceRefPtr device = CreateDevice( |
| 448 | kTestDeviceName, "address", kTestDeviceIndex, |
| 449 | Technology::kVirtioEthernet); |
| 450 | EXPECT_TRUE(device); |
| 451 | Mock::VerifyAndClearExpectations(&routing_table_); |
| 452 | Mock::VerifyAndClearExpectations(&rtnl_handler_); |
| 453 | |
| 454 | // The Ethernet device destructor notifies the manager. |
| 455 | EXPECT_CALL(manager_, UpdateEnabledTechnologies()).Times(1); |
| 456 | device = NULL; |
| 457 | } |
| 458 | |
Paul Stewart | 2ddf2c6 | 2013-04-16 09:47:34 -0700 | [diff] [blame] | 459 | MATCHER_P(IsGetInterfaceMessage, index, "") { |
| 460 | if (arg->message_type() != Nl80211Message::GetMessageType()) { |
| 461 | return false; |
| 462 | } |
| 463 | const Nl80211Message *msg = reinterpret_cast<const Nl80211Message *>(arg); |
| 464 | if (msg->command() != NL80211_CMD_GET_INTERFACE) { |
| 465 | return false; |
| 466 | } |
| 467 | uint32_t interface_index; |
| 468 | if (!msg->const_attributes()->GetU32AttributeValue(NL80211_ATTR_IFINDEX, |
| 469 | &interface_index)) { |
| 470 | return false; |
| 471 | } |
| 472 | // kInterfaceIndex is signed, but the attribute as handed from the kernel |
| 473 | // is unsigned. We're silently casting it away with this assignment. |
| 474 | uint32_t test_interface_index = index; |
| 475 | return interface_index == test_interface_index; |
| 476 | } |
| 477 | |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 478 | TEST_F(DeviceInfoTest, CreateDeviceWiFi) { |
| 479 | IPAddress address = CreateInterfaceAddress(); |
| 480 | |
| 481 | // WiFi looks a lot like Ethernet too. |
Paul Stewart | 2ddf2c6 | 2013-04-16 09:47:34 -0700 | [diff] [blame] | 482 | EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceIndex)); |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 483 | EXPECT_CALL(rtnl_handler_, RemoveInterfaceAddress(kTestDeviceIndex, |
| 484 | IsIPAddress(address))); |
Paul Stewart | 2ddf2c6 | 2013-04-16 09:47:34 -0700 | [diff] [blame] | 485 | |
| 486 | // Set the nl80211 message type to some non-default value. |
| 487 | Nl80211Message::SetMessageType(1234); |
| 488 | |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 489 | EXPECT_CALL( |
| 490 | netlink_manager_, |
| 491 | SendNl80211Message(IsGetInterfaceMessage(kTestDeviceIndex), _, _)); |
Paul Stewart | 2ddf2c6 | 2013-04-16 09:47:34 -0700 | [diff] [blame] | 492 | EXPECT_FALSE(CreateDevice( |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 493 | kTestDeviceName, "address", kTestDeviceIndex, Technology::kWifi)); |
| 494 | } |
| 495 | |
| 496 | TEST_F(DeviceInfoTest, CreateDeviceTunnelAccepted) { |
| 497 | IPAddress address = CreateInterfaceAddress(); |
| 498 | |
| 499 | // A VPN device should be offered to VPNProvider. |
Darin Petkov | c3505a5 | 2013-03-18 15:13:29 +0100 | [diff] [blame] | 500 | MockVPNProvider *vpn_provider = new StrictMock<MockVPNProvider>; |
| 501 | SetVPNProvider(vpn_provider); |
| 502 | EXPECT_CALL(*vpn_provider, |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 503 | OnDeviceInfoAvailable(kTestDeviceName, kTestDeviceIndex)) |
| 504 | .WillOnce(Return(true)); |
| 505 | EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceIndex)).Times(1); |
| 506 | EXPECT_CALL(rtnl_handler_, RemoveInterfaceAddress(kTestDeviceIndex, |
| 507 | IsIPAddress(address))); |
| 508 | EXPECT_CALL(rtnl_handler_, RemoveInterface(_)).Times(0); |
| 509 | EXPECT_FALSE(CreateDevice( |
| 510 | kTestDeviceName, "address", kTestDeviceIndex, Technology::kTunnel)); |
| 511 | } |
| 512 | |
| 513 | TEST_F(DeviceInfoTest, CreateDeviceTunnelRejected) { |
| 514 | IPAddress address = CreateInterfaceAddress(); |
| 515 | |
| 516 | // A VPN device should be offered to VPNProvider. |
Darin Petkov | c3505a5 | 2013-03-18 15:13:29 +0100 | [diff] [blame] | 517 | MockVPNProvider *vpn_provider = new StrictMock<MockVPNProvider>; |
| 518 | SetVPNProvider(vpn_provider); |
| 519 | EXPECT_CALL(*vpn_provider, |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 520 | OnDeviceInfoAvailable(kTestDeviceName, kTestDeviceIndex)) |
| 521 | .WillOnce(Return(false)); |
| 522 | EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceIndex)).Times(1); |
| 523 | EXPECT_CALL(rtnl_handler_, RemoveInterfaceAddress(kTestDeviceIndex, |
| 524 | IsIPAddress(address))); |
| 525 | // Since the device was rejected by the VPNProvider, DeviceInfo will |
| 526 | // remove the interface. |
| 527 | EXPECT_CALL(rtnl_handler_, RemoveInterface(kTestDeviceIndex)).Times(1); |
| 528 | EXPECT_FALSE(CreateDevice( |
| 529 | kTestDeviceName, "address", kTestDeviceIndex, Technology::kTunnel)); |
| 530 | } |
| 531 | |
| 532 | TEST_F(DeviceInfoTest, CreateDevicePPP) { |
| 533 | IPAddress address = CreateInterfaceAddress(); |
| 534 | |
| 535 | // A VPN device should be offered to VPNProvider. |
Darin Petkov | c3505a5 | 2013-03-18 15:13:29 +0100 | [diff] [blame] | 536 | MockVPNProvider *vpn_provider = new StrictMock<MockVPNProvider>; |
| 537 | SetVPNProvider(vpn_provider); |
| 538 | EXPECT_CALL(*vpn_provider, |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 539 | OnDeviceInfoAvailable(kTestDeviceName, kTestDeviceIndex)) |
| 540 | .WillOnce(Return(false)); |
| 541 | EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceIndex)).Times(1); |
| 542 | EXPECT_CALL(rtnl_handler_, RemoveInterfaceAddress(kTestDeviceIndex, |
| 543 | IsIPAddress(address))); |
| 544 | // We do not remove PPP interfaces even if the provider does not accept it. |
| 545 | EXPECT_CALL(rtnl_handler_, RemoveInterface(_)).Times(0); |
| 546 | EXPECT_FALSE(CreateDevice( |
| 547 | kTestDeviceName, "address", kTestDeviceIndex, Technology::kPPP)); |
| 548 | } |
| 549 | |
| 550 | TEST_F(DeviceInfoTest, CreateDeviceLoopback) { |
| 551 | // A loopback device should be brought up, and nothing else done to it. |
| 552 | EXPECT_CALL(routing_table_, FlushRoutes(_)).Times(0); |
| 553 | EXPECT_CALL(rtnl_handler_, RemoveInterfaceAddress(_, _)).Times(0); |
| 554 | EXPECT_CALL(rtnl_handler_, |
| 555 | SetInterfaceFlags(kTestDeviceIndex, IFF_UP, IFF_UP)).Times(1); |
| 556 | EXPECT_FALSE(CreateDevice( |
| 557 | kTestDeviceName, "address", kTestDeviceIndex, Technology::kLoopback)); |
| 558 | } |
| 559 | |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 560 | TEST_F(DeviceInfoTest, CreateDeviceCDCEthernet) { |
Ben Chan | 4eb4ddf | 2013-06-20 22:16:56 -0700 | [diff] [blame] | 561 | // A cdc_ether / cdc_ncm device should be postponed to a task. |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 562 | EXPECT_CALL(manager_, modem_info()).Times(0); |
| 563 | EXPECT_CALL(routing_table_, FlushRoutes(_)).Times(0); |
| 564 | EXPECT_CALL(rtnl_handler_, RemoveInterfaceAddress(_, _)).Times(0); |
| 565 | EXPECT_CALL(dispatcher_, |
Paul Stewart | 1ac4e84 | 2012-07-10 12:58:12 -0700 | [diff] [blame] | 566 | PostDelayedTask(_, GetDelayedDeviceCreationMilliseconds())); |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 567 | EXPECT_TRUE(GetDelayedDevices().empty()); |
| 568 | EXPECT_FALSE(CreateDevice( |
| 569 | kTestDeviceName, "address", kTestDeviceIndex, Technology::kCDCEthernet)); |
| 570 | EXPECT_FALSE(GetDelayedDevices().empty()); |
| 571 | EXPECT_EQ(1, GetDelayedDevices().size()); |
| 572 | EXPECT_EQ(kTestDeviceIndex, *GetDelayedDevices().begin()); |
| 573 | } |
| 574 | |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 575 | TEST_F(DeviceInfoTest, CreateDeviceUnknown) { |
| 576 | IPAddress address = CreateInterfaceAddress(); |
| 577 | |
| 578 | // An unknown (blacklisted, unhandled, etc) device won't be flushed or |
| 579 | // registered. |
| 580 | EXPECT_CALL(routing_table_, FlushRoutes(_)).Times(0); |
| 581 | EXPECT_CALL(rtnl_handler_, RemoveInterfaceAddress(_, _)).Times(0); |
| 582 | EXPECT_TRUE(CreateDevice( |
| 583 | kTestDeviceName, "address", kTestDeviceIndex, Technology::kUnknown)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 584 | } |
| 585 | |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 586 | TEST_F(DeviceInfoTest, DeviceBlackList) { |
| 587 | device_info_.AddDeviceToBlackList(kTestDeviceName); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 588 | scoped_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd)); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 589 | SendMessageToDeviceInfo(*message); |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 590 | |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 591 | DeviceRefPtr device = device_info_.GetDevice(kTestDeviceIndex); |
| 592 | ASSERT_TRUE(device.get()); |
Joshua Kroll | da79862 | 2012-06-05 12:30:48 -0700 | [diff] [blame] | 593 | EXPECT_TRUE(device->technology() == Technology::kBlacklisted); |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 596 | TEST_F(DeviceInfoTest, DeviceAddressList) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 597 | scoped_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd)); |
| 598 | SendMessageToDeviceInfo(*message); |
| 599 | |
| 600 | vector<DeviceInfo::AddressData> addresses; |
| 601 | EXPECT_TRUE(device_info_.GetAddresses(kTestDeviceIndex, &addresses)); |
| 602 | EXPECT_TRUE(addresses.empty()); |
| 603 | |
| 604 | // Add an address to the device address list |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 605 | IPAddress ip_address0(IPAddress::kFamilyIPv4); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 606 | EXPECT_TRUE(ip_address0.SetAddressFromString(kTestIPAddress0)); |
| 607 | ip_address0.set_prefix(kTestIPAddressPrefix0); |
| 608 | message.reset(BuildAddressMessage(RTNLMessage::kModeAdd, ip_address0, 0, 0)); |
| 609 | SendMessageToDeviceInfo(*message); |
| 610 | EXPECT_TRUE(device_info_.GetAddresses(kTestDeviceIndex, &addresses)); |
| 611 | EXPECT_EQ(1, addresses.size()); |
| 612 | EXPECT_TRUE(ip_address0.Equals(addresses[0].address)); |
| 613 | |
| 614 | // Re-adding the same address shouldn't cause the address list to change |
| 615 | SendMessageToDeviceInfo(*message); |
| 616 | EXPECT_TRUE(device_info_.GetAddresses(kTestDeviceIndex, &addresses)); |
| 617 | EXPECT_EQ(1, addresses.size()); |
| 618 | EXPECT_TRUE(ip_address0.Equals(addresses[0].address)); |
| 619 | |
| 620 | // Adding a new address should expand the list |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 621 | IPAddress ip_address1(IPAddress::kFamilyIPv6); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 622 | EXPECT_TRUE(ip_address1.SetAddressFromString(kTestIPAddress1)); |
| 623 | ip_address1.set_prefix(kTestIPAddressPrefix1); |
| 624 | message.reset(BuildAddressMessage(RTNLMessage::kModeAdd, ip_address1, 0, 0)); |
| 625 | SendMessageToDeviceInfo(*message); |
| 626 | EXPECT_TRUE(device_info_.GetAddresses(kTestDeviceIndex, &addresses)); |
| 627 | EXPECT_EQ(2, addresses.size()); |
| 628 | EXPECT_TRUE(ip_address0.Equals(addresses[0].address)); |
| 629 | EXPECT_TRUE(ip_address1.Equals(addresses[1].address)); |
| 630 | |
| 631 | // Deleting an address should reduce the list |
| 632 | message.reset(BuildAddressMessage(RTNLMessage::kModeDelete, |
| 633 | ip_address0, |
| 634 | 0, |
| 635 | 0)); |
| 636 | SendMessageToDeviceInfo(*message); |
| 637 | EXPECT_TRUE(device_info_.GetAddresses(kTestDeviceIndex, &addresses)); |
| 638 | EXPECT_EQ(1, addresses.size()); |
| 639 | EXPECT_TRUE(ip_address1.Equals(addresses[0].address)); |
| 640 | |
| 641 | // Delete last item |
| 642 | message.reset(BuildAddressMessage(RTNLMessage::kModeDelete, |
| 643 | ip_address1, |
| 644 | 0, |
| 645 | 0)); |
| 646 | SendMessageToDeviceInfo(*message); |
| 647 | EXPECT_TRUE(device_info_.GetAddresses(kTestDeviceIndex, &addresses)); |
| 648 | EXPECT_TRUE(addresses.empty()); |
| 649 | |
| 650 | // Delete device |
| 651 | message.reset(BuildLinkMessage(RTNLMessage::kModeDelete)); |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 652 | EXPECT_CALL(manager_, DeregisterDevice(_)).Times(1); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 653 | SendMessageToDeviceInfo(*message); |
| 654 | |
| 655 | // Should be able to handle message for interface that doesn't exist |
| 656 | message.reset(BuildAddressMessage(RTNLMessage::kModeAdd, ip_address0, 0, 0)); |
| 657 | SendMessageToDeviceInfo(*message); |
| 658 | EXPECT_FALSE(device_info_.GetDevice(kTestDeviceIndex).get()); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | TEST_F(DeviceInfoTest, FlushAddressList) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 662 | scoped_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd)); |
| 663 | SendMessageToDeviceInfo(*message); |
| 664 | |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 665 | IPAddress address1(IPAddress::kFamilyIPv6); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 666 | EXPECT_TRUE(address1.SetAddressFromString(kTestIPAddress1)); |
| 667 | address1.set_prefix(kTestIPAddressPrefix1); |
| 668 | message.reset(BuildAddressMessage(RTNLMessage::kModeAdd, |
| 669 | address1, |
| 670 | 0, |
| 671 | RT_SCOPE_UNIVERSE)); |
| 672 | SendMessageToDeviceInfo(*message); |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 673 | IPAddress address2(IPAddress::kFamilyIPv6); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 674 | EXPECT_TRUE(address2.SetAddressFromString(kTestIPAddress2)); |
| 675 | message.reset(BuildAddressMessage(RTNLMessage::kModeAdd, |
| 676 | address2, |
| 677 | IFA_F_TEMPORARY, |
| 678 | RT_SCOPE_UNIVERSE)); |
| 679 | SendMessageToDeviceInfo(*message); |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 680 | IPAddress address3(IPAddress::kFamilyIPv6); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 681 | EXPECT_TRUE(address3.SetAddressFromString(kTestIPAddress3)); |
| 682 | message.reset(BuildAddressMessage(RTNLMessage::kModeAdd, |
| 683 | address3, |
| 684 | 0, |
| 685 | RT_SCOPE_LINK)); |
| 686 | SendMessageToDeviceInfo(*message); |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 687 | IPAddress address4(IPAddress::kFamilyIPv6); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 688 | EXPECT_TRUE(address4.SetAddressFromString(kTestIPAddress4)); |
| 689 | message.reset(BuildAddressMessage(RTNLMessage::kModeAdd, |
| 690 | address4, |
| 691 | IFA_F_PERMANENT, |
| 692 | RT_SCOPE_UNIVERSE)); |
| 693 | SendMessageToDeviceInfo(*message); |
| 694 | |
| 695 | // DeviceInfo now has 4 addresses associated with it, but only two of |
| 696 | // them are valid for flush. |
| 697 | EXPECT_CALL(rtnl_handler_, RemoveInterfaceAddress(kTestDeviceIndex, |
| 698 | IsIPAddress(address1))); |
| 699 | EXPECT_CALL(rtnl_handler_, RemoveInterfaceAddress(kTestDeviceIndex, |
| 700 | IsIPAddress(address2))); |
| 701 | device_info_.FlushAddresses(kTestDeviceIndex); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 704 | TEST_F(DeviceInfoTest, HasOtherAddress) { |
| 705 | scoped_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd)); |
| 706 | SendMessageToDeviceInfo(*message); |
| 707 | |
| 708 | IPAddress address0(IPAddress::kFamilyIPv4); |
| 709 | EXPECT_TRUE(address0.SetAddressFromString(kTestIPAddress0)); |
| 710 | |
| 711 | // There are no addresses on this interface. |
| 712 | EXPECT_FALSE(device_info_.HasOtherAddress(kTestDeviceIndex, address0)); |
| 713 | |
| 714 | message.reset(BuildAddressMessage(RTNLMessage::kModeAdd, |
| 715 | address0, |
| 716 | 0, |
| 717 | RT_SCOPE_UNIVERSE)); |
| 718 | SendMessageToDeviceInfo(*message); |
| 719 | |
| 720 | IPAddress address1(IPAddress::kFamilyIPv6); |
| 721 | EXPECT_TRUE(address1.SetAddressFromString(kTestIPAddress1)); |
| 722 | address1.set_prefix(kTestIPAddressPrefix1); |
| 723 | message.reset(BuildAddressMessage(RTNLMessage::kModeAdd, |
| 724 | address1, |
| 725 | 0, |
| 726 | RT_SCOPE_LINK)); |
| 727 | SendMessageToDeviceInfo(*message); |
| 728 | |
| 729 | IPAddress address2(IPAddress::kFamilyIPv6); |
| 730 | EXPECT_TRUE(address2.SetAddressFromString(kTestIPAddress2)); |
| 731 | message.reset(BuildAddressMessage(RTNLMessage::kModeAdd, |
| 732 | address2, |
| 733 | IFA_F_TEMPORARY, |
| 734 | RT_SCOPE_UNIVERSE)); |
| 735 | SendMessageToDeviceInfo(*message); |
| 736 | |
| 737 | IPAddress address3(IPAddress::kFamilyIPv6); |
| 738 | EXPECT_TRUE(address3.SetAddressFromString(kTestIPAddress3)); |
| 739 | |
| 740 | // The only IPv6 addresses on this interface are either flagged as |
| 741 | // temporary, or they are not universally scoped. |
| 742 | EXPECT_FALSE(device_info_.HasOtherAddress(kTestDeviceIndex, address3)); |
| 743 | |
| 744 | |
| 745 | message.reset(BuildAddressMessage(RTNLMessage::kModeAdd, |
| 746 | address3, |
| 747 | 0, |
| 748 | RT_SCOPE_UNIVERSE)); |
| 749 | SendMessageToDeviceInfo(*message); |
| 750 | |
| 751 | // address0 is on this interface. |
| 752 | EXPECT_FALSE(device_info_.HasOtherAddress(kTestDeviceIndex, address0)); |
| 753 | // address1 is on this interface. |
| 754 | EXPECT_FALSE(device_info_.HasOtherAddress(kTestDeviceIndex, address1)); |
| 755 | // address2 is on this interface. |
| 756 | EXPECT_FALSE(device_info_.HasOtherAddress(kTestDeviceIndex, address2)); |
| 757 | // address3 is on this interface. |
| 758 | EXPECT_FALSE(device_info_.HasOtherAddress(kTestDeviceIndex, address3)); |
| 759 | |
| 760 | IPAddress address4(IPAddress::kFamilyIPv6); |
| 761 | EXPECT_TRUE(address4.SetAddressFromString(kTestIPAddress4)); |
| 762 | |
| 763 | // address4 is not on this interface, but address3 is, and is a qualified |
| 764 | // IPv6 address. |
| 765 | EXPECT_TRUE(device_info_.HasOtherAddress(kTestDeviceIndex, address4)); |
| 766 | |
| 767 | message.reset(BuildAddressMessage(RTNLMessage::kModeAdd, |
| 768 | address4, |
| 769 | IFA_F_PERMANENT, |
| 770 | RT_SCOPE_UNIVERSE)); |
| 771 | SendMessageToDeviceInfo(*message); |
| 772 | |
| 773 | // address4 is now on this interface. |
| 774 | EXPECT_FALSE(device_info_.HasOtherAddress(kTestDeviceIndex, address4)); |
| 775 | |
| 776 | IPAddress address5(IPAddress::kFamilyIPv4); |
| 777 | EXPECT_TRUE(address5.SetAddressFromString(kTestIPAddress5)); |
| 778 | // address5 is not on this interface, but address0 is. |
| 779 | EXPECT_TRUE(device_info_.HasOtherAddress(kTestDeviceIndex, address5)); |
| 780 | |
| 781 | message.reset(BuildAddressMessage(RTNLMessage::kModeAdd, |
| 782 | address5, |
| 783 | IFA_F_PERMANENT, |
| 784 | RT_SCOPE_UNIVERSE)); |
| 785 | SendMessageToDeviceInfo(*message); |
| 786 | |
| 787 | // address5 is now on this interface. |
| 788 | EXPECT_FALSE(device_info_.HasOtherAddress(kTestDeviceIndex, address5)); |
| 789 | } |
| 790 | |
Thieu Le | b27beee | 2012-04-20 09:19:06 -0700 | [diff] [blame] | 791 | TEST_F(DeviceInfoTest, HasSubdir) { |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 792 | base::ScopedTempDir temp_dir; |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 793 | EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); |
Thieu Le | b27beee | 2012-04-20 09:19:06 -0700 | [diff] [blame] | 794 | EXPECT_TRUE(file_util::CreateDirectory(temp_dir.path().Append("child1"))); |
| 795 | FilePath child2 = temp_dir.path().Append("child2"); |
| 796 | EXPECT_TRUE(file_util::CreateDirectory(child2)); |
| 797 | FilePath grandchild = child2.Append("grandchild"); |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 798 | EXPECT_TRUE(file_util::CreateDirectory(grandchild)); |
| 799 | EXPECT_TRUE(file_util::CreateDirectory(grandchild.Append("greatgrandchild"))); |
Thieu Le | b27beee | 2012-04-20 09:19:06 -0700 | [diff] [blame] | 800 | EXPECT_TRUE(DeviceInfo::HasSubdir(temp_dir.path(), |
| 801 | FilePath("grandchild"))); |
| 802 | EXPECT_TRUE(DeviceInfo::HasSubdir(temp_dir.path(), |
| 803 | FilePath("greatgrandchild"))); |
| 804 | EXPECT_FALSE(DeviceInfo::HasSubdir(temp_dir.path(), |
| 805 | FilePath("nonexistent"))); |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 806 | } |
| 807 | |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 808 | TEST_F(DeviceInfoTest, GetMACAddressFromKernelUnknownDevice) { |
| 809 | SetSockets(); |
| 810 | EXPECT_CALL(*mock_sockets_, Socket(PF_INET, SOCK_DGRAM, 0)).Times(0); |
| 811 | ByteString mac_address = |
| 812 | device_info_.GetMACAddressFromKernel(kTestDeviceIndex); |
| 813 | EXPECT_TRUE(mac_address.IsEmpty()); |
| 814 | } |
| 815 | |
| 816 | TEST_F(DeviceInfoTest, GetMACAddressFromKernelUnableToOpenSocket) { |
| 817 | SetSockets(); |
| 818 | EXPECT_CALL(*mock_sockets_, Socket(PF_INET, SOCK_DGRAM, 0)) |
| 819 | .WillOnce(Return(-1)); |
| 820 | scoped_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd)); |
| 821 | message->set_link_status(RTNLMessage::LinkStatus(0, IFF_LOWER_UP, 0)); |
| 822 | SendMessageToDeviceInfo(*message); |
| 823 | EXPECT_TRUE(device_info_.GetDevice(kTestDeviceIndex).get()); |
| 824 | ByteString mac_address = |
| 825 | device_info_.GetMACAddressFromKernel(kTestDeviceIndex); |
| 826 | EXPECT_TRUE(mac_address.IsEmpty()); |
| 827 | } |
| 828 | |
| 829 | TEST_F(DeviceInfoTest, GetMACAddressFromKernelIoctlFails) { |
| 830 | SetSockets(); |
| 831 | const int kFd = 99; |
| 832 | EXPECT_CALL(*mock_sockets_, Socket(PF_INET, SOCK_DGRAM, 0)) |
| 833 | .WillOnce(Return(kFd)); |
| 834 | EXPECT_CALL(*mock_sockets_, Ioctl(kFd, SIOCGIFHWADDR, NotNull())) |
| 835 | .WillOnce(Return(-1)); |
| 836 | EXPECT_CALL(*mock_sockets_, Close(kFd)); |
| 837 | |
| 838 | scoped_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd)); |
| 839 | message->set_link_status(RTNLMessage::LinkStatus(0, IFF_LOWER_UP, 0)); |
| 840 | SendMessageToDeviceInfo(*message); |
| 841 | EXPECT_TRUE(device_info_.GetDevice(kTestDeviceIndex).get()); |
| 842 | |
| 843 | ByteString mac_address = |
| 844 | device_info_.GetMACAddressFromKernel(kTestDeviceIndex); |
| 845 | EXPECT_TRUE(mac_address.IsEmpty()); |
| 846 | } |
| 847 | |
| 848 | MATCHER_P2(IfreqEquals, ifindex, ifname, "") { |
| 849 | const struct ifreq *const ifr = static_cast<struct ifreq *>(arg); |
| 850 | return (ifr != NULL) && |
| 851 | (ifr->ifr_ifindex == ifindex) && |
| 852 | (strcmp(ifname, ifr->ifr_name) == 0); |
| 853 | } |
| 854 | |
| 855 | ACTION_P(SetIfreq, ifr) { |
| 856 | struct ifreq *const ifr_arg = static_cast<struct ifreq *>(arg2); |
| 857 | *ifr_arg = ifr; |
| 858 | } |
| 859 | |
| 860 | TEST_F(DeviceInfoTest, GetMACAddressFromKernel) { |
| 861 | SetSockets(); |
| 862 | const int kFd = 99; |
| 863 | struct ifreq ifr; |
Han Shen | fc34925 | 2012-08-30 11:36:04 -0700 | [diff] [blame] | 864 | static uint8_t kMacAddress[] = {0x00, 0x01, 0x02, 0xaa, 0xbb, 0xcc}; |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 865 | memcpy(ifr.ifr_hwaddr.sa_data, kMacAddress, sizeof(kMacAddress)); |
| 866 | EXPECT_CALL(*mock_sockets_, Socket(PF_INET, SOCK_DGRAM, 0)) |
| 867 | .WillOnce(Return(kFd)); |
| 868 | EXPECT_CALL(*mock_sockets_, |
| 869 | Ioctl(kFd, SIOCGIFHWADDR, |
| 870 | IfreqEquals(kTestDeviceIndex, kTestDeviceName))) |
| 871 | .WillOnce(DoAll(SetIfreq(ifr), Return(0))); |
| 872 | EXPECT_CALL(*mock_sockets_, Close(kFd)); |
| 873 | |
| 874 | scoped_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd)); |
| 875 | message->set_link_status(RTNLMessage::LinkStatus(0, IFF_LOWER_UP, 0)); |
| 876 | SendMessageToDeviceInfo(*message); |
| 877 | EXPECT_TRUE(device_info_.GetDevice(kTestDeviceIndex).get()); |
| 878 | |
| 879 | ByteString mac_address = |
| 880 | device_info_.GetMACAddressFromKernel(kTestDeviceIndex); |
| 881 | EXPECT_THAT(kMacAddress, |
| 882 | ElementsAreArray(mac_address.GetData(), sizeof(kMacAddress))); |
| 883 | } |
| 884 | |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 885 | class DeviceInfoTechnologyTest : public DeviceInfoTest { |
| 886 | public: |
Jason Glasgow | abc5403 | 2012-04-20 16:08:32 -0400 | [diff] [blame] | 887 | DeviceInfoTechnologyTest() |
| 888 | : DeviceInfoTest(), |
| 889 | test_device_name_(kTestDeviceName) {} |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 890 | virtual ~DeviceInfoTechnologyTest() {} |
| 891 | |
| 892 | virtual void SetUp() { |
| 893 | temp_dir_.CreateUniqueTempDir(); |
| 894 | device_info_root_ = temp_dir_.path().Append("sys/class/net"); |
| 895 | device_info_.device_info_root_ = device_info_root_; |
| 896 | // Most tests require that the uevent file exist. |
| 897 | CreateInfoFile("uevent", "xxx"); |
| 898 | } |
| 899 | |
| 900 | Technology::Identifier GetDeviceTechnology() { |
Jason Glasgow | abc5403 | 2012-04-20 16:08:32 -0400 | [diff] [blame] | 901 | return device_info_.GetDeviceTechnology(test_device_name_); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 902 | } |
| 903 | FilePath GetInfoPath(const string &name); |
| 904 | void CreateInfoFile(const string &name, const string &contents); |
| 905 | void CreateInfoSymLink(const string &name, const string &contents); |
Jason Glasgow | abc5403 | 2012-04-20 16:08:32 -0400 | [diff] [blame] | 906 | void SetDeviceName(const string &name) { |
| 907 | test_device_name_ = name; |
| 908 | SetUp(); |
| 909 | } |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 910 | |
| 911 | protected: |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 912 | base::ScopedTempDir temp_dir_; |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 913 | FilePath device_info_root_; |
Jason Glasgow | abc5403 | 2012-04-20 16:08:32 -0400 | [diff] [blame] | 914 | string test_device_name_; |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 915 | }; |
| 916 | |
| 917 | FilePath DeviceInfoTechnologyTest::GetInfoPath(const string &name) { |
Jason Glasgow | abc5403 | 2012-04-20 16:08:32 -0400 | [diff] [blame] | 918 | return device_info_root_.Append(test_device_name_).Append(name); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | void DeviceInfoTechnologyTest::CreateInfoFile(const string &name, |
| 922 | const string &contents) { |
| 923 | FilePath info_path = GetInfoPath(name); |
| 924 | EXPECT_TRUE(file_util::CreateDirectory(info_path.DirName())); |
| 925 | string contents_newline(contents + "\n"); |
| 926 | EXPECT_TRUE(file_util::WriteFile(info_path, contents_newline.c_str(), |
| 927 | contents_newline.size())); |
| 928 | } |
| 929 | |
| 930 | void DeviceInfoTechnologyTest::CreateInfoSymLink(const string &name, |
| 931 | const string &contents) { |
| 932 | FilePath info_path = GetInfoPath(name); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 933 | EXPECT_TRUE(file_util::CreateDirectory(info_path.DirName())); |
| 934 | EXPECT_TRUE(file_util::CreateSymbolicLink(FilePath(contents), info_path)); |
| 935 | } |
| 936 | |
| 937 | TEST_F(DeviceInfoTechnologyTest, Unknown) { |
| 938 | EXPECT_EQ(Technology::kUnknown, GetDeviceTechnology()); |
| 939 | // Should still be unknown even without a uevent file. |
Ben Chan | 4eb4ddf | 2013-06-20 22:16:56 -0700 | [diff] [blame] | 940 | EXPECT_TRUE(file_util::Delete(GetInfoPath("uevent"), false)); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 941 | EXPECT_EQ(Technology::kUnknown, GetDeviceTechnology()); |
| 942 | } |
| 943 | |
| 944 | TEST_F(DeviceInfoTechnologyTest, Loopback) { |
| 945 | CreateInfoFile("type", base::IntToString(ARPHRD_LOOPBACK)); |
| 946 | EXPECT_EQ(Technology::kLoopback, GetDeviceTechnology()); |
| 947 | } |
| 948 | |
| 949 | TEST_F(DeviceInfoTechnologyTest, PPP) { |
| 950 | CreateInfoFile("type", base::IntToString(ARPHRD_PPP)); |
| 951 | EXPECT_EQ(Technology::kPPP, GetDeviceTechnology()); |
| 952 | } |
| 953 | |
| 954 | TEST_F(DeviceInfoTechnologyTest, Tunnel) { |
| 955 | CreateInfoFile("tun_flags", base::IntToString(IFF_TUN)); |
| 956 | EXPECT_EQ(Technology::kTunnel, GetDeviceTechnology()); |
| 957 | } |
| 958 | |
| 959 | TEST_F(DeviceInfoTechnologyTest, WiFi) { |
| 960 | CreateInfoFile("uevent", "DEVTYPE=wlan"); |
| 961 | EXPECT_EQ(Technology::kWifi, GetDeviceTechnology()); |
| 962 | CreateInfoFile("uevent", "foo\nDEVTYPE=wlan"); |
| 963 | EXPECT_EQ(Technology::kWifi, GetDeviceTechnology()); |
| 964 | CreateInfoFile("type", base::IntToString(ARPHRD_IEEE80211_RADIOTAP)); |
| 965 | EXPECT_EQ(Technology::kWiFiMonitor, GetDeviceTechnology()); |
| 966 | } |
| 967 | |
| 968 | TEST_F(DeviceInfoTechnologyTest, Ethernet) { |
| 969 | CreateInfoSymLink("device/driver", "xxx"); |
| 970 | EXPECT_EQ(Technology::kEthernet, GetDeviceTechnology()); |
| 971 | } |
| 972 | |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 973 | TEST_F(DeviceInfoTechnologyTest, WiMax) { |
| 974 | CreateInfoSymLink("device/driver", "gdm_wimax"); |
| 975 | EXPECT_EQ(Technology::kWiMax, GetDeviceTechnology()); |
| 976 | } |
| 977 | |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 978 | TEST_F(DeviceInfoTechnologyTest, CellularGobi1) { |
| 979 | CreateInfoSymLink("device/driver", "blah/foo/gobi"); |
| 980 | EXPECT_EQ(Technology::kCellular, GetDeviceTechnology()); |
| 981 | } |
| 982 | |
| 983 | TEST_F(DeviceInfoTechnologyTest, CellularGobi2) { |
| 984 | CreateInfoSymLink("device/driver", "../GobiNet"); |
| 985 | EXPECT_EQ(Technology::kCellular, GetDeviceTechnology()); |
| 986 | } |
| 987 | |
| 988 | TEST_F(DeviceInfoTechnologyTest, QCUSB) { |
| 989 | CreateInfoSymLink("device/driver", "QCUSBNet2k"); |
| 990 | EXPECT_EQ(Technology::kCellular, GetDeviceTechnology()); |
| 991 | } |
| 992 | |
Ben Chan | 0f90e0b | 2013-06-26 23:37:16 -0700 | [diff] [blame] | 993 | TEST_F(DeviceInfoTechnologyTest, CellularCdcMbim) { |
| 994 | CreateInfoSymLink("device/driver", "cdc_mbim"); |
| 995 | EXPECT_EQ(Technology::kCellular, GetDeviceTechnology()); |
| 996 | } |
| 997 | |
Ben Chan | 226d46a | 2012-10-11 00:22:17 -0700 | [diff] [blame] | 998 | TEST_F(DeviceInfoTechnologyTest, CellularQmiWwan) { |
| 999 | CreateInfoSymLink("device/driver", "qmi_wwan"); |
| 1000 | EXPECT_EQ(Technology::kCellular, GetDeviceTechnology()); |
| 1001 | } |
| 1002 | |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 1003 | // Modem with absolute driver path with top-level tty file: |
| 1004 | // /sys/class/net/dev0/device -> /sys/devices/virtual/0/00 |
Ben Chan | 4eb4ddf | 2013-06-20 22:16:56 -0700 | [diff] [blame] | 1005 | // /sys/devices/virtual/0/00/driver -> /drivers/cdc_ether or /drivers/cdc_ncm |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 1006 | // /sys/devices/virtual/0/01/tty [empty directory] |
Ben Chan | 4eb4ddf | 2013-06-20 22:16:56 -0700 | [diff] [blame] | 1007 | TEST_F(DeviceInfoTechnologyTest, CDCEthernetModem1) { |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 1008 | FilePath device_root(temp_dir_.path().Append("sys/devices/virtual/0")); |
| 1009 | FilePath device_path(device_root.Append("00")); |
Ben Chan | 4eb4ddf | 2013-06-20 22:16:56 -0700 | [diff] [blame] | 1010 | FilePath driver_symlink(device_path.Append("driver")); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 1011 | EXPECT_TRUE(file_util::CreateDirectory(device_path)); |
| 1012 | CreateInfoSymLink("device", device_path.value()); |
| 1013 | EXPECT_TRUE(file_util::CreateSymbolicLink(FilePath("/drivers/cdc_ether"), |
Ben Chan | 4eb4ddf | 2013-06-20 22:16:56 -0700 | [diff] [blame] | 1014 | driver_symlink)); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 1015 | EXPECT_TRUE(file_util::CreateDirectory(device_root.Append("01/tty"))); |
| 1016 | EXPECT_EQ(Technology::kCellular, GetDeviceTechnology()); |
Ben Chan | 4eb4ddf | 2013-06-20 22:16:56 -0700 | [diff] [blame] | 1017 | |
| 1018 | EXPECT_TRUE(file_util::Delete(driver_symlink, false)); |
| 1019 | EXPECT_TRUE(file_util::CreateSymbolicLink(FilePath("/drivers/cdc_ncm"), |
| 1020 | driver_symlink)); |
| 1021 | EXPECT_EQ(Technology::kCellular, GetDeviceTechnology()); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | // Modem with relative driver path with top-level tty file. |
| 1025 | // /sys/class/net/dev0/device -> ../../../device_dir/0/00 |
Ben Chan | 4eb4ddf | 2013-06-20 22:16:56 -0700 | [diff] [blame] | 1026 | // /sys/device_dir/0/00/driver -> /drivers/cdc_ether or /drivers/cdc_ncm |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 1027 | // /sys/device_dir/0/01/tty [empty directory] |
Ben Chan | 4eb4ddf | 2013-06-20 22:16:56 -0700 | [diff] [blame] | 1028 | TEST_F(DeviceInfoTechnologyTest, CDCEthernetModem2) { |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 1029 | CreateInfoSymLink("device", "../../../device_dir/0/00"); |
| 1030 | FilePath device_root(temp_dir_.path().Append("sys/device_dir/0")); |
| 1031 | FilePath device_path(device_root.Append("00")); |
Ben Chan | 4eb4ddf | 2013-06-20 22:16:56 -0700 | [diff] [blame] | 1032 | FilePath driver_symlink(device_path.Append("driver")); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 1033 | EXPECT_TRUE(file_util::CreateDirectory(device_path)); |
| 1034 | EXPECT_TRUE(file_util::CreateSymbolicLink(FilePath("/drivers/cdc_ether"), |
Ben Chan | 4eb4ddf | 2013-06-20 22:16:56 -0700 | [diff] [blame] | 1035 | driver_symlink)); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 1036 | EXPECT_TRUE(file_util::CreateDirectory(device_root.Append("01/tty"))); |
| 1037 | EXPECT_EQ(Technology::kCellular, GetDeviceTechnology()); |
Ben Chan | 4eb4ddf | 2013-06-20 22:16:56 -0700 | [diff] [blame] | 1038 | |
| 1039 | EXPECT_TRUE(file_util::Delete(driver_symlink, false)); |
| 1040 | EXPECT_TRUE(file_util::CreateSymbolicLink(FilePath("/drivers/cdc_ncm"), |
| 1041 | driver_symlink)); |
| 1042 | EXPECT_EQ(Technology::kCellular, GetDeviceTechnology()); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | // Modem with relative driver path with lower-level tty file. |
| 1046 | // /sys/class/net/dev0/device -> ../../../device_dir/0/00 |
Ben Chan | 4eb4ddf | 2013-06-20 22:16:56 -0700 | [diff] [blame] | 1047 | // /sys/device_dir/0/00/driver -> /drivers/cdc_ether or /drivers/cdc_ncm |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 1048 | // /sys/device_dir/0/01/yyy/tty [empty directory] |
Ben Chan | 4eb4ddf | 2013-06-20 22:16:56 -0700 | [diff] [blame] | 1049 | TEST_F(DeviceInfoTechnologyTest, CDCEthernetModem3) { |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 1050 | CreateInfoSymLink("device", "../../../device_dir/0/00"); |
| 1051 | FilePath device_root(temp_dir_.path().Append("sys/device_dir/0")); |
| 1052 | FilePath device_path(device_root.Append("00")); |
Ben Chan | 4eb4ddf | 2013-06-20 22:16:56 -0700 | [diff] [blame] | 1053 | FilePath driver_symlink(device_path.Append("driver")); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 1054 | EXPECT_TRUE(file_util::CreateDirectory(device_path)); |
| 1055 | EXPECT_TRUE(file_util::CreateSymbolicLink(FilePath("/drivers/cdc_ether"), |
Ben Chan | 4eb4ddf | 2013-06-20 22:16:56 -0700 | [diff] [blame] | 1056 | driver_symlink)); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 1057 | EXPECT_TRUE(file_util::CreateDirectory(device_root.Append("01/yyy/tty"))); |
| 1058 | EXPECT_EQ(Technology::kCellular, GetDeviceTechnology()); |
Ben Chan | 4eb4ddf | 2013-06-20 22:16:56 -0700 | [diff] [blame] | 1059 | |
| 1060 | EXPECT_TRUE(file_util::Delete(driver_symlink, false)); |
| 1061 | EXPECT_TRUE(file_util::CreateSymbolicLink(FilePath("/drivers/cdc_ncm"), |
| 1062 | driver_symlink)); |
| 1063 | EXPECT_EQ(Technology::kCellular, GetDeviceTechnology()); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | TEST_F(DeviceInfoTechnologyTest, CDCEtherNonModem) { |
| 1067 | CreateInfoSymLink("device", "device_dir"); |
| 1068 | CreateInfoSymLink("device_dir/driver", "cdc_ether"); |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 1069 | EXPECT_EQ(Technology::kCDCEthernet, GetDeviceTechnology()); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
Ben Chan | 4eb4ddf | 2013-06-20 22:16:56 -0700 | [diff] [blame] | 1072 | TEST_F(DeviceInfoTechnologyTest, CDCNcmNonModem) { |
| 1073 | CreateInfoSymLink("device", "device_dir"); |
| 1074 | CreateInfoSymLink("device_dir/driver", "cdc_ncm"); |
| 1075 | EXPECT_EQ(Technology::kCDCEthernet, GetDeviceTechnology()); |
| 1076 | } |
| 1077 | |
Jason Glasgow | abc5403 | 2012-04-20 16:08:32 -0400 | [diff] [blame] | 1078 | TEST_F(DeviceInfoTechnologyTest, PseudoModem) { |
| 1079 | SetDeviceName("pseudomodem"); |
| 1080 | CreateInfoSymLink("device", "device_dir"); |
| 1081 | CreateInfoSymLink("device_dir/driver", "cdc_ether"); |
| 1082 | EXPECT_EQ(Technology::kCellular, GetDeviceTechnology()); |
| 1083 | |
| 1084 | SetDeviceName("pseudomodem9"); |
| 1085 | CreateInfoSymLink("device", "device_dir"); |
| 1086 | CreateInfoSymLink("device_dir/driver", "cdc_ether"); |
| 1087 | EXPECT_EQ(Technology::kCellular, GetDeviceTechnology()); |
| 1088 | } |
| 1089 | |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 1090 | class DeviceInfoForDelayedCreationTest : public DeviceInfo { |
| 1091 | public: |
| 1092 | DeviceInfoForDelayedCreationTest(ControlInterface *control_interface, |
| 1093 | EventDispatcher *dispatcher, |
| 1094 | Metrics *metrics, |
| 1095 | Manager *manager) |
| 1096 | : DeviceInfo(control_interface, dispatcher, metrics, manager) {} |
| 1097 | MOCK_METHOD4(CreateDevice, DeviceRefPtr(const std::string &link_name, |
| 1098 | const std::string &address, |
| 1099 | int interface_index, |
| 1100 | Technology::Identifier technology)); |
| 1101 | MOCK_METHOD1(GetDeviceTechnology, |
| 1102 | Technology::Identifier(const string &iface_name)); |
| 1103 | }; |
| 1104 | |
| 1105 | class DeviceInfoDelayedCreationTest : public DeviceInfoTest { |
| 1106 | public: |
| 1107 | DeviceInfoDelayedCreationTest() |
| 1108 | : DeviceInfoTest(), |
| 1109 | test_device_info_( |
| 1110 | &control_interface_, &dispatcher_, &metrics_, &manager_) {} |
| 1111 | virtual ~DeviceInfoDelayedCreationTest() {} |
| 1112 | |
| 1113 | virtual std::set<int> &GetDelayedDevices() { |
| 1114 | return test_device_info_.delayed_devices_; |
| 1115 | } |
| 1116 | |
| 1117 | void DelayedDeviceCreationTask() { |
| 1118 | test_device_info_.DelayedDeviceCreationTask(); |
| 1119 | } |
| 1120 | |
| 1121 | void AddDelayedDevice() { |
| 1122 | scoped_ptr<RTNLMessage> message(BuildLinkMessage(RTNLMessage::kModeAdd)); |
| 1123 | EXPECT_CALL(test_device_info_, GetDeviceTechnology(kTestDeviceName)) |
| 1124 | .WillOnce(Return(Technology::kCDCEthernet)); |
| 1125 | EXPECT_CALL(test_device_info_, CreateDevice( |
| 1126 | kTestDeviceName, _, kTestDeviceIndex, Technology::kCDCEthernet)) |
| 1127 | .WillOnce(Return(DeviceRefPtr())); |
| 1128 | test_device_info_.AddLinkMsgHandler(*message); |
| 1129 | Mock::VerifyAndClearExpectations(&test_device_info_); |
| 1130 | // We need to insert the device index ourselves since we have mocked |
| 1131 | // out CreateDevice. This insertion is tested in CreateDeviceCDCEthernet |
| 1132 | // above. |
| 1133 | GetDelayedDevices().insert(kTestDeviceIndex); |
| 1134 | } |
| 1135 | |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 1136 | void TriggerOnWiFiInterfaceInfoReceived(const Nl80211Message &message) { |
Paul Stewart | 2ddf2c6 | 2013-04-16 09:47:34 -0700 | [diff] [blame] | 1137 | test_device_info_.OnWiFiInterfaceInfoReceived(message); |
| 1138 | } |
| 1139 | |
Paul Stewart | 050cfc0 | 2012-07-06 20:38:54 -0700 | [diff] [blame] | 1140 | protected: |
| 1141 | DeviceInfoForDelayedCreationTest test_device_info_; |
| 1142 | }; |
| 1143 | |
| 1144 | TEST_F(DeviceInfoDelayedCreationTest, NoDevices) { |
| 1145 | EXPECT_TRUE(GetDelayedDevices().empty()); |
| 1146 | EXPECT_CALL(test_device_info_, GetDeviceTechnology(_)).Times(0); |
| 1147 | DelayedDeviceCreationTask(); |
| 1148 | } |
| 1149 | |
| 1150 | TEST_F(DeviceInfoDelayedCreationTest, EthernetDevice) { |
| 1151 | AddDelayedDevice(); |
| 1152 | EXPECT_CALL(test_device_info_, GetDeviceTechnology(_)) |
| 1153 | .WillOnce(Return(Technology::kCDCEthernet)); |
| 1154 | EXPECT_CALL(test_device_info_, CreateDevice( |
| 1155 | kTestDeviceName, _, kTestDeviceIndex, Technology::kEthernet)) |
| 1156 | .WillOnce(Return(DeviceRefPtr())); |
| 1157 | DelayedDeviceCreationTask(); |
| 1158 | EXPECT_TRUE(GetDelayedDevices().empty()); |
| 1159 | } |
| 1160 | |
| 1161 | TEST_F(DeviceInfoDelayedCreationTest, CellularDevice) { |
| 1162 | AddDelayedDevice(); |
| 1163 | EXPECT_CALL(test_device_info_, GetDeviceTechnology(_)) |
| 1164 | .WillOnce(Return(Technology::kCellular)); |
| 1165 | EXPECT_CALL(test_device_info_, CreateDevice( |
| 1166 | kTestDeviceName, _, kTestDeviceIndex, Technology::kCellular)) |
| 1167 | .WillOnce(Return(DeviceRefPtr())); |
| 1168 | DelayedDeviceCreationTask(); |
| 1169 | EXPECT_TRUE(GetDelayedDevices().empty()); |
| 1170 | } |
| 1171 | |
Paul Stewart | 2ddf2c6 | 2013-04-16 09:47:34 -0700 | [diff] [blame] | 1172 | TEST_F(DeviceInfoDelayedCreationTest, WiFiDevice) { |
| 1173 | ScopedMockLog log; |
Paul Stewart | 2ddf2c6 | 2013-04-16 09:47:34 -0700 | [diff] [blame] | 1174 | EXPECT_CALL(log, Log(logging::LOG_ERROR, _, |
| 1175 | HasSubstr("Message is not a new interface response"))); |
| 1176 | GetInterfaceMessage non_interface_response_message; |
| 1177 | TriggerOnWiFiInterfaceInfoReceived(non_interface_response_message); |
| 1178 | Mock::VerifyAndClearExpectations(&log); |
| 1179 | |
| 1180 | EXPECT_CALL(log, Log(logging::LOG_ERROR, _, |
| 1181 | HasSubstr("Message contains no interface index"))); |
| 1182 | NewInterfaceMessage message; |
| 1183 | TriggerOnWiFiInterfaceInfoReceived(message); |
| 1184 | Mock::VerifyAndClearExpectations(&log); |
| 1185 | |
| 1186 | message.attributes()->CreateAttribute( |
| 1187 | NL80211_ATTR_IFINDEX, base::Bind( |
| 1188 | &NetlinkAttribute::NewNl80211AttributeFromId)); |
| 1189 | message.attributes()->SetU32AttributeValue(NL80211_ATTR_IFINDEX, |
| 1190 | kTestDeviceIndex); |
| 1191 | EXPECT_CALL(log, Log(logging::LOG_ERROR, _, |
| 1192 | HasSubstr("Message contains no interface type"))); |
| 1193 | TriggerOnWiFiInterfaceInfoReceived(message); |
| 1194 | Mock::VerifyAndClearExpectations(&log); |
| 1195 | |
| 1196 | message.attributes()->CreateAttribute( |
| 1197 | NL80211_ATTR_IFTYPE, base::Bind( |
| 1198 | &NetlinkAttribute::NewNl80211AttributeFromId)); |
| 1199 | message.attributes()->SetU32AttributeValue(NL80211_ATTR_IFTYPE, |
| 1200 | NL80211_IFTYPE_AP); |
| 1201 | EXPECT_CALL(log, Log(logging::LOG_ERROR, _, |
| 1202 | HasSubstr("Could not find device info for interface"))); |
| 1203 | TriggerOnWiFiInterfaceInfoReceived(message); |
| 1204 | Mock::VerifyAndClearExpectations(&log); |
| 1205 | |
| 1206 | // Use the AddDelayedDevice() method to create a device info entry with no |
| 1207 | // associated device. |
| 1208 | AddDelayedDevice(); |
| 1209 | |
| 1210 | EXPECT_CALL(log, Log(logging::LOG_INFO, _, |
| 1211 | HasSubstr("it is not in station mode"))); |
| 1212 | TriggerOnWiFiInterfaceInfoReceived(message); |
| 1213 | Mock::VerifyAndClearExpectations(&log); |
| 1214 | Mock::VerifyAndClearExpectations(&manager_); |
| 1215 | |
| 1216 | message.attributes()->SetU32AttributeValue(NL80211_ATTR_IFTYPE, |
| 1217 | NL80211_IFTYPE_STATION); |
| 1218 | EXPECT_CALL(manager_, RegisterDevice(_)); |
| 1219 | EXPECT_CALL(manager_, device_info()) |
| 1220 | .WillRepeatedly(Return(&test_device_info_)); |
| 1221 | EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber()); |
| 1222 | EXPECT_CALL(log, Log(logging::LOG_INFO, _, |
| 1223 | HasSubstr("Creating WiFi device"))); |
| 1224 | TriggerOnWiFiInterfaceInfoReceived(message); |
| 1225 | Mock::VerifyAndClearExpectations(&log); |
| 1226 | Mock::VerifyAndClearExpectations(&manager_); |
| 1227 | |
| 1228 | EXPECT_CALL(manager_, RegisterDevice(_)).Times(0); |
| 1229 | EXPECT_CALL(log, Log(logging::LOG_ERROR, _, |
| 1230 | HasSubstr("Device already created for interface"))); |
| 1231 | TriggerOnWiFiInterfaceInfoReceived(message); |
| 1232 | } |
| 1233 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 1234 | } // namespace shill |