xref: /freebsd-src/crypto/openssh/regress/unittests/misc/test_parse.c (revision 1323ec571215a77ddd21294f0871979d5ad6b992)
1*1323ec57SEd Maste /* 	$OpenBSD: test_parse.c,v 1.2 2021/12/14 21:25:27 deraadt Exp $ */
219261079SEd Maste /*
319261079SEd Maste  * Regress test for misc user/host/URI parsing functions.
419261079SEd Maste  *
519261079SEd Maste  * Placed in the public domain.
619261079SEd Maste  */
719261079SEd Maste 
819261079SEd Maste #include "includes.h"
919261079SEd Maste 
1019261079SEd Maste #include <sys/types.h>
1119261079SEd Maste #include <stdio.h>
1219261079SEd Maste #ifdef HAVE_STDINT_H
1319261079SEd Maste #include <stdint.h>
1419261079SEd Maste #endif
1519261079SEd Maste #include <stdlib.h>
1619261079SEd Maste #include <string.h>
1719261079SEd Maste 
1819261079SEd Maste #include "../test_helper/test_helper.h"
1919261079SEd Maste 
2019261079SEd Maste #include "log.h"
2119261079SEd Maste #include "misc.h"
2219261079SEd Maste 
2319261079SEd Maste void test_parse(void);
2419261079SEd Maste 
2519261079SEd Maste void
test_parse(void)2619261079SEd Maste test_parse(void)
2719261079SEd Maste {
2819261079SEd Maste 	int port;
2919261079SEd Maste 	char *user, *host, *path;
3019261079SEd Maste 
3119261079SEd Maste 	TEST_START("misc_parse_user_host_path");
3219261079SEd Maste 	ASSERT_INT_EQ(parse_user_host_path("someuser@some.host:some/path",
3319261079SEd Maste 	    &user, &host, &path), 0);
3419261079SEd Maste 	ASSERT_STRING_EQ(user, "someuser");
3519261079SEd Maste 	ASSERT_STRING_EQ(host, "some.host");
3619261079SEd Maste 	ASSERT_STRING_EQ(path, "some/path");
3719261079SEd Maste 	free(user); free(host); free(path);
3819261079SEd Maste 	TEST_DONE();
3919261079SEd Maste 
4019261079SEd Maste 	TEST_START("misc_parse_user_ipv4_path");
4119261079SEd Maste 	ASSERT_INT_EQ(parse_user_host_path("someuser@1.22.33.144:some/path",
4219261079SEd Maste 	    &user, &host, &path), 0);
4319261079SEd Maste 	ASSERT_STRING_EQ(user, "someuser");
4419261079SEd Maste 	ASSERT_STRING_EQ(host, "1.22.33.144");
4519261079SEd Maste 	ASSERT_STRING_EQ(path, "some/path");
4619261079SEd Maste 	free(user); free(host); free(path);
4719261079SEd Maste 	TEST_DONE();
4819261079SEd Maste 
4919261079SEd Maste 	TEST_START("misc_parse_user_[ipv4]_path");
5019261079SEd Maste 	ASSERT_INT_EQ(parse_user_host_path("someuser@[1.22.33.144]:some/path",
5119261079SEd Maste 	    &user, &host, &path), 0);
5219261079SEd Maste 	ASSERT_STRING_EQ(user, "someuser");
5319261079SEd Maste 	ASSERT_STRING_EQ(host, "1.22.33.144");
5419261079SEd Maste 	ASSERT_STRING_EQ(path, "some/path");
5519261079SEd Maste 	free(user); free(host); free(path);
5619261079SEd Maste 	TEST_DONE();
5719261079SEd Maste 
5819261079SEd Maste 	TEST_START("misc_parse_user_[ipv4]_nopath");
5919261079SEd Maste 	ASSERT_INT_EQ(parse_user_host_path("someuser@[1.22.33.144]:",
6019261079SEd Maste 	    &user, &host, &path), 0);
6119261079SEd Maste 	ASSERT_STRING_EQ(user, "someuser");
6219261079SEd Maste 	ASSERT_STRING_EQ(host, "1.22.33.144");
6319261079SEd Maste 	ASSERT_STRING_EQ(path, ".");
6419261079SEd Maste 	free(user); free(host); free(path);
6519261079SEd Maste 	TEST_DONE();
6619261079SEd Maste 
6719261079SEd Maste 	TEST_START("misc_parse_user_ipv6_path");
6819261079SEd Maste 	ASSERT_INT_EQ(parse_user_host_path("someuser@[::1]:some/path",
6919261079SEd Maste 	    &user, &host, &path), 0);
7019261079SEd Maste 	ASSERT_STRING_EQ(user, "someuser");
7119261079SEd Maste 	ASSERT_STRING_EQ(host, "::1");
7219261079SEd Maste 	ASSERT_STRING_EQ(path, "some/path");
7319261079SEd Maste 	free(user); free(host); free(path);
7419261079SEd Maste 	TEST_DONE();
7519261079SEd Maste 
7619261079SEd Maste 	TEST_START("misc_parse_uri");
7719261079SEd Maste 	ASSERT_INT_EQ(parse_uri("ssh", "ssh://someuser@some.host:22/some/path",
7819261079SEd Maste 	    &user, &host, &port, &path), 0);
7919261079SEd Maste 	ASSERT_STRING_EQ(user, "someuser");
8019261079SEd Maste 	ASSERT_STRING_EQ(host, "some.host");
8119261079SEd Maste 	ASSERT_INT_EQ(port, 22);
8219261079SEd Maste 	ASSERT_STRING_EQ(path, "some/path");
8319261079SEd Maste 	free(user); free(host); free(path);
8419261079SEd Maste 	TEST_DONE();
8519261079SEd Maste }
86