rrttl_unittest.cc 9.27 KB
Newer Older
JINMEI Tatuya's avatar
JINMEI Tatuya committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (C) 2010  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 <gtest/gtest.h>

17
#include <util/buffer.h>
18 19
#include <dns/messagerenderer.h>
#include <dns/rrttl.h>
JINMEI Tatuya's avatar
JINMEI Tatuya committed
20

21
#include <dns/tests/unittest_util.h>
JINMEI Tatuya's avatar
JINMEI Tatuya committed
22

23 24
#include <boost/scoped_ptr.hpp>

JINMEI Tatuya's avatar
JINMEI Tatuya committed
25 26 27
using namespace std;
using namespace isc;
using namespace isc::dns;
28
using namespace isc::util;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
29 30 31 32

namespace {
class RRTTLTest : public ::testing::Test {
protected:
33
    RRTTLTest() : obuffer(0) {}
JINMEI Tatuya's avatar
JINMEI Tatuya committed
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

    OutputBuffer obuffer;
    MessageRenderer renderer;

    static RRTTL rrttlFactoryFromWire(const char* datafile);
    static const RRTTL ttl_0, ttl_1h, ttl_1d, ttl_32bit, ttl_max;
    static const RRTTL ttl_small, ttl_large;
    static const uint8_t wiredata[20];
};

const RRTTL RRTTLTest::ttl_0(0);
const RRTTL RRTTLTest::ttl_1h(3600);
const RRTTL RRTTLTest::ttl_1d(86400);
const RRTTL RRTTLTest::ttl_32bit(0x12345678);
const RRTTL RRTTLTest::ttl_max(0xffffffff);

const RRTTL RRTTLTest::ttl_small(1);
const RRTTL RRTTLTest::ttl_large(0x80000001);
// This is wire-format data for the above sample RRTTLs rendered in the
// appearing order.
const uint8_t RRTTLTest::wiredata[20] = { 0x00, 0x00, 0x00, 0x00,
                                           0x00, 0x00, 0x0e, 0x10,
                                           0x00, 0x01, 0x51, 0x80,
                                           0x12, 0x34, 0x56, 0x78,
                                           0xff, 0xff, 0xff, 0xff };

RRTTL
61
RRTTLTest::rrttlFactoryFromWire(const char* datafile) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
62 63 64 65 66 67 68 69
    std::vector<unsigned char> data;
    UnitTestUtil::readWireData(datafile, data);

    InputBuffer buffer(&data[0], data.size());

    return (RRTTL(buffer));
}

Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
70
TEST_F(RRTTLTest, getValue) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
71 72 73 74 75
    EXPECT_EQ(0, ttl_0.getValue());
    EXPECT_EQ(3600, ttl_1h.getValue());
    EXPECT_EQ(86400, ttl_1d.getValue());
    EXPECT_EQ(0x12345678, ttl_32bit.getValue());
    EXPECT_EQ(0xffffffff, ttl_max.getValue());
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
76
}
JINMEI Tatuya's avatar
JINMEI Tatuya committed
77

Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
78
TEST_F(RRTTLTest, fromText) {
79 80
    // Border cases
    EXPECT_EQ(0, RRTTL("0").getValue());
81
    EXPECT_EQ(4294967295U, RRTTL("4294967295").getValue());
82 83

    // Invalid cases
JINMEI Tatuya's avatar
JINMEI Tatuya committed
84 85 86 87 88 89
    EXPECT_THROW(RRTTL("0xdeadbeef"), InvalidRRTTL); // must be decimal
    EXPECT_THROW(RRTTL("-1"), InvalidRRTTL); // must be positive
    EXPECT_THROW(RRTTL("1.1"), InvalidRRTTL); // must be integer
    EXPECT_THROW(RRTTL("4294967296"), InvalidRRTTL); // must be 32-bit
}

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
TEST_F(RRTTLTest, createFromText) {
    // If placeholder is NULL, a new RRTTL object is allocated
    boost::scoped_ptr<RRTTL> ttl_ptr;
    ttl_ptr.reset(RRTTL::createFromText("3600", NULL));
    ASSERT_TRUE(ttl_ptr);
    EXPECT_EQ(RRTTL(3600), *ttl_ptr);

    // If placeholder is non NULL, it will be overwritten
    RRTTL ttl(3600);
    EXPECT_NE(static_cast<RRTTL*>(NULL), RRTTL::createFromText("1800", &ttl));
    EXPECT_EQ(RRTTL(1800), ttl);

    // If text parsing fails, NULL is returned; if placeholder is given,
    // it will be intact.
    EXPECT_EQ(static_cast<RRTTL*>(NULL), RRTTL::createFromText("bad", NULL));
    EXPECT_EQ(static_cast<RRTTL*>(NULL), RRTTL::createFromText("bad", &ttl));
    EXPECT_EQ(RRTTL(1800), ttl);
}

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
void
checkUnit(unsigned multiply, char suffix) {
    SCOPED_TRACE(string("Unit check with suffix ") + suffix);
    const uint32_t value = 10 * multiply;
    const string num = "10";
    // Check both lower and upper version of the suffix
    EXPECT_EQ(value,
              RRTTL(num + static_cast<char>(tolower(suffix))).getValue());
    EXPECT_EQ(value,
              RRTTL(num + static_cast<char>(toupper(suffix))).getValue());
}

