2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
17 #include <openssl/opensslconf.h>
18 #include <openssl/err.h>
19 #include <openssl/crypto.h>
22 #include "test_main.h"
27 fprintf(stderr, "FATAL : %s != %d\n", #t, b); \
31 static int test_int(void)
33 C(err, 1, TEST_int_eq(1, 1));
34 C(err, 0, TEST_int_eq(1, -1));
35 C(err, 1, TEST_int_ne(1, 2));
36 C(err, 0, TEST_int_ne(3, 3));
37 C(err, 1, TEST_int_lt(4, 9));
38 C(err, 0, TEST_int_lt(9, 4));
39 C(err, 1, TEST_int_le(4, 9));
40 C(err, 1, TEST_int_le(5, 5));
41 C(err, 0, TEST_int_le(9, 4));
42 C(err, 1, TEST_int_gt(8, 5));
43 C(err, 0, TEST_int_gt(5, 8));
44 C(err, 1, TEST_int_ge(8, 5));
45 C(err, 1, TEST_int_ge(6, 6));
46 C(err, 0, TEST_int_ge(5, 8));
53 static int test_uint(void)
55 C(err, 1, TEST_uint_eq(3u, 3u));
56 C(err, 0, TEST_uint_eq(3u, 5u));
57 C(err, 1, TEST_uint_ne(4u, 2u));
58 C(err, 0, TEST_uint_ne(6u, 6u));
59 C(err, 1, TEST_uint_lt(5u, 9u));
60 C(err, 0, TEST_uint_lt(9u, 5u));
61 C(err, 1, TEST_uint_le(5u, 9u));
62 C(err, 1, TEST_uint_le(7u, 7u));
63 C(err, 0, TEST_uint_le(9u, 5u));
64 C(err, 1, TEST_uint_gt(11u, 1u));
65 C(err, 0, TEST_uint_gt(1u, 11u));
66 C(err, 1, TEST_uint_ge(11u, 1u));
67 C(err, 1, TEST_uint_ge(6u, 6u));
68 C(err, 0, TEST_uint_ge(1u, 11u));
75 static int test_char(void)
77 C(err, 1, TEST_char_eq('a', 'a'));
78 C(err, 0, TEST_char_eq('a', 'A'));
79 C(err, 1, TEST_char_ne('a', 'c'));
80 C(err, 0, TEST_char_ne('e', 'e'));
81 C(err, 1, TEST_char_lt('i', 'x'));
82 C(err, 0, TEST_char_lt('x', 'i'));
83 C(err, 1, TEST_char_le('i', 'x'));
84 C(err, 1, TEST_char_le('n', 'n'));
85 C(err, 0, TEST_char_le('x', 'i'));
86 C(err, 1, TEST_char_gt('w', 'n'));
87 C(err, 0, TEST_char_gt('n', 'w'));
88 C(err, 1, TEST_char_ge('w', 'n'));
89 C(err, 1, TEST_char_ge('p', 'p'));
90 C(err, 0, TEST_char_ge('n', 'w'));
97 static int test_uchar(void)
99 C(err, 1, TEST_uchar_eq(49, 49));
100 C(err, 0, TEST_uchar_eq(49, 60));
101 C(err, 1, TEST_uchar_ne(50, 2));
102 C(err, 0, TEST_uchar_ne(66, 66));
103 C(err, 1, TEST_uchar_lt(60, 80));
104 C(err, 0, TEST_uchar_lt(80, 60));
105 C(err, 1, TEST_uchar_le(60, 80));
106 C(err, 1, TEST_uchar_le(78, 78));
107 C(err, 0, TEST_uchar_le(80, 60));
108 C(err, 1, TEST_uchar_gt(88, 37));
109 C(err, 0, TEST_uchar_gt(37, 88));
110 C(err, 1, TEST_uchar_ge(88, 37));
111 C(err, 1, TEST_uchar_ge(66, 66));
112 C(err, 0, TEST_uchar_ge(37, 88));
119 static int test_long(void)
121 C(err, 1, TEST_long_eq(123l, 123l));
122 C(err, 0, TEST_long_eq(123l, -123l));
123 C(err, 1, TEST_long_ne(123l, 500l));
124 C(err, 0, TEST_long_ne(1000l, 1000l));
125 C(err, 1, TEST_long_lt(-8923l, 102934563l));
126 C(err, 0, TEST_long_lt(102934563l, -8923l));
127 C(err, 1, TEST_long_le(-8923l, 102934563l));
128 C(err, 1, TEST_long_le(12345l, 12345l));
129 C(err, 0, TEST_long_le(102934563l, -8923l));
130 C(err, 1, TEST_long_gt(84325677l, 12345l));
131 C(err, 0, TEST_long_gt(12345l, 84325677l));
132 C(err, 1, TEST_long_ge(84325677l, 12345l));
133 C(err, 1, TEST_long_ge(465869l, 465869l));
134 C(err, 0, TEST_long_ge(12345l, 84325677l));
141 static int test_ulong(void)
143 C(err, 1, TEST_ulong_eq(919ul, 919ul));
144 C(err, 0, TEST_ulong_eq(919ul, 10234ul));
145 C(err, 1, TEST_ulong_ne(8190ul, 66ul));
146 C(err, 0, TEST_ulong_ne(10555ul, 10555ul));
147 C(err, 1, TEST_ulong_lt(10234ul, 1000000ul));
148 C(err, 0, TEST_ulong_lt(1000000ul, 10234ul));
149 C(err, 1, TEST_ulong_le(10234ul, 1000000ul));
150 C(err, 1, TEST_ulong_le(100000ul, 100000ul));
151 C(err, 0, TEST_ulong_le(1000000ul, 10234ul));
152 C(err, 1, TEST_ulong_gt(100000000ul, 22ul));
153 C(err, 0, TEST_ulong_gt(22ul, 100000000ul));
154 C(err, 1, TEST_ulong_ge(100000000ul, 22ul));
155 C(err, 1, TEST_ulong_ge(10555ul, 10555ul));
156 C(err, 0, TEST_ulong_ge(22ul, 100000000ul));
163 static int test_size_t(void)
165 C(err, 1, TEST_int_eq((size_t)10, (size_t)10));
166 C(err, 0, TEST_int_eq((size_t)10, (size_t)12));
167 C(err, 1, TEST_int_ne((size_t)10, (size_t)12));
168 C(err, 0, TEST_int_ne((size_t)24, (size_t)24));
169 C(err, 1, TEST_int_lt((size_t)30, (size_t)88));
170 C(err, 0, TEST_int_lt((size_t)88, (size_t)30));
171 C(err, 1, TEST_int_le((size_t)30, (size_t)88));
172 C(err, 1, TEST_int_le((size_t)33, (size_t)33));
173 C(err, 0, TEST_int_le((size_t)88, (size_t)30));
174 C(err, 1, TEST_int_gt((size_t)52, (size_t)33));
175 C(err, 0, TEST_int_gt((size_t)33, (size_t)52));
176 C(err, 1, TEST_int_ge((size_t)52, (size_t)33));
177 C(err, 1, TEST_int_ge((size_t)38, (size_t)38));
178 C(err, 0, TEST_int_ge((size_t)33, (size_t)52));
185 static int test_pointer(void)
190 C(err, 1, TEST_ptr(&y));
191 C(err, 0, TEST_ptr(NULL));
192 C(err, 0, TEST_ptr_null(&y));
193 C(err, 1, TEST_ptr_null(NULL));
194 C(err, 1, TEST_ptr_eq(NULL, NULL));
195 C(err, 0, TEST_ptr_eq(NULL, &y));
196 C(err, 0, TEST_ptr_eq(&y, NULL));
197 C(err, 0, TEST_ptr_eq(&y, &x));
198 C(err, 1, TEST_ptr_eq(&x, &x));
199 C(err, 0, TEST_ptr_ne(NULL, NULL));
200 C(err, 1, TEST_ptr_ne(NULL, &y));
201 C(err, 1, TEST_ptr_ne(&y, NULL));
202 C(err, 1, TEST_ptr_ne(&y, &x));
203 C(err, 0, TEST_ptr_ne(&x, &x));
210 static int test_bool(void)
212 C(err, 0, TEST_true(0));
213 C(err, 1, TEST_true(1));
214 C(err, 1, TEST_false(0));
215 C(err, 0, TEST_false(1));
222 static int test_string(void)
224 static char buf[] = "abc";
225 C(err, 1, TEST_str_eq(NULL, NULL));
226 C(err, 1, TEST_str_eq("abc", buf));
227 C(err, 0, TEST_str_eq("abc", NULL));
228 C(err, 0, TEST_str_eq(NULL, buf));
229 C(err, 0, TEST_str_ne(NULL, NULL));
230 C(err, 0, TEST_str_ne("abc", buf));
231 C(err, 1, TEST_str_ne("abc", NULL));
232 C(err, 1, TEST_str_ne(NULL, buf));
239 static int test_memory(void)
241 static char buf[] = "xyz";
242 C(err, 1, TEST_mem_eq(NULL, 0, NULL, 0));
243 C(err, 1, TEST_mem_eq(NULL, 1, NULL, 2));
244 C(err, 0, TEST_mem_eq(NULL, 0, "xyz", 3));
245 C(err, 0, TEST_mem_eq(NULL, 0, "", 0));
246 C(err, 0, TEST_mem_eq("xyz", 3, NULL, 0));
247 C(err, 0, TEST_mem_eq("xyz", 3, buf, sizeof(buf)));
248 C(err, 1, TEST_mem_eq("xyz", 4, buf, sizeof(buf)));
255 static int test_messages(void)
257 TEST_info("This is an %s message.", "info");
258 TEST_error("This is an %s message.", "error");
262 static int test_single_eval(void)
267 unsigned char uc = 22;
268 unsigned long ul = 500;
270 char buf[4] = { 0 }, *p = buf;
273 return TEST_int_eq(i++, 4)
275 && TEST_int_gt(++i, 5)
276 && TEST_int_le(5, i++)
277 && TEST_int_ne(--i, 5)
278 && TEST_int_eq(12, i *= 2)
280 && TEST_long_eq(l--, -9000L)
281 && TEST_long_eq(++l, -9000L)
282 && TEST_long_ne(-9000L, l /= 2)
283 && TEST_long_lt(--l, -4500L)
285 && TEST_char_eq(++c, 'e')
286 && TEST_char_eq('e', c--)
287 && TEST_char_ne('d', --c)
288 && TEST_char_le('b', --c)
289 && TEST_char_lt(c++, 'c')
291 && TEST_uchar_eq(22, uc++)
292 && TEST_uchar_eq(uc /= 2, 11)
293 && TEST_ulong_eq(ul ^= 1, 501)
294 && TEST_ulong_eq(502, ul ^= 3)
295 && TEST_ulong_eq(ul = ul * 3 - 6, 1500)
297 && TEST_size_t_eq((--i, st++), 1234)
298 && TEST_size_t_eq(st, 1235)
299 && TEST_int_eq(11, i)
301 && TEST_ptr_eq(p++, buf)
302 && TEST_ptr_eq(buf + 2, ++p)
303 && TEST_ptr_eq(buf, p -= 2)
305 && TEST_ptr_eq(p, buf + 1)
306 && TEST_ptr_null(p = NULL)
308 && TEST_str_eq(p = "123456" + 1, "23456")
309 && TEST_str_eq("3456", ++p)
310 && TEST_str_ne(p++, "456")
312 && TEST_mem_eq(--p, sizeof("3456"), "3456", sizeof("3456"))
313 && TEST_mem_ne(p++, sizeof("456"), "456", sizeof("456"))
314 && TEST_mem_eq(p--, sizeof("456"), "456", sizeof("456"));
317 void register_tests(void)
322 ADD_TEST(test_uchar);
324 ADD_TEST(test_ulong);
325 ADD_TEST(test_size_t);
326 ADD_TEST(test_pointer);
328 ADD_TEST(test_string);
329 ADD_TEST(test_memory);
330 ADD_TEST(test_messages);
331 ADD_TEST(test_single_eval);