master_loader_unittest.cc 53.9 KB
Newer Older
1
// Copyright (C) 2012-2014  Internet Systems Consortium, Inc. ("ISC")
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#include <dns/master_loader_callbacks.h>
#include <dns/master_loader.h>
17
18
#include <dns/rrtype.h>
#include <dns/rrset.h>
19
#include <dns/rrclass.h>
20
#include <dns/rrttl.h>
21
#include <dns/name.h>
22
#include <dns/rdata.h>
23
24

#include <gtest/gtest.h>
25

26
#include <boost/bind.hpp>
27
#include <boost/lexical_cast.hpp>
28
29
#include <boost/scoped_ptr.hpp>

30
31
32
#include <string>
#include <vector>
#include <list>
33
#include <sstream>
34

35
using namespace isc::dns;
36
37
38
using std::vector;
using std::string;
using std::list;
39
using std::stringstream;
40
using std::endl;
41
using boost::lexical_cast;
42

43
namespace {
44
45
46
47
class MasterLoaderTest : public ::testing::Test {
public:
    MasterLoaderTest() :
        callbacks_(boost::bind(&MasterLoaderTest::callback, this,
48
                               &errors_, _1, _2, _3),
49
                   boost::bind(&MasterLoaderTest::callback, this,
50
                               &warnings_, _1, _2, _3))
51
52
    {}

53
54
55
56
57
    void TearDown() {
        // Check there are no more RRs we didn't expect
        EXPECT_TRUE(rrsets_.empty());
    }

58
59
    /// Concatenate file, line, and reason, and add it to either errors
    /// or warnings
60
    void callback(vector<string>* target, const std::string& file, size_t line,
61
                  const std::string& reason)
62
63
    {
        std::stringstream ss;
64
        ss << reason << " [" << file << ":" << line << "]";
65
        target->push_back(ss.str());
66
67
    }

68
69
70
71
72
    void addRRset(const Name& name, const RRClass& rrclass,
                  const RRType& rrtype, const RRTTL& rrttl,
                  const rdata::RdataPtr& data) {
        const RRsetPtr rrset(new BasicRRset(name, rrclass, rrtype, rrttl));
        rrset->addRdata(data);
73
        rrsets_.push_back(rrset);
74
75
    }

76
77
    void setLoader(const char* file, const Name& origin,
                   const RRClass& rrclass, const MasterLoader::Options options)
78
    {
79
        loader_.reset(new MasterLoader(file, origin, rrclass, callbacks_,
80
                                       boost::bind(&MasterLoaderTest::addRRset,
81
82
                                                   this, _1, _2, _3, _4, _5),
                                       options));
83
84
    }

85
86
87
88
89
90
91
92
93
    void setLoader(std::istream& stream, const Name& origin,
                   const RRClass& rrclass, const MasterLoader::Options options)
    {
        loader_.reset(new MasterLoader(stream, origin, rrclass, callbacks_,
                                       boost::bind(&MasterLoaderTest::addRRset,
                                                   this, _1, _2, _3, _4, _5),
                                       options));
    }

94
    static string prepareZone(const string& line, bool include_last) {
95
96
97
        string result;
        result += "example.org. 3600 IN SOA ns1.example.org. "
            "admin.example.org. 1234 3600 1800 2419200 7200\n";
98
99
100
101
102
        result += line;
        if (include_last) {
            result += "\n";
            result += "correct 3600    IN  A 192.0.2.2\n";
        }
103
        return (result);
104
105
106
107
108
109
110
111
    }

