namemap: change ossl_namemap_empty() to do what the documentation says.
[oweals/openssl.git] / test / testutil / tests.c
index 7a923889d609bdbc9ee1a5ce9918af2084585531..56177cd9992ee018f7424965bd71a6404bfe88b3 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
  *
- * Licensed under the OpenSSL license (the "License").  You may not use
+ * Licensed under the Apache License 2.0 (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
  * in the file LICENSE in the source distribution or at
  * https://www.openssl.org/source/license.html
 #include "output.h"
 #include "tu_local.h"
 
+#include <errno.h>
 #include <string.h>
 #include <ctype.h>
-#include "../../e_os.h"
+#include "internal/nelem.h"
+#include <openssl/asn1.h>
 
 /*
  * Output a failed test first line.
@@ -26,8 +28,7 @@ void test_fail_message_prefix(const char *prefix, const char *file,
                               const char *left, const char *right,
                               const char *op)
 {
-    test_printf_stderr("%*s# %s: ", subtest_level(), "",
-                       prefix != NULL ? prefix : "ERROR");
+    test_printf_stderr("%s: ", prefix != NULL ? prefix : "ERROR");
     if (type)
         test_printf_stderr("(%s) ", type);
     if (op != NULL) {
@@ -78,7 +79,6 @@ static void test_fail_message_va(const char *prefix, const char *file,
 {
     test_fail_message_prefix(prefix, file, line, type, left, right, op);
     if (fmt != NULL) {
-        test_printf_stderr("%*s# ", subtest_level(), "");
         test_vprintf_stderr(fmt, ap);
         test_printf_stderr("\n");
     }
@@ -135,12 +135,20 @@ void test_error(const char *file, int line, const char *desc, ...)
     test_printf_stderr("\n");
 }
 
+void test_perror(const char *s)
+{
+    /*
+     * Using openssl_strerror_r causes linking issues since it isn't
+     * exported from libcrypto.so
+     */
+    TEST_error("%s: %s", s, strerror(errno));
+}
+
 void test_note(const char *fmt, ...)
 {
     if (fmt != NULL) {
         va_list ap;
 
-        test_printf_stderr("%*s# ", subtest_level(), "");
         va_start(ap, fmt);
         test_vprintf_stderr(fmt, ap);
         va_end(ap);
@@ -149,9 +157,33 @@ void test_note(const char *fmt, ...)
     test_flush_stderr();
 }
 
+
+int test_skip(const char *file, int line, const char *desc, ...)
+{
+    va_list ap;
+
+    va_start(ap, desc);
+    test_fail_message_va("SKIP", file, line, NULL, NULL, NULL, NULL, desc, ap);
+    va_end(ap);
+    return TEST_SKIP_CODE;
+}
+
+int test_skip_c90(const char *desc, ...)
+{
+    va_list ap;
+
+    va_start(ap, desc);
+    test_fail_message_va("SKIP", NULL, -1, NULL, NULL, NULL, NULL, desc, ap);
+    va_end(ap);
+    test_printf_stderr("\n");
+    return TEST_SKIP_CODE;
+}
+
+
 void test_openssl_errors(void)
 {
     ERR_print_errors_cb(openssl_error_cb, NULL);
+    ERR_clear_error();
 }
 
 /*
@@ -204,6 +236,7 @@ DEFINE_COMPARISONS(unsigned char, uchar, "%u")
 DEFINE_COMPARISONS(long, long, "%ld")
 DEFINE_COMPARISONS(unsigned long, ulong, "%lu")
 DEFINE_COMPARISONS(size_t, size_t, "%zu")
+DEFINE_COMPARISONS(double, double, "%g")
 
 DEFINE_COMPARISON(void *, ptr, eq, ==, "%p")
 DEFINE_COMPARISON(void *, ptr, ne, !=, "%p")
@@ -408,3 +441,32 @@ int test_BN_abs_eq_word(const char *file, int line, const char *bns,
     BN_free(aa);
     return 0;
 }
+
+static const char *print_time(const ASN1_TIME *t)
+{
+    return t == NULL ? "<null>" : (const char *)ASN1_STRING_get0_data(t);
+}
+
+#define DEFINE_TIME_T_COMPARISON(opname, op)                            \
+    int test_time_t_ ## opname(const char *file, int line,              \
+                               const char *s1, const char *s2,          \
+                               const time_t t1, const time_t t2)        \
+    {                                                                   \
+        ASN1_TIME *at1 = ASN1_TIME_set(NULL, t1);                       \
+        ASN1_TIME *at2 = ASN1_TIME_set(NULL, t2);                       \
+        int r = at1 != NULL && at2 != NULL                              \
+                && ASN1_TIME_compare(at1, at2) op 0;                    \
+        if (!r)                                                         \
+            test_fail_message(NULL, file, line, "time_t", s1, s2, #op,  \
+                              "[%s] compared to [%s]",                  \
+                              print_time(at1), print_time(at2));        \
+        ASN1_STRING_free(at1);                                          \
+        ASN1_STRING_free(at2);                                          \
+        return r;                                                       \
+    }
+DEFINE_TIME_T_COMPARISON(eq, ==)
+DEFINE_TIME_T_COMPARISON(ne, !=)
+DEFINE_TIME_T_COMPARISON(gt, >)
+DEFINE_TIME_T_COMPARISON(ge, >=)
+DEFINE_TIME_T_COMPARISON(lt, <)
+DEFINE_TIME_T_COMPARISON(le, <=)