1 // Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #include <config.h>
8 
9 #include <util/buffer.h>
10 #include <dns/exceptions.h>
11 #include <dns/messagerenderer.h>
12 #include <dns/rdata.h>
13 #include <dns/rdataclass.h>
14 #include <dns/rrclass.h>
15 #include <dns/rrtype.h>
16 
17 #include <gtest/gtest.h>
18 
19 #include <dns/tests/unittest_util.h>
20 #include <dns/tests/rdata_unittest.h>
21 #include <util/unittests/wiredata.h>
22 
23 using namespace std;
24 using namespace isc::dns;
25 using namespace isc::util;
26 using namespace isc::dns::rdata;
27 using namespace isc::dns::rdata::generic;
28 using isc::UnitTestUtil;
29 using isc::util::unittests::matchWireData;
30 
31 namespace {
32 class Rdata_HINFO_Test : public RdataTest {
33 };
34 
35 static uint8_t hinfo_rdata[] = {0x07,0x50,0x65,0x6e,0x74,0x69,0x75,0x6d,0x05,
36     0x4c,0x69,0x6e,0x75,0x78};
37 static const char *hinfo_str = "\"Pentium\" \"Linux\"";
38 static const char *hinfo_str1 = "\"Pen\\\"tium\" \"Linux\"";
39 
40 static const char *hinfo_str_equal = "\"Pentium\"\"Linux\"";
41 static const char *hinfo_str_small1 = "\"Lentium\" \"Linux\"";
42 static const char *hinfo_str_small2 = "\"Pentium\" \"Kinux\"";
43 static const char *hinfo_str_large1 = "\"Qentium\" \"Linux\"";
44 static const char *hinfo_str_large2 = "\"Pentium\" \"UNIX\"";
45 
TEST_F(Rdata_HINFO_Test,createFromText)46 TEST_F(Rdata_HINFO_Test, createFromText) {
47     HINFO hinfo(hinfo_str);
48     EXPECT_EQ(string("Pentium"), hinfo.getCPU());
49     EXPECT_EQ(string("Linux"), hinfo.getOS());
50     // Test the text with double quotes in the middle of string
51     HINFO hinfo1(hinfo_str1);
52     EXPECT_EQ(string("Pen\\\"tium"), hinfo1.getCPU());
53 }
54 
TEST_F(Rdata_HINFO_Test,badText)55 TEST_F(Rdata_HINFO_Test, badText) {
56     // Only 2 fields must exist
57     EXPECT_THROW(const HINFO hinfo("\"Pentium\"\"Linux\"\"Computer\""),
58                  InvalidRdataText);
59     EXPECT_THROW(const HINFO hinfo("\"Pentium\" \"Linux\" \"Computer\""),
60                  InvalidRdataText);
61     // Field cannot be missing
62     EXPECT_THROW(const HINFO hinfo("Pentium"), InvalidRdataText);
63     // The <character-string> cannot exceed 255 characters
64     string hinfo_str;
65     for (int i = 0; i < 257; ++i) {
66         hinfo_str += 'A';
67     }
68     hinfo_str += " Linux";
69     EXPECT_THROW(const HINFO hinfo(hinfo_str), CharStringTooLong);
70 }
71 
TEST_F(Rdata_HINFO_Test,createFromWire)72 TEST_F(Rdata_HINFO_Test, createFromWire) {
73     InputBuffer input_buffer(hinfo_rdata, sizeof(hinfo_rdata));
74     HINFO hinfo(input_buffer, sizeof(hinfo_rdata));
75     EXPECT_EQ(string("Pentium"), hinfo.getCPU());
76     EXPECT_EQ(string("Linux"), hinfo.getOS());
77 }
78 
TEST_F(Rdata_HINFO_Test,createFromLexer)79 TEST_F(Rdata_HINFO_Test, createFromLexer) {
80     HINFO rdata_hinfo(hinfo_str);
81     EXPECT_EQ(0, rdata_hinfo.compare(
82         *test::createRdataUsingLexer(RRType::HINFO(), RRClass::IN(),
83                                      hinfo_str)));
84     EXPECT_EQ(0, rdata_hinfo.compare(
85         *test::createRdataUsingLexer(RRType::HINFO(), RRClass::IN(),
86                                      "\"Pentium\"\"Linux\"")));
87     // Exceptions cause NULL to be returned.
88     EXPECT_FALSE(test::createRdataUsingLexer(RRType::HINFO(), RRClass::IN(),
89                                              "\"Pentium\"\"Linux\""
90                                              "\"Computer\""));
91     EXPECT_FALSE(test::createRdataUsingLexer(RRType::HINFO(), RRClass::IN(),
92                                              "\"Pentium\" \"Linux\" "
93                                              "\"Computer\""));
94     EXPECT_FALSE(test::createRdataUsingLexer(RRType::HINFO(), RRClass::IN(),
95                                              "\"Pentium\""));
96 }
97 
TEST_F(Rdata_HINFO_Test,toText)98 TEST_F(Rdata_HINFO_Test, toText) {
99     HINFO hinfo(hinfo_str);
100     EXPECT_EQ(hinfo_str, hinfo.toText());
101 
102     // will add quotes even if they were not in the original input
103     EXPECT_EQ("\"a\" \"b\"", HINFO("a b").toText());
104     // will not add additional quotes
105     EXPECT_EQ("\"a\" \"b\"", HINFO("\"a\" \"b\"").toText());
106     // And make sure escaped quotes and spaces are left intact
107     EXPECT_EQ("\"a\\\"\" \"b c\"", HINFO("\"a\\\"\" \"b c\"").toText());
108 }
109 
TEST_F(Rdata_HINFO_Test,toWire)110 TEST_F(Rdata_HINFO_Test, toWire) {
111     HINFO hinfo(hinfo_str);
112 
113     hinfo.toWire(obuffer);
114     matchWireData(hinfo_rdata, sizeof (hinfo_rdata),
115                   obuffer.getData(), obuffer.getLength());
116 }
117 
TEST_F(Rdata_HINFO_Test,toWireRenderer)118 TEST_F(Rdata_HINFO_Test, toWireRenderer) {
119     HINFO hinfo(hinfo_str);
120 
121     hinfo.toWire(renderer);
122     matchWireData(hinfo_rdata, sizeof (hinfo_rdata),
123                   renderer.getData(), renderer.getLength());
124 }
125 
TEST_F(Rdata_HINFO_Test,compare)126 TEST_F(Rdata_HINFO_Test, compare) {
127     HINFO hinfo(hinfo_str);
128     HINFO hinfo_small1(hinfo_str_small1);
129     HINFO hinfo_small2(hinfo_str_small2);
130     HINFO hinfo_large1(hinfo_str_large1);
131     HINFO hinfo_large2(hinfo_str_large2);
132 
133     EXPECT_EQ(0, hinfo.compare(HINFO(hinfo_str)));
134     EXPECT_EQ(0, hinfo.compare(HINFO(hinfo_str_equal)));
135     EXPECT_EQ(1, hinfo.compare(HINFO(hinfo_str_small1)));
136     EXPECT_EQ(1, hinfo.compare(HINFO(hinfo_str_small2)));
137     EXPECT_EQ(-1, hinfo.compare(HINFO(hinfo_str_large1)));
138     EXPECT_EQ(-1, hinfo.compare(HINFO(hinfo_str_large2)));
139 }
140 
141 // Copy/assign test
TEST_F(Rdata_HINFO_Test,copy)142 TEST_F(Rdata_HINFO_Test, copy) {
143     HINFO hinfo(hinfo_str);
144     HINFO hinfo2(hinfo);
145     HINFO hinfo3 = hinfo;
146 
147     EXPECT_EQ(0, hinfo.compare(hinfo2));
148     EXPECT_EQ(0, hinfo.compare(hinfo3));
149 
150     hinfo3 = hinfo;
151     EXPECT_EQ(0, hinfo.compare(hinfo3));
152 }
153 
154 }
155