unittest_util.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright (C) 2009 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: unittest_util.cc 754 2010-02-08 22:28:56Z each $
  15. #include <iostream>
  16. #include <fstream>
  17. #include <sstream>
  18. #include <stdexcept>
  19. #include <vector>
  20. #include <string>
  21. #include <gtest/gtest.h>
  22. #include <dns/name.h>
  23. #include "unittest_util.h"
  24. using isc::UnitTestUtil;
  25. using isc::dns::NameComparisonResult;
  26. namespace isc {
  27. void
  28. UnitTestUtil::readWireData(const char* datafile,
  29. std::vector<unsigned char>& data)
  30. {
  31. std::ifstream ifs;
  32. ifs.open(datafile, std::ios_base::in);
  33. if ((ifs.rdstate() & std::istream::failbit) != 0) {
  34. throw std::runtime_error("failed to open data file: " +
  35. std::string(datafile));
  36. }
  37. data.clear();
  38. std::string s;
  39. while (getline(ifs, s), !ifs.eof()) {
  40. if (ifs.bad() || ifs.fail()) {
  41. throw std::runtime_error("unexpected data line");
  42. }
  43. if (s.empty() || s[0] == '#') {
  44. continue;
  45. }
  46. readWireData(s, data);
  47. }
  48. }
  49. void
  50. UnitTestUtil::readWireData(const std::string& datastr,
  51. std::vector<unsigned char>& data)
  52. {
  53. std::istringstream iss(datastr);
  54. do {
  55. std::string bytes;
  56. iss >> bytes;
  57. if (iss.bad() || iss.fail() || (bytes.size() % 2) != 0) {
  58. throw std::runtime_error("unexpected input or I/O error");
  59. }
  60. for (int pos = 0; pos < bytes.size(); pos += 2) {
  61. unsigned int ch;
  62. std::istringstream(bytes.substr(pos, 2)) >> std::hex >> ch;
  63. data.push_back(static_cast<unsigned char>(ch));
  64. }
  65. } while (!iss.eof());
  66. }
  67. ::testing::AssertionResult
  68. UnitTestUtil::matchWireData(const char* dataexp1, const char* lenexp1,
  69. const char* dataexp2, const char* lenexp2,
  70. const void* data1, size_t len1,
  71. const void* data2, size_t len2)
  72. {
  73. ::testing::Message msg;
  74. size_t cmplen = std::min(len1, len2);
  75. for (int i = 0; i < cmplen; i++) {
  76. int ch1 = static_cast<const uint8_t*>(data1)[i];
  77. int ch2 = static_cast<const uint8_t*>(data2)[i];
  78. if (ch1 != ch2) {
  79. msg << "Wire data mismatch at " << i << "th byte\n"
  80. << " Actual: " << ch1 << "\n"
  81. << "Expected: " << ch2 << "\n";
  82. return (::testing::AssertionFailure(msg));
  83. }
  84. }
  85. if (len1 != len2) {
  86. msg << "Wire data mismatch in length:\n"
  87. << " Actual: " << len1 << "\n"
  88. << "Expected: " << len2 << "\n";
  89. return (::testing::AssertionFailure(msg));
  90. }
  91. return ::testing::AssertionSuccess();
  92. }
  93. ::testing::AssertionResult
  94. UnitTestUtil::matchName(const char* nameexp1, const char* nameexp2,
  95. const isc::dns::Name& name1,
  96. const isc::dns::Name& name2)
  97. {
  98. ::testing::Message msg;
  99. NameComparisonResult cmpresult = name1.compare(name2);
  100. if (cmpresult.getOrder() != 0 ||
  101. cmpresult.getRelation() != NameComparisonResult::EQUAL) {
  102. msg << "Two names are expected to be equal but not:\n"
  103. << " One: " << name1 << "\n"
  104. << "Other: " << name2 << "\n";
  105. return (::testing::AssertionFailure(msg));
  106. }
  107. return ::testing::AssertionSuccess();
  108. }
  109. }