summaryrefslogtreecommitdiffstats
path: root/src/lib/dns/tests/masterload_unittest.cc
blob: e412de011a56601e1aaa6efcb251699951e9f896 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// Copyright (C) 2010-2022 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <functional>
#include <ios>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

#include <gtest/gtest.h>

#include <dns/masterload.h>
#include <dns/name.h>
#include <dns/rdata.h>
#include <dns/rrclass.h>
#include <dns/rrset.h>

using namespace std;
using namespace isc::dns;
namespace ph = std::placeholders;

namespace {
// A callback functor for masterLoad() commonly used for the following tests.
class TestCallback {
public:
    TestCallback(vector<ConstRRsetPtr>& rrsets) : rrsets_(rrsets) {}
    void operator()(ConstRRsetPtr rrset) {
        rrsets_.push_back(rrset);
    }
private:
    vector<ConstRRsetPtr>& rrsets_;
};

// A function version of TestCallback.
void
testCallback(ConstRRsetPtr rrset, vector<ConstRRsetPtr>* rrsets) {
    rrsets->push_back(rrset);
}

class MasterLoadTest : public ::testing::Test {
protected:
    MasterLoadTest() : origin("example.com"), zclass(RRClass::IN()),
                   callback(results) {}
public:
    void rrsetCallback(ConstRRsetPtr rrset) {
        results.push_back(rrset);
    }
protected:
    Name origin;
    RRClass zclass;
    stringstream rr_stream;
    vector<ConstRRsetPtr> results;
    TestCallback callback;
};

// Commonly used test RRs
const char* const txt_rr = "example.com. 3600 IN TXT \"test data\"\n";
const char* const a_rr1 = "www.example.com. 60 IN A 192.0.2.1\n";
const char* const a_rr2 = "www.example.com. 60 IN A 192.0.2.2\n";
const char* const a_rr3 = "ftp.example.com. 60 IN A 192.0.2.3\n";
// multi-field RR case
const char* const soa_rr = "example.com. 7200 IN SOA . . 0 0 0 0 0\n";
// A couple of RRSIGs, different type covered
const char* const rrsig_rr1 =
    "www.example.com. 60 IN RRSIG A 5 3 3600 20000101000000 20000201000000 "
    "12345 example.com. FAKEFAKEFAKE\n";
const char* const rrsig_rr2 =
    "www.example.com. 60 IN RRSIG AAAA 5 3 3600 20000101000000 20000201000000 "
    "12345 example.com. FAKEFAKEFAKE\n";

// Commonly used for some tests to check the constructed RR content.
const char* const dnskey_rdata =
    "256 3 7 AwEAAaetidLzsKWUt4swWR8yu0wPHPiUi8LUsAD0QPWU+wzt89epO6tH "
    "zkMBVDkC7qphQO2hTY4hHn9npWFRw5BYubE=\n";

TEST_F(MasterLoadTest, loadRRs) {
    // a simple case: loading 3 RRs, each consists of a single RRset.
    rr_stream << txt_rr << a_rr1 << soa_rr;
    masterLoad(rr_stream, origin, zclass, callback);
    ASSERT_EQ(3, results.size());
    EXPECT_EQ(txt_rr, results[0]->toText());
    EXPECT_EQ(a_rr1, results[1]->toText());
    EXPECT_EQ(soa_rr, results[2]->toText());
}

TEST_F(MasterLoadTest, loadWithFunctionCallback) {
    // The same test as loadRRs but using a normal function (not a functor
    // object)
    rr_stream << txt_rr << a_rr1 << soa_rr;
    masterLoad(rr_stream, origin, zclass,
               std::bind(&testCallback, ph::_1, &results));
    ASSERT_EQ(3, results.size());
    EXPECT_EQ(txt_rr, results[0]->toText());
    EXPECT_EQ(a_rr1, results[1]->toText());
    EXPECT_EQ(soa_rr, results[2]->toText());
}

TEST_F(MasterLoadTest, loadWithMemFunctionCallback) {
    // The same test as loadRRs but using a class member function (with a
    // help of std.bind)
    rr_stream << txt_rr << a_rr1 << soa_rr;
    masterLoad(rr_stream, origin, zclass,
               std::bind(&MasterLoadTest::rrsetCallback, this, ph::_1));
    ASSERT_EQ(3, results.size());
    EXPECT_EQ(txt_rr, results[0]->toText());
    EXPECT_EQ(a_rr1, results[1]->toText());
    EXPECT_EQ(soa_rr, results[2]->toText());
}

TEST_F(MasterLoadTest, loadComments) {
    rr_stream << ";; comment line, should be skipped\n"
              << "\n"           // blank line (should be skipped)
              << txt_rr;
    masterLoad(rr_stream, origin, zclass, callback);
    ASSERT_EQ(1, results.size());
    EXPECT_EQ(txt_rr, results[0]->toText());
}

TEST_F(MasterLoadTest, loadRRset) {
    // load an RRset containing two RRs
    rr_stream << a_rr1 << a_rr2;
    masterLoad(rr_stream, origin, zclass, callback);
    ASSERT_EQ(1, results.size());
    EXPECT_EQ(string(a_rr1) + string(a_rr2), results[0]->toText());
}

TEST_F(MasterLoadTest, loadRRsetsOfSameType) {
    // load two RRsets with the same RR type and different owner names.
    // the loader must distinguish them as separate RRsets.
    rr_stream << a_rr1 << a_rr3;
    masterLoad(rr_stream, origin, zclass, callback);
    ASSERT_EQ(2, results.size());
    EXPECT_EQ(a_rr1, results[0]->toText());
    EXPECT_EQ(a_rr3, results[1]->toText());
}

TEST_F(MasterLoadTest, loadRRsetsInterleaved) {
    // two RRs that belongs to the same RRset (rr1 and rr2) are interleaved
    // by another.  This is an unexpected case for this loader, but it's
    // not considered an error.  The loader will simply treat them separate
    // RRsets.
    rr_stream << a_rr1 << a_rr3 << a_rr2;
    masterLoad(rr_stream, origin, zclass, callback);
    ASSERT_EQ(3, results.size());
    EXPECT_EQ(a_rr1, results[0]->toText());
    EXPECT_EQ(a_rr3, results[1]->toText());
    EXPECT_EQ(a_rr2, results[2]->toText());
}

TEST_F(MasterLoadTest, loadRRsigs) {
    // RRSIGs of different types covered should be separated
    rr_stream << rrsig_rr1 << rrsig_rr2;
    masterLoad(rr_stream, origin, zclass, callback);
    EXPECT_EQ(2, results.size());
}

// This test was disabled by #2387, because the test data has trailing
// comments and it (eventually) uses the string RDATA constructor which
// doesn't support them. This test should be fixed and re-enabled by
// #2381, or deleted.
TEST_F(MasterLoadTest, DISABLED_loadRRWithComment) {
    // Comment at the end of line should be ignored and the RR should be
    // accepted.
    rr_stream << "example.com. 3600 IN DNSKEY\t256 3 7 "
        "AwEAAaetidLzsKWUt4swWR8yu0wPHPiUi8LUsAD0QPWU+wzt89epO6tH "
        "zkMBVDkC7qphQO2hTY4hHn9npWFRw5BYubE=  ; key id = 40430\n";
    masterLoad(rr_stream, origin, zclass, callback);
    ASSERT_EQ(1, results.size());
    EXPECT_EQ(0, results[0]->getRdataIterator()->getCurrent().compare(
                  *rdata::createRdata(RRType::DNSKEY(), zclass,
                                      dnskey_rdata)));
}

// This test was disabled by #2387, because the test data has trailing
// comments and it (eventually) uses the string RDATA constructor which
// doesn't support them. This test should be fixed and re-enabled by
// #2381, or deleted.
TEST_F(MasterLoadTest, DISABLED_loadRRWithCommentNoSpace) {
    // Similar to the previous one, but there's no space before comments.
    // It should still work.
    rr_stream << "example.com. 3600 IN DNSKEY\t256 3 7 "
        "AwEAAaetidLzsKWUt4swWR8yu0wPHPiUi8LUsAD0QPWU+wzt89epO6tH "
        "zkMBVDkC7qphQO2hTY4hHn9npWFRw5BYubE=; key id = 40430\n";
    masterLoad(rr_stream, origin, zclass, callback);
    ASSERT_EQ(1, results.size());
    EXPECT_EQ(0, results[0]->getRdataIterator()->getCurrent().compare(
                  *rdata::createRdata(RRType::DNSKEY(), zclass,
                                      dnskey_rdata)));
}

// This test was disabled by #2387, because the test data has trailing
// comments and it (eventually) uses the string RDATA constructor which
// doesn't support them. This test should be fixed and re-enabled by
// #2381, or deleted.
TEST_F(MasterLoadTest, DISABLED_loadRRWithCommentEmptyComment) {
    // Similar to the previous one, but there's no data after the ;
    // It should still work.
    rr_stream << "example.com. 3600 IN DNSKEY\t256 3 7 "
        "AwEAAaetidLzsKWUt4swWR8yu0wPHPiUi8LUsAD0QPWU+wzt89epO6tH "
        "zkMBVDkC7qphQO2hTY4hHn9npWFRw5BYubE= ;\n";
    masterLoad(rr_stream, origin, zclass, callback);
    ASSERT_EQ(1, results.size());
    EXPECT_EQ(0, results[0]->getRdataIterator()->getCurrent().compare(
                  *rdata::createRdata(RRType::DNSKEY(), zclass,
                                      dnskey_rdata)));
}

// This test was disabled by #2387, because the test data has trailing
// comments and it (eventually) uses the string RDATA constructor which
// doesn't support them. This test should be fixed and re-enabled by
// #2381, or deleted.
TEST_F(MasterLoadTest, DISABLED_loadRRWithCommentEmptyCommentNoSpace) {
    // Similar to the previous one, but there's no space before or after ;
    // It should still work.
    rr_stream << "example.com. 3600 IN DNSKEY\t256 3 7 "
        "AwEAAaetidLzsKWUt4swWR8yu0wPHPiUi8LUsAD0QPWU+wzt89epO6tH "
        "zkMBVDkC7qphQO2hTY4hHn9npWFRw5BYubE=;\n";
    masterLoad(rr_stream, origin, zclass, callback);
    ASSERT_EQ(1, results.size());
    EXPECT_EQ(0, results[0]->getRdataIterator()->getCurrent().compare(
                  *rdata::createRdata(RRType::DNSKEY(), zclass,
                                      dnskey_rdata)));
}

TEST_F(MasterLoadTest, loadRRWithEOLWhitespace) {
    // Test with whitespace after rdata
    // It should still work.
    rr_stream << "example.com. 3600 IN NSEC3PARAM 1 0 1 beef \n";
    masterLoad(rr_stream, origin, zclass, callback);
    ASSERT_EQ(1, results.size());
    EXPECT_EQ(0, results[0]->getRdataIterator()->getCurrent().compare(
                  *rdata::createRdata(RRType::NSEC3PARAM(), zclass,
                                      "1 0 1 beef")));
}

TEST_F(MasterLoadTest, loadRRWithEOLWhitespaceTab) {
    // Similar to the previous one, tab instead of space.
    // It should still work.
    rr_stream << "example.com. 3600 IN NSEC3PARAM 1 0 1 beef\t\n";
    masterLoad(rr_stream, origin, zclass, callback);
    ASSERT_EQ(1, results.size());
    EXPECT_EQ(0, results[0]->getRdataIterator()->getCurrent().compare(
                  *rdata::createRdata(RRType::NSEC3PARAM(), zclass,
                                      "1 0 1 beef")));
}

TEST_F(MasterLoadTest, loadRRNoComment) {
    // A semicolon in a character-string shouldn't confuse the parser.
    rr_stream << "example.com. 3600 IN TXT \"aaa;bbb\"\n";
    masterLoad(rr_stream, origin, zclass, callback);
    EXPECT_EQ(1, results.size());
    EXPECT_EQ(0, results[0]->getRdataIterator()->getCurrent().compare(
                  *rdata::createRdata(RRType::TXT(), zclass,
                                      "\"aaa;bbb\"")));
}

TEST_F(MasterLoadTest, nonAbsoluteOwner) {
    // If the owner name is not absolute, the zone origin is assumed to be
    // its origin.
    rr_stream << "example.com 3600 IN A 192.0.2.1";
    masterLoad(rr_stream, origin, zclass, callback);
    EXPECT_EQ(1, results.size());
    EXPECT_EQ(results[0]->getName(), Name("example.com.example.com"));
}

TEST_F(MasterLoadTest, loadRREmptyAndComment) {
    // There's no RDATA (invalid in this case) but a comment.  This position
    // shouldn't cause any disruption and should be treated as a normal error.
    rr_stream << "example.com. 3600 IN A ;\n";
    EXPECT_THROW(masterLoad(rr_stream, origin, zclass, callback),
                 MasterLoadError);
}

TEST_F(MasterLoadTest, loadWithNoEOF) {
    // the input stream doesn't end with a new line (and the following blank
    // line).  It should be accepted.
    string rr_string(a_rr1);
    rr_string.erase(rr_string.end() - 1);
    rr_stream << rr_string;
    masterLoad(rr_stream, origin, zclass, callback);
    ASSERT_EQ(1, results.size());
    EXPECT_EQ(a_rr1, results[0]->toText());
}

TEST_F(MasterLoadTest, loadEmpty) {
    // an unusual case: empty input.  load must succeed with an empty result.
    masterLoad(rr_stream, origin, zclass, callback);
    EXPECT_EQ(0, results.size());
}

TEST_F(MasterLoadTest, loadWithBeginningSpace) {
    rr_stream << " " << a_rr1;
    EXPECT_THROW(masterLoad(rr_stream, origin, zclass, callback),
                 MasterLoadError);
}

TEST_F(MasterLoadTest, loadWithBeginningTab) {
    rr_stream << "\t" << a_rr1;
    EXPECT_THROW(masterLoad(rr_stream, origin, zclass, callback),
                 MasterLoadError);
}

TEST_F(MasterLoadTest, loadInvalidRRClass) {
    rr_stream << "example.com. 3600 CH TXT \"test text\"";
    EXPECT_THROW(masterLoad(rr_stream, origin, zclass, callback),
                 MasterLoadError);
}

TEST_F(MasterLoadTest, loadOutOfZoneData) {
    rr_stream << "example.org. 3600 IN A 192.0.2.255";
    EXPECT_THROW(masterLoad(rr_stream, origin, zclass, callback),
                 MasterLoadError);
}

TEST_F(MasterLoadTest, loadNonAtopSOA) {
    // SOA's owner name must be zone's origin.
    rr_stream << "soa.example.com. 3600 IN SOA . . 0 0 0 0 0";
    EXPECT_THROW(masterLoad(rr_stream, origin, zclass, callback),
                 MasterLoadError);
}

// Load TTL with units
TEST_F(MasterLoadTest, loadUnitTTL) {
    stringstream rr_stream2("example.com. 1D IN A 192.0.2.1");
    masterLoad(rr_stream2, origin, zclass, callback);
    EXPECT_EQ(1, results.size());
    EXPECT_EQ(0, results[0]->getRdataIterator()->getCurrent().compare(
                  *rdata::createRdata(RRType::A(), zclass, "192.0.2.1")));
}

TEST_F(MasterLoadTest, loadBadRRText) {
    rr_stream << "example..com. 3600 IN A 192.0.2.1"; // bad owner name
    EXPECT_THROW(masterLoad(rr_stream, origin, zclass, callback),
                 MasterLoadError);

    // bad RR class text
    stringstream rr_stream3("example.com. 3600 BAD A 192.0.2.1");
    EXPECT_THROW(masterLoad(rr_stream3, origin, zclass, callback),
                 MasterLoadError);

    // bad RR type text
    stringstream rr_stream4("example.com. 3600 IN BAD 192.0.2.1");
    EXPECT_THROW(masterLoad(rr_stream4, origin, zclass, callback),
                 MasterLoadError);

    // bad RDATA text
    stringstream rr_stream5("example.com. 3600 IN A 2001:db8::1");
    EXPECT_THROW(masterLoad(rr_stream5, origin, zclass, callback),
                 MasterLoadError);

    // incomplete RR text
    stringstream rr_stream6("example.com. 3600 IN A");
    EXPECT_THROW(masterLoad(rr_stream6, origin, zclass, callback),
                 MasterLoadError);
}

// This is a helper callback to test the case the input stream becomes bad
// in the middle of processing.
class StreamInvalidator {
public:
    StreamInvalidator(stringstream& ss) : ss_(ss) {}
    void operator()(ConstRRsetPtr) {
        ss_.setstate(ios::badbit);
    }
private:
    stringstream& ss_;
};

TEST_F(MasterLoadTest, loadBadStream) {
    rr_stream << txt_rr << a_rr1;
    StreamInvalidator invalidator(rr_stream);
    EXPECT_THROW(masterLoad(rr_stream, origin, zclass, invalidator),
                 MasterLoadError);
}

TEST_F(MasterLoadTest, loadFromFile) {
    // The main parser is shared with the stream version, so we simply test
    // file I/O specific parts.
    masterLoad(TEST_DATA_SRCDIR "/masterload.txt", origin, zclass, callback);
    ASSERT_EQ(2, results.size());
    EXPECT_EQ(txt_rr, results[0]->toText());
    EXPECT_EQ(string(a_rr1) + string(a_rr2), results[1]->toText());

    // NULL file name.  Should result in exception.
    EXPECT_THROW(masterLoad(NULL, origin, zclass, callback), MasterLoadError);

    // Non existent file name.  Ditto.
    EXPECT_THROW(masterLoad(TEST_DATA_BUILDDIR "/nonexistent.txt", origin,
                            zclass, callback), MasterLoadError);
}
} // end namespace