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.
5 #include "base/memory/ref_counted.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/values.h"
8 #include "content/public/browser/notification_service.h"
9 #include "content/public/test/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/browser/api/dns/mock_host_resolver_creator.h"
13 #include "extensions/browser/api_test_utils.h"
14 #include "extensions/browser/notification_types.h"
15 #include "extensions/common/extension.h"
16 #include "extensions/common/test_util.h"
17 #include "extensions/shell/test/shell_apitest.h"
18 #include "net/base/net_errors.h"
20 using extensions::api_test_utils::RunFunctionAndReturnSingleResult
;
22 namespace extensions
{
24 class DnsApiTest
: public ShellApiTest
{
26 DnsApiTest() : resolver_creator_(new MockHostResolverCreator()) {}
29 void SetUpOnMainThread() override
{
30 ShellApiTest::SetUpOnMainThread();
31 HostResolverWrapper::GetInstance()->SetHostResolverForTesting(
32 resolver_creator_
->CreateMockHostResolver());
35 void TearDownOnMainThread() override
{
36 HostResolverWrapper::GetInstance()->SetHostResolverForTesting(NULL
);
37 resolver_creator_
->DeleteMockHostResolver();
38 ShellApiTest::TearDownOnMainThread();
41 // The MockHostResolver asserts that it's used on the same thread on which
42 // it's created, which is actually a stronger rule than its real counterpart.
43 // But that's fine; it's good practice.
44 scoped_refptr
<MockHostResolverCreator
> resolver_creator_
;
47 IN_PROC_BROWSER_TEST_F(DnsApiTest
, DnsResolveIPLiteral
) {
48 scoped_refptr
<DnsResolveFunction
> resolve_function(new DnsResolveFunction());
49 scoped_refptr
<Extension
> empty_extension
= test_util::CreateEmptyExtension();
51 resolve_function
->set_extension(empty_extension
.get());
52 resolve_function
->set_has_callback(true);
54 scoped_ptr
<base::Value
> result(RunFunctionAndReturnSingleResult(
55 resolve_function
.get(), "[\"127.0.0.1\"]", browser_context()));
56 base::DictionaryValue
* dict
= NULL
;
57 ASSERT_TRUE(result
->GetAsDictionary(&dict
));
60 EXPECT_TRUE(dict
->GetInteger("resultCode", &result_code
));
61 EXPECT_EQ(net::OK
, result_code
);
64 EXPECT_TRUE(dict
->GetString("address", &address
));
65 EXPECT_EQ("127.0.0.1", address
);
68 IN_PROC_BROWSER_TEST_F(DnsApiTest
, DnsResolveHostname
) {
69 scoped_refptr
<DnsResolveFunction
> resolve_function(new DnsResolveFunction());
70 scoped_refptr
<Extension
> empty_extension
= test_util::CreateEmptyExtension();
72 resolve_function
->set_extension(empty_extension
.get());
73 resolve_function
->set_has_callback(true);
75 std::string
function_arguments("[\"");
76 function_arguments
+= MockHostResolverCreator::kHostname
;
77 function_arguments
+= "\"]";
78 scoped_ptr
<base::Value
> result(RunFunctionAndReturnSingleResult(
79 resolve_function
.get(), function_arguments
, browser_context()));
80 base::DictionaryValue
* dict
= NULL
;
81 ASSERT_TRUE(result
->GetAsDictionary(&dict
));
84 EXPECT_TRUE(dict
->GetInteger("resultCode", &result_code
));
85 EXPECT_EQ(net::OK
, result_code
);
88 EXPECT_TRUE(dict
->GetString("address", &address
));
89 EXPECT_EQ(MockHostResolverCreator::kAddress
, address
);
92 IN_PROC_BROWSER_TEST_F(DnsApiTest
, DnsExtension
) {
93 ASSERT_TRUE(RunAppTest("api_test/dns/api")) << message_
;
96 } // namespace extensions