2
* test-run.c: test the aug_srun API function
4
* Copyright (C) 2009 Red Hat Inc.
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2.1 of the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
* Author: David Lutterkort <lutter@redhat.com>
37
static const char *abs_top_srcdir;
39
#define KW_TEST "test"
40
#define KW_PRINTS "prints"
41
#define KW_SOMETHING "something"
43
/* This array needs to be kept in sync with aug_errcode_t, and
44
* the entries need to be in the same order as in that enum; used
47
static const char *const errtokens[] = {
48
"NOERROR", "ENOMEM", "EINTERNAL", "EPATHX", "ENOMATCH",
49
"EMMATCH", "ESYNTAX", "ENOLENS", "EMXFM", "ENOSPAN",
65
fprintf(stderr, "%d: Fatal error: %s\n", __LINE__, msg); \
69
static char *skipws(char *s) {
70
while (isspace(*s)) s++;
74
static char *findws(char *s) {
75
while (*s && ! isspace(*s)) s++;
79
static char *token(char *s, char **tok) {
82
*tok = strndup(t, s - t);
86
static char *inttok(char *s, int *tok) {
89
*tok = strtol(t, NULL, 10);
93
static char *errtok(char *s, int *err) {
100
for (*err = 0; *err < ARRAY_CARDINALITY(errtokens); *err += 1) {
101
const char *e = errtokens[*err];
102
if (strlen(e) == s - t && STREQLEN(e, t, s - t))
105
fprintf(stderr, "errtok: '%s'\n", t);
106
die("unknown error code");
109
static bool looking_at(const char *s, const char *kw) {
110
return STREQLEN(s, kw, strlen(kw));
113
static struct test *read_tests(void) {
117
struct test *result = NULL, *t = NULL;
119
bool append_cmd = true;
121
if (asprintf(&fname, "%s/tests/run.tests", abs_top_srcdir) < 0)
122
die("asprintf fname");
124
if ((fp = fopen(fname, "r")) == NULL)
125
die("fopen run.tests");
127
while (fgets(line, BUFSIZ, fp) != NULL) {
129
char *s = skipws(line);
130
if (*s == '#' || *s == '\0')
134
if (looking_at(s, KW_TEST)) {
137
list_append(result, t);
139
s = token(s + strlen(KW_TEST), &(t->name));
140
s = inttok(s, &t->result);
141
s = errtok(s, &t->errcode);
142
} else if (looking_at(s, KW_PRINTS)) {
143
s = skipws(s + strlen(KW_PRINTS));
144
t->out_present = looking_at(s, KW_SOMETHING);
147
char **buf = append_cmd ? &(t->cmd) : &(t->out);
153
if (REALLOC_N(*buf, strlen(*buf) + strlen(s) + 1) < 0)
159
t->out_present = true;
164
#define fail(cond, msg ...) \
167
fprintf(stdout, ## msg); \
172
static int run_one_test(struct test *test) {
174
struct augeas *aug = NULL;
178
aug = aug_init("/dev/null", NULL, AUG_NO_STDINC|AUG_NO_LOAD);
179
fail(aug == NULL, "aug_init");
180
fail(aug_error(aug) != AUG_NOERROR, "aug_init: errcode was %d",
183
printf("%-30s ... ", test->name);
185
r = init_memstream(&ms);
186
fail(r < 0, "init_memstream");
188
r = aug_srun(aug, ms.stream, test->cmd);
189
fail(r != test->result, "return value: expected %d, actual %d",
191
fail(aug_error(aug) != test->errcode, "errcode: expected %s, actual %s",
192
errtokens[test->errcode], errtokens[aug_error(aug)]);
194
r = close_memstream(&ms);
195
fail(r < 0, "close_memstream");
196
fail(ms.buf == NULL, "close_memstream left buf NULL");
198
if (test->out != NULL) {
199
fail(STRNEQ(ms.buf, test->out), "output: expected '%s', actual '%s'",
201
} else if (test->out_present) {
202
fail(strlen(ms.buf) == 0,
203
"output: expected some output");
205
fail(strlen(ms.buf) > 0,
206
"output: expected nothing, actual '%s'", ms.buf);
219
static int run_tests(struct test *tests) {
220
int result = EXIT_SUCCESS;
222
list_for_each(t, tests) {
223
if (run_one_test(t) < 0)
224
result = EXIT_FAILURE;
232
abs_top_srcdir = getenv("abs_top_srcdir");
233
if (abs_top_srcdir == NULL)
234
die("env var abs_top_srcdir must be set");
236
tests = read_tests();
237
return run_tests(tests);
242
* indent-tabs-mode: nil