1/*	$OpenBSD: bio_host.c,v 1.1 2022/12/08 17:49:02 tb Exp $	*/
2/*
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
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 <stdint.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include <arpa/inet.h>
24#include <netinet/in.h>
25
26#include <openssl/bio.h>
27
28struct bio_get_host_ip_test {
29	char *input;
30	uint32_t ip;
31	int ret;
32};
33
34struct bio_get_host_ip_test bio_get_host_ip_tests[] = {
35	{"", 0, 0},
36	{".", 0, 0},
37	{"1", 0, 0},
38	{"1.2", 0, 0},
39	{"1.2.3", 0, 0},
40	{"1.2.3.", 0, 0},
41	{"1.2.3.4", 0x01020304, 1},
42	{"1.2.3.256", 0, 0},
43	{"1:2:3::4", 0, 0},
44	{"0.0.0.0", INADDR_ANY, 1},
45	{"127.0.0.1", INADDR_LOOPBACK, 1},
46	{"localhost", INADDR_LOOPBACK, 1},
47	{"255.255.255.255", INADDR_BROADCAST, 1},
48	{"0xff.0xff.0xff.0xff", 0, 0},
49};
50
51#define N_BIO_GET_IP_TESTS \
52    (sizeof(bio_get_host_ip_tests) / sizeof(*bio_get_host_ip_tests))
53
54struct bio_get_port_test {
55	char *input;
56	unsigned short port;
57	int ret;
58};
59
60struct bio_get_port_test bio_get_port_tests[] = {
61	{NULL, 0, 0},
62	{"", 0, 0},
63	{"-1", 0, 0},
64	{"0", 0, 1},
65	{"1", 1, 1},
66	{"12345", 12345, 1},
67	{"65535", 65535, 1},
68	{"65536", 0, 0},
69	{"999999999999", 0, 0},
70	{"xyzzy", 0, 0},
71	{"https", 443, 1},
72	{"imaps", 993, 1},
73	{"telnet", 23, 1},
74};
75
76#define N_BIO_GET_PORT_TESTS \
77    (sizeof(bio_get_port_tests) / sizeof(*bio_get_port_tests))
78
79static int
80do_bio_get_host_ip_tests(void)
81{
82	struct bio_get_host_ip_test *bgit;
83	union {
84		unsigned char c[4];
85		uint32_t i;
86	} ip;
87	int failed = 0;
88	size_t i;
89	int ret;
90
91	for (i = 0; i < N_BIO_GET_IP_TESTS; i++) {
92		bgit = &bio_get_host_ip_tests[i];
93		memset(&ip, 0, sizeof(ip));
94
95		ret = BIO_get_host_ip(bgit->input, ip.c);
96		if (ret != bgit->ret) {
97			fprintf(stderr, "FAIL: test %zd (\"%s\") %s, want %s\n",
98			    i, bgit->input, ret ? "success" : "failure",
99			    bgit->ret ? "success" : "failure");
100			failed = 1;
101			continue;
102		}
103		if (ret && ntohl(ip.i) != bgit->ip) {
104			fprintf(stderr, "FAIL: test %zd (\"%s\") returned ip "
105			    "%x != %x\n", i, bgit->input,
106			    ntohl(ip.i), bgit->ip);
107			failed = 1;
108		}
109	}
110
111	return failed;
112}
113
114static int
115do_bio_get_port_tests(void)
116{
117	struct bio_get_port_test *bgpt;
118	unsigned short port;
119	int failed = 0;
120	size_t i;
121	int ret;
122
123	for (i = 0; i < N_BIO_GET_PORT_TESTS; i++) {
124		bgpt = &bio_get_port_tests[i];
125		port = 0;
126
127		ret = BIO_get_port(bgpt->input, &port);
128		if (ret != bgpt->ret) {
129			fprintf(stderr, "FAIL: test %zd (\"%s\") %s, want %s\n",
130			    i, bgpt->input, ret ? "success" : "failure",
131			    bgpt->ret ? "success" : "failure");
132			failed = 1;
133			continue;
134		}
135		if (ret && port != bgpt->port) {
136			fprintf(stderr, "FAIL: test %zd (\"%s\") returned port "
137			    "%u != %u\n", i, bgpt->input, port, bgpt->port);
138			failed = 1;
139		}
140	}
141
142	return failed;
143}
144
145int
146main(int argc, char **argv)
147{
148	int failed = 0;
149
150	failed |= do_bio_get_host_ip_tests();
151	failed |= do_bio_get_port_tests();
152
153	return failed;
154}
155