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
// 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/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 std;
using namespace isc::dns;
using namespace isc::util;
using namespace isc::dns::rdata;
using isc::UnitTestUtil;
using isc::util::unittests::matchWireData;

namespace {
class Rdata_OPT_Test : public RdataTest {
    // there's nothing to specialize
};

const uint8_t rdata_opt_wiredata[] = {
    // Option code
    0x00, 0x2a,
    // Option length
    0x00, 0x03,
    // Option data
    0x00, 0x01, 0x02
};

TEST_F(Rdata_OPT_Test, createFromText) {<--- syntax error
    // OPT RR cannot be created from text.
    EXPECT_THROW(generic::OPT("this does not matter"), InvalidRdataText);
}

TEST_F(Rdata_OPT_Test, createFromWire) {
    // Valid cases: in the simple implementation with no supported options,
    // we can only check these don't throw.
    EXPECT_NO_THROW(rdataFactoryFromFile(RRType::OPT(), RRClass("CLASS4096"),
                                         "rdata_opt_fromWire1"));
    EXPECT_NO_THROW(rdataFactoryFromFile(RRType::OPT(), RRClass::CH(),
                                         "rdata_opt_fromWire1", 2));

    // Short RDLEN. This throws InvalidRdataLength even if subsequent
    // pseudo RRs cause RDLEN size to be exhausted.
    EXPECT_THROW(rdataFactoryFromFile(RRType::OPT(), RRClass::IN(),
                                      "rdata_opt_fromWire2"),
                 InvalidRdataLength);
    EXPECT_THROW(rdataFactoryFromFile(RRType::OPT(), RRClass::IN(),
                                      "rdata_opt_fromWire3"),
                 InvalidRdataLength);
    // Option lengths can add up and overflow RDLEN. Unlikely when
    // parsed from wire data, but we'll check for it anyway.
    EXPECT_THROW(rdataFactoryFromFile(RRType::OPT(), RRClass::IN(),
                                      "rdata_opt_fromWire4"),
                 InvalidRdataText);

    // short buffer case.
    EXPECT_THROW(rdataFactoryFromFile(RRType::OPT(), RRClass::IN(),
                                      "rdata_opt_fromWire1", 11),
                 isc::OutOfRange);
}

TEST_F(Rdata_OPT_Test, createFromLexer) {
    // OPT RR cannot be created from text. Exceptions cause null to be
    // returned.
    EXPECT_FALSE(test::createRdataUsingLexer(RRType::OPT(), RRClass::IN(),
                                             "this does not matter"));
}

TEST_F(Rdata_OPT_Test, toWireBuffer) {
    const generic::OPT rdata_opt =
        dynamic_cast<const generic::OPT&>
        (*rdataFactoryFromFile(RRType("OPT"), RRClass("IN"),
                               "rdata_opt_fromWire1", 2));

    obuffer.clear();
    rdata_opt.toWire(obuffer);

    matchWireData(rdata_opt_wiredata, sizeof(rdata_opt_wiredata),
                  obuffer.getData(), obuffer.getLength());
}

TEST_F(Rdata_OPT_Test, toWireRenderer) {
    const generic::OPT rdata_opt =
        dynamic_cast<const generic::OPT&>
        (*rdataFactoryFromFile(RRType("OPT"), RRClass("IN"),
                               "rdata_opt_fromWire1", 2));

    renderer.clear();
    rdata_opt.toWire(renderer);

    matchWireData(rdata_opt_wiredata, sizeof(rdata_opt_wiredata),
                  renderer.getData(), renderer.getLength());
}

TEST_F(Rdata_OPT_Test, toText) {
    // empty OPT
    const generic::OPT rdata_opt;

    EXPECT_THROW(rdata_opt.toText(),
                 isc::InvalidOperation);
}

TEST_F(Rdata_OPT_Test, compare) {
    // empty OPT
    const generic::OPT rdata_opt;

    EXPECT_THROW(rdata_opt.compare(
                  *rdataFactoryFromFile(RRType::OPT(), RRClass::CH(),
                                        "rdata_opt_fromWire1", 2)),
                 isc::InvalidOperation);

    // comparison attempt between incompatible RR types also results in
    // isc::InvalidOperation.
    EXPECT_THROW(rdata_opt.compare(*RdataTest::rdata_nomatch),
                 isc::InvalidOperation);
}

TEST_F(Rdata_OPT_Test, appendPseudoRR) {
    generic::OPT rdata_opt;

    // Append empty option data
    rdata_opt.appendPseudoRR(0x0042, 0, 0);

    // Append simple option data
    const uint8_t option_data[] = {'H', 'e', 'l', 'l', 'o'};
    rdata_opt.appendPseudoRR(0x0043, option_data, sizeof(option_data));

    // Duplicate option codes are okay.
    rdata_opt.appendPseudoRR(0x0042, option_data, sizeof(option_data));

    // When option length may overflow RDLEN, append should throw.
    const std::vector<uint8_t> buffer((1 << 16) - 1);
    EXPECT_THROW(rdata_opt.appendPseudoRR(0x0044, &buffer[0], buffer.size()),
                 isc::InvalidParameter);

    const uint8_t rdata_opt_wiredata2[] = {
        // OPTION #1
        // ` Option code
        0x00, 0x42,
        // ` Option length
        0x00, 0x00,

        // OPTION #2
        // ` Option code
        0x00, 0x43,
        // ` Option length
        0x00, 0x05,
        // ` Option data
        'H', 'e', 'l', 'l', 'o',

        // OPTION #3
        // ` Option code
        0x00, 0x42,
        // ` Option length
        0x00, 0x05,
        // ` Option data
        'H', 'e', 'l', 'l', 'o'
    };

    obuffer.clear();
    rdata_opt.toWire(obuffer);

    matchWireData(rdata_opt_wiredata2, sizeof(rdata_opt_wiredata2),
                  obuffer.getData(), obuffer.getLength());
}

TEST_F(Rdata_OPT_Test, getPseudoRRs) {
    const generic::OPT rdf =
        dynamic_cast<const generic::OPT&>
        (*rdataFactoryFromFile(RRType("OPT"), RRClass("IN"),
                               "rdata_opt_fromWire1", 2));

    const std::vector<generic::OPT::PseudoRR>& rrs = rdf.getPseudoRRs();
    ASSERT_FALSE(rrs.empty());
    EXPECT_EQ(1, rrs.size());
    EXPECT_EQ(0x2a, rrs.at(0).getCode());
    EXPECT_EQ(3, rrs.at(0).getLength());

    const uint8_t expected_data[] = {0x00, 0x01, 0x02};
    const uint8_t* actual_data = rrs.at(0).getData();
    EXPECT_EQ(0, std::memcmp(expected_data, actual_data,
                             sizeof(expected_data)));
}
}