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