test_base85.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. Copyright (c) 2016 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include "testutil.hpp"
  25. #include "testutil_unity.hpp"
  26. void setUp ()
  27. {
  28. }
  29. void tearDown ()
  30. {
  31. }
  32. // Test vector: rfc.zeromq.org/spec:32/Z85
  33. void test__zmq_z85_encode__valid__success ()
  34. {
  35. static const size_t size = 8;
  36. static const size_t length = size * 5 / 4;
  37. static const uint8_t decoded[size] = {0x86, 0x4F, 0xD2, 0x6F,
  38. 0xB5, 0x59, 0xF7, 0x5B};
  39. static const char expected[length + 1] = "HelloWorld";
  40. char out_encoded[length + 1] = {0};
  41. errno = 0;
  42. TEST_ASSERT_NOT_NULL (zmq_z85_encode (out_encoded, decoded, size));
  43. TEST_ASSERT_EQUAL_STRING (expected, out_encoded);
  44. TEST_ASSERT_EQUAL_INT (0, zmq_errno ());
  45. }
  46. // Buffer length must be evenly divisible by 4 or must fail with EINVAL.
  47. void test__zmq_z85_encode__invalid__failure (size_t size_)
  48. {
  49. errno = 0;
  50. TEST_ASSERT_NULL (zmq_z85_encode (NULL, NULL, size_));
  51. TEST_ASSERT_EQUAL_INT (EINVAL, zmq_errno ());
  52. }
  53. // Test vector: rfc.zeromq.org/spec:32/Z85
  54. void test__zmq_z85_decode__valid__success ()
  55. {
  56. static const size_t size = 10 * 4 / 5;
  57. static const uint8_t expected[size] = {0x86, 0x4F, 0xD2, 0x6F,
  58. 0xB5, 0x59, 0xF7, 0x5B};
  59. static const char *encoded = "HelloWorld";
  60. uint8_t out_decoded[size] = {0};
  61. errno = 0;
  62. TEST_ASSERT_NOT_NULL (zmq_z85_decode (out_decoded, encoded));
  63. TEST_ASSERT_EQUAL_INT (0, zmq_errno ());
  64. TEST_ASSERT_EQUAL_UINT8_ARRAY (expected, out_decoded, size);
  65. }
  66. // Invalid input data must fail with EINVAL.
  67. template <size_t SIZE>
  68. void test__zmq_z85_decode__invalid__failure (const char (&encoded_)[SIZE])
  69. {
  70. uint8_t decoded[SIZE * 4 / 5 + 1];
  71. errno = 0;
  72. TEST_ASSERT_NULL (zmq_z85_decode (decoded, encoded_));
  73. TEST_ASSERT_EQUAL_INT (EINVAL, zmq_errno ());
  74. }
  75. // call zmq_z85_encode, then zmq_z85_decode, and compare the results with the original
  76. template <size_t SIZE>
  77. void test__zmq_z85_encode__zmq_z85_decode__roundtrip (
  78. const uint8_t (&test_data_)[SIZE])
  79. {
  80. char test_data_z85[SIZE * 5 / 4 + 1];
  81. const char *const res1 = zmq_z85_encode (test_data_z85, test_data_, SIZE);
  82. TEST_ASSERT_NOT_NULL (res1);
  83. uint8_t test_data_decoded[SIZE];
  84. const uint8_t *const res2 =
  85. zmq_z85_decode (test_data_decoded, test_data_z85);
  86. TEST_ASSERT_NOT_NULL (res2);
  87. TEST_ASSERT_EQUAL_UINT8_ARRAY (test_data_, test_data_decoded, SIZE);
  88. }
  89. // call zmq_z85_encode, then zmq_z85_decode, and compare the results with the original
  90. template <size_t SIZE>
  91. void test__zmq_z85_decode__zmq_z85_encode__roundtrip (
  92. const char (&test_data_)[SIZE])
  93. {
  94. const size_t decoded_size = (SIZE - 1) * 4 / 5;
  95. uint8_t test_data_decoded[decoded_size];
  96. const uint8_t *const res1 = zmq_z85_decode (test_data_decoded, test_data_);
  97. TEST_ASSERT_NOT_NULL (res1);
  98. char test_data_z85[SIZE];
  99. const char *const res2 =
  100. zmq_z85_encode (test_data_z85, test_data_decoded, decoded_size);
  101. TEST_ASSERT_NOT_NULL (res2);
  102. TEST_ASSERT_EQUAL_UINT8_ARRAY (test_data_, test_data_z85, SIZE);
  103. }
  104. #define def_test__zmq_z85_basename(basename, name, param) \
  105. void test__zmq_z85_##basename##_##name () \
  106. { \
  107. test__zmq_z85_##basename (param); \
  108. }
  109. #define def_test__zmq_z85_encode__invalid__failure(name, param) \
  110. def_test__zmq_z85_basename (encode__invalid__failure, name, param)
  111. def_test__zmq_z85_encode__invalid__failure (1, 1)
  112. def_test__zmq_z85_encode__invalid__failure (42, 42)
  113. #define def_test__zmq_z85_decode__invalid__failure(name, param) \
  114. def_test__zmq_z85_basename (decode__invalid__failure, name, param)
  115. // String length must be evenly divisible by 5 or must fail with EINVAL.
  116. def_test__zmq_z85_decode__invalid__failure (indivisble_by_5_multiple_chars,
  117. "01234567")
  118. def_test__zmq_z85_decode__invalid__failure (indivisble_by_5_one_char, "0")
  119. // decode invalid data with the maximum representable value
  120. def_test__zmq_z85_decode__invalid__failure (max, "#####")
  121. // decode invalid data with the minimum value beyond the limit
  122. // "%nSc0" is 0xffffffff
  123. def_test__zmq_z85_decode__invalid__failure (above_limit, "%nSc1")
  124. // decode invalid data with an invalid character in the range of valid
  125. // characters
  126. def_test__zmq_z85_decode__invalid__failure (char_within, "####\0047")
  127. // decode invalid data with an invalid character just below the range of valid
  128. // characters
  129. def_test__zmq_z85_decode__invalid__failure (char_adjacent_below, "####\0200")
  130. // decode invalid data with an invalid character just above the range of valid
  131. // characters
  132. def_test__zmq_z85_decode__invalid__failure (char_adjacent_above, "####\0037")
  133. #define def_test__encode__zmq_z85_decode__roundtrip(name, param) \
  134. def_test__zmq_z85_basename (encode__zmq_z85_decode__roundtrip, name, param)
  135. const uint8_t test_data_min[] = {0x00, 0x00, 0x00, 0x00};
  136. const uint8_t test_data_max[] = {0xff, 0xff, 0xff, 0xff};
  137. def_test__encode__zmq_z85_decode__roundtrip (min, test_data_min)
  138. def_test__encode__zmq_z85_decode__roundtrip (max, test_data_max)
  139. #define def_test__decode__zmq_z85_encode__roundtrip(name, param) \
  140. def_test__zmq_z85_basename (decode__zmq_z85_encode__roundtrip, name, param)
  141. const char test_data_regular[] = "r^/rM9M=rMToK)63O8dCvd9D<PY<7iGlC+{BiSnG";
  142. def_test__decode__zmq_z85_encode__roundtrip (regular, test_data_regular)
  143. int main ()
  144. {
  145. UNITY_BEGIN ();
  146. RUN_TEST (test__zmq_z85_encode__valid__success);
  147. RUN_TEST (test__zmq_z85_encode__invalid__failure_1);
  148. RUN_TEST (test__zmq_z85_encode__invalid__failure_42);
  149. RUN_TEST (test__zmq_z85_decode__valid__success);
  150. RUN_TEST (
  151. test__zmq_z85_decode__invalid__failure_indivisble_by_5_multiple_chars);
  152. RUN_TEST (test__zmq_z85_decode__invalid__failure_indivisble_by_5_one_char);
  153. RUN_TEST (test__zmq_z85_decode__invalid__failure_max);
  154. RUN_TEST (test__zmq_z85_decode__invalid__failure_above_limit);
  155. RUN_TEST (test__zmq_z85_decode__invalid__failure_char_within);
  156. RUN_TEST (test__zmq_z85_decode__invalid__failure_char_adjacent_below);
  157. RUN_TEST (test__zmq_z85_decode__invalid__failure_char_adjacent_above);
  158. RUN_TEST (test__zmq_z85_encode__zmq_z85_decode__roundtrip_min);
  159. RUN_TEST (test__zmq_z85_encode__zmq_z85_decode__roundtrip_max);
  160. RUN_TEST (test__zmq_z85_decode__zmq_z85_encode__roundtrip_regular);
  161. return UNITY_END ();
  162. }