Thieu Le | 3bd0faa | 2012-02-15 15:44:46 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 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> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 8 | #include <base/scoped_temp_dir.h> |
| 9 | #include <base/stl_util.h> |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 10 | #include <base/stringprintf.h> |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 11 | #include <gtest/gtest.h> |
| 12 | |
| 13 | #include "shill/ipconfig.h" |
| 14 | #include "shill/mock_control.h" |
| 15 | |
| 16 | using std::string; |
| 17 | using std::vector; |
| 18 | using testing::Test; |
| 19 | |
| 20 | namespace shill { |
| 21 | |
| 22 | namespace { |
| 23 | const char kTestDeviceName0[] = "netdev0"; |
| 24 | const char kNameServer0[] = "8.8.8.8"; |
| 25 | const char kNameServer1[] = "8.8.9.9"; |
| 26 | const char kSearchDomain0[] = "chromium.org"; |
| 27 | const char kSearchDomain1[] = "google.com"; |
| 28 | const char kExpectedOutput[] = |
| 29 | "nameserver 8.8.8.8\n" |
| 30 | "nameserver 8.8.9.9\n" |
Thieu Le | 3bd0faa | 2012-02-15 15:44:46 -0800 | [diff] [blame] | 31 | "search chromium.org google.com\n" |
Paul Stewart | abc41aa | 2012-04-07 13:15:37 -0700 | [diff] [blame] | 32 | "options single-request timeout:1\n"; |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 33 | } // namespace {} |
| 34 | |
| 35 | class 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 | |
| 58 | string ResolverTest::ReadFile() { |
| 59 | string data; |
| 60 | EXPECT_TRUE(file_util::ReadFileToString(resolver_->path_, &data)); |
| 61 | return data; |
| 62 | } |
| 63 | |
| 64 | TEST_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 Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 78 | EXPECT_TRUE(resolver_->SetDNSFromIPConfig(ipconfig)); |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 79 | EXPECT_TRUE(file_util::PathExists(path_)); |
| 80 | EXPECT_EQ(kExpectedOutput, ReadFile()); |
| 81 | |
| 82 | EXPECT_TRUE(resolver_->ClearDNS()); |
| 83 | } |
| 84 | |
| 85 | TEST_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 Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 94 | EXPECT_TRUE(resolver_->SetDNSFromIPConfig(ipconfig)); |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 95 | EXPECT_FALSE(file_util::PathExists(path_)); |
| 96 | } |
| 97 | |
| 98 | } // namespace shill |