Darin Petkov | ca432fc | 2011-07-08 15:56:57 -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. |
| 4 | |
| 5 | #include <sys/socket.h> |
| 6 | #include <linux/netlink.h> |
| 7 | |
| 8 | #include <base/callback_old.h> |
| 9 | #include <base/memory/scoped_ptr.h> |
| 10 | #include <gmock/gmock.h> |
| 11 | #include <gtest/gtest.h> |
| 12 | |
| 13 | #include "shill/rtnl_handler.h" |
| 14 | #include "shill/rtnl_listener.h" |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 15 | #include "shill/rtnl_message.h" |
Darin Petkov | ca432fc | 2011-07-08 15:56:57 -0700 | [diff] [blame] | 16 | |
| 17 | using testing::_; |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 18 | using testing::A; |
Darin Petkov | ca432fc | 2011-07-08 15:56:57 -0700 | [diff] [blame] | 19 | using testing::Test; |
| 20 | |
| 21 | namespace shill { |
| 22 | |
| 23 | class RTNLListenerTest : public Test { |
| 24 | public: |
| 25 | RTNLListenerTest() |
| 26 | : callback_(NewCallback(this, &RTNLListenerTest::Callback)) {} |
| 27 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 28 | MOCK_METHOD1(Callback, void(const RTNLMessage &)); |
Darin Petkov | ca432fc | 2011-07-08 15:56:57 -0700 | [diff] [blame] | 29 | |
| 30 | protected: |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 31 | scoped_ptr<Callback1<const RTNLMessage &>::Type> callback_; |
Darin Petkov | ca432fc | 2011-07-08 15:56:57 -0700 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | TEST_F(RTNLListenerTest, NoRun) { |
| 35 | { |
| 36 | RTNLListener listener(RTNLHandler::kRequestAddr, callback_.get()); |
| 37 | EXPECT_EQ(1, RTNLHandler::GetInstance()->listeners_.size()); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 38 | RTNLMessage message; |
Darin Petkov | ca432fc | 2011-07-08 15:56:57 -0700 | [diff] [blame] | 39 | EXPECT_CALL(*this, Callback(_)).Times(0); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 40 | listener.NotifyEvent(RTNLHandler::kRequestLink, message); |
Darin Petkov | ca432fc | 2011-07-08 15:56:57 -0700 | [diff] [blame] | 41 | } |
| 42 | EXPECT_EQ(0, RTNLHandler::GetInstance()->listeners_.size()); |
| 43 | } |
| 44 | |
| 45 | TEST_F(RTNLListenerTest, Run) { |
| 46 | { |
| 47 | RTNLListener listener( |
| 48 | RTNLHandler::kRequestLink | RTNLHandler::kRequestAddr, |
| 49 | callback_.get()); |
| 50 | EXPECT_EQ(1, RTNLHandler::GetInstance()->listeners_.size()); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 51 | RTNLMessage message; |
| 52 | EXPECT_CALL(*this, Callback(A<const RTNLMessage &>())).Times(1); |
| 53 | listener.NotifyEvent(RTNLHandler::kRequestLink, message); |
Darin Petkov | ca432fc | 2011-07-08 15:56:57 -0700 | [diff] [blame] | 54 | } |
| 55 | EXPECT_EQ(0, RTNLHandler::GetInstance()->listeners_.size()); |
| 56 | } |
| 57 | |
| 58 | } // namespace shill |