// Check parsing the unit form (1D, etc)
TEST_F(RRTTLTest, fromTextUnit) {
    // Check each of the units separately
    checkUnit(1, 'S');
    checkUnit(60, 'M');
    checkUnit(60 * 60, 'H');
    checkUnit(24 * 60 * 60, 'D');
    checkUnit(7 * 24 * 60 * 60, 'W');

130
    // Some border cases (with units)
131
    EXPECT_EQ(4294967295U, RRTTL("4294967295S").getValue());
132
    EXPECT_EQ(0, RRTTL("0W0D0H0M0S").getValue());
133
    EXPECT_EQ(4294967295U, RRTTL("1193046H1695S").getValue());
134
    // Leading zeroes are accepted
135
    EXPECT_EQ(4294967295U, RRTTL("0000000000000004294967295S").getValue());
136

137
    // Now some compound ones. We allow any order (it would be much work to
138
    // check the order anyway).
139 140
    EXPECT_EQ(60 * 60 + 3, RRTTL("1H3S").getValue());

141 142 143
    // Awkward, but allowed case - the same unit used twice.
    EXPECT_EQ(20 * 3600, RRTTL("12H8H").getValue());

144 145 146 147 148 149 150 151 152 153 154
    // Negative number in part of the expression, but the total is positive.
    // Rejected.
    EXPECT_THROW(RRTTL("-1S1H"), InvalidRRTTL);

    // Some things out of range in the ttl, but it wraps to number in range
    // in int64_t. Should still not get fooled and reject it.

    // First part out of range
    EXPECT_THROW(RRTTL("9223372036854775807S9223372036854775807S2S"),
                 InvalidRRTTL);
    // Second part out of range, but it immediately wraps (2S+2^64-2S)
155
    EXPECT_THROW(RRTTL("2S18446744073709551614S"), InvalidRRTTL);
156
    // The whole thing wraps right away (2^64S)
157 158 159
    EXPECT_THROW(RRTTL("18446744073709551616S"), InvalidRRTTL);
    // Second part out of range, and will become negative with the unit,
    EXPECT_THROW(RRTTL("256S307445734561825856M"), InvalidRRTTL);
160

161 162 163 164 165 166
    // Missing before unit.
    EXPECT_THROW(RRTTL("W5H"), InvalidRRTTL);
    EXPECT_THROW(RRTTL("5hW"), InvalidRRTTL);

    // Empty string is not allowed
    EXPECT_THROW(RRTTL(""), InvalidRRTTL);
167 168
    // Missing the last unit is not allowed
    EXPECT_THROW(RRTTL("3D5"), InvalidRRTTL);
169 170 171 172 173 174

    // There are some wrong units
    EXPECT_THROW(RRTTL("13X"), InvalidRRTTL);
    EXPECT_THROW(RRTTL("3D5F"), InvalidRRTTL);
}

175
TEST_F(RRTTLTest, fromWire) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
176
    EXPECT_EQ(0x12345678,
177 178
              rrttlFactoryFromWire("rrcode32_fromWire1").getValue());
    EXPECT_THROW(rrttlFactoryFromWire("rrcode32_fromWire2"),
JINMEI Tatuya's avatar
JINMEI Tatuya committed
179 180 181
                 IncompleteRRTTL);
}

182
TEST_F(RRTTLTest, toText) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
183 184 185 186 187 188 189
    EXPECT_EQ("0", ttl_0.toText());
    EXPECT_EQ("3600", ttl_1h.toText());
    EXPECT_EQ("86400", ttl_1d.toText());
    EXPECT_EQ("305419896", ttl_32bit.toText());
    EXPECT_EQ("4294967295", ttl_max.toText());
}

