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 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 8 | #include <base/bind.h> |
Darin Petkov | ca432fc | 2011-07-08 15:56:57 -0700 | [diff] [blame] | 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 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 17 | using base::Bind; |
| 18 | using base::Callback; |
| 19 | using base::Unretained; |
Darin Petkov | ca432fc | 2011-07-08 15:56:57 -0700 | [diff] [blame] | 20 | using testing::_; |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 21 | using testing::A; |
Darin Petkov | ca432fc | 2011-07-08 15:56:57 -0700 | [diff] [blame] | 22 | using testing::Test; |
| 23 | |
| 24 | namespace shill { |
| 25 | |
| 26 | class RTNLListenerTest : public Test { |
| 27 | public: |
| 28 | RTNLListenerTest() |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 29 | : callback_(Bind(&RTNLListenerTest::ListenerCallback, |
| 30 | Unretained(this))) {} |
Darin Petkov | ca432fc | 2011-07-08 15:56:57 -0700 | [diff] [blame] | 31 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 32 | MOCK_METHOD1(ListenerCallback, void(const RTNLMessage &)); |
Darin Petkov | ca432fc | 2011-07-08 15:56:57 -0700 | [diff] [blame] | 33 | |
| 34 | protected: |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 35 | Callback<void(const RTNLMessage &)> callback_; |
Darin Petkov | ca432fc | 2011-07-08 15:56:57 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | TEST_F(RTNLListenerTest, NoRun) { |
| 39 | { |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 40 | RTNLListener listener(RTNLHandler::kRequestAddr, callback_); |
Darin Petkov | ca432fc | 2011-07-08 15:56:57 -0700 | [diff] [blame] | 41 | EXPECT_EQ(1, RTNLHandler::GetInstance()->listeners_.size()); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 42 | RTNLMessage message; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 43 | EXPECT_CALL(*this, ListenerCallback(_)).Times(0); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 44 | listener.NotifyEvent(RTNLHandler::kRequestLink, message); |
Darin Petkov | ca432fc | 2011-07-08 15:56:57 -0700 | [diff] [blame] | 45 | } |
| 46 | EXPECT_EQ(0, RTNLHandler::GetInstance()->listeners_.size()); |
| 47 | } |
| 48 | |
| 49 | TEST_F(RTNLListenerTest, Run) { |
| 50 | { |
| 51 | RTNLListener listener( |
| 52 | RTNLHandler::kRequestLink | RTNLHandler::kRequestAddr, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 53 | callback_); |
Darin Petkov | ca432fc | 2011-07-08 15:56:57 -0700 | [diff] [blame] | 54 | EXPECT_EQ(1, RTNLHandler::GetInstance()->listeners_.size()); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 55 | RTNLMessage message; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 56 | EXPECT_CALL(*this, ListenerCallback(A<const RTNLMessage &>())).Times(1); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 57 | listener.NotifyEvent(RTNLHandler::kRequestLink, message); |
Darin Petkov | ca432fc | 2011-07-08 15:56:57 -0700 | [diff] [blame] | 58 | } |
| 59 | EXPECT_EQ(0, RTNLHandler::GetInstance()->listeners_.size()); |
| 60 | } |
| 61 | |
| 62 | } // namespace shill |