rrttl_unittest.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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) {}
  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, getValue) {
  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. }
  62. TEST_F(RRTTLTest, fromText) {
  63. EXPECT_THROW(RRTTL("0xdeadbeef"), InvalidRRTTL); // must be decimal
  64. EXPECT_THROW(RRTTL("-1"), InvalidRRTTL); // must be positive
  65. EXPECT_THROW(RRTTL("1.1"), InvalidRRTTL); // must be integer
  66. EXPECT_THROW(RRTTL("4294967296"), InvalidRRTTL); // must be 32-bit
  67. }
  68. void
  69. checkUnit(unsigned multiply, char suffix) {
  70. SCOPED_TRACE(string("Unit check with suffix ") + suffix);
  71. const uint32_t value = 10 * multiply;
  72. const string num = "10";
  73. // Check both lower and upper version of the suffix
  74. EXPECT_EQ(value,
  75. RRTTL(num + static_cast<char>(tolower(suffix))).getValue());
  76. EXPECT_EQ(value,
  77. RRTTL(num + static_cast<char>(toupper(suffix))).getValue());
  78. }
  79. // Check parsing the unit form (1D, etc)
  80. TEST_F(RRTTLTest, fromTextUnit) {
  81. // Check each of the units separately
  82. checkUnit(1, 'S');
  83. checkUnit(60, 'M');
  84. checkUnit(60 * 60, 'H');
  85. checkUnit(24 * 60 * 60, 'D');
  86. checkUnit(7 * 24 * 60 * 60, 'W');
  87. // Now some compound ones. We allow any order (it would be much work to
  88. // check the order anyway). The last part can be without unit, in which
  89. // case it is considered seconds.
  90. EXPECT_EQ(60 * 60 + 3, RRTTL("1H3S").getValue());
  91. EXPECT_EQ(2 * 24 * 60 * 60 + 75 * 60 + 4, RRTTL("75M2D4").getValue());
  92. // Awkward, but allowed case - the same unit used twice.
  93. EXPECT_EQ(20 * 3600, RRTTL("12H8H").getValue());
  94. // Negative number in part of the expression, but the total is positive.
  95. // Rejected.
  96. EXPECT_THROW(RRTTL("-1S1H"), InvalidRRTTL);
  97. // Some things out of range in the ttl, but it wraps to number in range
  98. // in int64_t. Should still not get fooled and reject it.
  99. // First part out of range
  100. EXPECT_THROW(RRTTL("9223372036854775807S9223372036854775807S2S"),
  101. InvalidRRTTL);
  102. // Second part out of range, but it immediately wraps (2S+2^64-2S)
  103. EXPECT_THROW(RRTTL("2S18446744073709551614S"),
  104. InvalidRRTTL);
  105. // The whole thing wraps right away (2^64S)
  106. EXPECT_THROW(RRTTL("18446744073709551616S"),
  107. InvalidRRTTL);
  108. // Missing before unit.
  109. EXPECT_THROW(RRTTL("W5H"), InvalidRRTTL);
  110. EXPECT_THROW(RRTTL("5hW"), InvalidRRTTL);
  111. // Empty string is not allowed
  112. EXPECT_THROW(RRTTL(""), InvalidRRTTL);
  113. // There are some wrong units
  114. EXPECT_THROW(RRTTL("13X"), InvalidRRTTL);
  115. EXPECT_THROW(RRTTL("3D5F"), InvalidRRTTL);
  116. }
  117. TEST_F(RRTTLTest, fromWire) {
  118. EXPECT_EQ(0x12345678,
  119. rrttlFactoryFromWire("rrcode32_fromWire1").getValue());
  120. EXPECT_THROW(rrttlFactoryFromWire("rrcode32_fromWire2"),
  121. IncompleteRRTTL);
  122. }
  123. TEST_F(RRTTLTest, toText) {
  124. EXPECT_EQ("0", ttl_0.toText());
  125. EXPECT_EQ("3600", ttl_1h.toText());
  126. EXPECT_EQ("86400", ttl_1d.toText());
  127. EXPECT_EQ("305419896", ttl_32bit.toText());
  128. EXPECT_EQ("4294967295", ttl_max.toText());
  129. }
  130. TEST_F(RRTTLTest, toWireBuffer) {
  131. ttl_0.toWire(obuffer);
  132. ttl_1h.toWire(obuffer);
  133. ttl_1d.toWire(obuffer);
  134. ttl_32bit.toWire(obuffer);
  135. ttl_max.toWire(obuffer);
  136. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  137. obuffer.getData(), obuffer.getLength(),
  138. wiredata, sizeof(wiredata));
  139. }
  140. TEST_F(RRTTLTest, toWireRenderer) {
  141. ttl_0.toWire(renderer);
  142. ttl_1h.toWire(renderer);
  143. ttl_1d.toWire(renderer);
  144. ttl_32bit.toWire(renderer);
  145. ttl_max.toWire(renderer);
  146. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  147. renderer.getData(), renderer.getLength(),
  148. wiredata, sizeof(wiredata));
  149. }
  150. TEST_F(RRTTLTest, equal) {
  151. EXPECT_TRUE(RRTTL("3600") == ttl_1h);
  152. EXPECT_TRUE(RRTTL("86400").equals(ttl_1d));
  153. EXPECT_TRUE(ttl_1d != ttl_1h);
  154. EXPECT_TRUE(ttl_1d.nequals(ttl_max));
  155. }
  156. //
  157. // The following set of tests confirm the result of <=, <, >=, >
  158. // The test logic is simple, and all tests are just straightforward variations
  159. // of the first one.
  160. //
  161. TEST_F(RRTTLTest, leq) {
  162. // small <= large is true
  163. EXPECT_TRUE(ttl_small.leq(ttl_large));
  164. EXPECT_TRUE(ttl_small <= ttl_large);
  165. // small <= small is true
  166. EXPECT_TRUE(ttl_small.leq(ttl_small));
  167. EXPECT_LE(ttl_small, ttl_small);
  168. // large <= small is false
  169. EXPECT_FALSE(ttl_large.leq(ttl_small));
  170. EXPECT_FALSE(ttl_large <= ttl_small);
  171. }
  172. TEST_F(RRTTLTest, geq) {
  173. EXPECT_TRUE(ttl_large.geq(ttl_small));
  174. EXPECT_TRUE(ttl_large >= ttl_small);
  175. EXPECT_TRUE(ttl_large.geq(ttl_large));
  176. EXPECT_GE(ttl_large, ttl_large);
  177. EXPECT_FALSE(ttl_small.geq(ttl_large));
  178. EXPECT_FALSE(ttl_small >= ttl_large);
  179. }
  180. TEST_F(RRTTLTest, lthan) {
  181. EXPECT_TRUE(ttl_small.lthan(ttl_large));
  182. EXPECT_TRUE(ttl_small < ttl_large);
  183. EXPECT_FALSE(ttl_small.lthan(ttl_small));
  184. // cppcheck-suppress duplicateExpression
  185. EXPECT_FALSE(ttl_small < ttl_small);
  186. EXPECT_FALSE(ttl_large.lthan(ttl_small));
  187. EXPECT_FALSE(ttl_large < ttl_small);
  188. }
  189. TEST_F(RRTTLTest, gthan) {
  190. EXPECT_TRUE(ttl_large.gthan(ttl_small));
  191. EXPECT_TRUE(ttl_large > ttl_small);
  192. EXPECT_FALSE(ttl_large.gthan(ttl_large));
  193. // cppcheck-suppress duplicateExpression
  194. EXPECT_FALSE(ttl_large > ttl_large);
  195. EXPECT_FALSE(ttl_small.gthan(ttl_large));
  196. EXPECT_FALSE(ttl_small > ttl_large);
  197. }
  198. // test operator<<. We simply confirm it appends the result of toText().
  199. TEST_F(RRTTLTest, LeftShiftOperator) {
  200. ostringstream oss;
  201. oss << ttl_1h;
  202. EXPECT_EQ(ttl_1h.toText(), oss.str());
  203. }
  204. }