blob: aa043e58ea94a27a2c640f791bd7e9df603c3f99 [file] [log] [blame]
Darin Petkove7cb7f82011-06-03 13:21:51 -07001// Copyright (c) 2011 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 "shill/dhcp_config.h"
6#include "shill/mock_control.h"
7#include "shill/mock_device.h"
8
9using std::string;
10using std::vector;
11using testing::Test;
12
13namespace shill {
14
15class DHCPConfigTest : public Test {
16 public:
17 DHCPConfigTest() :
18 device_(new MockDevice(&control_interface_, NULL, NULL, "testname", 0)) {}
19
20 protected:
21 MockControl control_interface_;
22 scoped_refptr<MockDevice> device_;
23};
24
25TEST_F(DHCPConfigTest, GetIPv4AddressString) {
26 DHCPConfigRefPtr config(new DHCPConfig(NULL, *device_));
27 EXPECT_EQ("255.255.255.255", config->GetIPv4AddressString(0xffffffff));
28 EXPECT_EQ("0.0.0.0", config->GetIPv4AddressString(0));
29 EXPECT_EQ("1.2.3.4", config->GetIPv4AddressString(0x04030201));
30}
31
32TEST_F(DHCPConfigTest, ParseConfiguration) {
33 DHCPConfig::Configuration conf;
34 conf[DHCPConfig::kConfigurationKeyIPAddress].writer().append_uint32(
35 0x01020304);
36 conf[DHCPConfig::kConfigurationKeySubnetCIDR].writer().append_byte(
37 16);
38 conf[DHCPConfig::kConfigurationKeyBroadcastAddress].writer().append_uint32(
39 0x10203040);
40 {
41 DBus::Variant var;
42 vector<unsigned int> routers;
43 routers.push_back(0x02040608);
44 routers.push_back(0x03050709);
45 DBus::MessageIter writer = var.writer();
46 writer << routers;
47 conf[DHCPConfig::kConfigurationKeyRouters] = var;
48 }
49 {
50 DBus::Variant var;
51 vector<unsigned int> dns;
52 dns.push_back(0x09070503);
53 dns.push_back(0x08060402);
54 DBus::MessageIter writer = var.writer();
55 writer << dns;
56 conf[DHCPConfig::kConfigurationKeyDNS] = var;
57 }
58 conf[DHCPConfig::kConfigurationKeyDomainName].writer().append_string(
59 "domain-name");
60 {
61 DBus::Variant var;
62 vector<string> search;
63 search.push_back("foo.com");
64 search.push_back("bar.com");
65 DBus::MessageIter writer = var.writer();
66 writer << search;
67 conf[DHCPConfig::kConfigurationKeyDomainSearch] = var;
68 }
69 conf[DHCPConfig::kConfigurationKeyMTU].writer().append_uint16(600);
70 conf["UnknownKey"] = DBus::Variant();
71
72 DHCPConfigRefPtr config(new DHCPConfig(NULL, *device_));
73 IPConfig::Properties properties;
74 ASSERT_TRUE(config->ParseConfiguration(conf, &properties));
75 EXPECT_EQ("4.3.2.1", properties.address);
76 EXPECT_EQ(16, properties.subnet_cidr);
77 EXPECT_EQ("64.48.32.16", properties.broadcast_address);
78 EXPECT_EQ("8.6.4.2", properties.gateway);
79 ASSERT_EQ(2, properties.dns_servers.size());
80 EXPECT_EQ("3.5.7.9", properties.dns_servers[0]);
81 EXPECT_EQ("2.4.6.8", properties.dns_servers[1]);
82 EXPECT_EQ("domain-name", properties.domain_name);
83 ASSERT_EQ(2, properties.domain_search.size());
84 EXPECT_EQ("foo.com", properties.domain_search[0]);
85 EXPECT_EQ("bar.com", properties.domain_search[1]);
86 EXPECT_EQ(600, properties.mtu);
87}
88
89} // namespace shill