blob: 8b489fab333186d3c92af9484a968240f3c42edf [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"
33 "dhcp-range=192.168.1.1,192.168.1.128\n"
34 "interface=test_interface\n"
35 "dhcp-leasefile=/tmp/dhcpd-1.leases\n";
36} // namespace
37
38namespace apmanager {
39
40class DHCPServerTest : public testing::Test {
41 public:
42 DHCPServerTest()
43 : dhcp_server_(new DHCPServer(kServerAddressIndex, kTestInterfaceName)),
44 rtnl_handler_(new shill::MockRTNLHandler()) {}
45 virtual ~DHCPServerTest() {}
46
47 virtual void SetUp() {
48 dhcp_server_->rtnl_handler_ = rtnl_handler_.get();
49 }
50
51 virtual void TearDown() {
52 // Reset DHCP server now while RTNLHandler is still valid.
53 dhcp_server_.reset();
54 }
55
56 void StartDummyProcess() {
57 dhcp_server_->dnsmasq_process_.reset(new chromeos::ProcessImpl);
58 dhcp_server_->dnsmasq_process_->AddArg(kBinSleep);
59 dhcp_server_->dnsmasq_process_->AddArg("12345");
60 CHECK(dhcp_server_->dnsmasq_process_->Start());
61 }
62
63 string GenerateConfigFile() {
64 return dhcp_server_->GenerateConfigFile();
65 }
66
67 protected:
68 std::unique_ptr<DHCPServer> dhcp_server_;
69 std::unique_ptr<shill::MockRTNLHandler> rtnl_handler_;
70};
71
72
73TEST_F(DHCPServerTest, GenerateConfigFile) {
74 string config_content = GenerateConfigFile();
75 EXPECT_STREQ(kExpectedDnsmasqConfigFile, config_content.c_str())
76 << "Expected to find the following config...\n"
77 << kExpectedDnsmasqConfigFile << ".....\n"
78 << config_content;
79}
80
81TEST_F(DHCPServerTest, StartWhenServerAlreadyStarted) {
82 StartDummyProcess();
83
84 EXPECT_FALSE(dhcp_server_->Start());
85}
86
87TEST_F(DHCPServerTest, StartSuccess) {
88 const int kInterfaceIndex = 1;
89 EXPECT_CALL(*rtnl_handler_.get(), GetInterfaceIndex(kTestInterfaceName))
90 .WillOnce(Return(kInterfaceIndex));
91 EXPECT_CALL(*rtnl_handler_.get(),
92 AddInterfaceAddress(kInterfaceIndex, _, _, _)).Times(1);
93 EXPECT_CALL(*rtnl_handler_.get(),
94 SetInterfaceFlags(kInterfaceIndex, IFF_UP, IFF_UP)).Times(1);
95 // This will attempt to start a real dnsmasq process, and won't cause any
96 // trouble since the interface name specified is invalid. This can be avoid
97 // once we move to use ProcessFactory for process creation, which allows us
98 // to use the MockProcess instead of the real Process.
99 EXPECT_TRUE(dhcp_server_->Start());
100 Mock::VerifyAndClearExpectations(rtnl_handler_.get());
101}
102
103} // namespace apmanager