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 #include "resolv_test_utils.h" 19 20 #include <arpa/inet.h> 21 22 #include <android-base/chrono_utils.h> 23 #include <android-base/logging.h> 24 25 using android::netdutils::ScopedAddrinfo; 26 ToString(const hostent * he)27 std::string ToString(const hostent* he) { 28 if (he == nullptr) return "<null>"; 29 char buffer[INET6_ADDRSTRLEN]; 30 if (!inet_ntop(he->h_addrtype, he->h_addr_list[0], buffer, sizeof(buffer))) { 31 return "<invalid>"; 32 } 33 return buffer; 34 } 35 ToString(const addrinfo * ai)36 std::string ToString(const addrinfo* ai) { 37 if (!ai) return "<null>"; 38 39 char host[NI_MAXHOST]; 40 int rv = getnameinfo(ai->ai_addr, ai->ai_addrlen, host, sizeof(host), nullptr, 0, 41 NI_NUMERICHOST); 42 if (rv != 0) return gai_strerror(rv); 43 return host; 44 } 45 ToString(const ScopedAddrinfo & ai)46 std::string ToString(const ScopedAddrinfo& ai) { 47 return ToString(ai.get()); 48 } 49 ToString(const sockaddr_storage * addr)50 std::string ToString(const sockaddr_storage* addr) { 51 if (!addr) return "<null>"; 52 char host[NI_MAXHOST]; 53 int rv = getnameinfo((const sockaddr*)addr, sizeof(sockaddr_storage), host, sizeof(host), 54 nullptr, 0, NI_NUMERICHOST); 55 if (rv != 0) return gai_strerror(rv); 56 return host; 57 } 58 ToStrings(const hostent * he)59 std::vector<std::string> ToStrings(const hostent* he) { 60 std::vector<std::string> hosts; 61 if (he == nullptr) { 62 hosts.push_back("<null>"); 63 return hosts; 64 } 65 uint32_t i = 0; 66 while (he->h_addr_list[i] != nullptr) { 67 char host[INET6_ADDRSTRLEN]; 68 if (!inet_ntop(he->h_addrtype, he->h_addr_list[i], host, sizeof(host))) { 69 hosts.push_back("<invalid>"); 70 return hosts; 71 } else { 72 hosts.push_back(host); 73 } 74 i++; 75 } 76 if (hosts.empty()) hosts.push_back("<invalid>"); 77 return hosts; 78 } 79 ToStrings(const addrinfo * ai)80 std::vector<std::string> ToStrings(const addrinfo* ai) { 81 std::vector<std::string> hosts; 82 if (!ai) { 83 hosts.push_back("<null>"); 84 return hosts; 85 } 86 for (const auto* aip = ai; aip != nullptr; aip = aip->ai_next) { 87 char host[NI_MAXHOST]; 88 int rv = getnameinfo(aip->ai_addr, aip->ai_addrlen, host, sizeof(host), nullptr, 0, 89 NI_NUMERICHOST); 90 if (rv != 0) { 91 hosts.clear(); 92 hosts.push_back(gai_strerror(rv)); 93 return hosts; 94 } else { 95 hosts.push_back(host); 96 } 97 } 98 if (hosts.empty()) hosts.push_back("<invalid>"); 99 return hosts; 100 } 101 ToStrings(const ScopedAddrinfo & ai)102 std::vector<std::string> ToStrings(const ScopedAddrinfo& ai) { 103 return ToStrings(ai.get()); 104 } 105 GetNumQueries(const test::DNSResponder & dns,const char * name)106 size_t GetNumQueries(const test::DNSResponder& dns, const char* name) { 107 std::vector<test::DNSResponder::QueryInfo> queries = dns.queries(); 108 size_t found = 0; 109 for (const auto& p : queries) { 110 if (p.name == name) { 111 ++found; 112 } 113 } 114 return found; 115 } 116 GetNumQueriesForProtocol(const test::DNSResponder & dns,const int protocol,const char * name)117 size_t GetNumQueriesForProtocol(const test::DNSResponder& dns, const int protocol, 118 const char* name) { 119 std::vector<test::DNSResponder::QueryInfo> queries = dns.queries(); 120 size_t found = 0; 121 for (const auto& p : queries) { 122 if (p.protocol == protocol && p.name == name) { 123 ++found; 124 } 125 } 126 return found; 127 } 128 GetNumQueriesForType(const test::DNSResponder & dns,ns_type type,const char * name)129 size_t GetNumQueriesForType(const test::DNSResponder& dns, ns_type type, const char* name) { 130 std::vector<test::DNSResponder::QueryInfo> queries = dns.queries(); 131 size_t found = 0; 132 for (const auto& p : queries) { 133 if (p.type == type && p.name == name) { 134 ++found; 135 } 136 } 137 return found; 138 } 139 PollForCondition(const std::function<bool ()> & condition,std::chrono::milliseconds timeout)140 bool PollForCondition(const std::function<bool()>& condition, std::chrono::milliseconds timeout) { 141 constexpr std::chrono::milliseconds retryIntervalMs{5}; 142 android::base::Timer t; 143 while (t.duration() < timeout) { 144 if (condition()) return true; 145 std::this_thread::sleep_for(retryIntervalMs); 146 } 147 return false; 148 } 149 safe_getaddrinfo(const char * node,const char * service,const struct addrinfo * hints)150 ScopedAddrinfo safe_getaddrinfo(const char* node, const char* service, 151 const struct addrinfo* hints) { 152 addrinfo* result = nullptr; 153 if (getaddrinfo(node, service, hints, &result) != 0) { 154 result = nullptr; // Should already be the case, but... 155 } 156 return ScopedAddrinfo(result); 157 } 158 WaitChild(pid_t pid)159 int WaitChild(pid_t pid) { 160 int status; 161 const pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); 162 163 if (got_pid != pid) { 164 PLOG(WARNING) << __func__ << ": waitpid failed: wanted " << pid << ", got " << got_pid; 165 return 1; 166 } 167 168 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { 169 return 0; 170 } else { 171 return status; 172 } 173 } 174 ForkAndRun(const std::vector<std::string> & args)175 int ForkAndRun(const std::vector<std::string>& args) { 176 std::vector<const char*> argv; 177 argv.resize(args.size() + 1, nullptr); 178 std::transform(args.begin(), args.end(), argv.begin(), 179 [](const std::string& in) { return in.c_str(); }); 180 181 pid_t pid = fork(); 182 if (pid == -1) { 183 // Fork failed. 184 PLOG(ERROR) << __func__ << ": Unable to fork"; 185 return -1; 186 } 187 188 if (pid == 0) { 189 execv(argv[0], const_cast<char**>(argv.data())); 190 PLOG(ERROR) << __func__ << ": execv failed"; 191 _exit(1); 192 } 193 194 int rc = WaitChild(pid); 195 if (rc != 0) { 196 PLOG(ERROR) << __func__ << ": Failed run: status=" << rc; 197 } 198 return rc; 199 } 200 201 // Add routing rules for MDNS packets, or MDNS packets won't know the destination is MDNS 202 // muticast address "224.0.0.251". SetMdnsRoute()203 void SetMdnsRoute() { 204 const std::vector<std::string> args = { 205 "system/bin/ip", "route", "add", "local", "224.0.0.251", "dev", "lo", 206 "proto", "static", "scope", "host", "src", "127.0.0.1", 207 }; 208 EXPECT_EQ(0, ForkAndRun(args)); 209 210 const std::vector<std::string> args_v6 = { 211 "system/bin/ip", "-6", "route", "add", "local", "ff02::fb", 212 "dev", "lo", "proto", "static", "src", "::1", 213 }; 214 EXPECT_EQ(0, ForkAndRun(args_v6)); 215 } 216 RemoveMdnsRoute()217 void RemoveMdnsRoute() { 218 const std::vector<std::string> args = { 219 "system/bin/ip", "route", "del", "local", "224.0.0.251", "dev", "lo", 220 "proto", "static", "scope", "host", "src", "127.0.0.1", 221 }; 222 EXPECT_EQ(0, ForkAndRun(args)); 223 224 const std::vector<std::string> args_v6 = { 225 "system/bin/ip", "-6", "route", "del", "local", "ff02::fb", 226 "dev", "lo", "proto", "static", "src", "::1", 227 }; 228 EXPECT_EQ(0, ForkAndRun(args_v6)); 229 } 230