Add a test for the info callback
[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 "internal/nelem.h"
16 #include "../testutil.h"
17 #include "tu_local.h"
18
19 int 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));
24     if (!TEST_ptr(s->fp = BIO_new_file(testfile, "r")))
25         return 0;
26     s->test_file = testfile;
27     return 1;
28 }
29
30 int 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  */
41 static 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;
57         if (strncmp(tmpbuf, "-----END", 8) == 0)
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  */
68 static char *strip_spaces(char *p)
69 {
70     char *q;
71
72     /* Skip over leading spaces */
73     while (*p && isspace((unsigned char)*p))
74         p++;
75     if (!*p)
76         return NULL;
77
78     for (q = p + strlen(p) - 1; q != p && isspace((unsigned char)*q); )
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  */
86 int test_readstanza(STANZA *s)
87 {
88     PAIR *pp = s->pairs;
89     char *p, *equals, *key, *value;
90
91     for (s->numpairs = 0; BIO_gets(s->fp, s->buff, sizeof(s->buff)); ) {
92         s->curr++;
93         if (!TEST_ptr(p = strchr(s->buff, '\n'))) {
94             TEST_info("Line %d too long", s->curr);
95             return 0;
96         }
97         *p = '\0';
98
99         /* Blank line marks end of tests. */
100         if (s->buff[0] == '\0')
101             break;
102
103         /* Lines starting with a pound sign are ignored. */
104         if (s->buff[0] == '#')
105             continue;
106
107         /* Parse into key=value */
108         if (!TEST_ptr(equals = strchr(s->buff, '='))) {
109             TEST_info("Missing = at line %d\n", s->curr);
110             return 0;
111         }
112         *equals++ = '\0';
113         if (!TEST_ptr(key = strip_spaces(s->buff))) {
114             TEST_info("Empty field at line %d\n", s->curr);
115             return 0;
116         }
117         if ((value = strip_spaces(equals)) == NULL)
118             value = "";
119
120         if (strcmp(key, "Title") == 0) {
121             TEST_info("Starting \"%s\" tests at line %d", value, s->curr);
122             continue;
123         }
124
125         if (s->numpairs == 0)
126             s->start = s->curr;
127
128         if (strcmp(key, "PrivateKey") == 0) {
129             if (!read_key(s))
130                 return 0;
131         }
132         if (strcmp(key, "PublicKey") == 0) {
133             if (!read_key(s))
134                 return 0;
135         }
136
137         if (!TEST_int_lt(s->numpairs++, TESTMAXPAIRS)
138                 || !TEST_ptr(pp->key = OPENSSL_strdup(key))
139                 || !TEST_ptr(pp->value = OPENSSL_strdup(value)))
140             return 0;
141         pp++;
142     }
143
144     /* If we read anything, return ok. */
145     return 1;
146 }
147
148 void test_clearstanza(STANZA *s)
149 {
150     PAIR *pp = s->pairs;
151     int i = s->numpairs;
152
153     for ( ; --i >= 0; pp++) {
154         OPENSSL_free(pp->key);
155         OPENSSL_free(pp->value);
156     }
157     s->numpairs = 0;
158 }