rrttl_unittest.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <gtest/gtest.h>
  15. #include <util/buffer.h>
  16. #include <dns/messagerenderer.h>
  17. #include <dns/rrttl.h>
  18. #include <dns/tests/unittest_util.h>
  19. using namespace std;
  20. using namespace isc;
  21. using namespace isc::dns;
  22. using namespace isc::util;
  23. namespace {
  24. class RRTTLTest : public ::testing::Test {
  25. protected:
  26. RRTTLTest() : obuffer(0), renderer(obuffer) {}
  27. OutputBuffer obuffer;
  28. MessageRenderer renderer;
  29. static RRTTL rrttlFactoryFromWire(const char* datafile);
  30. static const RRTTL ttl_0, ttl_1h, ttl_1d, ttl_32bit, ttl_max;
  31. static const RRTTL ttl_small, ttl_large;
  32. static const uint8_t wiredata[20];
  33. };
  34. const RRTTL RRTTLTest::ttl_0(0);
  35. const RRTTL RRTTLTest::ttl_1h(3600);
  36. const RRTTL RRTTLTest::ttl_1d(86400);
  37. const RRTTL RRTTLTest::ttl_32bit(0x12345678);
  38. const RRTTL RRTTLTest::ttl_max(0xffffffff);
  39. const RRTTL RRTTLTest::ttl_small(1);
  40. const RRTTL RRTTLTest::ttl_large(0x80000001);
  41. // This is wire-format data for the above sample RRTTLs rendered in the
  42. // appearing order.
  43. const uint8_t RRTTLTest::wiredata[20] = { 0x00, 0x00, 0x00, 0x00,
  44. 0x00, 0x00, 0x0e, 0x10,
  45. 0x00, 0x01, 0x51, 0x80,
  46. 0x12, 0x34, 0x56, 0x78,
  47. 0xff, 0xff, 0xff, 0xff };
  48. RRTTL
  49. RRTTLTest::rrttlFactoryFromWire(const char* datafile) {
  50. std::vector<unsigned char> data;
  51. UnitTestUtil::readWireData(datafile, data);
  52. InputBuffer buffer(&data[0], data.size());
  53. return (RRTTL(buffer));
  54. }
  55. TEST_F(RRTTLTest, fromText) {
  56. EXPECT_EQ(0, ttl_0.getValue());
  57. EXPECT_EQ(3600, ttl_1h.getValue());
  58. EXPECT_EQ(86400, ttl_1d.getValue());
  59. EXPECT_EQ(0x12345678, ttl_32bit.getValue());
  60. EXPECT_EQ(0xffffffff, ttl_max.getValue());
  61. EXPECT_THROW(RRTTL("1D"), InvalidRRTTL); // we don't support this form yet
  62. EXPECT_THROW(RRTTL("0xdeadbeef"), InvalidRRTTL); // must be decimal
  63. EXPECT_THROW(RRTTL("-1"), InvalidRRTTL); // must be positive
  64. EXPECT_THROW(RRTTL("1.1"), InvalidRRTTL); // must be integer
  65. EXPECT_THROW(RRTTL("4294967296"), InvalidRRTTL); // must be 32-bit
  66. }
  67. TEST_F(RRTTLTest, fromWire) {
  68. EXPECT_EQ(0x12345678,
  69. rrttlFactoryFromWire("rrcode32_fromWire1").getValue());
  70. EXPECT_THROW(rrttlFactoryFromWire("rrcode32_fromWire2"),
  71. IncompleteRRTTL);
  72. }
  73. TEST_F(RRTTLTest, toText) {
  74. EXPECT_EQ("0", ttl_0.toText());
  75. EXPECT_EQ("3600", ttl_1h.toText());
  76. EXPECT_EQ("86400", ttl_1d.toText());
  77. EXPECT_EQ("305419896", ttl_32bit.toText());
  78. EXPECT_EQ("4294967295", ttl_max.toText());
  79. }
  80. TEST_F(RRTTLTest, toWireBuffer) {
  81. ttl_0.toWire(obuffer);
  82. ttl_1h.toWire(obuffer);
  83. ttl_1d.toWire(obuffer);
  84. ttl_32bit.toWire(obuffer);
  85. ttl_max.toWire(obuffer);
  86. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  87. obuffer.getData(), obuffer.getLength(),
  88. wiredata, sizeof(wiredata));
  89. }
  90. TEST_F(RRTTLTest, toWireRenderer) {
  91. ttl_0.toWire(renderer);
  92. ttl_1h.toWire(renderer);
  93. ttl_1d.toWire(renderer);
  94. ttl_32bit.toWire(renderer);
  95. ttl_max.toWire(renderer);
  96. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  97. obuffer.getData(), obuffer.getLength(),
  98. wiredata, sizeof(wiredata));
  99. }
  100. TEST_F(RRTTLTest, equal) {
  101. EXPECT_TRUE(RRTTL("3600") == ttl_1h);
  102. EXPECT_TRUE(RRTTL("86400").equals(ttl_1d));
  103. EXPECT_TRUE(ttl_1d != ttl_1h);
  104. EXPECT_TRUE(ttl_1d.nequals(ttl_max));
  105. }
  106. //
  107. // The following set of tests confirm the result of <=, <, >=, >
  108. // The test logic is simple, and all tests are just straightforward variations
  109. // of the first one.
  110. //
  111. TEST_F(RRTTLTest, leq) {
  112. // small <= large is true
  113. EXPECT_TRUE(ttl_small.leq(ttl_large));
  114. EXPECT_TRUE(ttl_small <= ttl_large);
  115. // small <= small is true
  116. EXPECT_TRUE(ttl_small.leq(ttl_small));
  117. EXPECT_LE(ttl_small, ttl_small);
  118. // large <= small is false
  119. EXPECT_FALSE(ttl_large.leq(ttl_small));
  120. EXPECT_FALSE(ttl_large <= ttl_small);
  121. }
  122. TEST_F(RRTTLTest, geq) {
  123. EXPECT_TRUE(ttl_large.geq(ttl_small));
  124. EXPECT_TRUE(ttl_large >= ttl_small);
  125. EXPECT_TRUE(ttl_large.geq(ttl_large));
  126. EXPECT_GE(ttl_large, ttl_large);
  127. EXPECT_FALSE(ttl_small.geq(ttl_large));
  128. EXPECT_FALSE(ttl_small >= ttl_large);
  129. }
  130. TEST_F(RRTTLTest, lthan) {
  131. EXPECT_TRUE(ttl_small.lthan(ttl_large));
  132. EXPECT_TRUE(ttl_small < ttl_large);
  133. EXPECT_FALSE(ttl_small.lthan(ttl_small));
  134. EXPECT_FALSE(ttl_small < ttl_small);
  135. EXPECT_FALSE(ttl_large.lthan(ttl_small));
  136. EXPECT_FALSE(ttl_large < ttl_small);
  137. }
  138. TEST_F(RRTTLTest, gthan) {
  139. EXPECT_TRUE(ttl_large.gthan(ttl_small));
  140. EXPECT_TRUE(ttl_large > ttl_small);
  141. EXPECT_FALSE(ttl_large.gthan(ttl_large));
  142. EXPECT_FALSE(ttl_large > ttl_large);
  143. EXPECT_FALSE(ttl_small.gthan(ttl_large));
  144. EXPECT_FALSE(ttl_small > ttl_large);
  145. }
  146. // test operator<<. We simply confirm it appends the result of toText().
  147. TEST_F(RRTTLTest, LeftShiftOperator) {
  148. ostringstream oss;
  149. oss << ttl_1h;
  150. EXPECT_EQ(ttl_1h.toText(), oss.str());
  151. }
  152. }