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
// Copyright (C) 2010-2024 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 <util/buffer.h>
#include <dns/exceptions.h>
#include <dns/messagerenderer.h>
#include <dns/rdata.h>
#include <dns/rdataclass.h>
#include <dns/rrclass.h>
#include <dns/rrtype.h>

#include <gtest/gtest.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <dns/tests/unittest_util.h>
#include <dns/tests/rdata_unittest.h>
#include <util/unittests/wiredata.h>

using namespace isc::dns;
using namespace isc::dns::rdata;
using namespace isc::util;
using isc::UnitTestUtil;
using isc::util::unittests::matchWireData;

using namespace std;
//
// This test currently simply copies the NS RDATA tests.
//

namespace {
class Rdata_PTR_Test : public RdataTest {
public:
    Rdata_PTR_Test() :
        rdata_ptr("ns.example.com."),
        rdata_ptr2("ns2.example.com.") {
    }

    const generic::PTR rdata_ptr;
    const generic::PTR rdata_ptr2;
};

const uint8_t wiredata_ptr[] = {
    0x02, 0x6e, 0x73, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03,
    0x63, 0x6f, 0x6d, 0x00 };
const uint8_t wiredata_ptr2[] = {
    // first name: ns.example.com.
    0x02, 0x6e, 0x73, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03,
    0x63, 0x6f, 0x6d, 0x00,
    // second name: ns2.example.com.  all labels except the first should be
    // compressed.
    0x03, 0x6e, 0x73, 0x32, 0xc0, 0x03 };

TEST_F(Rdata_PTR_Test, createFromText) {<--- syntax error
    EXPECT_EQ(0, rdata_ptr.compare(generic::PTR("ns.example.com.")));
    // explicitly add a trailing dot.  should be the same RDATA.
    EXPECT_EQ(0, rdata_ptr.compare(generic::PTR("ns.example.com.")));
    // should be case sensitive.
    EXPECT_EQ(0, rdata_ptr.compare(generic::PTR("NS.EXAMPLE.COM.")));
    // RDATA of a class-independent type should be recognized for any
    // "unknown" class.
    EXPECT_EQ(0, rdata_ptr.compare(*createRdata(RRType("PTR"), RRClass(65000),
                                               "ns.example.com.")));
}

TEST_F(Rdata_PTR_Test, badText) {
    // Extra text at end of line
    EXPECT_THROW(generic::PTR("foo.example.com. extra."), InvalidRdataText);
}

TEST_F(Rdata_PTR_Test, createFromWire) {
    EXPECT_EQ(0, rdata_ptr.compare(
                  *rdataFactoryFromFile(RRType("PTR"), RRClass("IN"),
                                        "rdata_ns_fromWire")));
    // RDLENGTH is too short
    EXPECT_THROW(rdataFactoryFromFile(RRType("PTR"), RRClass("IN"),
                                      "rdata_ns_fromWire", 18),
                 InvalidRdataLength);
    // RDLENGTH is too long
    EXPECT_THROW(rdataFactoryFromFile(RRType("PTR"), RRClass("IN"),
                                      "rdata_ns_fromWire", 36),
                 InvalidRdataLength);
    // incomplete name.  the error should be detected in the name constructor
    EXPECT_THROW(rdataFactoryFromFile(RRType("PTR"), RRClass("IN"),
                                      "rdata_ns_fromWire", 71),
                 DNSMessageFORMERR);

    EXPECT_EQ(0, generic::PTR("ns2.example.com.").compare(
                  *rdataFactoryFromFile(RRType("PTR"), RRClass("IN"),
                                        "rdata_ns_fromWire", 55)));
    EXPECT_THROW(*rdataFactoryFromFile(RRType("PTR"), RRClass("IN"),
                                       "rdata_ns_fromWire", 63),
                 InvalidRdataLength);
}

TEST_F(Rdata_PTR_Test, createFromLexer) {
    EXPECT_EQ(0, rdata_ptr.compare(
        *test::createRdataUsingLexer(RRType::PTR(), RRClass::IN(),
                                     "ns.example.com.")));

    // test::createRdataUsingLexer() constructs relative to
    // "example.org." origin.
    EXPECT_EQ(0, generic::PTR("foo0.example.org.").compare(
        *test::createRdataUsingLexer(RRType::PTR(), RRClass::IN(),
                                     "foo0")));

    // Extra text at end of line
    EXPECT_FALSE(test::createRdataUsingLexer(RRType::PTR(), RRClass::IN(),
                                             "foo.example.com. extra."));
}

TEST_F(Rdata_PTR_Test, toWireBuffer) {
    rdata_ptr.toWire(obuffer);
    matchWireData(wiredata_ptr, sizeof(wiredata_ptr),
                  obuffer.getData(), obuffer.getLength());
}

TEST_F(Rdata_PTR_Test, toWireRenderer) {
    rdata_ptr.toWire(renderer);
    matchWireData(wiredata_ptr, sizeof(wiredata_ptr),
                  renderer.getData(), renderer.getLength());

    rdata_ptr2.toWire(renderer);
    matchWireData(wiredata_ptr2, sizeof(wiredata_ptr2),
                  renderer.getData(), renderer.getLength());
}

TEST_F(Rdata_PTR_Test, toText) {
    EXPECT_EQ("ns.example.com.", rdata_ptr.toText());
}

TEST_F(Rdata_PTR_Test, compare) {
    generic::PTR small("a.example.");
    generic::PTR large("example.");
    EXPECT_TRUE(Name("a.example") > Name("example"));
    EXPECT_GT(0, small.compare(large));
}

TEST_F(Rdata_PTR_Test, getPTRName) {
    EXPECT_EQ(Name("ns.example.com"), rdata_ptr.getPTRName());
}
}