blob: 0885e44405fe6508cb9883d9d9ca36135f8f08f2 [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 Stewartabc41aa2012-04-07 13:15:37 -070032 "options single-request timeout:1\n";
Paul Stewartb6063942011-08-05 10:17:29 -070033} // namespace {}
34
35class ResolverTest : public Test {
36 public:
37 ResolverTest() : resolver_(Resolver::GetInstance()) {}
38
39 virtual void SetUp() {
40 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
41 path_ = temp_dir_.path().Append("resolver");
42 resolver_->set_path(path_);
43 }
44
45 virtual void TearDown() {
46 resolver_->set_path(FilePath("")); // Don't try to save the store.
47 ASSERT_TRUE(temp_dir_.Delete());
48 }
49
50 protected:
51 string ReadFile();
52
53 ScopedTempDir temp_dir_;
54 Resolver *resolver_;
55 FilePath path_;
56};
57
58string ResolverTest::ReadFile() {
59 string data;
60 EXPECT_TRUE(file_util::ReadFileToString(resolver_->path_, &data));
61 return data;
62}
63
64TEST_F(ResolverTest, NonEmpty) {
65 EXPECT_FALSE(file_util::PathExists(path_));
66 EXPECT_TRUE(resolver_->ClearDNS());
67
68 // Add DNS info from an IPConfig entry
69 MockControl control;
70 IPConfigRefPtr ipconfig(new IPConfig(&control, kTestDeviceName0));
71 IPConfig::Properties properties;
72 properties.dns_servers.push_back(kNameServer0);
73 properties.dns_servers.push_back(kNameServer1);
74 properties.domain_search.push_back(kSearchDomain0);
75 properties.domain_search.push_back(kSearchDomain1);
76 ipconfig->UpdateProperties(properties, true);
77
Paul Stewartdd60e452011-08-08 11:38:36 -070078 EXPECT_TRUE(resolver_->SetDNSFromIPConfig(ipconfig));
Paul Stewartb6063942011-08-05 10:17:29 -070079 EXPECT_TRUE(file_util::PathExists(path_));
80 EXPECT_EQ(kExpectedOutput, ReadFile());
81
82 EXPECT_TRUE(resolver_->ClearDNS());
83}
84
85TEST_F(ResolverTest, Empty) {
86 EXPECT_FALSE(file_util::PathExists(path_));
87
88 // Use empty ifconfig
89 MockControl control;
90 IPConfigRefPtr ipconfig(new IPConfig(&control, kTestDeviceName0));
91 IPConfig::Properties properties;
92 ipconfig->UpdateProperties(properties, true);
93
Paul Stewartdd60e452011-08-08 11:38:36 -070094 EXPECT_TRUE(resolver_->SetDNSFromIPConfig(ipconfig));
Paul Stewartb6063942011-08-05 10:17:29 -070095 EXPECT_FALSE(file_util::PathExists(path_));
96}
97
98} // namespace shill