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