1 // Copyright (C) 2010-2020 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #include <config.h>
8 
9 #include <functional>
10 #include <ios>
11 #include <fstream>
12 #include <sstream>
13 #include <string>
14 #include <vector>
15 
16 #include <gtest/gtest.h>
17 
18 #include <dns/masterload.h>
19 #include <dns/name.h>
20 #include <dns/rdata.h>
21 #include <dns/rrclass.h>
22 #include <dns/rrset.h>
23 
24 using namespace std;
25 using namespace isc::dns;
26 namespace ph = std::placeholders;
27 
28 namespace {
29 // A callback functor for masterLoad() commonly used for the following tests.
30 class TestCallback : public unary_function<ConstRRsetPtr, void> {
31 public:
TestCallback(vector<ConstRRsetPtr> & rrsets)32     TestCallback(vector<ConstRRsetPtr>& rrsets) : rrsets_(rrsets) {}
operator ()(ConstRRsetPtr rrset)33     void operator()(ConstRRsetPtr rrset) {
34         rrsets_.push_back(rrset);
35     }
36 private:
37     vector<ConstRRsetPtr>& rrsets_;
38 };
39 
40 // A function version of TestCallback.
41 void
testCallback(ConstRRsetPtr rrset,vector<ConstRRsetPtr> * rrsets)42 testCallback(ConstRRsetPtr rrset, vector<ConstRRsetPtr>* rrsets) {
43     rrsets->push_back(rrset);
44 }
45 
46 class MasterLoadTest : public ::testing::Test {
47 protected:
MasterLoadTest()48     MasterLoadTest() : origin("example.com"), zclass(RRClass::IN()),
49                    callback(results) {}
50 public:
rrsetCallback(ConstRRsetPtr rrset)51     void rrsetCallback(ConstRRsetPtr rrset) {
52         results.push_back(rrset);
53     }
54 protected:
55     Name origin;
56     RRClass zclass;
57     stringstream rr_stream;
58     vector<ConstRRsetPtr> results;
59     TestCallback callback;
60 };
61 
62 // Commonly used test RRs
63 const char* const txt_rr = "example.com. 3600 IN TXT \"test data\"\n";
64 const char* const a_rr1 = "www.example.com. 60 IN A 192.0.2.1\n";
65 const char* const a_rr2 = "www.example.com. 60 IN A 192.0.2.2\n";
66 const char* const a_rr3 = "ftp.example.com. 60 IN A 192.0.2.3\n";
67 // multi-field RR case
68 const char* const soa_rr = "example.com. 7200 IN SOA . . 0 0 0 0 0\n";
69 // A couple of RRSIGs, different type covered
70 const char* const rrsig_rr1 =
71     "www.example.com. 60 IN RRSIG A 5 3 3600 20000101000000 20000201000000 "
72     "12345 example.com. FAKEFAKEFAKE\n";
73 const char* const rrsig_rr2 =
74     "www.example.com. 60 IN RRSIG AAAA 5 3 3600 20000101000000 20000201000000 "
75     "12345 example.com. FAKEFAKEFAKE\n";
76 
77 // Commonly used for some tests to check the constructed RR content.
78 const char* const dnskey_rdata =
79     "256 3 7 AwEAAaetidLzsKWUt4swWR8yu0wPHPiUi8LUsAD0QPWU+wzt89epO6tH "
80     "zkMBVDkC7qphQO2hTY4hHn9npWFRw5BYubE=\n";
81 
TEST_F(MasterLoadTest,loadRRs)82 TEST_F(MasterLoadTest, loadRRs) {
83     // a simple case: loading 3 RRs, each consists of a single RRset.
84     rr_stream << txt_rr << a_rr1 << soa_rr;
85     masterLoad(rr_stream, origin, zclass, callback);
86     ASSERT_EQ(3, results.size());
87     EXPECT_EQ(txt_rr, results[0]->toText());
88     EXPECT_EQ(a_rr1, results[1]->toText());
89     EXPECT_EQ(soa_rr, results[2]->toText());
90 }
91 
TEST_F(MasterLoadTest,loadWithFunctionCallback)92 TEST_F(MasterLoadTest, loadWithFunctionCallback) {
93     // The same test as loadRRs but using a normal function (not a functor
94     // object)
95     rr_stream << txt_rr << a_rr1 << soa_rr;
96     masterLoad(rr_stream, origin, zclass,
97                std::bind(&testCallback, ph::_1, &results));
98     ASSERT_EQ(3, results.size());
99     EXPECT_EQ(txt_rr, results[0]->toText());
100     EXPECT_EQ(a_rr1, results[1]->toText());
101     EXPECT_EQ(soa_rr, results[2]->toText());
102 }
103 
TEST_F(MasterLoadTest,loadWithMemFunctionCallback)104 TEST_F(MasterLoadTest, loadWithMemFunctionCallback) {
105     // The same test as loadRRs but using a class member function (with a
106     // help of std.bind)
107     rr_stream << txt_rr << a_rr1 << soa_rr;
108     masterLoad(rr_stream, origin, zclass,
109                std::bind(&MasterLoadTest::rrsetCallback, this, ph::_1));
110     ASSERT_EQ(3, results.size());
111     EXPECT_EQ(txt_rr, results[0]->toText());
112     EXPECT_EQ(a_rr1, results[1]->toText());
113     EXPECT_EQ(soa_rr, results[2]->toText());
114 }
115 
TEST_F(MasterLoadTest,loadComments)116 TEST_F(MasterLoadTest, loadComments) {
117     rr_stream << ";; comment line, should be skipped\n"
118               << "\n"           // blank line (should be skipped)
119               << txt_rr;
120     masterLoad(rr_stream, origin, zclass, callback);
121     ASSERT_EQ(1, results.size());
122     EXPECT_EQ(txt_rr, results[0]->toText());
123 }
124 
TEST_F(MasterLoadTest,loadRRset)125 TEST_F(MasterLoadTest, loadRRset) {
126     // load an RRset containing two RRs
127     rr_stream << a_rr1 << a_rr2;
128     masterLoad(rr_stream, origin, zclass, callback);
129     ASSERT_EQ(1, results.size());
130     EXPECT_EQ(string(a_rr1) + string(a_rr2), results[0]->toText());
131 }
132 
TEST_F(MasterLoadTest,loadRRsetsOfSameType)133 TEST_F(MasterLoadTest, loadRRsetsOfSameType) {
134     // load two RRsets with the same RR type and different owner names.
135     // the loader must distinguish them as separate RRsets.
136     rr_stream << a_rr1 << a_rr3;
137     masterLoad(rr_stream, origin, zclass, callback);
138     ASSERT_EQ(2, results.size());
139     EXPECT_EQ(a_rr1, results[0]->toText());
140     EXPECT_EQ(a_rr3, results[1]->toText());
141 }
142 
TEST_F(MasterLoadTest,loadRRsetsInterleaved)143 TEST_F(MasterLoadTest, loadRRsetsInterleaved) {
144     // two RRs that belongs to the same RRset (rr1 and rr2) are interleaved
145     // by another.  This is an unexpected case for this loader, but it's
146     // not considered an error.  The loader will simply treat them separate
147     // RRsets.
148     rr_stream << a_rr1 << a_rr3 << a_rr2;
149     masterLoad(rr_stream, origin, zclass, callback);
150     ASSERT_EQ(3, results.size());
151     EXPECT_EQ(a_rr1, results[0]->toText());
152     EXPECT_EQ(a_rr3, results[1]->toText());
153     EXPECT_EQ(a_rr2, results[2]->toText());
154 }
155 
TEST_F(MasterLoadTest,loadRRsigs)156 TEST_F(MasterLoadTest, loadRRsigs) {
157     // RRSIGs of different types covered should be separated
158     rr_stream << rrsig_rr1 << rrsig_rr2;
159     masterLoad(rr_stream, origin, zclass, callback);
160     EXPECT_EQ(2, results.size());
161 }
162 
163 // This test was disabled by #2387, because the test data has trailing
164 // comments and it (eventually) uses the string RDATA constructor which
165 // doesn't support them. This test should be fixed and re-enabled by
166 // #2381, or deleted.
TEST_F(MasterLoadTest,DISABLED_loadRRWithComment)167 TEST_F(MasterLoadTest, DISABLED_loadRRWithComment) {
168     // Comment at the end of line should be ignored and the RR should be
169     // accepted.
170     rr_stream << "example.com. 3600 IN DNSKEY\t256 3 7 "
171         "AwEAAaetidLzsKWUt4swWR8yu0wPHPiUi8LUsAD0QPWU+wzt89epO6tH "
172         "zkMBVDkC7qphQO2hTY4hHn9npWFRw5BYubE=  ; key id = 40430\n";
173     masterLoad(rr_stream, origin, zclass, callback);
174     ASSERT_EQ(1, results.size());
175     EXPECT_EQ(0, results[0]->getRdataIterator()->getCurrent().compare(
176                   *rdata::createRdata(RRType::DNSKEY(), zclass,
177                                       dnskey_rdata)));
178 }
179 
180 // This test was disabled by #2387, because the test data has trailing
181 // comments and it (eventually) uses the string RDATA constructor which
182 // doesn't support them. This test should be fixed and re-enabled by
183 // #2381, or deleted.
TEST_F(MasterLoadTest,DISABLED_loadRRWithCommentNoSpace)184 TEST_F(MasterLoadTest, DISABLED_loadRRWithCommentNoSpace) {
185     // Similar to the previous one, but there's no space before comments.
186     // It should still work.
187     rr_stream << "example.com. 3600 IN DNSKEY\t256 3 7 "
188         "AwEAAaetidLzsKWUt4swWR8yu0wPHPiUi8LUsAD0QPWU+wzt89epO6tH "
189         "zkMBVDkC7qphQO2hTY4hHn9npWFRw5BYubE=; key id = 40430\n";
190     masterLoad(rr_stream, origin, zclass, callback);
191     ASSERT_EQ(1, results.size());
192     EXPECT_EQ(0, results[0]->getRdataIterator()->getCurrent().compare(
193                   *rdata::createRdata(RRType::DNSKEY(), zclass,
194                                       dnskey_rdata)));
195 }
196 
197 // This test was disabled by #2387, because the test data has trailing
198 // comments and it (eventually) uses the string RDATA constructor which
199 // doesn't support them. This test should be fixed and re-enabled by
200 // #2381, or deleted.
TEST_F(MasterLoadTest,DISABLED_loadRRWithCommentEmptyComment)201 TEST_F(MasterLoadTest, DISABLED_loadRRWithCommentEmptyComment) {
202     // Similar to the previous one, but there's no data after the ;
203     // It should still work.
204     rr_stream << "example.com. 3600 IN DNSKEY\t256 3 7 "
205         "AwEAAaetidLzsKWUt4swWR8yu0wPHPiUi8LUsAD0QPWU+wzt89epO6tH "
206         "zkMBVDkC7qphQO2hTY4hHn9npWFRw5BYubE= ;\n";
207     masterLoad(rr_stream, origin, zclass, callback);
208     ASSERT_EQ(1, results.size());
209     EXPECT_EQ(0, results[0]->getRdataIterator()->getCurrent().compare(
210                   *rdata::createRdata(RRType::DNSKEY(), zclass,
211                                       dnskey_rdata)));
212 }
213 
214 // This test was disabled by #2387, because the test data has trailing
215 // comments and it (eventually) uses the string RDATA constructor which
216 // doesn't support them. This test should be fixed and re-enabled by
217 // #2381, or deleted.
TEST_F(MasterLoadTest,DISABLED_loadRRWithCommentEmptyCommentNoSpace)218 TEST_F(MasterLoadTest, DISABLED_loadRRWithCommentEmptyCommentNoSpace) {
219     // Similar to the previous one, but there's no space before or after ;
220     // It should still work.
221     rr_stream << "example.com. 3600 IN DNSKEY\t256 3 7 "
222         "AwEAAaetidLzsKWUt4swWR8yu0wPHPiUi8LUsAD0QPWU+wzt89epO6tH "
223         "zkMBVDkC7qphQO2hTY4hHn9npWFRw5BYubE=;\n";
224     masterLoad(rr_stream, origin, zclass, callback);
225     ASSERT_EQ(1, results.size());
226     EXPECT_EQ(0, results[0]->getRdataIterator()->getCurrent().compare(
227                   *rdata::createRdata(RRType::DNSKEY(), zclass,
228                                       dnskey_rdata)));
229 }
230 
TEST_F(MasterLoadTest,loadRRWithEOLWhitespace)231 TEST_F(MasterLoadTest, loadRRWithEOLWhitespace) {
232     // Test with whitespace after rdata
233     // It should still work.
234     rr_stream << "example.com. 3600 IN NSEC3PARAM 1 0 1 beef \n";
235     masterLoad(rr_stream, origin, zclass, callback);
236     ASSERT_EQ(1, results.size());
237     EXPECT_EQ(0, results[0]->getRdataIterator()->getCurrent().compare(
238                   *rdata::createRdata(RRType::NSEC3PARAM(), zclass,
239                                       "1 0 1 beef")));
240 }
241 
TEST_F(MasterLoadTest,loadRRWithEOLWhitespaceTab)242 TEST_F(MasterLoadTest, loadRRWithEOLWhitespaceTab) {
243     // Similar to the previous one, tab instead of space.
244     // It should still work.
245     rr_stream << "example.com. 3600 IN NSEC3PARAM 1 0 1 beef\t\n";
246     masterLoad(rr_stream, origin, zclass, callback);
247     ASSERT_EQ(1, results.size());
248     EXPECT_EQ(0, results[0]->getRdataIterator()->getCurrent().compare(
249                   *rdata::createRdata(RRType::NSEC3PARAM(), zclass,
250                                       "1 0 1 beef")));
251 }
252 
TEST_F(MasterLoadTest,loadRRNoComment)253 TEST_F(MasterLoadTest, loadRRNoComment) {
254     // A semicolon in a character-string shouldn't confuse the parser.
255     rr_stream << "example.com. 3600 IN TXT \"aaa;bbb\"\n";
256     masterLoad(rr_stream, origin, zclass, callback);
257     EXPECT_EQ(1, results.size());
258     EXPECT_EQ(0, results[0]->getRdataIterator()->getCurrent().compare(
259                   *rdata::createRdata(RRType::TXT(), zclass,
260                                       "\"aaa;bbb\"")));
261 }
262 
TEST_F(MasterLoadTest,nonAbsoluteOwner)263 TEST_F(MasterLoadTest, nonAbsoluteOwner) {
264     // If the owner name is not absolute, the zone origin is assumed to be
265     // its origin.
266     rr_stream << "example.com 3600 IN A 192.0.2.1";
267     masterLoad(rr_stream, origin, zclass, callback);
268     EXPECT_EQ(1, results.size());
269     EXPECT_EQ(results[0]->getName(), Name("example.com.example.com"));
270 }
271 
TEST_F(MasterLoadTest,loadRREmptyAndComment)272 TEST_F(MasterLoadTest, loadRREmptyAndComment) {
273     // There's no RDATA (invalid in this case) but a comment.  This position
274     // shouldn't cause any disruption and should be treated as a normal error.
275     rr_stream << "example.com. 3600 IN A ;\n";
276     EXPECT_THROW(masterLoad(rr_stream, origin, zclass, callback),
277                  MasterLoadError);
278 }
279 
TEST_F(MasterLoadTest,loadWithNoEOF)280 TEST_F(MasterLoadTest, loadWithNoEOF) {
281     // the input stream doesn't end with a new line (and the following blank
282     // line).  It should be accepted.
283     string rr_string(a_rr1);
284     rr_string.erase(rr_string.end() - 1);
285     rr_stream << rr_string;
286     masterLoad(rr_stream, origin, zclass, callback);
287     ASSERT_EQ(1, results.size());
288     EXPECT_EQ(a_rr1, results[0]->toText());
289 }
290 
TEST_F(MasterLoadTest,loadEmpty)291 TEST_F(MasterLoadTest, loadEmpty) {
292     // an unusual case: empty input.  load must succeed with an empty result.
293     masterLoad(rr_stream, origin, zclass, callback);
294     EXPECT_EQ(0, results.size());
295 }
296 
TEST_F(MasterLoadTest,loadWithBeginningSpace)297 TEST_F(MasterLoadTest, loadWithBeginningSpace) {
298     rr_stream << " " << a_rr1;
299     EXPECT_THROW(masterLoad(rr_stream, origin, zclass, callback),
300                  MasterLoadError);
301 }
302 
TEST_F(MasterLoadTest,loadWithBeginningTab)303 TEST_F(MasterLoadTest, loadWithBeginningTab) {
304     rr_stream << "\t" << a_rr1;
305     EXPECT_THROW(masterLoad(rr_stream, origin, zclass, callback),
306                  MasterLoadError);
307 }
308 
TEST_F(MasterLoadTest,loadInvalidRRClass)309 TEST_F(MasterLoadTest, loadInvalidRRClass) {
310     rr_stream << "example.com. 3600 CH TXT \"test text\"";
311     EXPECT_THROW(masterLoad(rr_stream, origin, zclass, callback),
312                  MasterLoadError);
313 }
314 
TEST_F(MasterLoadTest,loadOutOfZoneData)315 TEST_F(MasterLoadTest, loadOutOfZoneData) {
316     rr_stream << "example.org. 3600 IN A 192.0.2.255";
317     EXPECT_THROW(masterLoad(rr_stream, origin, zclass, callback),
318                  MasterLoadError);
319 }
320 
TEST_F(MasterLoadTest,loadNonAtopSOA)321 TEST_F(MasterLoadTest, loadNonAtopSOA) {
322     // SOA's owner name must be zone's origin.
323     rr_stream << "soa.example.com. 3600 IN SOA . . 0 0 0 0 0";
324     EXPECT_THROW(masterLoad(rr_stream, origin, zclass, callback),
325                  MasterLoadError);
326 }
327 
328 // Load TTL with units
TEST_F(MasterLoadTest,loadUnitTTL)329 TEST_F(MasterLoadTest, loadUnitTTL) {
330     stringstream rr_stream2("example.com. 1D IN A 192.0.2.1");
331     masterLoad(rr_stream2, origin, zclass, callback);
332     EXPECT_EQ(1, results.size());
333     EXPECT_EQ(0, results[0]->getRdataIterator()->getCurrent().compare(
334                   *rdata::createRdata(RRType::A(), zclass, "192.0.2.1")));
335 }
336 
TEST_F(MasterLoadTest,loadBadRRText)337 TEST_F(MasterLoadTest, loadBadRRText) {
338     rr_stream << "example..com. 3600 IN A 192.0.2.1"; // bad owner name
339     EXPECT_THROW(masterLoad(rr_stream, origin, zclass, callback),
340                  MasterLoadError);
341 
342     // bad RR class text
343     stringstream rr_stream3("example.com. 3600 BAD A 192.0.2.1");
344     EXPECT_THROW(masterLoad(rr_stream3, origin, zclass, callback),
345                  MasterLoadError);
346 
347     // bad RR type text
348     stringstream rr_stream4("example.com. 3600 IN BAD 192.0.2.1");
349     EXPECT_THROW(masterLoad(rr_stream4, origin, zclass, callback),
350                  MasterLoadError);
351 
352     // bad RDATA text
353     stringstream rr_stream5("example.com. 3600 IN A 2001:db8::1");
354     EXPECT_THROW(masterLoad(rr_stream5, origin, zclass, callback),
355                  MasterLoadError);
356 
357     // incomplete RR text
358     stringstream rr_stream6("example.com. 3600 IN A");
359     EXPECT_THROW(masterLoad(rr_stream6, origin, zclass, callback),
360                  MasterLoadError);
361 }
362 
363 // This is a helper callback to test the case the input stream becomes bad
364 // in the middle of processing.
365 class StreamInvalidator : public unary_function<ConstRRsetPtr, void> {
366 public:
StreamInvalidator(stringstream & ss)367     StreamInvalidator(stringstream& ss) : ss_(ss) {}
operator ()(ConstRRsetPtr)368     void operator()(ConstRRsetPtr) {
369         ss_.setstate(ios::badbit);
370     }
371 private:
372     stringstream& ss_;
373 };
374 
TEST_F(MasterLoadTest,loadBadStream)375 TEST_F(MasterLoadTest, loadBadStream) {
376     rr_stream << txt_rr << a_rr1;
377     StreamInvalidator invalidator(rr_stream);
378     EXPECT_THROW(masterLoad(rr_stream, origin, zclass, invalidator),
379                  MasterLoadError);
380 }
381 
TEST_F(MasterLoadTest,loadFromFile)382 TEST_F(MasterLoadTest, loadFromFile) {
383     // The main parser is shared with the stream version, so we simply test
384     // file I/O specific parts.
385     masterLoad(TEST_DATA_SRCDIR "/masterload.txt", origin, zclass, callback);
386     ASSERT_EQ(2, results.size());
387     EXPECT_EQ(txt_rr, results[0]->toText());
388     EXPECT_EQ(string(a_rr1) + string(a_rr2), results[1]->toText());
389 
390     // NULL file name.  Should result in exception.
391     EXPECT_THROW(masterLoad(NULL, origin, zclass, callback), MasterLoadError);
392 
393     // Non existent file name.  Ditto.
394     EXPECT_THROW(masterLoad(TEST_DATA_BUILDDIR "/nonexistent.txt", origin,
395                             zclass, callback), MasterLoadError);
396 }
397 } // end namespace
398