• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 requied 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 #include <arpa/inet.h>
19 #include <errno.h>
20 #include <netdb.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 
26 #include <cutils/sockets.h>
27 #include <android-base/stringprintf.h>
28 #include <private/android_filesystem_config.h>
29 
30 #include <algorithm>
31 #include <chrono>
32 #include <iterator>
33 #include <numeric>
34 #include <thread>
35 
36 #define LOG_TAG "netd_test"
37 // TODO: make this dynamic and stop depending on implementation details.
38 #define TEST_OEM_NETWORK "oem29"
39 #define TEST_NETID 30
40 
41 #include "NetdClient.h"
42 
43 #include <gtest/gtest.h>
44 
45 #include <utils/Log.h>
46 
47 #include <testUtil.h>
48 
49 #include "dns_responder.h"
50 #include "dns_responder_client.h"
51 #include "resolv_params.h"
52 #include "ResolverStats.h"
53 
54 #include "android/net/INetd.h"
55 #include "android/net/metrics/INetdEventListener.h"
56 #include "binder/IServiceManager.h"
57 
58 using android::base::StringPrintf;
59 using android::base::StringAppendF;
60 using android::net::ResolverStats;
61 using android::net::metrics::INetdEventListener;
62 
63 // Emulates the behavior of UnorderedElementsAreArray, which currently cannot be used.
64 // TODO: Use UnorderedElementsAreArray, which depends on being able to compile libgmock_host,
65 // if that is not possible, improve this hacky algorithm, which is O(n**2)
66 template <class A, class B>
UnorderedCompareArray(const A & a,const B & b)67 bool UnorderedCompareArray(const A& a, const B& b) {
68     if (a.size() != b.size()) return false;
69     for (const auto& a_elem : a) {
70         size_t a_count = 0;
71         for (const auto& a_elem2 : a) {
72             if (a_elem == a_elem2) {
73                 ++a_count;
74             }
75         }
76         size_t b_count = 0;
77         for (const auto& b_elem : b) {
78             if (a_elem == b_elem) ++b_count;
79         }
80         if (a_count != b_count) return false;
81     }
82     return true;
83 }
84 
85 class AddrInfo {
86   public:
AddrInfo()87     AddrInfo() : ai_(nullptr), error_(0) {}
88 
AddrInfo(const char * node,const char * service,const addrinfo & hints)89     AddrInfo(const char* node, const char* service, const addrinfo& hints) : ai_(nullptr) {
90         init(node, service, hints);
91     }
92 
AddrInfo(const char * node,const char * service)93     AddrInfo(const char* node, const char* service) : ai_(nullptr) {
94         init(node, service);
95     }
96 
~AddrInfo()97     ~AddrInfo() { clear(); }
98 
init(const char * node,const char * service,const addrinfo & hints)99     int init(const char* node, const char* service, const addrinfo& hints) {
100         clear();
101         error_ = getaddrinfo(node, service, &hints, &ai_);
102         return error_;
103     }
104 
init(const char * node,const char * service)105     int init(const char* node, const char* service) {
106         clear();
107         error_ = getaddrinfo(node, service, nullptr, &ai_);
108         return error_;
109     }
110 
clear()111     void clear() {
112         if (ai_ != nullptr) {
113             freeaddrinfo(ai_);
114             ai_ = nullptr;
115             error_ = 0;
116         }
117     }
118 
operator *() const119     const addrinfo& operator*() const { return *ai_; }
get() const120     const addrinfo* get() const { return ai_; }
operator &() const121     const addrinfo* operator&() const { return ai_; }
error() const122     int error() const { return error_; }
123 
124   private:
125     addrinfo* ai_;
126     int error_;
127 };
128 
129 class ResolverTest : public ::testing::Test, public DnsResponderClient {
130 private:
131     int mOriginalMetricsLevel;
132 
133 protected:
SetUp()134     virtual void SetUp() {
135         // Ensure resolutions go via proxy.
136         DnsResponderClient::SetUp();
137 
138         // If DNS reporting is off: turn it on so we run through everything.
139         auto rv = mNetdSrv->getMetricsReportingLevel(&mOriginalMetricsLevel);
140         ASSERT_TRUE(rv.isOk());
141         if (mOriginalMetricsLevel != INetdEventListener::REPORTING_LEVEL_FULL) {
142             rv = mNetdSrv->setMetricsReportingLevel(INetdEventListener::REPORTING_LEVEL_FULL);
143             ASSERT_TRUE(rv.isOk());
144         }
145     }
146 
TearDown()147     virtual void TearDown() {
148         if (mOriginalMetricsLevel != INetdEventListener::REPORTING_LEVEL_FULL) {
149             auto rv = mNetdSrv->setMetricsReportingLevel(mOriginalMetricsLevel);
150             ASSERT_TRUE(rv.isOk());
151         }
152 
153         DnsResponderClient::TearDown();
154     }
155 
GetResolverInfo(std::vector<std::string> * servers,std::vector<std::string> * domains,__res_params * params,std::vector<ResolverStats> * stats)156     bool GetResolverInfo(std::vector<std::string>* servers, std::vector<std::string>* domains,
157             __res_params* params, std::vector<ResolverStats>* stats) {
158         using android::net::INetd;
159         std::vector<int32_t> params32;
160         std::vector<int32_t> stats32;
161         auto rv = mNetdSrv->getResolverInfo(TEST_NETID, servers, domains, &params32, &stats32);
162         if (!rv.isOk() || params32.size() != INetd::RESOLVER_PARAMS_COUNT) {
163             return false;
164         }
165         *params = __res_params {
166             .sample_validity = static_cast<uint16_t>(
167                     params32[INetd::RESOLVER_PARAMS_SAMPLE_VALIDITY]),
168             .success_threshold = static_cast<uint8_t>(
169                     params32[INetd::RESOLVER_PARAMS_SUCCESS_THRESHOLD]),
170             .min_samples = static_cast<uint8_t>(
171                     params32[INetd::RESOLVER_PARAMS_MIN_SAMPLES]),
172             .max_samples = static_cast<uint8_t>(
173                     params32[INetd::RESOLVER_PARAMS_MAX_SAMPLES])
174         };
175         return ResolverStats::decodeAll(stats32, stats);
176     }
177 
ToString(const hostent * he) const178     std::string ToString(const hostent* he) const {
179         if (he == nullptr) return "<null>";
180         char buffer[INET6_ADDRSTRLEN];
181         if (!inet_ntop(he->h_addrtype, he->h_addr_list[0], buffer, sizeof(buffer))) {
182             return "<invalid>";
183         }
184         return buffer;
185     }
186 
ToString(const addrinfo * ai) const187     std::string ToString(const addrinfo* ai) const {
188         if (!ai)
189             return "<null>";
190         for (const auto* aip = ai ; aip != nullptr ; aip = aip->ai_next) {
191             char host[NI_MAXHOST];
192             int rv = getnameinfo(aip->ai_addr, aip->ai_addrlen, host, sizeof(host), nullptr, 0,
193                     NI_NUMERICHOST);
194             if (rv != 0)
195                 return gai_strerror(rv);
196             return host;
197         }
198         return "<invalid>";
199     }
200 
GetNumQueries(const test::DNSResponder & dns,const char * name) const201     size_t GetNumQueries(const test::DNSResponder& dns, const char* name) const {
202         auto queries = dns.queries();
203         size_t found = 0;
204         for (const auto& p : queries) {
205             if (p.first == name) {
206                 ++found;
207             }
208         }
209         return found;
210     }
211 
GetNumQueriesForType(const test::DNSResponder & dns,ns_type type,const char * name) const212     size_t GetNumQueriesForType(const test::DNSResponder& dns, ns_type type,
213             const char* name) const {
214         auto queries = dns.queries();
215         size_t found = 0;
216         for (const auto& p : queries) {
217             if (p.second == type && p.first == name) {
218                 ++found;
219             }
220         }
221         return found;
222     }
223 
RunGetAddrInfoStressTest_Binder(unsigned num_hosts,unsigned num_threads,unsigned num_queries)224     void RunGetAddrInfoStressTest_Binder(unsigned num_hosts, unsigned num_threads,
225             unsigned num_queries) {
226         std::vector<std::string> domains = { "example.com" };
227         std::vector<std::unique_ptr<test::DNSResponder>> dns;
228         std::vector<std::string> servers;
229         std::vector<DnsResponderClient::Mapping> mappings;
230         ASSERT_NO_FATAL_FAILURE(SetupMappings(num_hosts, domains, &mappings));
231         ASSERT_NO_FATAL_FAILURE(SetupDNSServers(MAXNS, mappings, &dns, &servers));
232 
233         ASSERT_TRUE(SetResolversForNetwork(servers, domains, mDefaultParams_Binder));
234 
235         auto t0 = std::chrono::steady_clock::now();
236         std::vector<std::thread> threads(num_threads);
237         for (std::thread& thread : threads) {
238            thread = std::thread([this, &servers, &dns, &mappings, num_queries]() {
239                 for (unsigned i = 0 ; i < num_queries ; ++i) {
240                     uint32_t ofs = arc4random_uniform(mappings.size());
241                     auto& mapping = mappings[ofs];
242                     addrinfo* result = nullptr;
243                     int rv = getaddrinfo(mapping.host.c_str(), nullptr, nullptr, &result);
244                     EXPECT_EQ(0, rv) << "error [" << rv << "] " << gai_strerror(rv);
245                     if (rv == 0) {
246                         std::string result_str = ToString(result);
247                         EXPECT_TRUE(result_str == mapping.ip4 || result_str == mapping.ip6)
248                             << "result='" << result_str << "', ip4='" << mapping.ip4
249                             << "', ip6='" << mapping.ip6;
250                     }
251                     if (result) {
252                         freeaddrinfo(result);
253                         result = nullptr;
254                     }
255                 }
256             });
257         }
258 
259         for (std::thread& thread : threads) {
260             thread.join();
261         }
262         auto t1 = std::chrono::steady_clock::now();
263         ALOGI("%u hosts, %u threads, %u queries, %Es", num_hosts, num_threads, num_queries,
264                 std::chrono::duration<double>(t1 - t0).count());
265         ASSERT_NO_FATAL_FAILURE(ShutdownDNSServers(&dns));
266     }
267 
268     const std::vector<std::string> mDefaultSearchDomains = { "example.com" };
269     // <sample validity in s> <success threshold in percent> <min samples> <max samples>
270     const std::string mDefaultParams = "300 25 8 8";
271     const std::vector<int> mDefaultParams_Binder = { 300, 25, 8, 8 };
272 };
273 
TEST_F(ResolverTest,GetHostByName)274 TEST_F(ResolverTest, GetHostByName) {
275     const char* listen_addr = "127.0.0.3";
276     const char* listen_srv = "53";
277     const char* host_name = "hello.example.com.";
278     const char *nonexistent_host_name = "nonexistent.example.com.";
279     test::DNSResponder dns(listen_addr, listen_srv, 250, ns_rcode::ns_r_servfail, 1.0);
280     dns.addMapping(host_name, ns_type::ns_t_a, "1.2.3.3");
281     ASSERT_TRUE(dns.startServer());
282     std::vector<std::string> servers = { listen_addr };
283     ASSERT_TRUE(SetResolversForNetwork(mDefaultSearchDomains, servers, mDefaultParams));
284 
285     const hostent* result;
286 
287     dns.clearQueries();
288     result = gethostbyname("nonexistent");
289     EXPECT_EQ(1U, GetNumQueriesForType(dns, ns_type::ns_t_a, nonexistent_host_name));
290     ASSERT_TRUE(result == nullptr);
291     ASSERT_EQ(HOST_NOT_FOUND, h_errno);
292 
293     dns.clearQueries();
294     result = gethostbyname("hello");
295     EXPECT_EQ(1U, GetNumQueriesForType(dns, ns_type::ns_t_a, host_name));
296     ASSERT_FALSE(result == nullptr);
297     ASSERT_EQ(4, result->h_length);
298     ASSERT_FALSE(result->h_addr_list[0] == nullptr);
299     EXPECT_EQ("1.2.3.3", ToString(result));
300     EXPECT_TRUE(result->h_addr_list[1] == nullptr);
301 
302     dns.stopServer();
303 }
304 
TEST_F(ResolverTest,TestBinderSerialization)305 TEST_F(ResolverTest, TestBinderSerialization) {
306     using android::net::INetd;
307     std::vector<int> params_offsets = {
308         INetd::RESOLVER_PARAMS_SAMPLE_VALIDITY,
309         INetd::RESOLVER_PARAMS_SUCCESS_THRESHOLD,
310         INetd::RESOLVER_PARAMS_MIN_SAMPLES,
311         INetd::RESOLVER_PARAMS_MAX_SAMPLES
312     };
313     int size = static_cast<int>(params_offsets.size());
314     EXPECT_EQ(size, INetd::RESOLVER_PARAMS_COUNT);
315     std::sort(params_offsets.begin(), params_offsets.end());
316     for (int i = 0 ; i < size ; ++i) {
317         EXPECT_EQ(params_offsets[i], i);
318     }
319 }
320 
TEST_F(ResolverTest,GetHostByName_Binder)321 TEST_F(ResolverTest, GetHostByName_Binder) {
322     using android::net::INetd;
323 
324     std::vector<std::string> domains = { "example.com" };
325     std::vector<std::unique_ptr<test::DNSResponder>> dns;
326     std::vector<std::string> servers;
327     std::vector<Mapping> mappings;
328     ASSERT_NO_FATAL_FAILURE(SetupMappings(1, domains, &mappings));
329     ASSERT_NO_FATAL_FAILURE(SetupDNSServers(4, mappings, &dns, &servers));
330     ASSERT_EQ(1U, mappings.size());
331     const Mapping& mapping = mappings[0];
332 
333     ASSERT_TRUE(SetResolversForNetwork(servers, domains, mDefaultParams_Binder));
334 
335     const hostent* result = gethostbyname(mapping.host.c_str());
336     size_t total_queries = std::accumulate(dns.begin(), dns.end(), 0,
337             [this, &mapping](size_t total, auto& d) {
338                 return total + GetNumQueriesForType(*d, ns_type::ns_t_a, mapping.entry.c_str());
339             });
340 
341     EXPECT_LE(1U, total_queries);
342     ASSERT_FALSE(result == nullptr);
343     ASSERT_EQ(4, result->h_length);
344     ASSERT_FALSE(result->h_addr_list[0] == nullptr);
345     EXPECT_EQ(mapping.ip4, ToString(result));
346     EXPECT_TRUE(result->h_addr_list[1] == nullptr);
347 
348     std::vector<std::string> res_servers;
349     std::vector<std::string> res_domains;
350     __res_params res_params;
351     std::vector<ResolverStats> res_stats;
352     ASSERT_TRUE(GetResolverInfo(&res_servers, &res_domains, &res_params, &res_stats));
353     EXPECT_EQ(servers.size(), res_servers.size());
354     EXPECT_EQ(domains.size(), res_domains.size());
355     ASSERT_EQ(INetd::RESOLVER_PARAMS_COUNT, mDefaultParams_Binder.size());
356     EXPECT_EQ(mDefaultParams_Binder[INetd::RESOLVER_PARAMS_SAMPLE_VALIDITY],
357             res_params.sample_validity);
358     EXPECT_EQ(mDefaultParams_Binder[INetd::RESOLVER_PARAMS_SUCCESS_THRESHOLD],
359             res_params.success_threshold);
360     EXPECT_EQ(mDefaultParams_Binder[INetd::RESOLVER_PARAMS_MIN_SAMPLES], res_params.min_samples);
361     EXPECT_EQ(mDefaultParams_Binder[INetd::RESOLVER_PARAMS_MAX_SAMPLES], res_params.max_samples);
362     EXPECT_EQ(servers.size(), res_stats.size());
363 
364     EXPECT_TRUE(UnorderedCompareArray(res_servers, servers));
365     EXPECT_TRUE(UnorderedCompareArray(res_domains, domains));
366 
367     ASSERT_NO_FATAL_FAILURE(ShutdownDNSServers(&dns));
368 }
369 
TEST_F(ResolverTest,GetAddrInfo)370 TEST_F(ResolverTest, GetAddrInfo) {
371     addrinfo* result = nullptr;
372 
373     const char* listen_addr = "127.0.0.4";
374     const char* listen_addr2 = "127.0.0.5";
375     const char* listen_srv = "53";
376     const char* host_name = "howdy.example.com.";
377     test::DNSResponder dns(listen_addr, listen_srv, 250,
378                            ns_rcode::ns_r_servfail, 1.0);
379     dns.addMapping(host_name, ns_type::ns_t_a, "1.2.3.4");
380     dns.addMapping(host_name, ns_type::ns_t_aaaa, "::1.2.3.4");
381     ASSERT_TRUE(dns.startServer());
382 
383     test::DNSResponder dns2(listen_addr2, listen_srv, 250,
384                             ns_rcode::ns_r_servfail, 1.0);
385     dns2.addMapping(host_name, ns_type::ns_t_a, "1.2.3.4");
386     dns2.addMapping(host_name, ns_type::ns_t_aaaa, "::1.2.3.4");
387     ASSERT_TRUE(dns2.startServer());
388 
389     for (size_t i = 0 ; i < 1000 ; ++i) {
390         std::vector<std::string> servers = { listen_addr };
391         ASSERT_TRUE(SetResolversForNetwork(mDefaultSearchDomains, servers, mDefaultParams));
392         dns.clearQueries();
393         dns2.clearQueries();
394 
395         EXPECT_EQ(0, getaddrinfo("howdy", nullptr, nullptr, &result));
396         size_t found = GetNumQueries(dns, host_name);
397         EXPECT_LE(1U, found);
398         // Could be A or AAAA
399         std::string result_str = ToString(result);
400         EXPECT_TRUE(result_str == "1.2.3.4" || result_str == "::1.2.3.4")
401             << ", result_str='" << result_str << "'";
402         // TODO: Use ScopedAddrinfo or similar once it is available in a common header file.
403         if (result) {
404             freeaddrinfo(result);
405             result = nullptr;
406         }
407 
408         // Verify that the name is cached.
409         size_t old_found = found;
410         EXPECT_EQ(0, getaddrinfo("howdy", nullptr, nullptr, &result));
411         found = GetNumQueries(dns, host_name);
412         EXPECT_LE(1U, found);
413         EXPECT_EQ(old_found, found);
414         result_str = ToString(result);
415         EXPECT_TRUE(result_str == "1.2.3.4" || result_str == "::1.2.3.4")
416             << result_str;
417         if (result) {
418             freeaddrinfo(result);
419             result = nullptr;
420         }
421 
422         // Change the DNS resolver, ensure that queries are no longer cached.
423         servers = { listen_addr2 };
424         ASSERT_TRUE(SetResolversForNetwork(mDefaultSearchDomains, servers, mDefaultParams));
425         dns.clearQueries();
426         dns2.clearQueries();
427 
428         EXPECT_EQ(0, getaddrinfo("howdy", nullptr, nullptr, &result));
429         found = GetNumQueries(dns, host_name);
430         size_t found2 = GetNumQueries(dns2, host_name);
431         EXPECT_EQ(0U, found);
432         EXPECT_LE(1U, found2);
433 
434         // Could be A or AAAA
435         result_str = ToString(result);
436         EXPECT_TRUE(result_str == "1.2.3.4" || result_str == "::1.2.3.4")
437             << ", result_str='" << result_str << "'";
438         if (result) {
439             freeaddrinfo(result);
440             result = nullptr;
441         }
442     }
443     dns.stopServer();
444     dns2.stopServer();
445 }
446 
TEST_F(ResolverTest,GetAddrInfoV4)447 TEST_F(ResolverTest, GetAddrInfoV4) {
448     addrinfo* result = nullptr;
449 
450     const char* listen_addr = "127.0.0.5";
451     const char* listen_srv = "53";
452     const char* host_name = "hola.example.com.";
453     test::DNSResponder dns(listen_addr, listen_srv, 250,
454                            ns_rcode::ns_r_servfail, 1.0);
455     dns.addMapping(host_name, ns_type::ns_t_a, "1.2.3.5");
456     ASSERT_TRUE(dns.startServer());
457     std::vector<std::string> servers = { listen_addr };
458     ASSERT_TRUE(SetResolversForNetwork(mDefaultSearchDomains, servers, mDefaultParams));
459 
460     addrinfo hints;
461     memset(&hints, 0, sizeof(hints));
462     hints.ai_family = AF_INET;
463     EXPECT_EQ(0, getaddrinfo("hola", nullptr, &hints, &result));
464     EXPECT_EQ(1U, GetNumQueries(dns, host_name));
465     EXPECT_EQ("1.2.3.5", ToString(result));
466     if (result) {
467         freeaddrinfo(result);
468         result = nullptr;
469     }
470 }
471 
TEST_F(ResolverTest,MultidomainResolution)472 TEST_F(ResolverTest, MultidomainResolution) {
473     std::vector<std::string> searchDomains = { "example1.com", "example2.com", "example3.com" };
474     const char* listen_addr = "127.0.0.6";
475     const char* listen_srv = "53";
476     const char* host_name = "nihao.example2.com.";
477     test::DNSResponder dns(listen_addr, listen_srv, 250,
478                            ns_rcode::ns_r_servfail, 1.0);
479     dns.addMapping(host_name, ns_type::ns_t_a, "1.2.3.3");
480     ASSERT_TRUE(dns.startServer());
481     std::vector<std::string> servers = { listen_addr };
482     ASSERT_TRUE(SetResolversForNetwork(searchDomains, servers, mDefaultParams));
483 
484     dns.clearQueries();
485     const hostent* result = gethostbyname("nihao");
486     EXPECT_EQ(1U, GetNumQueriesForType(dns, ns_type::ns_t_a, host_name));
487     ASSERT_FALSE(result == nullptr);
488     ASSERT_EQ(4, result->h_length);
489     ASSERT_FALSE(result->h_addr_list[0] == nullptr);
490     EXPECT_EQ("1.2.3.3", ToString(result));
491     EXPECT_TRUE(result->h_addr_list[1] == nullptr);
492     dns.stopServer();
493 }
494 
TEST_F(ResolverTest,GetAddrInfoV6_failing)495 TEST_F(ResolverTest, GetAddrInfoV6_failing) {
496     addrinfo* result = nullptr;
497 
498     const char* listen_addr0 = "127.0.0.7";
499     const char* listen_addr1 = "127.0.0.8";
500     const char* listen_srv = "53";
501     const char* host_name = "ohayou.example.com.";
502     test::DNSResponder dns0(listen_addr0, listen_srv, 250,
503                             ns_rcode::ns_r_servfail, 0.0);
504     test::DNSResponder dns1(listen_addr1, listen_srv, 250,
505                             ns_rcode::ns_r_servfail, 1.0);
506     dns0.addMapping(host_name, ns_type::ns_t_aaaa, "2001:db8::5");
507     dns1.addMapping(host_name, ns_type::ns_t_aaaa, "2001:db8::6");
508     ASSERT_TRUE(dns0.startServer());
509     ASSERT_TRUE(dns1.startServer());
510     std::vector<std::string> servers = { listen_addr0, listen_addr1 };
511     // <sample validity in s> <success threshold in percent> <min samples> <max samples>
512     unsigned sample_validity = 300;
513     int success_threshold = 25;
514     int sample_count = 8;
515     std::string params = StringPrintf("%u %d %d %d", sample_validity, success_threshold,
516             sample_count, sample_count);
517     ASSERT_TRUE(SetResolversForNetwork(mDefaultSearchDomains, servers, params));
518 
519     // Repeatedly perform resolutions for non-existing domains until MAXNSSAMPLES resolutions have
520     // reached the dns0, which is set to fail. No more requests should then arrive at that server
521     // for the next sample_lifetime seconds.
522     // TODO: This approach is implementation-dependent, change once metrics reporting is available.
523     addrinfo hints;
524     memset(&hints, 0, sizeof(hints));
525     hints.ai_family = AF_INET6;
526     for (int i = 0 ; i < sample_count ; ++i) {
527         std::string domain = StringPrintf("nonexistent%d", i);
528         getaddrinfo(domain.c_str(), nullptr, &hints, &result);
529         if (result) {
530             freeaddrinfo(result);
531             result = nullptr;
532         }
533     }
534     // Due to 100% errors for all possible samples, the server should be ignored from now on and
535     // only the second one used for all following queries, until NSSAMPLE_VALIDITY is reached.
536     dns0.clearQueries();
537     dns1.clearQueries();
538     EXPECT_EQ(0, getaddrinfo("ohayou", nullptr, &hints, &result));
539     EXPECT_EQ(0U, GetNumQueries(dns0, host_name));
540     EXPECT_EQ(1U, GetNumQueries(dns1, host_name));
541     if (result) {
542         freeaddrinfo(result);
543         result = nullptr;
544     }
545 }
546 
TEST_F(ResolverTest,GetAddrInfoV6_concurrent)547 TEST_F(ResolverTest, GetAddrInfoV6_concurrent) {
548     const char* listen_addr0 = "127.0.0.9";
549     const char* listen_addr1 = "127.0.0.10";
550     const char* listen_addr2 = "127.0.0.11";
551     const char* listen_srv = "53";
552     const char* host_name = "konbanha.example.com.";
553     test::DNSResponder dns0(listen_addr0, listen_srv, 250,
554                             ns_rcode::ns_r_servfail, 1.0);
555     test::DNSResponder dns1(listen_addr1, listen_srv, 250,
556                             ns_rcode::ns_r_servfail, 1.0);
557     test::DNSResponder dns2(listen_addr2, listen_srv, 250,
558                             ns_rcode::ns_r_servfail, 1.0);
559     dns0.addMapping(host_name, ns_type::ns_t_aaaa, "2001:db8::5");
560     dns1.addMapping(host_name, ns_type::ns_t_aaaa, "2001:db8::6");
561     dns2.addMapping(host_name, ns_type::ns_t_aaaa, "2001:db8::7");
562     ASSERT_TRUE(dns0.startServer());
563     ASSERT_TRUE(dns1.startServer());
564     ASSERT_TRUE(dns2.startServer());
565     const std::vector<std::string> servers = { listen_addr0, listen_addr1, listen_addr2 };
566     std::vector<std::thread> threads(10);
567     for (std::thread& thread : threads) {
568        thread = std::thread([this, &servers, &dns0, &dns1, &dns2]() {
569             unsigned delay = arc4random_uniform(1*1000*1000); // <= 1s
570             usleep(delay);
571             std::vector<std::string> serverSubset;
572             for (const auto& server : servers) {
573                 if (arc4random_uniform(2)) {
574                     serverSubset.push_back(server);
575                 }
576             }
577             if (serverSubset.empty()) serverSubset = servers;
578             ASSERT_TRUE(SetResolversForNetwork(mDefaultSearchDomains, serverSubset,
579                     mDefaultParams));
580             addrinfo hints;
581             memset(&hints, 0, sizeof(hints));
582             hints.ai_family = AF_INET6;
583             addrinfo* result = nullptr;
584             int rv = getaddrinfo("konbanha", nullptr, &hints, &result);
585             EXPECT_EQ(0, rv) << "error [" << rv << "] " << gai_strerror(rv);
586             if (result) {
587                 freeaddrinfo(result);
588                 result = nullptr;
589             }
590         });
591     }
592     for (std::thread& thread : threads) {
593         thread.join();
594     }
595 }
596 
TEST_F(ResolverTest,GetAddrInfoStressTest_Binder_100)597 TEST_F(ResolverTest, GetAddrInfoStressTest_Binder_100) {
598     const unsigned num_hosts = 100;
599     const unsigned num_threads = 100;
600     const unsigned num_queries = 100;
601     ASSERT_NO_FATAL_FAILURE(RunGetAddrInfoStressTest_Binder(num_hosts, num_threads, num_queries));
602 }
603 
TEST_F(ResolverTest,GetAddrInfoStressTest_Binder_100000)604 TEST_F(ResolverTest, GetAddrInfoStressTest_Binder_100000) {
605     const unsigned num_hosts = 100000;
606     const unsigned num_threads = 100;
607     const unsigned num_queries = 100;
608     ASSERT_NO_FATAL_FAILURE(RunGetAddrInfoStressTest_Binder(num_hosts, num_threads, num_queries));
609 }
610 
TEST_F(ResolverTest,EmptySetup)611 TEST_F(ResolverTest, EmptySetup) {
612     using android::net::INetd;
613     std::vector<std::string> servers;
614     std::vector<std::string> domains;
615     ASSERT_TRUE(SetResolversForNetwork(servers, domains, mDefaultParams_Binder));
616     std::vector<std::string> res_servers;
617     std::vector<std::string> res_domains;
618     __res_params res_params;
619     std::vector<ResolverStats> res_stats;
620     ASSERT_TRUE(GetResolverInfo(&res_servers, &res_domains, &res_params, &res_stats));
621     EXPECT_EQ(0U, res_servers.size());
622     EXPECT_EQ(0U, res_domains.size());
623     ASSERT_EQ(INetd::RESOLVER_PARAMS_COUNT, mDefaultParams_Binder.size());
624     EXPECT_EQ(mDefaultParams_Binder[INetd::RESOLVER_PARAMS_SAMPLE_VALIDITY],
625             res_params.sample_validity);
626     EXPECT_EQ(mDefaultParams_Binder[INetd::RESOLVER_PARAMS_SUCCESS_THRESHOLD],
627             res_params.success_threshold);
628     EXPECT_EQ(mDefaultParams_Binder[INetd::RESOLVER_PARAMS_MIN_SAMPLES], res_params.min_samples);
629     EXPECT_EQ(mDefaultParams_Binder[INetd::RESOLVER_PARAMS_MAX_SAMPLES], res_params.max_samples);
630 }
631 
TEST_F(ResolverTest,SearchPathChange)632 TEST_F(ResolverTest, SearchPathChange) {
633     addrinfo* result = nullptr;
634 
635     const char* listen_addr = "127.0.0.13";
636     const char* listen_srv = "53";
637     const char* host_name1 = "test13.domain1.org.";
638     const char* host_name2 = "test13.domain2.org.";
639     test::DNSResponder dns(listen_addr, listen_srv, 250,
640                            ns_rcode::ns_r_servfail, 1.0);
641     dns.addMapping(host_name1, ns_type::ns_t_aaaa, "2001:db8::13");
642     dns.addMapping(host_name2, ns_type::ns_t_aaaa, "2001:db8::1:13");
643     ASSERT_TRUE(dns.startServer());
644     std::vector<std::string> servers = { listen_addr };
645     std::vector<std::string> domains = { "domain1.org" };
646     ASSERT_TRUE(SetResolversForNetwork(domains, servers, mDefaultParams));
647 
648     addrinfo hints;
649     memset(&hints, 0, sizeof(hints));
650     hints.ai_family = AF_INET6;
651     EXPECT_EQ(0, getaddrinfo("test13", nullptr, &hints, &result));
652     EXPECT_EQ(1U, dns.queries().size());
653     EXPECT_EQ(1U, GetNumQueries(dns, host_name1));
654     EXPECT_EQ("2001:db8::13", ToString(result));
655     if (result) freeaddrinfo(result);
656 
657     // Test that changing the domain search path on its own works.
658     domains = { "domain2.org" };
659     ASSERT_TRUE(SetResolversForNetwork(domains, servers, mDefaultParams));
660     dns.clearQueries();
661 
662     EXPECT_EQ(0, getaddrinfo("test13", nullptr, &hints, &result));
663     EXPECT_EQ(1U, dns.queries().size());
664     EXPECT_EQ(1U, GetNumQueries(dns, host_name2));
665     EXPECT_EQ("2001:db8::1:13", ToString(result));
666     if (result) freeaddrinfo(result);
667 }
668 
TEST_F(ResolverTest,MaxServerPrune_Binder)669 TEST_F(ResolverTest, MaxServerPrune_Binder) {
670     using android::net::INetd;
671 
672     std::vector<std::string> domains = { "example.com" };
673     std::vector<std::unique_ptr<test::DNSResponder>> dns;
674     std::vector<std::string> servers;
675     std::vector<Mapping> mappings;
676     ASSERT_NO_FATAL_FAILURE(SetupMappings(1, domains, &mappings));
677     ASSERT_NO_FATAL_FAILURE(SetupDNSServers(MAXNS + 1, mappings, &dns, &servers));
678 
679     ASSERT_TRUE(SetResolversForNetwork(servers, domains,  mDefaultParams_Binder));
680 
681     std::vector<std::string> res_servers;
682     std::vector<std::string> res_domains;
683     __res_params res_params;
684     std::vector<ResolverStats> res_stats;
685     ASSERT_TRUE(GetResolverInfo(&res_servers, &res_domains, &res_params, &res_stats));
686     EXPECT_EQ(static_cast<size_t>(MAXNS), res_servers.size());
687 
688     ASSERT_NO_FATAL_FAILURE(ShutdownDNSServers(&dns));
689 }
690