lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-2023 The OpenSSL Project Authors. All Rights Reserved. |
| 3 | * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
| 4 | * |
| 5 | * Licensed under the OpenSSL license (the "License"). You may not use |
| 6 | * this file except in compliance with the License. You can obtain a copy |
| 7 | * in the file LICENSE in the source distribution or at |
| 8 | * https://www.openssl.org/source/license.html |
| 9 | */ |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <string.h> |
| 13 | |
| 14 | #include <openssl/opensslconf.h> |
| 15 | #include <openssl/err.h> |
| 16 | #include <openssl/crypto.h> |
| 17 | #include <openssl/bn.h> |
| 18 | |
| 19 | #include "internal/nelem.h" |
| 20 | #include "testutil.h" |
| 21 | |
| 22 | #define TEST(expected, test) test_case((expected), #test, (test)) |
| 23 | |
| 24 | static int test_case(int expected, const char *test, int result) |
| 25 | { |
| 26 | if (result != expected) { |
| 27 | fprintf(stderr, "# FATAL: %s != %d\n", test, expected); |
| 28 | return 0; |
| 29 | } |
| 30 | return 1; |
| 31 | } |
| 32 | |
| 33 | static int test_int(void) |
| 34 | { |
| 35 | if (!TEST(1, TEST_int_eq(1, 1)) |
| 36 | || !TEST(0, TEST_int_eq(1, -1)) |
| 37 | || !TEST(1, TEST_int_ne(1, 2)) |
| 38 | || !TEST(0, TEST_int_ne(3, 3)) |
| 39 | || !TEST(1, TEST_int_lt(4, 9)) |
| 40 | || !TEST(0, TEST_int_lt(9, 4)) |
| 41 | || !TEST(1, TEST_int_le(4, 9)) |
| 42 | || !TEST(1, TEST_int_le(5, 5)) |
| 43 | || !TEST(0, TEST_int_le(9, 4)) |
| 44 | || !TEST(1, TEST_int_gt(8, 5)) |
| 45 | || !TEST(0, TEST_int_gt(5, 8)) |
| 46 | || !TEST(1, TEST_int_ge(8, 5)) |
| 47 | || !TEST(1, TEST_int_ge(6, 6)) |
| 48 | || !TEST(0, TEST_int_ge(5, 8))) |
| 49 | goto err; |
| 50 | return 1; |
| 51 | |
| 52 | err: |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static int test_uint(void) |
| 57 | { |
| 58 | if (!TEST(1, TEST_uint_eq(3u, 3u)) |
| 59 | || !TEST(0, TEST_uint_eq(3u, 5u)) |
| 60 | || !TEST(1, TEST_uint_ne(4u, 2u)) |
| 61 | || !TEST(0, TEST_uint_ne(6u, 6u)) |
| 62 | || !TEST(1, TEST_uint_lt(5u, 9u)) |
| 63 | || !TEST(0, TEST_uint_lt(9u, 5u)) |
| 64 | || !TEST(1, TEST_uint_le(5u, 9u)) |
| 65 | || !TEST(1, TEST_uint_le(7u, 7u)) |
| 66 | || !TEST(0, TEST_uint_le(9u, 5u)) |
| 67 | || !TEST(1, TEST_uint_gt(11u, 1u)) |
| 68 | || !TEST(0, TEST_uint_gt(1u, 11u)) |
| 69 | || !TEST(1, TEST_uint_ge(11u, 1u)) |
| 70 | || !TEST(1, TEST_uint_ge(6u, 6u)) |
| 71 | || !TEST(0, TEST_uint_ge(1u, 11u))) |
| 72 | goto err; |
| 73 | return 1; |
| 74 | |
| 75 | err: |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static int test_char(void) |
| 80 | { |
| 81 | if (!TEST(1, TEST_char_eq('a', 'a')) |
| 82 | || !TEST(0, TEST_char_eq('a', 'A')) |
| 83 | || !TEST(1, TEST_char_ne('a', 'c')) |
| 84 | || !TEST(0, TEST_char_ne('e', 'e')) |
| 85 | || !TEST(1, TEST_char_lt('i', 'x')) |
| 86 | || !TEST(0, TEST_char_lt('x', 'i')) |
| 87 | || !TEST(1, TEST_char_le('i', 'x')) |
| 88 | || !TEST(1, TEST_char_le('n', 'n')) |
| 89 | || !TEST(0, TEST_char_le('x', 'i')) |
| 90 | || !TEST(1, TEST_char_gt('w', 'n')) |
| 91 | || !TEST(0, TEST_char_gt('n', 'w')) |
| 92 | || !TEST(1, TEST_char_ge('w', 'n')) |
| 93 | || !TEST(1, TEST_char_ge('p', 'p')) |
| 94 | || !TEST(0, TEST_char_ge('n', 'w'))) |
| 95 | goto err; |
| 96 | return 1; |
| 97 | |
| 98 | err: |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static int test_uchar(void) |
| 103 | { |
| 104 | if (!TEST(1, TEST_uchar_eq(49, 49)) |
| 105 | || !TEST(0, TEST_uchar_eq(49, 60)) |
| 106 | || !TEST(1, TEST_uchar_ne(50, 2)) |
| 107 | || !TEST(0, TEST_uchar_ne(66, 66)) |
| 108 | || !TEST(1, TEST_uchar_lt(60, 80)) |
| 109 | || !TEST(0, TEST_uchar_lt(80, 60)) |
| 110 | || !TEST(1, TEST_uchar_le(60, 80)) |
| 111 | || !TEST(1, TEST_uchar_le(78, 78)) |
| 112 | || !TEST(0, TEST_uchar_le(80, 60)) |
| 113 | || !TEST(1, TEST_uchar_gt(88, 37)) |
| 114 | || !TEST(0, TEST_uchar_gt(37, 88)) |
| 115 | || !TEST(1, TEST_uchar_ge(88, 37)) |
| 116 | || !TEST(1, TEST_uchar_ge(66, 66)) |
| 117 | || !TEST(0, TEST_uchar_ge(37, 88))) |
| 118 | goto err; |
| 119 | return 1; |
| 120 | |
| 121 | err: |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static int test_long(void) |
| 126 | { |
| 127 | if (!TEST(1, TEST_long_eq(123l, 123l)) |
| 128 | || !TEST(0, TEST_long_eq(123l, -123l)) |
| 129 | || !TEST(1, TEST_long_ne(123l, 500l)) |
| 130 | || !TEST(0, TEST_long_ne(1000l, 1000l)) |
| 131 | || !TEST(1, TEST_long_lt(-8923l, 102934563l)) |
| 132 | || !TEST(0, TEST_long_lt(102934563l, -8923l)) |
| 133 | || !TEST(1, TEST_long_le(-8923l, 102934563l)) |
| 134 | || !TEST(1, TEST_long_le(12345l, 12345l)) |
| 135 | || !TEST(0, TEST_long_le(102934563l, -8923l)) |
| 136 | || !TEST(1, TEST_long_gt(84325677l, 12345l)) |
| 137 | || !TEST(0, TEST_long_gt(12345l, 84325677l)) |
| 138 | || !TEST(1, TEST_long_ge(84325677l, 12345l)) |
| 139 | || !TEST(1, TEST_long_ge(465869l, 465869l)) |
| 140 | || !TEST(0, TEST_long_ge(12345l, 84325677l))) |
| 141 | goto err; |
| 142 | return 1; |
| 143 | |
| 144 | err: |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static int test_ulong(void) |
| 149 | { |
| 150 | if (!TEST(1, TEST_ulong_eq(919ul, 919ul)) |
| 151 | || !TEST(0, TEST_ulong_eq(919ul, 10234ul)) |
| 152 | || !TEST(1, TEST_ulong_ne(8190ul, 66ul)) |
| 153 | || !TEST(0, TEST_ulong_ne(10555ul, 10555ul)) |
| 154 | || !TEST(1, TEST_ulong_lt(10234ul, 1000000ul)) |
| 155 | || !TEST(0, TEST_ulong_lt(1000000ul, 10234ul)) |
| 156 | || !TEST(1, TEST_ulong_le(10234ul, 1000000ul)) |
| 157 | || !TEST(1, TEST_ulong_le(100000ul, 100000ul)) |
| 158 | || !TEST(0, TEST_ulong_le(1000000ul, 10234ul)) |
| 159 | || !TEST(1, TEST_ulong_gt(100000000ul, 22ul)) |
| 160 | || !TEST(0, TEST_ulong_gt(22ul, 100000000ul)) |
| 161 | || !TEST(1, TEST_ulong_ge(100000000ul, 22ul)) |
| 162 | || !TEST(1, TEST_ulong_ge(10555ul, 10555ul)) |
| 163 | || !TEST(0, TEST_ulong_ge(22ul, 100000000ul))) |
| 164 | goto err; |
| 165 | return 1; |
| 166 | |
| 167 | err: |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | static int test_size_t(void) |
| 172 | { |
| 173 | if (!TEST(1, TEST_size_t_eq((size_t)10, (size_t)10)) |
| 174 | || !TEST(0, TEST_size_t_eq((size_t)10, (size_t)12)) |
| 175 | || !TEST(1, TEST_size_t_ne((size_t)10, (size_t)12)) |
| 176 | || !TEST(0, TEST_size_t_ne((size_t)24, (size_t)24)) |
| 177 | || !TEST(1, TEST_size_t_lt((size_t)30, (size_t)88)) |
| 178 | || !TEST(0, TEST_size_t_lt((size_t)88, (size_t)30)) |
| 179 | || !TEST(1, TEST_size_t_le((size_t)30, (size_t)88)) |
| 180 | || !TEST(1, TEST_size_t_le((size_t)33, (size_t)33)) |
| 181 | || !TEST(0, TEST_size_t_le((size_t)88, (size_t)30)) |
| 182 | || !TEST(1, TEST_size_t_gt((size_t)52, (size_t)33)) |
| 183 | || !TEST(0, TEST_size_t_gt((size_t)33, (size_t)52)) |
| 184 | || !TEST(1, TEST_size_t_ge((size_t)52, (size_t)33)) |
| 185 | || !TEST(1, TEST_size_t_ge((size_t)38, (size_t)38)) |
| 186 | || !TEST(0, TEST_size_t_ge((size_t)33, (size_t)52))) |
| 187 | goto err; |
| 188 | return 1; |
| 189 | |
| 190 | err: |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static int test_time_t(void) |
| 195 | { |
| 196 | if (!TEST(1, TEST_time_t_eq((time_t)10, (time_t)10)) |
| 197 | || !TEST(0, TEST_time_t_eq((time_t)10, (time_t)12)) |
| 198 | || !TEST(1, TEST_time_t_ne((time_t)10, (time_t)12)) |
| 199 | || !TEST(0, TEST_time_t_ne((time_t)24, (time_t)24)) |
| 200 | || !TEST(1, TEST_time_t_lt((time_t)30, (time_t)88)) |
| 201 | || !TEST(0, TEST_time_t_lt((time_t)88, (time_t)30)) |
| 202 | || !TEST(1, TEST_time_t_le((time_t)30, (time_t)88)) |
| 203 | || !TEST(1, TEST_time_t_le((time_t)33, (time_t)33)) |
| 204 | || !TEST(0, TEST_time_t_le((time_t)88, (time_t)30)) |
| 205 | || !TEST(1, TEST_time_t_gt((time_t)52, (time_t)33)) |
| 206 | || !TEST(0, TEST_time_t_gt((time_t)33, (time_t)52)) |
| 207 | || !TEST(1, TEST_time_t_ge((time_t)52, (time_t)33)) |
| 208 | || !TEST(1, TEST_time_t_ge((time_t)38, (time_t)38)) |
| 209 | || !TEST(0, TEST_time_t_ge((time_t)33, (time_t)52))) |
| 210 | goto err; |
| 211 | return 1; |
| 212 | |
| 213 | err: |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static int test_pointer(void) |
| 218 | { |
| 219 | int x = 0; |
| 220 | char y = 1; |
| 221 | |
| 222 | if (!TEST(1, TEST_ptr(&y)) |
| 223 | || !TEST(0, TEST_ptr(NULL)) |
| 224 | || !TEST(0, TEST_ptr_null(&y)) |
| 225 | || !TEST(1, TEST_ptr_null(NULL)) |
| 226 | || !TEST(1, TEST_ptr_eq(NULL, NULL)) |
| 227 | || !TEST(0, TEST_ptr_eq(NULL, &y)) |
| 228 | || !TEST(0, TEST_ptr_eq(&y, NULL)) |
| 229 | || !TEST(0, TEST_ptr_eq(&y, &x)) |
| 230 | || !TEST(1, TEST_ptr_eq(&x, &x)) |
| 231 | || !TEST(0, TEST_ptr_ne(NULL, NULL)) |
| 232 | || !TEST(1, TEST_ptr_ne(NULL, &y)) |
| 233 | || !TEST(1, TEST_ptr_ne(&y, NULL)) |
| 234 | || !TEST(1, TEST_ptr_ne(&y, &x)) |
| 235 | || !TEST(0, TEST_ptr_ne(&x, &x))) |
| 236 | goto err; |
| 237 | return 1; |
| 238 | |
| 239 | err: |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | static int test_bool(void) |
| 244 | { |
| 245 | if (!TEST(0, TEST_true(0)) |
| 246 | || !TEST(1, TEST_true(1)) |
| 247 | || !TEST(1, TEST_false(0)) |
| 248 | || !TEST(0, TEST_false(1))) |
| 249 | goto err; |
| 250 | return 1; |
| 251 | |
| 252 | err: |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static int test_string(void) |
| 257 | { |
| 258 | static char buf[] = "abc"; |
| 259 | |
| 260 | if (!TEST(1, TEST_str_eq(NULL, NULL)) |
| 261 | || !TEST(1, TEST_str_eq("abc", buf)) |
| 262 | || !TEST(0, TEST_str_eq("abc", NULL)) |
| 263 | || !TEST(0, TEST_str_eq("abc", "")) |
| 264 | || !TEST(0, TEST_str_eq(NULL, buf)) |
| 265 | || !TEST(0, TEST_str_ne(NULL, NULL)) |
| 266 | || !TEST(0, TEST_str_eq("", NULL)) |
| 267 | || !TEST(0, TEST_str_eq(NULL, "")) |
| 268 | || !TEST(0, TEST_str_ne("", "")) |
| 269 | || !TEST(0, TEST_str_eq("\1\2\3\4\5", "\1x\3\6\5")) |
| 270 | || !TEST(0, TEST_str_ne("abc", buf)) |
| 271 | || !TEST(1, TEST_str_ne("abc", NULL)) |
| 272 | || !TEST(1, TEST_str_ne(NULL, buf)) |
| 273 | || !TEST(0, TEST_str_eq("abcdef", "abcdefghijk"))) |
| 274 | goto err; |
| 275 | return 1; |
| 276 | |
| 277 | err: |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | static int test_memory(void) |
| 282 | { |
| 283 | static char buf[] = "xyz"; |
| 284 | |
| 285 | if (!TEST(1, TEST_mem_eq(NULL, 0, NULL, 0)) |
| 286 | || !TEST(1, TEST_mem_eq(NULL, 1, NULL, 2)) |
| 287 | || !TEST(0, TEST_mem_eq(NULL, 0, "xyz", 3)) |
| 288 | || !TEST(0, TEST_mem_eq(NULL, 7, "abc", 3)) |
| 289 | || !TEST(0, TEST_mem_ne(NULL, 0, NULL, 0)) |
| 290 | || !TEST(0, TEST_mem_eq(NULL, 0, "", 0)) |
| 291 | || !TEST(0, TEST_mem_eq("", 0, NULL, 0)) |
| 292 | || !TEST(0, TEST_mem_ne("", 0, "", 0)) |
| 293 | || !TEST(0, TEST_mem_eq("xyz", 3, NULL, 0)) |
| 294 | || !TEST(0, TEST_mem_eq("xyz", 3, buf, sizeof(buf))) |
| 295 | || !TEST(1, TEST_mem_eq("xyz", 4, buf, sizeof(buf)))) |
| 296 | goto err; |
| 297 | return 1; |
| 298 | |
| 299 | err: |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | static int test_memory_overflow(void) |
| 304 | { |
| 305 | /* Verify that the memory printing overflows without walking the stack */ |
| 306 | const char *p = "1234567890123456789012345678901234567890123456789012"; |
| 307 | const char *q = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 308 | |
| 309 | return TEST(0, TEST_mem_eq(p, strlen(p), q, strlen(q))); |
| 310 | } |
| 311 | |
| 312 | static int test_bignum(void) |
| 313 | { |
| 314 | BIGNUM *a = NULL, *b = NULL, *c = NULL; |
| 315 | int r = 0; |
| 316 | |
| 317 | if (!TEST(1, TEST_int_eq(BN_dec2bn(&a, "0"), 1)) |
| 318 | || !TEST(1, TEST_BN_eq_word(a, 0)) |
| 319 | || !TEST(0, TEST_BN_eq_word(a, 30)) |
| 320 | || !TEST(1, TEST_BN_abs_eq_word(a, 0)) |
| 321 | || !TEST(0, TEST_BN_eq_one(a)) |
| 322 | || !TEST(1, TEST_BN_eq_zero(a)) |
| 323 | || !TEST(0, TEST_BN_ne_zero(a)) |
| 324 | || !TEST(1, TEST_BN_le_zero(a)) |
| 325 | || !TEST(0, TEST_BN_lt_zero(a)) |
| 326 | || !TEST(1, TEST_BN_ge_zero(a)) |
| 327 | || !TEST(0, TEST_BN_gt_zero(a)) |
| 328 | || !TEST(1, TEST_BN_even(a)) |
| 329 | || !TEST(0, TEST_BN_odd(a)) |
| 330 | || !TEST(1, TEST_BN_eq(b, c)) |
| 331 | || !TEST(0, TEST_BN_eq(a, b)) |
| 332 | || !TEST(0, TEST_BN_ne(NULL, c)) |
| 333 | || !TEST(1, TEST_int_eq(BN_dec2bn(&b, "1"), 1)) |
| 334 | || !TEST(1, TEST_BN_eq_word(b, 1)) |
| 335 | || !TEST(1, TEST_BN_eq_one(b)) |
| 336 | || !TEST(0, TEST_BN_abs_eq_word(b, 0)) |
| 337 | || !TEST(1, TEST_BN_abs_eq_word(b, 1)) |
| 338 | || !TEST(0, TEST_BN_eq_zero(b)) |
| 339 | || !TEST(1, TEST_BN_ne_zero(b)) |
| 340 | || !TEST(0, TEST_BN_le_zero(b)) |
| 341 | || !TEST(0, TEST_BN_lt_zero(b)) |
| 342 | || !TEST(1, TEST_BN_ge_zero(b)) |
| 343 | || !TEST(1, TEST_BN_gt_zero(b)) |
| 344 | || !TEST(0, TEST_BN_even(b)) |
| 345 | || !TEST(1, TEST_BN_odd(b)) |
| 346 | || !TEST(1, TEST_int_eq(BN_dec2bn(&c, "-334739439"), 10)) |
| 347 | || !TEST(0, TEST_BN_eq_word(c, 334739439)) |
| 348 | || !TEST(1, TEST_BN_abs_eq_word(c, 334739439)) |
| 349 | || !TEST(0, TEST_BN_eq_zero(c)) |
| 350 | || !TEST(1, TEST_BN_ne_zero(c)) |
| 351 | || !TEST(1, TEST_BN_le_zero(c)) |
| 352 | || !TEST(1, TEST_BN_lt_zero(c)) |
| 353 | || !TEST(0, TEST_BN_ge_zero(c)) |
| 354 | || !TEST(0, TEST_BN_gt_zero(c)) |
| 355 | || !TEST(0, TEST_BN_even(c)) |
| 356 | || !TEST(1, TEST_BN_odd(c)) |
| 357 | || !TEST(1, TEST_BN_eq(a, a)) |
| 358 | || !TEST(0, TEST_BN_ne(a, a)) |
| 359 | || !TEST(0, TEST_BN_eq(a, b)) |
| 360 | || !TEST(1, TEST_BN_ne(a, b)) |
| 361 | || !TEST(0, TEST_BN_lt(a, c)) |
| 362 | || !TEST(1, TEST_BN_lt(c, b)) |
| 363 | || !TEST(0, TEST_BN_lt(b, c)) |
| 364 | || !TEST(0, TEST_BN_le(a, c)) |
| 365 | || !TEST(1, TEST_BN_le(c, b)) |
| 366 | || !TEST(0, TEST_BN_le(b, c)) |
| 367 | || !TEST(1, TEST_BN_gt(a, c)) |
| 368 | || !TEST(0, TEST_BN_gt(c, b)) |
| 369 | || !TEST(1, TEST_BN_gt(b, c)) |
| 370 | || !TEST(1, TEST_BN_ge(a, c)) |
| 371 | || !TEST(0, TEST_BN_ge(c, b)) |
| 372 | || !TEST(1, TEST_BN_ge(b, c))) |
| 373 | goto err; |
| 374 | |
| 375 | r = 1; |
| 376 | err: |
| 377 | BN_free(a); |
| 378 | BN_free(b); |
| 379 | BN_free(c); |
| 380 | return r; |
| 381 | } |
| 382 | |
| 383 | static int test_long_output(void) |
| 384 | { |
| 385 | const char *p = "1234567890123456789012345678901234567890123456789012"; |
| 386 | const char *q = "1234567890klmnopqrs01234567890EFGHIJKLM0123456789XYZ"; |
| 387 | const char *r = "1234567890123456789012345678901234567890123456789012" |
| 388 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY+" |
| 389 | "12345678901234567890123ABC78901234567890123456789012"; |
| 390 | const char *s = "1234567890123456789012345678901234567890123456789012" |
| 391 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY-" |
| 392 | "1234567890123456789012345678901234567890123456789012" |
| 393 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 394 | |
| 395 | return TEST(0, TEST_str_eq(p, q)) |
| 396 | & TEST(0, TEST_str_eq(q, r)) |
| 397 | & TEST(0, TEST_str_eq(r, s)) |
| 398 | & TEST(0, TEST_mem_eq(r, strlen(r), s, strlen(s))); |
| 399 | } |
| 400 | |
| 401 | static int test_long_bignum(void) |
| 402 | { |
| 403 | int r; |
| 404 | BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL; |
| 405 | const char as[] = "1234567890123456789012345678901234567890123456789012" |
| 406 | "1234567890123456789012345678901234567890123456789012" |
| 407 | "1234567890123456789012345678901234567890123456789012" |
| 408 | "1234567890123456789012345678901234567890123456789012" |
| 409 | "1234567890123456789012345678901234567890123456789012" |
| 410 | "1234567890123456789012345678901234567890123456789012" |
| 411 | "FFFFFF"; |
| 412 | const char bs[] = "1234567890123456789012345678901234567890123456789012" |
| 413 | "1234567890123456789012345678901234567890123456789013" |
| 414 | "987657"; |
| 415 | const char cs[] = "-" /* 64 characters plus sign */ |
| 416 | "123456789012345678901234567890" |
| 417 | "123456789012345678901234567890" |
| 418 | "ABCD"; |
| 419 | const char ds[] = "-" /* 63 characters plus sign */ |
| 420 | "23456789A123456789B123456789C" |
| 421 | "123456789D123456789E123456789F" |
| 422 | "ABCD"; |
| 423 | |
| 424 | r = TEST_true(BN_hex2bn(&a, as)) |
| 425 | && TEST_true(BN_hex2bn(&b, bs)) |
| 426 | && TEST_true(BN_hex2bn(&c, cs)) |
| 427 | && TEST_true(BN_hex2bn(&d, ds)) |
| 428 | && (TEST(0, TEST_BN_eq(a, b)) |
| 429 | & TEST(0, TEST_BN_eq(b, a)) |
| 430 | & TEST(0, TEST_BN_eq(b, NULL)) |
| 431 | & TEST(0, TEST_BN_eq(NULL, a)) |
| 432 | & TEST(1, TEST_BN_ne(a, NULL)) |
| 433 | & TEST(0, TEST_BN_eq(c, d))); |
| 434 | BN_free(a); |
| 435 | BN_free(b); |
| 436 | BN_free(c); |
| 437 | BN_free(d); |
| 438 | return r; |
| 439 | } |
| 440 | |
| 441 | static int test_messages(void) |
| 442 | { |
| 443 | TEST_info("This is an %s message.", "info"); |
| 444 | TEST_error("This is an %s message.", "error"); |
| 445 | return 1; |
| 446 | } |
| 447 | |
| 448 | static int test_single_eval(void) |
| 449 | { |
| 450 | int i = 4; |
| 451 | long l = -9000; |
| 452 | char c = 'd'; |
| 453 | unsigned char uc = 22; |
| 454 | unsigned long ul = 500; |
| 455 | size_t st = 1234; |
| 456 | char buf[4] = { 0 }, *p = buf; |
| 457 | |
| 458 | /* int */ |
| 459 | return TEST_int_eq(i++, 4) |
| 460 | && TEST_int_eq(i, 5) |
| 461 | && TEST_int_gt(++i, 5) |
| 462 | && TEST_int_le(5, i++) |
| 463 | && TEST_int_ne(--i, 5) |
| 464 | && TEST_int_eq(12, i *= 2) |
| 465 | /* Long */ |
| 466 | && TEST_long_eq(l--, -9000L) |
| 467 | && TEST_long_eq(++l, -9000L) |
| 468 | && TEST_long_ne(-9000L, l /= 2) |
| 469 | && TEST_long_lt(--l, -4500L) |
| 470 | /* char */ |
| 471 | && TEST_char_eq(++c, 'e') |
| 472 | && TEST_char_eq('e', c--) |
| 473 | && TEST_char_ne('d', --c) |
| 474 | && TEST_char_le('b', --c) |
| 475 | && TEST_char_lt(c++, 'c') |
| 476 | /* unsigned char */ |
| 477 | && TEST_uchar_eq(22, uc++) |
| 478 | && TEST_uchar_eq(uc /= 2, 11) |
| 479 | && TEST_ulong_eq(ul ^= 1, 501) |
| 480 | && TEST_ulong_eq(502, ul ^= 3) |
| 481 | && TEST_ulong_eq(ul = ul * 3 - 6, 1500) |
| 482 | /* size_t */ |
| 483 | && TEST_size_t_eq((--i, st++), 1234) |
| 484 | && TEST_size_t_eq(st, 1235) |
| 485 | && TEST_int_eq(11, i) |
| 486 | /* pointers */ |
| 487 | && TEST_ptr_eq(p++, buf) |
| 488 | && TEST_ptr_eq(buf + 2, ++p) |
| 489 | && TEST_ptr_eq(buf, p -= 2) |
| 490 | && TEST_ptr(++p) |
| 491 | && TEST_ptr_eq(p, buf + 1) |
| 492 | && TEST_ptr_null(p = NULL) |
| 493 | /* strings */ |
| 494 | && TEST_str_eq(p = &("123456"[1]), "23456") |
| 495 | && TEST_str_eq("3456", ++p) |
| 496 | && TEST_str_ne(p++, "456") |
| 497 | /* memory */ |
| 498 | && TEST_mem_eq(--p, sizeof("3456"), "3456", sizeof("3456")) |
| 499 | && TEST_mem_ne(p++, sizeof("456"), "456", sizeof("456")) |
| 500 | && TEST_mem_eq(p--, sizeof("456"), "456", sizeof("456")); |
| 501 | } |
| 502 | |
| 503 | static int test_output(void) |
| 504 | { |
| 505 | const char s[] = "1234567890123456789012345678901234567890123456789012" |
| 506 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 507 | |
| 508 | test_output_string("test", s, sizeof(s) - 1); |
| 509 | test_output_memory("test", (const unsigned char *)s, sizeof(s)); |
| 510 | return 1; |
| 511 | } |
| 512 | |
| 513 | static const char *bn_output_tests[] = { |
| 514 | NULL, |
| 515 | "0", |
| 516 | "-12345678", |
| 517 | "1234567890123456789012345678901234567890123456789012" |
| 518 | "1234567890123456789012345678901234567890123456789013" |
| 519 | "987657" |
| 520 | }; |
| 521 | |
| 522 | static int test_bn_output(int n) |
| 523 | { |
| 524 | BIGNUM *b = NULL; |
| 525 | |
| 526 | if (bn_output_tests[n] != NULL |
| 527 | && !TEST_true(BN_hex2bn(&b, bn_output_tests[n]))) |
| 528 | return 0; |
| 529 | test_output_bignum(bn_output_tests[n], b); |
| 530 | BN_free(b); |
| 531 | return 1; |
| 532 | } |
| 533 | |
| 534 | int setup_tests(void) |
| 535 | { |
| 536 | ADD_TEST(test_int); |
| 537 | ADD_TEST(test_uint); |
| 538 | ADD_TEST(test_char); |
| 539 | ADD_TEST(test_uchar); |
| 540 | ADD_TEST(test_long); |
| 541 | ADD_TEST(test_ulong); |
| 542 | ADD_TEST(test_size_t); |
| 543 | ADD_TEST(test_time_t); |
| 544 | ADD_TEST(test_pointer); |
| 545 | ADD_TEST(test_bool); |
| 546 | ADD_TEST(test_string); |
| 547 | ADD_TEST(test_memory); |
| 548 | ADD_TEST(test_memory_overflow); |
| 549 | ADD_TEST(test_bignum); |
| 550 | ADD_TEST(test_long_bignum); |
| 551 | ADD_TEST(test_long_output); |
| 552 | ADD_TEST(test_messages); |
| 553 | ADD_TEST(test_single_eval); |
| 554 | ADD_TEST(test_output); |
| 555 | ADD_ALL_TESTS(test_bn_output, OSSL_NELEM(bn_output_tests)); |
| 556 | return 1; |
| 557 | } |