1 /* $OpenBSD: test_argv.c,v 1.3 2021/06/08 07:40:12 djm Exp $ */ 2 /* 3 * Regress test for misc argv handling 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_argv(void); 25 26 void 27 test_argv(void) 28 { 29 char **av = NULL; 30 int ac = 0; 31 32 #define RESET_ARGV() \ 33 do { \ 34 argv_free(av, ac); \ 35 av = NULL; \ 36 ac = -1; \ 37 } while (0) 38 39 TEST_START("empty args"); 40 ASSERT_INT_EQ(argv_split("", &ac, &av, 0), 0); 41 ASSERT_INT_EQ(ac, 0); 42 ASSERT_PTR_NE(av, NULL); 43 ASSERT_PTR_EQ(av[0], NULL); 44 RESET_ARGV(); 45 ASSERT_INT_EQ(argv_split(" ", &ac, &av, 0), 0); 46 ASSERT_INT_EQ(ac, 0); 47 ASSERT_PTR_NE(av, NULL); 48 ASSERT_PTR_EQ(av[0], NULL); 49 RESET_ARGV(); 50 TEST_DONE(); 51 52 TEST_START("trivial args"); 53 ASSERT_INT_EQ(argv_split("leamas", &ac, &av, 0), 0); 54 ASSERT_INT_EQ(ac, 1); 55 ASSERT_PTR_NE(av, NULL); 56 ASSERT_STRING_EQ(av[0], "leamas"); 57 ASSERT_PTR_EQ(av[1], NULL); 58 RESET_ARGV(); 59 ASSERT_INT_EQ(argv_split("smiley leamas", &ac, &av, 0), 0); 60 ASSERT_INT_EQ(ac, 2); 61 ASSERT_PTR_NE(av, NULL); 62 ASSERT_STRING_EQ(av[0], "smiley"); 63 ASSERT_STRING_EQ(av[1], "leamas"); 64 ASSERT_PTR_EQ(av[2], NULL); 65 RESET_ARGV(); 66 TEST_DONE(); 67 68 TEST_START("quoted"); 69 ASSERT_INT_EQ(argv_split("\"smiley\"", &ac, &av, 0), 0); 70 ASSERT_INT_EQ(ac, 1); 71 ASSERT_PTR_NE(av, NULL); 72 ASSERT_STRING_EQ(av[0], "smiley"); 73 ASSERT_PTR_EQ(av[1], NULL); 74 RESET_ARGV(); 75 ASSERT_INT_EQ(argv_split("leamas \" smiley \"", &ac, &av, 0), 0); 76 ASSERT_INT_EQ(ac, 2); 77 ASSERT_PTR_NE(av, NULL); 78 ASSERT_STRING_EQ(av[0], "leamas"); 79 ASSERT_STRING_EQ(av[1], " smiley "); 80 ASSERT_PTR_EQ(av[2], NULL); 81 RESET_ARGV(); 82 ASSERT_INT_EQ(argv_split("\"smiley leamas\"", &ac, &av, 0), 0); 83 ASSERT_INT_EQ(ac, 1); 84 ASSERT_PTR_NE(av, NULL); 85 ASSERT_STRING_EQ(av[0], "smiley leamas"); 86 ASSERT_PTR_EQ(av[1], NULL); 87 RESET_ARGV(); 88 ASSERT_INT_EQ(argv_split("smiley\" leamas\" liz", &ac, &av, 0), 0); 89 ASSERT_INT_EQ(ac, 2); 90 ASSERT_PTR_NE(av, NULL); 91 ASSERT_STRING_EQ(av[0], "smiley leamas"); 92 ASSERT_STRING_EQ(av[1], "liz"); 93 ASSERT_PTR_EQ(av[2], NULL); 94 RESET_ARGV(); 95 TEST_DONE(); 96 97 TEST_START("escaped"); 98 ASSERT_INT_EQ(argv_split("\\\"smiley\\'", &ac, &av, 0), 0); 99 ASSERT_INT_EQ(ac, 1); 100 ASSERT_PTR_NE(av, NULL); 101 ASSERT_STRING_EQ(av[0], "\"smiley'"); 102 ASSERT_PTR_EQ(av[1], NULL); 103 RESET_ARGV(); 104 ASSERT_INT_EQ(argv_split("'\\'smiley\\\"'", &ac, &av, 0), 0); 105 ASSERT_INT_EQ(ac, 1); 106 ASSERT_PTR_NE(av, NULL); 107 ASSERT_STRING_EQ(av[0], "'smiley\""); 108 ASSERT_PTR_EQ(av[1], NULL); 109 RESET_ARGV(); 110 ASSERT_INT_EQ(argv_split("smiley\\'s leamas\\'", &ac, &av, 0), 0); 111 ASSERT_INT_EQ(ac, 2); 112 ASSERT_PTR_NE(av, NULL); 113 ASSERT_STRING_EQ(av[0], "smiley's"); 114 ASSERT_STRING_EQ(av[1], "leamas'"); 115 ASSERT_PTR_EQ(av[2], NULL); 116 RESET_ARGV(); 117 ASSERT_INT_EQ(argv_split("leamas\\\\smiley", &ac, &av, 0), 0); 118 ASSERT_INT_EQ(ac, 1); 119 ASSERT_PTR_NE(av, NULL); 120 ASSERT_STRING_EQ(av[0], "leamas\\smiley"); 121 ASSERT_PTR_EQ(av[1], NULL); 122 RESET_ARGV(); 123 ASSERT_INT_EQ(argv_split("leamas\\\\ \\\\smiley", &ac, &av, 0), 0); 124 ASSERT_INT_EQ(ac, 2); 125 ASSERT_PTR_NE(av, NULL); 126 ASSERT_STRING_EQ(av[0], "leamas\\"); 127 ASSERT_STRING_EQ(av[1], "\\smiley"); 128 ASSERT_PTR_EQ(av[2], NULL); 129 RESET_ARGV(); 130 ASSERT_INT_EQ(argv_split("smiley\\ leamas", &ac, &av, 0), 0); 131 ASSERT_INT_EQ(ac, 1); 132 ASSERT_PTR_NE(av, NULL); 133 ASSERT_STRING_EQ(av[0], "smiley leamas"); 134 ASSERT_PTR_EQ(av[1], NULL); 135 RESET_ARGV(); 136 TEST_DONE(); 137 138 TEST_START("quoted escaped"); 139 ASSERT_INT_EQ(argv_split("'smiley\\ leamas'", &ac, &av, 0), 0); 140 ASSERT_INT_EQ(ac, 1); 141 ASSERT_PTR_NE(av, NULL); 142 ASSERT_STRING_EQ(av[0], "smiley\\ leamas"); 143 ASSERT_PTR_EQ(av[1], NULL); 144 RESET_ARGV(); 145 ASSERT_INT_EQ(argv_split("\"smiley\\ leamas\"", &ac, &av, 0), 0); 146 ASSERT_INT_EQ(ac, 1); 147 ASSERT_PTR_NE(av, NULL); 148 ASSERT_STRING_EQ(av[0], "smiley\\ leamas"); 149 ASSERT_PTR_EQ(av[1], NULL); 150 RESET_ARGV(); 151 TEST_DONE(); 152 153 TEST_START("comments"); 154 ASSERT_INT_EQ(argv_split("# gold", &ac, &av, 0), 0); 155 ASSERT_INT_EQ(ac, 2); 156 ASSERT_PTR_NE(av, NULL); 157 ASSERT_STRING_EQ(av[0], "#"); 158 ASSERT_STRING_EQ(av[1], "gold"); 159 ASSERT_PTR_EQ(av[2], NULL); 160 RESET_ARGV(); 161 ASSERT_INT_EQ(argv_split("# gold", &ac, &av, 1), 0); 162 ASSERT_INT_EQ(ac, 0); 163 ASSERT_PTR_NE(av, NULL); 164 ASSERT_PTR_EQ(av[0], NULL); 165 RESET_ARGV(); 166 ASSERT_INT_EQ(argv_split("leamas#gold", &ac, &av, 1), 0); 167 ASSERT_INT_EQ(ac, 1); 168 ASSERT_PTR_NE(av, NULL); 169 ASSERT_STRING_EQ(av[0], "leamas#gold"); 170 ASSERT_PTR_EQ(av[1], NULL); 171 RESET_ARGV(); 172 ASSERT_INT_EQ(argv_split("\"leamas # gold\"", &ac, &av, 1), 0); 173 ASSERT_INT_EQ(ac, 1); 174 ASSERT_PTR_NE(av, NULL); 175 ASSERT_STRING_EQ(av[0], "leamas # gold"); 176 ASSERT_PTR_EQ(av[1], NULL); 177 RESET_ARGV(); 178 ASSERT_INT_EQ(argv_split("\"leamas\"#gold", &ac, &av, 1), 0); 179 ASSERT_INT_EQ(ac, 1); 180 ASSERT_PTR_NE(av, NULL); 181 ASSERT_STRING_EQ(av[0], "leamas#gold"); 182 ASSERT_PTR_EQ(av[1], NULL); 183 RESET_ARGV(); 184 TEST_DONE(); 185 186 /* XXX test char *argv_assemble(int argc, char **argv) */ 187 } 188