base32hex_unittest.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #include <stdint.h>
  15. #include <cctype>
  16. #include <string>
  17. #include <utility>
  18. #include <vector>
  19. #include <exceptions/exceptions.h>
  20. #include <util/encode/base32hex.h>
  21. #include <gtest/gtest.h>
  22. using namespace std;
  23. using namespace isc;
  24. using namespace isc::util::encode;
  25. namespace {
  26. typedef pair<string, string> StringPair;
  27. class Base32HexTest : public ::testing::Test {
  28. protected:
  29. Base32HexTest() : encoding_chars("0123456789ABCDEFGHIJKLMNOPQRSTUV=") {
  30. // test vectors from RFC4648
  31. test_sequence.push_back(StringPair("", ""));
  32. test_sequence.push_back(StringPair("f", "CO======"));
  33. test_sequence.push_back(StringPair("fo", "CPNG===="));
  34. test_sequence.push_back(StringPair("foo", "CPNMU==="));
  35. test_sequence.push_back(StringPair("foob", "CPNMUOG="));
  36. test_sequence.push_back(StringPair("fooba", "CPNMUOJ1"));
  37. test_sequence.push_back(StringPair("foobar", "CPNMUOJ1E8======"));
  38. // the same data, encoded using lower case chars (testable only
  39. // for the decode side)
  40. test_sequence_lower.push_back(StringPair("f", "co======"));
  41. test_sequence_lower.push_back(StringPair("fo", "cpng===="));
  42. test_sequence_lower.push_back(StringPair("foo", "cpnmu==="));
  43. test_sequence_lower.push_back(StringPair("foob", "cpnmuog="));
  44. test_sequence_lower.push_back(StringPair("fooba", "cpnmuoj1"));
  45. test_sequence_lower.push_back(StringPair("foobar",
  46. "cpnmuoj1e8======"));
  47. }
  48. vector<StringPair> test_sequence;
  49. vector<StringPair> test_sequence_lower;
  50. vector<uint8_t> decoded_data;
  51. const string encoding_chars;
  52. };
  53. void
  54. decodeCheck(const string& input_string, vector<uint8_t>& output,
  55. const string& expected)
  56. {
  57. decodeBase32Hex(input_string, output);
  58. EXPECT_EQ(expected, string(output.begin(), output.end()));
  59. }
  60. TEST_F(Base32HexTest, decode) {
  61. for (vector<StringPair>::const_iterator it = test_sequence.begin();
  62. it != test_sequence.end();
  63. ++it) {
  64. decodeCheck((*it).second, decoded_data, (*it).first);
  65. }
  66. // whitespace should be allowed
  67. decodeCheck("CP NM\tUOG=", decoded_data, "foob");
  68. decodeCheck("CPNMU===\n", decoded_data, "foo");
  69. decodeCheck(" CP NM\tUOG=", decoded_data, "foob");
  70. decodeCheck(" ", decoded_data, "");
  71. // Incomplete input
  72. EXPECT_THROW(decodeBase32Hex("CPNMUOJ", decoded_data), BadValue);
  73. // invalid number of padding characters
  74. EXPECT_THROW(decodeBase32Hex("CPNMU0==", decoded_data), BadValue);
  75. EXPECT_THROW(decodeBase32Hex("CO0=====", decoded_data), BadValue);
  76. EXPECT_THROW(decodeBase32Hex("CO=======", decoded_data), // too many ='s
  77. BadValue);
  78. // intermediate padding isn't allowed
  79. EXPECT_THROW(decodeBase32Hex("CPNMUOG=CPNMUOG=", decoded_data), BadValue);
  80. // Non canonical form isn't allowed.
  81. // P => 25(11001), so the padding byte would be 01000000
  82. EXPECT_THROW(decodeBase32Hex("0P======", decoded_data), BadValue);
  83. }
  84. TEST_F(Base32HexTest, decodeLower) {
  85. for (vector<StringPair>::const_iterator it = test_sequence_lower.begin();
  86. it != test_sequence_lower.end();
  87. ++it) {
  88. decodeCheck((*it).second, decoded_data, (*it).first);
  89. }
  90. }
  91. TEST_F(Base32HexTest, encode) {
  92. for (vector<StringPair>::const_iterator it = test_sequence.begin();
  93. it != test_sequence.end();
  94. ++it) {
  95. decoded_data.assign((*it).first.begin(), (*it).first.end());
  96. EXPECT_EQ((*it).second, encodeBase32Hex(decoded_data));
  97. }
  98. }
  99. // For Base32Hex we use handmade mappings, so it's prudent to test the
  100. // entire mapping table explicitly.
  101. TEST_F(Base32HexTest, decodeMap) {
  102. string input(8, '0'); // input placeholder
  103. // We're going to populate an input string with only the last character
  104. // not equal to the zero character ('0') for each valid base32hex encoding
  105. // character. Decoding that input should result in a data stream with
  106. // the last byte equal to the numeric value represented by the that
  107. // character. For example, we'll generate and confirm the following:
  108. // "00000000" => should be 0 (as a 40bit integer)
  109. // "00000001" => should be 1 (as a 40bit integer)
  110. // ...
  111. // "0000000V" => should be 31 (as a 40bit integer)
  112. // We also check the use of an invalid character for the last character
  113. // surely fails. '=' should be accepted as a valid padding in this
  114. // context; space characters shouldn't be allowed in this context.
  115. for (int i = 0; i < 256; ++i) {
  116. input[7] = i;
  117. const char ch = toupper(i);
  118. const size_t pos = encoding_chars.find(ch);
  119. if (pos == string::npos) {
  120. EXPECT_THROW(decodeBase32Hex(input, decoded_data), BadValue);
  121. } else {
  122. decodeBase32Hex(input, decoded_data);
  123. if (ch == '=') {
  124. EXPECT_EQ(4, decoded_data.size());
  125. } else {
  126. EXPECT_EQ(5, decoded_data.size());
  127. EXPECT_EQ(pos, decoded_data[4]);
  128. }
  129. }
  130. }
  131. }
  132. TEST_F(Base32HexTest, encodeMap) {
  133. for (int i = 0; i < 32; ++i) {
  134. decoded_data.assign(4, 0);
  135. decoded_data.push_back(i);
  136. EXPECT_EQ(encoding_chars[i], encodeBase32Hex(decoded_data)[7]);
  137. }
  138. }
  139. }