xref: /openbsd-src/regress/usr.bin/ssh/unittests/misc/test_parse.c (revision 24bb5fcea3ed904bc467217bdaadb5dfc618d5bf)
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 <sys/types.h>
9 #include <sys/param.h>
10 #include <stdio.h>
11 #include <stdint.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include "test_helper.h"
16 
17 #include "log.h"
18 #include "misc.h"
19 
20 void test_parse(void);
21 
22 void
23 test_parse(void)
24 {
25 	int port;
26 	char *user, *host, *path;
27 
28 	TEST_START("misc_parse_user_host_path");
29 	ASSERT_INT_EQ(parse_user_host_path("someuser@some.host:some/path",
30 	    &user, &host, &path), 0);
31 	ASSERT_STRING_EQ(user, "someuser");
32 	ASSERT_STRING_EQ(host, "some.host");
33 	ASSERT_STRING_EQ(path, "some/path");
34 	free(user); free(host); free(path);
35 	TEST_DONE();
36 
37 	TEST_START("misc_parse_user_ipv4_path");
38 	ASSERT_INT_EQ(parse_user_host_path("someuser@1.22.33.144:some/path",
39 	    &user, &host, &path), 0);
40 	ASSERT_STRING_EQ(user, "someuser");
41 	ASSERT_STRING_EQ(host, "1.22.33.144");
42 	ASSERT_STRING_EQ(path, "some/path");
43 	free(user); free(host); free(path);
44 	TEST_DONE();
45 
46 	TEST_START("misc_parse_user_[ipv4]_path");
47 	ASSERT_INT_EQ(parse_user_host_path("someuser@[1.22.33.144]:some/path",
48 	    &user, &host, &path), 0);
49 	ASSERT_STRING_EQ(user, "someuser");
50 	ASSERT_STRING_EQ(host, "1.22.33.144");
51 	ASSERT_STRING_EQ(path, "some/path");
52 	free(user); free(host); free(path);
53 	TEST_DONE();
54 
55 	TEST_START("misc_parse_user_[ipv4]_nopath");
56 	ASSERT_INT_EQ(parse_user_host_path("someuser@[1.22.33.144]:",
57 	    &user, &host, &path), 0);
58 	ASSERT_STRING_EQ(user, "someuser");
59 	ASSERT_STRING_EQ(host, "1.22.33.144");
60 	ASSERT_STRING_EQ(path, ".");
61 	free(user); free(host); free(path);
62 	TEST_DONE();
63 
64 	TEST_START("misc_parse_user_ipv6_path");
65 	ASSERT_INT_EQ(parse_user_host_path("someuser@[::1]:some/path",
66 	    &user, &host, &path), 0);
67 	ASSERT_STRING_EQ(user, "someuser");
68 	ASSERT_STRING_EQ(host, "::1");
69 	ASSERT_STRING_EQ(path, "some/path");
70 	free(user); free(host); free(path);
71 	TEST_DONE();
72 
73 	TEST_START("misc_parse_uri");
74 	ASSERT_INT_EQ(parse_uri("ssh", "ssh://someuser@some.host:22/some/path",
75 	    &user, &host, &port, &path), 0);
76 	ASSERT_STRING_EQ(user, "someuser");
77 	ASSERT_STRING_EQ(host, "some.host");
78 	ASSERT_INT_EQ(port, 22);
79 	ASSERT_STRING_EQ(path, "some/path");
80 	free(user); free(host); free(path);
81 	TEST_DONE();
82 }
83