• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gmock/gmock-matchers.h>
27 #include <gtest/gtest.h>
28 
29 static const char* GOOGLE_SERVER_IP = "8.8.8.8";
30 static const int TIMEOUT_MS = 3000;
31 constexpr int MAXPACKET = (8 * 1024);
32 constexpr unsigned int MINIMAL_NET_ID = 100;
33 
34 std::mutex m;
35 std::condition_variable cv;
36 unsigned int dnsNetId;
37 
TEST(DoHFFITest,SmokeTest)38 TEST(DoHFFITest, SmokeTest) {
39     getNetworkForDns(&dnsNetId);
40     // To ensure that we have a real network.
41     ASSERT_GE(dnsNetId, MINIMAL_NET_ID) << "No available networks";
42 
43     auto validation_cb = [](uint32_t netId, bool success, const char* ip_addr, const char* host) {
44         EXPECT_EQ(netId, dnsNetId);
45         EXPECT_TRUE(success);
46         EXPECT_STREQ(ip_addr, GOOGLE_SERVER_IP);
47         EXPECT_STREQ(host, "");
48         cv.notify_one();
49     };
50 
51     auto tag_socket_cb = [](int32_t sock) { EXPECT_GE(sock, 0); };
52 
53     DohDispatcher* doh = doh_dispatcher_new(validation_cb, tag_socket_cb);
54     EXPECT_TRUE(doh != nullptr);
55 
56     const FeatureFlags flags = {
57             .probe_timeout_ms = TIMEOUT_MS,
58             .idle_timeout_ms = TIMEOUT_MS,
59             .use_session_resumption = true,
60     };
61 
62     // TODO: Use a local server instead of dns.google.
63     // sk_mark doesn't matter here because this test doesn't have permission to set sk_mark.
64     // The DNS packet would be sent via default network.
65     EXPECT_EQ(doh_net_new(doh, dnsNetId, "https://dns.google/dns-query", /* domain */ "",
66                           GOOGLE_SERVER_IP, /* sk_mark */ 0, /* cert_path */ "", &flags),
67               0);
68     {
69         std::unique_lock<std::mutex> lk(m);
70         EXPECT_EQ(cv.wait_for(lk, std::chrono::milliseconds(TIMEOUT_MS)),
71                   std::cv_status::no_timeout);
72     }
73 
74     std::vector<uint8_t> buf(MAXPACKET, 0);
75     ssize_t len = res_mkquery(ns_o_query, "www.example.com", ns_c_in, ns_t_aaaa, nullptr, 0,
76                               nullptr, buf.data(), MAXPACKET);
77     uint8_t answer[8192];
78 
79     len = doh_query(doh, dnsNetId, buf.data(), len, answer, sizeof answer, TIMEOUT_MS);
80     EXPECT_GT(len, 0);
81     doh_net_delete(doh, dnsNetId);
82     doh_dispatcher_delete(doh);
83 }
84