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