1 // Copyright 2017 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 "extensions/browser/api/system_network/system_network_api.h"
6 #include "extensions/browser/api_test_utils.h"
7 #include "extensions/browser/api_unittest.h"
8 #include "extensions/common/extension_builder.h"
9 
10 using extensions::api_test_utils::RunFunctionAndReturnSingleResult;
11 using extensions::api::SystemNetworkGetNetworkInterfacesFunction;
12 using extensions::api::system_network::NetworkInterface;
13 
14 namespace extensions {
15 
16 namespace {
17 
18 using SystemNetworkApiUnitTest = extensions::ApiUnitTest;
19 
20 }  // namespace
21 
TEST_F(SystemNetworkApiUnitTest,GetNetworkInterfaces)22 TEST_F(SystemNetworkApiUnitTest, GetNetworkInterfaces) {
23   scoped_refptr<SystemNetworkGetNetworkInterfacesFunction> socket_function(
24       new SystemNetworkGetNetworkInterfacesFunction());
25   scoped_refptr<const Extension> empty_extension(
26       extensions::ExtensionBuilder("Test").Build());
27 
28   socket_function->set_extension(empty_extension.get());
29   socket_function->set_has_callback(true);
30 
31   std::unique_ptr<base::Value> result(RunFunctionAndReturnSingleResult(
32       socket_function.get(), "[]", browser_context()));
33   ASSERT_EQ(base::Value::Type::LIST, result->type());
34 
35   // All we can confirm is that we have at least one address, but not what it
36   // is.
37   base::ListValue* value = static_cast<base::ListValue*>(result.get());
38   ASSERT_TRUE(value->GetSize() > 0);
39 
40   for (const auto& network_interface_value : *value) {
41     NetworkInterface network_interface;
42     ASSERT_TRUE(NetworkInterface::Populate(network_interface_value,
43                                            &network_interface));
44 
45     LOG(INFO) << "Network interface: address=" << network_interface.address
46               << ", name=" << network_interface.name
47               << ", prefix length=" << network_interface.prefix_length;
48     ASSERT_NE(std::string(), network_interface.address);
49     ASSERT_NE(std::string(), network_interface.name);
50     ASSERT_LE(0, network_interface.prefix_length);
51   }
52 }
53 
54 }  // namespace extensions
55