blob: 16918027e4b21038296bfd04add9b44560088ec9 [file] [log] [blame]
Peter Qiubf8e36c2014-12-03 22:59:45 -08001// Copyright 2014 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 "apmanager/dhcp_server.h"
6
7#include <string>
8
9#include <net/if.h>
10
11#include <base/strings/string_util.h>
12#include <base/strings/stringprintf.h>
13#include <chromeos/dbus/service_constants.h>
14#include <chromeos/process_mock.h>
15#include <gmock/gmock.h>
16#include <gtest/gtest.h>
17#include <shill/net/mock_rtnl_handler.h>
18
19using chromeos::ProcessMock;
20using ::testing::_;
21using ::testing::Mock;
22using ::testing::Return;
23using std::string;
24
25namespace {
26 const uint16_t kServerAddressIndex = 1;
27 const char kTestInterfaceName[] = "test_interface";
28 const char kBinSleep[] = "/bin/sleep";
29 const char kExpectedDnsmasqConfigFile[] =
30 "port=0\n"
31 "bind-interfaces\n"
32 "log-dhcp\n"
Peter Qiue1ed1cd2014-12-06 16:03:35 -080033 "keep-in-foreground\n"
34 "user=root\n"
Peter Qiubf8e36c2014-12-03 22:59:45 -080035 "dhcp-range=192.168.1.1,192.168.1.128\n"
36 "interface=test_interface\n"
37 "dhcp-leasefile=/tmp/dhcpd-1.leases\n";
38} // namespace
39
40namespace apmanager {
41
42class DHCPServerTest : public testing::Test {
43 public:
44 DHCPServerTest()
45 : dhcp_server_(new DHCPServer(kServerAddressIndex, kTestInterfaceName)),
46 rtnl_handler_(new shill::MockRTNLHandler()) {}
47 virtual ~DHCPServerTest() {}
48
49 virtual void SetUp() {
50 dhcp_server_->rtnl_handler_ = rtnl_handler_.get();
51 }
52
53 virtual void TearDown() {
54 // Reset DHCP server now while RTNLHandler is still valid.
55 dhcp_server_.reset();
56 }
57
58 void StartDummyProcess() {
59 dhcp_server_->dnsmasq_process_.reset(new chromeos::ProcessImpl);
60 dhcp_server_->dnsmasq_process_->AddArg(kBinSleep);
61 dhcp_server_->dnsmasq_process_->AddArg("12345");
62 CHECK(dhcp_server_->dnsmasq_process_->Start());
63 }
64
65 string GenerateConfigFile() {
66 return dhcp_server_->GenerateConfigFile();
67 }
68
69 protected:
70 std::unique_ptr<DHCPServer> dhcp_server_;
71 std::unique_ptr<shill::MockRTNLHandler> rtnl_handler_;
72};
73
74
75TEST_F(DHCPServerTest, GenerateConfigFile) {
76 string config_content = GenerateConfigFile();
77 EXPECT_STREQ(kExpectedDnsmasqConfigFile, config_content.c_str())
78 << "Expected to find the following config...\n"
79 << kExpectedDnsmasqConfigFile << ".....\n"
80 << config_content;
81}
82
83TEST_F(DHCPServerTest, StartWhenServerAlreadyStarted) {
84 StartDummyProcess();
85
86 EXPECT_FALSE(dhcp_server_->Start());
87}
88
89TEST_F(DHCPServerTest, StartSuccess) {
90 const int kInterfaceIndex = 1;
91 EXPECT_CALL(*rtnl_handler_.get(), GetInterfaceIndex(kTestInterfaceName))
92 .WillOnce(Return(kInterfaceIndex));
93 EXPECT_CALL(*rtnl_handler_.get(),
94 AddInterfaceAddress(kInterfaceIndex, _, _, _)).Times(1);
95 EXPECT_CALL(*rtnl_handler_.get(),
96 SetInterfaceFlags(kInterfaceIndex, IFF_UP, IFF_UP)).Times(1);
97 // This will attempt to start a real dnsmasq process, and won't cause any
98 // trouble since the interface name specified is invalid. This can be avoid
99 // once we move to use ProcessFactory for process creation, which allows us
100 // to use the MockProcess instead of the real Process.
101 EXPECT_TRUE(dhcp_server_->Start());
102 Mock::VerifyAndClearExpectations(rtnl_handler_.get());
103}
104
105} // namespace apmanager