Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 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. |
| 4 | |
| 5 | // This file provides tests for individual messages. It tests |
Wade Guthrie | f48a195 | 2013-03-04 17:33:47 -0800 | [diff] [blame] | 6 | // NetlinkMessageFactory's ability to create specific message types and it |
| 7 | // tests the various NetlinkMessage types' ability to parse those |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 8 | // messages. |
| 9 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 10 | // This file tests the public interface to NetlinkManager. |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 11 | #include "shill/netlink_manager.h" |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 12 | |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame^] | 13 | #include <string> |
| 14 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 15 | #include <gmock/gmock.h> |
| 16 | #include <gtest/gtest.h> |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 17 | |
Wade Guthrie | cc53f23 | 2013-03-05 13:22:23 -0800 | [diff] [blame] | 18 | #include "shill/mock_netlink_socket.h" |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame^] | 19 | #include "shill/mock_sockets.h" |
| 20 | #include "shill/mock_time.h" |
| 21 | #include "shill/netlink_attribute.h" |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 22 | #include "shill/nl80211_message.h" |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 23 | |
| 24 | using base::Bind; |
| 25 | using base::Unretained; |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame^] | 26 | using std::string; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 27 | using testing::_; |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 28 | using testing::Invoke; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 29 | using testing::Return; |
| 30 | using testing::Test; |
| 31 | |
| 32 | namespace shill { |
| 33 | |
| 34 | namespace { |
| 35 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 36 | // These data blocks have been collected by shill using NetlinkManager while, |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 37 | // simultaneously (and manually) comparing shill output with that of the 'iw' |
| 38 | // code from which it was derived. The test strings represent the raw packet |
| 39 | // data coming from the kernel. The comments above each of these strings is |
| 40 | // the markup that "iw" outputs for ech of these packets. |
| 41 | |
| 42 | // These constants are consistent throughout the packets, below. |
| 43 | |
Wade Guthrie | bdcdaa7 | 2013-03-04 12:47:12 -0800 | [diff] [blame] | 44 | const uint16_t kNl80211FamilyId = 0x13; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 45 | |
| 46 | // wlan0 (phy #0): disconnected (by AP) reason: 2: Previous authentication no |
| 47 | // longer valid |
| 48 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 49 | const unsigned char kNL80211_CMD_DISCONNECT[] = { |
| 50 | 0x30, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, |
| 51 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 52 | 0x30, 0x01, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00, |
| 53 | 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00, |
| 54 | 0x04, 0x00, 0x00, 0x00, 0x06, 0x00, 0x36, 0x00, |
| 55 | 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x47, 0x00, |
| 56 | }; |
| 57 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 58 | const char kGetFamilyCommandString[] = "CTRL_CMD_GETFAMILY"; |
| 59 | |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 60 | } // namespace |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 61 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 62 | class NetlinkManagerTest : public Test { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 63 | public: |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame^] | 64 | NetlinkManagerTest() |
| 65 | : netlink_manager_(NetlinkManager::GetInstance()), |
| 66 | sockets_(new MockSockets), |
| 67 | saved_sequence_number_(0) { |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 68 | netlink_manager_->message_types_[Nl80211Message::kMessageTypeString] |
| 69 | .family_id = kNl80211FamilyId; |
| 70 | netlink_manager_->message_factory_.AddFactoryMethod( |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 71 | kNl80211FamilyId, Bind(&Nl80211Message::CreateMessage)); |
Wade Guthrie | 318445d | 2013-05-16 14:05:28 -0700 | [diff] [blame] | 72 | Nl80211Message::SetMessageType(kNl80211FamilyId); |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame^] | 73 | // Passes ownership. |
| 74 | netlink_socket_.sockets_.reset(sockets_); |
| 75 | |
| 76 | EXPECT_NE(reinterpret_cast<NetlinkManager *>(NULL), netlink_manager_); |
| 77 | netlink_manager_->sock_ = &netlink_socket_; |
| 78 | EXPECT_TRUE(netlink_manager_->Init()); |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 79 | } |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 80 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 81 | ~NetlinkManagerTest() { |
| 82 | // NetlinkManager is a singleton, the sock_ field *MUST* be cleared |
| 83 | // before "NetlinkManagerTest::socket_" gets invalidated, otherwise |
Han Shen | 4afba20 | 2012-12-17 08:48:35 -0800 | [diff] [blame] | 84 | // later tests will refer to a corrupted memory. |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 85 | netlink_manager_->sock_ = NULL; |
Han Shen | 4afba20 | 2012-12-17 08:48:35 -0800 | [diff] [blame] | 86 | } |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 87 | |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame^] | 88 | // |SaveReply|, |SendMessage|, and |ReplyToSentMessage| work together to |
| 89 | // enable a test to get a response to a sent message. They must be called |
| 90 | // in the order, above, so that a) a reply message is available to b) have |
| 91 | // its sequence number replaced, and then c) sent back to the code. |
| 92 | void SaveReply(const ByteString &message) { |
| 93 | saved_message_ = message; |
| 94 | } |
| 95 | |
| 96 | // Replaces the |saved_message_|'s sequence number with the sent value. |
| 97 | bool SendMessage(const ByteString &outgoing_message) { |
| 98 | if (outgoing_message.GetLength() < sizeof(nlmsghdr)) { |
| 99 | LOG(ERROR) << "Outgoing message is too short"; |
| 100 | return false; |
| 101 | } |
| 102 | const nlmsghdr *outgoing_header = |
| 103 | reinterpret_cast<const nlmsghdr *>(outgoing_message.GetConstData()); |
| 104 | |
| 105 | if (saved_message_.GetLength() < sizeof(nlmsghdr)) { |
| 106 | LOG(ERROR) << "Saved message is too short; have you called |SaveReply|?"; |
| 107 | return false; |
| 108 | } |
| 109 | nlmsghdr *reply_header = |
| 110 | reinterpret_cast<nlmsghdr *>(saved_message_.GetData()); |
| 111 | |
| 112 | reply_header->nlmsg_seq = outgoing_header->nlmsg_seq; |
| 113 | saved_sequence_number_ = reply_header->nlmsg_seq; |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | bool ReplyToSentMessage(ByteString *message) { |
| 118 | if (!message) { |
| 119 | return false; |
| 120 | } |
| 121 | *message = saved_message_; |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | bool ReplyWithRandomMessage(ByteString *message) { |
| 126 | GetFamilyMessage get_family_message; |
| 127 | // Any number that's not 0 or 1 is acceptable, here. Zero is bad because |
| 128 | // we want to make sure that this message is different than the main |
| 129 | // send/receive pair. One is bad becasue the default for |
| 130 | // |saved_sequence_number_| is zero and the likely default value for the |
| 131 | // first sequence number generated from the code is 1. |
| 132 | const uint32_t kRandomOffset = 1003; |
| 133 | if (!message) { |
| 134 | return false; |
| 135 | } |
| 136 | *message = get_family_message.Encode(saved_sequence_number_ + |
| 137 | kRandomOffset); |
| 138 | return true; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 141 | protected: |
| 142 | class MockHandler80211 { |
| 143 | public: |
| 144 | MockHandler80211() : |
| 145 | on_netlink_message_(base::Bind(&MockHandler80211::OnNetlinkMessage, |
| 146 | base::Unretained(this))) {} |
| 147 | MOCK_METHOD1(OnNetlinkMessage, void(const NetlinkMessage &msg)); |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 148 | const NetlinkManager::NetlinkMessageHandler &on_netlink_message() const { |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 149 | return on_netlink_message_; |
| 150 | } |
| 151 | private: |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 152 | NetlinkManager::NetlinkMessageHandler on_netlink_message_; |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 153 | DISALLOW_COPY_AND_ASSIGN(MockHandler80211); |
| 154 | }; |
| 155 | |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame^] | 156 | void Reset() { |
| 157 | netlink_manager_->Reset(false); |
| 158 | } |
| 159 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 160 | NetlinkManager *netlink_manager_; |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame^] | 161 | MockNetlinkSocket netlink_socket_; |
| 162 | MockSockets *sockets_; // Owned by |netlink_socket_|. |
| 163 | ByteString saved_message_; |
| 164 | uint32_t saved_sequence_number_; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 165 | }; |
| 166 | |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame^] | 167 | namespace { |
| 168 | |
| 169 | class TimeFunctor { |
| 170 | public: |
| 171 | TimeFunctor(time_t tv_sec, suseconds_t tv_usec) { |
| 172 | return_value_.tv_sec = tv_sec; |
| 173 | return_value_.tv_usec = tv_usec; |
| 174 | } |
| 175 | |
| 176 | TimeFunctor() { |
| 177 | return_value_.tv_sec = 0; |
| 178 | return_value_.tv_usec = 0; |
| 179 | } |
| 180 | |
| 181 | TimeFunctor(const TimeFunctor &other) { |
| 182 | return_value_.tv_sec = other.return_value_.tv_sec; |
| 183 | return_value_.tv_usec = other.return_value_.tv_usec; |
| 184 | } |
| 185 | |
| 186 | TimeFunctor &operator=(const TimeFunctor &rhs) { |
| 187 | return_value_.tv_sec = rhs.return_value_.tv_sec; |
| 188 | return_value_.tv_usec = rhs.return_value_.tv_usec; |
| 189 | return *this; |
| 190 | } |
| 191 | |
| 192 | // Replaces GetTimeMonotonic. |
| 193 | int operator()(struct timeval *answer) { |
| 194 | if (answer) { |
| 195 | *answer = return_value_; |
| 196 | } |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | private: |
| 201 | struct timeval return_value_; |
| 202 | |
| 203 | // No DISALLOW_COPY_AND_ASSIGN since testing::Invoke uses copy. |
| 204 | }; |
| 205 | |
| 206 | } // namespace |
| 207 | |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 208 | // TODO(wdg): Add a test for multi-part messages. crbug.com/224652 |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 209 | // TODO(wdg): Add a test for SubscribeToEvents (verify that it handles bad input |
| 210 | // appropriately, and that it calls NetlinkSocket::SubscribeToEvents if input |
| 211 | // is good.) |
| 212 | |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame^] | 213 | TEST_F(NetlinkManagerTest, GetFamily) { |
| 214 | const uint16_t kSampleMessageType = 42; |
| 215 | const string kSampleMessageName("SampleMessageName"); |
| 216 | const uint32_t kRandomSequenceNumber = 3; |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 217 | |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame^] | 218 | NewFamilyMessage new_family_message; |
| 219 | new_family_message.attributes()->CreateAttribute( |
| 220 | CTRL_ATTR_FAMILY_ID, |
| 221 | base::Bind(&NetlinkAttribute::NewControlAttributeFromId)); |
| 222 | new_family_message.attributes()->SetU16AttributeValue( |
| 223 | CTRL_ATTR_FAMILY_ID, kSampleMessageType); |
| 224 | new_family_message.attributes()->CreateAttribute( |
| 225 | CTRL_ATTR_FAMILY_NAME, |
| 226 | base::Bind(&NetlinkAttribute::NewControlAttributeFromId)); |
| 227 | new_family_message.attributes()->SetStringAttributeValue( |
| 228 | CTRL_ATTR_FAMILY_NAME, kSampleMessageName); |
| 229 | |
| 230 | // The sequence number is immaterial since it'll be overwritten. |
| 231 | SaveReply(new_family_message.Encode(kRandomSequenceNumber)); |
| 232 | EXPECT_CALL(netlink_socket_, SendMessage(_)). |
| 233 | WillOnce(Invoke(this, &NetlinkManagerTest::SendMessage)); |
| 234 | EXPECT_CALL(netlink_socket_, file_descriptor()).WillRepeatedly(Return(0)); |
| 235 | EXPECT_CALL(*sockets_, Select(_, _, _, _, _)).WillOnce(Return(1)); |
| 236 | EXPECT_CALL(netlink_socket_, RecvMessage(_)). |
| 237 | WillOnce(Invoke(this, &NetlinkManagerTest::ReplyToSentMessage)); |
| 238 | NetlinkMessageFactory::FactoryMethod null_factory; |
| 239 | EXPECT_EQ(kSampleMessageType, netlink_manager_->GetFamily(kSampleMessageName, |
| 240 | null_factory)); |
| 241 | } |
| 242 | |
| 243 | TEST_F(NetlinkManagerTest, GetFamilyOneInterstitialMessage) { |
| 244 | Reset(); |
| 245 | |
| 246 | const uint16_t kSampleMessageType = 42; |
| 247 | const string kSampleMessageName("SampleMessageName"); |
| 248 | const uint32_t kRandomSequenceNumber = 3; |
| 249 | |
| 250 | NewFamilyMessage new_family_message; |
| 251 | new_family_message.attributes()->CreateAttribute( |
| 252 | CTRL_ATTR_FAMILY_ID, |
| 253 | base::Bind(&NetlinkAttribute::NewControlAttributeFromId)); |
| 254 | new_family_message.attributes()->SetU16AttributeValue( |
| 255 | CTRL_ATTR_FAMILY_ID, kSampleMessageType); |
| 256 | new_family_message.attributes()->CreateAttribute( |
| 257 | CTRL_ATTR_FAMILY_NAME, |
| 258 | base::Bind(&NetlinkAttribute::NewControlAttributeFromId)); |
| 259 | new_family_message.attributes()->SetStringAttributeValue( |
| 260 | CTRL_ATTR_FAMILY_NAME, kSampleMessageName); |
| 261 | |
| 262 | // The sequence number is immaterial since it'll be overwritten. |
| 263 | SaveReply(new_family_message.Encode(kRandomSequenceNumber)); |
| 264 | EXPECT_CALL(netlink_socket_, SendMessage(_)). |
| 265 | WillOnce(Invoke(this, &NetlinkManagerTest::SendMessage)); |
| 266 | EXPECT_CALL(netlink_socket_, file_descriptor()).WillRepeatedly(Return(0)); |
| 267 | EXPECT_CALL(*sockets_, Select(_, _, _, _, _)).WillRepeatedly(Return(1)); |
| 268 | EXPECT_CALL(netlink_socket_, RecvMessage(_)). |
| 269 | WillOnce(Invoke(this, &NetlinkManagerTest::ReplyWithRandomMessage)). |
| 270 | WillOnce(Invoke(this, &NetlinkManagerTest::ReplyToSentMessage)); |
| 271 | NetlinkMessageFactory::FactoryMethod null_factory; |
| 272 | EXPECT_EQ(kSampleMessageType, netlink_manager_->GetFamily(kSampleMessageName, |
| 273 | null_factory)); |
| 274 | } |
| 275 | |
| 276 | TEST_F(NetlinkManagerTest, GetFamilyTimeout) { |
| 277 | Reset(); |
| 278 | MockTime time; |
| 279 | netlink_manager_->time_ = &time; |
| 280 | |
| 281 | EXPECT_CALL(netlink_socket_, SendMessage(_)).WillOnce(Return(true)); |
| 282 | time_t kStartSeconds = 1234; // Arbitrary. |
| 283 | suseconds_t kSmallUsec = 100; |
| 284 | EXPECT_CALL(time, GetTimeMonotonic(_)). |
| 285 | WillOnce(Invoke(TimeFunctor(kStartSeconds, 0))). // Initial time. |
| 286 | WillOnce(Invoke(TimeFunctor(kStartSeconds, kSmallUsec))). |
| 287 | WillOnce(Invoke(TimeFunctor(kStartSeconds, 2 * kSmallUsec))). |
| 288 | WillOnce(Invoke(TimeFunctor( |
| 289 | kStartSeconds + NetlinkManager::kMaximumNewFamilyWaitSeconds + 1, |
| 290 | NetlinkManager::kMaximumNewFamilyWaitMicroSeconds))); |
| 291 | EXPECT_CALL(netlink_socket_, file_descriptor()).WillRepeatedly(Return(0)); |
| 292 | EXPECT_CALL(*sockets_, Select(_, _, _, _, _)).WillRepeatedly(Return(1)); |
| 293 | EXPECT_CALL(netlink_socket_, RecvMessage(_)). |
| 294 | WillRepeatedly(Invoke(this, &NetlinkManagerTest::ReplyWithRandomMessage)); |
| 295 | NetlinkMessageFactory::FactoryMethod null_factory; |
| 296 | |
| 297 | const string kSampleMessageName("SampleMessageName"); |
| 298 | EXPECT_EQ(NetlinkMessage::kIllegalMessageType, |
| 299 | netlink_manager_->GetFamily(kSampleMessageName, null_factory)); |
| 300 | } |
| 301 | |
| 302 | TEST_F(NetlinkManagerTest, BroadcastHandlerTest) { |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 303 | nlmsghdr *message = const_cast<nlmsghdr *>( |
| 304 | reinterpret_cast<const nlmsghdr *>(kNL80211_CMD_DISCONNECT)); |
| 305 | |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 306 | MockHandler80211 handler1; |
| 307 | MockHandler80211 handler2; |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 308 | |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 309 | // Simple, 1 handler, case. |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 310 | EXPECT_CALL(handler1, OnNetlinkMessage(_)).Times(1); |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 311 | EXPECT_FALSE( |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 312 | netlink_manager_->FindBroadcastHandler(handler1.on_netlink_message())); |
| 313 | netlink_manager_->AddBroadcastHandler(handler1.on_netlink_message()); |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 314 | EXPECT_TRUE( |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 315 | netlink_manager_->FindBroadcastHandler(handler1.on_netlink_message())); |
| 316 | netlink_manager_->OnNlMessageReceived(message); |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 317 | |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 318 | // Add a second handler. |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 319 | EXPECT_CALL(handler1, OnNetlinkMessage(_)).Times(1); |
| 320 | EXPECT_CALL(handler2, OnNetlinkMessage(_)).Times(1); |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 321 | netlink_manager_->AddBroadcastHandler(handler2.on_netlink_message()); |
| 322 | netlink_manager_->OnNlMessageReceived(message); |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 323 | |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 324 | // Verify that a handler can't be added twice. |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 325 | EXPECT_CALL(handler1, OnNetlinkMessage(_)).Times(1); |
| 326 | EXPECT_CALL(handler2, OnNetlinkMessage(_)).Times(1); |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 327 | netlink_manager_->AddBroadcastHandler(handler1.on_netlink_message()); |
| 328 | netlink_manager_->OnNlMessageReceived(message); |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 329 | |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 330 | // Check that we can remove a handler. |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 331 | EXPECT_CALL(handler1, OnNetlinkMessage(_)).Times(0); |
| 332 | EXPECT_CALL(handler2, OnNetlinkMessage(_)).Times(1); |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 333 | EXPECT_TRUE(netlink_manager_->RemoveBroadcastHandler( |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 334 | handler1.on_netlink_message())); |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 335 | netlink_manager_->OnNlMessageReceived(message); |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 336 | |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 337 | // Check that re-adding the handler goes smoothly. |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 338 | EXPECT_CALL(handler1, OnNetlinkMessage(_)).Times(1); |
| 339 | EXPECT_CALL(handler2, OnNetlinkMessage(_)).Times(1); |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 340 | netlink_manager_->AddBroadcastHandler(handler1.on_netlink_message()); |
| 341 | netlink_manager_->OnNlMessageReceived(message); |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 342 | |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 343 | // Check that ClearBroadcastHandlers works. |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 344 | netlink_manager_->ClearBroadcastHandlers(); |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 345 | EXPECT_CALL(handler1, OnNetlinkMessage(_)).Times(0); |
| 346 | EXPECT_CALL(handler2, OnNetlinkMessage(_)).Times(0); |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 347 | netlink_manager_->OnNlMessageReceived(message); |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 350 | TEST_F(NetlinkManagerTest, MessageHandlerTest) { |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame^] | 351 | Reset(); |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 352 | MockHandler80211 handler_broadcast; |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 353 | EXPECT_TRUE(netlink_manager_->AddBroadcastHandler( |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 354 | handler_broadcast.on_netlink_message())); |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 355 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 356 | Nl80211Message sent_message_1(CTRL_CMD_GETFAMILY, kGetFamilyCommandString); |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 357 | MockHandler80211 handler_sent_1; |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 358 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 359 | Nl80211Message sent_message_2(CTRL_CMD_GETFAMILY, kGetFamilyCommandString); |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 360 | MockHandler80211 handler_sent_2; |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 361 | |
| 362 | // Set up the received message as a response to sent_message_1. |
| 363 | scoped_array<unsigned char> message_memory( |
| 364 | new unsigned char[sizeof(kNL80211_CMD_DISCONNECT)]); |
| 365 | memcpy(message_memory.get(), kNL80211_CMD_DISCONNECT, |
| 366 | sizeof(kNL80211_CMD_DISCONNECT)); |
| 367 | nlmsghdr *received_message = |
| 368 | reinterpret_cast<nlmsghdr *>(message_memory.get()); |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 369 | |
| 370 | // Now, we can start the actual test... |
| 371 | |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 372 | // Verify that generic handler gets called for a message when no |
| 373 | // message-specific handler has been installed. |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 374 | EXPECT_CALL(handler_broadcast, OnNetlinkMessage(_)).Times(1); |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 375 | netlink_manager_->OnNlMessageReceived(received_message); |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 376 | |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 377 | // Send the message and give our handler. Verify that we get called back. |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame^] | 378 | EXPECT_CALL(netlink_socket_, SendMessage(_)).WillOnce(Return(true)); |
Wade Guthrie | 92d0636 | 2013-04-25 15:41:30 -0700 | [diff] [blame] | 379 | EXPECT_TRUE(netlink_manager_->SendMessage( |
| 380 | &sent_message_1, handler_sent_1.on_netlink_message())); |
Christopher Wiley | 393b93f | 2012-11-08 17:30:58 -0800 | [diff] [blame] | 381 | // Make it appear that this message is in response to our sent message. |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame^] | 382 | received_message->nlmsg_seq = netlink_socket_.GetLastSequenceNumber(); |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 383 | EXPECT_CALL(handler_sent_1, OnNetlinkMessage(_)).Times(1); |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 384 | netlink_manager_->OnNlMessageReceived(received_message); |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 385 | |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 386 | // Verify that broadcast handler is called for the message after the |
| 387 | // message-specific handler is called once. |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 388 | EXPECT_CALL(handler_broadcast, OnNetlinkMessage(_)).Times(1); |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 389 | netlink_manager_->OnNlMessageReceived(received_message); |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 390 | |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 391 | // Install and then uninstall message-specific handler; verify broadcast |
| 392 | // handler is called on message receipt. |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame^] | 393 | EXPECT_CALL(netlink_socket_, SendMessage(_)).WillOnce(Return(true)); |
Wade Guthrie | 92d0636 | 2013-04-25 15:41:30 -0700 | [diff] [blame] | 394 | EXPECT_TRUE(netlink_manager_->SendMessage( |
| 395 | &sent_message_1, handler_sent_1.on_netlink_message())); |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame^] | 396 | received_message->nlmsg_seq = netlink_socket_.GetLastSequenceNumber(); |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 397 | EXPECT_TRUE(netlink_manager_->RemoveMessageHandler(sent_message_1)); |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 398 | EXPECT_CALL(handler_broadcast, OnNetlinkMessage(_)).Times(1); |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 399 | netlink_manager_->OnNlMessageReceived(received_message); |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 400 | |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 401 | // Install handler for different message; verify that broadcast handler is |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 402 | // called for _this_ message. |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame^] | 403 | EXPECT_CALL(netlink_socket_, SendMessage(_)).WillOnce(Return(true)); |
Wade Guthrie | 92d0636 | 2013-04-25 15:41:30 -0700 | [diff] [blame] | 404 | EXPECT_TRUE(netlink_manager_->SendMessage( |
| 405 | &sent_message_2, handler_sent_2.on_netlink_message())); |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 406 | EXPECT_CALL(handler_broadcast, OnNetlinkMessage(_)).Times(1); |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 407 | netlink_manager_->OnNlMessageReceived(received_message); |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 408 | |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 409 | // Change the ID for the message to that of the second handler; verify that |
| 410 | // the appropriate handler is called for _that_ message. |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame^] | 411 | received_message->nlmsg_seq = netlink_socket_.GetLastSequenceNumber(); |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 412 | EXPECT_CALL(handler_sent_2, OnNetlinkMessage(_)).Times(1); |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 413 | netlink_manager_->OnNlMessageReceived(received_message); |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 416 | } // namespace shill |