1 //
2 // Copyright (C) 2012 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "shill/resolver.h"
18
19 #include <base/files/file_util.h>
20 #include <base/files/scoped_temp_dir.h>
21 #include <base/stl_util.h>
22 #include <base/strings/stringprintf.h>
23 #include <gtest/gtest.h>
24
25 #include "shill/mock_control.h"
26
27 using base::FilePath;
28 using std::string;
29 using std::vector;
30 using testing::Test;
31
32 namespace shill {
33
34 namespace {
35 const char kNameServer0[] = "8.8.8.8";
36 const char kNameServer1[] = "8.8.9.9";
37 const char kSearchDomain0[] = "chromium.org";
38 const char kSearchDomain1[] = "google.com";
39 const char kSearchDomain2[] = "crbug.com";
40 const char kExpectedOutput[] =
41 "nameserver 8.8.8.8\n"
42 "nameserver 8.8.9.9\n"
43 "search chromium.org google.com\n"
44 "options single-request timeout:1 attempts:5\n";
45 const char kExpectedIgnoredSearchOutput[] =
46 "nameserver 8.8.8.8\n"
47 "nameserver 8.8.9.9\n"
48 "search google.com\n"
49 "options single-request timeout:1 attempts:5\n";
50 } // namespace
51
52 class ResolverTest : public Test {
53 public:
ResolverTest()54 ResolverTest() : resolver_(Resolver::GetInstance()) {}
55
SetUp()56 virtual void SetUp() {
57 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
58 path_ = temp_dir_.path().Append("resolver");
59 resolver_->set_path(path_);
60 }
61
TearDown()62 virtual void TearDown() {
63 resolver_->set_path(FilePath("")); // Don't try to save the store.
64 ASSERT_TRUE(temp_dir_.Delete());
65 }
66
67 protected:
68 string ReadFile();
69
70 base::ScopedTempDir temp_dir_;
71 Resolver* resolver_;
72 FilePath path_;
73 };
74
ReadFile()75 string ResolverTest::ReadFile() {
76 string data;
77 EXPECT_TRUE(base::ReadFileToString(resolver_->path_, &data));
78 return data;
79 }
80
TEST_F(ResolverTest,NonEmpty)81 TEST_F(ResolverTest, NonEmpty) {
82 EXPECT_FALSE(base::PathExists(path_));
83 EXPECT_TRUE(resolver_->ClearDNS());
84
85 MockControl control;
86 vector<string> dns_servers;
87 vector<string> domain_search;
88 dns_servers.push_back(kNameServer0);
89 dns_servers.push_back(kNameServer1);
90 domain_search.push_back(kSearchDomain0);
91 domain_search.push_back(kSearchDomain1);
92
93 EXPECT_TRUE(resolver_->SetDNSFromLists(dns_servers, domain_search));
94 EXPECT_TRUE(base::PathExists(path_));
95 EXPECT_EQ(kExpectedOutput, ReadFile());
96
97 EXPECT_TRUE(resolver_->ClearDNS());
98 }
99
TEST_F(ResolverTest,Empty)100 TEST_F(ResolverTest, Empty) {
101 EXPECT_FALSE(base::PathExists(path_));
102
103 MockControl control;
104 vector<string> dns_servers;
105 vector<string> domain_search;
106
107 EXPECT_TRUE(resolver_->SetDNSFromLists(dns_servers, domain_search));
108 EXPECT_FALSE(base::PathExists(path_));
109 }
110
TEST_F(ResolverTest,IgnoredSearchList)111 TEST_F(ResolverTest, IgnoredSearchList) {
112 EXPECT_FALSE(base::PathExists(path_));
113 EXPECT_TRUE(resolver_->ClearDNS());
114
115 MockControl control;
116 vector<string> dns_servers;
117 vector<string> domain_search;
118 dns_servers.push_back(kNameServer0);
119 dns_servers.push_back(kNameServer1);
120 domain_search.push_back(kSearchDomain0);
121 domain_search.push_back(kSearchDomain1);
122 vector<string> ignored_search;
123 ignored_search.push_back(kSearchDomain0);
124 ignored_search.push_back(kSearchDomain2);
125 resolver_->set_ignored_search_list(ignored_search);
126 EXPECT_TRUE(resolver_->SetDNSFromLists(dns_servers, domain_search));
127 EXPECT_TRUE(base::PathExists(path_));
128 EXPECT_EQ(kExpectedIgnoredSearchOutput, ReadFile());
129
130 EXPECT_TRUE(resolver_->ClearDNS());
131 }
132
133 } // namespace shill
134