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