blob: 41819f032970047a320d14fefaa80d77ad627599 [file] [log] [blame]
Paul Stewartb6063942011-08-05 10:17:29 -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/resolver.h"
6
7#include <base/file_util.h>
8#include <base/stl_util-inl.h>
9#include <base/stringprintf.h>
10#include <base/memory/scoped_temp_dir.h>
11#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"
31 "search chromium.org google.com\n";
32} // namespace {}
33
34class ResolverTest : public Test {
35 public:
36 ResolverTest() : resolver_(Resolver::GetInstance()) {}
37
38 virtual void SetUp() {
39 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
40 path_ = temp_dir_.path().Append("resolver");
41 resolver_->set_path(path_);
42 }
43
44 virtual void TearDown() {
45 resolver_->set_path(FilePath("")); // Don't try to save the store.
46 ASSERT_TRUE(temp_dir_.Delete());
47 }
48
49 protected:
50 string ReadFile();
51
52 ScopedTempDir temp_dir_;
53 Resolver *resolver_;
54 FilePath path_;
55};
56
57string ResolverTest::ReadFile() {
58 string data;
59 EXPECT_TRUE(file_util::ReadFileToString(resolver_->path_, &data));
60 return data;
61}
62
63TEST_F(ResolverTest, NonEmpty) {
64 EXPECT_FALSE(file_util::PathExists(path_));
65 EXPECT_TRUE(resolver_->ClearDNS());
66
67 // Add DNS info from an IPConfig entry
68 MockControl control;
69 IPConfigRefPtr ipconfig(new IPConfig(&control, kTestDeviceName0));
70 IPConfig::Properties properties;
71 properties.dns_servers.push_back(kNameServer0);
72 properties.dns_servers.push_back(kNameServer1);
73 properties.domain_search.push_back(kSearchDomain0);
74 properties.domain_search.push_back(kSearchDomain1);
75 ipconfig->UpdateProperties(properties, true);
76
Paul Stewartdd60e452011-08-08 11:38:36 -070077 EXPECT_TRUE(resolver_->SetDNSFromIPConfig(ipconfig));
Paul Stewartb6063942011-08-05 10:17:29 -070078 EXPECT_TRUE(file_util::PathExists(path_));
79 EXPECT_EQ(kExpectedOutput, ReadFile());
80
81 EXPECT_TRUE(resolver_->ClearDNS());
82}
83
84TEST_F(ResolverTest, Empty) {
85 EXPECT_FALSE(file_util::PathExists(path_));
86
87 // Use empty ifconfig
88 MockControl control;
89 IPConfigRefPtr ipconfig(new IPConfig(&control, kTestDeviceName0));
90 IPConfig::Properties properties;
91 ipconfig->UpdateProperties(properties, true);
92
Paul Stewartdd60e452011-08-08 11:38:36 -070093 EXPECT_TRUE(resolver_->SetDNSFromIPConfig(ipconfig));
Paul Stewartb6063942011-08-05 10:17:29 -070094 EXPECT_FALSE(file_util::PathExists(path_));
95}
96
97} // namespace shill