190
TEST_F(RRTTLTest, toWireBuffer) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
191 192 193 194 195 196 197 198 199 200 201
    ttl_0.toWire(obuffer);
    ttl_1h.toWire(obuffer);
    ttl_1d.toWire(obuffer);
    ttl_32bit.toWire(obuffer);
    ttl_max.toWire(obuffer);

    EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
                        obuffer.getData(), obuffer.getLength(),
                        wiredata, sizeof(wiredata));
}

202
TEST_F(RRTTLTest, toWireRenderer) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
203 204 205 206 207 208 209
    ttl_0.toWire(renderer);
    ttl_1h.toWire(renderer);
    ttl_1d.toWire(renderer);
    ttl_32bit.toWire(renderer);
    ttl_max.toWire(renderer);

    EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
210
                        renderer.getData(), renderer.getLength(),
JINMEI Tatuya's avatar
JINMEI Tatuya committed
211 212 213
                        wiredata, sizeof(wiredata));
}

214
TEST_F(RRTTLTest, equal) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
215 216 217 218 219 220 221 222 223 224 225 226
    EXPECT_TRUE(RRTTL("3600") == ttl_1h);
    EXPECT_TRUE(RRTTL("86400").equals(ttl_1d));

    EXPECT_TRUE(ttl_1d != ttl_1h);
    EXPECT_TRUE(ttl_1d.nequals(ttl_max));
}

//
// The following set of tests confirm the result of <=, <, >=, >
// The test logic is simple, and all tests are just straightforward variations
// of the first one.
//
227
TEST_F(RRTTLTest, leq) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
228 229 230 231 232 233
    // small <= large is true
    EXPECT_TRUE(ttl_small.leq(ttl_large));
    EXPECT_TRUE(ttl_small <= ttl_large);

    // small <= small is true
    EXPECT_TRUE(ttl_small.leq(ttl_small));
234
    EXPECT_LE(ttl_small, ttl_small);
JINMEI Tatuya's avatar
JINMEI Tatuya committed
235 236 237 238 239 240

    // large <= small is false
    EXPECT_FALSE(ttl_large.leq(ttl_small));
    EXPECT_FALSE(ttl_large <= ttl_small);
}

241
TEST_F(RRTTLTest, geq) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
242 243 244 245
    EXPECT_TRUE(ttl_large.geq(ttl_small));
    EXPECT_TRUE(ttl_large >= ttl_small);

    EXPECT_TRUE(ttl_large.geq(ttl_large));
246
    EXPECT_GE(ttl_large, ttl_large);
JINMEI Tatuya's avatar
JINMEI Tatuya committed
247 248 249 250 251

    EXPECT_FALSE(ttl_small.geq(ttl_large));
    EXPECT_FALSE(ttl_small >= ttl_large);
}

252
TEST_F(RRTTLTest, lthan) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
253 254 255 256
    EXPECT_TRUE(ttl_small.lthan(ttl_large));
    EXPECT_TRUE(ttl_small < ttl_large);

    EXPECT_FALSE(ttl_small.lthan(ttl_small));
257
    // cppcheck-suppress duplicateExpression
JINMEI Tatuya's avatar
JINMEI Tatuya committed
258 259 260 261 262 263
    EXPECT_FALSE(ttl_small < ttl_small);

    EXPECT_FALSE(ttl_large.lthan(ttl_small));
    EXPECT_FALSE(ttl_large < ttl_small);
}

264
TEST_F(RRTTLTest, gthan) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
265 266 267 268
    EXPECT_TRUE(ttl_large.gthan(ttl_small));
    EXPECT_TRUE(ttl_large > ttl_small);

    EXPECT_FALSE(ttl_large.gthan(ttl_large));
269
    // cppcheck-suppress duplicateExpression
JINMEI Tatuya's avatar
JINMEI Tatuya committed
270 271 272 273 274 275
    EXPECT_FALSE(ttl_large > ttl_large);

    EXPECT_FALSE(ttl_small.gthan(ttl_large));
    EXPECT_FALSE(ttl_small > ttl_large);
}

276
TEST_F(RRTTLTest, maxTTL) {
277
    EXPECT_EQ((1 << 31) - 1, RRTTL::MAX().getValue());
278 279
}

JINMEI Tatuya's avatar
JINMEI Tatuya committed
280
// test operator<<.  We simply confirm it appends the result of toText().
281
TEST_F(RRTTLTest, LeftShiftOperator) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
282 283 284 285 286
    ostringstream oss;
    oss << ttl_1h;
    EXPECT_EQ(ttl_1h.toText(), oss.str());
}
}