.travis.yml: where it matters, have build and source nesting levels differ
[openssl.git] / test / ct_test.c
index 5123e50d0deaa4b4f954f9d74909ebcc13fbec97..c55fb7ca90d62dd0287cbc62666cf68cfbed19ab 100644 (file)
@@ -1,14 +1,13 @@
 /*
- * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2016-2018 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 <ctype.h>
-#include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -19,7 +18,7 @@
 #include <openssl/x509.h>
 #include <openssl/x509v3.h>
 #include "testutil.h"
-#include "openssl/crypto.h"
+#include <openssl/crypto.h>
 
 #ifndef OPENSSL_NO_CT
 /* Used when declaring buffers to read text files into */
@@ -64,7 +63,7 @@ static CT_TEST_FIXTURE *set_up(const char *const test_case_name)
     if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
         goto end;
     fixture->test_case_name = test_case_name;
-    fixture->epoch_time_in_ms = 1473269626000; /* Sep 7 17:33:46 2016 GMT */
+    fixture->epoch_time_in_ms = 1580335307000ULL; /* Wed 29 Jan 2020 10:01:47 PM UTC */
     if (!TEST_ptr(fixture->ctlog_store = CTLOG_STORE_new())
             || !TEST_int_eq(
                     CTLOG_STORE_load_default_file(fixture->ctlog_store), 1))
@@ -88,29 +87,10 @@ static void tear_down(CT_TEST_FIXTURE *fixture)
     OPENSSL_free(fixture);
 }
 
