1*720505cfStb /* $OpenBSD: bio_host.c,v 1.1 2022/12/08 17:49:02 tb Exp $ */
2*720505cfStb /*
3*720505cfStb * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
4*720505cfStb *
5*720505cfStb * Permission to use, copy, modify, and distribute this software for any
6*720505cfStb * purpose with or without fee is hereby granted, provided that the above
7*720505cfStb * copyright notice and this permission notice appear in all copies.
8*720505cfStb *
9*720505cfStb * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10*720505cfStb * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*720505cfStb * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12*720505cfStb * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*720505cfStb * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*720505cfStb * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15*720505cfStb * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*720505cfStb */
17*720505cfStb
18*720505cfStb #include <stdint.h>
19*720505cfStb #include <stdio.h>
20*720505cfStb #include <stdlib.h>
21*720505cfStb #include <string.h>
22*720505cfStb
23*720505cfStb #include <arpa/inet.h>
24*720505cfStb #include <netinet/in.h>
25*720505cfStb
26*720505cfStb #include <openssl/bio.h>
27*720505cfStb
28*720505cfStb struct bio_get_host_ip_test {
29*720505cfStb char *input;
30*720505cfStb uint32_t ip;
31*720505cfStb int ret;
32*720505cfStb };
33*720505cfStb
34*720505cfStb struct bio_get_host_ip_test bio_get_host_ip_tests[] = {
35*720505cfStb {"", 0, 0},
36*720505cfStb {".", 0, 0},
37*720505cfStb {"1", 0, 0},
38*720505cfStb {"1.2", 0, 0},
39*720505cfStb {"1.2.3", 0, 0},
40*720505cfStb {"1.2.3.", 0, 0},
41*720505cfStb {"1.2.3.4", 0x01020304, 1},
42*720505cfStb {"1.2.3.256", 0, 0},
43*720505cfStb {"1:2:3::4", 0, 0},
44*720505cfStb {"0.0.0.0", INADDR_ANY, 1},
45*720505cfStb {"127.0.0.1", INADDR_LOOPBACK, 1},
46*720505cfStb {"localhost", INADDR_LOOPBACK, 1},
47*720505cfStb {"255.255.255.255", INADDR_BROADCAST, 1},
48*720505cfStb {"0xff.0xff.0xff.0xff", 0, 0},
49*720505cfStb };
50*720505cfStb
51*720505cfStb #define N_BIO_GET_IP_TESTS \
52*720505cfStb (sizeof(bio_get_host_ip_tests) / sizeof(*bio_get_host_ip_tests))
53*720505cfStb
54*720505cfStb struct bio_get_port_test {
55*720505cfStb char *input;
56*720505cfStb unsigned short port;
57*720505cfStb int ret;
58*720505cfStb };
59*720505cfStb
60*720505cfStb struct bio_get_port_test bio_get_port_tests[] = {
61*720505cfStb {NULL, 0, 0},
62*720505cfStb {"", 0, 0},
63*720505cfStb {"-1", 0, 0},
64*720505cfStb {"0", 0, 1},
65*720505cfStb {"1", 1, 1},
66*720505cfStb {"12345", 12345, 1},
67*720505cfStb {"65535", 65535, 1},
68*720505cfStb {"65536", 0, 0},
69*720505cfStb {"999999999999", 0, 0},
70*720505cfStb {"xyzzy", 0, 0},
71*720505cfStb {"https", 443, 1},
72*720505cfStb {"imaps", 993, 1},
73*720505cfStb {"telnet", 23, 1},
74*720505cfStb };
75*720505cfStb
76*720505cfStb #define N_BIO_GET_PORT_TESTS \
77*720505cfStb (sizeof(bio_get_port_tests) / sizeof(*bio_get_port_tests))
78*720505cfStb
79*720505cfStb static int
do_bio_get_host_ip_tests(void)80*720505cfStb do_bio_get_host_ip_tests(void)
81*720505cfStb {
82*720505cfStb struct bio_get_host_ip_test *bgit;
83*720505cfStb union {
84*720505cfStb unsigned char c[4];
85*720505cfStb uint32_t i;
86*720505cfStb } ip;
87*720505cfStb int failed = 0;
88*720505cfStb size_t i;
89*720505cfStb int ret;
90*720505cfStb
91*720505cfStb for (i = 0; i < N_BIO_GET_IP_TESTS; i++) {
92*720505cfStb bgit = &bio_get_host_ip_tests[i];
93*720505cfStb memset(&ip, 0, sizeof(ip));
94*720505cfStb
95*720505cfStb ret = BIO_get_host_ip(bgit->input, ip.c);
96*720505cfStb if (ret != bgit->ret) {
97*720505cfStb fprintf(stderr, "FAIL: test %zd (\"%s\") %s, want %s\n",
98*720505cfStb i, bgit->input, ret ? "success" : "failure",
99*720505cfStb bgit->ret ? "success" : "failure");
100*720505cfStb failed = 1;
101*720505cfStb continue;
102*720505cfStb }
103*720505cfStb if (ret && ntohl(ip.i) != bgit->ip) {
104*720505cfStb fprintf(stderr, "FAIL: test %zd (\"%s\") returned ip "
105*720505cfStb "%x != %x\n", i, bgit->input,
106*720505cfStb ntohl(ip.i), bgit->ip);
107*720505cfStb failed = 1;
108*720505cfStb }
109*720505cfStb }
110*720505cfStb
111*720505cfStb return failed;
112*720505cfStb }
113*720505cfStb
114*720505cfStb static int
do_bio_get_port_tests(void)115*720505cfStb do_bio_get_port_tests(void)
116*720505cfStb {
117*720505cfStb struct bio_get_port_test *bgpt;
118*720505cfStb unsigned short port;
119*720505cfStb int failed = 0;
120*720505cfStb size_t i;
121*720505cfStb int ret;
122*720505cfStb
123*720505cfStb for (i = 0; i < N_BIO_GET_PORT_TESTS; i++) {
124*720505cfStb bgpt = &bio_get_port_tests[i];
125*720505cfStb port = 0;
126*720505cfStb
127*720505cfStb ret = BIO_get_port(bgpt->input, &port);
128*720505cfStb if (ret != bgpt->ret) {
129*720505cfStb fprintf(stderr, "FAIL: test %zd (\"%s\") %s, want %s\n",
130*720505cfStb i, bgpt->input, ret ? "success" : "failure",
131*720505cfStb bgpt->ret ? "success" : "failure");
132*720505cfStb failed = 1;
133*720505cfStb continue;
134*720505cfStb }
135*720505cfStb if (ret && port != bgpt->port) {
136*720505cfStb fprintf(stderr, "FAIL: test %zd (\"%s\") returned port "
137*720505cfStb "%u != %u\n", i, bgpt->input, port, bgpt->port);
138*720505cfStb failed = 1;
139*720505cfStb }
140*720505cfStb }
141*720505cfStb
142*720505cfStb return failed;
143*720505cfStb }
144*720505cfStb
145*720505cfStb int
main(int argc,char ** argv)146*720505cfStb main(int argc, char **argv)
147*720505cfStb {
148*720505cfStb int failed = 0;
149*720505cfStb
150*720505cfStb failed |= do_bio_get_host_ip_tests();
151*720505cfStb failed |= do_bio_get_port_tests();
152*720505cfStb
153*720505cfStb return failed;
154*720505cfStb }
155