]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/testutil/stanza.c
2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
16 #include "../testutil.h"
18 int test_start_file(STANZA
*s
, const char *testfile
)
20 TEST_info("Reading %s", testfile
);
21 set_test_title(testfile
);
22 memset(s
, 0, sizeof(*s
));
23 if (!TEST_ptr(s
->fp
= BIO_new_file(testfile
, "rb")))
25 s
->test_file
= testfile
;
29 int test_end_file(STANZA
*s
)
31 TEST_info("Completed %d tests with %d errors and %d skipped",
32 s
->numtests
, s
->errors
, s
->numskip
);
38 * Read a PEM block. Return 1 if okay, 0 on error.
40 static int read_key(STANZA
*s
)
45 if (!TEST_ptr(s
->key
= BIO_new(BIO_s_mem())))
47 } else if (!TEST_int_gt(BIO_reset(s
->key
), 0)) {
51 /* Read to PEM end line and place content in memory BIO */
52 while (BIO_gets(s
->fp
, tmpbuf
, sizeof(tmpbuf
))) {
54 if (!TEST_int_gt(BIO_puts(s
->key
, tmpbuf
), 0))
56 if (strncmp(tmpbuf
, "-----END", 8) == 0)
59 TEST_error("Can't find key end");
65 * Delete leading and trailing spaces from a string
67 static char *strip_spaces(char *p
)
71 /* Skip over leading spaces */
72 while (*p
&& isspace(*p
))
77 for (q
= p
+ strlen(p
) - 1; q
!= p
&& isspace(*q
); )
83 * Read next test stanza; return 1 if found, 0 on EOF or error.
85 int test_readstanza(STANZA
*s
)
88 char *p
, *equals
, *key
, *value
;
90 for (s
->numpairs
= 0; BIO_gets(s
->fp
, s
->buff
, sizeof(s
->buff
)); ) {
92 if (!TEST_ptr(p
= strchr(s
->buff
, '\n'))) {
93 TEST_info("Line %d too long", s
->curr
);
98 /* Blank line marks end of tests. */
99 if (s
->buff
[0] == '\0')
102 /* Lines starting with a pound sign are ignored. */
103 if (s
->buff
[0] == '#')
106 /* Parse into key=value */
107 if (!TEST_ptr(equals
= strchr(s
->buff
, '='))) {
108 TEST_info("Missing = at line %d\n", s
->curr
);
112 if (!TEST_ptr(key
= strip_spaces(s
->buff
))) {
113 TEST_info("Empty field at line %d\n", s
->curr
);
116 if ((value
= strip_spaces(equals
)) == NULL
)
119 if (strcmp(key
, "Title") == 0) {
120 TEST_info("Starting \"%s\" tests at line %d", value
, s
->curr
);
124 if (s
->numpairs
== 0)
127 if (strcmp(key
, "PrivateKey") == 0) {
131 if (strcmp(key
, "PublicKey") == 0) {
136 if (!TEST_int_lt(s
->numpairs
++, TESTMAXPAIRS
)
137 || !TEST_ptr(pp
->key
= OPENSSL_strdup(key
))
138 || !TEST_ptr(pp
->value
= OPENSSL_strdup(value
)))
143 /* If we read anything, return ok. */
147 void test_clearstanza(STANZA
*s
)
152 for ( ; --i
>= 0; pp
++) {
153 OPENSSL_free(pp
->key
);
154 OPENSSL_free(pp
->value
);