Update tests to avoid printf to stdout/stderr when running as test cases.
[openssl.git] / test / ssl_test.c
index 0cdec46d3e01e939e1b187423bf6895c4dd86b1d..06dd18915de34dddfda57495a05cca8faec8b310 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
  *
  * Licensed under the OpenSSL license (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
@@ -17,7 +17,6 @@
 #include "handshake_helper.h"
 #include "ssl_test_ctx.h"
 #include "testutil.h"
-#include "test_main_custom.h"
 
 static CONF *conf = NULL;
 
@@ -195,6 +194,53 @@ static int check_nid(const char *name, int expected_nid, int nid)
     return 0;
 }
 
+static void print_ca_names(STACK_OF(X509_NAME) *names)
+{
+    int i;
+
+    if (names == NULL || sk_X509_NAME_num(names) == 0) {
+        TEST_note("    <empty>");
+        return;
+    }
+    for (i = 0; i < sk_X509_NAME_num(names); i++) {
+        X509_NAME_print_ex(bio_err, sk_X509_NAME_value(names, i), 4,
+                           XN_FLAG_ONELINE);
+        BIO_puts(bio_err, "\n");
+    }
+}
+
+static int check_ca_names(const char *name,
+                          STACK_OF(X509_NAME) *expected_names,
+                          STACK_OF(X509_NAME) *names)
+{
+    int i;
+
+    if (expected_names == NULL)
+        return 1;
+    if (names == NULL || sk_X509_NAME_num(names) == 0) {
+        if (TEST_int_eq(sk_X509_NAME_num(expected_names), 0))
+            return 1;
+        goto err;
+    }
+    if (sk_X509_NAME_num(names) != sk_X509_NAME_num(expected_names))
+        goto err;
+    for (i = 0; i < sk_X509_NAME_num(names); i++) {
+        if (!TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(names, i),
+                                       sk_X509_NAME_value(expected_names, i)),
+                         0)) {
+            goto err;
+        }
+    }
+    return 1;
+err:
+    TEST_info("%s: list mismatch", name);
+    TEST_note("Expected Names:");
+    print_ca_names(expected_names);
+    TEST_note("Received Names:");
+    print_ca_names(names);
+    return 0;
+}
+
 static int check_tmp_key(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
 {
     return check_nid("Tmp key", test_ctx->expected_tmp_key_type,
@@ -222,6 +268,14 @@ static int check_server_sign_type(HANDSHAKE_RESULT *result,
                      result->server_sign_type);
 }
 
+static int check_server_ca_names(HANDSHAKE_RESULT *result,
+                                 SSL_TEST_CTX *test_ctx)
+{
+    return check_ca_names("Server CA names",
+                          test_ctx->expected_server_ca_names,
+                          result->server_ca_names);
+}
+
 static int check_client_cert_type(HANDSHAKE_RESULT *result,
                                   SSL_TEST_CTX *test_ctx)
 {
@@ -243,54 +297,6 @@ static int check_client_sign_type(HANDSHAKE_RESULT *result,
                      result->client_sign_type);
 }
 
-static void print_ca_names(STACK_OF(X509_NAME) *names)
-{
-    BIO *err;
-    int i;
-
-    if (names == NULL || sk_X509_NAME_num(names) == 0) {
-        fprintf(stderr, "    <empty>\n");
-        return;
-    }
-    err = BIO_new_fp(stderr, BIO_NOCLOSE);
-    for (i = 0; i < sk_X509_NAME_num(names); i++) {
-        X509_NAME_print_ex(err, sk_X509_NAME_value(names, i), 4,
-                           XN_FLAG_ONELINE);
-        BIO_puts(err, "\n");
-    }
-    BIO_free(err);
-}
-
-static int check_ca_names(const char *name,
-                          STACK_OF(X509_NAME) *expected_names,
-                          STACK_OF(X509_NAME) *names)
-{
-    int i;
-
-    if (expected_names == NULL)
-        return 1;
-    if (names == NULL || sk_X509_NAME_num(names) == 0) {
-        if (sk_X509_NAME_num(expected_names) == 0)
-            return 1;
-        goto err;
-    }
-    if (sk_X509_NAME_num(names) != sk_X509_NAME_num(expected_names))
-        goto err;
-    for (i = 0; i < sk_X509_NAME_num(names); i++) {
-        if (X509_NAME_cmp(sk_X509_NAME_value(names, i),
-                          sk_X509_NAME_value(expected_names, i)) != 0) {
-            goto err;
-        }
-    }
-    return 1;
-    err:
-    fprintf(stderr, "%s: list mismatch\nExpected Names:\n", name);
-    print_ca_names(expected_names);
-    fprintf(stderr, "Received Names:\n");
-    print_ca_names(names);
-    return 0;
-}
-
 static int check_client_ca_names(HANDSHAKE_RESULT *result,
                                  SSL_TEST_CTX *test_ctx)
 {
@@ -324,6 +330,7 @@ static int check_test(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
         ret &= check_server_cert_type(result, test_ctx);
         ret &= check_server_sign_hash(result, test_ctx);
         ret &= check_server_sign_type(result, test_ctx);
+        ret &= check_server_ca_names(result, test_ctx);
         ret &= check_client_cert_type(result, test_ctx);
         ret &= check_client_sign_hash(result, test_ctx);
         ret &= check_client_sign_type(result, test_ctx);