rdata_unittest.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #ifndef RDATA_UNITTEST_H
  15. #define RDATA_UNITTEST_H 1
  16. #include <util/buffer.h>
  17. #include <dns/messagerenderer.h>
  18. #include <dns/rrclass.h>
  19. #include <dns/rrtype.h>
  20. #include <dns/rdata.h>
  21. #include <dns/master_lexer.h>
  22. #include <gtest/gtest.h>
  23. #include <string>
  24. #include <sstream>
  25. using namespace isc::util;
  26. namespace isc {
  27. namespace dns {
  28. namespace rdata {
  29. class RdataTest : public ::testing::Test {
  30. protected:
  31. RdataTest();
  32. static RdataPtr rdataFactoryFromFile(const RRType& rrtype,
  33. const RRClass& rrclass,
  34. const char* datafile,
  35. size_t position = 0);
  36. // Common check to see the result of Rdata construction of given type
  37. // (template parameter RdataType) either from std::string or with
  38. // MasterLexer object. If it's expected to succeed the result should be
  39. // identical to the commonly used test data (rdata_expected); otherwise it
  40. // should result in the exception specified as the template parameter:
  41. // ExForString for the string version, and ExForLexer for the lexer
  42. // version. throw_str_version and throw_lexer_version are set to true
  43. // iff the string/lexer version is expected to throw, respectively.
  44. // Parameter origin can be set to non NULL for the origin parameter of
  45. // the lexer version of Rdata constructor.
  46. template <typename RdataType, typename ExForString, typename ExForLexer>
  47. void checkFromText(const std::string& rdata_txt,
  48. const RdataType& rdata_expected,
  49. bool throw_str_version = true,
  50. bool throw_lexer_version = true,
  51. const Name* origin = NULL)
  52. {
  53. SCOPED_TRACE(rdata_txt);
  54. if (throw_str_version) {
  55. EXPECT_THROW(RdataType rdata(rdata_txt), ExForString);
  56. } else {
  57. EXPECT_EQ(0, RdataType(rdata_txt).compare(rdata_expected));
  58. }
  59. std::stringstream ss(rdata_txt);
  60. MasterLexer lexer;
  61. lexer.pushSource(ss);
  62. if (throw_lexer_version) {
  63. EXPECT_THROW(RdataType rdata(lexer, origin, MasterLoader::DEFAULT,
  64. loader_cb), ExForLexer);
  65. } else {
  66. EXPECT_EQ(0, RdataType(lexer, origin, MasterLoader::DEFAULT,
  67. loader_cb).compare(rdata_expected));
  68. }
  69. }
  70. OutputBuffer obuffer;
  71. MessageRenderer renderer;
  72. /// This is an RDATA object of some "unknown" RR type so that it can be
  73. /// used to test the compare() method against a well-known RR type.
  74. RdataPtr rdata_nomatch;
  75. MasterLexer lexer;
  76. MasterLoaderCallbacks loader_cb;
  77. };
  78. namespace test {
  79. RdataPtr
  80. createRdataUsingLexer(const RRType& rrtype, const RRClass& rrclass,
  81. const std::string& str);
  82. }
  83. }
  84. }
  85. }
  86. #endif // RDATA_UNITTEST_H
  87. // Local Variables:
  88. // mode: c++
  89. // End: