xref: /aosp_15_r20/external/cronet/net/base/network_interfaces_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 
60     if (it->type == NetworkChangeNotifier::CONNECTION_WIFI) {
61       EXPECT_NE(WIFI_PHY_LAYER_PROTOCOL_NONE, GetWifiPHYLayerProtocol());
62     }
63 #elif BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID)
64     char name[IF_NAMESIZE];
65     EXPECT_TRUE(if_indextoname(it->interface_index, name));
66     EXPECT_STREQ(it->name.c_str(), name);
67 #endif
68   }
69 }
70 
TEST(NetworkInterfacesTest,GetWifiSSID)71 TEST(NetworkInterfacesTest, GetWifiSSID) {
72   // We can't check the result of GetWifiSSID() directly, since the result
73   // will differ across machines. Simply exercise the code path and hope that it
74   // doesn't crash.
75   EXPECT_NE((const char*)nullptr, GetWifiSSID().c_str());
76 }
77 
TEST(NetworkInterfacesTest,GetHostName)78 TEST(NetworkInterfacesTest, GetHostName) {
79   // We can't check the result of GetHostName() directly, since the result
80   // will differ across machines. Our goal here is to simply exercise the
81   // code path, and check that things "look about right".
82   std::string hostname = GetHostName();
83   EXPECT_FALSE(hostname.empty());
84 }
85 
86 }  // namespace
87 
88 }  // namespace net
89