1 /*	$Id: test-tal.c,v 1.5 2019/11/06 07:19:45 claudio Exp $ */
2 /*
3  * Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <assert.h>
19 #include <err.h>
20 #include <stdio.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 
26 #include <openssl/err.h>
27 #include <openssl/evp.h>
28 #include <openssl/x509v3.h>
29 
30 #include "extern.h"
31 
32 int verbose;
33 
34 static void
35 tal_print(const struct tal *p)
36 {
37 	size_t	 i;
38 
39 	assert(p != NULL);
40 
41 	for (i = 0; i < p->urisz; i++)
42 		printf("%5zu: URI: %s\n", i + 1, p->uri[i]);
43 }
44 
45 int
46 main(int argc, char *argv[])
47 {
48 	int		 c, i, verb = 0;
49 	char		*buf;
50 	struct tal	*tal;
51 
52 	ERR_load_crypto_strings();
53 	OpenSSL_add_all_ciphers();
54 	OpenSSL_add_all_digests();
55 
56 	while ((c = getopt(argc, argv, "v")) != -1)
57 		switch (c) {
58 		case 'v':
59 			verb++;
60 			break;
61 		default:
62 			errx(1, "bad argument %c", c);
63 		}
64 
65 	argv += optind;
66 	argc -= optind;
67 
68 	if (argc == 0)
69 		errx(1, "argument missing");
70 
71 	for (i = 0; i < argc; i++) {
72 		buf = tal_read_file(argv[i]);
73 		tal = tal_parse(argv[i], buf);
74 		free(buf);
75 		if (tal == NULL)
76 			break;
77 		if (verb)
78 			tal_print(tal);
79 		tal_free(tal);
80 	}
81 
82 	EVP_cleanup();
83 	CRYPTO_cleanup_all_ex_data();
84 	ERR_remove_state(0);
85 	ERR_free_strings();
86 
87 	if (i < argc)
88 		errx(1, "test failed for %s", argv[i]);
89 
90 	printf("OK\n");
91 	return 0;
92 }
93