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