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