xref: /openbsd-src/regress/usr.sbin/rpki-client/test-ip.c (revision 6822f9c8033774f6aab00bcb43e8718e7034e67d)
1 /*	$Id: test-ip.c,v 1.11 2024/08/23 12:56:26 anton 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 <sys/socket.h>
19 #include <arpa/inet.h>
20 
21 #include <assert.h>
22 #include <err.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 
28 #include <openssl/err.h>
29 #include <openssl/evp.h>
30 #include <openssl/x509v3.h>
31 
32 #include "extern.h"
33 
34 int outformats;
35 int verbose;
36 int filemode;
37 int experimental;
38 
39 static void
40 test(const char *res, uint16_t afiv, size_t sz, size_t unused, ...)
41 {
42 	va_list		 ap;
43 	struct ip_addr	 addr;
44 	char		 buf[64];
45 	size_t		 i;
46 	enum afi	 afi;
47 	struct cert_ip	 ip;
48 	int		 rc;
49 
50 	afi = (afiv == 1) ? AFI_IPV4 : AFI_IPV6;
51 
52 	memset(&addr, 0, sizeof(struct ip_addr));
53 
54 	va_start(ap, unused);
55 	for (i = 0; i < sz; i++)
56 		addr.addr[i] = (unsigned char)va_arg(ap, int);
57 	va_end(ap);
58 
59 	addr.prefixlen = sz * 8 - unused;
60 	ip_addr_print(&addr, afi, buf, sizeof(buf));
61 	if (res != NULL && strcmp(res, buf))
62 		errx(EXIT_FAILURE, "fail: %s != %s", res, buf);
63 	else if (res != NULL)
64 		warnx("pass: %s", buf);
65 	else
66 		warnx("check: %s", buf);
67 
68 	ip.afi = afi;
69 	ip.type = CERT_IP_ADDR;
70 	ip.ip = addr;
71 	rc = ip_cert_compose_ranges(&ip);
72 
73 	inet_ntop((afiv == 1) ? AF_INET : AF_INET6, ip.min, buf, sizeof(buf));
74 	warnx("minimum: %s", buf);
75 	inet_ntop((afiv == 1) ? AF_INET : AF_INET6, ip.max, buf, sizeof(buf));
76 	warnx("maximum: %s", buf);
77 	if (!rc)
78 		errx(EXIT_FAILURE, "fail: minimum > maximum");
79 }
80 
81 int
82 main(int argc, char *argv[])
83 {
84 	ERR_load_crypto_strings();
85 	OpenSSL_add_all_ciphers();
86 	OpenSSL_add_all_digests();
87 
88 	test("10.5.0.4/32",
89 	     1, 0x04, 0x00, 0x0a, 0x05, 0x00, 0x04);
90 
91 	test("10.5.0.0/23",
92 	     1, 0x03, 0x01, 0x0a, 0x05, 0x00);
93 
94 	test("2001:0:200:3::1/128",
95 	     2, 0x10, 0x00, 0x20, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x03,
96                             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01);
97 
98 	test("2001:0:200::/39",
99 	     2, 0x05, 0x01, 0x20, 0x01, 0x00, 0x00, 0x02);
100 
101 	test("10.5.0.0/16",
102 	     1, 0x02, 0x00, 0x0a, 0x05);
103 
104 	test("10.5.0.0/23",
105 	     1, 0x03, 0x01, 0x0a, 0x05, 0x00);
106 
107 	test("2001:0:200::/39",
108 	     2, 0x05, 0x01, 0x20, 0x01, 0x00, 0x00, 0x02);
109 
110 	test("2001::/38",
111 	     2, 0x05, 0x02, 0x20, 0x01, 0x00, 0x00, 0x00);
112 
113 	test("0.0.0.0/0",
114 	     1, 0x00, 0x00);
115 
116 	test("10.64.0.0/12",
117 	     1, 0x02, 0x04, 0x0a, 0x40);
118 
119 	test("10.64.0.0/20",
120 	     1, 0x03, 0x04, 0x0a, 0x40, 0x00);
121 
122 	test("128.0.0.0/4",
123 	     1, 0x01, 0x04, 0x80);
124 	test("129.64.0.0/10",
125 	     1, 0x02, 0x06, 0x81, 0x40);
126 
127 	ERR_free_strings();
128 
129 	printf("OK\n");
130 	return 0;
131 }
132 
133 time_t
134 get_current_time(void)
135 {
136 	return time(NULL);
137 }
138