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