OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "base/synchronization/waitable_event.h" | 7 #include "base/synchronization/waitable_event.h" |
8 #include "chrome/browser/extensions/api/dns/dns_api.h" | 8 #include "chrome/browser/extensions/api/dns/dns_api.h" |
9 #include "chrome/browser/extensions/extension_apitest.h" | 9 #include "chrome/browser/extensions/extension_apitest.h" |
10 #include "chrome/browser/extensions/extension_function_test_utils.h" | 10 #include "chrome/browser/extensions/extension_function_test_utils.h" |
11 #include "chrome/common/chrome_switches.h" | 11 #include "chrome/common/chrome_switches.h" |
12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
13 #include "net/base/host_resolver.h" | 13 #include "net/base/host_resolver.h" |
14 #include "net/base/mock_host_resolver.h" | 14 #include "net/base/mock_host_resolver.h" |
15 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
16 #include "net/base/net_util.h" | 16 #include "net/base/net_util.h" |
17 | 17 |
18 using content::BrowserThread; | 18 using content::BrowserThread; |
19 using extension_function_test_utils::CreateEmptyExtension; | 19 using extension_function_test_utils::CreateEmptyExtension; |
20 using extension_function_test_utils::RunFunctionAndReturnResult; | 20 using extension_function_test_utils::RunFunctionAndReturnResult; |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 class DNSApiTest : public ExtensionApiTest { | 24 class DnsApiTest : public ExtensionApiTest { |
25 public: | 25 public: |
26 static const std::string kHostname; | 26 static const std::string kHostname; |
27 static const std::string kAddress; | 27 static const std::string kAddress; |
28 | 28 |
29 DNSApiTest() : resolver_event_(true, false), | 29 DnsApiTest() : resolver_event_(true, false), |
30 mock_host_resolver_(NULL) { | 30 mock_host_resolver_(NULL) { |
31 } | 31 } |
32 | 32 |
33 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 33 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
34 ExtensionApiTest::SetUpCommandLine(command_line); | 34 ExtensionApiTest::SetUpCommandLine(command_line); |
35 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); | 35 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); |
36 command_line->AppendSwitch(switches::kEnablePlatformApps); | 36 command_line->AppendSwitch(switches::kEnablePlatformApps); |
37 } | 37 } |
38 | 38 |
39 virtual void SetUpOnMainThread() OVERRIDE { | 39 virtual void SetUpOnMainThread() OVERRIDE { |
40 CreateMockHostResolverOnIOThread(); | 40 CreateMockHostResolverOnIOThread(); |
41 extensions::DNSResolveFunction::set_host_resolver_for_testing( | 41 extensions::DnsResolveFunction::set_host_resolver_for_testing( |
42 get_mock_host_resolver()); | 42 get_mock_host_resolver()); |
43 } | 43 } |
44 | 44 |
45 virtual void CleanUpOnMainThread() OVERRIDE { | 45 virtual void CleanUpOnMainThread() OVERRIDE { |
46 if (mock_host_resolver_) { | 46 if (mock_host_resolver_) { |
47 extensions::DNSResolveFunction::set_host_resolver_for_testing(NULL); | 47 extensions::DnsResolveFunction::set_host_resolver_for_testing(NULL); |
48 DeleteMockHostResolverOnIOThread(); | 48 DeleteMockHostResolverOnIOThread(); |
49 } | 49 } |
50 } | 50 } |
51 | 51 |
52 void CreateMockHostResolverOnIOThread() { | 52 void CreateMockHostResolverOnIOThread() { |
53 bool result = BrowserThread::PostTask( | 53 bool result = BrowserThread::PostTask( |
54 BrowserThread::IO, FROM_HERE, | 54 BrowserThread::IO, FROM_HERE, |
55 base::Bind(&DNSApiTest::FinishMockHostResolverCreation, this)); | 55 base::Bind(&DnsApiTest::FinishMockHostResolverCreation, this)); |
56 DCHECK(result); | 56 DCHECK(result); |
57 | 57 |
58 base::TimeDelta max_time = base::TimeDelta::FromSeconds(5); | 58 base::TimeDelta max_time = base::TimeDelta::FromSeconds(5); |
59 ASSERT_TRUE(resolver_event_.TimedWait(max_time)); | 59 ASSERT_TRUE(resolver_event_.TimedWait(max_time)); |
60 | 60 |
61 } | 61 } |
62 | 62 |
63 void FinishMockHostResolverCreation() { | 63 void FinishMockHostResolverCreation() { |
64 mock_host_resolver_ = new net::MockHostResolver(); | 64 mock_host_resolver_ = new net::MockHostResolver(); |
65 get_mock_host_resolver()->rules()->AddRule(kHostname, kAddress); | 65 get_mock_host_resolver()->rules()->AddRule(kHostname, kAddress); |
66 get_mock_host_resolver()->rules()->AddSimulatedFailure( | 66 get_mock_host_resolver()->rules()->AddSimulatedFailure( |
67 "this.hostname.is.bogus"); | 67 "this.hostname.is.bogus"); |
68 resolver_event_.Signal(); | 68 resolver_event_.Signal(); |
69 } | 69 } |
70 | 70 |
71 void DeleteMockHostResolverOnIOThread() { | 71 void DeleteMockHostResolverOnIOThread() { |
72 if (!mock_host_resolver_) | 72 if (!mock_host_resolver_) |
73 return; | 73 return; |
74 resolver_event_.Reset(); | 74 resolver_event_.Reset(); |
75 bool result = BrowserThread::PostTask( | 75 bool result = BrowserThread::PostTask( |
76 BrowserThread::IO, FROM_HERE, | 76 BrowserThread::IO, FROM_HERE, |
77 base::Bind(&DNSApiTest::FinishMockHostResolverDeletion, this)); | 77 base::Bind(&DnsApiTest::FinishMockHostResolverDeletion, this)); |
78 DCHECK(result); | 78 DCHECK(result); |
79 | 79 |
80 base::TimeDelta max_time = base::TimeDelta::FromSeconds(5); | 80 base::TimeDelta max_time = base::TimeDelta::FromSeconds(5); |
81 ASSERT_TRUE(resolver_event_.TimedWait(max_time)); | 81 ASSERT_TRUE(resolver_event_.TimedWait(max_time)); |
82 } | 82 } |
83 | 83 |
84 void FinishMockHostResolverDeletion() { | 84 void FinishMockHostResolverDeletion() { |
85 delete(mock_host_resolver_); | 85 delete(mock_host_resolver_); |
86 resolver_event_.Signal(); | 86 resolver_event_.Signal(); |
87 } | 87 } |
88 | 88 |
89 net::MockHostResolver* get_mock_host_resolver() { | 89 net::MockHostResolver* get_mock_host_resolver() { |
90 return mock_host_resolver_; | 90 return mock_host_resolver_; |
91 } | 91 } |
92 | 92 |
93 private: | 93 private: |
94 base::WaitableEvent resolver_event_; | 94 base::WaitableEvent resolver_event_; |
95 | 95 |
96 // The MockHostResolver asserts that it's used on the same thread on which | 96 // The MockHostResolver asserts that it's used on the same thread on which |
97 // it's created, which is actually a stronger rule than its real counterpart. | 97 // it's created, which is actually a stronger rule than its real counterpart. |
98 // But that's fine; it's good practice. | 98 // But that's fine; it's good practice. |
99 // | 99 // |
100 // Plain pointer because we have to manage lifetime manually. | 100 // Plain pointer because we have to manage lifetime manually. |
101 net::MockHostResolver* mock_host_resolver_; | 101 net::MockHostResolver* mock_host_resolver_; |
102 }; | 102 }; |
103 const std::string DNSApiTest::kHostname = "www.sowbug.org"; | 103 const std::string DnsApiTest::kHostname = "www.sowbug.org"; |
104 const std::string DNSApiTest::kAddress = "9.8.7.6"; | 104 const std::string DnsApiTest::kAddress = "9.8.7.6"; |
105 | 105 |
106 } // namespace | 106 } // namespace |
107 | 107 |
108 IN_PROC_BROWSER_TEST_F(DNSApiTest, DNSResolveIPLiteral) { | 108 IN_PROC_BROWSER_TEST_F(DnsApiTest, DnsResolveIPLiteral) { |
109 scoped_refptr<extensions::DNSResolveFunction> resolve_function( | 109 scoped_refptr<extensions::DnsResolveFunction> resolve_function( |
110 new extensions::DNSResolveFunction()); | 110 new extensions::DnsResolveFunction()); |
111 scoped_refptr<Extension> empty_extension(CreateEmptyExtension()); | 111 scoped_refptr<Extension> empty_extension(CreateEmptyExtension()); |
112 | 112 |
113 resolve_function->set_extension(empty_extension.get()); | 113 resolve_function->set_extension(empty_extension.get()); |
114 resolve_function->set_has_callback(true); | 114 resolve_function->set_has_callback(true); |
115 | 115 |
116 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( | 116 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( |
117 resolve_function, "[\"127.0.0.1\"]", browser())); | 117 resolve_function, "[\"127.0.0.1\"]", browser())); |
118 ASSERT_EQ(base::Value::TYPE_DICTIONARY, result->GetType()); | 118 ASSERT_EQ(base::Value::TYPE_DICTIONARY, result->GetType()); |
119 DictionaryValue *value = static_cast<DictionaryValue*>(result.get()); | 119 DictionaryValue *value = static_cast<DictionaryValue*>(result.get()); |
120 | 120 |
121 int resultCode; | 121 int resultCode; |
122 EXPECT_TRUE(value->GetInteger("resultCode", &resultCode)); | 122 EXPECT_TRUE(value->GetInteger("resultCode", &resultCode)); |
123 EXPECT_EQ(net::OK, resultCode); | 123 EXPECT_EQ(net::OK, resultCode); |
124 | 124 |
125 std::string address; | 125 std::string address; |
126 EXPECT_TRUE(value->GetString("address", &address)); | 126 EXPECT_TRUE(value->GetString("address", &address)); |
127 EXPECT_EQ("127.0.0.1", address); | 127 EXPECT_EQ("127.0.0.1", address); |
128 } | 128 } |
129 | 129 |
130 IN_PROC_BROWSER_TEST_F(DNSApiTest, DNSResolveHostname) { | 130 IN_PROC_BROWSER_TEST_F(DnsApiTest, DnsResolveHostname) { |
131 scoped_refptr<extensions::DNSResolveFunction> resolve_function( | 131 scoped_refptr<extensions::DnsResolveFunction> resolve_function( |
132 new extensions::DNSResolveFunction()); | 132 new extensions::DnsResolveFunction()); |
133 scoped_refptr<Extension> empty_extension(CreateEmptyExtension()); | 133 scoped_refptr<Extension> empty_extension(CreateEmptyExtension()); |
134 | 134 |
135 resolve_function->set_extension(empty_extension.get()); | 135 resolve_function->set_extension(empty_extension.get()); |
136 resolve_function->set_has_callback(true); | 136 resolve_function->set_has_callback(true); |
137 | 137 |
138 std::string function_arguments("[\""); | 138 std::string function_arguments("[\""); |
139 function_arguments += DNSApiTest::kHostname; | 139 function_arguments += DnsApiTest::kHostname; |
140 function_arguments += "\"]"; | 140 function_arguments += "\"]"; |
141 scoped_ptr<base::Value> result( | 141 scoped_ptr<base::Value> result( |
142 RunFunctionAndReturnResult(resolve_function.get(), | 142 RunFunctionAndReturnResult(resolve_function.get(), |
143 function_arguments, browser())); | 143 function_arguments, browser())); |
144 ASSERT_EQ(base::Value::TYPE_DICTIONARY, result->GetType()); | 144 ASSERT_EQ(base::Value::TYPE_DICTIONARY, result->GetType()); |
145 DictionaryValue *value = static_cast<DictionaryValue*>(result.get()); | 145 DictionaryValue *value = static_cast<DictionaryValue*>(result.get()); |
146 | 146 |
147 int resultCode; | 147 int resultCode; |
148 EXPECT_TRUE(value->GetInteger("resultCode", &resultCode)); | 148 EXPECT_TRUE(value->GetInteger("resultCode", &resultCode)); |
149 EXPECT_EQ(net::OK, resultCode); | 149 EXPECT_EQ(net::OK, resultCode); |
150 | 150 |
151 std::string address; | 151 std::string address; |
152 EXPECT_TRUE(value->GetString("address", &address)); | 152 EXPECT_TRUE(value->GetString("address", &address)); |
153 EXPECT_EQ(DNSApiTest::kAddress, address); | 153 EXPECT_EQ(DnsApiTest::kAddress, address); |
154 } | 154 } |
155 | 155 |
156 IN_PROC_BROWSER_TEST_F(DNSApiTest, DNSExtension) { | 156 IN_PROC_BROWSER_TEST_F(DnsApiTest, DnsExtension) { |
157 ASSERT_TRUE(RunExtensionTest("dns/api")) << message_; | 157 ASSERT_TRUE(RunExtensionTest("dns/api")) << message_; |
158 } | 158 } |
OLD | NEW |