xref: /freebsd-src/crypto/openssh/regress/unittests/misc/test_hpdelim.c (revision 1323ec571215a77ddd21294f0871979d5ad6b992)
1*1323ec57SEd Maste /* 	$OpenBSD: test_hpdelim.c,v 1.2 2022/02/06 22:58:33 dtucker Exp $ */
2*1323ec57SEd Maste /*
3*1323ec57SEd Maste  * Regress test for misc hpdelim() and co
4*1323ec57SEd Maste  *
5*1323ec57SEd Maste  * Placed in the public domain.
6*1323ec57SEd Maste  */
7*1323ec57SEd Maste 
8*1323ec57SEd Maste #include "includes.h"
9*1323ec57SEd Maste 
10*1323ec57SEd Maste #include <sys/types.h>
11*1323ec57SEd Maste #include <stdio.h>
12*1323ec57SEd Maste #ifdef HAVE_STDINT_H
13*1323ec57SEd Maste #include <stdint.h>
14*1323ec57SEd Maste #endif
15*1323ec57SEd Maste #include <stdlib.h>
16*1323ec57SEd Maste #include <string.h>
17*1323ec57SEd Maste 
18*1323ec57SEd Maste #include "../test_helper/test_helper.h"
19*1323ec57SEd Maste 
20*1323ec57SEd Maste #include "log.h"
21*1323ec57SEd Maste #include "misc.h"
22*1323ec57SEd Maste #include "xmalloc.h"
23*1323ec57SEd Maste 
24*1323ec57SEd Maste void test_hpdelim(void);
25*1323ec57SEd Maste 
26*1323ec57SEd Maste void
test_hpdelim(void)27*1323ec57SEd Maste test_hpdelim(void)
28*1323ec57SEd Maste {
29*1323ec57SEd Maste 	char *orig, *str, *cp, *port;
30*1323ec57SEd Maste 
31*1323ec57SEd Maste #define START_STRING(x)	orig = str = xstrdup(x)
32*1323ec57SEd Maste #define DONE_STRING()	free(orig)
33*1323ec57SEd Maste 
34*1323ec57SEd Maste 	TEST_START("hpdelim host only");
35*1323ec57SEd Maste 	START_STRING("host");
36*1323ec57SEd Maste 	cp = hpdelim(&str);
37*1323ec57SEd Maste 	ASSERT_STRING_EQ(cp, "host");
38*1323ec57SEd Maste 	ASSERT_PTR_EQ(str, NULL);
39*1323ec57SEd Maste 	DONE_STRING();
40*1323ec57SEd Maste 	TEST_DONE();
41*1323ec57SEd Maste 
42*1323ec57SEd Maste 	TEST_START("hpdelim :port");
43*1323ec57SEd Maste 	START_STRING(":1234");
44*1323ec57SEd Maste 	cp = hpdelim(&str);
45*1323ec57SEd Maste 	ASSERT_STRING_EQ(cp, "");
46*1323ec57SEd Maste 	ASSERT_PTR_NE(str, NULL);
47*1323ec57SEd Maste 	port = hpdelim(&str);
48*1323ec57SEd Maste 	ASSERT_STRING_EQ(port, "1234");
49*1323ec57SEd Maste 	ASSERT_PTR_EQ(str, NULL);
50*1323ec57SEd Maste 	DONE_STRING();
51*1323ec57SEd Maste 	TEST_DONE();
52*1323ec57SEd Maste 
53*1323ec57SEd Maste 	TEST_START("hpdelim host:port");
54*1323ec57SEd Maste 	START_STRING("host:1234");
55*1323ec57SEd Maste 	cp = hpdelim(&str);
56*1323ec57SEd Maste 	ASSERT_STRING_EQ(cp, "host");
57*1323ec57SEd Maste 	ASSERT_PTR_NE(str, NULL);
58*1323ec57SEd Maste 	port = hpdelim(&str);
59*1323ec57SEd Maste 	ASSERT_STRING_EQ(port, "1234");
60*1323ec57SEd Maste 	ASSERT_PTR_EQ(str, NULL);
61*1323ec57SEd Maste 	DONE_STRING();
62*1323ec57SEd Maste 	TEST_DONE();
63*1323ec57SEd Maste 
64*1323ec57SEd Maste 	TEST_START("hpdelim [host]:port");
65*1323ec57SEd Maste 	START_STRING("[::1]:1234");
66*1323ec57SEd Maste 	cp = hpdelim(&str);
67*1323ec57SEd Maste 	ASSERT_STRING_EQ(cp, "[::1]");
68*1323ec57SEd Maste 	ASSERT_PTR_NE(str, NULL);
69*1323ec57SEd Maste 	port = hpdelim(&str);
70*1323ec57SEd Maste 	ASSERT_STRING_EQ(port, "1234");
71*1323ec57SEd Maste 	ASSERT_PTR_EQ(str, NULL);
72*1323ec57SEd Maste 	DONE_STRING();
73*1323ec57SEd Maste 	TEST_DONE();
74*1323ec57SEd Maste 
75*1323ec57SEd Maste 	TEST_START("hpdelim missing ] error");
76*1323ec57SEd Maste 	START_STRING("[::1:1234");
77*1323ec57SEd Maste 	cp = hpdelim(&str);
78*1323ec57SEd Maste 	ASSERT_PTR_EQ(cp, NULL);
79*1323ec57SEd Maste 	DONE_STRING();
80*1323ec57SEd Maste 	TEST_DONE();
81*1323ec57SEd Maste 
82*1323ec57SEd Maste }
83