1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci#include "../testutil.h" 11e1051a39Sopenharmony_ci#include "output.h" 12e1051a39Sopenharmony_ci#include "tu_local.h" 13e1051a39Sopenharmony_ci 14e1051a39Sopenharmony_ci#include <errno.h> 15e1051a39Sopenharmony_ci#include <string.h> 16e1051a39Sopenharmony_ci#include <ctype.h> 17e1051a39Sopenharmony_ci#include "internal/nelem.h" 18e1051a39Sopenharmony_ci#include <openssl/asn1.h> 19e1051a39Sopenharmony_ci 20e1051a39Sopenharmony_ci/* 21e1051a39Sopenharmony_ci * Output a failed test first line. 22e1051a39Sopenharmony_ci * All items are optional are generally not preinted if passed as NULL. 23e1051a39Sopenharmony_ci * The special cases are for prefix where "ERROR" is assumed and for left 24e1051a39Sopenharmony_ci * and right where a non-failure message is produced if either is NULL. 25e1051a39Sopenharmony_ci */ 26e1051a39Sopenharmony_civoid test_fail_message_prefix(const char *prefix, const char *file, 27e1051a39Sopenharmony_ci int line, const char *type, 28e1051a39Sopenharmony_ci const char *left, const char *right, 29e1051a39Sopenharmony_ci const char *op) 30e1051a39Sopenharmony_ci{ 31e1051a39Sopenharmony_ci test_printf_stderr("%s: ", prefix != NULL ? prefix : "ERROR"); 32e1051a39Sopenharmony_ci if (type) 33e1051a39Sopenharmony_ci test_printf_stderr("(%s) ", type); 34e1051a39Sopenharmony_ci if (op != NULL) { 35e1051a39Sopenharmony_ci if (left != NULL && right != NULL) 36e1051a39Sopenharmony_ci test_printf_stderr("'%s %s %s' failed", left, op, right); 37e1051a39Sopenharmony_ci else 38e1051a39Sopenharmony_ci test_printf_stderr("'%s'", op); 39e1051a39Sopenharmony_ci } 40e1051a39Sopenharmony_ci if (file != NULL) { 41e1051a39Sopenharmony_ci test_printf_stderr(" @ %s:%d", file, line); 42e1051a39Sopenharmony_ci } 43e1051a39Sopenharmony_ci test_printf_stderr("\n"); 44e1051a39Sopenharmony_ci} 45e1051a39Sopenharmony_ci 46e1051a39Sopenharmony_ci/* 47e1051a39Sopenharmony_ci * A common routine to output test failure messages. Generally this should not 48e1051a39Sopenharmony_ci * be called directly, rather it should be called by the following functions. 49e1051a39Sopenharmony_ci * 50e1051a39Sopenharmony_ci * |desc| is a printf formatted description with arguments |args| that is 51e1051a39Sopenharmony_ci * supplied by the user and |desc| can be NULL. |type| is the data type 52e1051a39Sopenharmony_ci * that was tested (int, char, ptr, ...). |fmt| is a system provided 53e1051a39Sopenharmony_ci * printf format with following arguments that spell out the failure 54e1051a39Sopenharmony_ci * details i.e. the actual values compared and the operator used. 55e1051a39Sopenharmony_ci * 56e1051a39Sopenharmony_ci * The typical use for this is from an utility test function: 57e1051a39Sopenharmony_ci * 58e1051a39Sopenharmony_ci * int test6(const char *file, int line, int n) { 59e1051a39Sopenharmony_ci * if (n != 6) { 60e1051a39Sopenharmony_ci * test_fail_message(1, file, line, "int", "value %d is not %d", n, 6); 61e1051a39Sopenharmony_ci * return 0; 62e1051a39Sopenharmony_ci * } 63e1051a39Sopenharmony_ci * return 1; 64e1051a39Sopenharmony_ci * } 65e1051a39Sopenharmony_ci * 66e1051a39Sopenharmony_ci * calling test6(3, "oops") will return 0 and produce out along the lines of: 67e1051a39Sopenharmony_ci * FAIL oops: (int) value 3 is not 6\n 68e1051a39Sopenharmony_ci */ 69e1051a39Sopenharmony_cistatic void test_fail_message(const char *prefix, const char *file, int line, 70e1051a39Sopenharmony_ci const char *type, const char *left, 71e1051a39Sopenharmony_ci const char *right, const char *op, 72e1051a39Sopenharmony_ci const char *fmt, ...) 73e1051a39Sopenharmony_ci PRINTF_FORMAT(8, 9); 74e1051a39Sopenharmony_ci 75e1051a39Sopenharmony_cistatic void test_fail_message_va(const char *prefix, const char *file, 76e1051a39Sopenharmony_ci int line, const char *type, 77e1051a39Sopenharmony_ci const char *left, const char *right, 78e1051a39Sopenharmony_ci const char *op, const char *fmt, va_list ap) 79e1051a39Sopenharmony_ci{ 80e1051a39Sopenharmony_ci test_fail_message_prefix(prefix, file, line, type, left, right, op); 81e1051a39Sopenharmony_ci if (fmt != NULL) { 82e1051a39Sopenharmony_ci test_vprintf_stderr(fmt, ap); 83e1051a39Sopenharmony_ci test_printf_stderr("\n"); 84e1051a39Sopenharmony_ci } 85e1051a39Sopenharmony_ci test_flush_stderr(); 86e1051a39Sopenharmony_ci} 87e1051a39Sopenharmony_ci 88e1051a39Sopenharmony_cistatic void test_fail_message(const char *prefix, const char *file, 89e1051a39Sopenharmony_ci int line, const char *type, 90e1051a39Sopenharmony_ci const char *left, const char *right, 91e1051a39Sopenharmony_ci const char *op, const char *fmt, ...) 92e1051a39Sopenharmony_ci{ 93e1051a39Sopenharmony_ci va_list ap; 94e1051a39Sopenharmony_ci 95e1051a39Sopenharmony_ci va_start(ap, fmt); 96e1051a39Sopenharmony_ci test_fail_message_va(prefix, file, line, type, left, right, op, fmt, ap); 97e1051a39Sopenharmony_ci va_end(ap); 98e1051a39Sopenharmony_ci} 99e1051a39Sopenharmony_ci 100e1051a39Sopenharmony_civoid test_info_c90(const char *desc, ...) 101e1051a39Sopenharmony_ci{ 102e1051a39Sopenharmony_ci va_list ap; 103e1051a39Sopenharmony_ci 104e1051a39Sopenharmony_ci va_start(ap, desc); 105e1051a39Sopenharmony_ci test_fail_message_va("INFO", NULL, -1, NULL, NULL, NULL, NULL, desc, ap); 106e1051a39Sopenharmony_ci va_end(ap); 107e1051a39Sopenharmony_ci} 108e1051a39Sopenharmony_ci 109e1051a39Sopenharmony_civoid test_info(const char *file, int line, const char *desc, ...) 110e1051a39Sopenharmony_ci{ 111e1051a39Sopenharmony_ci va_list ap; 112e1051a39Sopenharmony_ci 113e1051a39Sopenharmony_ci va_start(ap, desc); 114e1051a39Sopenharmony_ci test_fail_message_va("INFO", file, line, NULL, NULL, NULL, NULL, desc, ap); 115e1051a39Sopenharmony_ci va_end(ap); 116e1051a39Sopenharmony_ci} 117e1051a39Sopenharmony_ci 118e1051a39Sopenharmony_civoid test_error_c90(const char *desc, ...) 119e1051a39Sopenharmony_ci{ 120e1051a39Sopenharmony_ci va_list ap; 121e1051a39Sopenharmony_ci 122e1051a39Sopenharmony_ci va_start(ap, desc); 123e1051a39Sopenharmony_ci test_fail_message_va(NULL, NULL, -1, NULL, NULL, NULL, NULL, desc, ap); 124e1051a39Sopenharmony_ci va_end(ap); 125e1051a39Sopenharmony_ci test_printf_stderr("\n"); 126e1051a39Sopenharmony_ci} 127e1051a39Sopenharmony_ci 128e1051a39Sopenharmony_civoid test_error(const char *file, int line, const char *desc, ...) 129e1051a39Sopenharmony_ci{ 130e1051a39Sopenharmony_ci va_list ap; 131e1051a39Sopenharmony_ci 132e1051a39Sopenharmony_ci va_start(ap, desc); 133e1051a39Sopenharmony_ci test_fail_message_va(NULL, file, line, NULL, NULL, NULL, NULL, desc, ap); 134e1051a39Sopenharmony_ci va_end(ap); 135e1051a39Sopenharmony_ci test_printf_stderr("\n"); 136e1051a39Sopenharmony_ci} 137e1051a39Sopenharmony_ci 138e1051a39Sopenharmony_civoid test_perror(const char *s) 139e1051a39Sopenharmony_ci{ 140e1051a39Sopenharmony_ci /* 141e1051a39Sopenharmony_ci * Using openssl_strerror_r causes linking issues since it isn't 142e1051a39Sopenharmony_ci * exported from libcrypto.so 143e1051a39Sopenharmony_ci */ 144e1051a39Sopenharmony_ci TEST_error("%s: %s", s, strerror(errno)); 145e1051a39Sopenharmony_ci} 146e1051a39Sopenharmony_ci 147e1051a39Sopenharmony_civoid test_note(const char *fmt, ...) 148e1051a39Sopenharmony_ci{ 149e1051a39Sopenharmony_ci if (fmt != NULL) { 150e1051a39Sopenharmony_ci va_list ap; 151e1051a39Sopenharmony_ci 152e1051a39Sopenharmony_ci va_start(ap, fmt); 153e1051a39Sopenharmony_ci test_vprintf_stderr(fmt, ap); 154e1051a39Sopenharmony_ci va_end(ap); 155e1051a39Sopenharmony_ci test_printf_stderr("\n"); 156e1051a39Sopenharmony_ci } 157e1051a39Sopenharmony_ci test_flush_stderr(); 158e1051a39Sopenharmony_ci} 159e1051a39Sopenharmony_ci 160e1051a39Sopenharmony_ci 161e1051a39Sopenharmony_ciint test_skip(const char *file, int line, const char *desc, ...) 162e1051a39Sopenharmony_ci{ 163e1051a39Sopenharmony_ci va_list ap; 164e1051a39Sopenharmony_ci 165e1051a39Sopenharmony_ci va_start(ap, desc); 166e1051a39Sopenharmony_ci test_fail_message_va("SKIP", file, line, NULL, NULL, NULL, NULL, desc, ap); 167e1051a39Sopenharmony_ci va_end(ap); 168e1051a39Sopenharmony_ci return TEST_SKIP_CODE; 169e1051a39Sopenharmony_ci} 170e1051a39Sopenharmony_ci 171e1051a39Sopenharmony_ciint test_skip_c90(const char *desc, ...) 172e1051a39Sopenharmony_ci{ 173e1051a39Sopenharmony_ci va_list ap; 174e1051a39Sopenharmony_ci 175e1051a39Sopenharmony_ci va_start(ap, desc); 176e1051a39Sopenharmony_ci test_fail_message_va("SKIP", NULL, -1, NULL, NULL, NULL, NULL, desc, ap); 177e1051a39Sopenharmony_ci va_end(ap); 178e1051a39Sopenharmony_ci test_printf_stderr("\n"); 179e1051a39Sopenharmony_ci return TEST_SKIP_CODE; 180e1051a39Sopenharmony_ci} 181e1051a39Sopenharmony_ci 182e1051a39Sopenharmony_ci 183e1051a39Sopenharmony_civoid test_openssl_errors(void) 184e1051a39Sopenharmony_ci{ 185e1051a39Sopenharmony_ci ERR_print_errors_cb(openssl_error_cb, NULL); 186e1051a39Sopenharmony_ci ERR_clear_error(); 187e1051a39Sopenharmony_ci} 188e1051a39Sopenharmony_ci 189e1051a39Sopenharmony_ci/* 190e1051a39Sopenharmony_ci * Define some comparisons between pairs of various types. 191e1051a39Sopenharmony_ci * These functions return 1 if the test is true. 192e1051a39Sopenharmony_ci * Otherwise, they return 0 and pretty-print diagnostics. 193e1051a39Sopenharmony_ci * 194e1051a39Sopenharmony_ci * In each case the functions produced are: 195e1051a39Sopenharmony_ci * int test_name_eq(const type t1, const type t2, const char *desc, ...); 196e1051a39Sopenharmony_ci * int test_name_ne(const type t1, const type t2, const char *desc, ...); 197e1051a39Sopenharmony_ci * int test_name_lt(const type t1, const type t2, const char *desc, ...); 198e1051a39Sopenharmony_ci * int test_name_le(const type t1, const type t2, const char *desc, ...); 199e1051a39Sopenharmony_ci * int test_name_gt(const type t1, const type t2, const char *desc, ...); 200e1051a39Sopenharmony_ci * int test_name_ge(const type t1, const type t2, const char *desc, ...); 201e1051a39Sopenharmony_ci * 202e1051a39Sopenharmony_ci * The t1 and t2 arguments are to be compared for equality, inequality, 203e1051a39Sopenharmony_ci * less than, less than or equal to, greater than and greater than or 204e1051a39Sopenharmony_ci * equal to respectively. If the specified condition holds, the functions 205e1051a39Sopenharmony_ci * return 1. If the condition does not hold, the functions print a diagnostic 206e1051a39Sopenharmony_ci * message and return 0. 207e1051a39Sopenharmony_ci * 208e1051a39Sopenharmony_ci * The desc argument is a printf format string followed by its arguments and 209e1051a39Sopenharmony_ci * this is included in the output if the condition being tested for is false. 210e1051a39Sopenharmony_ci */ 211e1051a39Sopenharmony_ci#define DEFINE_COMPARISON(type, name, opname, op, fmt) \ 212e1051a39Sopenharmony_ci int test_ ## name ## _ ## opname(const char *file, int line, \ 213e1051a39Sopenharmony_ci const char *s1, const char *s2, \ 214e1051a39Sopenharmony_ci const type t1, const type t2) \ 215e1051a39Sopenharmony_ci { \ 216e1051a39Sopenharmony_ci if (t1 op t2) \ 217e1051a39Sopenharmony_ci return 1; \ 218e1051a39Sopenharmony_ci test_fail_message(NULL, file, line, #type, s1, s2, #op, \ 219e1051a39Sopenharmony_ci "[" fmt "] compared to [" fmt "]", \ 220e1051a39Sopenharmony_ci t1, t2); \ 221e1051a39Sopenharmony_ci return 0; \ 222e1051a39Sopenharmony_ci } 223e1051a39Sopenharmony_ci 224e1051a39Sopenharmony_ci#define DEFINE_COMPARISONS(type, name, fmt) \ 225e1051a39Sopenharmony_ci DEFINE_COMPARISON(type, name, eq, ==, fmt) \ 226e1051a39Sopenharmony_ci DEFINE_COMPARISON(type, name, ne, !=, fmt) \ 227e1051a39Sopenharmony_ci DEFINE_COMPARISON(type, name, lt, <, fmt) \ 228e1051a39Sopenharmony_ci DEFINE_COMPARISON(type, name, le, <=, fmt) \ 229e1051a39Sopenharmony_ci DEFINE_COMPARISON(type, name, gt, >, fmt) \ 230e1051a39Sopenharmony_ci DEFINE_COMPARISON(type, name, ge, >=, fmt) 231e1051a39Sopenharmony_ci 232e1051a39Sopenharmony_ciDEFINE_COMPARISONS(int, int, "%d") 233e1051a39Sopenharmony_ciDEFINE_COMPARISONS(unsigned int, uint, "%u") 234e1051a39Sopenharmony_ciDEFINE_COMPARISONS(char, char, "%c") 235e1051a39Sopenharmony_ciDEFINE_COMPARISONS(unsigned char, uchar, "%u") 236e1051a39Sopenharmony_ciDEFINE_COMPARISONS(long, long, "%ld") 237e1051a39Sopenharmony_ciDEFINE_COMPARISONS(unsigned long, ulong, "%lu") 238e1051a39Sopenharmony_ciDEFINE_COMPARISONS(size_t, size_t, "%zu") 239e1051a39Sopenharmony_ciDEFINE_COMPARISONS(double, double, "%g") 240e1051a39Sopenharmony_ci 241e1051a39Sopenharmony_ciDEFINE_COMPARISON(void *, ptr, eq, ==, "%p") 242e1051a39Sopenharmony_ciDEFINE_COMPARISON(void *, ptr, ne, !=, "%p") 243e1051a39Sopenharmony_ci 244e1051a39Sopenharmony_ciint test_ptr_null(const char *file, int line, const char *s, const void *p) 245e1051a39Sopenharmony_ci{ 246e1051a39Sopenharmony_ci if (p == NULL) 247e1051a39Sopenharmony_ci return 1; 248e1051a39Sopenharmony_ci test_fail_message(NULL, file, line, "ptr", s, "NULL", "==", "%p", p); 249e1051a39Sopenharmony_ci return 0; 250e1051a39Sopenharmony_ci} 251e1051a39Sopenharmony_ci 252e1051a39Sopenharmony_ciint test_ptr(const char *file, int line, const char *s, const void *p) 253e1051a39Sopenharmony_ci{ 254e1051a39Sopenharmony_ci if (p != NULL) 255e1051a39Sopenharmony_ci return 1; 256e1051a39Sopenharmony_ci test_fail_message(NULL, file, line, "ptr", s, "NULL", "!=", "%p", p); 257e1051a39Sopenharmony_ci return 0; 258e1051a39Sopenharmony_ci} 259e1051a39Sopenharmony_ci 260e1051a39Sopenharmony_ciint test_true(const char *file, int line, const char *s, int b) 261e1051a39Sopenharmony_ci{ 262e1051a39Sopenharmony_ci if (b) 263e1051a39Sopenharmony_ci return 1; 264e1051a39Sopenharmony_ci test_fail_message(NULL, file, line, "bool", s, "true", "==", "false"); 265e1051a39Sopenharmony_ci return 0; 266e1051a39Sopenharmony_ci} 267e1051a39Sopenharmony_ci 268e1051a39Sopenharmony_ciint test_false(const char *file, int line, const char *s, int b) 269e1051a39Sopenharmony_ci{ 270e1051a39Sopenharmony_ci if (!b) 271e1051a39Sopenharmony_ci return 1; 272e1051a39Sopenharmony_ci test_fail_message(NULL, file, line, "bool", s, "false", "==", "true"); 273e1051a39Sopenharmony_ci return 0; 274e1051a39Sopenharmony_ci} 275e1051a39Sopenharmony_ci 276e1051a39Sopenharmony_ciint test_str_eq(const char *file, int line, const char *st1, const char *st2, 277e1051a39Sopenharmony_ci const char *s1, const char *s2) 278e1051a39Sopenharmony_ci{ 279e1051a39Sopenharmony_ci if (s1 == NULL && s2 == NULL) 280e1051a39Sopenharmony_ci return 1; 281e1051a39Sopenharmony_ci if (s1 == NULL || s2 == NULL || strcmp(s1, s2) != 0) { 282e1051a39Sopenharmony_ci test_fail_string_message(NULL, file, line, "string", st1, st2, "==", 283e1051a39Sopenharmony_ci s1, s1 == NULL ? 0 : strlen(s1), 284e1051a39Sopenharmony_ci s2, s2 == NULL ? 0 : strlen(s2)); 285e1051a39Sopenharmony_ci return 0; 286e1051a39Sopenharmony_ci } 287e1051a39Sopenharmony_ci return 1; 288e1051a39Sopenharmony_ci} 289e1051a39Sopenharmony_ci 290e1051a39Sopenharmony_ciint test_str_ne(const char *file, int line, const char *st1, const char *st2, 291e1051a39Sopenharmony_ci const char *s1, const char *s2) 292e1051a39Sopenharmony_ci{ 293e1051a39Sopenharmony_ci if ((s1 == NULL) ^ (s2 == NULL)) 294e1051a39Sopenharmony_ci return 1; 295e1051a39Sopenharmony_ci if (s1 == NULL || strcmp(s1, s2) == 0) { 296e1051a39Sopenharmony_ci test_fail_string_message(NULL, file, line, "string", st1, st2, "!=", 297e1051a39Sopenharmony_ci s1, s1 == NULL ? 0 : strlen(s1), 298e1051a39Sopenharmony_ci s2, s2 == NULL ? 0 : strlen(s2)); 299e1051a39Sopenharmony_ci return 0; 300e1051a39Sopenharmony_ci } 301e1051a39Sopenharmony_ci return 1; 302e1051a39Sopenharmony_ci} 303e1051a39Sopenharmony_ci 304e1051a39Sopenharmony_ciint test_strn_eq(const char *file, int line, const char *st1, const char *st2, 305e1051a39Sopenharmony_ci const char *s1, size_t n1, const char *s2, size_t n2) 306e1051a39Sopenharmony_ci{ 307e1051a39Sopenharmony_ci if (s1 == NULL && s2 == NULL) 308e1051a39Sopenharmony_ci return 1; 309e1051a39Sopenharmony_ci if (n1 != n2 || s1 == NULL || s2 == NULL || strncmp(s1, s2, n1) != 0) { 310e1051a39Sopenharmony_ci test_fail_string_message(NULL, file, line, "string", st1, st2, "==", 311e1051a39Sopenharmony_ci s1, s1 == NULL ? 0 : OPENSSL_strnlen(s1, n1), 312e1051a39Sopenharmony_ci s2, s2 == NULL ? 0 : OPENSSL_strnlen(s2, n2)); 313e1051a39Sopenharmony_ci return 0; 314e1051a39Sopenharmony_ci } 315e1051a39Sopenharmony_ci return 1; 316e1051a39Sopenharmony_ci} 317e1051a39Sopenharmony_ci 318e1051a39Sopenharmony_ciint test_strn_ne(const char *file, int line, const char *st1, const char *st2, 319e1051a39Sopenharmony_ci const char *s1, size_t n1, const char *s2, size_t n2) 320e1051a39Sopenharmony_ci{ 321e1051a39Sopenharmony_ci if ((s1 == NULL) ^ (s2 == NULL)) 322e1051a39Sopenharmony_ci return 1; 323e1051a39Sopenharmony_ci if (n1 != n2 || s1 == NULL || strncmp(s1, s2, n1) == 0) { 324e1051a39Sopenharmony_ci test_fail_string_message(NULL, file, line, "string", st1, st2, "!=", 325e1051a39Sopenharmony_ci s1, s1 == NULL ? 0 : OPENSSL_strnlen(s1, n1), 326e1051a39Sopenharmony_ci s2, s2 == NULL ? 0 : OPENSSL_strnlen(s2, n2)); 327e1051a39Sopenharmony_ci return 0; 328e1051a39Sopenharmony_ci } 329e1051a39Sopenharmony_ci return 1; 330e1051a39Sopenharmony_ci} 331e1051a39Sopenharmony_ci 332e1051a39Sopenharmony_ciint test_mem_eq(const char *file, int line, const char *st1, const char *st2, 333e1051a39Sopenharmony_ci const void *s1, size_t n1, const void *s2, size_t n2) 334e1051a39Sopenharmony_ci{ 335e1051a39Sopenharmony_ci if (s1 == NULL && s2 == NULL) 336e1051a39Sopenharmony_ci return 1; 337e1051a39Sopenharmony_ci if (n1 != n2 || s1 == NULL || s2 == NULL || memcmp(s1, s2, n1) != 0) { 338e1051a39Sopenharmony_ci test_fail_memory_message(NULL, file, line, "memory", st1, st2, "==", 339e1051a39Sopenharmony_ci s1, n1, s2, n2); 340e1051a39Sopenharmony_ci return 0; 341e1051a39Sopenharmony_ci } 342e1051a39Sopenharmony_ci return 1; 343e1051a39Sopenharmony_ci} 344e1051a39Sopenharmony_ci 345e1051a39Sopenharmony_ciint test_mem_ne(const char *file, int line, const char *st1, const char *st2, 346e1051a39Sopenharmony_ci const void *s1, size_t n1, const void *s2, size_t n2) 347e1051a39Sopenharmony_ci{ 348e1051a39Sopenharmony_ci if ((s1 == NULL) ^ (s2 == NULL)) 349e1051a39Sopenharmony_ci return 1; 350e1051a39Sopenharmony_ci if (n1 != n2) 351e1051a39Sopenharmony_ci return 1; 352e1051a39Sopenharmony_ci if (s1 == NULL || memcmp(s1, s2, n1) == 0) { 353e1051a39Sopenharmony_ci test_fail_memory_message(NULL, file, line, "memory", st1, st2, "!=", 354e1051a39Sopenharmony_ci s1, n1, s2, n2); 355e1051a39Sopenharmony_ci return 0; 356e1051a39Sopenharmony_ci } 357e1051a39Sopenharmony_ci return 1; 358e1051a39Sopenharmony_ci} 359e1051a39Sopenharmony_ci 360e1051a39Sopenharmony_ci#define DEFINE_BN_COMPARISONS(opname, op, zero_cond) \ 361e1051a39Sopenharmony_ci int test_BN_ ## opname(const char *file, int line, \ 362e1051a39Sopenharmony_ci const char *s1, const char *s2, \ 363e1051a39Sopenharmony_ci const BIGNUM *t1, const BIGNUM *t2) \ 364e1051a39Sopenharmony_ci { \ 365e1051a39Sopenharmony_ci if (BN_cmp(t1, t2) op 0) \ 366e1051a39Sopenharmony_ci return 1; \ 367e1051a39Sopenharmony_ci test_fail_bignum_message(NULL, file, line, "BIGNUM", s1, s2, \ 368e1051a39Sopenharmony_ci #op, t1, t2); \ 369e1051a39Sopenharmony_ci return 0; \ 370e1051a39Sopenharmony_ci } \ 371e1051a39Sopenharmony_ci int test_BN_ ## opname ## _zero(const char *file, int line, \ 372e1051a39Sopenharmony_ci const char *s, const BIGNUM *a) \ 373e1051a39Sopenharmony_ci { \ 374e1051a39Sopenharmony_ci if (a != NULL &&(zero_cond)) \ 375e1051a39Sopenharmony_ci return 1; \ 376e1051a39Sopenharmony_ci test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", \ 377e1051a39Sopenharmony_ci s, "0", #op, a); \ 378e1051a39Sopenharmony_ci return 0; \ 379e1051a39Sopenharmony_ci } 380e1051a39Sopenharmony_ci 381e1051a39Sopenharmony_ciDEFINE_BN_COMPARISONS(eq, ==, BN_is_zero(a)) 382e1051a39Sopenharmony_ciDEFINE_BN_COMPARISONS(ne, !=, !BN_is_zero(a)) 383e1051a39Sopenharmony_ciDEFINE_BN_COMPARISONS(gt, >, !BN_is_negative(a) && !BN_is_zero(a)) 384e1051a39Sopenharmony_ciDEFINE_BN_COMPARISONS(ge, >=, !BN_is_negative(a) || BN_is_zero(a)) 385e1051a39Sopenharmony_ciDEFINE_BN_COMPARISONS(lt, <, BN_is_negative(a) && !BN_is_zero(a)) 386e1051a39Sopenharmony_ciDEFINE_BN_COMPARISONS(le, <=, BN_is_negative(a) || BN_is_zero(a)) 387e1051a39Sopenharmony_ci 388e1051a39Sopenharmony_ciint test_BN_eq_one(const char *file, int line, const char *s, const BIGNUM *a) 389e1051a39Sopenharmony_ci{ 390e1051a39Sopenharmony_ci if (a != NULL && BN_is_one(a)) 391e1051a39Sopenharmony_ci return 1; 392e1051a39Sopenharmony_ci test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", s, "1", "==", a); 393e1051a39Sopenharmony_ci return 0; 394e1051a39Sopenharmony_ci} 395e1051a39Sopenharmony_ci 396e1051a39Sopenharmony_ciint test_BN_odd(const char *file, int line, const char *s, const BIGNUM *a) 397e1051a39Sopenharmony_ci{ 398e1051a39Sopenharmony_ci if (a != NULL && BN_is_odd(a)) 399e1051a39Sopenharmony_ci return 1; 400e1051a39Sopenharmony_ci test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", "ODD(", ")", s, a); 401e1051a39Sopenharmony_ci return 0; 402e1051a39Sopenharmony_ci} 403e1051a39Sopenharmony_ci 404e1051a39Sopenharmony_ciint test_BN_even(const char *file, int line, const char *s, const BIGNUM *a) 405e1051a39Sopenharmony_ci{ 406e1051a39Sopenharmony_ci if (a != NULL && !BN_is_odd(a)) 407e1051a39Sopenharmony_ci return 1; 408e1051a39Sopenharmony_ci test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", "EVEN(", ")", s, 409e1051a39Sopenharmony_ci a); 410e1051a39Sopenharmony_ci return 0; 411e1051a39Sopenharmony_ci} 412e1051a39Sopenharmony_ci 413e1051a39Sopenharmony_ciint test_BN_eq_word(const char *file, int line, const char *bns, const char *ws, 414e1051a39Sopenharmony_ci const BIGNUM *a, BN_ULONG w) 415e1051a39Sopenharmony_ci{ 416e1051a39Sopenharmony_ci BIGNUM *bw; 417e1051a39Sopenharmony_ci 418e1051a39Sopenharmony_ci if (a != NULL && BN_is_word(a, w)) 419e1051a39Sopenharmony_ci return 1; 420e1051a39Sopenharmony_ci if ((bw = BN_new()) != NULL) 421e1051a39Sopenharmony_ci BN_set_word(bw, w); 422e1051a39Sopenharmony_ci test_fail_bignum_message(NULL, file, line, "BIGNUM", bns, ws, "==", a, bw); 423e1051a39Sopenharmony_ci BN_free(bw); 424e1051a39Sopenharmony_ci return 0; 425e1051a39Sopenharmony_ci} 426e1051a39Sopenharmony_ci 427e1051a39Sopenharmony_ciint test_BN_abs_eq_word(const char *file, int line, const char *bns, 428e1051a39Sopenharmony_ci const char *ws, const BIGNUM *a, BN_ULONG w) 429e1051a39Sopenharmony_ci{ 430e1051a39Sopenharmony_ci BIGNUM *bw, *aa; 431e1051a39Sopenharmony_ci 432e1051a39Sopenharmony_ci if (a != NULL && BN_abs_is_word(a, w)) 433e1051a39Sopenharmony_ci return 1; 434e1051a39Sopenharmony_ci if ((aa = BN_dup(a)) != NULL) 435e1051a39Sopenharmony_ci BN_set_negative(aa, 0); 436e1051a39Sopenharmony_ci if ((bw = BN_new()) != NULL) 437e1051a39Sopenharmony_ci BN_set_word(bw, w); 438e1051a39Sopenharmony_ci test_fail_bignum_message(NULL, file, line, "BIGNUM", bns, ws, "abs==", 439e1051a39Sopenharmony_ci aa, bw); 440e1051a39Sopenharmony_ci BN_free(bw); 441e1051a39Sopenharmony_ci BN_free(aa); 442e1051a39Sopenharmony_ci return 0; 443e1051a39Sopenharmony_ci} 444e1051a39Sopenharmony_ci 445e1051a39Sopenharmony_cistatic const char *print_time(const ASN1_TIME *t) 446e1051a39Sopenharmony_ci{ 447e1051a39Sopenharmony_ci return t == NULL ? "<null>" : (const char *)ASN1_STRING_get0_data(t); 448e1051a39Sopenharmony_ci} 449e1051a39Sopenharmony_ci 450e1051a39Sopenharmony_ci#define DEFINE_TIME_T_COMPARISON(opname, op) \ 451e1051a39Sopenharmony_ci int test_time_t_ ## opname(const char *file, int line, \ 452e1051a39Sopenharmony_ci const char *s1, const char *s2, \ 453e1051a39Sopenharmony_ci const time_t t1, const time_t t2) \ 454e1051a39Sopenharmony_ci { \ 455e1051a39Sopenharmony_ci ASN1_TIME *at1 = ASN1_TIME_set(NULL, t1); \ 456e1051a39Sopenharmony_ci ASN1_TIME *at2 = ASN1_TIME_set(NULL, t2); \ 457e1051a39Sopenharmony_ci int r = at1 != NULL && at2 != NULL \ 458e1051a39Sopenharmony_ci && ASN1_TIME_compare(at1, at2) op 0; \ 459e1051a39Sopenharmony_ci if (!r) \ 460e1051a39Sopenharmony_ci test_fail_message(NULL, file, line, "time_t", s1, s2, #op, \ 461e1051a39Sopenharmony_ci "[%s] compared to [%s]", \ 462e1051a39Sopenharmony_ci print_time(at1), print_time(at2)); \ 463e1051a39Sopenharmony_ci ASN1_STRING_free(at1); \ 464e1051a39Sopenharmony_ci ASN1_STRING_free(at2); \ 465e1051a39Sopenharmony_ci return r; \ 466e1051a39Sopenharmony_ci } 467e1051a39Sopenharmony_ciDEFINE_TIME_T_COMPARISON(eq, ==) 468e1051a39Sopenharmony_ciDEFINE_TIME_T_COMPARISON(ne, !=) 469e1051a39Sopenharmony_ciDEFINE_TIME_T_COMPARISON(gt, >) 470e1051a39Sopenharmony_ciDEFINE_TIME_T_COMPARISON(ge, >=) 471e1051a39Sopenharmony_ciDEFINE_TIME_T_COMPARISON(lt, <) 472e1051a39Sopenharmony_ciDEFINE_TIME_T_COMPARISON(le, <=) 473