2
* Copyright (C) 2016 Canonical, Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 3, as published
6
* by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranties of
10
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
* PURPOSE. See the GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License along
14
* with this program. If not, see <http://www.gnu.org/licenses/>.
18
#include <gtest/gtest.h>
19
#include <gmock/gmock.h>
21
#include <asm/types.h>
22
#include <linux/netlink.h>
23
#include <linux/rtnetlink.h>
24
#include <sys/socket.h>
26
#include <common/glibhelpers.h>
27
#include <common/virtualnetwork.h>
29
#include <mcs/networkutils.h>
31
#include <w11tng/netlinklistener.h>
34
class MockNetlinkListenerDelegate : public w11tng::NetlinkListener::Delegate {
36
MOCK_METHOD2(OnInterfaceAddressChanged, void(const std::string &, const std::string &));
38
class NetlinkListener : public ::testing::TestWithParam<bool> {
42
TEST_P(NetlinkListener, NotifiesIpAddressChanges) {
43
auto delegate = std::make_shared<MockNetlinkListenerDelegate>();
45
mcs::testing::VirtualNetwork veth;
47
auto listener = w11tng::NetlinkListener::Create(delegate);
49
listener->SetInterfaceFilter(veth.Endpoint1());
51
std::string expected_address = "192.168.7.1";
53
// We can't really influence the number of times this is
54
// called as the events come in multiple times (for whatever
55
// reason) so we require it to happen at least once.
56
EXPECT_CALL(*delegate, OnInterfaceAddressChanged(veth.Endpoint1(), expected_address))
57
.Times(::testing::AtLeast(1));
59
auto interface_index = mcs::NetworkUtils::RetrieveInterfaceIndex(veth.Endpoint1().c_str());
60
mcs::NetworkUtils::ModifyInterfaceAddress(RTM_NEWADDR, NLM_F_REPLACE | NLM_F_ACK,
61
interface_index, AF_INET, expected_address.c_str(),
62
nullptr, 24, "255.255.255.0");
64
mcs::testing::RunMainLoop(std::chrono::seconds{2});
67
INSTANTIATE_TEST_CASE_P(InterfaceAddressChanges, NetlinkListener, ::testing::Values(false, true));