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
3 // found in the LICENSE file.
5 #include "base/memory/ref_counted.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "chrome/browser/extensions/api/dns/mock_host_resolver_creator.h"
8 #include "chrome/browser/extensions/extension_apitest.h"
9 #include "chrome/browser/extensions/extension_function_test_utils.h"
10 #include "extensions/browser/api/dns/dns_api.h"
11 #include "extensions/browser/api/dns/host_resolver_wrapper.h"
12 #include "extensions/common/switches.h"
13 #include "net/base/net_errors.h"
14 #include "net/base/net_util.h"
15 #include "net/dns/mock_host_resolver.h"
17 using extension_function_test_utils::CreateEmptyExtension
;
18 using extension_function_test_utils::RunFunctionAndReturnSingleResult
;
22 class DnsApiTest
: public ExtensionApiTest
{
24 DnsApiTest() : resolver_event_(true, false),
25 resolver_creator_(new extensions::MockHostResolverCreator()) {
28 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
29 ExtensionApiTest::SetUpCommandLine(command_line
);
30 command_line
->AppendSwitch(
31 extensions::switches::kEnableExperimentalExtensionApis
);
34 virtual void SetUpOnMainThread() OVERRIDE
{
35 extensions::HostResolverWrapper::GetInstance()->SetHostResolverForTesting(
36 resolver_creator_
->CreateMockHostResolver());
39 virtual void CleanUpOnMainThread() OVERRIDE
{
40 extensions::HostResolverWrapper::GetInstance()->
41 SetHostResolverForTesting(NULL
);
42 resolver_creator_
->DeleteMockHostResolver();
46 base::WaitableEvent resolver_event_
;
48 // The MockHostResolver asserts that it's used on the same thread on which
49 // it's created, which is actually a stronger rule than its real counterpart.
50 // But that's fine; it's good practice.
51 scoped_refptr
<extensions::MockHostResolverCreator
> resolver_creator_
;
56 IN_PROC_BROWSER_TEST_F(DnsApiTest
, DnsResolveIPLiteral
) {
57 scoped_refptr
<extensions::DnsResolveFunction
> resolve_function(
58 new extensions::DnsResolveFunction());
59 scoped_refptr
<extensions::Extension
> empty_extension(CreateEmptyExtension());
61 resolve_function
->set_extension(empty_extension
.get());
62 resolve_function
->set_has_callback(true);
64 scoped_ptr
<base::Value
> result(RunFunctionAndReturnSingleResult(
65 resolve_function
.get(), "[\"127.0.0.1\"]", browser()));
66 ASSERT_EQ(base::Value::TYPE_DICTIONARY
, result
->GetType());
67 base::DictionaryValue
*value
=
68 static_cast<base::DictionaryValue
*>(result
.get());
71 EXPECT_TRUE(value
->GetInteger("resultCode", &resultCode
));
72 EXPECT_EQ(net::OK
, resultCode
);
75 EXPECT_TRUE(value
->GetString("address", &address
));
76 EXPECT_EQ("127.0.0.1", address
);
79 IN_PROC_BROWSER_TEST_F(DnsApiTest
, DnsResolveHostname
) {
80 scoped_refptr
<extensions::DnsResolveFunction
> resolve_function(
81 new extensions::DnsResolveFunction());
82 scoped_refptr
<extensions::Extension
> empty_extension(CreateEmptyExtension());
84 resolve_function
->set_extension(empty_extension
.get());
85 resolve_function
->set_has_callback(true);
87 std::string
function_arguments("[\"");
88 function_arguments
+= extensions::MockHostResolverCreator::kHostname
;
89 function_arguments
+= "\"]";
90 scoped_ptr
<base::Value
> result(
91 RunFunctionAndReturnSingleResult(resolve_function
.get(),
92 function_arguments
, browser()));
93 ASSERT_EQ(base::Value::TYPE_DICTIONARY
, result
->GetType());
94 base::DictionaryValue
*value
=
95 static_cast<base::DictionaryValue
*>(result
.get());
98 EXPECT_TRUE(value
->GetInteger("resultCode", &resultCode
));
99 EXPECT_EQ(net::OK
, resultCode
);
102 EXPECT_TRUE(value
->GetString("address", &address
));
103 EXPECT_EQ(extensions::MockHostResolverCreator::kAddress
, address
);
106 IN_PROC_BROWSER_TEST_F(DnsApiTest
, DnsExtension
) {
107 ASSERT_TRUE(RunExtensionTest("dns/api")) << message_
;