Annotation of embedaddon/sudo/plugins/sudoers/regress/parser/check_fill.c, revision 1.1.1.3

1.1       misho       1: /*
1.1.1.3 ! misho       2:  * Copyright (c) 2011-2013 Todd C. Miller <Todd.Miller@courtesan.com>
1.1       misho       3:  *
                      4:  * Permission to use, copy, modify, and distribute this software for any
                      5:  * purpose with or without fee is hereby granted, provided that the above
                      6:  * copyright notice and this permission notice appear in all copies.
                      7:  *
                      8:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                      9:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     10:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     11:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     12:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     13:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     14:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     15:  */
                     16: 
                     17: #include <config.h>
                     18: 
                     19: #include <sys/types.h>
                     20: #include <stdio.h>
                     21: #ifdef STDC_HEADERS
                     22: # include <stdlib.h>
                     23: # include <stddef.h>
                     24: #else
                     25: # ifdef HAVE_STDLIB_H
                     26: #  include <stdlib.h>
                     27: # endif
                     28: #endif /* STDC_HEADERS */
1.1.1.2   misho      29: #ifdef HAVE_STDBOOL_H
                     30: # include <stdbool.h>
                     31: #else
                     32: # include "compat/stdbool.h"
                     33: #endif /* HAVE_STDBOOL_H */
1.1       misho      34: #ifdef HAVE_STRING_H
                     35: # include <string.h>
                     36: #endif /* HAVE_STRING_H */
                     37: #ifdef HAVE_STRINGS_H
                     38: # include <strings.h>
                     39: #endif /* HAVE_STRINGS_H */
                     40: #include <grp.h>
                     41: #include <pwd.h>
                     42: 
1.1.1.2   misho      43: #define SUDO_ERROR_WRAP 0
                     44: 
1.1.1.3 ! misho      45: #include "missing.h"
1.1       misho      46: #include "list.h"
                     47: #include "parse.h"
                     48: #include "toke.h"
1.1.1.2   misho      49: #include "sudo_plugin.h"
                     50: #include <gram.h>
1.1       misho      51: 
1.1.1.3 ! misho      52: __dso_public int main(int argc, char *argv[]);
        !            53: 
1.1       misho      54: /*
                     55:  * TODO: test realloc
                     56:  */
                     57: 
