summaryrefslogtreecommitdiffstats
path: root/src/lib/dns/tests/rdata_tlsa_unittest.cc
blob: 92970cb0112d4888492649a11a81448cc0c4da54 (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
// Copyright (C) 2014-2015 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>

#include <boost/algorithm/string.hpp>

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

namespace {
class Rdata_TLSA_Test : public RdataTest {
protected:
        Rdata_TLSA_Test() :
            tlsa_txt("0 0 1 d2abde240d7cd3ee6b4b28c54df034b9"
                     "7983a1d16e8a410e4561cb106618e971"),
            rdata_tlsa(tlsa_txt)
        {}

    void checkFromText_None(const string& rdata_str) {
        checkFromText<generic::TLSA, isc::Exception, isc::Exception>(
            rdata_str, rdata_tlsa, false, false);
    }

    void checkFromText_InvalidText(const string& rdata_str) {
        checkFromText<generic::TLSA, InvalidRdataText, InvalidRdataText>(
            rdata_str, rdata_tlsa, true, true);
    }

    void checkFromText_LexerError(const string& rdata_str) {
        checkFromText
            <generic::TLSA, InvalidRdataText, MasterLexer::LexerError>(
                rdata_str, rdata_tlsa, true, true);
    }

    void checkFromText_BadString(const string& rdata_str) {
        checkFromText
            <generic::TLSA, InvalidRdataText, isc::Exception>(
                rdata_str, rdata_tlsa, true, false);
    }

    const string tlsa_txt;
    const generic::TLSA rdata_tlsa;
};

const uint8_t rdata_tlsa_wiredata[] = {
    // certificate usage
    0x00,
    // selector
    0x00,
    // matching type
    0x01,
    // certificate association data
    0xd2, 0xab, 0xde, 0x24, 0x0d, 0x7c, 0xd3, 0xee,
    0x6b, 0x4b, 0x28, 0xc5, 0x4d, 0xf0, 0x34, 0xb9,
    0x79, 0x83, 0xa1, 0xd1, 0x6e, 0x8a, 0x41, 0x0e,
    0x45, 0x61, 0xcb, 0x10, 0x66, 0x18, 0xe9, 0x71
};

TEST_F(Rdata_TLSA_Test, createFromText) {
    // Basic test
    checkFromText_None(tlsa_txt);

    // With different spacing
    checkFromText_None("0 0 1    d2abde240d7cd3ee6b4b28c54df034b9"
                       "7983a1d16e8a410e4561cb106618e971");

    // Combination of lowercase and uppercase
    checkFromText_None("0 0 1 D2ABDE240D7CD3EE6B4B28C54DF034B9"
                       "7983a1d16e8a410e4561cb106618e971");

    // spacing in the certificate association data field
    checkFromText_None("0 0 1 d2abde240d7cd3ee6b4b28c54df034b9"
                       "      7983a1d16e8a410e4561cb106618e971");

    // multi-line certificate association data field
    checkFromText_None("0 0 1 ( d2abde240d7cd3ee6b4b28c54df034b9\n"
                       "        7983a1d16e8a410e4561cb106618e971 )");

    // string constructor throws if there's extra text,
    // but lexer constructor doesn't
    checkFromText_BadString(tlsa_txt + "\n" + tlsa_txt);
}

TEST_F(Rdata_TLSA_Test, fields) {
    // Some of these may not be RFC conformant, but we relax the check
    // in our code to work with other field values that may show up in
    // the future.
    EXPECT_NO_THROW(const generic::TLSA rdata_tlsa("1 0 1 12ab"));
    EXPECT_NO_THROW(const generic::TLSA rdata_tlsa("2 0 1 12ab"));
    EXPECT_NO_THROW(const generic::TLSA rdata_tlsa("3 0 1 12ab"));
    EXPECT_NO_THROW(const generic::TLSA rdata_tlsa("128 0 1 12ab"));
    EXPECT_NO_THROW(const generic::TLSA rdata_tlsa("255 0 1 12ab"));

    EXPECT_NO_THROW(const generic::TLSA rdata_tlsa("0 1 1 12ab"));
    EXPECT_NO_THROW(const generic::TLSA rdata_tlsa("0 2 1 12ab"));
    EXPECT_NO_THROW(const generic::TLSA rdata_tlsa("0 3 1 12ab"));
    EXPECT_NO_THROW(const generic::TLSA rdata_tlsa("0 128 1 12ab"));
    EXPECT_NO_THROW(const generic::TLSA rdata_tlsa("0 255 1 12ab"));

    EXPECT_NO_THROW(const generic::TLSA rdata_tlsa("0 0 1 12ab"));
    EXPECT_NO_THROW(const generic::TLSA rdata_tlsa("0 0 2 12ab"));
    EXPECT_NO_THROW(const generic::TLSA rdata_tlsa("0 0 3 12ab"));
    EXPECT_NO_THROW(const generic::TLSA rdata_tlsa("0 0 128 12ab"));
    EXPECT_NO_THROW(const generic::TLSA rdata_tlsa("0 0 255 12ab"));

    // > 255 would be broken
    EXPECT_THROW(const generic::TLSA rdata_tlsa("256 0 1 12ab"),
                 InvalidRdataText);
    EXPECT_THROW(const generic::TLSA rdata_tlsa("0 256 1 12ab"),
                 InvalidRdataText);
    EXPECT_THROW(const generic::TLSA rdata_tlsa("0 0 256 12ab"),
                 InvalidRdataText);
}

TEST_F(Rdata_TLSA_Test, badText) {
    checkFromText_LexerError("1");
    checkFromText_LexerError("ONE 2 3 123456789abcdef67890123456789abcdef67890");
    checkFromText_LexerError("1 TWO 3 123456789abcdef67890123456789abcdef67890");
    checkFromText_LexerError("1 2 THREE 123456789abcdef67890123456789abcdef67890");
    checkFromText_InvalidText("1 2 3 BAABAABLACKSHEEP");
    checkFromText_InvalidText(tlsa_txt + " extra text");

    // yes, these are redundant to the last test cases in the .fields
    // test
    checkFromText_InvalidText(
        "2345 1 2 123456789abcdef67890123456789abcdef67890");
    checkFromText_InvalidText(
        "3 1234 4 123456789abcdef67890123456789abcdef67890");
    checkFromText_InvalidText(
        "5 6 1234 123456789abcdef67890123456789abcdef67890");

    // negative values are trapped in the lexer rather than the
    // constructor
    checkFromText_LexerError("-2 0 1 123456789abcdef67890123456789abcdef67890");
    checkFromText_LexerError("0 -2 1 123456789abcdef67890123456789abcdef67890");
    checkFromText_LexerError("0 0 -2 123456789abcdef67890123456789abcdef67890");
}

TEST_F(Rdata_TLSA_Test, copyAndAssign) {
    // Copy construct
    generic::TLSA rdata_tlsa2(rdata_tlsa);
    EXPECT_EQ(0, rdata_tlsa.compare(rdata_tlsa2));

    // Assignment, mainly to confirm it doesn't cause disruption.
    rdata_tlsa2 = rdata_tlsa;
    EXPECT_EQ(0, rdata_tlsa.compare(rdata_tlsa2));
}

TEST_F(Rdata_TLSA_Test, createFromWire) {
    // Basic test
    EXPECT_EQ(0, rdata_tlsa.compare(
                  *rdataFactoryFromFile(RRType("TLSA"), RRClass("IN"),
                                        "rdata_tlsa_fromWire")));
    // Combination of lowercase and uppercase
    EXPECT_EQ(0, rdata_tlsa.compare(
                  *rdataFactoryFromFile(RRType("TLSA"), RRClass("IN"),
                                        "rdata_tlsa_fromWire2")));
    // certificate_usage=0, selector=0, matching_type=1
    EXPECT_NO_THROW(rdataFactoryFromFile(RRType("TLSA"), RRClass("IN"),
                                         "rdata_tlsa_fromWire3.wire"));

    // certificate_usage=255, selector=0, matching_type=1
    EXPECT_NO_THROW(rdataFactoryFromFile(RRType("TLSA"), RRClass("IN"),
                                         "rdata_tlsa_fromWire4.wire"));

    // certificate_usage=0, selector=255, matching_type=1
    EXPECT_NO_THROW(rdataFactoryFromFile(RRType("TLSA"), RRClass("IN"),
                                         "rdata_tlsa_fromWire5.wire"));

    // certificate_usage=0, selector=0, matching_type=255
    EXPECT_NO_THROW(rdataFactoryFromFile(RRType("TLSA"), RRClass("IN"),
                                         "rdata_tlsa_fromWire6.wire"));

    // certificate_usage=3, selector=1, matching_type=2
    EXPECT_NO_THROW(rdataFactoryFromFile(RRType("TLSA"), RRClass("IN"),
                                         "rdata_tlsa_fromWire7.wire"));

    // short certificate association data
    EXPECT_NO_THROW(rdataFactoryFromFile(RRType("TLSA"), RRClass("IN"),
                                         "rdata_tlsa_fromWire8.wire"));

    // certificate association data is shorter than rdata len
    EXPECT_THROW(rdataFactoryFromFile(RRType("TLSA"), RRClass("IN"),
                                      "rdata_tlsa_fromWire9"),
                 InvalidBufferPosition);

    // certificate association data is missing
    EXPECT_THROW(rdataFactoryFromFile(RRType("TLSA"), RRClass("IN"),
                                      "rdata_tlsa_fromWire10"),
                 InvalidBufferPosition);

    // certificate association data is empty
    EXPECT_THROW(rdataFactoryFromFile(RRType("TLSA"), RRClass("IN"),
                                      "rdata_tlsa_fromWire12"),
                 InvalidRdataLength);

    // all RDATA is missing
    EXPECT_THROW(rdataFactoryFromFile(RRType("TLSA"), RRClass("IN"),
                                      "rdata_tlsa_fromWire11"),
                 InvalidBufferPosition);
}

TEST_F(Rdata_TLSA_Test, createFromParams) {
    const generic::TLSA rdata_tlsa2(
        0, 0, 1, "d2abde240d7cd3ee6b4b28c54df034b9"
                 "7983a1d16e8a410e4561cb106618e971");
    EXPECT_EQ(0, rdata_tlsa2.compare(rdata_tlsa));

    // empty certificate association data should throw
    EXPECT_THROW(const generic::TLSA rdata_tlsa2(0, 0, 1, ""),
                 InvalidRdataText);
}

TEST_F(Rdata_TLSA_Test, toText) {
    EXPECT_TRUE(boost::iequals(tlsa_txt, rdata_tlsa.toText()));
}

TEST_F(Rdata_TLSA_Test, toWire) {
    this->obuffer.clear();
    rdata_tlsa.toWire(this->obuffer);

    EXPECT_EQ(sizeof (rdata_tlsa_wiredata),
              this->obuffer.getLength());

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

TEST_F(Rdata_TLSA_Test, compare) {
    const generic::TLSA rdata_tlsa2("0 0 0 d2abde240d7cd3ee6b4b28c54df034b9"
                                    "7983a1d16e8a410e4561cb106618e971");
    EXPECT_EQ(-1, rdata_tlsa2.compare(rdata_tlsa));
    EXPECT_EQ(1, rdata_tlsa.compare(rdata_tlsa2));
}

TEST_F(Rdata_TLSA_Test, getCertificateUsage) {
    EXPECT_EQ(0, rdata_tlsa.getCertificateUsage());
}

TEST_F(Rdata_TLSA_Test, getSelector) {
    EXPECT_EQ(0, rdata_tlsa.getSelector());
}

TEST_F(Rdata_TLSA_Test, getMatchingType) {
    EXPECT_EQ(1, rdata_tlsa.getMatchingType());
}

TEST_F(Rdata_TLSA_Test, getDataLength) {
    EXPECT_EQ(32, rdata_tlsa.getDataLength());
}
}