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 std::unique_ptr<TCPSocket> socket_tcp_ipv4 =
95 TCPSocket::Create(nullptr, nullptr, NetLogSource());
96 ASSERT_EQ(OK, socket_tcp_ipv4->Open(ADDRESS_FAMILY_IPV4));
97 std::unique_ptr<TCPSocket> socket_tcp_ipv6 =
98 TCPSocket::Create(nullptr, nullptr, NetLogSource());
99 ASSERT_EQ(OK, socket_tcp_ipv6->Open(ADDRESS_FAMILY_IPV6));
100 UDPSocket socket_udp_ipv4(DatagramSocket::DEFAULT_BIND, nullptr,
101 NetLogSource());
102 ASSERT_EQ(OK, socket_udp_ipv4.Open(ADDRESS_FAMILY_IPV4));
103 UDPSocket socket_udp_ipv6(DatagramSocket::DEFAULT_BIND, nullptr,
104 NetLogSource());
105 ASSERT_EQ(OK, socket_udp_ipv6.Open(ADDRESS_FAMILY_IPV6));
106 std::array sockets{socket_tcp_ipv4->SocketDescriptorForTesting(),
107 socket_tcp_ipv6->SocketDescriptorForTesting(),
108 socket_udp_ipv4.SocketDescriptorForTesting(),
109 socket_udp_ipv6.SocketDescriptorForTesting()};
110
111 for (SocketDescriptor socket : sockets) {
112 if (base::android::BuildInfo::GetInstance()->sdk_int() >=
113 base::android::SDK_VERSION_LOLLIPOP) {
114 EXPECT_TRUE(NetworkChangeNotifier::AreNetworkHandlesSupported());
115 // Test successful binding.
116 handles::NetworkHandle existing_network_handle =
117 NetworkChangeNotifier::GetDefaultNetwork();
118 if (existing_network_handle != handles::kInvalidNetworkHandle) {
119 EXPECT_EQ(OK, BindToNetwork(socket, existing_network_handle));
120 }
121 // Test invalid binding.
122 EXPECT_EQ(ERR_INVALID_ARGUMENT,
123 BindToNetwork(socket, handles::kInvalidNetworkHandle));
124 }
125
126 // Attempt to bind to a not existing handles::NetworkHandle.
127 constexpr handles::NetworkHandle wrong_network_handle = 65536;
128 int rv = BindToNetwork(socket, wrong_network_handle);
129 if (base::android::BuildInfo::GetInstance()->sdk_int() <
130 base::android::SDK_VERSION_LOLLIPOP) {
131 EXPECT_EQ(ERR_NOT_IMPLEMENTED, rv);
132 } else if (base::android::BuildInfo::GetInstance()->sdk_int() >=
133 base::android::SDK_VERSION_LOLLIPOP &&
134 base::android::BuildInfo::GetInstance()->sdk_int() <
135 base::android::SDK_VERSION_MARSHMALLOW) {
136 // On Lollipop, we assume if the user has a handles::NetworkHandle that
137 // they must have gotten it from a legitimate source, so if binding to the
138 // network fails it's assumed to be because the network went away so
139 // ERR_NETWORK_CHANGED is returned. In this test the network never existed
140 // anyhow. ConnectivityService.MAX_NET_ID is 65535, so 65536 won't be
141 // used.
142 EXPECT_EQ(ERR_NETWORK_CHANGED, rv);
143 } else if (base::android::BuildInfo::GetInstance()->sdk_int() >=
144 base::android::SDK_VERSION_MARSHMALLOW) {
145 // On Marshmallow and newer releases, the handles::NetworkHandle is munged
146 // by Network.getNetworkHandle() and 65536 isn't munged so it's rejected.
147 EXPECT_EQ(ERR_INVALID_ARGUMENT, rv);
148 }
149 }
150 }
151
152 } // namespace net::android
153