Home | History | Annotate | Download | only in dns
      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.
      4 
      5 #include "base/memory/ref_counted.h"
      6 #include "base/memory/scoped_ptr.h"
      7 #include "chrome/browser/extensions/api/dns/dns_api.h"
      8 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h"
      9 #include "chrome/browser/extensions/api/dns/mock_host_resolver_creator.h"
     10 #include "chrome/browser/extensions/extension_apitest.h"
     11 #include "chrome/browser/extensions/extension_function_test_utils.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"
     16 
     17 using extension_function_test_utils::CreateEmptyExtension;
     18 using extension_function_test_utils::RunFunctionAndReturnSingleResult;
     19 
     20 namespace {
     21 
     22 class DnsApiTest : public ExtensionApiTest {
     23  public:
     24   DnsApiTest() : resolver_event_(true, false),
     25                  resolver_creator_(new extensions::MockHostResolverCreator()) {
     26   }
     27 
     28   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
     29     ExtensionApiTest::SetUpCommandLine(command_line);
     30     command_line->AppendSwitch(
     31         extensions::switches::kEnableExperimentalExtensionApis);
     32   }
     33 
     34   virtual void SetUpOnMainThread() OVERRIDE {
     35     extensions::HostResolverWrapper::GetInstance()->SetHostResolverForTesting(
     36         resolver_creator_->CreateMockHostResolver());
     37   }
     38 
     39   virtual void CleanUpOnMainThread() OVERRIDE {
     40     extensions::HostResolverWrapper::GetInstance()->
     41         SetHostResolverForTesting(NULL);
     42     resolver_creator_->DeleteMockHostResolver();
     43   }
     44 
     45  private:
     46   base::WaitableEvent resolver_event_;
     47 
     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_;
     52 };
     53 
     54 }  // namespace
     55 
     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());
     60 
     61   resolve_function->set_extension(empty_extension.get());
     62   resolve_function->set_has_callback(true);
     63 
     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());
     69 
     70   int resultCode;
     71   EXPECT_TRUE(value->GetInteger("resultCode", &resultCode));
     72   EXPECT_EQ(net::OK, resultCode);
     73 
     74   std::string address;
     75   EXPECT_TRUE(value->GetString("address", &address));
     76   EXPECT_EQ("127.0.0.1", address);
     77 }
     78 
     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());
     83 
     84   resolve_function->set_extension(empty_extension.get());
     85   resolve_function->set_has_callback(true);
     86 
     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());
     96 
     97   int resultCode;
     98   EXPECT_TRUE(value->GetInteger("resultCode", &resultCode));
     99   EXPECT_EQ(net::OK, resultCode);
    100 
    101   std::string address;
    102   EXPECT_TRUE(value->GetString("address", &address));
    103   EXPECT_EQ(extensions::MockHostResolverCreator::kAddress, address);
    104 }
    105 
    106 IN_PROC_BROWSER_TEST_F(DnsApiTest, DnsExtension) {
    107   ASSERT_TRUE(RunExtensionTest("dns/api")) << message_;
    108 }
    109