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