xref: /freebsd/crypto/openssh/regress/unittests/misc/test_parse.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /* 	$OpenBSD: test_parse.c,v 1.2 2021/12/14 21:25:27 deraadt 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 <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 
23 void test_parse(void);
24 
25 void
26 test_parse(void)
27 {
28 	int port;
29 	char *user, *host, *path;
30 
31 	TEST_START("misc_parse_user_host_path");
32 	ASSERT_INT_EQ(parse_user_host_path("someuser@some.host:some/path",
33 	    &user, &host, &path), 0);
34 	ASSERT_STRING_EQ(user, "someuser");
35 	ASSERT_STRING_EQ(host, "some.host");
36 	ASSERT_STRING_EQ(path, "some/path");
37 	free(user); free(host); free(path);
38 	TEST_DONE();
39 
40 	TEST_START("misc_parse_user_ipv4_path");
41 	ASSERT_INT_EQ(parse_user_host_path("someuser@1.22.33.144:some/path",
42 	    &user, &host, &path), 0);
43 	ASSERT_STRING_EQ(user, "someuser");
44 	ASSERT_STRING_EQ(host, "1.22.33.144");
45 	ASSERT_STRING_EQ(path, "some/path");
46 	free(user); free(host); free(path);
47 	TEST_DONE();
48 
49 	TEST_START("misc_parse_user_[ipv4]_path");
50 	ASSERT_INT_EQ(parse_user_host_path("someuser@[1.22.33.144]:some/path",
51 	    &user, &host, &path), 0);
52 	ASSERT_STRING_EQ(user, "someuser");
53 	ASSERT_STRING_EQ(host, "1.22.33.144");
54 	ASSERT_STRING_EQ(path, "some/path");
55 	free(user); free(host); free(path);
56 	TEST_DONE();
57 
58 	TEST_START("misc_parse_user_[ipv4]_nopath");
59 	ASSERT_INT_EQ(parse_user_host_path("someuser@[1.22.33.144]:",
60 	    &user, &host, &path), 0);
61 	ASSERT_STRING_EQ(user, "someuser");
62 	ASSERT_STRING_EQ(host, "1.22.33.144");
63 	ASSERT_STRING_EQ(path, ".");
64 	free(user); free(host); free(path);
65 	TEST_DONE();
66 
67 	TEST_START("misc_parse_user_ipv6_path");
68 	ASSERT_INT_EQ(parse_user_host_path("someuser@[::1]:some/path",
69 	    &user, &host, &path), 0);
70 	ASSERT_STRING_EQ(user, "someuser");
71 	ASSERT_STRING_EQ(host, "::1");
72 	ASSERT_STRING_EQ(path, "some/path");
73 	free(user); free(host); free(path);
74 	TEST_DONE();
75 
76 	TEST_START("misc_parse_uri");
77 	ASSERT_INT_EQ(parse_uri("ssh", "ssh://someuser@some.host:22/some/path",
78 	    &user, &host, &port, &path), 0);
79 	ASSERT_STRING_EQ(user, "someuser");
80 	ASSERT_STRING_EQ(host, "some.host");
81 	ASSERT_INT_EQ(port, 22);
82 	ASSERT_STRING_EQ(path, "some/path");
83 	free(user); free(host); free(path);
84 	TEST_DONE();
85 }
86