blob: 31501e2f07536c94a9fc8c30ff24a8d5f5714555 [file] [log] [blame]
Thieu Le3bd0faa2012-02-15 15:44:46 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewartb6063942011-08-05 10:17:29 -07002// 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/resolver.h"
6
7#include <base/file_util.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -05008#include <base/scoped_temp_dir.h>
9#include <base/stl_util.h>
Paul Stewartb6063942011-08-05 10:17:29 -070010#include <base/stringprintf.h>
Paul Stewartb6063942011-08-05 10:17:29 -070011#include <gtest/gtest.h>
12
13#include "shill/ipconfig.h"
14#include "shill/mock_control.h"
15
16using std::string;
17using std::vector;
18using testing::Test;
19
20namespace shill {
21
22namespace {
23const char kTestDeviceName0[] = "netdev0";
24const char kNameServer0[] = "8.8.8.8";
25const char kNameServer1[] = "8.8.9.9";
26const char kSearchDomain0[] = "chromium.org";
27const char kSearchDomain1[] = "google.com";
28const char kExpectedOutput[] =
29 "nameserver 8.8.8.8\n"
30 "nameserver 8.8.9.9\n"
Thieu Le3bd0faa2012-02-15 15:44:46 -080031 "search chromium.org google.com\n"
Paul Stewartbf667612012-06-29 14:49:54 -070032 "options single-request timeout:1 attempts:3\n";
33const char kExpectedShortTimeoutOutput[] =
34 "nameserver 8.8.8.8\n"
35 "nameserver 8.8.9.9\n"
36 "search chromium.org google.com\n"
37 "options single-request timeout-ms:300 attempts:15\n";
Paul Stewartb6063942011-08-05 10:17:29 -070038} // namespace {}
39
40class ResolverTest : public Test {
41 public:
42 ResolverTest() : resolver_(Resolver::GetInstance()) {}
43
44 virtual void SetUp() {
45 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
46 path_ = temp_dir_.path().Append("resolver");
47 resolver_->set_path(path_);
48 }
49
50 virtual void TearDown() {
51 resolver_->set_path(FilePath("")); // Don't try to save the store.
52 ASSERT_TRUE(temp_dir_.Delete());
53 }
54
55 protected:
56 string ReadFile();
57
58 ScopedTempDir temp_dir_;
59 Resolver *resolver_;
60 FilePath path_;
61};
62
63string ResolverTest::ReadFile() {
64 string data;
65 EXPECT_TRUE(file_util::ReadFileToString(resolver_->path_, &data));
66 return data;
67}
68
69TEST_F(ResolverTest, NonEmpty) {
70 EXPECT_FALSE(file_util::PathExists(path_));
71 EXPECT_TRUE(resolver_->ClearDNS());
72
73 // Add DNS info from an IPConfig entry
74 MockControl control;
75 IPConfigRefPtr ipconfig(new IPConfig(&control, kTestDeviceName0));
76 IPConfig::Properties properties;
77 properties.dns_servers.push_back(kNameServer0);
78 properties.dns_servers.push_back(kNameServer1);
79 properties.domain_search.push_back(kSearchDomain0);
80 properties.domain_search.push_back(kSearchDomain1);
81 ipconfig->UpdateProperties(properties, true);
82
Paul Stewartbf667612012-06-29 14:49:54 -070083 EXPECT_TRUE(resolver_->SetDNSFromIPConfig(
84 ipconfig, Resolver::kDefaultTimeout));
Paul Stewartb6063942011-08-05 10:17:29 -070085 EXPECT_TRUE(file_util::PathExists(path_));
86 EXPECT_EQ(kExpectedOutput, ReadFile());
87
88 EXPECT_TRUE(resolver_->ClearDNS());
89}
90
Paul Stewartbf667612012-06-29 14:49:54 -070091TEST_F(ResolverTest, ShortTimeout) {
92 EXPECT_FALSE(file_util::PathExists(path_));
93 EXPECT_TRUE(resolver_->ClearDNS());
94
95 // Add DNS info from an IPConfig entry
96 MockControl control;
97 IPConfigRefPtr ipconfig(new IPConfig(&control, kTestDeviceName0));
98 IPConfig::Properties properties;
99 properties.dns_servers.push_back(kNameServer0);
100 properties.dns_servers.push_back(kNameServer1);
101 properties.domain_search.push_back(kSearchDomain0);
102 properties.domain_search.push_back(kSearchDomain1);
103 ipconfig->UpdateProperties(properties, true);
104
105 EXPECT_TRUE(resolver_->SetDNSFromIPConfig(
106 ipconfig, Resolver::kShortTimeout));
107 EXPECT_TRUE(file_util::PathExists(path_));
108 EXPECT_EQ(kExpectedShortTimeoutOutput, ReadFile());
109
110 EXPECT_TRUE(resolver_->ClearDNS());
111}
112
Paul Stewartb6063942011-08-05 10:17:29 -0700113TEST_F(ResolverTest, Empty) {
114 EXPECT_FALSE(file_util::PathExists(path_));
115
116 // Use empty ifconfig
117 MockControl control;
118 IPConfigRefPtr ipconfig(new IPConfig(&control, kTestDeviceName0));
119 IPConfig::Properties properties;
120 ipconfig->UpdateProperties(properties, true);
121
Paul Stewartbf667612012-06-29 14:49:54 -0700122 EXPECT_TRUE(resolver_->SetDNSFromIPConfig(
123 ipconfig, Resolver::kDefaultTimeout));
Paul Stewartb6063942011-08-05 10:17:29 -0700124 EXPECT_FALSE(file_util::PathExists(path_));
125}
126
127} // namespace shill