Annotation of embedaddon/sudo/common/regress/atofoo/atofoo_test.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (c) 2014 Todd C. Miller <Todd.Miller@courtesan.com>
        !             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 */
        !            29: #ifdef HAVE_STDBOOL_H
        !            30: # include <stdbool.h>
        !            31: #else
        !            32: # include "compat/stdbool.h"
        !            33: #endif
        !            34: 
        !            35: #include "missing.h"
        !            36: #include "sudo_util.h"
        !            37: #include "fatal.h"
        !            38: 
        !            39: __dso_public int main(int argc, char *argv[]);
        !            40: 
        !            41: /* atobool() tests */
        !            42: static struct atobool_data {
        !            43:     const char *bool_str;
        !            44:     int value;
        !            45: } atobool_data[] = {
        !            46:     { "true", true },
        !            47:     { "false", false },
        !            48:     { "TrUe", true },
        !            49:     { "fAlSe", false },
        !            50:     { "1", true },
        !            51:     { "0", false },
        !            52:     { "on", true },
        !            53:     { "off", false },
        !            54:     { "yes", true },
        !            55:     { "no", false },
        !            56:     { "nope", -1 },
        !            57:     { "10", -1 },
        !            58:     { "one", -1 },
        !            59:     { "zero", -1 },
        !            60:     { NULL, 0 }
        !            61: };
        !            62: 
        !            63: static int
        !            64: test_atobool(int *ntests)
        !            65: {
        !            66:     struct atobool_data *d;
        !            67:     int errors = 0;
        !            68:     int value;
        !            69: 
        !            70:     for (d = atobool_data; d->bool_str != NULL; d++) {
        !            71:        (*ntests)++;
        !            72:        value = atobool(d->bool_str);
        !            73:        if (value != d->value) {
        !            74:            warningx_nodebug("FAIL: %s != %d", d->bool_str, d->value);
        !            75:            errors++;
        !            76:        }
        !            77:     }
        !            78: 
        !            79:     return errors;
        !            80: }
        !            81: 
        !            82: /* atoid() tests */
        !            83: static struct atoid_data {
        !            84:     const char *idstr;
        !            85:     id_t id;
        !            86:     const char *sep;
        !            87:     const char *ep;
        !            88: } atoid_data[] = {
        !            89:     { "0,1", 0, ",", "," },
        !            90:     { "10", 10, NULL, NULL },
        !            91:     { "-2", -2, NULL, NULL },
        !            92:     { "-2", 4294967294U, NULL, NULL },
        !            93:     { "4294967294", 4294967294U, NULL, NULL },
        !            94:     { NULL, 0, NULL, NULL }
        !            95: };
        !            96: 
        !            97: static int
        !            98: test_atoid(int *ntests)
        !            99: {
        !           100:     struct atoid_data *d;
        !           101:     const char *errstr;
        !           102:     char *ep;
        !           103:     int errors = 0;
        !           104:     id_t value;
        !           105: 
        !           106:     for (d = atoid_data; d->idstr != NULL; d++) {
        !           107:        (*ntests)++;
        !           108:        errstr = "some error";
        !           109:        value = atoid(d->idstr, d->sep, &ep, &errstr);
        !           110:        if (errstr != NULL) {
        !           111:            if (d->id != (id_t)-1) {
        !           112:                warningx_nodebug("FAIL: %s: %s", d->idstr, errstr);
        !           113:                errors++;
        !           114:            }
        !           115:        } else if (value != d->id) {
        !           116:            warningx_nodebug("FAIL: %s != %u", d->idstr, (unsigned int)d->id);
        !           117:            errors++;
        !           118:        } else if (d->ep != NULL && ep[0] != d->ep[0]) {
        !           119:            warningx_nodebug("FAIL: ep[0] %d != %d", (int)(unsigned char)ep[0],
        !           120:                (int)(unsigned char)d->ep[0]);
        !           121:            errors++;
        !           122:        }
        !           123:     }
        !           124: 
        !           125:     return errors;
        !           126: }
        !           127: 
        !           128: /* atomode() tests */
        !           129: static struct atomode_data {
        !           130:     const char *mode_str;
        !           131:     mode_t mode;
        !           132: } atomode_data[] = {
        !           133:     { "755", 0755 },
        !           134:     { "007", 007 },
        !           135:     { "7", 7 },
        !           136:     { "8", -1 },
        !           137:     { NULL, 0 }
        !           138: };
        !           139: 
        !           140: static int
        !           141: test_atomode(int *ntests)
        !           142: {
        !           143:     struct atomode_data *d;
        !           144:     const char *errstr;
        !           145:     int errors = 0;
        !           146:     mode_t mode;
        !           147: 
        !           148:     for (d = atomode_data; d->mode_str != NULL; d++) {
        !           149:        (*ntests)++;
        !           150:        errstr = "some error";
        !           151:        mode = atomode(d->mode_str, &errstr);
        !           152:        if (errstr != NULL) {
        !           153:            if (d->mode != (mode_t)-1) {
        !           154:                warningx_nodebug("FAIL: %s: %s", d->mode_str, errstr);
        !           155:                errors++;
        !           156:            }
        !           157:        } else if (mode != d->mode) {
        !           158:            warningx_nodebug("FAIL: %s != 0%o", d->mode_str, d->mode);
        !           159:            errors++;
        !           160:        }
        !           161:     }
        !           162: 
        !           163:     return errors;
        !           164: }
        !           165: 
        !           166: /*
        !           167:  * Simple tests for atobool(), atoid(), atomode().
        !           168:  */
        !           169: int
        !           170: main(int argc, char *argv[])
        !           171: {
        !           172:     int errors = 0;
        !           173:     int ntests = 0;
        !           174: 
        !           175:     initprogname(argc > 0 ? argv[0] : "atofoo");
        !           176: 
        !           177:     errors += test_atobool(&ntests);
        !           178:     errors += test_atoid(&ntests);
        !           179:     errors += test_atomode(&ntests);
        !           180: 
        !           181:     printf("%s: %d tests run, %d errors, %d%% success rate\n", getprogname(),
        !           182:        ntests, errors, (ntests - errors) * 100 / ntests);
        !           183: 
        !           184:     exit(errors);
        !           185: }

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