hex_unittest.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 <exceptions/exceptions.h>
  19. #include <dns/util/hex.h>
  20. #include <gtest/gtest.h>
  21. using namespace std;
  22. using namespace isc;
  23. using namespace isc::dns;
  24. namespace {
  25. const string hex_txt("DEADBEEFDECADE");
  26. const string hex_txt_space("DEAD BEEF DECADE");
  27. const string hex_txt_lower("deadbeefdecade");
  28. class HexTest : public ::testing::Test {
  29. protected:
  30. HexTest() : encoding_chars("0123456789ABCDEF") {}
  31. vector<uint8_t> decoded_data;
  32. const string encoding_chars;
  33. };
  34. TEST_F(HexTest, encodeHex) {
  35. std::vector<uint8_t> data;
  36. data.push_back(0xde);
  37. data.push_back(0xad);
  38. data.push_back(0xbe);
  39. data.push_back(0xef);
  40. data.push_back(0xde);
  41. data.push_back(0xca);
  42. data.push_back(0xde);
  43. EXPECT_EQ(hex_txt, encodeHex(data));
  44. }
  45. void
  46. compareData(const std::vector<uint8_t>& data) {
  47. EXPECT_EQ(0xde, data[0]);
  48. EXPECT_EQ(0xad, data[1]);
  49. EXPECT_EQ(0xbe, data[2]);
  50. EXPECT_EQ(0xef, data[3]);
  51. EXPECT_EQ(0xde, data[4]);
  52. EXPECT_EQ(0xca, data[5]);
  53. EXPECT_EQ(0xde, data[6]);
  54. }
  55. TEST_F(HexTest, decodeHex) {
  56. std::vector<uint8_t> result;
  57. decodeHex(hex_txt, result);
  58. compareData(result);
  59. // lower case hex digits should be accepted
  60. result.clear();
  61. decodeHex(hex_txt_lower, result);
  62. compareData(result);
  63. // white space should be ignored
  64. result.clear();
  65. decodeHex(hex_txt_space, result);
  66. compareData(result);
  67. // Bogus input: should fail
  68. result.clear();
  69. EXPECT_THROW(decodeHex("1x", result), BadValue);
  70. // Bogus input: encoded string must have an even number of characters.
  71. result.clear();
  72. EXPECT_THROW(decodeHex("dea", result), BadValue);
  73. }
  74. // For Hex encode/decode we use handmade mappings, so it's prudent to test the
  75. // entire mapping table explicitly.
  76. TEST_F(HexTest, decodeMap) {
  77. string input("00"); // input placeholder
  78. // See Base32HexTest.decodeMap for details of the following tests.
  79. for (int i = 0; i < 256; ++i) {
  80. input[1] = i;
  81. const char ch = toupper(i);
  82. const size_t pos = encoding_chars.find(ch);
  83. if (pos == string::npos) {
  84. EXPECT_THROW(decodeHex(input, decoded_data), BadValue);
  85. } else {
  86. decodeHex(input, decoded_data);
  87. EXPECT_EQ(1, decoded_data.size());
  88. EXPECT_EQ(pos, decoded_data[0]);
  89. }
  90. }
  91. }
  92. TEST_F(HexTest, encodeMap) {
  93. for (int i = 0; i < 16; ++i) {
  94. decoded_data.clear();
  95. decoded_data.push_back(i);
  96. EXPECT_EQ(encoding_chars[i], encodeHex(decoded_data)[1]);
  97. }
  98. }
  99. }