rdata_in_a_unittest.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. // $Id$
  15. #include <dns/buffer.h>
  16. #include <dns/exceptions.h>
  17. #include <dns/messagerenderer.h>
  18. #include <dns/rdata.h>
  19. #include <dns/rdataclass.h>
  20. #include <dns/rrclass.h>
  21. #include <dns/rrtype.h>
  22. #include <gtest/gtest.h>
  23. #include <dns/tests/unittest_util.h>
  24. #include <dns/tests/rdata_unittest.h>
  25. using isc::UnitTestUtil;
  26. using namespace std;
  27. using namespace isc::dns;
  28. using namespace isc::dns::rdata;
  29. namespace {
  30. class Rdata_IN_A_Test : public RdataTest {
  31. // there's nothing to specialize
  32. };
  33. const in::A rdata_in_a("192.0.2.1");
  34. const uint8_t wiredata_in_a[] = { 192, 0, 2, 1 };
  35. TEST_F(Rdata_IN_A_Test, createFromText) {
  36. EXPECT_EQ(0, rdata_in_a.compare(in::A("192.0.2.1")));
  37. // should reject an abbreviated form of IPv4 address
  38. EXPECT_THROW(in::A("10.1"), InvalidRdataText);
  39. // or an IPv6 address
  40. EXPECT_THROW(in::A("2001:db8::1234"), InvalidRdataText);
  41. // or any meaningless text as an IP address
  42. EXPECT_THROW(in::A("xxx"), InvalidRdataText);
  43. }
  44. TEST_F(Rdata_IN_A_Test, createFromWire) {
  45. // Valid data
  46. EXPECT_EQ(0, rdata_in_a.compare(
  47. *rdataFactoryFromFile(RRType::A(), RRClass::IN(),
  48. "rdata_in_a_fromWire")));
  49. // RDLENGTH is too short
  50. EXPECT_THROW(rdataFactoryFromFile(RRType::A(), RRClass::IN(),
  51. "rdata_in_a_fromWire", 6),
  52. DNSMessageFORMERR);
  53. // RDLENGTH is too long
  54. EXPECT_THROW(rdataFactoryFromFile(RRType::A(), RRClass::IN(),
  55. "rdata_in_a_fromWire", 12),
  56. DNSMessageFORMERR);
  57. // buffer too short.
  58. EXPECT_THROW(rdataFactoryFromFile(RRType::A(), RRClass::IN(),
  59. "rdata_in_a_fromWire", 19),
  60. DNSMessageFORMERR);
  61. }
  62. TEST_F(Rdata_IN_A_Test, toWireBuffer) {
  63. rdata_in_a.toWire(obuffer);
  64. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  65. obuffer.getData(), obuffer.getLength(),
  66. wiredata_in_a, sizeof(wiredata_in_a));
  67. }
  68. TEST_F(Rdata_IN_A_Test, toWireRenderer) {
  69. rdata_in_a.toWire(renderer);
  70. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  71. obuffer.getData(), obuffer.getLength(),
  72. wiredata_in_a, sizeof(wiredata_in_a));
  73. }
  74. TEST_F(Rdata_IN_A_Test, toText) {
  75. EXPECT_EQ("192.0.2.1", rdata_in_a.toText());
  76. string longaddr("255.255.255.255"); // this shouldn't make the code crash
  77. EXPECT_EQ(longaddr, in::A(longaddr).toText());
  78. }
  79. TEST_F(Rdata_IN_A_Test, compare) {
  80. in::A small1("1.1.1.1");
  81. in::A small2("1.2.3.4");
  82. in::A large1("255.255.255.255");
  83. in::A large2("4.3.2.1");
  84. // trivial case: self equivalence
  85. EXPECT_EQ(0, small1.compare(small1));
  86. // confirm these are compared as unsigned values
  87. EXPECT_GT(0, small1.compare(large1));
  88. EXPECT_LT(0, large1.compare(small1));
  89. // confirm these are compared in network byte order
  90. EXPECT_GT(0, small2.compare(large2));
  91. EXPECT_LT(0, large2.compare(small2));
  92. // comparison attempt between incompatible RR types should be rejected
  93. EXPECT_THROW(rdata_in_a.compare(*RdataTest::rdata_nomatch), bad_cast);
  94. }
  95. }