summaryrefslogtreecommitdiffstats
path: root/src/lib/dns/tests/rdata_hinfo_unittest.cc
blob: 887848e2b2fec94c434740386cc74ab1ab0f05cb (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
// Copyright (C) 2011  Internet Systems Consortium, Inc. ("ISC")
//
// 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 <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 <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 namespace isc::dns::rdata::generic;
using isc::UnitTestUtil;
using isc::util::unittests::matchWireData;

namespace {
class Rdata_HINFO_Test : public RdataTest {
};

static uint8_t hinfo_rdata[] = {0x07,0x50,0x65,0x6e,0x74,0x69,0x75,0x6d,0x05,
    0x4c,0x69,0x6e,0x75,0x78};
static const char *hinfo_str = "\"Pentium\" \"Linux\"";
static const char *hinfo_str1 = "\"Pen\\\"tium\" \"Linux\"";

static const char *hinfo_str_equal = "\"Pentium\"\"Linux\"";
static const char *hinfo_str_small1 = "\"Lentium\" \"Linux\"";
static const char *hinfo_str_small2 = "\"Pentium\" \"Kinux\"";
static const char *hinfo_str_large1 = "\"Qentium\" \"Linux\"";
static const char *hinfo_str_large2 = "\"Pentium\" \"UNIX\"";

TEST_F(Rdata_HINFO_Test, createFromText) {
    HINFO hinfo(hinfo_str);
    EXPECT_EQ(string("Pentium"), hinfo.getCPU());
    EXPECT_EQ(string("Linux"), hinfo.getOS());
    // Test the text with double quotes in the middle of string
    HINFO hinfo1(hinfo_str1);
    EXPECT_EQ(string("Pen\\\"tium"), hinfo1.getCPU());
}

TEST_F(Rdata_HINFO_Test, badText) {
    // Only 2 fields must exist
    EXPECT_THROW(const HINFO hinfo("\"Pentium\"\"Linux\"\"Computer\""),
                 InvalidRdataText);
    EXPECT_THROW(const HINFO hinfo("\"Pentium\" \"Linux\" \"Computer\""),
                 InvalidRdataText);
    // Field cannot be missing
    EXPECT_THROW(const HINFO hinfo("Pentium"), InvalidRdataText);
    // The <character-string> cannot exceed 255 characters
    string hinfo_str;
    for (int i = 0; i < 257; ++i) {
        hinfo_str += 'A';
    }
    hinfo_str += " Linux";
    EXPECT_THROW(const HINFO hinfo(hinfo_str), CharStringTooLong);
}

TEST_F(Rdata_HINFO_Test, createFromWire) {
    InputBuffer input_buffer(hinfo_rdata, sizeof(hinfo_rdata));
    HINFO hinfo(input_buffer, sizeof(hinfo_rdata));
    EXPECT_EQ(string("Pentium"), hinfo.getCPU());
    EXPECT_EQ(string("Linux"), hinfo.getOS());
}

TEST_F(Rdata_HINFO_Test, createFromLexer) {
    HINFO rdata_hinfo(hinfo_str);
    EXPECT_EQ(0, rdata_hinfo.compare(
        *test::createRdataUsingLexer(RRType::HINFO(), RRClass::IN(),
                                     hinfo_str)));
    EXPECT_EQ(0, rdata_hinfo.compare(
        *test::createRdataUsingLexer(RRType::HINFO(), RRClass::IN(),
                                     "\"Pentium\"\"Linux\"")));
    // Exceptions cause NULL to be returned.
    EXPECT_FALSE(test::createRdataUsingLexer(RRType::HINFO(), RRClass::IN(),
                                             "\"Pentium\"\"Linux\""
                                             "\"Computer\""));
    EXPECT_FALSE(test::createRdataUsingLexer(RRType::HINFO(), RRClass::IN(),
                                             "\"Pentium\" \"Linux\" "
                                             "\"Computer\""));
    EXPECT_FALSE(test::createRdataUsingLexer(RRType::HINFO(), RRClass::IN(),
                                             "\"Pentium\""));
}

TEST_F(Rdata_HINFO_Test, toText) {
    HINFO hinfo(hinfo_str);
    EXPECT_EQ(hinfo_str, hinfo.toText());

    // will add quotes even if they were not in the original input
    EXPECT_EQ("\"a\" \"b\"", HINFO("a b").toText());
    // will not add additional quotes
    EXPECT_EQ("\"a\" \"b\"", HINFO("\"a\" \"b\"").toText());
    // And make sure escaped quotes and spaces are left intact
    EXPECT_EQ("\"a\\\"\" \"b c\"", HINFO("\"a\\\"\" \"b c\"").toText());
}

TEST_F(Rdata_HINFO_Test, toWire) {
    HINFO hinfo(hinfo_str);

    hinfo.toWire(obuffer);
    matchWireData(hinfo_rdata, sizeof (hinfo_rdata),
                  obuffer.getData(), obuffer.getLength());
}

TEST_F(Rdata_HINFO_Test, toWireRenderer) {
    HINFO hinfo(hinfo_str);

    hinfo.toWire(renderer);
    matchWireData(hinfo_rdata, sizeof (hinfo_rdata),
                  renderer.getData(), renderer.getLength());
}

TEST_F(Rdata_HINFO_Test, compare) {
    HINFO hinfo(hinfo_str);
    HINFO hinfo_small1(hinfo_str_small1);
    HINFO hinfo_small2(hinfo_str_small2);
    HINFO hinfo_large1(hinfo_str_large1);
    HINFO hinfo_large2(hinfo_str_large2);

    EXPECT_EQ(0, hinfo.compare(HINFO(hinfo_str)));
    EXPECT_EQ(0, hinfo.compare(HINFO(hinfo_str_equal)));
    EXPECT_EQ(1, hinfo.compare(HINFO(hinfo_str_small1)));
    EXPECT_EQ(1, hinfo.compare(HINFO(hinfo_str_small2)));
    EXPECT_EQ(-1, hinfo.compare(HINFO(hinfo_str_large1)));
    EXPECT_EQ(-1, hinfo.compare(HINFO(hinfo_str_large2)));
}

// Copy/assign test
TEST_F(Rdata_HINFO_Test, copy) {
    HINFO hinfo(hinfo_str);
    HINFO hinfo2(hinfo);
    HINFO hinfo3 = hinfo;

    EXPECT_EQ(0, hinfo.compare(hinfo2));
    EXPECT_EQ(0, hinfo.compare(hinfo3));

    hinfo3 = hinfo;
    EXPECT_EQ(0, hinfo.compare(hinfo3));
}

}