Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 1 | // 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 | |
| 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" |
| 31 | "search chromium.org google.com\n"; |
| 32 | } // namespace {} |
| 33 | |
| 34 | class 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 | |
| 57 | string ResolverTest::ReadFile() { |
| 58 | string data; |
| 59 | EXPECT_TRUE(file_util::ReadFileToString(resolver_->path_, &data)); |
| 60 | return data; |
| 61 | } |
| 62 | |
| 63 | TEST_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 Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 77 | EXPECT_TRUE(resolver_->SetDNSFromIPConfig(ipconfig)); |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 78 | EXPECT_TRUE(file_util::PathExists(path_)); |
| 79 | EXPECT_EQ(kExpectedOutput, ReadFile()); |
| 80 | |
| 81 | EXPECT_TRUE(resolver_->ClearDNS()); |
| 82 | } |
| 83 | |
| 84 | TEST_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 Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 93 | EXPECT_TRUE(resolver_->SetDNSFromIPConfig(ipconfig)); |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 94 | EXPECT_FALSE(file_util::PathExists(path_)); |
| 95 | } |
| 96 | |
| 97 | } // namespace shill |