From: Michal 'vorner' Vaner Date: Mon, 5 Nov 2012 14:39:21 +0000 (+0100) Subject: [2384] Tests for units in TTL text X-Git-Tag: trac2487_base~1^2~29^2~2^2~21 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=91cac5e55e2733b249d0ca956e8522e8f28688d8;p=thirdparty%2Fkea.git [2384] Tests for units in TTL text They fail, since this is not implemented yet. --- diff --git a/src/lib/dns/tests/rrttl_unittest.cc b/src/lib/dns/tests/rrttl_unittest.cc index 0e3ab446fd..cef5816393 100644 --- a/src/lib/dns/tests/rrttl_unittest.cc +++ b/src/lib/dns/tests/rrttl_unittest.cc @@ -72,13 +72,48 @@ TEST_F(RRTTLTest, fromText) { EXPECT_EQ(0x12345678, ttl_32bit.getValue()); EXPECT_EQ(0xffffffff, ttl_max.getValue()); - EXPECT_THROW(RRTTL("1D"), InvalidRRTTL); // we don't support this form yet 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 } +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(tolower(suffix))).getValue()); + EXPECT_EQ(value, + RRTTL(num + static_cast(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'); + + // Now some compound ones. We allow any order (it would be much work to + // check the order anyway). The last part can be without unit, in which + // case it is considered seconds. + EXPECT_EQ(60 * 60 + 3, RRTTL("1H3S").getValue()); + EXPECT_EQ(2 * 24 * 60 * 60 + 75 * 60 + 4, RRTTL("75M2D4").getValue()); + + // Missing number is taken as 1 (or should we disallow that?) + EXPECT_EQ(7 * 24 * 60 * 60 + 5 * 60 * 60, RRTTL("W5H").getValue()); + EXPECT_EQ(7 * 24 * 60 * 60 + 5 * 60 * 60, RRTTL("5hW").getValue()); + + // There are some wrong units + EXPECT_THROW(RRTTL("13X"), InvalidRRTTL); + EXPECT_THROW(RRTTL("3D5F"), InvalidRRTTL); +} + TEST_F(RRTTLTest, fromWire) { EXPECT_EQ(0x12345678, rrttlFactoryFromWire("rrcode32_fromWire1").getValue());