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
strip_line_ends(char * str)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
21 str[i] = '\0';
22 }
23
compare_with_reference_file(BIO * membio,const char * reffile)24 int compare_with_reference_file(BIO *membio, const char *reffile)
25 {
26 BIO *file = NULL, *newfile = NULL;
27 char buf1[8192], buf2[8192];
28 int ret = 0;
29 size_t i;
30
31 if (!TEST_ptr(reffile))
32 goto err;
33
34 file = BIO_new_file(reffile, "rb");
35 if (!TEST_ptr(file))
36 goto err;
37
38 newfile = BIO_new_file("ssltraceref-new.txt", "wb");
39 if (!TEST_ptr(newfile))
40 goto err;
41
42 while (BIO_gets(membio, buf2, sizeof(buf2)) > 0)
43 if (BIO_puts(newfile, buf2) <= 0) {
44 TEST_error("Failed writing new file data");
45 goto err;
46 }
47
48 if (!TEST_int_ge(BIO_seek(membio, 0), 0))
49 goto err;
50
51 while (BIO_gets(file, buf1, sizeof(buf1)) > 0) {
52 size_t line_len;
53
54 if (BIO_gets(membio, buf2, sizeof(buf2)) <= 0) {
55 TEST_error("Failed reading mem data");
56 goto err;
57 }
58 strip_line_ends(buf1);
59 strip_line_ends(buf2);
60 line_len = strlen(buf1);
61 if (line_len > 0 && buf1[line_len - 1] == '?') {
62 /* Wildcard at the EOL means ignore anything after it */
63 if (strlen(buf2) > line_len)
64 buf2[line_len] = '\0';
65 }
66 if (line_len != strlen(buf2)) {
67 TEST_error("Actual and ref line data length mismatch");
68 TEST_info("%s", buf1);
69 TEST_info("%s", buf2);
70 goto err;
71 }
72 for (i = 0; i < line_len; i++) {
73 /* '?' is a wild card character in the reference text */
74 if (buf1[i] == '?')
75 buf2[i] = '?';
76 }
77 if (!TEST_str_eq(buf1, buf2))
78 goto err;
79 }
80 if (!TEST_true(BIO_eof(file))
81 || !TEST_true(BIO_eof(membio)))
82 goto err;
83
84 ret = 1;
85 err:
86 BIO_free(file);
87 BIO_free(newfile);
88 return ret;
89 }
90