Debian Codegraph @

regress/unittests/misc/test_parse.c

raw
1 /* $OpenBSD: test_parse.c,v 1.3 2025/06/12 10:09:39 dtucker 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 #include <stdint.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include "../test_helper/test_helper.h"
17
18 #include "log.h"
19 #include "misc.h"
20
21 void test_parse(void);
22
23 void
24 test_parse(void)
25 {
26 int port;
27 char *user, *host, *path;
28
29 TEST_START("misc_parse_user_host_path");
30 user = host = path = NULL;
31 ASSERT_INT_EQ(parse_user_host_path("[email protected]: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 user = host = path = NULL;
41 ASSERT_INT_EQ(parse_user_host_path("[email protected]: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 user = host = path = NULL;
51 ASSERT_INT_EQ(parse_user_host_path("someuser@[1.22.33.144]:some/path",
52 &user, &host, &path), 0);
53 ASSERT_STRING_EQ(user, "someuser");
54 ASSERT_STRING_EQ(host, "1.22.33.144");
55 ASSERT_STRING_EQ(path, "some/path");
56 free(user); free(host); free(path);
57 TEST_DONE();
58
59 TEST_START("misc_parse_user_[ipv4]_nopath");
60 user = host = path = NULL;
61 ASSERT_INT_EQ(parse_user_host_path("someuser@[1.22.33.144]:",
62 &user, &host, &path), 0);
63 ASSERT_STRING_EQ(user, "someuser");
64 ASSERT_STRING_EQ(host, "1.22.33.144");
65 ASSERT_STRING_EQ(path, ".");
66 free(user); free(host); free(path);
67 TEST_DONE();
68
69 TEST_START("misc_parse_user_ipv6_path");
70 user = host = path = NULL;
71 ASSERT_INT_EQ(parse_user_host_path("someuser@[::1]:some/path",
72 &user, &host, &path), 0);
73 ASSERT_STRING_EQ(user, "someuser");
74 ASSERT_STRING_EQ(host, "::1");
75 ASSERT_STRING_EQ(path, "some/path");
76 free(user); free(host); free(path);
77 TEST_DONE();
78
79 TEST_START("misc_parse_uri");
80 user = host = path = NULL;
81 ASSERT_INT_EQ(parse_uri("ssh", "ssh://[email protected]:22/some/path",
82 &user, &host, &port, &path), 0);
83 ASSERT_STRING_EQ(user, "someuser");
84 ASSERT_STRING_EQ(host, "some.host");
85 ASSERT_INT_EQ(port, 22);
86 ASSERT_STRING_EQ(path, "some/path");
87 free(user); free(host); free(path);
88 TEST_DONE();
89 }