2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
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
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;
92 for (s->numpairs = 0; BIO_gets(s->fp, s->buff, sizeof(s->buff)); ) {
94 if (!TEST_ptr(p = strchr(s->buff, '\n'))) {
95 TEST_info("Line %d too long", s->curr);
100 /* Blank line marks end of tests. */
101 if (s->buff[0] == '\0')
104 /* Lines starting with a pound sign are ignored. */
105 if (s->buff[0] == '#')
108 /* Parse into key=value */
109 if (!TEST_ptr(equals = strchr(s->buff, '='))) {
110 TEST_info("Missing = at line %d\n", s->curr);
114 if (!TEST_ptr(key = strip_spaces(s->buff))) {
115 TEST_info("Empty field at line %d\n", s->curr);
118 if ((value = strip_spaces(equals)) == NULL)
121 if (strcmp(key, "Title") == 0) {
122 TEST_info("Starting \"%s\" tests at line %d", value, s->curr);
126 if (s->numpairs == 0)
129 if (strcmp(key, "PrivateKey") == 0) {
133 if (strcmp(key, "PublicKey") == 0) {
138 if (!TEST_int_lt(s->numpairs++, TESTMAXPAIRS)
139 || !TEST_ptr(pp->key = OPENSSL_strdup(key))
140 || !TEST_ptr(pp->value = OPENSSL_strdup(value)))
145 /* If we read anything, return ok. */
149 void test_clearstanza(STANZA *s)
154 for ( ; --i >= 0; pp++) {
155 OPENSSL_free(pp->key);
156 OPENSSL_free(pp->value);