OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium 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 "base/macros.h" | |
6 #include "base/memory/ref_counted.h" | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "base/values.h" | |
9 #include "extensions/browser/api/dns/dns_api.h" | |
10 #include "extensions/browser/api/dns/host_resolver_wrapper.h" | |
11 #include "extensions/browser/api/dns/mock_host_resolver_creator.h" | |
12 #include "extensions/browser/api_test_utils.h" | |
13 #include "extensions/browser/extension_function_dispatcher.h" | |
14 #include "extensions/common/extension.h" | |
15 #include "extensions/common/extension_builder.h" | |
16 #include "extensions/shell/test/shell_test.h" | |
17 #include "net/base/net_errors.h" | |
18 | |
19 using extensions::api_test_utils::RunFunctionAndReturnSingleResult; | |
20 | |
21 namespace extensions { | |
22 | |
23 namespace { | |
24 | |
25 class TestFunctionDispatcherDelegate | |
26 : public ExtensionFunctionDispatcher::Delegate { | |
27 public: | |
28 TestFunctionDispatcherDelegate() {} | |
29 virtual ~TestFunctionDispatcherDelegate() {} | |
30 | |
31 // NULL implementation. | |
32 private: | |
33 DISALLOW_COPY_AND_ASSIGN(TestFunctionDispatcherDelegate); | |
34 }; | |
35 | |
36 } // namespace | |
37 | |
38 class DnsApiTest : public AppShellTest { | |
39 public: | |
40 DnsApiTest() : resolver_creator_(new MockHostResolverCreator()) {} | |
41 | |
42 private: | |
43 virtual void SetUpOnMainThread() OVERRIDE { | |
44 AppShellTest::SetUpOnMainThread(); | |
45 HostResolverWrapper::GetInstance()->SetHostResolverForTesting( | |
46 resolver_creator_->CreateMockHostResolver()); | |
47 } | |
48 | |
49 virtual void TearDownOnMainThread() OVERRIDE { | |
50 HostResolverWrapper::GetInstance()->SetHostResolverForTesting(NULL); | |
51 resolver_creator_->DeleteMockHostResolver(); | |
52 AppShellTest::TearDownOnMainThread(); | |
53 } | |
54 | |
55 // The MockHostResolver asserts that it's used on the same thread on which | |
56 // it's created, which is actually a stronger rule than its real counterpart. | |
57 // But that's fine; it's good practice. | |
58 scoped_refptr<MockHostResolverCreator> resolver_creator_; | |
59 }; | |
60 | |
61 IN_PROC_BROWSER_TEST_F(DnsApiTest, DnsResolveIPLiteral) { | |
62 scoped_refptr<DnsResolveFunction> resolve_function(new DnsResolveFunction()); | |
63 scoped_refptr<Extension> empty_extension( | |
64 ExtensionBuilder() | |
65 .SetManifest( | |
66 DictionaryBuilder().Set("name", "Test").Set("version", "1.0")) | |
67 .Build()); | |
68 | |
69 resolve_function->set_extension(empty_extension.get()); | |
70 resolve_function->set_has_callback(true); | |
71 | |
72 TestFunctionDispatcherDelegate delegate; | |
73 scoped_ptr<ExtensionFunctionDispatcher> dispatcher( | |
74 new ExtensionFunctionDispatcher(browser_context(), &delegate)); | |
75 | |
76 scoped_ptr<base::Value> result( | |
77 RunFunctionAndReturnSingleResult(resolve_function.get(), | |
78 "[\"127.0.0.1\"]", | |
79 browser_context(), | |
80 dispatcher.Pass())); | |
81 base::DictionaryValue* dict = NULL; | |
82 ASSERT_TRUE(result->GetAsDictionary(&dict)); | |
83 | |
84 int result_code = 0; | |
85 EXPECT_TRUE(dict->GetInteger("resultCode", &result_code)); | |
86 EXPECT_EQ(net::OK, result_code); | |
87 | |
88 std::string address; | |
89 EXPECT_TRUE(dict->GetString("address", &address)); | |
90 EXPECT_EQ("127.0.0.1", address); | |
91 } | |
92 | |
93 IN_PROC_BROWSER_TEST_F(DnsApiTest, DnsResolveHostname) { | |
94 scoped_refptr<DnsResolveFunction> resolve_function(new DnsResolveFunction()); | |
95 scoped_refptr<Extension> empty_extension( | |
96 ExtensionBuilder() | |
97 .SetManifest( | |
98 DictionaryBuilder().Set("name", "Test").Set("version", "1.0")) | |
99 .Build()); | |
100 | |
101 resolve_function->set_extension(empty_extension.get()); | |
102 resolve_function->set_has_callback(true); | |
103 | |
104 TestFunctionDispatcherDelegate delegate; | |
105 scoped_ptr<ExtensionFunctionDispatcher> dispatcher( | |
106 new ExtensionFunctionDispatcher(browser_context(), &delegate)); | |
107 | |
108 std::string function_arguments("[\""); | |
109 function_arguments += MockHostResolverCreator::kHostname; | |
110 function_arguments += "\"]"; | |
111 scoped_ptr<base::Value> result( | |
112 RunFunctionAndReturnSingleResult(resolve_function.get(), | |
113 function_arguments, | |
114 browser_context(), | |
115 dispatcher.Pass())); | |
116 base::DictionaryValue* dict = NULL; | |
117 ASSERT_TRUE(result->GetAsDictionary(&dict)); | |
118 | |
119 int result_code = 0; | |
120 EXPECT_TRUE(dict->GetInteger("resultCode", &result_code)); | |
121 EXPECT_EQ(net::OK, result_code); | |
122 | |
123 std::string address; | |
124 EXPECT_TRUE(dict->GetString("address", &address)); | |
125 EXPECT_EQ(MockHostResolverCreator::kAddress, address); | |
126 } | |
127 | |
128 } // namespace extensions | |
OLD | NEW |