    void clear() {
        warnings_.clear();
        errors_.clear();
        rrsets_.clear();
    }

112
113
    // Check the next RR in the ones produced by the loader
    // Other than passed arguments are checked to be the default for the tests
114
115
    void checkRR(const string& name, const RRType& type, const string& data,
                 const RRTTL& rrttl = RRTTL(3600)) {
116
117
118
119
120
        ASSERT_FALSE(rrsets_.empty());
        RRsetPtr current = rrsets_.front();
        rrsets_.pop_front();

        EXPECT_EQ(Name(name), current->getName());
121
        EXPECT_EQ(type, current->getType());
122
        EXPECT_EQ(RRClass::IN(), current->getClass());
123
        EXPECT_EQ(rrttl, current->getTTL());
124
        ASSERT_EQ(1, current->getRdataCount());
125
        EXPECT_EQ(0, isc::dns::rdata::createRdata(type, RRClass::IN(), data)->
126
127
128
                  compare(current->getRdataIterator()->getCurrent()))
            << data << " vs. "
            << current->getRdataIterator()->getCurrent().toText();
129
130
    }

131
132
133
134
135
136
    void checkBasicRRs() {
        checkRR("example.org", RRType::SOA(),
                "ns1.example.org. admin.example.org. "
                "1234 3600 1800 2419200 7200");
        checkRR("example.org", RRType::NS(), "ns1.example.org.");
        checkRR("www.example.org", RRType::A(), "192.0.2.1");
137
        checkRR("www.example.org", RRType::AAAA(), "2001:db8::1");
138
139
    }

140
141
    void checkARR(const string& name) {
        checkRR(name, RRType::A(), "192.0.2.1");
142
143
    }

144
    MasterLoaderCallbacks callbacks_;
145
146
147
148
    boost::scoped_ptr<MasterLoader> loader_;
    vector<string> errors_;
    vector<string> warnings_;
    list<RRsetPtr> rrsets_;
149
150
};

151
152
// Test simple loading. The zone file contains no tricky things, and nothing is
// omitted. No RRset contains more than one RR Also no errors or warnings.
153
TEST_F(MasterLoaderTest, basicLoad) {
154
155
    setLoader(TEST_DATA_SRCDIR "/example.org", Name("example.org."),
              RRClass::IN(), MasterLoader::MANY_ERRORS);
156

157
    EXPECT_FALSE(loader_->loadedSucessfully());
158
159
160
161
162
163

    // The following three should be set to 0 initially in case the loader
    // is constructed from a file name.
    EXPECT_EQ(0, loader_->getSize());
    EXPECT_EQ(0, loader_->getPosition());

164
    loader_->load();
165
    EXPECT_TRUE(loader_->loadedSucessfully());
166
167
168

    EXPECT_TRUE(errors_.empty());
    EXPECT_TRUE(warnings_.empty());
169
170
171

    // Hardcode expected values taken from the test data file, assuming it
    // won't change too often.
172
173
    EXPECT_EQ(550, loader_->getSize());
    EXPECT_EQ(550, loader_->getPosition());
174

175
176
177
178
179
180
181
    checkBasicRRs();
}

// Test the $INCLUDE directive
TEST_F(MasterLoaderTest, include) {
    // Test various cases of include
    const char* includes[] = {
182
183
184
185
186
        "$include",
        "$INCLUDE",
        "$Include",
        "$InCluDe",
        "\"$INCLUDE\"",
187
188
189
190
        NULL
    };
    for (const char** include = includes; *include != NULL; ++include) {
        SCOPED_TRACE(*include);
191
192

        clear();
193
194
        // Prepare input source that has the include and some more data
        // below (to see it returns back to the original source).
195
        const string include_str = string(*include) + " " +
196
            TEST_DATA_SRCDIR + "/example.org\nwww 3600 IN AAAA 2001:db8::1\n";
197
198
199
200
201
202
        stringstream ss(include_str);
        setLoader(ss, Name("example.org."), RRClass::IN(),
                  MasterLoader::MANY_ERRORS);

        loader_->load();
        EXPECT_TRUE(loader_->loadedSucessfully());
203
        EXPECT_TRUE(errors_.empty());
204
205
206
        EXPECT_TRUE(warnings_.empty());

        checkBasicRRs();
207
        checkRR("www.example.org", RRType::AAAA(), "2001:db8::1");
208
    }
209
}
210

211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
TEST_F(MasterLoaderTest, includeAndIncremental) {
    // Check getSize() and getPosition() are adjusted before and after
    // $INCLUDE.
    const string first_rr = "before.example.org. 0 A 192.0.2.1\n";
    const string include_str = "$INCLUDE " TEST_DATA_SRCDIR "/example.org";
    const string zone_data = first_rr + include_str + "\n" +
        "www 3600 IN AAAA 2001:db8::1\n";
    stringstream ss(zone_data);
    setLoader(ss, Name("example.org."), RRClass::IN(), MasterLoader::DEFAULT);

    // On construction, getSize() returns the size of the data (exclude the
    // the file to be included); position is set to 0.
    EXPECT_EQ(zone_data.size(), loader_->getSize());
    EXPECT_EQ(0, loader_->getPosition());

    // Read the first RR.  getSize() doesn't change; position should be
    // at the end of the first line.
    loader_->loadIncremental(1);
    EXPECT_EQ(zone_data.size(), loader_->getSize());
    EXPECT_EQ(first_rr.size(), loader_->getPosition());

232
233
    // Read next 4.  It includes $INCLUDE processing.  Magic number of 550
    // is the size of the test zone file (see above); 507 is the position in
234
235
236
    // the file at the end of 4th RR (due to extra comments it's smaller than
    // the file size).
    loader_->loadIncremental(4);
237
238
    EXPECT_EQ(zone_data.size() + 550, loader_->getSize());
    EXPECT_EQ(first_rr.size() + include_str.size() + 507,
239
240
241
242
243
              loader_->getPosition());

    // Read the last one.  At this point getSize and getPosition return
    // the same value, indicating progress of 100%.
    loader_->loadIncremental(1);
244
245
    EXPECT_EQ(zone_data.size() + 550, loader_->getSize());
    EXPECT_EQ(zone_data.size() + 550, loader_->getPosition());
246
247
248
249
250
251

    // we were not interested in checking RRs in this test.  clear them to
    // not confuse TearDown().
    rrsets_.clear();
}

252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// A commonly used helper to check callback message.
void
checkCallbackMessage(const string& actual_msg, const string& expected_msg,
                     size_t expected_line) {
    // The actual message should begin with the expected message.
    EXPECT_EQ(0, actual_msg.find(expected_msg)) << "actual message: " <<
                                                actual_msg << " expected: " <<
                                                expected_msg;

    // and it should end with "...:<line_num>]"
    const string line_desc = ":" + lexical_cast<string>(expected_line) + "]";
    EXPECT_EQ(actual_msg.size() - line_desc.size(),
              actual_msg.find(line_desc)) << "Expected on line " <<
        expected_line;
}

268
TEST_F(MasterLoaderTest, origin) {
269
    // Various forms of the directive
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
    const char* origins[] = {
        "$origin",
        "$ORIGIN",
        "$Origin",
        "$OrigiN",
        "\"$ORIGIN\"",
        NULL
    };
    for (const char** origin = origins; *origin != NULL; ++origin) {
        SCOPED_TRACE(*origin);

        clear();
        const string directive = *origin;
        const string input =
            "@  1H  IN  A   192.0.2.1\n" +
            directive + " sub.example.org.\n"
286
            "\"www\"    1H  IN  A   192.0.2.1\n" +
287
288
289
290
291
292
293
294
295
296
297
298
            // Relative name in the origin
            directive + " relative\n"
            "@  1H  IN  A   192.0.2.1\n"
            // Origin is _not_ used here (absolute name)
            "noorigin.example.org.  60M IN  A   192.0.2.1\n";
        stringstream ss(input);
        setLoader(ss, Name("example.org."), RRClass::IN(),
                  MasterLoader::MANY_ERRORS);

        loader_->load();
        EXPECT_TRUE(loader_->loadedSucessfully());
        EXPECT_TRUE(errors_.empty());
299
300
301
302
303
        // There's a relative origin in it, we warn about that.
        EXPECT_EQ(1, warnings_.size());
        checkCallbackMessage(warnings_.at(0),
                             "The new origin is relative, did you really mean "
                             "relative.sub.example.org.?", 4);
304
305
306
307
308
309
310
311

        checkARR("example.org");
        checkARR("www.sub.example.org");
        checkARR("relative.sub.example.org");
        checkARR("noorigin.example.org");
    }
}

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
TEST_F(MasterLoaderTest, generate) {
    // Various forms of the directive
    const char* generates[] = {
        "$generate",
        "$GENERATE",
        "$Generate",
        "$GeneratE",
        "\"$GENERATE\"",
        NULL
    };
    for (const char** generate = generates; *generate != NULL; ++generate) {
        SCOPED_TRACE(*generate);

        clear();
        const string directive = *generate;
        const string input =
          "$ORIGIN example.org.\n"
          "before.example.org. 3600 IN A 192.0.2.0\n" +
          directive + " 3-5 host$ A 192.0.2.$\n" +
          "after.example.org. 3600 IN A 192.0.2.255\n";
        stringstream ss(input);
        setLoader(ss, Name("example.org."), RRClass::IN(),
                  MasterLoader::MANY_ERRORS);

        loader_->load();
        EXPECT_TRUE(loader_->loadedSucessfully());
        EXPECT_TRUE(errors_.empty());

340
341
342
        // The "before" and "after" scaffolding below checks that no
        // extra records are added by $GENERATE outside the requested
        // range.
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
        checkRR("before.example.org", RRType::A(), "192.0.2.0");
        checkRR("host3.example.org", RRType::A(), "192.0.2.3");
        checkRR("host4.example.org", RRType::A(), "192.0.2.4");
        checkRR("host5.example.org", RRType::A(), "192.0.2.5");
        checkRR("after.example.org", RRType::A(), "192.0.2.255");
    }
}

TEST_F(MasterLoaderTest, generateRelativeLHS) {
    const string input =
        "$ORIGIN example.org.\n"
        "$GENERATE 1-2 @ 3600 NS ns$.example.org.\n";
    stringstream ss(input);
    setLoader(ss, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);

    loader_->load();
    EXPECT_TRUE(loader_->loadedSucessfully());
    EXPECT_TRUE(errors_.empty());

    checkRR("example.org", RRType::NS(), "ns1.example.org.");
    checkRR("example.org", RRType::NS(), "ns2.example.org.");
}

TEST_F(MasterLoaderTest, generateInFront) {
    // $ is in the front
    const string input =
        "$ORIGIN example.org.\n"
        "$GENERATE 9-10 $host 3600 TXT \"$ pomegranate\"\n";
    stringstream ss(input);
    setLoader(ss, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);

    loader_->load();
    EXPECT_TRUE(loader_->loadedSucessfully());
    EXPECT_TRUE(errors_.empty());

    checkRR("9host.example.org", RRType::TXT(), "9 pomegranate");
    checkRR("10host.example.org", RRType::TXT(), "10 pomegranate");
}

TEST_F(MasterLoaderTest, generateInMiddle) {
    // $ is in the middle
    const string input =
        "$ORIGIN example.org.\n"
        "$GENERATE 9-10 num$-host 3600 TXT \"This is $ pomegranate\"\n";
    stringstream ss(input);
    setLoader(ss, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);

    loader_->load();
    EXPECT_TRUE(loader_->loadedSucessfully());
    EXPECT_TRUE(errors_.empty());

    checkRR("num9-host.example.org", RRType::TXT(), "This is 9 pomegranate");
    checkRR("num10-host.example.org", RRType::TXT(), "This is 10 pomegranate");
}

TEST_F(MasterLoaderTest, generateStripsQuotes) {
    const string input =
        "$ORIGIN example.org.\n"
        "$GENERATE 1-2 @ 3600 MX \"$ mx$.example.org.\"\n";
    stringstream ss(input);
    setLoader(ss, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);

    loader_->load();
    EXPECT_TRUE(loader_->loadedSucessfully());
    EXPECT_TRUE(errors_.empty());

    checkRR("example.org", RRType::MX(), "1 mx1.example.org.");
    checkRR("example.org", RRType::MX(), "2 mx2.example.org.");
}

TEST_F(MasterLoaderTest, generateWithDoublePlaceholder) {
    const string input =
        "$ORIGIN example.org.\n"
        "$GENERATE 9-10 host$ 3600 TXT \"This is $$ pomegranate\"\n";
    stringstream ss(input);
    setLoader(ss, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);

    loader_->load();
    EXPECT_TRUE(loader_->loadedSucessfully());
    EXPECT_TRUE(errors_.empty());

    checkRR("host9.example.org", RRType::TXT(), "This is $ pomegranate");
    checkRR("host10.example.org", RRType::TXT(), "This is $ pomegranate");
}

TEST_F(MasterLoaderTest, generateWithEscape) {
    const string input =
        "$ORIGIN example.org.\n"
        "$GENERATE 9-10 host$ 3600 TXT \"This is \\$\\pomegranate\"\n";
    stringstream ss(input);
    setLoader(ss, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);

    loader_->load();
    EXPECT_TRUE(loader_->loadedSucessfully());
    EXPECT_TRUE(errors_.empty());

    checkRR("host9.example.org", RRType::TXT(), "This is \\$\\pomegranate");
    checkRR("host10.example.org", RRType::TXT(), "This is \\$\\pomegranate");
}

TEST_F(MasterLoaderTest, generateWithParams) {
    const string input =
        "$ORIGIN example.org.\n"
        "$TTL 3600\n"
        "$GENERATE 2-3 host$ A 192.0.2.$\n"
        "$GENERATE 5-6 host$ 3600 A 192.0.2.$\n"
        "$GENERATE 8-9 host$ IN A 192.0.2.$\n"
        "$GENERATE 11-12 host$ IN 3600 A 192.0.2.$\n"
        "$GENERATE 14-15 host$ 3600 IN A 192.0.2.$\n";
    stringstream ss(input);
    setLoader(ss, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);

    loader_->load();
    EXPECT_TRUE(loader_->loadedSucessfully());
    EXPECT_TRUE(errors_.empty());

    checkRR("host2.example.org", RRType::A(), "192.0.2.2");
    checkRR("host3.example.org", RRType::A(), "192.0.2.3");

    checkRR("host5.example.org", RRType::A(), "192.0.2.5");
    checkRR("host6.example.org", RRType::A(), "192.0.2.6");

    checkRR("host8.example.org", RRType::A(), "192.0.2.8");
    checkRR("host9.example.org", RRType::A(), "192.0.2.9");

    checkRR("host11.example.org", RRType::A(), "192.0.2.11");
    checkRR("host12.example.org", RRType::A(), "192.0.2.12");

    checkRR("host14.example.org", RRType::A(), "192.0.2.14");
    checkRR("host15.example.org", RRType::A(), "192.0.2.15");
}

TEST_F(MasterLoaderTest, generateWithStep) {
    const string input =
        "$ORIGIN example.org.\n"
        "$GENERATE 2-9/2 host$ 3600 A 192.0.2.$\n"
        "$GENERATE 12-21/3 host$ 3600 A 192.0.2.$\n"
        "$GENERATE 30-31/1 host$ 3600 A 192.0.2.$\n";
    stringstream ss(input);
    setLoader(ss, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);

    loader_->load();
    EXPECT_TRUE(loader_->loadedSucessfully());
    EXPECT_TRUE(errors_.empty());

    checkRR("host2.example.org", RRType::A(), "192.0.2.2");
    checkRR("host4.example.org", RRType::A(), "192.0.2.4");
    checkRR("host6.example.org", RRType::A(), "192.0.2.6");
    checkRR("host8.example.org", RRType::A(), "192.0.2.8");

    checkRR("host12.example.org", RRType::A(), "192.0.2.12");
    checkRR("host15.example.org", RRType::A(), "192.0.2.15");
    checkRR("host18.example.org", RRType::A(), "192.0.2.18");
    checkRR("host21.example.org", RRType::A(), "192.0.2.21");

    checkRR("host30.example.org", RRType::A(), "192.0.2.30");
    checkRR("host31.example.org", RRType::A(), "192.0.2.31");
}

510
511
512
513
TEST_F(MasterLoaderTest, generateWithModifiers) {
    const string input =
        "$ORIGIN example.org.\n"
        "$TTL 3600\n"
514
515
516

        // Use a positive delta of 1 in the LHS and a negative delta of
        // -1 in the RHS
517
        "$GENERATE 2-9/2 host${1} A 192.0.2.${-1}\n"
518

519
520
        "$GENERATE 10-12 host${0,4} A 192.0.2.$\n"
        "$GENERATE 14-15 host${0,4,d} A 192.0.2.$\n"
521

522
        // Names are case-insensitive, so we use TXT's RDATA to check
523
524
        // case with hex representation.
        "$GENERATE 30-31 host$ TXT \"Value ${0,4,x}\"\n"
525
        "$GENERATE 42-43 host$ TXT \"Value ${0,4,X}\"\n"
526
527

        // Octal does not use any alphabets
528
        "$GENERATE 45-46 host${0,4,o} A 192.0.2.$\n"
529

530
531
532
533
        // Here, the LHS has a trailing dot (which would result in an
        // out-of-zone name), but that should be handled as a relative
        // name.
        "$GENERATE 90-92 ${0,8,n} A 192.0.2.$\n"
534

535
536
537
        // Here, the LHS has no trailing dot, and results in the same
        // number of labels as width=8 above.
        "$GENERATE 94-96 ${0,7,n} A 192.0.2.$\n"
538
539
540
541
542
543

        // Names are case-insensitive, so we use TXT's RDATA to check
        // case with nibble representation.
        "$GENERATE 106-107 host$ TXT \"Value ${0,9,n}\"\n"
        "$GENERATE 109-110 host$ TXT \"Value ${0,9,N}\"\n"

Mukund Sivaraman's avatar
Mukund Sivaraman committed
544
545
        // Junk type will not parse and 'd' is assumed. No error is
        // generated (this is to match BIND 9 behavior).
546
        "$GENERATE 200-201 host${0,4,j} A 192.0.2.$\n";
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
    stringstream ss(input);
    setLoader(ss, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);

    loader_->load();
    EXPECT_TRUE(loader_->loadedSucessfully());
    EXPECT_TRUE(errors_.empty());

    checkRR("host3.example.org", RRType::A(), "192.0.2.1");
    checkRR("host5.example.org", RRType::A(), "192.0.2.3");
    checkRR("host7.example.org", RRType::A(), "192.0.2.5");
    checkRR("host9.example.org", RRType::A(), "192.0.2.7");

    checkRR("host0010.example.org", RRType::A(), "192.0.2.10");
    checkRR("host0011.example.org", RRType::A(), "192.0.2.11");
    checkRR("host0012.example.org", RRType::A(), "192.0.2.12");

    checkRR("host0014.example.org", RRType::A(), "192.0.2.14");
    checkRR("host0015.example.org", RRType::A(), "192.0.2.15");

567
568
    checkRR("host30.example.org", RRType::TXT(), "Value 001e");
    checkRR("host31.example.org", RRType::TXT(), "Value 001f");
569
570
571
572
573
574
575

    checkRR("host42.example.org", RRType::TXT(), "Value 002A");
    checkRR("host43.example.org", RRType::TXT(), "Value 002B");

    checkRR("host0055.example.org", RRType::A(), "192.0.2.45");
    checkRR("host0056.example.org", RRType::A(), "192.0.2.46");

576
577
578
579
    checkRR("a.5.0.0.example.org", RRType::A(), "192.0.2.90");
    checkRR("b.5.0.0.example.org", RRType::A(), "192.0.2.91");
    checkRR("c.5.0.0.example.org", RRType::A(), "192.0.2.92");

Mukund Sivaraman's avatar
Mukund Sivaraman committed
580
581
    checkRR("e.5.0.0.example.org", RRType::A(), "192.0.2.94");
    checkRR("f.5.0.0.example.org", RRType::A(), "192.0.2.95");
582
583
    checkRR("0.6.0.0.example.org", RRType::A(), "192.0.2.96");

584
585
586
587
    checkRR("host106.example.org", RRType::TXT(), "Value a.6.0.0.0");
    checkRR("host107.example.org", RRType::TXT(), "Value b.6.0.0.0");
    checkRR("host109.example.org", RRType::TXT(), "Value D.6.0.0.0");
    checkRR("host110.example.org", RRType::TXT(), "Value E.6.0.0.0");
588

589
590
    checkRR("host0200.example.org", RRType::A(), "192.0.2.200");
    checkRR("host0201.example.org", RRType::A(), "192.0.2.201");
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
}

TEST_F(MasterLoaderTest, generateWithNoModifiers) {
    const string input =
        "$ORIGIN example.org.\n"
        "$TTL 3600\n"
        "$GENERATE 10-12 host${} A 192.0.2.$\n";
    stringstream ss(input);
    setLoader(ss, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);

    loader_->load();
    EXPECT_FALSE(loader_->loadedSucessfully());
    ASSERT_EQ(2, errors_.size()); // For the broken GENERATE
    EXPECT_TRUE(warnings_.empty());

    checkCallbackMessage(errors_.at(0),
                         "Invalid $GENERATE format modifiers", 3);
    checkCallbackMessage(errors_.at(1),
                         "$GENERATE error", 3);
}

TEST_F(MasterLoaderTest, generateWithBadModifiers) {
    const string input =
        "$ORIGIN example.org.\n"
        "$TTL 3600\n"
        "$GENERATE 10-12 host${GARBAGE} A 192.0.2.$\n";
    stringstream ss(input);
    setLoader(ss, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);

    loader_->load();
    EXPECT_FALSE(loader_->loadedSucessfully());
    ASSERT_EQ(2, errors_.size()); // For the broken GENERATE
    EXPECT_TRUE(warnings_.empty());

    checkCallbackMessage(errors_.at(0),
                         "Invalid $GENERATE format modifiers", 3);
    checkCallbackMessage(errors_.at(1),
                         "$GENERATE error", 3);
}

633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
TEST_F(MasterLoaderTest, generateMissingRange) {
    const string input =
        "$ORIGIN example.org.\n"
        "$GENERATE\n";
    stringstream ss(input);
    setLoader(ss, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);

    loader_->load();
    EXPECT_FALSE(loader_->loadedSucessfully());
    EXPECT_EQ(1, errors_.size()); // For the broken GENERATE
    EXPECT_TRUE(warnings_.empty());

    checkCallbackMessage(errors_.at(0),
                         "unexpected end of input", 2);
}

TEST_F(MasterLoaderTest, generateMissingLHS) {
    const string input =
        "$ORIGIN example.org.\n"
        "$GENERATE 2-4\n";
    stringstream ss(input);
    setLoader(ss, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);

    loader_->load();
    EXPECT_FALSE(loader_->loadedSucessfully());
    EXPECT_EQ(1, errors_.size()); // For the broken GENERATE
    EXPECT_TRUE(warnings_.empty());

    checkCallbackMessage(errors_.at(0),
                         "unexpected end of input", 2);
}

TEST_F(MasterLoaderTest, generateMissingType) {
    const string input =
        "$ORIGIN example.org.\n"
        "$GENERATE 2-4 host$\n";
    stringstream ss(input);
    setLoader(ss, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);

    loader_->load();
    EXPECT_FALSE(loader_->loadedSucessfully());
    EXPECT_EQ(1, errors_.size()); // For the broken GENERATE
    EXPECT_TRUE(warnings_.empty());

    checkCallbackMessage(errors_.at(0),
                         "unexpected end of input", 2);
}

TEST_F(MasterLoaderTest, generateMissingRHS) {
    const string input =
        "$ORIGIN example.org.\n"
        "$GENERATE 2-4 host$ A\n";
    stringstream ss(input);
    setLoader(ss, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);

    loader_->load();
    EXPECT_FALSE(loader_->loadedSucessfully());
    EXPECT_EQ(1, errors_.size()); // For the broken GENERATE
    EXPECT_TRUE(warnings_.empty());

    checkCallbackMessage(errors_.at(0),
                         "unexpected end of input", 2);
}

TEST_F(MasterLoaderTest, generateWithBadRangeSyntax) {
    const string input =
        "$ORIGIN example.org.\n"
        "$GENERATE ABCD host$ 3600 A 192.0.2.$\n";
    stringstream ss(input);
    setLoader(ss, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);

    loader_->load();
    EXPECT_FALSE(loader_->loadedSucessfully());
    EXPECT_EQ(1, errors_.size()); // For the broken GENERATE
    EXPECT_TRUE(warnings_.empty());

    checkCallbackMessage(errors_.at(0),
                         "$GENERATE: invalid range: ABCD", 2);
}

TEST_F(MasterLoaderTest, generateWithInvalidRange) {
    // start > stop
    const string input =
        "$ORIGIN example.org.\n"
        "$GENERATE 2-1 host$ 3600 A 192.0.2.$\n";
    stringstream ss(input);
    setLoader(ss, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);

    loader_->load();
    EXPECT_FALSE(loader_->loadedSucessfully());
    EXPECT_EQ(1, errors_.size()); // For the broken GENERATE
    EXPECT_TRUE(warnings_.empty());

    checkCallbackMessage(errors_.at(0),
                         "$GENERATE: invalid range: 2-1", 2);
}

TEST_F(MasterLoaderTest, generateWithInvalidClass) {
    const string input =
        "$ORIGIN example.org.\n"
        "$GENERATE 1-2 host$ 3600 CH A 192.0.2.$\n";
    stringstream ss(input);
    setLoader(ss, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);

    loader_->load();
    EXPECT_FALSE(loader_->loadedSucessfully());
    EXPECT_EQ(1, errors_.size()); // For the broken GENERATE
    EXPECT_TRUE(warnings_.empty());

    checkCallbackMessage(errors_.at(0),
                         "Class mismatch: CH vs. IN", 2);
}

TEST_F(MasterLoaderTest, generateWithNoAvailableTTL) {
    const string input =
        "$ORIGIN example.org.\n"
        "$GENERATE 1-2 host$ A 192.0.2.$\n";
    stringstream ss(input);
    setLoader(ss, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);

    loader_->load();
    EXPECT_FALSE(loader_->loadedSucessfully());
    EXPECT_EQ(1, errors_.size()); // For the broken GENERATE
    EXPECT_TRUE(warnings_.empty());

    checkCallbackMessage(errors_.at(0),
                         "no TTL specified; load rejected", 2);
}

770
771
772
773
774
// Test the source is correctly popped even after error
TEST_F(MasterLoaderTest, popAfterError) {
    const string include_str = "$include " TEST_DATA_SRCDIR
        "/broken.zone\nwww 3600 IN AAAA 2001:db8::1\n";
    stringstream ss(include_str);
775
    // We perform the test with MANY_ERRORS, we want to see what happens
776
777
778
779
780
781
782
783
784
785
786
787
    // after the error.
    setLoader(ss, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);

    loader_->load();
    EXPECT_FALSE(loader_->loadedSucessfully());
    EXPECT_EQ(1, errors_.size()); // For the broken RR
    EXPECT_EQ(1, warnings_.size()); // For missing EOLN

    // The included file doesn't contain anything usable, but the
    // line after the include should be there.
    checkRR("www.example.org", RRType::AAAA(), "2001:db8::1");
788
}
789

790
791
// Check it works the same when created based on a stream, not filename
TEST_F(MasterLoaderTest, streamConstructor) {
792
793
    const string zone_data(prepareZone("", true));
    stringstream zone_stream(zone_data);
794
795
796
    setLoader(zone_stream, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);

797
    EXPECT_FALSE(loader_->loadedSucessfully());
798
799
800
801
802
803
804

    // Unlike the basicLoad test, if we construct the loader from a stream
    // getSize() returns the data size in the stream immediately after the
    // construction.
    EXPECT_EQ(zone_data.size(), loader_->getSize());
    EXPECT_EQ(0, loader_->getPosition());

805
    loader_->load();
806
    EXPECT_TRUE(loader_->loadedSucessfully());
807
808
809
810
811
812

    EXPECT_TRUE(errors_.empty());
    EXPECT_TRUE(warnings_.empty());
    checkRR("example.org", RRType::SOA(), "ns1.example.org. "
            "admin.example.org. 1234 3600 1800 2419200 7200");
    checkRR("correct.example.org", RRType::A(), "192.0.2.2");
813
814

    // On completion of the load, both getSize() and getPosition() return the
815
    // size of the data.
816
817
    EXPECT_EQ(zone_data.size(), loader_->getSize());
    EXPECT_EQ(zone_data.size(), loader_->getPosition());
818
819
}

820
821
// Try loading data incrementally.
TEST_F(MasterLoaderTest, incrementalLoad) {
822
823
    setLoader(TEST_DATA_SRCDIR "/example.org", Name("example.org."),
              RRClass::IN(), MasterLoader::MANY_ERRORS);
824

825
    EXPECT_FALSE(loader_->loadedSucessfully());
826
    EXPECT_FALSE(loader_->loadIncremental(2));
827
    EXPECT_FALSE(loader_->loadedSucessfully());
828
829
830
831

    EXPECT_TRUE(errors_.empty());
    EXPECT_TRUE(warnings_.empty());

832
833
    checkRR("example.org", RRType::SOA(),
            "ns1.example.org. admin.example.org. "
834
835
836
837
838
839
840
841
            "1234 3600 1800 2419200 7200");
    checkRR("example.org", RRType::NS(), "ns1.example.org.");

    // The third one is not loaded yet
    EXPECT_TRUE(rrsets_.empty());

    // Load the rest.
    EXPECT_TRUE(loader_->loadIncremental(20));
842
    EXPECT_TRUE(loader_->loadedSucessfully());
843
844
845
846
847

    EXPECT_TRUE(errors_.empty());
    EXPECT_TRUE(warnings_.empty());

    checkRR("www.example.org", RRType::A(), "192.0.2.1");
848
    checkRR("www.example.org", RRType::AAAA(), "2001:db8::1");
849
850
}

851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
// Try loading from file that doesn't exist. There should be single error
// saying so.
TEST_F(MasterLoaderTest, invalidFile) {
    setLoader("This file doesn't exist at all",
              Name("exmaple.org."), RRClass::IN(), MasterLoader::MANY_ERRORS);

    // Nothing yet. The loader is dormant until invoked.
    // Is it really what we want?
    EXPECT_TRUE(errors_.empty());

    loader_->load();

    EXPECT_TRUE(warnings_.empty());
    EXPECT_TRUE(rrsets_.empty());
    ASSERT_EQ(1, errors_.size());
    EXPECT_EQ(0, errors_[0].find("Error opening the input source file: ")) <<
        "Different error: " << errors_[0];
}
869
870

struct ErrorCase {
871
    const char* const line;    // The broken line in master file
872
    const char* const reason;  // If non NULL, the reason string
873
    const char* const problem; // Description of the problem for SCOPED_TRACE
874
} const error_cases[] = {
875
876
877
878
    { "www...   3600    IN  A   192.0.2.1", NULL, "Invalid name" },
    { "www      FORTNIGHT   IN  A   192.0.2.1", NULL, "Invalid TTL" },
    { "www      3600    XX  A   192.0.2.1", NULL, "Invalid class" },
    { "www      3600    IN  A   bad_ip", NULL, "Invalid Rdata" },
879
880

    // Parameter ordering errors
881
    { "www      IN      A   3600 192.168.2.7",
882
      "createRdata from text failed: Bad IN/A RDATA text: '3600'",
883
884
      "Incorrect order of class, TTL and type" },
    { "www      A       IN  3600 192.168.2.8",
885
      "createRdata from text failed: Bad IN/A RDATA text: 'IN'",
886
887
      "Incorrect order of class, TTL and type" },
    { "www      3600    A   IN   192.168.2.7",
888
      "createRdata from text failed: Bad IN/A RDATA text: 'IN'",
889
890
      "Incorrect order of class, TTL and type" },
    { "www      A       3600 IN  192.168.2.8",
891
      "createRdata from text failed: Bad IN/A RDATA text: '3600'",
892
      "Incorrect order of class, TTL and type" },
893
894
895
896
897
898
899
900
901
902

    // Missing type and Rdata
    { "www", "unexpected end of input", "Missing type and Rdata" },
    { "www 3600", "unexpected end of input", "Missing type and Rdata" },
    { "www IN", "unexpected end of input", "Missing type and Rdata" },
    { "www 3600 IN", "unexpected end of input", "Missing type and Rdata" },
    { "www IN 3600", "unexpected end of input", "Missing type and Rdata" },

    // Missing Rdata
    { "www A",
903
      "createRdata from text failed: unexpected end of input",
904
905
      "Missing Rdata" },
    { "www 3600 A",
906
      "createRdata from text failed: unexpected end of input",
907
908
      "Missing Rdata" },
    { "www IN A",
909
      "createRdata from text failed: unexpected end of input",
910
911
      "Missing Rdata" },
    { "www 3600 IN A",
912
      "createRdata from text failed: unexpected end of input",
913
914
      "Missing Rdata" },
    { "www IN 3600 A",
915
      "createRdata from text failed: unexpected end of input",
916
917
      "Missing Rdata" },

918
    { "www      3600    IN", NULL, "Unexpected EOLN" },
919
920
    { "www      3600    CH  TXT nothing", "Class mismatch: CH vs. IN",
      "Class mismatch" },
921
922
923
924
925
926
    { "www      \"3600\"  IN  A   192.0.2.1", NULL, "Quoted TTL" },
    { "www      3600    \"IN\"  A   192.0.2.1", NULL, "Quoted class" },
    { "www      3600    IN  \"A\"   192.0.2.1", NULL, "Quoted type" },
    { "unbalanced)paren 3600    IN  A   192.0.2.1", NULL, "Token error 1" },
    { "www  3600    unbalanced)paren    A   192.0.2.1", NULL,
      "Token error 2" },
927
928
    // Check the unknown directive. The rest looks like ordinary RR,
    // so we see the $ is actually special.
929
    { "$UNKNOWN 3600    IN  A   192.0.2.1", NULL, "Unknown $ directive" },
930
931
932
933
934
935
    { "$INCLUD " TEST_DATA_SRCDIR "/example.org", "Unknown directive 'INCLUD'",
        "Include too short" },
    { "$INCLUDES " TEST_DATA_SRCDIR "/example.org",
        "Unknown directive 'INCLUDES'", "Include too long" },
    { "$INCLUDE", "unexpected end of input", "Missing include path" },
    // The following two error messages are system dependant, omitting
936
    { "$INCLUDE /file/not/found", NULL, "Include file not found" },
937
    { "$INCLUDE /file/not/found example.org. and here goes bunch of garbage",
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
938
        NULL, "Include file not found and garbage at the end of line" },
939
940
941
942
943
944
945
946
947
    { "$ORIGIN", "unexpected end of input", "Missing origin name" },
    { "$ORIGIN invalid...name", "duplicate period in invalid...name",
        "Invalid name for origin" },
    { "$ORIGIN )brokentoken", "unbalanced parentheses",
        "Broken token in origin" },
    { "$ORIGIN example.org. garbage", "Extra tokens at the end of line",
        "Garbage after origin" },
    { "$ORIGI name.", "Unknown directive 'ORIGI'", "$ORIGIN too short" },
    { "$ORIGINAL name.", "Unknown directive 'ORIGINAL'", "$ORIGIN too long" },
948
949
    { "$TTL 100 extra-garbage", "Extra tokens at the end of line",
      "$TTL with extra token" },
950
951
952
953
954
    { "$TTL", "unexpected end of input", "missing TTL" },
    { "$TTL No-ttl", "Unknown unit used: N in: No-ttl", "bad TTL" },
    { "$TTL \"100\"", "invalid TTL: \"100\"", "bad TTL, quoted" },
    { "$TT 100", "Unknown directive 'TT'", "bad directive, too short" },
    { "$TTLLIKE 100", "Unknown directive 'TTLLIKE'", "bad directive, extra" },
955
    { NULL, NULL, NULL }
956
957
958
959
960
961
962
};

// Test a broken zone is handled properly. We test several problems,
// both in strict and lenient mode.
TEST_F(MasterLoaderTest, brokenZone) {
    for (const ErrorCase* ec = error_cases; ec->line != NULL; ++ec) {
        SCOPED_TRACE(ec->problem);
963
        const string zone(prepareZone(ec->line, true));
964
965
966
967

        {
            SCOPED_TRACE("Strict mode");
            clear();
968
969
            stringstream zone_stream(zone);
            setLoader(zone_stream, Name("example.org."), RRClass::IN(),
970
                      MasterLoader::DEFAULT);
971
            EXPECT_FALSE(loader_->loadedSucessfully());
972
            EXPECT_THROW(loader_->load(), MasterLoaderError);
973
            EXPECT_FALSE(loader_->loadedSucessfully());
974
            EXPECT_EQ(1, errors_.size());
975
            if (ec->reason != NULL) {
976
                checkCallbackMessage(errors_.at(0), ec->reason, 2);
977
            }
978
979
980
981
982
983
984
985
986
987
988
989
            EXPECT_TRUE(warnings_.empty());

            checkRR("example.org", RRType::SOA(), "ns1.example.org. "
                    "admin.example.org. 1234 3600 1800 2419200 7200");
            // In the strict mode, it is aborted. The last RR is not
            // even attempted.
            EXPECT_TRUE(rrsets_.empty());
        }

        {
            SCOPED_TRACE("Lenient mode");
            clear();
990
991
            stringstream zone_stream(zone);
            setLoader(zone_stream, Name("example.org."), RRClass::IN(),
992
                      MasterLoader::MANY_ERRORS);
993
            EXPECT_FALSE(loader_->loadedSucessfully());
994
            EXPECT_NO_THROW(loader_->load());
995
            EXPECT_FALSE(loader_->loadedSucessfully());
996
997
998
999
1000
1001
1002
1003
            EXPECT_EQ(1, errors_.size());
            EXPECT_TRUE(warnings_.empty());
            checkRR("example.org", RRType::SOA(), "ns1.example.org. "
                    "admin.example.org. 1234 3600 1800 2419200 7200");
            // This one is below the error one.
            checkRR("correct.example.org", RRType::A(), "192.0.2.2");
            EXPECT_TRUE(rrsets_.empty());
        }
1004
1005
1006
1007
1008

        {
            SCOPED_TRACE("Error at EOF");
            // This case is interesting only in the lenient mode.
            clear();
1009
            const string zoneEOF(prepareZone(ec->line, false));
1010
1011
1012
            stringstream zone_stream(zoneEOF);
            setLoader(zone_stream, Name("example.org."), RRClass::IN(),
                      MasterLoader::MANY_ERRORS);
1013
            EXPECT_FALSE(loader_->loadedSucessfully());
1014
            EXPECT_NO_THROW(loader_->load());
1015
            EXPECT_FALSE(loader_->loadedSucessfully());
1016
            EXPECT_EQ(1, errors_.size()) << errors_[0] << "\n" << errors_[1];
1017
1018
            // The unexpected EOF warning
            EXPECT_EQ(1, warnings_.size());
1019
1020
1021
1022
            checkRR("example.org", RRType::SOA(), "ns1.example.org. "
                    "admin.example.org. 1234 3600 1800 2419200 7200");
            EXPECT_TRUE(rrsets_.empty());
        }
1023
1024
    }
}
1025

1026
1027
1028
1029
1030
1031
// Check that a garbage after the include generates an error, but not fatal
// one (in lenient mode) and we can recover.
TEST_F(MasterLoaderTest, includeWithGarbage) {
    // Include an origin (example.org) because we expect it to be handled
    // soon and we don't want it to break here.
    const string include_str("$INCLUDE " TEST_DATA_SRCDIR
1032
                             "/example.org example.org. bunch of other stuff\n"
1033
1034
1035
1036
1037
1038
1039
1040
                             "www 3600 IN AAAA 2001:db8::1\n");
    stringstream zone_stream(include_str);
    setLoader(zone_stream, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);

    EXPECT_NO_THROW(loader_->load());
    EXPECT_FALSE(loader_->loadedSucessfully());
    ASSERT_EQ(1, errors_.size());
1041
    checkCallbackMessage(errors_.at(0), "Extra tokens at the end of line", 1);
1042
1043
1044
1045
1046
1047
1048
    // It says something about extra tokens at the end
    EXPECT_NE(string::npos, errors_[0].find("Extra"));
    EXPECT_TRUE(warnings_.empty());
    checkBasicRRs();
    checkRR("www.example.org", RRType::AAAA(), "2001:db8::1");
}

Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
1049
1050
// Check we error about garbage at the end of $ORIGIN line (but the line
// works).
1051
TEST_F(MasterLoaderTest, originWithGarbage) {
1052
    const string origin_str = "$ORIGIN www.example.org. More garbage here\n"
1053
1054
1055
1056
1057
1058
1059
        "@  1H  IN  A   192.0.2.1\n";
    stringstream ss(origin_str);
    setLoader(ss, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);
    EXPECT_NO_THROW(loader_->load());
    EXPECT_FALSE(loader_->loadedSucessfully());
    ASSERT_EQ(1, errors_.size());
1060
    checkCallbackMessage(errors_.at(0), "Extra tokens at the end of line", 1);
1061
1062
1063
1064
    EXPECT_TRUE(warnings_.empty());
    checkARR("www.example.org");
}

1065
1066
1067
1068
// Test we can pass both file to include and the origin to switch
TEST_F(MasterLoaderTest, includeAndOrigin) {
    // First, switch origin to something else, so we can check it is
    // switched back.
1069
    const string include_string = "$ORIGIN www.example.org.\n"
1070
1071
1072
        "@  1H  IN  A   192.0.2.1\n"
        // Then include the file with data and switch origin back
        "$INCLUDE " TEST_DATA_SRCDIR "/example.org example.org.\n"
1073
        // Another RR to see we fall back to the previous origin.
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
        "www    1H  IN  A   192.0.2.1\n";
    stringstream ss(include_string);
    setLoader(ss, Name("example.org"), RRClass::IN(),
              MasterLoader::MANY_ERRORS);
    // Successfully load the data
    loader_->load();
    EXPECT_TRUE(loader_->loadedSucessfully());
    EXPECT_TRUE(errors_.empty());
    EXPECT_TRUE(warnings_.empty());
    // And check it's the correct data
    checkARR("www.example.org");
    checkBasicRRs();
1086
    checkARR("www.www.example.org");
1087
1088
}

1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
// Like above, but the origin after include is bogus. The whole line should
// be rejected.
TEST_F(MasterLoaderTest, includeAndBadOrigin) {
    const string include_string =
        "$INCLUDE " TEST_DATA_SRCDIR "/example.org example..org.\n"
        // Another RR to see the switch survives after we exit include
        "www    1H  IN  A   192.0.2.1\n";
    stringstream ss(include_string);
    setLoader(ss, Name("example.org"), RRClass::IN(),
              MasterLoader::MANY_ERRORS);
    loader_->load();
    EXPECT_FALSE(loader_->loadedSucessfully());
    EXPECT_EQ(1, errors_.size());
1102
1103
    checkCallbackMessage(errors_.at(0), "duplicate period in example..org.",
                         1);
1104
1105
1106
1107
1108
    EXPECT_TRUE(warnings_.empty());
    // And check it's the correct data
    checkARR("www.example.org");
}

1109
1110
// Check the origin doesn't get outside of the included file.
TEST_F(MasterLoaderTest, includeOriginRestore) {
1111
1112
    const string include_string =
        "$INCLUDE " TEST_DATA_SRCDIR "/origincheck.txt\n"
1113
1114
1115
1116
1117
1118
1119
        "@  1H  IN  A   192.0.2.1\n";
    stringstream ss(include_string);
    setLoader(ss, Name("example.org"), RRClass::IN(),
              MasterLoader::MANY_ERRORS);
    // Successfully load the data
    loader_->load();
    EXPECT_TRUE(loader_->loadedSucessfully());
1120
    EXPECT_TRUE(errors_.empty());
1121
1122
1123
1124
1125
1126
    EXPECT_TRUE(warnings_.empty());
    // And check it's the correct data
    checkARR("www.example.org");
    checkARR("example.org");
}

1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
// Check we restore the last name for initial whitespace when returning from
// include. But we do produce a warning if there's one just ofter the include.
TEST_F(MasterLoaderTest, includeAndInitialWS) {
    const string include_string = "xyz  1H  IN  A   192.0.2.1\n"
        "$INCLUDE " TEST_DATA_SRCDIR "/example.org\n"
        "   1H  IN  A   192.0.2.1\n";
    stringstream ss(include_string);
    setLoader(ss, Name("example.org"), RRClass::IN(),
              MasterLoader::MANY_ERRORS);
    // Successfully load the data
    loader_->load();
    EXPECT_TRUE(loader_->loadedSucessfully());
    EXPECT_TRUE(errors_.empty());
    EXPECT_EQ(1, warnings_.size());
1141
    checkCallbackMessage(warnings_.at(0),
1142
1143
                         "Owner name omitted around $INCLUDE, the result might "
                         "not be as expected", 3);
1144
1145
1146
1147
1148
    checkARR("xyz.example.org");
    checkBasicRRs();
    checkARR("xyz.example.org");
}

1149
1150
// Test for "$TTL"
TEST_F(MasterLoaderTest, ttlDirective) {
1151
1152
    stringstream zone_stream;

1153
    // Set the default TTL with $TTL followed by an RR omitting the TTL
1154
1155
    zone_stream << "$TTL 1800\nexample.org. IN A 192.0.2.1\n";
    // $TTL can be quoted.  Also testing the case of $TTL being changed.
1156
    zone_stream << "\"$TTL\" 100\na.example.org. IN A 192.0.2.2\n";
1157
    // Extended TTL form is accepted.
1158
1159
    zone_stream << "$TTL 1H\nb.example.org. IN A 192.0.2.3\n";
    // Matching is case insensitive.
1160
1161
1162
    zone_stream << "$tTl 360\nc.example.org. IN A 192.0.2.4\n";
    // Maximum allowable TTL
    zone_stream << "$TTL 2147483647\nd.example.org. IN A 192.0.2.5\n";
1163

1164
1165
1166
1167
1168
    setLoader(zone_stream, Name("example.org."), RRClass::IN(),
              MasterLoader::DEFAULT);
    loader_->load();
    EXPECT_TRUE(loader_->loadedSucessfully());
    checkRR("example.org", RRType::A(), "192.0.2.1", RRTTL(1800));
1169
1170
    checkRR("a.example.org", RRType::A(), "192.0.2.2", RRTTL(100));
    checkRR("b.example.org", RRType::A(), "192.0.2.3", RRTTL(3600));
1171
1172
    checkRR("c.example.org", RRType::A(), "192.0.2.4", RRTTL(360));
    checkRR("d.example.org", RRType::A(), "192.0.2.5", RRTTL(2147483647));
1173
1174
}

1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
TEST_F(MasterLoaderTest, ttlFromSOA) {
    // No $TTL, and the SOA doesn't have an explicit TTL field.  Its minimum
    // TTL field will be used as the RR's TTL, and it'll be used as the
    // default TTL for others.
    stringstream zone_stream("example.org. IN SOA . . 0 0 0 0 1800\n"
                             "a.example.org. IN A 192.0.2.1\n");
    setLoader(zone_stream, Name("example.org."), RRClass::IN(),
              MasterLoader::DEFAULT);
    loader_->load();
    EXPECT_TRUE(loader_->loadedSucessfully());
    checkRR("example.org", RRType::SOA(), ". . 0 0 0 0 1800", RRTTL(1800));
    checkRR("a.example.org", RRType::A(), "192.0.2.1", RRTTL(1800));

    // The use of SOA minimum TTL should have caused a warning.
    EXPECT_EQ(1, warnings_.size());
1190
1191
    checkCallbackMessage(warnings_.at(0),
                         "no TTL specified; using SOA MINTTL instead", 1);
1192
1193
}

1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
TEST_F(MasterLoaderTest, ttlFromPrevious) {
    // No available default TTL.  2nd and 3rd RR will use the TTL of the
    // 1st RR.  This will result in a warning, but only for the first time.
    stringstream zone_stream("a.example.org. 1800 IN A 192.0.2.1\n"
                             "b.example.org. IN A 192.0.2.2\n"
                             "c.example.org. IN A 192.0.2.3\n");
    setLoader(zone_stream, Name("example.org."), RRClass::IN(),
              MasterLoader::DEFAULT);
    loader_->load();
    EXPECT_TRUE(loader_->loadedSucessfully());
    checkRR("a.example.org", RRType::A(), "192.0.2.1", RRTTL(1800));
    checkRR("b.example.org", RRType::A(), "192.0.2.2", RRTTL(1800));
    checkRR("c.example.org", RRType::A(), "192.0.2.3", RRTTL(1800));

    EXPECT_EQ(1, warnings_.size());
1209
1210
1211
    checkCallbackMessage(warnings_.at(0), "using RFC1035 TTL semantics", 2);
}

Mukund Sivaraman's avatar
Mukund Sivaraman committed
1212
TEST_F(MasterLoaderTest, RRParamsOrdering) {
1213
1214
    // We test the order and existence of TTL, class and type. See
    // MasterLoader::MasterLoaderImpl::parseRRParams() for ordering.
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241

    stringstream zone_stream;
    // <TTL> <class> <type> <RDATA>
    zone_stream << "a.example.org. 1800 IN A 192.0.2.1\n";
    // <type> <RDATA>
    zone_stream << "b.example.org. A 192.0.2.2\n";
    // <class> <TTL> <type> <RDATA>
    zone_stream << "c.example.org. IN 3600 A 192.0.2.3\n";
    // <TTL> <type> <RDATA>
    zone_stream << "d.example.org. 7200 A 192.0.2.4\n";
    // <class> <type> <RDATA>
    zone_stream << "e.example.org. IN A 192.0.2.5\n";

    setLoader(zone_stream, Name("example.org."), RRClass::IN(),
              MasterLoader::DEFAULT);
    loader_->load();
    EXPECT_TRUE(loader_->loadedSucessfully());
    checkRR("a.example.org", RRType::A(), "192.0.2.1", RRTTL(1800));
    checkRR("b.example.org", RRType::A(), "192.0.2.2", RRTTL(1800));
    checkRR("c.example.org", RRType::A(), "192.0.2.3", RRTTL(3600));
    checkRR("d.example.org", RRType::A(), "192.0.2.4", RRTTL(7200));
    checkRR("e.example.org", RRType::A(), "192.0.2.5", RRTTL(7200));

    EXPECT_EQ(1, warnings_.size());
    checkCallbackMessage(warnings_.at(0), "using RFC1035 TTL semantics", 2);
}

1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
TEST_F(MasterLoaderTest, ttlFromPreviousSOA) {
    // Mixture of the previous two cases: SOA has explicit TTL, followed by
    // an RR without an explicit TTL.  In this case the minimum TTL won't be
    // recognized as the "default TTL".
    stringstream zone_stream("example.org. 100 IN SOA . . 0 0 0 0 1800\n"
                             "a.example.org. IN A 192.0.2.1\n");
    setLoader(zone_stream, Name("example.org."), RRClass::IN(),
              MasterLoader::DEFAULT);
    loader_->load();
    EXPECT_TRUE(loader_->loadedSucessfully());

    checkRR("example.org", RRType::SOA(), ". . 0 0 0 0 1800", RRTTL(100));
    checkRR("a.example.org", RRType::A(), "192.0.2.1", RRTTL(100));

    EXPECT_EQ(1, warnings_.size());
    checkCallbackMessage(warnings_.at(0), "using RFC1035 TTL semantics", 2);
}

TEST_F(MasterLoaderTest, ttlUnknown) {
    // No available TTL is known for the first RR.
    stringstream zone_stream("a.example.org. IN A 192.0.2.1\n");
    setLoader(zone_stream, Name("example.org."), RRClass::IN(),
              MasterLoader::DEFAULT);
    EXPECT_THROW(loader_->load(), MasterLoaderError);
}

TEST_F(MasterLoaderTest, ttlUnknownAndContinue) {
    stringstream zone_stream("a.example.org. IN A 192.0.2.1\n"
                             "b.example.org. 1800 IN A 192.0.2.2\n");
    setLoader(zone_stream, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);
    loader_->load();
    EXPECT_FALSE(loader_->loadedSucessfully());
    checkRR("b.example.org", RRType::A(), "192.0.2.2", RRTTL(1800));

    EXPECT_TRUE(warnings_.empty());
    EXPECT_EQ(1, errors_.size());
    checkCallbackMessage(errors_.at(0), "no TTL specified; load rejected", 1);
}

TEST_F(MasterLoaderTest, ttlUnknownAndEOF) {
    // Similar to the previous case, but the input will be abruptly terminated
    // after the offending RR.  This will cause an additional warning.
    stringstream zone_stream("a.example.org. IN A 192.0.2.1");
    setLoader(zone_stream, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);
    loader_->load();
    EXPECT_FALSE(loader_->loadedSucessfully());
    EXPECT_TRUE(rrsets_.empty());

    EXPECT_EQ(1, errors_.size());
    checkCallbackMessage(errors_.at(0), "no TTL specified; load rejected", 1);

    // RDATA implementation can complain about it, too.  To be independent of
    // its details, we focus on the very last warning.
    EXPECT_FALSE(warnings_.empty());
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
1298
1299
    checkCallbackMessage(*warnings_.rbegin(), "File does not end with newline",
                         1);
1300
1301
}

1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
TEST_F(MasterLoaderTest, ttlOverflow) {
    stringstream zone_stream;
    zone_stream << "example.org. IN SOA . . 0 0 0 0 2147483648\n";
    zone_stream << "$TTL 3600\n"; // reset to an in-range value
    zone_stream << "$TTL 2147483649\n" << "a.example.org. IN A 192.0.2.1\n";
    zone_stream << "$TTL 3600\n"; // reset to an in-range value
    zone_stream << "b.example.org. 2147483650 IN A 192.0.2.2\n";
    setLoader(zone_stream, Name("example.org."), RRClass::IN(),
              MasterLoader::DEFAULT);

    loader_->load();
    EXPECT_TRUE(loader_->loadedSucessfully());
    EXPECT_EQ(3, rrsets_.size());

    checkRR("example.org", RRType::SOA(), ". . 0 0 0 0 2147483648", RRTTL(0));
    checkRR("a.example.org", RRType::A(), "192.0.2.1", RRTTL(0));
    checkRR("b.example.org", RRType::A(), "192.0.2.2", RRTTL(0));

    EXPECT_EQ(4, warnings_.size());
    checkCallbackMessage(warnings_.at(1),
                         "TTL 2147483648 > MAXTTL, setting to 0 per RFC2181",
                         1);
    checkCallbackMessage(warnings_.at(2),
                         "TTL 2147483649 > MAXTTL, setting to 0 per RFC2181",
                         3);
    checkCallbackMessage(warnings_.at(3),
                         "TTL 2147483650 > MAXTTL, setting to 0 per RFC2181",
                         6);
}

1332
1333
1334
1335
1336
// Test the constructor rejects empty add callback.
TEST_F(MasterLoaderTest, emptyCallback) {
    EXPECT_THROW(MasterLoader(TEST_DATA_SRCDIR "/example.org",
                              Name("example.org"), RRClass::IN(), callbacks_,
                              AddRRCallback()), isc::InvalidParameter);
1337
1338
1339
1340
1341
    // And the same with the second constructor
    stringstream ss("");
    EXPECT_THROW(MasterLoader(ss, Name("example.org"), RRClass::IN(),
                              callbacks_, AddRRCallback()),
                 isc::InvalidParameter);
1342
1343
1344
1345
1346
1347
1348
1349
1350
}

// Check it throws when we try to load after loading was complete.
TEST_F(MasterLoaderTest, loadTwice) {
    setLoader(TEST_DATA_SRCDIR "/example.org", Name("example.org."),
              RRClass::IN(), MasterLoader::MANY_ERRORS);

    loader_->load();
    EXPECT_THROW(loader_->load(), isc::InvalidOperation);
1351
1352
1353
    // Don't check them, they are not interesting, so suppress the error
    // at TearDown
    rrsets_.clear();
1354
}
1355
1356
1357
1358
1359
1360
1361
1362

// Load 0 items should be rejected
TEST_F(MasterLoaderTest, loadZero) {
    setLoader(TEST_DATA_SRCDIR "/example.org", Name("example.org."),
              RRClass::IN(), MasterLoader::MANY_ERRORS);
    EXPECT_THROW(loader_->loadIncremental(0), isc::InvalidParameter);
}

1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
// Test there's a warning when the file terminates without end of
// line.
TEST_F(MasterLoaderTest, noEOLN) {
    // No \n at the end
    const string input("example.org. 3600 IN SOA ns1.example.org. "
                       "admin.example.org. 1234 3600 1800 2419200 7200");
    stringstream ss(input);
    setLoader(ss, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);

    loader_->load();
    EXPECT_TRUE(loader_->loadedSucessfully());
1375
    EXPECT_TRUE(errors_.empty());
1376
1377
1378
1379
1380
1381
    // There should be one warning about the EOLN
    EXPECT_EQ(1, warnings_.size());
    checkRR("example.org", RRType::SOA(), "ns1.example.org. "
            "admin.example.org. 1234 3600 1800 2419200 7200");
}

1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
// Test it rejects when we don't have the previous name to use in place of
// initial whitespace
TEST_F(MasterLoaderTest, noPreviousName) {
    const string input("    1H  IN  A   192.0.2.1\n");
    stringstream ss(input);
    setLoader(ss, Name("example.org."), RRClass::IN(),
              MasterLoader::MANY_ERRORS);
    loader_->load();
    EXPECT_FALSE(loader_->loadedSucessfully());
    EXPECT_EQ(1, errors_.size());
1392
1393
    checkCallbackMessage(errors_.at(0), "No previous name to use in place of "
                         "initial whitespace", 1);
1394
1395
1396
    EXPECT_TRUE(warnings_.empty());
}

1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
// Check we warn if the first RR in an included file has omitted name
TEST_F(MasterLoaderTest, previousInInclude) {
    const string input("www 1H  IN  A   192.0.2.1\n"
                       "$INCLUDE " TEST_DATA_SRCDIR "/omitcheck.txt\n");
    stringstream ss(input);
    setLoader(ss, Name("example.org"), RRClass::IN(),
              MasterLoader::MANY_ERRORS);
    loader_->load();
    EXPECT_TRUE(loader_->loadedSucessfully());
    EXPECT_TRUE(errors_.empty());
    // There should be one warning about the EOLN
    EXPECT_EQ(1, warnings_.size());
    checkCallbackMessage(warnings_.at(0), "Owner name omitted around "
                         "$INCLUDE, the result might not be as expected", 1);
    checkARR("www.example.org");
    checkARR("www.example.org");
}

1415
}