hex_unittest.cc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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: rrtype_unittest.cc 476 2010-01-19 00:29:28Z jinmei $
  15. #include <stdint.h>
  16. #include <vector>
  17. #include <string>
  18. #include <dns/hex.h>
  19. #include <gtest/gtest.h>
  20. #include "unittest_util.h"
  21. using isc::UnitTestUtil;
  22. using namespace std;
  23. using namespace isc::dns;
  24. namespace {
  25. class HexTest : public ::testing::Test {
  26. protected:
  27. HexTest() {}
  28. };
  29. const std::string hex_txt("DEADBEEFDECADE");
  30. const std::string hex_txt_lower("deadbeefdecade");
  31. TEST_F(HexTest, encodeHex) {
  32. std::vector<uint8_t> data;
  33. data.push_back(0xde);
  34. data.push_back(0xad);
  35. data.push_back(0xbe);
  36. data.push_back(0xef);
  37. data.push_back(0xde);
  38. data.push_back(0xca);
  39. data.push_back(0xde);
  40. EXPECT_EQ(hex_txt, encodeHex(data));
  41. }
  42. void
  43. compareData(const std::vector<uint8_t>& data)
  44. {
  45. EXPECT_EQ(0xde, data[0]);
  46. EXPECT_EQ(0xad, data[1]);
  47. EXPECT_EQ(0xbe, data[2]);
  48. EXPECT_EQ(0xef, data[3]);
  49. EXPECT_EQ(0xde, data[4]);
  50. EXPECT_EQ(0xca, data[5]);
  51. EXPECT_EQ(0xde, data[6]);
  52. }
  53. TEST_F(HexTest, decodeHex) {
  54. std::vector<uint8_t> result;
  55. decodeHex(hex_txt, result);
  56. compareData(result);
  57. // lower case hex digits should be accepted
  58. result.clear();
  59. decodeHex(hex_txt_lower, result);
  60. compareData(result);
  61. // Bogus input: should fail
  62. result.clear();
  63. EXPECT_THROW(decodeHex("1x", result), BadHexString);
  64. }
  65. }