1 /* 	$OpenBSD: test_parse.c,v 1.1 2021/03/19 03:25:01 djm Exp $ */
2 /*
3  * Regress test for misc user/host/URI parsing functions.
4  *
5  * Placed in the public domain.
6  */
7 
8 #include "includes.h"
9 
10 #include <sys/types.h>
11 #include <sys/param.h>
12 #include <stdio.h>
13 #include <stdint.h>
14 #include <stdlib.h>
15 #include <string.h>
16 
17 #include "../test_helper/test_helper.h"
18 
19 #include "log.h"
20 #include "misc.h"
21 
22 void test_parse(void);
23 
24 void
test_parse(void)25 test_parse(void)
26 {
27 	int port;
28 	char *user, *host, *path;
29 
30 	TEST_START("misc_parse_user_host_path");
31 	ASSERT_INT_EQ(parse_user_host_path("someuser@some.host:some/path",
32 	    &user, &host, &path), 0);
33 	ASSERT_STRING_EQ(user, "someuser");
34 	ASSERT_STRING_EQ(host, "some.host");
35 	ASSERT_STRING_EQ(path, "some/path");
36 	free(user); free(host); free(path);
37 	TEST_DONE();
38 
39 	TEST_START("misc_parse_user_ipv4_path");
40 	ASSERT_INT_EQ(parse_user_host_path("someuser@1.22.33.144:some/path",
41 	    &user, &host, &path), 0);
42 	ASSERT_STRING_EQ(user, "someuser");
43 	ASSERT_STRING_EQ(host, "1.22.33.144");
44 	ASSERT_STRING_EQ(path, "some/path");
45 	free(user); free(host); free(path);
46 	TEST_DONE();
47 
48 	TEST_START("misc_parse_user_[ipv4]_path");
49 	ASSERT_INT_EQ(parse_user_host_path("someuser@[1.22.33.144]:some/path",
50 	    &user, &host, &path), 0);
51 	ASSERT_STRING_EQ(user, "someuser");
52 	ASSERT_STRING_EQ(host, "1.22.33.144");
53 	ASSERT_STRING_EQ(path, "some/path");
54 	free(user); free(host); free(path);
55 	TEST_DONE();
56 
57 	TEST_START("misc_parse_user_[ipv4]_nopath");
58 	ASSERT_INT_EQ(parse_user_host_path("someuser@[1.22.33.144]:",
59 	    &user, &host, &path), 0);
60 	ASSERT_STRING_EQ(user, "someuser");
61 	ASSERT_STRING_EQ(host, "1.22.33.144");
62 	ASSERT_STRING_EQ(path, ".");
63 	free(user); free(host); free(path);
64 	TEST_DONE();
65 
66 	TEST_START("misc_parse_user_ipv6_path");
67 	ASSERT_INT_EQ(parse_user_host_path("someuser@[::1]:some/path",
68 	    &user, &host, &path), 0);
69 	ASSERT_STRING_EQ(user, "someuser");
70 	ASSERT_STRING_EQ(host, "::1");
71 	ASSERT_STRING_EQ(path, "some/path");
72 	free(user); free(host); free(path);
73 	TEST_DONE();
74 
75 	TEST_START("misc_parse_uri");
76 	ASSERT_INT_EQ(parse_uri("ssh", "ssh://someuser@some.host:22/some/path",
77 	    &user, &host, &port, &path), 0);
78 	ASSERT_STRING_EQ(user, "someuser");
79 	ASSERT_STRING_EQ(host, "some.host");
80 	ASSERT_INT_EQ(port, 22);
81 	ASSERT_STRING_EQ(path, "some/path");
82 	free(user); free(host); free(path);
83 	TEST_DONE();
84 }
85