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