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 | bf66761 | 2012-06-29 14:49:54 -0700 | [diff] [blame] | 32 | "options single-request timeout:1 attempts:3\n"; |
| 33 | const char kExpectedShortTimeoutOutput[] = |
| 34 | "nameserver 8.8.8.8\n" |
| 35 | "nameserver 8.8.9.9\n" |
| 36 | "search chromium.org google.com\n" |
| 37 | "options single-request timeout-ms:300 attempts:15\n"; |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 38 | } // namespace {} |
| 39 | |
| 40 | class ResolverTest : public Test { |
| 41 | public: |
| 42 | ResolverTest() : resolver_(Resolver::GetInstance()) {} |
| 43 | |
| 44 | virtual void SetUp() { |
| 45 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 46 | path_ = temp_dir_.path().Append("resolver"); |
| 47 | resolver_->set_path(path_); |
| 48 | } |
| 49 | |
| 50 | virtual void TearDown() { |
| 51 | resolver_->set_path(FilePath("")); // Don't try to save the store. |
| 52 | ASSERT_TRUE(temp_dir_.Delete()); |
| 53 | } |
| 54 | |
| 55 | protected: |
| 56 | string ReadFile(); |
| 57 | |
| 58 | ScopedTempDir temp_dir_; |
| 59 | Resolver *resolver_; |
| 60 | FilePath path_; |
| 61 | }; |
| 62 | |
| 63 | string ResolverTest::ReadFile() { |
| 64 | string data; |
| 65 | EXPECT_TRUE(file_util::ReadFileToString(resolver_->path_, &data)); |
| 66 | return data; |
| 67 | } |
| 68 | |
| 69 | TEST_F(ResolverTest, NonEmpty) { |
| 70 | EXPECT_FALSE(file_util::PathExists(path_)); |
| 71 | EXPECT_TRUE(resolver_->ClearDNS()); |
| 72 | |
| 73 | // Add DNS info from an IPConfig entry |
| 74 | MockControl control; |
| 75 | IPConfigRefPtr ipconfig(new IPConfig(&control, kTestDeviceName0)); |
| 76 | IPConfig::Properties properties; |
| 77 | properties.dns_servers.push_back(kNameServer0); |
| 78 | properties.dns_servers.push_back(kNameServer1); |
| 79 | properties.domain_search.push_back(kSearchDomain0); |
| 80 | properties.domain_search.push_back(kSearchDomain1); |
| 81 | ipconfig->UpdateProperties(properties, true); |
| 82 | |
Paul Stewart | bf66761 | 2012-06-29 14:49:54 -0700 | [diff] [blame] | 83 | EXPECT_TRUE(resolver_->SetDNSFromIPConfig( |
| 84 | ipconfig, Resolver::kDefaultTimeout)); |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 85 | EXPECT_TRUE(file_util::PathExists(path_)); |
| 86 | EXPECT_EQ(kExpectedOutput, ReadFile()); |
| 87 | |
| 88 | EXPECT_TRUE(resolver_->ClearDNS()); |
| 89 | } |
| 90 | |
Paul Stewart | bf66761 | 2012-06-29 14:49:54 -0700 | [diff] [blame] | 91 | TEST_F(ResolverTest, ShortTimeout) { |
| 92 | EXPECT_FALSE(file_util::PathExists(path_)); |
| 93 | EXPECT_TRUE(resolver_->ClearDNS()); |
| 94 | |
| 95 | // Add DNS info from an IPConfig entry |
| 96 | MockControl control; |
| 97 | IPConfigRefPtr ipconfig(new IPConfig(&control, kTestDeviceName0)); |
| 98 | IPConfig::Properties properties; |
| 99 | properties.dns_servers.push_back(kNameServer0); |
| 100 | properties.dns_servers.push_back(kNameServer1); |
| 101 | properties.domain_search.push_back(kSearchDomain0); |
| 102 | properties.domain_search.push_back(kSearchDomain1); |
| 103 | ipconfig->UpdateProperties(properties, true); |
| 104 | |
| 105 | EXPECT_TRUE(resolver_->SetDNSFromIPConfig( |
| 106 | ipconfig, Resolver::kShortTimeout)); |
| 107 | EXPECT_TRUE(file_util::PathExists(path_)); |
| 108 | EXPECT_EQ(kExpectedShortTimeoutOutput, ReadFile()); |
| 109 | |
| 110 | EXPECT_TRUE(resolver_->ClearDNS()); |
| 111 | } |
| 112 | |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 113 | TEST_F(ResolverTest, Empty) { |
| 114 | EXPECT_FALSE(file_util::PathExists(path_)); |
| 115 | |
| 116 | // Use empty ifconfig |
| 117 | MockControl control; |
| 118 | IPConfigRefPtr ipconfig(new IPConfig(&control, kTestDeviceName0)); |
| 119 | IPConfig::Properties properties; |
| 120 | ipconfig->UpdateProperties(properties, true); |
| 121 | |
Paul Stewart | bf66761 | 2012-06-29 14:49:54 -0700 | [diff] [blame] | 122 | EXPECT_TRUE(resolver_->SetDNSFromIPConfig( |
| 123 | ipconfig, Resolver::kDefaultTimeout)); |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 124 | EXPECT_FALSE(file_util::PathExists(path_)); |
| 125 | } |
| 126 | |
| 127 | } // namespace shill |