xref: /aosp_15_r20/external/cronet/net/android/network_library_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2016 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/android/network_library.h"
6 
7 #include <string>
8 #include <vector>
9 
10 #include "base/android/build_info.h"
11 #include "base/test/task_environment.h"
12 #include "net/android/network_change_notifier_factory_android.h"
13 #include "net/base/ip_endpoint.h"
14 #include "net/base/net_errors.h"
15 #include "net/log/net_log_source.h"
16 #include "net/socket/tcp_socket.h"
17 #include "net/socket/udp_socket.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 
20 namespace net::android {
21 
TEST(NetworkLibraryTest,CaptivePortal)22 TEST(NetworkLibraryTest, CaptivePortal) {
23   EXPECT_FALSE(android::GetIsCaptivePortal());
24 }
25 
TEST(NetworkLibraryTest,GetWifiSignalLevel)26 TEST(NetworkLibraryTest, GetWifiSignalLevel) {
27   std::optional<int32_t> signal_strength = android::GetWifiSignalLevel();
28   if (!signal_strength.has_value())
29     return;
30   EXPECT_LE(0, signal_strength.value());
31   EXPECT_GE(4, signal_strength.value());
32 }
33 
TEST(NetworkLibraryTest,GetDnsSearchDomains)34 TEST(NetworkLibraryTest, GetDnsSearchDomains) {
35   if (base::android::BuildInfo::GetInstance()->sdk_int() <
36       base::android::SDK_VERSION_MARSHMALLOW) {
37     GTEST_SKIP() << "Cannot call or test GetDnsServers() in pre-M.";
38   }
39 
40   std::vector<IPEndPoint> dns_servers;
41   bool dns_over_tls_active;
42   std::string dns_over_tls_hostname;
43   std::vector<std::string> search_suffixes;
44 
45   if (!GetCurrentDnsServers(&dns_servers, &dns_over_tls_active,
46                             &dns_over_tls_hostname, &search_suffixes)) {
47     return;
48   }
49 
50   for (std::string suffix : search_suffixes) {
51     EXPECT_FALSE(suffix.empty());
52   }
53 }
54 
TEST(NetworkLibraryTest,GetDnsSearchDomainsForNetwork)55 TEST(NetworkLibraryTest, GetDnsSearchDomainsForNetwork) {
56   base::test::TaskEnvironment task_environment;
57 
58   if (base::android::BuildInfo::GetInstance()->sdk_int() <
59       base::android::SDK_VERSION_P) {
60     GTEST_SKIP() << "Cannot call or test GetDnsServersForNetwork() in pre-P.";
61   }
62 
63   NetworkChangeNotifierFactoryAndroid ncn_factory;
64   NetworkChangeNotifier::DisableForTest ncn_disable_for_test;
65   std::unique_ptr<NetworkChangeNotifier> ncn(ncn_factory.CreateInstance());
66   EXPECT_TRUE(NetworkChangeNotifier::AreNetworkHandlesSupported());
67 
68   auto default_network_handle = NetworkChangeNotifier::GetDefaultNetwork();
69   if (default_network_handle == handles::kInvalidNetworkHandle)
70     GTEST_SKIP() << "Could not retrieve a working active network handle.";
71 
72   std::vector<IPEndPoint> dns_servers;
73   bool dns_over_tls_active;
74   std::string dns_over_tls_hostname;
75   std::vector<std::string> search_suffixes;
76 
77   if (!GetDnsServersForNetwork(&dns_servers, &dns_over_tls_active,
78                                &dns_over_tls_hostname, &search_suffixes,
79                                default_network_handle)) {
80     return;
81   }
82 
83   for (std::string suffix : search_suffixes) {
84     EXPECT_FALSE(suffix.empty());
85   }
86 }
87 
TEST(NetworkLibraryTest,BindToNetwork)88 TEST(NetworkLibraryTest, BindToNetwork) {
89   base::test::TaskEnvironment task_environment;
90 
91   NetworkChangeNotifierFactoryAndroid ncn_factory;
92   NetworkChangeNotifier::DisableForTest ncn_disable_for_test;
93   std::unique_ptr<NetworkChangeNotifier> ncn(ncn_factory.CreateInstance());
94   TCPSocket socket_tcp_ipv4(nullptr, nullptr, NetLogSource());
95   ASSERT_EQ(OK, socket_tcp_ipv4.Open(ADDRESS_FAMILY_IPV4));
96   TCPSocket socket_tcp_ipv6(nullptr, nullptr, NetLogSource());
97   ASSERT_EQ(OK, socket_tcp_ipv6.Open(ADDRESS_FAMILY_IPV6));
98   UDPSocket socket_udp_ipv4(DatagramSocket::DEFAULT_BIND, nullptr,
99                             NetLogSource());
100   ASSERT_EQ(OK, socket_udp_ipv4.Open(ADDRESS_FAMILY_IPV4));
101   UDPSocket socket_udp_ipv6(DatagramSocket::DEFAULT_BIND, nullptr,
102                             NetLogSource());
103   ASSERT_EQ(OK, socket_udp_ipv6.Open(ADDRESS_FAMILY_IPV6));
104   std::array sockets{socket_tcp_ipv4.SocketDescriptorForTesting(),
105                      socket_tcp_ipv6.SocketDescriptorForTesting(),
106                      socket_udp_ipv4.SocketDescriptorForTesting(),
107                      socket_udp_ipv6.SocketDescriptorForTesting()};
108 
109   for (SocketDescriptor socket : sockets) {
110     if (base::android::BuildInfo::GetInstance()->sdk_int() >=
111         base::android::SDK_VERSION_LOLLIPOP) {
112       EXPECT_TRUE(NetworkChangeNotifier::AreNetworkHandlesSupported());
113       // Test successful binding.
114       handles::NetworkHandle existing_network_handle =
115           NetworkChangeNotifier::GetDefaultNetwork();
116       if (existing_network_handle != handles::kInvalidNetworkHandle) {
117         EXPECT_EQ(OK, BindToNetwork(socket, existing_network_handle));
118       }
119       // Test invalid binding.
120       EXPECT_EQ(ERR_INVALID_ARGUMENT,
121                 BindToNetwork(socket, handles::kInvalidNetworkHandle));
122     }
123 
124     // Attempt to bind to a not existing handles::NetworkHandle.
125     constexpr handles::NetworkHandle wrong_network_handle = 65536;
126     int rv = BindToNetwork(socket, wrong_network_handle);
127     if (base::android::BuildInfo::GetInstance()->sdk_int() <
128         base::android::SDK_VERSION_LOLLIPOP) {
129       EXPECT_EQ(ERR_NOT_IMPLEMENTED, rv);
130     } else if (base::android::BuildInfo::GetInstance()->sdk_int() >=
131                    base::android::SDK_VERSION_LOLLIPOP &&
132                base::android::BuildInfo::GetInstance()->sdk_int() <
133                    base::android::SDK_VERSION_MARSHMALLOW) {
134       // On Lollipop, we assume if the user has a handles::NetworkHandle that
135       // they must have gotten it from a legitimate source, so if binding to the
136       // network fails it's assumed to be because the network went away so
137       // ERR_NETWORK_CHANGED is returned. In this test the network never existed
138       // anyhow. ConnectivityService.MAX_NET_ID is 65535, so 65536 won't be
139       // used.
140       EXPECT_EQ(ERR_NETWORK_CHANGED, rv);
141     } else if (base::android::BuildInfo::GetInstance()->sdk_int() >=
142                base::android::SDK_VERSION_MARSHMALLOW) {
143       // On Marshmallow and newer releases, the handles::NetworkHandle is munged
144       // by Network.getNetworkHandle() and 65536 isn't munged so it's rejected.
145       EXPECT_EQ(ERR_INVALID_ARGUMENT, rv);
146     }
147   }
148 }
149 
150 }  // namespace net::android
151