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/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"
19
20using extensions::api_test_utils::RunFunctionAndReturnSingleResult;
21
22namespace extensions {
23
24class DnsApiTest : public ShellApiTest {
25 public:
26  DnsApiTest() : resolver_creator_(new MockHostResolverCreator()) {}
27
28 private:
29  virtual void SetUpOnMainThread() OVERRIDE {
30    ShellApiTest::SetUpOnMainThread();
31    HostResolverWrapper::GetInstance()->SetHostResolverForTesting(
32        resolver_creator_->CreateMockHostResolver());
33  }
34
35  virtual void TearDownOnMainThread() OVERRIDE {
36    HostResolverWrapper::GetInstance()->SetHostResolverForTesting(NULL);
37    resolver_creator_->DeleteMockHostResolver();
38    ShellApiTest::TearDownOnMainThread();
39  }
40
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_;
45};
46
47IN_PROC_BROWSER_TEST_F(DnsApiTest, DnsResolveIPLiteral) {
48  scoped_refptr<DnsResolveFunction> resolve_function(new DnsResolveFunction());
49  scoped_refptr<Extension> empty_extension = test_util::CreateEmptyExtension();
50
51  resolve_function->set_extension(empty_extension.get());
52  resolve_function->set_has_callback(true);
53
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));
58
59  int result_code = 0;
60  EXPECT_TRUE(dict->GetInteger("resultCode", &result_code));
61  EXPECT_EQ(net::OK, result_code);
62
63  std::string address;
64  EXPECT_TRUE(dict->GetString("address", &address));
65  EXPECT_EQ("127.0.0.1", address);
66}
67
68IN_PROC_BROWSER_TEST_F(DnsApiTest, DnsResolveHostname) {
69  scoped_refptr<DnsResolveFunction> resolve_function(new DnsResolveFunction());
70  scoped_refptr<Extension> empty_extension = test_util::CreateEmptyExtension();
71
72  resolve_function->set_extension(empty_extension.get());
73  resolve_function->set_has_callback(true);
74
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));
82
83  int result_code = 0;
84  EXPECT_TRUE(dict->GetInteger("resultCode", &result_code));
85  EXPECT_EQ(net::OK, result_code);
86
87  std::string address;
88  EXPECT_TRUE(dict->GetString("address", &address));
89  EXPECT_EQ(MockHostResolverCreator::kAddress, address);
90}
91
92IN_PROC_BROWSER_TEST_F(DnsApiTest, DnsExtension) {
93  ASSERT_TRUE(RunAppTest("api_test/dns/api")) << message_;
94}
95
96}  // namespace extensions
97