Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
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 | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 8 | #include <sys/socket.h> |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 9 | #include <linux/if.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> |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 12 | |
| 13 | #include <base/callback_old.h> |
| 14 | #include <base/logging.h> |
| 15 | #include <base/memory/ref_counted.h> |
| 16 | #include <base/message_loop.h> |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 17 | #include <base/stl_util-inl.h> |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 18 | #include <gtest/gtest.h> |
| 19 | #include <gmock/gmock.h> |
| 20 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 21 | #include "shill/manager.h" |
Chris Masone | 46eaaf5 | 2011-05-24 13:08:30 -0700 | [diff] [blame] | 22 | #include "shill/mock_control.h" |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 23 | #include "shill/mock_glib.h" |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 24 | #include "shill/mock_manager.h" |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 25 | #include "shill/mock_sockets.h" |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 26 | #include "shill/rtnl_handler.h" |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 27 | #include "shill/rtnl_message.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 28 | |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 29 | using std::map; |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 30 | using std::string; |
| 31 | using testing::_; |
| 32 | using testing::Return; |
| 33 | using testing::Test; |
| 34 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 35 | namespace shill { |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 36 | |
| 37 | class TestEventDispatcher : public EventDispatcher { |
| 38 | public: |
| 39 | virtual IOInputHandler *CreateInputHandler( |
| 40 | int fd, |
| 41 | Callback1<InputData*>::Type *callback) { |
| 42 | return NULL; |
| 43 | } |
| 44 | }; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 45 | |
| 46 | class DeviceInfoTest : public Test { |
| 47 | public: |
| 48 | DeviceInfoTest() |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 49 | : manager_(&control_interface_, &dispatcher_, &glib_), |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 50 | device_info_(&control_interface_, &dispatcher_, &manager_) { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 51 | } |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 52 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 53 | protected: |
Chris Masone | b2e326b | 2011-07-12 13:28:51 -0700 | [diff] [blame] | 54 | static const int kTestDeviceIndex; |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 55 | static const char kTestDeviceName[]; |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 56 | static const char kTestAddress[]; |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 57 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 58 | RTNLMessage *BuildMessage(RTNLMessage::MessageType type, |
| 59 | RTNLMessage::MessageMode mode); |
| 60 | void SendMessageToDeviceInfo(const RTNLMessage &message); |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 61 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 62 | MockGLib glib_; |
Chris Masone | 46eaaf5 | 2011-05-24 13:08:30 -0700 | [diff] [blame] | 63 | MockControl control_interface_; |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 64 | MockManager manager_; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 65 | DeviceInfo device_info_; |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 66 | TestEventDispatcher dispatcher_; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
Chris Masone | b2e326b | 2011-07-12 13:28:51 -0700 | [diff] [blame] | 69 | const int DeviceInfoTest::kTestDeviceIndex = 123456; |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 70 | const char DeviceInfoTest::kTestDeviceName[] = "test-device"; |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 71 | const char DeviceInfoTest::kTestAddress[] = { |
| 72 | 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 73 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 74 | RTNLMessage *DeviceInfoTest::BuildMessage(RTNLMessage::MessageType type, |
| 75 | RTNLMessage::MessageMode mode) { |
| 76 | RTNLMessage *message = new RTNLMessage(type, mode, 0, 0, 0, kTestDeviceIndex, |
| 77 | IPAddress::kAddressFamilyIPv4); |
| 78 | message->SetAttribute(static_cast<uint16>(IFLA_IFNAME), |
| 79 | ByteString(kTestDeviceName, true)); |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 80 | ByteString test_address(kTestAddress, sizeof(kTestAddress)); |
| 81 | message->SetAttribute(IFLA_ADDRESS, test_address); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 82 | return message; |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 85 | void DeviceInfoTest::SendMessageToDeviceInfo(const RTNLMessage &message) { |
| 86 | device_info_.LinkMsgHandler(message); |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | TEST_F(DeviceInfoTest, DeviceEnumeration) { |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 90 | // Start our own private device_info |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 91 | device_info_.Start(); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 92 | scoped_ptr<RTNLMessage> message(BuildMessage(RTNLMessage::kMessageTypeLink, |
| 93 | RTNLMessage::kMessageModeAdd)); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 94 | message->set_link_status(RTNLMessage::LinkStatus(0, IFF_LOWER_UP, 0)); |
| 95 | EXPECT_FALSE(device_info_.GetDevice(kTestDeviceIndex).get()); |
| 96 | SendMessageToDeviceInfo(*message); |
| 97 | EXPECT_TRUE(device_info_.GetDevice(kTestDeviceIndex).get()); |
| 98 | unsigned int flags = 0; |
| 99 | EXPECT_TRUE(device_info_.GetFlags(kTestDeviceIndex, &flags)); |
| 100 | EXPECT_EQ(IFF_LOWER_UP, flags); |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 101 | ByteString address; |
| 102 | EXPECT_TRUE(device_info_.GetAddress(kTestDeviceIndex, &address)); |
| 103 | EXPECT_FALSE(address.IsEmpty()); |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 104 | EXPECT_TRUE(address.Equals(ByteString(kTestAddress, sizeof(kTestAddress)))); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 105 | |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 106 | message.reset(BuildMessage(RTNLMessage::kMessageTypeLink, |
| 107 | RTNLMessage::kMessageModeAdd)); |
| 108 | message->set_link_status(RTNLMessage::LinkStatus(0, IFF_UP | IFF_RUNNING, 0)); |
| 109 | SendMessageToDeviceInfo(*message); |
| 110 | EXPECT_TRUE(device_info_.GetFlags(kTestDeviceIndex, &flags)); |
| 111 | EXPECT_EQ(IFF_UP | IFF_RUNNING, flags); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 112 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 113 | message.reset(BuildMessage(RTNLMessage::kMessageTypeLink, |
| 114 | RTNLMessage::kMessageModeDelete)); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 115 | SendMessageToDeviceInfo(*message); |
| 116 | EXPECT_FALSE(device_info_.GetDevice(kTestDeviceIndex).get()); |
| 117 | EXPECT_FALSE(device_info_.GetFlags(kTestDeviceIndex, NULL)); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 118 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 119 | device_info_.Stop(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 120 | } |
| 121 | |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 122 | TEST_F(DeviceInfoTest, DeviceBlackList) { |
| 123 | device_info_.AddDeviceToBlackList(kTestDeviceName); |
| 124 | device_info_.Start(); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 125 | scoped_ptr<RTNLMessage> message(BuildMessage(RTNLMessage::kMessageTypeLink, |
| 126 | RTNLMessage::kMessageModeAdd)); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 127 | SendMessageToDeviceInfo(*message); |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 128 | |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 129 | DeviceRefPtr device = device_info_.GetDevice(kTestDeviceIndex); |
| 130 | ASSERT_TRUE(device.get()); |
| 131 | EXPECT_TRUE(device->TechnologyIs(Device::kBlacklisted)); |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 132 | |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 133 | device_info_.Stop(); |
| 134 | } |
| 135 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 136 | } // namespace shill |