blob: aed4137cf2aae333c53944056a21d8c4c114752d [file] [log] [blame]
Darin Petkovca432fc2011-07-08 15:56:57 -07001// 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 Shienbrood3e20a232012-02-16 11:35:56 -05008#include <base/bind.h>
Darin Petkovca432fc2011-07-08 15:56:57 -07009#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 Masone2aa97072011-08-09 17:35:08 -070015#include "shill/rtnl_message.h"
Darin Petkovca432fc2011-07-08 15:56:57 -070016
Eric Shienbrood3e20a232012-02-16 11:35:56 -050017using base::Bind;
18using base::Callback;
19using base::Unretained;
Darin Petkovca432fc2011-07-08 15:56:57 -070020using testing::_;
Chris Masone2aa97072011-08-09 17:35:08 -070021using testing::A;
Darin Petkovca432fc2011-07-08 15:56:57 -070022using testing::Test;
23
24namespace shill {
25
26class RTNLListenerTest : public Test {
27 public:
28 RTNLListenerTest()
Eric Shienbrood3e20a232012-02-16 11:35:56 -050029 : callback_(Bind(&RTNLListenerTest::ListenerCallback,
30 Unretained(this))) {}
Darin Petkovca432fc2011-07-08 15:56:57 -070031
Eric Shienbrood3e20a232012-02-16 11:35:56 -050032 MOCK_METHOD1(ListenerCallback, void(const RTNLMessage &));
Darin Petkovca432fc2011-07-08 15:56:57 -070033
34 protected:
Eric Shienbrood3e20a232012-02-16 11:35:56 -050035 Callback<void(const RTNLMessage &)> callback_;
Darin Petkovca432fc2011-07-08 15:56:57 -070036};
37
38TEST_F(RTNLListenerTest, NoRun) {
39 {
Eric Shienbrood3e20a232012-02-16 11:35:56 -050040 RTNLListener listener(RTNLHandler::kRequestAddr, callback_);
Darin Petkovca432fc2011-07-08 15:56:57 -070041 EXPECT_EQ(1, RTNLHandler::GetInstance()->listeners_.size());
Chris Masone2aa97072011-08-09 17:35:08 -070042 RTNLMessage message;
Eric Shienbrood3e20a232012-02-16 11:35:56 -050043 EXPECT_CALL(*this, ListenerCallback(_)).Times(0);
Chris Masone2aa97072011-08-09 17:35:08 -070044 listener.NotifyEvent(RTNLHandler::kRequestLink, message);
Darin Petkovca432fc2011-07-08 15:56:57 -070045 }
46 EXPECT_EQ(0, RTNLHandler::GetInstance()->listeners_.size());
47}
48
49TEST_F(RTNLListenerTest, Run) {
50 {
51 RTNLListener listener(
52 RTNLHandler::kRequestLink | RTNLHandler::kRequestAddr,
Eric Shienbrood3e20a232012-02-16 11:35:56 -050053 callback_);
Darin Petkovca432fc2011-07-08 15:56:57 -070054 EXPECT_EQ(1, RTNLHandler::GetInstance()->listeners_.size());
Chris Masone2aa97072011-08-09 17:35:08 -070055 RTNLMessage message;
Eric Shienbrood3e20a232012-02-16 11:35:56 -050056 EXPECT_CALL(*this, ListenerCallback(A<const RTNLMessage &>())).Times(1);
Chris Masone2aa97072011-08-09 17:35:08 -070057 listener.NotifyEvent(RTNLHandler::kRequestLink, message);
Darin Petkovca432fc2011-07-08 15:56:57 -070058 }
59 EXPECT_EQ(0, RTNLHandler::GetInstance()->listeners_.size());
60}
61
62} // namespace shill