test_base85.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #include "testutil.hpp"
4 #include "testutil_unity.hpp"
5 
6 void setUp ()
7 {
8 }
9 
10 void tearDown ()
11 {
12 }
13 
14 // Test vector: rfc.zeromq.org/spec:32/Z85
16 {
17  static const size_t size = 8;
18  static const size_t length = size * 5 / 4;
19  static const uint8_t decoded[size] = {0x86, 0x4F, 0xD2, 0x6F,
20  0xB5, 0x59, 0xF7, 0x5B};
21  static const char expected[length + 1] = "HelloWorld";
22  char out_encoded[length + 1] = {0};
23 
24  errno = 0;
25  TEST_ASSERT_NOT_NULL (zmq_z85_encode (out_encoded, decoded, size));
26  TEST_ASSERT_EQUAL_STRING (expected, out_encoded);
28 }
29 
30 // Buffer length must be evenly divisible by 4 or must fail with EINVAL.
32 {
33  errno = 0;
36 }
37 
38 // Test vector: rfc.zeromq.org/spec:32/Z85
40 {
41  static const size_t size = 10 * 4 / 5;
42  static const uint8_t expected[size] = {0x86, 0x4F, 0xD2, 0x6F,
43  0xB5, 0x59, 0xF7, 0x5B};
44  static const char *encoded = "HelloWorld";
45  uint8_t out_decoded[size] = {0};
46 
47  errno = 0;
48  TEST_ASSERT_NOT_NULL (zmq_z85_decode (out_decoded, encoded));
50  TEST_ASSERT_EQUAL_UINT8_ARRAY (expected, out_decoded, size);
51 }
52 
53 // Invalid input data must fail with EINVAL.
54 template <size_t SIZE>
55 void test__zmq_z85_decode__invalid__failure (const char (&encoded_)[SIZE])
56 {
57  uint8_t decoded[SIZE * 4 / 5 + 1];
58  errno = 0;
59  TEST_ASSERT_NULL (zmq_z85_decode (decoded, encoded_));
61 }
62 
63 
64 // call zmq_z85_encode, then zmq_z85_decode, and compare the results with the original
65 template <size_t SIZE>
67  const uint8_t (&test_data_)[SIZE])
68 {
69  char test_data_z85[SIZE * 5 / 4 + 1];
70  const char *const res1 = zmq_z85_encode (test_data_z85, test_data_, SIZE);
71  TEST_ASSERT_NOT_NULL (res1);
72 
73  uint8_t test_data_decoded[SIZE];
74  const uint8_t *const res2 =
75  zmq_z85_decode (test_data_decoded, test_data_z85);
76  TEST_ASSERT_NOT_NULL (res2);
77 
78  TEST_ASSERT_EQUAL_UINT8_ARRAY (test_data_, test_data_decoded, SIZE);
79 }
80 
81 // call zmq_z85_encode, then zmq_z85_decode, and compare the results with the original
82 template <size_t SIZE>
84  const char (&test_data_)[SIZE])
85 {
86  const size_t decoded_size = (SIZE - 1) * 4 / 5;
87  uint8_t test_data_decoded[decoded_size];
88  const uint8_t *const res1 = zmq_z85_decode (test_data_decoded, test_data_);
89  TEST_ASSERT_NOT_NULL (res1);
90 
91  char test_data_z85[SIZE];
92  const char *const res2 =
93  zmq_z85_encode (test_data_z85, test_data_decoded, decoded_size);
94  TEST_ASSERT_NOT_NULL (res2);
95 
96  TEST_ASSERT_EQUAL_UINT8_ARRAY (test_data_, test_data_z85, SIZE);
97 }
98 
99 #define def_test__zmq_z85_basename(basename, name, param) \
100  void test__zmq_z85_##basename##_##name () \
101  { \
102  test__zmq_z85_##basename (param); \
103  }
104 
105 #define def_test__zmq_z85_encode__invalid__failure(name, param) \
106  def_test__zmq_z85_basename (encode__invalid__failure, name, param)
107 
110 
111 #define def_test__zmq_z85_decode__invalid__failure(name, param) \
112  def_test__zmq_z85_basename (decode__invalid__failure, name, param)
113 
114  // String length must be evenly divisible by 5 or must fail with EINVAL.
115  def_test__zmq_z85_decode__invalid__failure (indivisble_by_5_multiple_chars,
116  "01234567")
117  def_test__zmq_z85_decode__invalid__failure (indivisble_by_5_one_char, "0")
118 
119  // decode invalid data with the maximum representable value
121 
122  // decode invalid data with the minimum value beyond the limit
123  // "%nSc0" is 0xffffffff
124  def_test__zmq_z85_decode__invalid__failure (above_limit, "%nSc1")
125 
126  // decode invalid data with an invalid character in the range of valid
127  // characters
128  def_test__zmq_z85_decode__invalid__failure (char_within, "####\0047")
129 
130  // decode invalid data with an invalid character just below the range of valid
131  // characters
132  def_test__zmq_z85_decode__invalid__failure (char_adjacent_below, "####\0200")
133 
134  // decode invalid data with an invalid character just above the range of valid
135  // characters
136  def_test__zmq_z85_decode__invalid__failure (char_adjacent_above, "####\0037")
137 
138 #define def_test__encode__zmq_z85_decode__roundtrip(name, param) \
139  def_test__zmq_z85_basename (encode__zmq_z85_decode__roundtrip, name, param)
140 
141  const uint8_t test_data_min[] = {0x00, 0x00, 0x00, 0x00};
142 const uint8_t test_data_max[] = {0xff, 0xff, 0xff, 0xff};
143 
146 
147 #define def_test__decode__zmq_z85_encode__roundtrip(name, param) \
148  def_test__zmq_z85_basename (decode__zmq_z85_encode__roundtrip, name, param)
149 
150  const char test_data_regular[] = "r^/rM9M=rMToK)63O8dCvd9D<PY<7iGlC+{BiSnG";
151 
153 
154  int main ()
155 {
156  UNITY_BEGIN ();
158  RUN_TEST (test__zmq_z85_encode__invalid__failure_1);
159  RUN_TEST (test__zmq_z85_encode__invalid__failure_42);
160 
162  RUN_TEST (
163  test__zmq_z85_decode__invalid__failure_indivisble_by_5_multiple_chars);
164  RUN_TEST (test__zmq_z85_decode__invalid__failure_indivisble_by_5_one_char);
165  RUN_TEST (test__zmq_z85_decode__invalid__failure_max);
166  RUN_TEST (test__zmq_z85_decode__invalid__failure_above_limit);
167  RUN_TEST (test__zmq_z85_decode__invalid__failure_char_within);
168  RUN_TEST (test__zmq_z85_decode__invalid__failure_char_adjacent_below);
169  RUN_TEST (test__zmq_z85_decode__invalid__failure_char_adjacent_above);
170 
171  RUN_TEST (test__zmq_z85_encode__zmq_z85_decode__roundtrip_min);
172  RUN_TEST (test__zmq_z85_encode__zmq_z85_decode__roundtrip_max);
173 
174  RUN_TEST (test__zmq_z85_decode__zmq_z85_encode__roundtrip_regular);
175 
176  return UNITY_END ();
177 }
test_data_regular
const char test_data_regular[]
Definition: test_base85.cpp:150
test__zmq_z85_encode__zmq_z85_decode__roundtrip
void test__zmq_z85_encode__zmq_z85_decode__roundtrip(const uint8_t(&test_data_)[SIZE])
Definition: test_base85.cpp:66
TEST_ASSERT_EQUAL_STRING
#define TEST_ASSERT_EQUAL_STRING(expected, actual)
Definition: unity.h:235
zmq_z85_decode
ZMQ_EXPORT uint8_t * zmq_z85_decode(uint8_t *dest_, const char *string_)
Definition: zmq_utils.cpp:135
NULL
NULL
Definition: test_security_zap.cpp:405
UNITY_END
return UNITY_END()
length
GLenum GLuint GLenum GLsizei length
Definition: glcorearb.h:2695
zmq_errno
ZMQ_EXPORT int zmq_errno(void)
Definition: zmq.cpp:101
EINVAL
#define EINVAL
Definition: errno.hpp:25
RUN_TEST
#define RUN_TEST(func)
Definition: unity_internals.h:615
TEST_ASSERT_EQUAL_UINT8_ARRAY
#define TEST_ASSERT_EQUAL_UINT8_ARRAY(expected, actual, num_elements)
Definition: unity.h:246
errno
int errno
testutil_unity.hpp
zmq_z85_encode
ZMQ_EXPORT char * zmq_z85_encode(char *dest_, const uint8_t *data_, size_t size_)
Definition: zmq_utils.cpp:101
testutil.hpp
test_data_max
const uint8_t test_data_max[]
Definition: test_base85.cpp:142
def_test__zmq_z85_encode__invalid__failure
#define def_test__zmq_z85_encode__invalid__failure(name, param)
Definition: test_base85.cpp:105
size
#define size
Definition: glcorearb.h:2944
def_test__decode__zmq_z85_encode__roundtrip
#define def_test__decode__zmq_z85_encode__roundtrip(name, param)
Definition: test_base85.cpp:147
test__zmq_z85_encode__valid__success
void test__zmq_z85_encode__valid__success()
Definition: test_base85.cpp:15
TEST_ASSERT_EQUAL_INT
#define TEST_ASSERT_EQUAL_INT(expected, actual)
Definition: unity.h:128
test__zmq_z85_decode__valid__success
void test__zmq_z85_decode__valid__success()
Definition: test_base85.cpp:39
test__zmq_z85_decode__zmq_z85_encode__roundtrip
void test__zmq_z85_decode__zmq_z85_encode__roundtrip(const char(&test_data_)[SIZE])
Definition: test_base85.cpp:83
def_test__encode__zmq_z85_decode__roundtrip
#define def_test__encode__zmq_z85_decode__roundtrip(name, param)
setUp
void setUp()
Definition: test_base85.cpp:6
tearDown
void tearDown()
Definition: test_base85.cpp:10
main
int main(int argc, char **argv)
Definition: cppzmq/demo/main.cpp:3
size
GLsizeiptr size
Definition: glcorearb.h:2943
UNITY_BEGIN
UNITY_BEGIN()
test__zmq_z85_decode__invalid__failure
void test__zmq_z85_decode__invalid__failure(const char(&encoded_)[SIZE])
Definition: test_base85.cpp:55
def_test__zmq_z85_decode__invalid__failure
#define def_test__zmq_z85_decode__invalid__failure(name, param)
Definition: test_base85.cpp:111
test__zmq_z85_encode__invalid__failure
void test__zmq_z85_encode__invalid__failure(size_t size_)
Definition: test_base85.cpp:31
TEST_ASSERT_NOT_NULL
#define TEST_ASSERT_NOT_NULL(pointer)
Definition: unity.h:125
TEST_ASSERT_NULL
#define TEST_ASSERT_NULL(pointer)
Definition: unity.h:124


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:59