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