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 <memory>
21 #include <string>
22 #include <vector>
23 
24 #include <aidl/android/net/IDnsResolver.h>
25 #include <aidl/android/net/INetd.h>
26 #include "ResolverStats.h"  // TODO: stop depending on this internal header
27 #include "dns_responder.h"
28 #include "dns_tls_certificate.h"
29 #include "params.h"
30 
31 inline const std::vector<std::string> kDefaultServers = {"127.0.0.3"};
32 inline const std::vector<std::string> kDefaultSearchDomains = {"example.com"};
33 inline const std::vector<int> kDefaultParams = {
34         300,      // sample validity in seconds
35         25,       // success threshod in percent
36         8,    8,  // {MIN,MAX}_SAMPLES
37         1000,     // BASE_TIMEOUT_MSEC
38         2,        // retry count
39 };
40 
41 #define SKIP_IF_REMOTE_VERSION_LESS_THAN(service, version)                                         \
42     do {                                                                                           \
43         if (!DnsResponderClient::isRemoteVersionSupported(service, version)) {                     \
44             std::cerr << "    Skip test. Remote version is too old, required version: " << version \
45                       << std::endl;                                                                \
46             return;                                                                                \
47         }                                                                                          \
48     } while (0)
49 
50 // TODO: Remove dns_responder_client_ndk.{h,cpp} after replacing the binder usage of
51 // dns_responder_client.*
52 class DnsResponderClient {
53   public:
54     struct Mapping {
55         std::string host;
56         std::string entry;
57         std::string ip4;
58         std::string ip6;
59     };
60 
61     virtual ~DnsResponderClient() = default;
62 
63     static void SetupMappings(unsigned num_hosts, const std::vector<std::string>& domains,
64                               std::vector<Mapping>* mappings);
65 
66     // This function is deprecated. Please use SetResolversFromParcel() instead.
67     bool SetResolversForNetwork(const std::vector<std::string>& servers = kDefaultServers,
68                                 const std::vector<std::string>& domains = kDefaultSearchDomains,
69                                 const std::vector<int>& params = kDefaultParams);
70 
71     // This function is deprecated. Please use SetResolversFromParcel() instead.
SetResolversWithTls(const std::vector<std::string> & servers,const std::vector<std::string> & searchDomains,const std::vector<int> & params,const std::string & name)72     bool SetResolversWithTls(const std::vector<std::string>& servers,
73                              const std::vector<std::string>& searchDomains,
74                              const std::vector<int>& params, const std::string& name) {
75         // Pass servers as both network-assigned and TLS servers.  Tests can
76         // determine on which server and by which protocol queries arrived.
77         return SetResolversWithTls(servers, searchDomains, params, servers, name);
78     }
79 
80     // This function is deprecated. Please use SetResolversFromParcel() instead.
81     bool SetResolversWithTls(const std::vector<std::string>& servers,
82                              const std::vector<std::string>& searchDomains,
83                              const std::vector<int>& params,
84                              const std::vector<std::string>& tlsServers, const std::string& name);
85 
86     bool SetResolversFromParcel(const aidl::android::net::ResolverParamsParcel& resolverParams);
87 
88     static bool isRemoteVersionSupported(aidl::android::net::IDnsResolver* dnsResolverService,
89                                          int enabledVersion);
90 
91     static bool GetResolverInfo(aidl::android::net::IDnsResolver* dnsResolverService,
92                                 unsigned netId, std::vector<std::string>* servers,
93                                 std::vector<std::string>* domains,
94                                 std::vector<std::string>* tlsServers, res_params* params,
95                                 std::vector<android::net::ResolverStats>* stats,
96                                 int* waitForPendingReqTimeoutCount);
97 
98     // Return a default resolver configuration for opportunistic mode.
99     static aidl::android::net::ResolverParamsParcel GetDefaultResolverParamsParcel();
100 
101     static void SetupDNSServers(unsigned numServers, const std::vector<Mapping>& mappings,
102                                 std::vector<std::unique_ptr<test::DNSResponder>>* dns,
103                                 std::vector<std::string>* servers);
104 
105     static aidl::android::net::ResolverParamsParcel makeResolverParamsParcel(
106             int netId, const std::vector<int>& params, const std::vector<std::string>& servers,
107             const std::vector<std::string>& domains, const std::string& tlsHostname,
108             const std::vector<std::string>& tlsServers, const std::string& caCert = "");
109 
110     int SetupOemNetwork();
111 
112     void TearDownOemNetwork(int oemNetId);
113 
114     virtual void SetUp();
115     virtual void TearDown();
116 
resolvService()117     aidl::android::net::IDnsResolver* resolvService() const { return mDnsResolvSrv.get(); }
netdService()118     aidl::android::net::INetd* netdService() const { return mNetdSrv.get(); }
119 
120   private:
121     std::shared_ptr<aidl::android::net::INetd> mNetdSrv;
122     std::shared_ptr<aidl::android::net::IDnsResolver> mDnsResolvSrv;
123     int mOemNetId = -1;
124 };
125