Home | History | Annotate | Download | only in system_network
      1 // Copyright 2013 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/path_service.h"
      7 #include "base/strings/stringprintf.h"
      8 #include "chrome/browser/extensions/extension_apitest.h"
      9 #include "chrome/browser/extensions/extension_function_test_utils.h"
     10 #include "chrome/browser/extensions/extension_service.h"
     11 #include "chrome/browser/ui/browser.h"
     12 #include "chrome/browser/ui/extensions/application_launch.h"
     13 #include "chrome/test/base/in_process_browser_test.h"
     14 #include "chrome/test/base/ui_test_utils.h"
     15 #include "extensions/browser/api/system_network/system_network_api.h"
     16 #include "extensions/common/test_util.h"
     17 #include "extensions/test/extension_test_message_listener.h"
     18 
     19 using extensions::Extension;
     20 using extensions::core_api::SystemNetworkGetNetworkInterfacesFunction;
     21 using extensions::core_api::system_network::NetworkInterface;
     22 
     23 namespace utils = extension_function_test_utils;
     24 
     25 namespace {
     26 
     27 class SystemNetworkApiTest : public ExtensionApiTest {
     28 };
     29 
     30 }  // namespace
     31 
     32 IN_PROC_BROWSER_TEST_F(SystemNetworkApiTest, SystemNetworkExtension) {
     33   ASSERT_TRUE(RunExtensionTest("system/network")) << message_;
     34 }
     35 
     36 IN_PROC_BROWSER_TEST_F(SystemNetworkApiTest, GetNetworkInterfaces) {
     37   scoped_refptr<SystemNetworkGetNetworkInterfacesFunction> socket_function(
     38       new SystemNetworkGetNetworkInterfacesFunction());
     39   scoped_refptr<Extension> empty_extension(
     40       extensions::test_util::CreateEmptyExtension());
     41 
     42   socket_function->set_extension(empty_extension.get());
     43   socket_function->set_has_callback(true);
     44 
     45   scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult(
     46       socket_function.get(), "[]", browser(), utils::NONE));
     47   ASSERT_EQ(base::Value::TYPE_LIST, result->GetType());
     48 
     49   // All we can confirm is that we have at least one address, but not what it
     50   // is.
     51   base::ListValue *value = static_cast<base::ListValue*>(result.get());
     52   ASSERT_TRUE(value->GetSize() > 0);
     53 
     54   for (base::ListValue::const_iterator it = value->begin();
     55       it != value->end(); ++it) {
     56     base::Value* network_interface_value = *it;
     57 
     58     NetworkInterface network_interface;
     59     ASSERT_TRUE(NetworkInterface::Populate(*network_interface_value,
     60                                            &network_interface));
     61 
     62     LOG(INFO) << "Network interface: address=" << network_interface.address
     63               << ", name=" << network_interface.name
     64               << ", prefix length=" << network_interface.prefix_length;
     65     ASSERT_NE(std::string(), network_interface.address);
     66     ASSERT_NE(std::string(), network_interface.name);
     67     ASSERT_LE(0, network_interface.prefix_length);
     68   }
     69 }
     70