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