xref: /freebsd/crypto/openssh/regress/unittests/misc/test_hpdelim.c (revision 644b4646c7acab87dc20d4e5dd53d2d9da152989)
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 "includes.h"
9 
10 #include <sys/types.h>
11 #include <stdio.h>
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <string.h>
15 
16 #include "../test_helper/test_helper.h"
17 
18 #include "log.h"
19 #include "misc.h"
20 #include "xmalloc.h"
21 
22 void test_hpdelim(void);
23 
24 void
test_hpdelim(void)25 test_hpdelim(void)
26 {
27 	char *orig, *str, *cp, *port;
28 
29 #define START_STRING(x)	orig = str = xstrdup(x)
30 #define DONE_STRING()	free(orig)
31 
32 	TEST_START("hpdelim host only");
33 	START_STRING("host");
34 	cp = hpdelim(&str);
35 	ASSERT_STRING_EQ(cp, "host");
36 	ASSERT_PTR_EQ(str, NULL);
37 	DONE_STRING();
38 	TEST_DONE();
39 
40 	TEST_START("hpdelim :port");
41 	START_STRING(":1234");
42 	cp = hpdelim(&str);
43 	ASSERT_STRING_EQ(cp, "");
44 	ASSERT_PTR_NE(str, NULL);
45 	port = hpdelim(&str);
46 	ASSERT_STRING_EQ(port, "1234");
47 	ASSERT_PTR_EQ(str, NULL);
48 	DONE_STRING();
49 	TEST_DONE();
50 
51 	TEST_START("hpdelim host:port");
52 	START_STRING("host:1234");
53 	cp = hpdelim(&str);
54 	ASSERT_STRING_EQ(cp, "host");
55 	ASSERT_PTR_NE(str, NULL);
56 	port = hpdelim(&str);
57 	ASSERT_STRING_EQ(port, "1234");
58 	ASSERT_PTR_EQ(str, NULL);
59 	DONE_STRING();
60 	TEST_DONE();
61 
62 	TEST_START("hpdelim [host]:port");
63 	START_STRING("[::1]:1234");
64 	cp = hpdelim(&str);
65 	ASSERT_STRING_EQ(cp, "[::1]");
66 	ASSERT_PTR_NE(str, NULL);
67 	port = hpdelim(&str);
68 	ASSERT_STRING_EQ(port, "1234");
69 	ASSERT_PTR_EQ(str, NULL);
70 	DONE_STRING();
71 	TEST_DONE();
72 
73 	TEST_START("hpdelim missing ] error");
74 	START_STRING("[::1:1234");
75 	cp = hpdelim(&str);
76 	ASSERT_PTR_EQ(cp, NULL);
77 	DONE_STRING();
78 	TEST_DONE();
79 
80 }
81