Luke Huang | 3c5f35a | 2018-11-21 14:38:40 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #include <arpa/inet.h> |
| 18 | #include <arpa/nameser.h> |
Luke Huang | 3c5f35a | 2018-11-21 14:38:40 +0800 | [diff] [blame] | 19 | #include <error.h> |
| 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <inttypes.h> |
Luke Huang | c4464a0 | 2018-12-21 18:55:11 +0800 | [diff] [blame^] | 23 | #include <netinet/in.h> |
Luke Huang | 3c5f35a | 2018-11-21 14:38:40 +0800 | [diff] [blame] | 24 | #include <poll.h> /* poll */ |
| 25 | #include <resolv.h> |
| 26 | #include <string.h> |
Luke Huang | c4464a0 | 2018-12-21 18:55:11 +0800 | [diff] [blame^] | 27 | #include <sys/socket.h> |
Luke Huang | 3c5f35a | 2018-11-21 14:38:40 +0800 | [diff] [blame] | 28 | |
| 29 | #include <android/multinetwork.h> |
| 30 | #include <gtest/gtest.h> |
| 31 | |
| 32 | namespace { |
| 33 | constexpr int MAXPACKET = 8 * 1024; |
| 34 | constexpr int PTON_MAX = 16; |
Luke Huang | c4464a0 | 2018-12-21 18:55:11 +0800 | [diff] [blame^] | 35 | constexpr int TIMEOUT_MS = 10000; |
Luke Huang | 3c5f35a | 2018-11-21 14:38:40 +0800 | [diff] [blame] | 36 | |
Luke Huang | c4464a0 | 2018-12-21 18:55:11 +0800 | [diff] [blame^] | 37 | int getAsyncResponse(int fd, int timeoutMs, int* rcode, uint8_t* buf, size_t bufLen) { |
Luke Huang | 3c5f35a | 2018-11-21 14:38:40 +0800 | [diff] [blame] | 38 | struct pollfd wait_fd[1]; |
| 39 | wait_fd[0].fd = fd; |
| 40 | wait_fd[0].events = POLLIN; |
| 41 | short revents; |
| 42 | int ret; |
| 43 | ret = poll(wait_fd, 1, timeoutMs); |
| 44 | revents = wait_fd[0].revents; |
| 45 | if (revents & POLLIN) { |
| 46 | int n = android_res_nresult(fd, rcode, buf, bufLen); |
| 47 | return n; |
| 48 | } |
| 49 | |
| 50 | return -1; |
| 51 | } |
| 52 | |
Luke Huang | c4464a0 | 2018-12-21 18:55:11 +0800 | [diff] [blame^] | 53 | std::vector<std::string> extractIpAddressAnswers(uint8_t* buf, size_t bufLen, int ipType) { |
Luke Huang | 3c5f35a | 2018-11-21 14:38:40 +0800 | [diff] [blame] | 54 | ns_msg handle; |
| 55 | if (ns_initparse((const uint8_t*) buf, bufLen, &handle) < 0) { |
| 56 | return {}; |
| 57 | } |
Luke Huang | c4464a0 | 2018-12-21 18:55:11 +0800 | [diff] [blame^] | 58 | const int ancount = ns_msg_count(handle, ns_s_an); |
Luke Huang | 3c5f35a | 2018-11-21 14:38:40 +0800 | [diff] [blame] | 59 | ns_rr rr; |
| 60 | std::vector<std::string> answers; |
| 61 | for (int i = 0; i < ancount; i++) { |
| 62 | if (ns_parserr(&handle, ns_s_an, i, &rr) < 0) { |
| 63 | continue; |
| 64 | } |
Luke Huang | c4464a0 | 2018-12-21 18:55:11 +0800 | [diff] [blame^] | 65 | const uint8_t* rdata = ns_rr_rdata(rr); |
Luke Huang | 3c5f35a | 2018-11-21 14:38:40 +0800 | [diff] [blame] | 66 | char buffer[INET6_ADDRSTRLEN]; |
| 67 | if (inet_ntop(ipType, (const char*) rdata, buffer, sizeof(buffer))) { |
| 68 | answers.push_back(buffer); |
| 69 | } |
| 70 | } |
| 71 | return answers; |
| 72 | } |
| 73 | |
| 74 | void expectAnswersValid(int fd, int ipType, int expectedRcode) { |
| 75 | int rcode = -1; |
Luke Huang | c4464a0 | 2018-12-21 18:55:11 +0800 | [diff] [blame^] | 76 | uint8_t buf[MAXPACKET] = {}; |
| 77 | int res = getAsyncResponse(fd, TIMEOUT_MS, &rcode, buf, MAXPACKET); |
| 78 | EXPECT_GE(res, 0); |
Luke Huang | 3c5f35a | 2018-11-21 14:38:40 +0800 | [diff] [blame] | 79 | EXPECT_EQ(rcode, expectedRcode); |
| 80 | |
| 81 | |
Luke Huang | c4464a0 | 2018-12-21 18:55:11 +0800 | [diff] [blame^] | 82 | if (expectedRcode == ns_r_noerror) { |
Luke Huang | 3c5f35a | 2018-11-21 14:38:40 +0800 | [diff] [blame] | 83 | auto answers = extractIpAddressAnswers(buf, res, ipType); |
Luke Huang | c4464a0 | 2018-12-21 18:55:11 +0800 | [diff] [blame^] | 84 | EXPECT_GE(answers.size(), 0U); |
Luke Huang | 3c5f35a | 2018-11-21 14:38:40 +0800 | [diff] [blame] | 85 | for (auto &answer : answers) { |
| 86 | char pton[PTON_MAX]; |
| 87 | EXPECT_EQ(1, inet_pton(ipType, answer.c_str(), pton)); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
Luke Huang | c4464a0 | 2018-12-21 18:55:11 +0800 | [diff] [blame^] | 92 | void expectAnswersNotValid(int fd, int expectedErrno) { |
| 93 | int rcode = -1; |
| 94 | uint8_t buf[MAXPACKET] = {}; |
| 95 | int res = getAsyncResponse(fd, TIMEOUT_MS, &rcode, buf, MAXPACKET); |
| 96 | EXPECT_EQ(expectedErrno, res); |
| 97 | } |
| 98 | |
Luke Huang | 3c5f35a | 2018-11-21 14:38:40 +0800 | [diff] [blame] | 99 | } // namespace |
| 100 | |
| 101 | TEST (NativeDnsAsyncTest, Async_Query) { |
| 102 | // V4 |
Luke Huang | c4464a0 | 2018-12-21 18:55:11 +0800 | [diff] [blame^] | 103 | int fd1 = android_res_nquery( |
| 104 | NETWORK_UNSPECIFIED, "www.google.com", ns_c_in, ns_t_a, ResNsendFlags(0)); |
| 105 | EXPECT_GE(fd1, 0); |
| 106 | int fd2 = android_res_nquery( |
| 107 | NETWORK_UNSPECIFIED, "www.youtube.com", ns_c_in, ns_t_a, ResNsendFlags(0)); |
| 108 | EXPECT_GE(fd2, 0); |
| 109 | expectAnswersValid(fd2, AF_INET, ns_r_noerror); |
| 110 | expectAnswersValid(fd1, AF_INET, ns_r_noerror); |
Luke Huang | 3c5f35a | 2018-11-21 14:38:40 +0800 | [diff] [blame] | 111 | |
| 112 | // V6 |
Luke Huang | c4464a0 | 2018-12-21 18:55:11 +0800 | [diff] [blame^] | 113 | fd1 = android_res_nquery( |
| 114 | NETWORK_UNSPECIFIED, "www.google.com", ns_c_in, ns_t_aaaa, ResNsendFlags(0)); |
| 115 | EXPECT_GE(fd1, 0); |
| 116 | fd2 = android_res_nquery( |
| 117 | NETWORK_UNSPECIFIED, "www.youtube.com", ns_c_in, ns_t_aaaa, ResNsendFlags(0)); |
| 118 | EXPECT_GE(fd2, 0); |
| 119 | expectAnswersValid(fd2, AF_INET6, ns_r_noerror); |
| 120 | expectAnswersValid(fd1, AF_INET6, ns_r_noerror); |
Luke Huang | 3c5f35a | 2018-11-21 14:38:40 +0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | TEST (NativeDnsAsyncTest, Async_Send) { |
| 124 | // V4 |
Luke Huang | c4464a0 | 2018-12-21 18:55:11 +0800 | [diff] [blame^] | 125 | uint8_t buf1[MAXPACKET] = {}; |
| 126 | int len1 = res_mkquery(ns_o_query, "www.googleapis.com", |
| 127 | ns_c_in, ns_t_a, nullptr, 0, nullptr, buf1, sizeof(buf1)); |
| 128 | EXPECT_GT(len1, 0); |
| 129 | |
| 130 | uint8_t buf2[MAXPACKET] = {}; |
| 131 | int len2 = res_mkquery(ns_o_query, "play.googleapis.com", |
| 132 | ns_c_in, ns_t_a, nullptr, 0, nullptr, buf2, sizeof(buf2)); |
| 133 | EXPECT_GT(len2, 0); |
| 134 | |
| 135 | int fd1 = android_res_nsend(NETWORK_UNSPECIFIED, buf1, len1, ResNsendFlags(0)); |
| 136 | EXPECT_GE(fd1, 0); |
| 137 | int fd2 = android_res_nsend(NETWORK_UNSPECIFIED, buf2, len2, ResNsendFlags(0)); |
| 138 | EXPECT_GE(fd2, 0); |
| 139 | |
| 140 | expectAnswersValid(fd2, AF_INET, ns_r_noerror); |
| 141 | expectAnswersValid(fd1, AF_INET, ns_r_noerror); |
Luke Huang | 3c5f35a | 2018-11-21 14:38:40 +0800 | [diff] [blame] | 142 | |
| 143 | // V6 |
Luke Huang | c4464a0 | 2018-12-21 18:55:11 +0800 | [diff] [blame^] | 144 | memset(buf1, 0, sizeof(buf1)); |
| 145 | memset(buf2, 0, sizeof(buf2)); |
| 146 | len1 = res_mkquery(ns_o_query, "www.googleapis.com", |
| 147 | ns_c_in, ns_t_aaaa, nullptr, 0, nullptr, buf1, sizeof(buf1)); |
| 148 | EXPECT_GT(len1, 0); |
| 149 | len2 = res_mkquery(ns_o_query, "play.googleapis.com", |
| 150 | ns_c_in, ns_t_aaaa, nullptr, 0, nullptr, buf2, sizeof(buf2)); |
| 151 | EXPECT_GT(len2, 0); |
| 152 | |
| 153 | fd1 = android_res_nsend(NETWORK_UNSPECIFIED, buf1, len1, ResNsendFlags(0)); |
| 154 | EXPECT_GE(fd1, 0); |
| 155 | fd2 = android_res_nsend(NETWORK_UNSPECIFIED, buf2, len2, ResNsendFlags(0)); |
| 156 | EXPECT_GE(fd2, 0); |
| 157 | |
| 158 | expectAnswersValid(fd2, AF_INET6, ns_r_noerror); |
| 159 | expectAnswersValid(fd1, AF_INET6, ns_r_noerror); |
Luke Huang | 3c5f35a | 2018-11-21 14:38:40 +0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | TEST (NativeDnsAsyncTest, Async_NXDOMAIN) { |
Luke Huang | c4464a0 | 2018-12-21 18:55:11 +0800 | [diff] [blame^] | 163 | uint8_t buf[MAXPACKET] = {}; |
| 164 | int len = res_mkquery(ns_o_query, "test1-nx.metric.gstatic.com", |
Luke Huang | 3c5f35a | 2018-11-21 14:38:40 +0800 | [diff] [blame] | 165 | ns_c_in, ns_t_a, nullptr, 0, nullptr, buf, sizeof(buf)); |
| 166 | EXPECT_GT(len, 0); |
Luke Huang | c4464a0 | 2018-12-21 18:55:11 +0800 | [diff] [blame^] | 167 | int fd1 = android_res_nsend(NETWORK_UNSPECIFIED, buf, len, ANDROID_RESOLV_NO_CACHE_LOOKUP); |
| 168 | EXPECT_GE(fd1, 0); |
| 169 | |
| 170 | len = res_mkquery(ns_o_query, "test2-nx.metric.gstatic.com", |
| 171 | ns_c_in, ns_t_a, nullptr, 0, nullptr, buf, sizeof(buf)); |
| 172 | EXPECT_GT(len, 0); |
| 173 | int fd2 = android_res_nsend(NETWORK_UNSPECIFIED, buf, len, ANDROID_RESOLV_NO_CACHE_LOOKUP); |
| 174 | EXPECT_GE(fd2, 0); |
| 175 | |
| 176 | expectAnswersValid(fd2, AF_INET, ns_r_nxdomain); |
| 177 | expectAnswersValid(fd1, AF_INET, ns_r_nxdomain); |
| 178 | |
| 179 | fd1 = android_res_nquery( |
| 180 | NETWORK_UNSPECIFIED, "test3-nx.metric.gstatic.com", |
| 181 | ns_c_in, ns_t_aaaa, ANDROID_RESOLV_NO_CACHE_LOOKUP); |
| 182 | EXPECT_GE(fd1, 0); |
| 183 | fd2 = android_res_nquery( |
| 184 | NETWORK_UNSPECIFIED, "test4-nx.metric.gstatic.com", |
| 185 | ns_c_in, ns_t_aaaa, ANDROID_RESOLV_NO_CACHE_LOOKUP); |
| 186 | EXPECT_GE(fd2, 0); |
| 187 | expectAnswersValid(fd2, AF_INET6, ns_r_nxdomain); |
| 188 | expectAnswersValid(fd1, AF_INET6, ns_r_nxdomain); |
Luke Huang | 3c5f35a | 2018-11-21 14:38:40 +0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | TEST (NativeDnsAsyncTest, Async_Cancel) { |
Luke Huang | c4464a0 | 2018-12-21 18:55:11 +0800 | [diff] [blame^] | 192 | int fd = android_res_nquery( |
| 193 | NETWORK_UNSPECIFIED, "www.google.com", ns_c_in, ns_t_a, ResNsendFlags(0)); |
Luke Huang | 3c5f35a | 2018-11-21 14:38:40 +0800 | [diff] [blame] | 194 | int rcode = -1; |
Luke Huang | c4464a0 | 2018-12-21 18:55:11 +0800 | [diff] [blame^] | 195 | uint8_t buf[MAXPACKET] = {}; |
| 196 | android_res_cancel(fd); |
Luke Huang | 3c5f35a | 2018-11-21 14:38:40 +0800 | [diff] [blame] | 197 | android_res_cancel(fd); |
| 198 | |
| 199 | int res = android_res_nresult(fd, &rcode, buf, MAXPACKET); |
Luke Huang | c4464a0 | 2018-12-21 18:55:11 +0800 | [diff] [blame^] | 200 | EXPECT_EQ(-EBADF, res); |
Luke Huang | 3c5f35a | 2018-11-21 14:38:40 +0800 | [diff] [blame] | 201 | } |
| 202 | |
Luke Huang | c4464a0 | 2018-12-21 18:55:11 +0800 | [diff] [blame^] | 203 | TEST (NativeDnsAsyncTest, Async_Query_MALFORMED) { |
| 204 | // Empty string to create BLOB and query, we will get empty result and rcode = 0 |
| 205 | // on DNSTLS. |
| 206 | int fd = android_res_nquery( |
| 207 | NETWORK_UNSPECIFIED, "", ns_c_in, ns_t_a, ResNsendFlags(0)); |
| 208 | EXPECT_GE(fd, 0); |
| 209 | expectAnswersValid(fd, AF_INET, ns_r_noerror); |
| 210 | |
| 211 | std::string exceedingLabelQuery = "www." + std::string(70, 'g') + ".com"; |
| 212 | std::string exceedingDomainQuery = "www." + std::string(255, 'g') + ".com"; |
| 213 | |
| 214 | fd = android_res_nquery(NETWORK_UNSPECIFIED, |
| 215 | exceedingLabelQuery.c_str(), ns_c_in, ns_t_a, ResNsendFlags(0)); |
| 216 | EXPECT_EQ(-EMSGSIZE, fd); |
| 217 | fd = android_res_nquery(NETWORK_UNSPECIFIED, |
| 218 | exceedingDomainQuery.c_str(), ns_c_in, ns_t_a, ResNsendFlags(0)); |
| 219 | EXPECT_EQ(-EMSGSIZE, fd); |
Luke Huang | 3c5f35a | 2018-11-21 14:38:40 +0800 | [diff] [blame] | 220 | } |
Luke Huang | c4464a0 | 2018-12-21 18:55:11 +0800 | [diff] [blame^] | 221 | |
| 222 | TEST (NativeDnsAsyncTest, Async_Send_MALFORMED) { |
| 223 | uint8_t buf[10] = {}; |
| 224 | // empty BLOB |
| 225 | int fd = android_res_nsend(NETWORK_UNSPECIFIED, buf, 10, ResNsendFlags(0)); |
| 226 | EXPECT_GE(fd, 0); |
| 227 | expectAnswersNotValid(fd, -EINVAL); |
| 228 | |
| 229 | std::vector<uint8_t> largeBuf(2 * MAXPACKET, 0); |
| 230 | // A buffer larger than 8KB |
| 231 | fd = android_res_nsend( |
| 232 | NETWORK_UNSPECIFIED, largeBuf.data(), largeBuf.size(), ResNsendFlags(0)); |
| 233 | EXPECT_EQ(-EMSGSIZE, fd); |
| 234 | |
| 235 | // 1000 bytes filled with 0. This returns EMSGSIZE because FrameworkListener limits the size of |
| 236 | // commands to 1024 bytes. TODO: fix this. |
| 237 | fd = android_res_nsend(NETWORK_UNSPECIFIED, largeBuf.data(), 1000, ResNsendFlags(0)); |
| 238 | EXPECT_EQ(-EMSGSIZE, fd); |
| 239 | |
| 240 | // 500 bytes filled with 0 |
| 241 | fd = android_res_nsend(NETWORK_UNSPECIFIED, largeBuf.data(), 500, ResNsendFlags(0)); |
| 242 | EXPECT_GE(fd, 0); |
| 243 | expectAnswersNotValid(fd, -EINVAL); |
| 244 | |
| 245 | // 1000 bytes filled with 0xFF |
| 246 | std::vector<uint8_t> ffBuf(1000, 255); |
| 247 | fd = android_res_nsend( |
| 248 | NETWORK_UNSPECIFIED, ffBuf.data(), ffBuf.size(), ResNsendFlags(0)); |
| 249 | EXPECT_EQ(-EMSGSIZE, fd); |
| 250 | |
| 251 | // 500 bytes filled with 0xFF |
| 252 | fd = android_res_nsend(NETWORK_UNSPECIFIED, ffBuf.data(), 500, ResNsendFlags(0)); |
| 253 | EXPECT_GE(fd, 0); |
| 254 | expectAnswersNotValid(fd, -EINVAL); |
| 255 | } |