Fix va_arg all in test_error_c90
[openssl.git] / test / testutil / tests.c
index ef020f7899ac7bf55d357d538f93802f191197ab..a5538e7d9f2a8ac95eef80d0ec867177e636c35f 100644 (file)
@@ -89,6 +89,14 @@ static void test_fail_message_va(const char *prefix, const char *file,
     test_flush_stderr();
 }
 
+static void test_string_null_empty(const char *m, int indent, char c)
+{
+    if (m == NULL)
+        test_printf_stderr("%*s# % 4s %c NULL\n", indent, "", "", c);
+    else
+        test_printf_stderr("%*s# % 4u:%c ''\n", indent, "", 0u, c);
+}
+
 static void test_fail_string_message(const char *prefix, const char *file,
                                      int line, const char *type,
                                      const char *left, const char *right,
@@ -109,14 +117,11 @@ static void test_fail_string_message(const char *prefix, const char *file,
         l2 = 0;
     if (l1 == 0 && l2 == 0) {
         if ((m1 == NULL) == (m2 == NULL)) {
-            test_printf_stderr("%*s# % 4s   %s\n", indent, "", "",
-                               m1 == NULL ? "NULL" : "''");
+            test_string_null_empty(m1, indent, ' ');
         } else {
             test_diff_header(left, right);
-            test_printf_stderr("%*s# % 4s - %s\n", indent, "", "",
-                               m1 == NULL ? "NULL" : "''");
-            test_printf_stderr("%*s# % 4s + %s\n", indent, "", "",
-                               m2 == NULL ? "NULL" : "''");
+            test_string_null_empty(m1, indent, '-');
+            test_string_null_empty(m2, indent, '+');
         }
         goto fin;
     }
@@ -136,11 +141,10 @@ static void test_fail_string_message(const char *prefix, const char *file,
             for (i = 0; i < n2; i++)
                 b2[i] = isprint(m2[i]) ? m2[i] : '.';
         }
-        diff = n1 != n2;
+        diff = 0;
         i = 0;
         if (n1 > 0 && n2 > 0) {
             const size_t j = n1 < n2 ? n1 : n2;
-            const size_t k = n1 > n2 ? n1 : n2;
 
             for (; i < j; i++)
                 if (m1[i] == m2[i]) {
@@ -149,26 +153,21 @@ static void test_fail_string_message(const char *prefix, const char *file,
                     bdiff[i] = '^';
                     diff = 1;
                 }
-            for (; i < k; i++)
-                bdiff[i] = '^';
             bdiff[i] = '\0';
         }
-        if (!diff) {
-            test_printf_stderr("%*s# % 4u:  '%s'\n", indent, "", cnt, b1);
+        if (n1 == n2 && !diff) {
+            test_printf_stderr("%*s# % 4u:  '%s'\n", indent, "", cnt,
+                               n2 > n1 ? b2 : b1);
         } else {
-            if (cnt == 0 && m1 == NULL)
-                test_printf_stderr("%*s# % 4s - NULL\n", indent, "", "");
-            else if (cnt == 0 && *m1 == '\0')
-                test_printf_stderr("%*s# % 4s - ''\n", indent, "", "");
+            if (cnt == 0 && (m1 == NULL || *m1 == '\0'))
+                test_string_null_empty(m1, indent, '-');
             else if (n1 > 0)
                 test_printf_stderr("%*s# % 4u:- '%s'\n", indent, "", cnt, b1);
-            if (cnt == 0 && m2 == NULL)
-                test_printf_stderr("%*s# % 4s + NULL\n", indent, "", "");
-            else if (cnt == 0 && *m2 == '\0')
-                test_printf_stderr("%*s# % 4s + ''\n", indent, "", "");
+            if (cnt == 0 && (m2 == NULL || *m2 == '\0'))
+               test_string_null_empty(m2, indent, '+');
             else if (n2 > 0)
                 test_printf_stderr("%*s# % 4u:+ '%s'\n", indent, "", cnt, b2);
-            if (i > 0)
+            if (diff && i > 0)
                 test_printf_stderr("%*s# % 4s    %s\n", indent, "", "", bdiff);
         }
         m1 += n1;
@@ -392,6 +391,14 @@ static void test_fail_bignum_mono_message(const char *prefix, const char *file,
     test_fail_bignum_common(prefix, file, line, type, left, right, op, bn, bn);
 }
 
+static void test_memory_null_empty(const unsigned char *m, int indent, char c)
+{
+    if (m == NULL)
+        test_printf_stderr("%*s# % 4s %c%s\n", indent, "", "", c, "NULL");
+    else
+        test_printf_stderr("%*s# %04x %c%s\n", indent, "", 0u, c, "empty");
+}
+
 static void test_fail_memory_message(const char *prefix, const char *file,
                                      int line, const char *type,
                                      const char *left, const char *right,
@@ -413,14 +420,11 @@ static void test_fail_memory_message(const char *prefix, const char *file,
         l2 = 0;
     if (l1 == 0 && l2 == 0) {
         if ((m1 == NULL) == (m2 == NULL)) {
-            test_printf_stderr("%*s# %04s  %s\n", indent, "", "",
-                               m1 == NULL ? "NULL" : "empty");
+            test_memory_null_empty(m1, indent, ' ');
         } else {
             test_diff_header(left, right);
-            test_printf_stderr("%*s# %04s -%s\n", indent, "", "",
-                               m1 == NULL ? "NULL" : "empty");
-            test_printf_stderr("%*s# %04s +%s\n", indent, "", "",
-                               m2 == NULL ? "NULL" : "empty");
+            test_memory_null_empty(m1, indent, '-');
+            test_memory_null_empty(m2, indent, '+');
         }
         goto fin;
     }
@@ -439,12 +443,11 @@ static void test_fail_memory_message(const char *prefix, const char *file,
             hex_convert_memory(m2, n2, b2, 8);
         }
 
-        diff = n1 != n2;
+        diff = 0;
         i = 0;
         p = bdiff;
         if (n1 > 0 && n2 > 0) {
             const size_t j = n1 < n2 ? n1 : n2;
-            const size_t k = n1 > n2 ? n1 : n2;
 
             for (; i < j; i++) {
                 if (m1[i] == m2[i]) {
@@ -455,35 +458,24 @@ static void test_fail_memory_message(const char *prefix, const char *file,
                     *p++ = '^';
                     diff = 1;
                 }
-                if ((i % 8) == 7 && (i != j - 1 || j != k))
-                    *p++ = ' ';
-            }
-
-            for (; i < k; i++) {
-                *p++ = '^';
-                *p++ = '^';
-                if ((i % 8) == 7 && i != k - 1)
+                if (i % 8 == 7 && i != j - 1)
                     *p++ = ' ';
             }
             *p++ = '\0';
         }
 
-        if (!diff) {
+        if (n1 == n2 && !diff) {
             test_printf_stderr("%*s# %04x: %s\n", indent, "", cnt, b1);
         } else {
-            if (cnt == 0 && m1 == NULL)
-                test_printf_stderr("%*s# %04s -NULL\n", indent, "", "");
-            else if (cnt == 0 && l1 == 0)
-                test_printf_stderr("%*s# %04s -empty\n", indent, "", "");
+            if (cnt == 0 && (m1 == NULL || l1 == 0))
+                test_memory_null_empty(m1, indent, '-');
             else if (n1 > 0)
                 test_printf_stderr("%*s# %04x:-%s\n", indent, "", cnt, b1);
-            if (cnt == 0 && m2 == NULL)
-                test_printf_stderr("%*s# %04s +NULL\n", indent, "", "");
-            else if (cnt == 0 && l2 == 0)
-                test_printf_stderr("%*s# %04s +empty\n", indent, "", "");
+            if (cnt == 0 && (m2 == NULL || l2 == 0))
+                test_memory_null_empty(m2, indent, '+');
             else if (n2 > 0)
                 test_printf_stderr("%*s# %04x:+%s\n", indent, "", cnt, b2);
-            if (i > 0)
+            if (diff && i > 0)
                 test_printf_stderr("%*s# % 4s  %s\n", indent, "", "", bdiff);
         }
         m1 += n1;
@@ -532,7 +524,7 @@ void test_error_c90(const char *desc, ...)
     va_list ap;
 
     va_start(ap, desc);
-    test_fail_message(NULL, NULL, -1, NULL, NULL, NULL, NULL, desc, ap);
+    test_fail_message_va(NULL, NULL, -1, NULL, NULL, NULL, NULL, desc, ap);
     va_end(ap);
 }