summaryrefslogtreecommitdiffstats
path: root/plugins/sudoers/regress/iolog_util/check_iolog_util.c
blob: d9c932d311e2925e9f2bc824f7e5d8e3b932089d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright (c) 2018 Todd C. Miller <Todd.Miller@sudo.ws>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <config.h>

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_STRING_H
# include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif /* HAVE_STRINGS_H */
#include <time.h>
#include <unistd.h>

#define SUDO_ERROR_WRAP 0

#include "sudo_compat.h"
#include "sudo_util.h"
#include "sudo_fatal.h"
#include "iolog.h"

__dso_public int main(int argc, char *argv[]);

static struct parse_delay_test {
    const char *input;
    const char *next_field;
    struct timespec expected_delay;
} parse_delay_tests[] = {
    { "10.99999999999 X", "X", { 10, 999999999 } },	/* clamp to nsec */
    { "10.999999999 X",   "X", { 10, 999999999 } },	/* nsec */
    { "10.999999 X",      "X", { 10, 999999000 } },	/* usec -> nsec */
    { "10.000999999 X",   "X", { 10, 999999 } },
    { "10.9 X",           "X", { 10, 900000000 } },
    { "10.0 X",           "X", { 10, 0 } }
};

/*
 * Test parse_delay()
 */
void
test_parse_delay(int *ntests, int *nerrors)
{
    unsigned int i;

    for (i = 0; i < nitems(parse_delay_tests); i++) {
	struct timespec delay;
	struct parse_delay_test *test = &parse_delay_tests[i];
	char *cp = parse_delay(test->input, &delay, ".");
	if (cp == NULL) {
	    sudo_warnx("%s:%u failed to parse delay: %s", __func__,
		i, test->input);
	    (*nerrors)++;
	    continue;
	}
	if (strcmp(cp, test->next_field) != 0) {
	    sudo_warnx("%s:%u next field (want \"%s\", got \"%s\"", __func__,
		i, test->next_field, cp);
	    (*nerrors)++;
	    continue;
	}
	if (delay.tv_sec != test->expected_delay.tv_sec) {
	    sudo_warnx("%s:%u wrong seconds (want %lld, got %lld)", __func__,
		i, (long long)test->expected_delay.tv_sec,
		(long long)delay.tv_sec);
	    (*nerrors)++;
	    continue;
	}
	if (delay.tv_nsec != test->expected_delay.tv_nsec) {
	    sudo_warnx("%s:%u wrong nanoseconds (want %ld, got %ld)", __func__,
		i, test->expected_delay.tv_nsec, delay.tv_nsec);
	    (*nerrors)++;
	    continue;
	}
    }
    (*ntests) += i;
}

static struct adjust_delay_test {
    struct timespec in_delay;
    struct timespec out_delay;
    struct timespec max_delay;
    double scale_factor;
} adjust_delay_tests[] = {
    { { 10,       300 }, { 10,       300 }, { 0, 0 }, 1.0 },
    { { 10,       300 }, {  5,       150 }, { 0, 0 }, 2.0 },
    { {  5,       300 }, {  2, 500000150 }, { 0, 0 }, 2.0 },
    { {  0,   1000000 }, {  0,    333333 }, { 0, 0 },   3 },
    { { 10,   1000000 }, {  3, 333666666 }, { 0, 0 },   3 },
    { {  5,       150 }, { 10,       300 }, { 0, 0 }, 0.5 },
    { {  5, 500000000 }, { 11,         0 }, { 0, 0 }, 0.5 },
    { {  5,       150 }, {  5,         0 }, { 5, 0 }, 0.5 }
};

/*
 * Test adjust_delay()
 */
void
test_adjust_delay(int *ntests, int *nerrors)
{
    unsigned int i;

    for (i = 0; i < nitems(adjust_delay_tests); i++) {
	struct adjust_delay_test *test = &adjust_delay_tests[i];

	adjust_delay(&test->in_delay, sudo_timespecisset(&test->max_delay) ?
	    &test->max_delay : NULL, test->scale_factor);
	if (!sudo_timespeccmp(&test->in_delay, &test->out_delay, ==)) {
	    sudo_warnx("%s:%u want {%lld, %ld}, got {%lld, %ld}", __func__, i,
		(long long)test->out_delay.tv_sec, test->out_delay.tv_nsec,
		(long long)test->in_delay.tv_sec, test->in_delay.tv_nsec);
	    (*nerrors)++;
	}
    }
    (*ntests) += i;
}

int
main(int argc, char *argv[])
{
    int tests = 0, errors = 0;

    initprogname(argc > 0 ? argv[0] : "check_iolog_util");

    test_parse_delay(&tests, &errors);

    test_adjust_delay(&tests, &errors);

    if (tests != 0) {
	printf("check_iolog_util: %d test%s run, %d errors, %d%% success rate\n",
	    tests, tests == 1 ? "" : "s", errors,
	    (tests - errors) * 100 / tests);
    }

    exit(errors);
}