]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/testutil/stanza.c
threads_pthread.c: change inline to ossl_inline
[thirdparty/openssl.git] / test / testutil / stanza.c
CommitLineData
ae269dd8
RS
1/*
2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3 *
909f1a2e 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
ae269dd8
RS
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#include <assert.h>
10#include <errno.h>
11#include <stdio.h>
12#include <string.h>
13#include <ctype.h>
14
b99fe5f4 15#include "internal/nelem.h"
ae269dd8 16#include "../testutil.h"
ad887416 17#include "tu_local.h"
ae269dd8
RS
18
19int test_start_file(STANZA *s, const char *testfile)
20{
21 TEST_info("Reading %s", testfile);
22 set_test_title(testfile);
23 memset(s, 0, sizeof(*s));
74df8c4c 24 if (!TEST_ptr(s->fp = BIO_new_file(testfile, "r")))
ae269dd8
RS
25 return 0;
26 s->test_file = testfile;
27 return 1;
28}
29
30int test_end_file(STANZA *s)
31{
32 TEST_info("Completed %d tests with %d errors and %d skipped",
33 s->numtests, s->errors, s->numskip);
34 BIO_free(s->fp);
35 return 1;
36}
37
38/*
39 * Read a PEM block. Return 1 if okay, 0 on error.
40 */
41static int read_key(STANZA *s)
42{
43 char tmpbuf[128];
44
45 if (s->key == NULL) {
46 if (!TEST_ptr(s->key = BIO_new(BIO_s_mem())))
47 return 0;
48 } else if (!TEST_int_gt(BIO_reset(s->key), 0)) {
49 return 0;
50 }
51
52 /* Read to PEM end line and place content in memory BIO */
53 while (BIO_gets(s->fp, tmpbuf, sizeof(tmpbuf))) {
54 s->curr++;
55 if (!TEST_int_gt(BIO_puts(s->key, tmpbuf), 0))
56 return 0;
2ff286c2 57 if (HAS_PREFIX(tmpbuf, "-----END"))
ae269dd8
RS
58 return 1;
59 }
60 TEST_error("Can't find key end");
61 return 0;
62}
63
64
65/*
66 * Delete leading and trailing spaces from a string
67 */
68static char *strip_spaces(char *p)
69{
70 char *q;
71
72 /* Skip over leading spaces */
00dfbaad 73 while (*p && isspace((unsigned char)*p))
ae269dd8 74 p++;
12a765a5 75 if (*p == '\0')
ae269dd8
RS
76 return NULL;
77
00dfbaad 78 for (q = p + strlen(p) - 1; q != p && isspace((unsigned char)*q); )
ae269dd8
RS
79 *q-- = '\0';
80 return *p ? p : NULL;
81}
82
83/*
84 * Read next test stanza; return 1 if found, 0 on EOF or error.
85 */
86int test_readstanza(STANZA *s)
87{
88 PAIR *pp = s->pairs;
d8adf68c
CZ
89 char *p, *equals, *key;
90 const char *value;
ae269dd8
RS
91
92 for (s->numpairs = 0; BIO_gets(s->fp, s->buff, sizeof(s->buff)); ) {
93 s->curr++;
94 if (!TEST_ptr(p = strchr(s->buff, '\n'))) {
95 TEST_info("Line %d too long", s->curr);
96 return 0;
97 }
98 *p = '\0';
99
100 /* Blank line marks end of tests. */
101 if (s->buff[0] == '\0')
102 break;
103
104 /* Lines starting with a pound sign are ignored. */
105 if (s->buff[0] == '#')
106 continue;
107
108 /* Parse into key=value */
109 if (!TEST_ptr(equals = strchr(s->buff, '='))) {
110 TEST_info("Missing = at line %d\n", s->curr);
111 return 0;
112 }
113 *equals++ = '\0';
114 if (!TEST_ptr(key = strip_spaces(s->buff))) {
115 TEST_info("Empty field at line %d\n", s->curr);
116 return 0;
117 }
118 if ((value = strip_spaces(equals)) == NULL)
119 value = "";
120
121 if (strcmp(key, "Title") == 0) {
122 TEST_info("Starting \"%s\" tests at line %d", value, s->curr);
123 continue;
124 }
125
126 if (s->numpairs == 0)
127 s->start = s->curr;
128
129 if (strcmp(key, "PrivateKey") == 0) {
130 if (!read_key(s))
131 return 0;
132 }
133 if (strcmp(key, "PublicKey") == 0) {
134 if (!read_key(s))
135 return 0;
136 }
137
138 if (!TEST_int_lt(s->numpairs++, TESTMAXPAIRS)
139 || !TEST_ptr(pp->key = OPENSSL_strdup(key))
140 || !TEST_ptr(pp->value = OPENSSL_strdup(value)))
141 return 0;
142 pp++;
143 }
144
145 /* If we read anything, return ok. */
146 return 1;
147}
148
149void test_clearstanza(STANZA *s)
150{
151 PAIR *pp = s->pairs;
152 int i = s->numpairs;
153
154 for ( ; --i >= 0; pp++) {
155 OPENSSL_free(pp->key);
156 OPENSSL_free(pp->value);
157 }
158 s->numpairs = 0;
159}