1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 18 #pragma once 19 20 #include <arpa/nameser.h> 21 #include <netdb.h> 22 23 #include <filesystem> 24 #include <functional> 25 #include <string> 26 #include <vector> 27 28 #include <aidl/android/net/INetd.h> 29 #include <aidl/android/net/ResolverParamsParcel.h> 30 #include <android-base/properties.h> 31 #include <android-modules-utils/sdk_level.h> 32 #include <firewall.h> 33 #include <gtest/gtest.h> 34 #include <netdutils/InternetAddresses.h> 35 36 #include "dns_responder/dns_responder.h" 37 #include "params.h" 38 #include "util.h" 39 40 class ScopeBlockedUIDRule { 41 using INetd = aidl::android::net::INetd; 42 43 public: ScopeBlockedUIDRule(INetd * netSrv,uid_t testUid)44 ScopeBlockedUIDRule(INetd* netSrv, uid_t testUid) 45 : mNetSrv(netSrv), mTestUid(testUid), mSavedUid(getuid()) { 46 // Add drop rule for testUid. Also enable the standby chain because it might not be 47 // enabled. Unfortunately we cannot use FIREWALL_CHAIN_NONE, or custom iptables rules, for 48 // this purpose because netd calls fchown() on the DNS query sockets, and "iptables -m 49 // owner" matches the UID of the socket creator, not the UID set by fchown(). 50 // TODO: migrate FIREWALL_CHAIN_NONE to eBPF as well. 51 if (android::modules::sdklevel::IsAtLeastT()) { 52 mFw = Firewall::getInstance(); 53 EXPECT_RESULT_OK(mFw->toggleStandbyMatch(true)); 54 EXPECT_RESULT_OK(mFw->addRule(mTestUid, STANDBY_MATCH)); 55 } else { 56 EXPECT_TRUE( 57 mNetSrv->firewallEnableChildChain(INetd::FIREWALL_CHAIN_STANDBY, true).isOk()); 58 EXPECT_TRUE(mNetSrv->firewallSetUidRule(INetd::FIREWALL_CHAIN_STANDBY, mTestUid, 59 INetd::FIREWALL_RULE_DENY) 60 .isOk()); 61 } 62 EXPECT_TRUE(seteuid(mTestUid) == 0); 63 }; ~ScopeBlockedUIDRule()64 ~ScopeBlockedUIDRule() { 65 // Restore uid 66 EXPECT_TRUE(seteuid(mSavedUid) == 0); 67 // Remove drop rule for testUid, and disable the standby chain. 68 if (android::modules::sdklevel::IsAtLeastT()) { 69 EXPECT_RESULT_OK(mFw->removeRule(mTestUid, STANDBY_MATCH)); 70 EXPECT_RESULT_OK(mFw->toggleStandbyMatch(false)); 71 } else { 72 EXPECT_TRUE(mNetSrv->firewallSetUidRule(INetd::FIREWALL_CHAIN_STANDBY, mTestUid, 73 INetd::FIREWALL_RULE_ALLOW) 74 .isOk()); 75 EXPECT_TRUE( 76 mNetSrv->firewallEnableChildChain(INetd::FIREWALL_CHAIN_STANDBY, false).isOk()); 77 } 78 } 79 80 private: 81 INetd* mNetSrv; 82 Firewall* mFw; 83 const uid_t mTestUid; 84 const uid_t mSavedUid; 85 }; 86 87 // Supported from T+ only. 88 class ScopedSetDataSaverByBPF { 89 public: ScopedSetDataSaverByBPF(bool wanted)90 ScopedSetDataSaverByBPF(bool wanted) { 91 if (android::modules::sdklevel::IsAtLeastT()) { 92 mFw = Firewall::getInstance(); 93 // Backup current setting. 94 const Result<bool> current = mFw->getDataSaverSetting(); 95 EXPECT_RESULT_OK(current); 96 if (wanted != current.value()) { 97 mSavedDataSaverSetting = current; 98 EXPECT_RESULT_OK(mFw->setDataSaver(wanted)); 99 } 100 } 101 }; ~ScopedSetDataSaverByBPF()102 ~ScopedSetDataSaverByBPF() { 103 // Restore the setting. 104 if (mSavedDataSaverSetting.has_value()) { 105 EXPECT_RESULT_OK(mFw->setDataSaver(mSavedDataSaverSetting.value())); 106 } 107 } 108 109 private: 110 Firewall* mFw; 111 Result<bool> mSavedDataSaverSetting; 112 }; 113 114 class ScopedChangeUID { 115 public: ScopedChangeUID(uid_t testUid)116 ScopedChangeUID(uid_t testUid) : mTestUid(testUid), mSavedUid(getuid()) { 117 EXPECT_TRUE(seteuid(mTestUid) == 0); 118 }; ~ScopedChangeUID()119 ~ScopedChangeUID() { EXPECT_TRUE(seteuid(mSavedUid) == 0); } 120 121 private: 122 const uid_t mTestUid; 123 const uid_t mSavedUid; 124 }; 125 126 class ScopedSystemProperties { 127 public: ScopedSystemProperties(const std::string & key,const std::string & value)128 ScopedSystemProperties(const std::string& key, const std::string& value) : mStoredKey(key) { 129 mStoredValue = android::base::GetProperty(key, ""); 130 android::base::SetProperty(key, value); 131 } ~ScopedSystemProperties()132 ~ScopedSystemProperties() { android::base::SetProperty(mStoredKey, mStoredValue); } 133 134 private: 135 std::string mStoredKey; 136 std::string mStoredValue; 137 }; 138 139 class ScopedDefaultNetwork { 140 using INetd = aidl::android::net::INetd; 141 142 public: ScopedDefaultNetwork(INetd * netSrv,uid_t testDefaultNetwork)143 ScopedDefaultNetwork(INetd* netSrv, uid_t testDefaultNetwork) : mNetSrv(netSrv) { 144 EXPECT_TRUE(mNetSrv->networkGetDefault(&mStoredDefaultNetwork).isOk()); 145 EXPECT_TRUE(mNetSrv->networkSetDefault(testDefaultNetwork).isOk()); 146 }; ~ScopedDefaultNetwork()147 ~ScopedDefaultNetwork() { 148 EXPECT_TRUE(mNetSrv->networkSetDefault(mStoredDefaultNetwork).isOk()); 149 } 150 151 private: 152 INetd* mNetSrv; 153 int mStoredDefaultNetwork; 154 }; 155 156 struct DnsRecord { 157 std::string host_name; // host name 158 ns_type type; // record type 159 std::string addr; // ipv4/v6 address 160 }; 161 162 // TODO: make this dynamic and stop depending on implementation details. 163 constexpr int TEST_NETID = 30; 164 // Use the biggest two reserved appId for applications to avoid conflict with existing uids. 165 constexpr int TEST_UID = 99999; 166 constexpr int TEST_UID2 = 99998; 167 168 constexpr char kDnsPortString[] = "53"; 169 constexpr char kDohPortString[] = "443"; 170 constexpr char kDotPortString[] = "853"; 171 172 const std::string kFlagPrefix("persist.device_config.netd_native."); 173 174 const std::string kDohEarlyDataFlag(kFlagPrefix + "doh_early_data"); 175 const std::string kDohIdleTimeoutFlag(kFlagPrefix + "doh_idle_timeout_ms"); 176 const std::string kDohProbeTimeoutFlag(kFlagPrefix + "doh_probe_timeout_ms"); 177 const std::string kDohQueryTimeoutFlag(kFlagPrefix + "doh_query_timeout_ms"); 178 const std::string kDohSessionResumptionFlag(kFlagPrefix + "doh_session_resumption"); 179 const std::string kDotAsyncHandshakeFlag(kFlagPrefix + "dot_async_handshake"); 180 const std::string kDotConnectTimeoutMsFlag(kFlagPrefix + "dot_connect_timeout_ms"); 181 const std::string kDotMaxretriesFlag(kFlagPrefix + "dot_maxtries"); 182 const std::string kDotQueryTimeoutMsFlag(kFlagPrefix + "dot_query_timeout_ms"); 183 const std::string kDotQuickFallbackFlag(kFlagPrefix + "dot_quick_fallback"); 184 const std::string kDotRevalidationThresholdFlag(kFlagPrefix + "dot_revalidation_threshold"); 185 const std::string kDotXportUnusableThresholdFlag(kFlagPrefix + "dot_xport_unusable_threshold"); 186 const std::string kDotValidationLatencyFactorFlag(kFlagPrefix + "dot_validation_latency_factor"); 187 const std::string kDotValidationLatencyOffsetMsFlag(kFlagPrefix + 188 "dot_validation_latency_offset_ms"); 189 const std::string kFailFastOnUidNetworkBlockingFlag(kFlagPrefix + 190 "fail_fast_on_uid_network_blocking"); 191 const std::string kKeepListeningUdpFlag(kFlagPrefix + "keep_listening_udp"); 192 const std::string kParallelLookupSleepTimeFlag(kFlagPrefix + "parallel_lookup_sleep_time"); 193 const std::string kRetransIntervalFlag(kFlagPrefix + "retransmission_time_interval"); 194 const std::string kRetryCountFlag(kFlagPrefix + "retry_count"); 195 const std::string kSortNameserversFlag(kFlagPrefix + "sort_nameservers"); 196 197 const std::string kPersistNetPrefix("persist.net."); 198 199 const std::string kQueryLogSize(kPersistNetPrefix + "dns_query_log_size"); 200 201 static constexpr char kLocalHost[] = "localhost"; 202 static constexpr char kLocalHostAddr[] = "127.0.0.1"; 203 static constexpr char kIp6LocalHost[] = "ip6-localhost"; 204 static constexpr char kIp6LocalHostAddr[] = "::1"; 205 static constexpr char kHelloExampleCom[] = "hello.example.com."; 206 static constexpr char kHelloExampleComAddrV4[] = "1.2.3.4"; 207 static constexpr char kHelloExampleComAddrV4_2[] = "8.8.8.8"; 208 static constexpr char kHelloExampleComAddrV4_3[] = "81.117.21.202"; 209 static constexpr char kHelloExampleComAddrV6[] = "::1.2.3.4"; 210 static constexpr char kHelloExampleComAddrV6_IPV4COMPAT[] = "::1.2.3.4"; 211 static constexpr char kHelloExampleComAddrV6_TEREDO[] = "2001::47c1"; 212 static constexpr char kHelloExampleComAddrV6_GUA[] = "2404:6800::5175:15ca"; 213 static constexpr char kExampleComDomain[] = ".example.com"; 214 215 static const std::string kNat64Prefix = "64:ff9b::/96"; 216 static const std::string kNat64Prefix2 = "2001:db8:6464::/96"; 217 218 constexpr size_t kMaxmiumLabelSize = 63; // see RFC 1035 section 2.3.4. 219 220 static const std::vector<uint8_t> kHelloExampleComQueryV4 = { 221 /* Header */ 222 0x00, 0x00, /* Transaction ID: 0x0000 */ 223 0x01, 0x00, /* Flags: rd */ 224 0x00, 0x01, /* Questions: 1 */ 225 0x00, 0x00, /* Answer RRs: 0 */ 226 0x00, 0x00, /* Authority RRs: 0 */ 227 0x00, 0x00, /* Additional RRs: 0 */ 228 /* Queries */ 229 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03, 230 0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */ 231 0x00, 0x01, /* Type: A */ 232 0x00, 0x01 /* Class: IN */ 233 }; 234 235 static const std::vector<uint8_t> kHelloExampleComResponseV4 = { 236 /* Header */ 237 0x00, 0x00, /* Transaction ID: 0x0000 */ 238 0x81, 0x80, /* Flags: qr rd ra */ 239 0x00, 0x01, /* Questions: 1 */ 240 0x00, 0x01, /* Answer RRs: 1 */ 241 0x00, 0x00, /* Authority RRs: 0 */ 242 0x00, 0x00, /* Additional RRs: 0 */ 243 /* Queries */ 244 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03, 245 0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */ 246 0x00, 0x01, /* Type: A */ 247 0x00, 0x01, /* Class: IN */ 248 /* Answers */ 249 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03, 250 0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */ 251 0x00, 0x01, /* Type: A */ 252 0x00, 0x01, /* Class: IN */ 253 0x00, 0x00, 0x00, 0x00, /* Time to live: 0 */ 254 0x00, 0x04, /* Data length: 4 */ 255 0x01, 0x02, 0x03, 0x04 /* Address: 1.2.3.4 */ 256 }; 257 258 const std::vector<uint8_t> kHelloExampleComResponsesV4 = { 259 // scapy.DNS( 260 // id=0, 261 // qr=1, 262 // ra=1, 263 // qd=scapy.DNSQR(qname="hello.example.com",qtype="A"), 264 // an=scapy.DNSRR(rrname="hello.example.com",type="A",ttl=0,rdata='1.2.3.4') / 265 // scapy.DNSRR(rrname="hello.example.com",type="A",ttl=0,rdata='8.8.8.8') / 266 // scapy.DNSRR(rrname="hello.example.com",type="A",ttl=0,rdata='81.117.21.202')) 267 /* Header */ 268 0x00, 0x00, /* Transaction ID: 0x0000 */ 269 0x81, 0x80, /* Flags: qr rd ra */ 270 0x00, 0x01, /* Questions: 1 */ 271 0x00, 0x03, /* Answer RRs: 3 */ 272 0x00, 0x00, /* Authority RRs: 0 */ 273 0x00, 0x00, /* Additional RRs: 0 */ 274 /* Queries */ 275 0x05, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x07, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x03, 276 0x63, 0x6F, 0x6D, 0x00, /* Name: hello.example.com */ 277 0x00, 0x01, /* Type: A */ 278 0x00, 0x01, /* Class: IN */ 279 /* Answers */ 280 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03, 281 0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */ 282 0x00, 0x01, /* Type: A */ 283 0x00, 0x01, /* Class: IN */ 284 0x00, 0x00, 0x00, 0x00, /* Time to live: 0 */ 285 0x00, 0x04, /* Data length: 4 */ 286 0x01, 0x02, 0x03, 0x04, /* Address: 1.2.3.4 */ 287 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03, 288 0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */ 289 0x00, 0x01, /* Type: A */ 290 0x00, 0x01, /* Class: IN */ 291 0x00, 0x00, 0x00, 0x00, /* Time to live: 0 */ 292 0x00, 0x04, /* Data length: 4 */ 293 0x08, 0x08, 0x08, 0x08, /* Address: 8.8.8.8 */ 294 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03, 295 0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */ 296 0x00, 0x01, /* Type: A */ 297 0x00, 0x01, /* Class: IN */ 298 0x00, 0x00, 0x00, 0x00, /* Time to live: 0 */ 299 0x00, 0x04, /* Data length: 4 */ 300 0x51, 0x75, 0x15, 0xca /* Address: 81.117.21.202 */ 301 }; 302 303 static const std::vector<uint8_t> kHelloExampleComQueryV6 = { 304 /* Header */ 305 0x00, 0x00, /* Transaction ID: 0x0000 */ 306 0x01, 0x00, /* Flags: rd */ 307 0x00, 0x01, /* Questions: 1 */ 308 0x00, 0x00, /* Answer RRs: 0 */ 309 0x00, 0x00, /* Authority RRs: 0 */ 310 0x00, 0x00, /* Additional RRs: 0 */ 311 /* Queries */ 312 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03, 313 0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */ 314 0x00, 0x1c, /* Type: AAAA */ 315 0x00, 0x01 /* Class: IN */ 316 }; 317 318 const std::vector<uint8_t> kHelloExampleComResponsesV6 = { 319 // The addresses are IPv4-compatible address, teredo tunneling address and global unicast 320 // address. 321 // 322 // scapy.DNS( 323 // id=0, 324 // qr=1, 325 // ra=1, 326 // qd=scapy.DNSQR(qname="hello.example.com",qtype="AAAA"), 327 // an=scapy.DNSRR(rrname="hello.example.com",type="AAAA",rdata='::1.2.3.4') / 328 // scapy.DNSRR(rrname="hello.example.com",type="AAAA",rdata='2001::47c1') / 329 // scapy.DNSRR(rrname="hello.example.com",type="AAAA",rdata='2404:6800::5175:15ca')) 330 /* Header */ 331 0x00, 0x00, /* Transaction ID: 0x0000 */ 332 0x81, 0x80, /* Flags: qr rd ra */ 333 0x00, 0x01, /* Questions: 1 */ 334 0x00, 0x03, /* Answer RRs: 3 */ 335 0x00, 0x00, /* Authority RRs: 0 */ 336 0x00, 0x00, /* Additional RRs: 0 */ 337 /* Queries */ 338 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03, 339 0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */ 340 0x00, 0x1c, /* Type: AAAA */ 341 0x00, 0x01, /* Class: IN */ 342 /* Answers */ 343 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03, 344 0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */ 345 0x00, 0x1c, /* Type: AAAA */ 346 0x00, 0x01, /* Class: IN */ 347 0x00, 0x00, 0x00, 0x00, /* Time to live: 0 */ 348 0x00, 0x10, /* Data length: 4 */ 349 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 350 0x04, /* Address: ::1.2.3.4 */ 351 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03, 352 0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */ 353 0x00, 0x1c, /* Type: AAAA */ 354 0x00, 0x01, /* Class: IN */ 355 0x00, 0x00, 0x00, 0x00, /* Time to live: 0 */ 356 0x00, 0x10, /* Data length: 4 */ 357 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x47, 358 0xc1, /* Address: 2001::47c1 */ 359 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03, 360 0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */ 361 0x00, 0x1c, /* Type: AAAA */ 362 0x00, 0x01, /* Class: IN */ 363 0x00, 0x00, 0x00, 0x00, /* Time to live: 0 */ 364 0x00, 0x10, /* Data length: 4 */ 365 0x24, 0x04, 0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x51, 0x75, 0x15, 366 0xCA /* Address: 2404:6800::5175:15ca */ 367 }; 368 369 // Illegal hostnames 370 static constexpr char kBadCharAfterPeriodHost[] = "hello.example.^com."; 371 static constexpr char kBadCharBeforePeriodHost[] = "hello.example^.com."; 372 static constexpr char kBadCharAtTheEndHost[] = "hello.example.com^."; 373 static constexpr char kBadCharInTheMiddleOfLabelHost[] = "hello.ex^ample.com."; 374 375 static const test::DNSHeader kDefaultDnsHeader = { 376 // Don't need to initialize the flag "id" and "rd" because DNS responder assigns them from 377 // query to response. See RFC 1035 section 4.1.1. 378 .id = 0, // unused. should be assigned from query to response 379 .ra = false, // recursive query support is not available 380 .rcode = ns_r_noerror, // no error 381 .qr = true, // message is a response 382 .opcode = QUERY, // a standard query 383 .aa = false, // answer/authority portion was not authenticated by the server 384 .tr = false, // message is not truncated 385 .rd = false, // unused. should be assigned from query to response 386 .ad = false, // non-authenticated data is unacceptable 387 }; 388 389 // The CNAME chain records for building a response message which exceeds 512 bytes. 390 // 391 // Ignoring the other fields of the message, the response message has 8 CNAMEs in 5 answer RRs 392 // and each CNAME has 77 bytes as the follows. The response message at least has 616 bytes in 393 // answer section and has already exceeded 512 bytes totally. 394 // 395 // The CNAME is presented as: 396 // 0 1 64 65 72 73 76 77 397 // +---+--........--+---+---+---+---+---+---+---+---+---+---+---+---+---+ 398 // | 63| {x, .., x} | 7 | e | x | a | m | p | l | e | 3 | c | o | m | 0 | 399 // +---+--........--+---+---+---+---+---+---+---+---+---+---+---+---+---+ 400 // ^-- x = {a, b, c, d} 401 // 402 const std::string kCnameA = std::string(kMaxmiumLabelSize, 'a') + kExampleComDomain + "."; 403 const std::string kCnameB = std::string(kMaxmiumLabelSize, 'b') + kExampleComDomain + "."; 404 const std::string kCnameC = std::string(kMaxmiumLabelSize, 'c') + kExampleComDomain + "."; 405 const std::string kCnameD = std::string(kMaxmiumLabelSize, 'd') + kExampleComDomain + "."; 406 const std::vector<DnsRecord> kLargeCnameChainRecords = { 407 {kHelloExampleCom, ns_type::ns_t_cname, kCnameA}, 408 {kCnameA, ns_type::ns_t_cname, kCnameB}, 409 {kCnameB, ns_type::ns_t_cname, kCnameC}, 410 {kCnameC, ns_type::ns_t_cname, kCnameD}, 411 {kCnameD, ns_type::ns_t_a, kHelloExampleComAddrV4}, 412 }; 413 414 // TODO: Integrate GetNumQueries relevent functions 415 size_t GetNumQueries(const test::DNSResponder& dns, const char* name); 416 size_t GetNumQueriesForProtocol(const test::DNSResponder& dns, const int protocol, 417 const char* name); 418 size_t GetNumQueriesForType(const test::DNSResponder& dns, ns_type type, const char* name); 419 std::string ToString(const hostent* he); 420 std::string ToString(const addrinfo* ai); 421 std::string ToString(const android::netdutils::ScopedAddrinfo& ai); 422 std::string ToString(const sockaddr_storage* addr); 423 std::vector<std::string> ToStrings(const hostent* he); 424 std::vector<std::string> ToStrings(const addrinfo* ai); 425 std::vector<std::string> ToStrings(const android::netdutils::ScopedAddrinfo& ai); 426 427 // Wait for |condition| to be met until |timeout|. 428 bool PollForCondition(const std::function<bool()>& condition, 429 std::chrono::milliseconds timeout = std::chrono::milliseconds(1000)); 430 431 android::netdutils::ScopedAddrinfo safe_getaddrinfo(const char* node, const char* service, 432 const struct addrinfo* hints); 433 434 void SetMdnsRoute(); 435 void RemoveMdnsRoute(); 436 void AllowNetworkInBackground(int uid, bool allow); 437 438 // Local definition to avoid including resolv_cache.h. 439 int resolv_set_nameservers(const aidl::android::net::ResolverParamsParcel& params); 440 441 // For testing only. Production code passes the parcel down directly. 442 inline int resolv_set_nameservers( 443 unsigned netid, const std::vector<std::string>& servers, 444 const std::vector<std::string>& domains, const res_params& res_params, 445 std::optional<aidl::android::net::ResolverOptionsParcel> resolverOptions, 446 const std::vector<int32_t>& transportTypes = {}, bool metered = false) { 447 aidl::android::net::ResolverParamsParcel params; 448 params.netId = netid; 449 params.servers = servers; 450 params.domains = domains; 451 params.resolverOptions = resolverOptions; 452 params.transportTypes = transportTypes; 453 params.meteredNetwork = metered; 454 455 params.sampleValiditySeconds = res_params.sample_validity; 456 params.successThreshold = res_params.success_threshold; 457 params.minSamples = res_params.min_samples; 458 params.maxSamples = res_params.max_samples; 459 params.baseTimeoutMsec = res_params.base_timeout_msec; 460 params.retryCount = res_params.retry_count; 461 462 return resolv_set_nameservers(params); 463 } 464 465 #define SKIP_IF_BEFORE_T \ 466 do { \ 467 if (!isAtLeastT()) { \ 468 GTEST_SKIP() << "Skipping test because SDK version is less than T."; \ 469 } \ 470 } while (0) 471 472 bool is64bitAbi(); 473 474 static const std::string DNS_HELPER = 475 is64bitAbi() ? "/apex/com.android.tethering/lib64/libcom.android.tethering.dns_helper.so" 476 : "/apex/com.android.tethering/lib/libcom.android.tethering.dns_helper.so"; 477 478 #define SKIP_IF_DEPENDENT_LIB_DOES_NOT_EXIST(libPath) \ 479 do { \ 480 if (!std::filesystem::exists(libPath)) \ 481 GTEST_SKIP() << "Required " << (libPath) << " not found."; \ 482 } while (0) 483