Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -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. |
| 4 | |
| 5 | #include <string> |
| 6 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 7 | #include <glib.h> |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 8 | #include <gtest/gtest.h> |
| 9 | #include <net/if.h> |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 10 | #include <sys/socket.h> |
| 11 | #include <linux/netlink.h> // Needs typedefs from sys/socket.h. |
| 12 | #include <linux/rtnetlink.h> |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 13 | #include <sys/ioctl.h> |
| 14 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 15 | #include <base/bind.h> |
| 16 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 17 | #include "shill/manager.h" |
| 18 | #include "shill/mock_control.h" |
| 19 | #include "shill/mock_glib.h" |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 20 | #include "shill/mock_manager.h" |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 21 | #include "shill/mock_metrics.h" |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 22 | #include "shill/mock_sockets.h" |
| 23 | #include "shill/rtnl_handler.h" |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 24 | #include "shill/rtnl_message.h" |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 25 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 26 | using base::Bind; |
| 27 | using base::Callback; |
| 28 | using base::Unretained; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 29 | using std::string; |
| 30 | using testing::_; |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 31 | using testing::A; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 32 | using testing::DoAll; |
| 33 | using testing::Return; |
| 34 | using testing::StrictMock; |
| 35 | using testing::Test; |
| 36 | |
| 37 | namespace shill { |
| 38 | |
| 39 | namespace { |
| 40 | |
| 41 | const int kTestInterfaceIndex = 4; |
| 42 | |
| 43 | ACTION(SetInterfaceIndex) { |
| 44 | if (arg2) { |
| 45 | reinterpret_cast<struct ifreq *>(arg2)->ifr_ifindex = kTestInterfaceIndex; |
| 46 | } |
| 47 | } |
| 48 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 49 | class TestEventDispatcher : public EventDispatcher { |
| 50 | public: |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 51 | virtual IOHandler *CreateInputHandler( |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 52 | int /*fd*/, |
Paul Stewart | 5f06a0e | 2012-12-20 11:11:33 -0800 | [diff] [blame] | 53 | const IOHandler::InputCallback &/*input_callback*/, |
| 54 | const IOHandler::ErrorCallback &/*error_callback*/) { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 55 | return NULL; |
| 56 | } |
| 57 | }; |
| 58 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 59 | } // namespace |
| 60 | |
| 61 | class RTNLHandlerTest : public Test { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 62 | public: |
| 63 | RTNLHandlerTest() |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 64 | : manager_(&control_interface_, &dispatcher_, &metrics_, &glib_), |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 65 | callback_(Bind(&RTNLHandlerTest::HandlerCallback, Unretained(this))) { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | virtual void TearDown() { |
| 69 | RTNLHandler::GetInstance()->Stop(); |
| 70 | SetSockets(NULL); |
| 71 | } |
| 72 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 73 | MOCK_METHOD1(HandlerCallback, void(const RTNLMessage &)); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 74 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 75 | protected: |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 76 | static const int kTestSocket; |
| 77 | static const int kTestDeviceIndex; |
| 78 | static const char kTestDeviceName[]; |
| 79 | |
| 80 | void AddLink(); |
| 81 | void StartRTNLHandler(); |
| 82 | void StopRTNLHandler(); |
| 83 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 84 | void SetSockets(Sockets *sockets) { |
| 85 | RTNLHandler::GetInstance()->sockets_ = sockets; |
| 86 | } |
| 87 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 88 | StrictMock<MockSockets> sockets_; |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 89 | MockGLib glib_; |
| 90 | MockControl control_interface_; |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 91 | MockMetrics metrics_; |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 92 | MockManager manager_; |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 93 | TestEventDispatcher dispatcher_; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 94 | Callback<void(const RTNLMessage &)> callback_; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 97 | const int RTNLHandlerTest::kTestSocket = 123; |
| 98 | const int RTNLHandlerTest::kTestDeviceIndex = 123456; |
| 99 | const char RTNLHandlerTest::kTestDeviceName[] = "test-device"; |
| 100 | |
| 101 | void RTNLHandlerTest::StartRTNLHandler() { |
| 102 | EXPECT_CALL(sockets_, Socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) |
| 103 | .WillOnce(Return(kTestSocket)); |
| 104 | EXPECT_CALL(sockets_, Bind(kTestSocket, _, sizeof(sockaddr_nl))) |
| 105 | .WillOnce(Return(0)); |
Julius Werner | f253c84 | 2012-12-20 14:52:30 -0800 | [diff] [blame] | 106 | EXPECT_CALL(sockets_, SetReceiveBuffer(kTestSocket, _)).WillOnce(Return(0)); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 107 | RTNLHandler::GetInstance()->Start(&dispatcher_, &sockets_); |
| 108 | } |
| 109 | |
| 110 | void RTNLHandlerTest::StopRTNLHandler() { |
| 111 | EXPECT_CALL(sockets_, Close(kTestSocket)).WillOnce(Return(0)); |
| 112 | RTNLHandler::GetInstance()->Stop(); |
| 113 | } |
| 114 | |
| 115 | void RTNLHandlerTest::AddLink() { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 116 | RTNLMessage message(RTNLMessage::kTypeLink, |
| 117 | RTNLMessage::kModeAdd, |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 118 | 0, |
| 119 | 0, |
| 120 | 0, |
| 121 | kTestDeviceIndex, |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 122 | IPAddress::kFamilyIPv4); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 123 | message.SetAttribute(static_cast<uint16>(IFLA_IFNAME), |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 124 | ByteString(string(kTestDeviceName), true)); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 125 | ByteString b(message.Encode()); |
| 126 | InputData data(b.GetData(), b.GetLength()); |
| 127 | RTNLHandler::GetInstance()->ParseRTNL(&data); |
| 128 | } |
| 129 | |
| 130 | TEST_F(RTNLHandlerTest, AddLinkTest) { |
| 131 | StartRTNLHandler(); |
| 132 | scoped_ptr<RTNLListener> link_listener( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 133 | new RTNLListener(RTNLHandler::kRequestLink, callback_)); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 134 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 135 | EXPECT_CALL(*this, HandlerCallback(A<const RTNLMessage &>())).Times(1); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 136 | |
| 137 | AddLink(); |
| 138 | |
| 139 | StopRTNLHandler(); |
| 140 | } |
| 141 | |
| 142 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 143 | TEST_F(RTNLHandlerTest, GetInterfaceName) { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 144 | SetSockets(&sockets_); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 145 | EXPECT_EQ(-1, RTNLHandler::GetInstance()->GetInterfaceIndex("")); |
| 146 | { |
| 147 | struct ifreq ifr; |
| 148 | string name(sizeof(ifr.ifr_name), 'x'); |
| 149 | EXPECT_EQ(-1, RTNLHandler::GetInstance()->GetInterfaceIndex(name)); |
| 150 | } |
| 151 | |
| 152 | const int kTestSocket = 123; |
| 153 | EXPECT_CALL(sockets_, Socket(PF_INET, SOCK_DGRAM, 0)) |
| 154 | .Times(3) |
| 155 | .WillOnce(Return(-1)) |
| 156 | .WillRepeatedly(Return(kTestSocket)); |
| 157 | EXPECT_CALL(sockets_, Ioctl(kTestSocket, SIOCGIFINDEX, _)) |
| 158 | .WillOnce(Return(-1)) |
| 159 | .WillOnce(DoAll(SetInterfaceIndex(), Return(0))); |
| 160 | EXPECT_CALL(sockets_, Close(kTestSocket)) |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 161 | .Times(3) |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 162 | .WillRepeatedly(Return(0)); |
| 163 | EXPECT_EQ(-1, RTNLHandler::GetInstance()->GetInterfaceIndex("eth0")); |
| 164 | EXPECT_EQ(-1, RTNLHandler::GetInstance()->GetInterfaceIndex("wlan0")); |
| 165 | EXPECT_EQ(kTestInterfaceIndex, |
| 166 | RTNLHandler::GetInstance()->GetInterfaceIndex("usb0")); |
| 167 | } |
| 168 | |
| 169 | } // namespace shill |