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