• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
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 "net/base/network_interfaces.h"
6 
7 #include <ostream>
8 #include <string>
9 #include <unordered_set>
10 
11 #include "base/strings/utf_string_conversions.h"
12 #include "build/build_config.h"
13 #include "net/base/ip_endpoint.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 
16 #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID)
17 #include <net/if.h>
18 #elif BUILDFLAG(IS_WIN)
19 #include <windows.h>
20 
21 #include <iphlpapi.h>
22 #include <objbase.h>
23 #include "base/strings/string_util.h"
24 #include "base/win/win_util.h"
25 #endif
26 
27 namespace net {
28 
29 namespace {
30 
31 // Verify GetNetworkList().
TEST(NetworkInterfacesTest,GetNetworkList)32 TEST(NetworkInterfacesTest, GetNetworkList) {
33   NetworkInterfaceList list;
34   ASSERT_TRUE(GetNetworkList(&list, INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES));
35   for (auto it = list.begin(); it != list.end(); ++it) {
36     // Verify that the names are not empty.
37     EXPECT_FALSE(it->name.empty());
38     EXPECT_FALSE(it->friendly_name.empty());
39 
40     // Verify that the address is correct.
41     EXPECT_TRUE(it->address.IsValid()) << "Invalid address of size "
42                                        << it->address.size();
43     EXPECT_FALSE(it->address.IsZero());
44     EXPECT_GT(it->prefix_length, 1u);
45     EXPECT_LE(it->prefix_length, it->address.size() * 8);
46 
47 #if BUILDFLAG(IS_WIN)
48     // On Windows |name| is NET_LUID.
49     NET_LUID luid;
50     EXPECT_EQ(static_cast<DWORD>(NO_ERROR),
51               ConvertInterfaceIndexToLuid(it->interface_index, &luid));
52     GUID guid;
53     EXPECT_EQ(static_cast<DWORD>(NO_ERROR),
54               ConvertInterfaceLuidToGuid(&luid, &guid));
55     auto name = base::win::WStringFromGUID(guid);
56     EXPECT_EQ(base::UTF8ToWide(it->name), name);
57 
58     if (it->type == NetworkChangeNotifier::CONNECTION_WIFI) {
59       EXPECT_NE(WIFI_PHY_LAYER_PROTOCOL_NONE, GetWifiPHYLayerProtocol());
60     }
61 #elif BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID)
62     char name[IF_NAMESIZE];
63     EXPECT_TRUE(if_indextoname(it->interface_index, name));
64     EXPECT_STREQ(it->name.c_str(), name);
65 #endif
66   }
67 }
68 
TEST(NetworkInterfacesTest,GetWifiSSID)69 TEST(NetworkInterfacesTest, GetWifiSSID) {
70   // We can't check the result of GetWifiSSID() directly, since the result
71   // will differ across machines. Simply exercise the code path and hope that it
72   // doesn't crash.
73   EXPECT_NE((const char*)nullptr, GetWifiSSID().c_str());
74 }
75 
TEST(NetworkInterfacesTest,GetHostName)76 TEST(NetworkInterfacesTest, GetHostName) {
77   // We can't check the result of GetHostName() directly, since the result
78   // will differ across machines. Our goal here is to simply exercise the
79   // code path, and check that things "look about right".
80   std::string hostname = GetHostName();
81   EXPECT_FALSE(hostname.empty());
82 }
83 
84 }  // namespace
85 
86 }  // namespace net
87