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