Home | History | Annotate | Line # | Download | only in testutil
      1  1.1  christos /*
      2  1.1  christos  * Copyright 2017-2025 The OpenSSL Project Authors. All Rights Reserved.
      3  1.1  christos  *
      4  1.1  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5  1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      6  1.1  christos  * in the file LICENSE in the source distribution or at
      7  1.1  christos  * https://www.openssl.org/source/license.html
      8  1.1  christos  */
      9  1.1  christos 
     10  1.1  christos #include "../testutil.h"
     11  1.1  christos 
     12  1.1  christos static void strip_line_ends(char *str)
     13  1.1  christos {
     14  1.1  christos     size_t i;
     15  1.1  christos 
     16  1.1  christos     for (i = strlen(str);
     17  1.1  christos         i > 0 && (str[i - 1] == '\n' || str[i - 1] == '\r');
     18  1.1  christos         i--)
     19  1.1  christos         ;
     20  1.1  christos 
     21  1.1  christos     str[i] = '\0';
     22  1.1  christos }
     23  1.1  christos 
     24  1.1  christos int compare_with_reference_file(BIO *membio, const char *reffile)
     25  1.1  christos {
     26  1.1  christos     BIO *file = NULL, *newfile = NULL;
     27  1.1  christos     char buf1[8192], buf2[8192];
     28  1.1  christos     int ret = 0;
     29  1.1  christos     size_t i;
     30  1.1  christos 
     31  1.1  christos     if (!TEST_ptr(reffile))
     32  1.1  christos         goto err;
     33  1.1  christos 
     34  1.1  christos     file = BIO_new_file(reffile, "rb");
     35  1.1  christos     if (!TEST_ptr(file))
     36  1.1  christos         goto err;
     37  1.1  christos 
     38  1.1  christos     newfile = BIO_new_file("ssltraceref-new.txt", "wb");
     39  1.1  christos     if (!TEST_ptr(newfile))
     40  1.1  christos         goto err;
     41  1.1  christos 
     42  1.1  christos     while (BIO_gets(membio, buf2, sizeof(buf2)) > 0)
     43  1.1  christos         if (BIO_puts(newfile, buf2) <= 0) {
     44  1.1  christos             TEST_error("Failed writing new file data");
     45  1.1  christos             goto err;
     46  1.1  christos         }
     47  1.1  christos 
     48  1.1  christos     if (!TEST_int_ge(BIO_seek(membio, 0), 0))
     49  1.1  christos         goto err;
     50  1.1  christos 
     51  1.1  christos     while (BIO_gets(file, buf1, sizeof(buf1)) > 0) {
     52  1.1  christos         size_t line_len;
     53  1.1  christos 
     54  1.1  christos         if (BIO_gets(membio, buf2, sizeof(buf2)) <= 0) {
     55  1.1  christos             TEST_error("Failed reading mem data");
     56  1.1  christos             goto err;
     57  1.1  christos         }
     58  1.1  christos         strip_line_ends(buf1);
     59  1.1  christos         strip_line_ends(buf2);
     60  1.1  christos         line_len = strlen(buf1);
     61  1.1  christos         if (line_len > 0 && buf1[line_len - 1] == '?') {
     62  1.1  christos             /* Wildcard at the EOL means ignore anything after it */
     63  1.1  christos             if (strlen(buf2) > line_len)
     64  1.1  christos                 buf2[line_len] = '\0';
     65  1.1  christos         }
     66  1.1  christos         if (line_len != strlen(buf2)) {
     67  1.1  christos             TEST_error("Actual and ref line data length mismatch");
     68  1.1  christos             TEST_info("%s", buf1);
     69  1.1  christos             TEST_info("%s", buf2);
     70  1.1  christos             goto err;
     71  1.1  christos         }
     72  1.1  christos         for (i = 0; i < line_len; i++) {
     73  1.1  christos             /* '?' is a wild card character in the reference text */
     74  1.1  christos             if (buf1[i] == '?')
     75  1.1  christos                 buf2[i] = '?';
     76  1.1  christos         }
     77  1.1  christos         if (!TEST_str_eq(buf1, buf2))
     78  1.1  christos             goto err;
     79  1.1  christos     }
     80  1.1  christos     if (!TEST_true(BIO_eof(file))
     81  1.1  christos         || !TEST_true(BIO_eof(membio)))
     82  1.1  christos         goto err;
     83  1.1  christos 
     84  1.1  christos     ret = 1;
     85  1.1  christos err:
     86  1.1  christos     BIO_free(file);
     87  1.1  christos     BIO_free(newfile);
     88  1.1  christos     return ret;
     89  1.1  christos }
     90