1 /*
2  * Copyright (C) 2020 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 "doh.h"
18 
19 #include <chrono>
20 #include <condition_variable>
21 #include <mutex>
22 
23 #include <resolv.h>
24 
25 #include <NetdClient.h>
26 #include <android-base/unique_fd.h>
27 #include <gmock/gmock-matchers.h>
28 #include <gtest/gtest.h>
29 #include <netdutils/NetNativeTestBase.h>
30 
31 constexpr char GOOGLE_SERVER_IP[] = "8.8.8.8";
32 constexpr char GOOGLE_SERVER_IPV6[] = "2001:4860:4860::8888";
33 static const int TIMEOUT_MS = 10000;
34 constexpr int MAXPACKET = (8 * 1024);
35 constexpr unsigned int MINIMAL_NET_ID = 100;
36 
37 using android::base::unique_fd;
38 
39 // TODO: Move to DoHFFITest class.
40 std::mutex m;
41 std::condition_variable cv;
42 unsigned int dnsNetId;
43 
44 namespace {
45 
haveIpv4()46 bool haveIpv4() {
47     const sockaddr_in server = {
48             .sin_family = AF_INET,
49             .sin_addr.s_addr = __constant_htonl(0x08080808L)  // 8.8.8.8
50     };
51     unique_fd sock(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP));
52     if (sock == -1) {
53         PLOG(INFO) << "Failed to create socket";
54         return false;
55     }
56     return connect(sock, reinterpret_cast<const sockaddr*>(&server), sizeof(server)) == 0;
57 }
58 
haveIpv6()59 bool haveIpv6() {
60     const sockaddr_in6 server = {
61             .sin6_family = AF_INET6,
62             .sin6_addr.s6_addr = {0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}  // 2000::
63     };
64     unique_fd sock(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP));
65     if (sock == -1) {
66         PLOG(INFO) << "Failed to create socket";
67         return false;
68     }
69     return connect(sock, reinterpret_cast<const sockaddr*>(&server), sizeof(server)) == 0;
70 }
71 
72 }  // namespace
73 
74 class DoHFFITest : public NetNativeTestBase {
75   public:
SetUpTestSuite()76     static void SetUpTestSuite() { doh_init_logger(DOH_LOG_LEVEL_TRACE); }
77 };
78 
TEST_F(DoHFFITest,SmokeTest)79 TEST_F(DoHFFITest, SmokeTest) {
80     getNetworkForDns(&dnsNetId);
81     ASSERT_GE(dnsNetId, MINIMAL_NET_ID) << "No available networks";
82     LOG(INFO) << "dnsNetId: " << dnsNetId;
83 
84     const bool have_ipv4 = haveIpv4();
85     const bool have_ipv6 = haveIpv6();
86     ASSERT_TRUE(have_ipv4 | have_ipv6) << "No connectivity on network " << dnsNetId;
87 
88     const static char* server_ip = have_ipv6 ? GOOGLE_SERVER_IPV6 : GOOGLE_SERVER_IP;
89     auto validation_cb = [](uint32_t netId, bool success, const char* ip_addr, const char* host) {
90         EXPECT_EQ(netId, dnsNetId);
91         EXPECT_TRUE(success);
92         EXPECT_STREQ(ip_addr, server_ip);
93         EXPECT_STREQ(host, "");
94         cv.notify_one();
95     };
96 
97     auto tag_socket_cb = [](int32_t sock) { EXPECT_GE(sock, 0); };
98 
99     DohDispatcher* doh = doh_dispatcher_new(validation_cb, tag_socket_cb);
100     EXPECT_TRUE(doh != nullptr);
101 
102     const FeatureFlags flags = {
103             .probe_timeout_ms = TIMEOUT_MS,
104             .idle_timeout_ms = TIMEOUT_MS,
105             .use_session_resumption = true,
106             .enable_early_data = true,
107     };
108 
109     // sk_mark doesn't matter here because this test doesn't have permission to set sk_mark.
110     // The DNS packet would be sent via default network.
111     EXPECT_EQ(doh_net_new(doh, dnsNetId, "https://dns.google/dns-query", /* domain */ "", server_ip,
112                           /* sk_mark */ 0, /* cert_path */ "", &flags,
113                           /* NetworkType::NT_WIFI */ 3,
114                           /* PrivateDnsMode::STRICT */ 2),
115               0);
116     {
117         std::unique_lock<std::mutex> lk(m);
118         EXPECT_EQ(cv.wait_for(lk, std::chrono::milliseconds(TIMEOUT_MS)),
119                   std::cv_status::no_timeout);
120     }
121 
122     std::vector<uint8_t> buf(MAXPACKET, 0);
123     ssize_t len = res_mkquery(ns_o_query, "www.example.com", ns_c_in, ns_t_aaaa, nullptr, 0,
124                               nullptr, buf.data(), MAXPACKET);
125     uint8_t answer[8192];
126 
127     len = doh_query(doh, dnsNetId, buf.data(), len, answer, sizeof answer, TIMEOUT_MS);
128     EXPECT_GT(len, 0);
129     doh_net_delete(doh, dnsNetId);
130     doh_dispatcher_delete(doh);
131 }
132