1.1.1.3 ! misho      58: YYSTYPE sudoerslval;
1.1       misho      59: 
                     60: struct fill_test {
                     61:     const char *input;
                     62:     const char *output;
                     63:     int len;
                     64:     int addspace;
                     65: };
                     66: 
                     67: /*
                     68:  * In "normal" fill, anything can be escaped and hex chars are expanded.
                     69:  */
                     70: static struct fill_test txt_data[] = {
                     71:     { "Embedded\\x20Space", "Embedded Space", 0 },
                     72:     { "\\x20Leading", " Leading", 0 },
                     73:     { "Trailing\\x20", "Trailing ", 0 },
                     74:     { "Multiple\\x20\\x20Spaces", "Multiple  Spaces", 0 },
                     75:     { "Hexparse\\x200Check", "Hexparse 0Check", 0 },
                     76:     { "Escaped\\\\Escape", "Escaped\\Escape", 0 },
                     77:     { "LongGroupName", "LongGrou", 8 }
                     78: };
                     79: 
                     80: /*
                     81:  * The only escaped chars in a command should be [,:= \t#]
                     82:  * The rest are done by glob() or fnmatch().
                     83:  */
                     84: static struct fill_test cmd_data[] = {
                     85:     { "foo\\,bar", "foo,bar", 0 },
                     86:     { "this\\:that", "this:that", 0 },
                     87:     { "foo\\=bar", "foo=bar", 0 },
                     88:     { "tab\\\tstop", "tab\tstop", 0 },
                     89:     { "not a \\#comment", "not a #comment", 0 }
                     90: };
                     91: 
                     92: /*
                     93:  * No escaped characters in command line args.
                     94:  * Arguments get appended.
                     95:  */
                     96: static struct fill_test args_data[] = {
                     97:     { "/", "/", 0, 0 },
                     98:     { "-type", "/ -type", 0, 1 },
                     99:     { "f", "/ -type f", 0, 1 },
                    100:     { "-exec", "/ -type f -exec", 0, 1 },
                    101:     { "ls", "/ -type f -exec ls", 0, 1 },
                    102:     { "{}", "/ -type f -exec ls {}", 0, 1 }
                    103: };
                    104: 
                    105: static int
                    106: check_fill(const char *input, int len, int addspace, const char *expect, char **resultp)
                    107: {
                    108:     if (!fill(input, len))
                    109:        return -1;
1.1.1.3 ! misho     110:     *resultp = sudoerslval.string;
        !           111:     return !strcmp(sudoerslval.string, expect);
1.1       misho     112: }
                    113: 
                    114: static int
                    115: check_fill_cmnd(const char *input, int len, int addspace, const char *expect, char **resultp)
                    116: {
                    117:     if (!fill_cmnd(input, len))
                    118:        return -1;
1.1.1.3 ! misho     119:     *resultp = sudoerslval.command.cmnd;
        !           120:     return !strcmp(sudoerslval.command.cmnd, expect);
1.1       misho     121: }
                    122: 
                    123: static int
                    124: check_fill_args(const char *input, int len, int addspace, const char *expect, char **resultp)
                    125: {
                    126:     if (!fill_args(input, len, addspace))
                    127:        return -1;
1.1.1.3 ! misho     128:     *resultp = sudoerslval.command.args;
        !           129:     return !strcmp(sudoerslval.command.args, expect);
1.1       misho     130: }
                    131: 
                    132: static int
                    133: do_tests(int (*checker)(const char *, int, int, const char *, char **),
                    134:     struct fill_test *data, size_t ntests)
                    135: {
                    136:     int i, len;
                    137:     int errors = 0;
                    138:     char *result;
                    139: 
                    140:     for (i = 0; i < ntests; i++) {
                    141:        if (data[i].len == 0)
                    142:            len = strlen(data[i].input);
                    143:        else
                    144:            len = data[i].len;
                    145: 
                    146:        switch ((*checker)(data[i].input, len, data[i].addspace, data[i].output, &result)) {
                    147:        case 0:
                    148:            /* no match */
                    149:            fprintf(stderr, "Failed parsing %.*s: expected [%s], got [%s]\n",
                    150:                (int)data[i].len, data[i].input, data[i].output, result);
                    151:            errors++;
                    152:            break;
                    153:        case 1:
                    154:            /* match */
                    155:            break;
                    156:        default:
                    157:            /* error */
                    158:            fprintf(stderr, "Failed parsing %.*s: fill function failure\n",
                    159:                (int)data[i].len, data[i].input);
                    160:            errors++;
                    161:            break;
                    162:        }
                    163:     }
                    164: 
                    165:     return errors;
                    166: }
                    167: 
                    168: int
                    169: main(int argc, char *argv[])
                    170: {
                    171:     int ntests, errors = 0;
                    172: 
                    173:     errors += do_tests(check_fill, txt_data, sizeof(txt_data) / sizeof(txt_data[0]));
                    174:     errors += do_tests(check_fill_cmnd, cmd_data, sizeof(cmd_data) / sizeof(cmd_data[0]));
                    175:     errors += do_tests(check_fill_args, args_data, sizeof(args_data) / sizeof(args_data[0]));
                    176: 
                    177:     ntests = sizeof(txt_data) / sizeof(txt_data[0]) +
                    178:        sizeof(cmd_data) / sizeof(cmd_data[0]) +
                    179:        sizeof(args_data) / sizeof(args_data[0]);
                    180:     printf("check_fill: %d tests run, %d errors, %d%% success rate\n",
                    181:        ntests, errors, (ntests - errors) * 100 / ntests);
                    182: 
                    183:     exit(errors);
                    184: }
                    185: 
                    186: /* STUB */
                    187: void
1.1.1.3 ! misho     188: sudoerserror(const char *s)
1.1       misho     189: {
                    190:     return;
                    191: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>