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