2 * test-run.c: test the aug_srun API function
4 * Copyright (C) 2009-2011 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 char *eos = s + strlen(s) - 2;
135 if (eos >= s && *eos == ':') {
140 if (looking_at(s, KW_TEST)) {
143 list_append(result, t);
145 s = token(s + strlen(KW_TEST), &(t->name));
146 s = inttok(s, &t->result);
147 s = errtok(s, &t->errcode);
148 } else if (looking_at(s, KW_PRINTS)) {
149 s = skipws(s + strlen(KW_PRINTS));
150 t->out_present = looking_at(s, KW_SOMETHING);
153 char **buf = append_cmd ? &(t->cmd) : &(t->out);
159 if (REALLOC_N(*buf, strlen(*buf) + strlen(s) + 1) < 0)
165 t->out_present = true;
170 #define fail(cond, msg ...) \
173 fprintf(stdout, ## msg); \
178 static int run_one_test(struct test *test) {
180 struct augeas *aug = NULL;
184 aug = aug_init("/dev/null", NULL, AUG_NO_STDINC|AUG_NO_LOAD);
185 fail(aug == NULL, "aug_init");
186 fail(aug_error(aug) != AUG_NOERROR, "aug_init: errcode was %d",
189 printf("%-30s ... ", test->name);
191 r = init_memstream(&ms);
192 fail(r < 0, "init_memstream");
194 r = aug_srun(aug, ms.stream, test->cmd);
195 fail(r != test->result, "return value: expected %d, actual %d",
197 fail(aug_error(aug) != test->errcode, "errcode: expected %s, actual %s",
198 errtokens[test->errcode], errtokens[aug_error(aug)]);
200 r = close_memstream(&ms);
201 fail(r < 0, "close_memstream");
202 fail(ms.buf == NULL, "close_memstream left buf NULL");
204 if (test->out != NULL) {
205 fail(STRNEQ(ms.buf, test->out), "output: expected '%s', actual '%s'",
207 } else if (test->out_present) {
208 fail(strlen(ms.buf) == 0,
209 "output: expected some output");
211 fail(strlen(ms.buf) > 0,
212 "output: expected nothing, actual '%s'", ms.buf);
225 static int run_tests(struct test *tests) {
226 int result = EXIT_SUCCESS;
228 list_for_each(t, tests) {
229 if (run_one_test(t) < 0)
230 result = EXIT_FAILURE;
238 abs_top_srcdir = getenv("abs_top_srcdir");
239 if (abs_top_srcdir == NULL)
240 die("env var abs_top_srcdir must be set");
242 tests = read_tests();
243 return run_tests(tests);
248 * indent-tabs-mode: nil