-static char *mk_file_path(const char *dir, const char *file)
-{
-# ifndef OPENSSL_SYS_VMS
-    const char *sep = "/";
-# else
-    const char *sep = "";
-# endif
-    size_t len = strlen(dir) + strlen(sep) + strlen(file) + 1;
-    char *full_file = OPENSSL_zalloc(len);
-
-    if (full_file != NULL) {
-        OPENSSL_strlcpy(full_file, dir, len);
-        OPENSSL_strlcat(full_file, sep, len);
-        OPENSSL_strlcat(full_file, file, len);
-    }
-
-    return full_file;
-}
-
 static X509 *load_pem_cert(const char *dir, const char *file)
 {
     X509 *cert = NULL;
-    char *file_path = mk_file_path(dir, file);
+    char *file_path = test_mk_file_path(dir, file);
 
     if (file_path != NULL) {
         BIO *cert_io = BIO_new_file(file_path, "r");
@@ -128,7 +108,7 @@ static int read_text_file(const char *dir, const char *file,
                           char *buffer, int buffer_length)
 {
     int len = -1;
-    char *file_path = mk_file_path(dir, file);
+    char *file_path = test_mk_file_path(dir, file);
 
     if (file_path != NULL) {
         BIO *file_io = BIO_new_file(file_path, "r");
@@ -180,6 +160,10 @@ static int compare_extension_printout(X509_EXTENSION *extension,
                                            X509V3_EXT_DEFAULT, 0)))
         goto end;
 
+    /* Append \n because it's easier to create files that end with one. */
+    if (!TEST_true(BIO_write(text_buffer, "\n", 1)))
+        goto end;
+
     /* Append \0 because we're about to use the buffer contents as a string. */
     if (!TEST_true(BIO_write(text_buffer, "\0", 1)))
         goto end;
@@ -297,7 +281,8 @@ static int execute_cert_test(CT_TEST_FIXTURE *fixture)
             for (i = 0; i < sk_SCT_num(scts); ++i) {
                 SCT *sct_i = sk_SCT_value(scts, i);
 
-                if (!TEST_int_eq(SCT_get_source(sct_i), SCT_SOURCE_X509V3_EXTENSION)) {
+                if (!TEST_int_eq(SCT_get_source(sct_i),
+                                 SCT_SOURCE_X509V3_EXTENSION)) {
                     goto end;
                 }
             }
@@ -344,22 +329,27 @@ end:
     return success;
 }
 
-# define SETUP_CT_TEST_FIXTURE() SETUP_TEST_FIXTURE(CT_TEST_FIXTURE *, set_up)
+# define SETUP_CT_TEST_FIXTURE() SETUP_TEST_FIXTURE(CT_TEST_FIXTURE, set_up)
 # define EXECUTE_CT_TEST() EXECUTE_TEST(execute_cert_test, tear_down)
 
 static int test_no_scts_in_certificate(void)
 {
     SETUP_CT_TEST_FIXTURE();
+    if (fixture == NULL)
+        return 0;
     fixture->certs_dir = certs_dir;
     fixture->certificate_file = "leaf.pem";
     fixture->issuer_file = "subinterCA.pem";
     fixture->expected_sct_count = 0;
     EXECUTE_CT_TEST();
+    return result;
 }
 
 static int test_one_sct_in_certificate(void)
 {
     SETUP_CT_TEST_FIXTURE();
+    if (fixture == NULL)
+        return 0;
     fixture->certs_dir = certs_dir;
     fixture->certificate_file = "embeddedSCTs1.pem";
     fixture->issuer_file = "embeddedSCTs1_issuer.pem";
@@ -367,11 +357,14 @@ static int test_one_sct_in_certificate(void)
     fixture->sct_dir = certs_dir;
     fixture->sct_text_file = "embeddedSCTs1.sct";
     EXECUTE_CT_TEST();
+    return result;
 }
 
 static int test_multiple_scts_in_certificate(void)
 {
     SETUP_CT_TEST_FIXTURE();
+    if (fixture == NULL)
+        return 0;
     fixture->certs_dir = certs_dir;
     fixture->certificate_file = "embeddedSCTs3.pem";
     fixture->issuer_file = "embeddedSCTs3_issuer.pem";
@@ -379,34 +372,43 @@ static int test_multiple_scts_in_certificate(void)
     fixture->sct_dir = certs_dir;
     fixture->sct_text_file = "embeddedSCTs3.sct";
     EXECUTE_CT_TEST();
+    return result;
 }
 
 static int test_verify_one_sct(void)
 {
     SETUP_CT_TEST_FIXTURE();
+    if (fixture == NULL)
+        return 0;
     fixture->certs_dir = certs_dir;
     fixture->certificate_file = "embeddedSCTs1.pem";
     fixture->issuer_file = "embeddedSCTs1_issuer.pem";
     fixture->expected_sct_count = fixture->expected_valid_sct_count = 1;
     fixture->test_validity = 1;
     EXECUTE_CT_TEST();
+    return result;
 }
 
 static int test_verify_multiple_scts(void)
 {
     SETUP_CT_TEST_FIXTURE();
+    if (fixture == NULL)
+        return 0;
     fixture->certs_dir = certs_dir;
     fixture->certificate_file = "embeddedSCTs3.pem";
     fixture->issuer_file = "embeddedSCTs3_issuer.pem";
     fixture->expected_sct_count = fixture->expected_valid_sct_count = 3;
     fixture->test_validity = 1;
     EXECUTE_CT_TEST();
+    return result;
 }
 
 static int test_verify_fails_for_future_sct(void)
 {
     SETUP_CT_TEST_FIXTURE();
-    fixture->epoch_time_in_ms = 1365094800000; /* Apr 4 17:00:00 2013 GMT */
+    if (fixture == NULL)
+        return 0;
+    fixture->epoch_time_in_ms = 1365094800000ULL; /* Apr 4 17:00:00 2013 GMT */
     fixture->certs_dir = certs_dir;
     fixture->certificate_file = "embeddedSCTs1.pem";
     fixture->issuer_file = "embeddedSCTs1_issuer.pem";
@@ -414,6 +416,7 @@ static int test_verify_fails_for_future_sct(void)
     fixture->expected_valid_sct_count = 0;
     fixture->test_validity = 1;
     EXECUTE_CT_TEST();
+    return result;
 }
 
 static int test_decode_tls_sct(void)
@@ -437,11 +440,14 @@ static int test_decode_tls_sct(void)
         "\xED\xBF\x08";
 
     SETUP_CT_TEST_FIXTURE();
+    if (fixture == NULL)
+        return 0;
     fixture->tls_sct_list = tls_sct_list;
     fixture->tls_sct_list_len = 0x7a;
     fixture->sct_dir = ct_dir;
     fixture->sct_text_file = "tls1.sct";
     EXECUTE_CT_TEST();
+    return result;
 }
 
 static int test_encode_tls_sct(void)
@@ -454,6 +460,8 @@ static int test_encode_tls_sct(void)
     SCT *sct = NULL;
 
     SETUP_CT_TEST_FIXTURE();
+    if (fixture == NULL)
+        return 0;
 
     fixture->sct_list = sk_SCT_new_null();
     if (!TEST_ptr(sct = SCT_new_from_base64(SCT_VERSION_V1, log_id,
@@ -466,6 +474,7 @@ static int test_encode_tls_sct(void)
     fixture->sct_dir = ct_dir;
     fixture->sct_text_file = "tls1.sct";
     EXECUTE_CT_TEST();
+    return result;
 }
 
 /*
@@ -476,11 +485,12 @@ static int test_default_ct_policy_eval_ctx_time_is_now(void)
 {
     int success = 0;
     CT_POLICY_EVAL_CTX *ct_policy_ctx = CT_POLICY_EVAL_CTX_new();
-    const time_t default_time = CT_POLICY_EVAL_CTX_get_time(ct_policy_ctx) /
-            1000;
+    const time_t default_time =
+        (time_t)(CT_POLICY_EVAL_CTX_get_time(ct_policy_ctx) / 1000);
     const time_t time_tolerance = 600;  /* 10 minutes */
 
-    if (!TEST_uint_le(fabs(difftime(time(NULL), default_time)), time_tolerance))
+    if (!TEST_time_t_le(abs((int)difftime(time(NULL), default_time)),
+                        time_tolerance))
         goto end;
 
     success = 1;