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
15 #include "internal/nelem.h"
16 #include "../testutil.h"
19 int test_start_file(STANZA *s, const char *testfile)
21 TEST_info("Reading %s", testfile);
22 set_test_title(testfile);
23 memset(s, 0, sizeof(*s));
24 if (!TEST_ptr(s->fp = BIO_new_file(testfile, "r")))
26 s->test_file = testfile;
30 int test_end_file(STANZA *s)
32 TEST_info("Completed %d tests with %d errors and %d skipped",
33 s->numtests, s->errors, s->numskip);
39 * Read a PEM block. Return 1 if okay, 0 on error.
41 static int read_key(STANZA *s)
46 if (!TEST_ptr(s->key = BIO_new(BIO_s_mem())))
48 } else if (!TEST_int_gt(BIO_reset(s->key), 0)) {
52 /* Read to PEM end line and place content in memory BIO */
53 while (BIO_gets(s->fp, tmpbuf, sizeof(tmpbuf))) {
55 if (!TEST_int_gt(BIO_puts(s->key, tmpbuf), 0))
57 if (strncmp(tmpbuf, "-----END", 8) == 0)
60 TEST_error("Can't find key end");
66 * Delete leading and trailing spaces from a string
68 static char *strip_spaces(char *p)
72 /* Skip over leading spaces */
73 while (*p && isspace((unsigned char)*p))
78 for (q = p + strlen(p) - 1; q != p && isspace((unsigned char)*q); )
84 * Read next test stanza; return 1 if found, 0 on EOF or error.
86 int test_readstanza(STANZA *s)
89 char *p, *equals, *key, *value;
91 for (s->numpairs = 0; BIO_gets(s->fp, s->buff, sizeof(s->buff)); ) {
93 if (!TEST_ptr(p = strchr(s->buff, '\n'))) {
94 TEST_info("Line %d too long", s->curr);
99 /* Blank line marks end of tests. */
100 if (s->buff[0] == '\0')
103 /* Lines starting with a pound sign are ignored. */
104 if (s->buff[0] == '#')
107 /* Parse into key=value */
108 if (!TEST_ptr(equals = strchr(s->buff, '='))) {
109 TEST_info("Missing = at line %d\n", s->curr);
113 if (!TEST_ptr(key = strip_spaces(s->buff))) {
114 TEST_info("Empty field at line %d\n", s->curr);
117 if ((value = strip_spaces(equals)) == NULL)
120 if (strcmp(key, "Title") == 0) {
121 TEST_info("Starting \"%s\" tests at line %d", value, s->curr);
125 if (s->numpairs == 0)
128 if (strcmp(key, "PrivateKey") == 0) {
132 if (strcmp(key, "PublicKey") == 0) {
137 if (!TEST_int_lt(s->numpairs++, TESTMAXPAIRS)
138 || !TEST_ptr(pp->key = OPENSSL_strdup(key))
139 || !TEST_ptr(pp->value = OPENSSL_strdup(value)))
144 /* If we read anything, return ok. */
148 void test_clearstanza(STANZA *s)
153 for ( ; --i >= 0; pp++) {
154 OPENSSL_free(pp->key);
155 OPENSSL_free(pp->value);