summaryrefslogtreecommitdiffstats
path: root/src/lib/dns/tests/rdata_ds_like_unittest.cc
blob: d4ea924020aa5b36fc0b994f746178f036628800 (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
// Copyright (C) 2011-2019 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 <algorithm>
#include <string>

#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 <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 {
// hacks to make templates work
template <class T>
class RRTYPE : public RRType {
public:
    RRTYPE();
};

template<> RRTYPE<generic::DS>::RRTYPE() : RRType(RRType::DS()) {}
template<> RRTYPE<generic::DLV>::RRTYPE() : RRType(RRType::DLV()) {}

template <class DS_LIKE>
class Rdata_DS_LIKE_Test : public RdataTest {
protected:
    Rdata_DS_LIKE_Test() :
        ds_like_txt("12892 5 2 F1E184C0E1D615D20EB3C223ACED3B03C773DD952D"
                    "5F0EB5C777586DE18DA6B5"),
        rdata_ds_like(ds_like_txt)
    {}
    const string ds_like_txt;
    const DS_LIKE rdata_ds_like;
};

// The list of types we want to test.
typedef testing::Types<generic::DS, generic::DLV> Implementations;

#ifdef TYPED_TEST_SUITE
TYPED_TEST_SUITE(Rdata_DS_LIKE_Test, Implementations);
#else
TYPED_TEST_CASE(Rdata_DS_LIKE_Test, Implementations);
#endif

TYPED_TEST(Rdata_DS_LIKE_Test, createFromText) {
    // It's valid for the digest's presentation format to contain
    // spaces. See RFC4034 section 5.3.
    EXPECT_EQ(0, this->rdata_ds_like.compare(
        TypeParam("12892 5 2 F1E184C0E1D615D20EB3C223ACED3B03C773DD952D5F0EB5"
                  "C777 58 6DE18  \t DA6B5")));
}

TYPED_TEST(Rdata_DS_LIKE_Test, toText_DS_LIKE) {
    EXPECT_EQ(this->ds_like_txt, this->rdata_ds_like.toText());
}

TYPED_TEST(Rdata_DS_LIKE_Test, badText_DS_LIKE) {
    EXPECT_THROW(const TypeParam ds_like2("99999 5 2 BEEF"), InvalidRdataText);
    EXPECT_THROW(const TypeParam ds_like2("11111 555 2 BEEF"),
                 InvalidRdataText);
    EXPECT_THROW(const TypeParam ds_like2("11111 5 22222 BEEF"),
                 InvalidRdataText);
    EXPECT_THROW(const TypeParam ds_like2("11111 5 2"), InvalidRdataText);
    EXPECT_THROW(const TypeParam ds_like2("GARBAGE IN"), InvalidRdataText);
    // no space between the digest type and the digest.
    EXPECT_THROW(const TypeParam ds_like2(
                     "12892 5 2F1E184C0E1D615D20EB3C223ACED3B03C773DD952D"
                     "5F0EB5C777586DE18DA6B5"), InvalidRdataText);
}

TYPED_TEST(Rdata_DS_LIKE_Test, createFromWire_DS_LIKE) {
    EXPECT_EQ(0, this->rdata_ds_like.compare(
              *this->rdataFactoryFromFile(RRTYPE<TypeParam>(), RRClass::IN(),
                                          "rdata_ds_fromWire")));
}

TYPED_TEST(Rdata_DS_LIKE_Test, createFromLexer_DS_LIKE) {
    EXPECT_EQ(0, this->rdata_ds_like.compare(
        *test::createRdataUsingLexer(RRTYPE<TypeParam>(), RRClass::IN(),
                                     this->ds_like_txt)));

    // Whitespace is okay
    EXPECT_EQ(0, this->rdata_ds_like.compare(
        *test::createRdataUsingLexer(RRTYPE<TypeParam>(), RRClass::IN(),
                                     "12892 5 2 F1E184C0E1D615D20EB3C223ACED3B0"
                                     "3C773DD952D5F0EB5C777 58 6DE18  \t DA6B5"
             )));

    // Exceptions cause NULL to be returned.

    // Bad tag
    EXPECT_FALSE(test::createRdataUsingLexer(RRTYPE<TypeParam>(), RRClass::IN(),
                                             "65536 5 2 BEEF"));

    // Bad algorithm
    EXPECT_FALSE(test::createRdataUsingLexer(RRTYPE<TypeParam>(), RRClass::IN(),
                                             "1024 256 2 BEEF"));

    // Bad digest type
    EXPECT_FALSE(test::createRdataUsingLexer(RRTYPE<TypeParam>(), RRClass::IN(),
                                             "2048 2 256 BEEF"));
}

TYPED_TEST(Rdata_DS_LIKE_Test, assignment_DS_LIKE) {
    TypeParam copy(this->ds_like_txt);
    copy = this->rdata_ds_like;
    EXPECT_EQ(0, copy.compare(this->rdata_ds_like));

    // Check if the copied data is valid even after the original is deleted
    TypeParam* copy2 = new TypeParam(this->rdata_ds_like);
    TypeParam copy3(this->ds_like_txt);
    copy3 = *copy2;
    delete copy2;
    EXPECT_EQ(0, copy3.compare(this->rdata_ds_like));

    // Self assignment
    copy = *&copy;
    EXPECT_EQ(0, copy.compare(this->rdata_ds_like));
}

TYPED_TEST(Rdata_DS_LIKE_Test, getTag_DS_LIKE) {
    EXPECT_EQ(12892, this->rdata_ds_like.getTag());
}

TYPED_TEST(Rdata_DS_LIKE_Test, toWireRenderer) {
    Rdata_DS_LIKE_Test<TypeParam>::renderer.skip(2);
    TypeParam rdata_ds_like(this->ds_like_txt);
    rdata_ds_like.toWire(this->renderer);

    vector<unsigned char> data;
    UnitTestUtil::readWireData("rdata_ds_fromWire", data);
    matchWireData(&data[2], data.size() - 2,
                  static_cast<const uint8_t*>(this->renderer.getData()) + 2,
                  this->renderer.getLength() - 2);
}

TYPED_TEST(Rdata_DS_LIKE_Test, toWireBuffer) {
    TypeParam rdata_ds_like(this->ds_like_txt);
    rdata_ds_like.toWire(this->obuffer);
}

string ds_like_txt1("12892 5 2 F1E184C0E1D615D20EB3C223ACED3B03C773DD952D"
                   "5F0EB5C777586DE18DA6B5");
// different tag
string ds_like_txt2("12893 5 2 F1E184C0E1D615D20EB3C223ACED3B03C773DD952D"
                   "5F0EB5C777586DE18DA6B5");
// different algorithm
string ds_like_txt3("12892 6 2 F1E184C0E1D615D20EB3C223ACED3B03C773DD952D"
                   "5F0EB5C777586DE18DA6B5");
// different digest type
string ds_like_txt4("12892 5 3 F1E184C0E1D615D20EB3C223ACED3B03C773DD952D"
                   "5F0EB5C777586DE18DA6B5");
// different digest
string ds_like_txt5("12892 5 2 F2E184C0E1D615D20EB3C223ACED3B03C773DD952D"
                   "5F0EB5C777586DE18DA6B5");
// different digest length
string ds_like_txt6("12892 5 2 F2E184C0E1D615D20EB3C223ACED3B03C773DD952D"
                   "5F0EB5C777586DE18DA6B555");

TYPED_TEST(Rdata_DS_LIKE_Test, compare) {
    const string ds_like_txt1(
        "12892 5 2 F1E184C0E1D615D20EB3C223ACED3B03C773DD952D"
        "5F0EB5C777586DE18DA6B5");
    // different tag
    const string ds_like_txt2(
        "12893 5 2 F1E184C0E1D615D20EB3C223ACED3B03C773DD952D"
        "5F0EB5C777586DE18DA6B5");
    // different algorithm
    const string ds_like_txt3(
        "12892 6 2 F1E184C0E1D615D20EB3C223ACED3B03C773DD952D"
        "5F0EB5C777586DE18DA6B5");
    // different digest type
    const string ds_like_txt4(
        "12892 5 3 F1E184C0E1D615D20EB3C223ACED3B03C773DD952D"
        "5F0EB5C777586DE18DA6B5");
    // different digest
    const string ds_like_txt5(
        "12892 5 2 F2E184C0E1D615D20EB3C223ACED3B03C773DD952D"
        "5F0EB5C777586DE18DA6B5");
    // different digest length
    const string ds_like_txt6(
        "12892 5 2 F2E184C0E1D615D20EB3C223ACED3B03C773DD952D"
        "5F0EB5C777586DE18DA6B555");

    // trivial case: self equivalence
    EXPECT_EQ(0, TypeParam(this->ds_like_txt).
              compare(TypeParam(this->ds_like_txt)));

    // non-equivalence tests
    EXPECT_LT(TypeParam(ds_like_txt1).compare(TypeParam(ds_like_txt2)), 0);
    EXPECT_GT(TypeParam(ds_like_txt2).compare(TypeParam(ds_like_txt1)), 0);

    EXPECT_LT(TypeParam(ds_like_txt1).compare(TypeParam(ds_like_txt3)), 0);
    EXPECT_GT(TypeParam(ds_like_txt3).compare(TypeParam(ds_like_txt1)), 0);

    EXPECT_LT(TypeParam(ds_like_txt1).compare(TypeParam(ds_like_txt4)), 0);
    EXPECT_GT(TypeParam(ds_like_txt4).compare(TypeParam(ds_like_txt1)), 0);

    EXPECT_LT(TypeParam(ds_like_txt1).compare(TypeParam(ds_like_txt5)), 0);
    EXPECT_GT(TypeParam(ds_like_txt5).compare(TypeParam(ds_like_txt1)), 0);

    EXPECT_LT(TypeParam(ds_like_txt1).compare(TypeParam(ds_like_txt6)), 0);
    EXPECT_GT(TypeParam(ds_like_txt6).compare(TypeParam(ds_like_txt1)), 0);

    // comparison attempt between incompatible RR types should be rejected
    EXPECT_THROW(this->rdata_ds_like.compare(*this->rdata_nomatch),
                 bad_cast);
}

}