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