• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 <regex>
18 #include <thread>
19 
20 #include <android-base/strings.h>
21 #include <android-base/test_utils.h>
22 #include <gtest/gtest.h>
23 
24 #include "DnsQueryLog.h"
25 #include "tests/resolv_test_base.h"
26 
27 using namespace std::chrono_literals;
28 
29 namespace android::net {
30 
31 namespace {
32 
33 // Dump the log to STDOUT and capture it.
captureDumpOutput(const DnsQueryLog & queryLog)34 std::string captureDumpOutput(const DnsQueryLog& queryLog) {
35     netdutils::DumpWriter dw(STDOUT_FILENO);
36     CapturedStdout captured;
37     queryLog.dump(dw);
38     return captured.str();
39 }
40 
41 // A simple check for the dump result by checking the netIds one by one.
verifyDumpOutput(const std::string & dumpLog,const std::vector<int> & expectedNetIds)42 void verifyDumpOutput(const std::string& dumpLog, const std::vector<int>& expectedNetIds) {
43     // Capture three matches: netId, hostname, and answer (empty allowed).
44     static const std::regex pattern(
45             R"(netId=(\d+).* hostname=([\w\*]+) answer=\[([\w:,\.\*\s]*)\])");
46 
47     std::string str(dumpLog);
48     std::smatch sm;
49     for (const auto& netId : expectedNetIds) {
50         SCOPED_TRACE(netId);
51         EXPECT_TRUE(std::regex_search(str, sm, pattern));
52         EXPECT_EQ(sm[1], std::to_string(netId));
53         str = sm.suffix();
54     }
55 
56     // Ensure the dumpLog is exactly as expected.
57     EXPECT_FALSE(std::regex_search(str, sm, pattern));
58 }
59 
60 }  // namespace
61 
62 class DnsQueryLogTest : public ResolvTestBase {
63   protected:
64     const std::vector<std::string> serversV4 = {"127.0.0.1", "1.2.3.4"};
65     const std::vector<std::string> serversV4V6 = {"127.0.0.1", "1.2.3.4", "2001:db8::1",
66                                                   "fe80:1::2%testnet"};
67 };
68 
TEST_F(DnsQueryLogTest,Push)69 TEST_F(DnsQueryLogTest, Push) {
70     std::vector<DnsQueryLog::Record> records = {
71             DnsQueryLog::Record(30, 1000, 1000, "example.com", serversV4, 10),
72             DnsQueryLog::Record(31, 1000, 1000, "", serversV4, 10),      // Empty hostname.
73             DnsQueryLog::Record(32, 1000, 1000, "example.com", {}, 10),  // No answer.
74             DnsQueryLog::Record(33, 1000, 1000, "example.com", serversV4V6, 10),
75     };
76     DnsQueryLog queryLog;
77     for (auto& r : records) {
78         queryLog.push(std::move(r));
79     }
80 
81     std::string output = captureDumpOutput(queryLog);
82     verifyDumpOutput(output, {30, 31, 32, 33});
83 }
84 
TEST_F(DnsQueryLogTest,PushStressTest)85 TEST_F(DnsQueryLogTest, PushStressTest) {
86     const int threadNum = 100;
87     const int pushNum = 1000;
88     const size_t size = 500;
89     DnsQueryLog queryLog(size);
90     std::vector<std::thread> threads(threadNum);
91 
92     // Launch 'threadNum' threads to push the same queryLog 'pushNum' times.
93     for (auto& thread : threads) {
94         thread = std::thread([&]() {
95             for (int i = 0; i < pushNum; i++) {
96                 DnsQueryLog::Record record(30, 1000, 1000, "www.example.com", serversV4, 10);
97                 queryLog.push(std::move(record));
98             }
99         });
100     }
101     for (auto& thread : threads) {
102         thread.join();
103     }
104 
105     // Verify there are exact 'size' records in queryLog.
106     std::string output = captureDumpOutput(queryLog);
107     verifyDumpOutput(output, std::vector(size, 30));
108 }
109 
TEST_F(DnsQueryLogTest,ZeroSize)110 TEST_F(DnsQueryLogTest, ZeroSize) {
111     const size_t size = 0;
112     DnsQueryLog::Record r1(30, 1000, 1000, "www.example1.com", serversV4V6, 10);
113     DnsQueryLog::Record r2(31, 1000, 1000, "www.example2.com", serversV4V6, 10);
114     DnsQueryLog::Record r3(32, 1000, 1000, "www.example3.com", serversV4V6, 10);
115 
116     DnsQueryLog queryLog(size);
117     queryLog.push(std::move(r1));
118     queryLog.push(std::move(r2));
119     queryLog.push(std::move(r3));
120 
121     std::string output = captureDumpOutput(queryLog);
122     verifyDumpOutput(output, {});
123 }
124 
TEST_F(DnsQueryLogTest,CapacityFull)125 TEST_F(DnsQueryLogTest, CapacityFull) {
126     const size_t size = 3;
127     DnsQueryLog::Record r1(30, 1000, 1000, "www.example1.com", serversV4V6, 10);
128     DnsQueryLog::Record r2(31, 1000, 1000, "www.example2.com", serversV4V6, 10);
129     DnsQueryLog::Record r3(32, 1000, 1000, "www.example3.com", serversV4V6, 10);
130     DnsQueryLog::Record r4(33, 1000, 1000, "www.example4.com", serversV4V6, 10);
131     const std::vector<int> expectedNetIds = {31, 32, 33};
132 
133     DnsQueryLog queryLog(size);
134     queryLog.push(std::move(r1));
135     queryLog.push(std::move(r2));
136     queryLog.push(std::move(r3));
137     queryLog.push(std::move(r4));
138 
139     std::string output = captureDumpOutput(queryLog);
140     verifyDumpOutput(output, expectedNetIds);
141 }
142 
TEST_F(DnsQueryLogTest,ValidityTime)143 TEST_F(DnsQueryLogTest, ValidityTime) {
144     DnsQueryLog::Record r1(30, 1000, 1000, "www.example.com", serversV4, 10);
145     DnsQueryLog queryLog(3, 100ms);
146     queryLog.push(std::move(r1));
147 
148     // Dump the output and verify the correctness by checking netId.
149     std::string output = captureDumpOutput(queryLog);
150     verifyDumpOutput(output, {30});
151 
152     std::this_thread::sleep_for(150ms);
153 
154     // The record is expired thus not shown in the output.
155     output = captureDumpOutput(queryLog);
156     verifyDumpOutput(output, {});
157 
158     // Push another record to ensure it still works.
159     DnsQueryLog::Record r2(31, 1000, 1000, "example.com", serversV4V6, 10);
160     queryLog.push(std::move(r2));
161     output = captureDumpOutput(queryLog);
162     verifyDumpOutput(output, {31});
163 }
164 
165 }  // namespace android::net
166