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 | |
Han Shen | 4afba20 | 2012-12-17 08:48:35 -0800 | [diff] [blame] | 34 | virtual void SetUp() { |
| 35 | // RTNLHandler is a singleton, there's no guarentee that it is not |
| 36 | // setup/used by other unittests. Clear "listeners_" field before we run |
| 37 | // tests. |
| 38 | RTNLHandler::GetInstance()->listeners_.clear(); |
| 39 | } |
| 40 | |
Darin Petkov | ca432fc | 2011-07-08 15:56:57 -0700 | [diff] [blame] | 41 | protected: |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 42 | Callback<void(const RTNLMessage &)> callback_; |
Darin Petkov | ca432fc | 2011-07-08 15:56:57 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | TEST_F(RTNLListenerTest, NoRun) { |
| 46 | { |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 47 | RTNLListener listener(RTNLHandler::kRequestAddr, callback_); |
Darin Petkov | ca432fc | 2011-07-08 15:56:57 -0700 | [diff] [blame] | 48 | EXPECT_EQ(1, RTNLHandler::GetInstance()->listeners_.size()); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 49 | RTNLMessage message; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 50 | EXPECT_CALL(*this, ListenerCallback(_)).Times(0); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 51 | listener.NotifyEvent(RTNLHandler::kRequestLink, message); |
Darin Petkov | ca432fc | 2011-07-08 15:56:57 -0700 | [diff] [blame] | 52 | } |
| 53 | EXPECT_EQ(0, RTNLHandler::GetInstance()->listeners_.size()); |
| 54 | } |
| 55 | |
| 56 | TEST_F(RTNLListenerTest, Run) { |
| 57 | { |
| 58 | RTNLListener listener( |
| 59 | RTNLHandler::kRequestLink | RTNLHandler::kRequestAddr, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 60 | callback_); |
Darin Petkov | ca432fc | 2011-07-08 15:56:57 -0700 | [diff] [blame] | 61 | EXPECT_EQ(1, RTNLHandler::GetInstance()->listeners_.size()); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 62 | RTNLMessage message; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 63 | EXPECT_CALL(*this, ListenerCallback(A<const RTNLMessage &>())).Times(1); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 64 | listener.NotifyEvent(RTNLHandler::kRequestLink, message); |
Darin Petkov | ca432fc | 2011-07-08 15:56:57 -0700 | [diff] [blame] | 65 | } |
| 66 | EXPECT_EQ(0, RTNLHandler::GetInstance()->listeners_.size()); |
| 67 | } |
| 68 | |
| 69 | } // namespace shill |