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 <boost/filesystem.hpp>
23
#include <w11tng/dhcpserver.h>
24
#include <w11tng/dhcpclient.h>
26
#include <core/posix/process.h>
28
#include <common/glibhelpers.h>
29
#include <common/virtualnetwork.h>
32
class MockDhcpServerDelegate : public w11tng::DhcpServer::Delegate {
34
MOCK_METHOD2(OnDhcpAddressAssigned, void(const mcs::IpV4Address &, const mcs::IpV4Address&));
35
MOCK_METHOD0(OnDhcpTerminated, void());
37
class MockDhcpClientDelegate : public w11tng::DhcpClient::Delegate {
39
MOCK_METHOD2(OnDhcpAddressAssigned, void(const mcs::IpV4Address &, const mcs::IpV4Address&));
40
MOCK_METHOD0(OnDhcpTerminated, void());
44
TEST(Dhcp, AddressAssignment) {
45
mcs::testing::VirtualNetwork veth;
47
boost::filesystem::create_directory("/run/aethercast");
49
auto server_delegate = std::make_shared<MockDhcpServerDelegate>();
50
EXPECT_CALL(*server_delegate, OnDhcpAddressAssigned(mcs::IpV4Address::from_string("192.168.7.1"),
51
mcs::IpV4Address::from_string("192.168.7.5")))
54
auto client_delegate = std::make_shared<MockDhcpClientDelegate>();
55
EXPECT_CALL(*client_delegate, OnDhcpAddressAssigned(mcs::IpV4Address::from_string("192.168.7.5"),
56
mcs::IpV4Address::from_string("192.168.7.1")))
59
auto server = w11tng::DhcpServer::Create(server_delegate, veth.Endpoint1());
60
auto client = w11tng::DhcpClient::Create(client_delegate, veth.Endpoint2());
62
mcs::testing::RunMainLoop(std::chrono::seconds{5});
64
EXPECT_EQ(client->LocalAddress().to_string(), "192.168.7.5");
65
EXPECT_EQ(client->RemoteAddress().to_string(), "192.168.7.1");