File: test-xpath.c

package info (click to toggle)
augeas 1.2.0-0.2%2Bdeb8u2
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 12,356 kB
  • ctags: 7,389
  • sloc: ansic: 71,016; sh: 14,401; yacc: 528; makefile: 392; lex: 210; perl: 42; pascal: 27
file content (476 lines) | stat: -rw-r--r-- 12,170 bytes parent folder | download
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
 * test-xpath.c: check that XPath expressions yield the expected result
 *
 * Copyright (C) 2007-2011 Red Hat Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: David Lutterkort <dlutter@redhat.com>
 */

#include <config.h>

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

#include <augeas.h>
#include <internal.h>
#include <memory.h>

#include "cutest.h"

static const char *abs_top_srcdir;
static char *root;

#define KW_TEST "test"

struct entry {
    struct entry *next;
    char *path;
    char *value;
};

struct test {
    struct test *next;
    char *name;
    char *match;
    struct entry *entries;
};

#define die(msg)                                                    \
    do {                                                            \
        fprintf(stderr, "%d: Fatal error: %s\n", __LINE__, msg);    \
        exit(EXIT_FAILURE);                                         \
    } while(0)

static char *skipws(char *s) {
    while (isspace(*s)) s++;
    return s;
}

static char *findws(char *s) {
    while (*s && ! isspace(*s)) s++;
    return s;
}

static char *token(char *s, char **tok) {
    char *t = skipws(s);
    s = findws(t);
    *tok = strndup(t, s - t);
    return s;
}

static char *token_to_eol(char *s, char **tok) {
    char *t = skipws(s);
    while (*s && *s != '\n') s++;
    *tok = strndup(t, s - t);
    return s;
}

static char *findpath(char *s, char **p) {
    char *t = skipws(s);

    while (*s && *s != '=') s++;
    if (s > t) {
        s -= 1;
        while (*s && isspace(*s)) s -= 1;
        s += 1;
    }
    *p = strndup(t, s - t);
    return s;
}

static struct test *read_tests(void) {
    char *fname;
    FILE *fp;
    char line[BUFSIZ];
    struct test *result = NULL, *t = NULL;
    int lc = 0;

    if (asprintf(&fname, "%s/tests/xpath.tests", abs_top_srcdir) < 0)
        die("asprintf fname");

    if ((fp = fopen(fname, "r")) == NULL)
        die("fopen xpath.tests");

    while (fgets(line, BUFSIZ, fp) != NULL) {
        lc += 1;
        char *s = skipws(line);
        if (*s == '#' || *s == '\0')
            continue;
        if (STREQLEN(s, KW_TEST, strlen(KW_TEST))) {
            if (ALLOC(t) < 0)
                die("out of memory");
            list_append(result, t);
            s = token(s + strlen(KW_TEST), &(t->name));
            s = token_to_eol(s, &(t->match));
        } else {
            struct entry *e = NULL;
            if (ALLOC(e) < 0)
                die("out of memory");
            list_append(t->entries, e);
            s = findpath(s, &(e->path));
            s = skipws(s);
            if (*s) {
                if (*s != '=') {
                    fprintf(stderr,
                     "line %d: either list only a path or path = value\n", lc);
                    die("xpath.tests has incorrect format");
                }
                s = token_to_eol(s + 1, &(e->value));
            }
        }
        s = skipws(s);
        if (*s != '\0') {
            fprintf(stderr, "line %d: junk at end of line\n", lc);
            die("xpath.tests has incorrect format");
        }
    }
    return result;
}

static void print_pv(const char *path, const char *value) {
    if (value)
        printf("    %s = %s\n", path, value);
    else
        printf("    %s\n", path);
}

static int has_match(const char *path, char **matches, int nmatches) {
    int found = 0;
    for (int i=0; i < nmatches; i++) {
        if (matches[i] != NULL && STREQ(path, matches[i])) {
            found = 1;
            break;
        }
    }
    return found;
}

static int run_one_test(struct augeas *aug, struct test *t) {
    int nexp = 0, nact;
    char **matches;
    int result = 0;

    printf("%-30s ... ", t->name);
    list_for_each(e, t->entries)
        nexp++;
    nact = aug_match(aug, t->match, &matches);
    if (nact != nexp) {
        result = -1;
    } else {
        struct entry *e;
        const char *val;

        for (e = t->entries; e != NULL; e = e->next) {
            if (! has_match(e->path, matches, nact))
                result = -1;
            if (! streqv(e->value, "...")) {
                aug_get(aug, e->path, &val);
                if (!streqv(e->value, val))
                    result = -1;
            }
        }
    }
    if (result == 0) {
        printf("PASS\n");
    } else {
        printf("FAIL\n");

        printf("  Match: %s\n", t->match);
        printf("  Expected: %d entries\n", nexp);
        list_for_each(e, t->entries) {
            print_pv(e->path, e->value);
        }
        if (nact < 0) {
            printf("  Actual: aug_match failed\n");
            } else {
            printf("  Actual: %d entries\n", nact);
        }
        for (int i=0; i < nact; i++) {
            const char *val;
            aug_get(aug, matches[i], &val);
            print_pv(matches[i], val);
        }
    }
    return result;
}

static int test_rm_var(struct augeas *aug) {
    int r;

    printf("%-30s ... ", "rm_var");
    r = aug_defvar(aug, "h", "/files/etc/hosts/2/ipaddr");
    if (r < 0)
        die("aug_defvar failed");

    r = aug_match(aug, "$h", NULL);
    if (r != 1) {
        fprintf(stderr, "expected 1 match, got %d\n", r);
        goto fail;
    }

    r = aug_rm(aug, "/files/etc/hosts/2");
    if (r != 4) {
        fprintf(stderr, "expected 4 nodes removed, got %d\n", r);
        goto fail;
    }

    r = aug_match(aug, "$h", NULL);
    if (r != 0) {
        fprintf(stderr, "expected no match, got %d\n", r);
        goto fail;
    }
    printf("PASS\n");
    return 0;
 fail:
    printf("FAIL\n");
    return -1;
}

static int test_defvar_nonexistent(struct augeas *aug) {
    int r;

    printf("%-30s ... ", "defvar_nonexistent");
    r = aug_defvar(aug, "x", "/foo/bar");
    if (r < 0)
        die("aug_defvar failed");

    r = aug_set(aug, "$x", "baz");
    if (r != -1)
        goto fail;
    printf("PASS\n");
    return 0;
 fail:
    printf("FAIL\n");
    return -1;
}

static int test_defnode_nonexistent(struct augeas *aug) {
    int r, created;

    printf("%-30s ... ", "defnode_nonexistent");
    r = aug_defnode(aug, "x", "/defnode/bar[0 = 1]", "foo", &created);
    if (r != 1)
        die("aug_defnode failed");
    if (created != 1) {
        fprintf(stderr, "defnode did not create a node\n");
        goto fail;
    }
    r = aug_match(aug, "$x", NULL);
    if (r != 1) {
        fprintf(stderr, "$x must have exactly one entry, but has %d\n", r);
        goto fail;
    }

    r = aug_defnode(aug, "x", "/defnode/bar", NULL, &created);
    if (r != 1)
        die("aug_defnode failed");
    if (created != 0) {
        fprintf(stderr, "defnode created node again\n");
        goto fail;
    }

    // FIXME: get values and compare them, too

    r = aug_set(aug, "$x", "baz");
    if (r != 0)
        goto fail;

    r = aug_match(aug, "$x", NULL);
    if (r != 1)
        goto fail;

    printf("PASS\n");
    return 0;
 fail:
    printf("FAIL\n");
    return -1;
}

static int test_invalid_regexp(struct augeas *aug) {
    int r;

    printf("%-30s ... ", "invalid_regexp");
    r = aug_match(aug, "/files/*[ * =~ regexp('.*[aeiou')]", NULL);
    if (r >= 0)
        goto fail;

    printf("PASS\n");
    return 0;
 fail:
    printf("FAIL\n");
    return -1;
}

static int test_wrong_regexp_flag(struct augeas *aug) {
    int r;

    printf("%-30s ... ", "wrong_regexp_flag");
    r = aug_match(aug, "/files/*[ * =~ regexp('abc', 'o')]", NULL);
    if (r >= 0)
        goto fail;

    printf("PASS\n");
    return 0;
 fail:
    printf("FAIL\n");
    return -1;
}

static int test_trailing_ws_in_name(struct augeas *aug) {
    int r;

    printf("%-30s ... ", "trailing_ws_in_name");

    /* We used to incorrectly lop escaped whitespace off the end of a
     * name. Make sure that we really create a tree node with label 'x '
     * with the below set, and look for it in a number of ways to ensure we
     * are not lopping off trailing whitespace. */
    r = aug_set(aug, "/ws\\ ", "1");
    if (r < 0) {
        fprintf(stderr, "failed to set '/ws ': %d\n", r);
        goto fail;
    }
    /* We did not create a node with label 'ws' */
    r = aug_get(aug, "/ws", NULL);
    if (r != 0) {
        fprintf(stderr, "created '/ws' instead: %d\n", r);
        goto fail;
    }

    /* We did not create a node with label 'ws\t' (this also checks that we
     * don't create something like 'ws\\' by dropping the last whitespace
     * character. */
    r = aug_get(aug, "/ws\\\t", NULL);
    if (r != 0) {
        fprintf(stderr, "found '/ws\\t': %d\n", r);
        goto fail;
    }

    /* But we did create 'ws ' */
    r = aug_get(aug, "/ws\\ ", NULL);
    if (r != 1) {
        fprintf(stderr, "could not find '/ws ': %d\n", r);
        goto fail;
    }

    /* If the whitespace is preceded by an even number of '\\' chars,
     * whitespace must be stripped */
    r = aug_set(aug, "/nows\\\\ ", "1");
    if (r < 0) {
        fprintf(stderr, "set of '/nows' failed: %d\n", r);
        goto fail;
    }
    r = aug_get(aug, "/nows\\\\", NULL);
    if (r != 1) {
        fprintf(stderr, "could not get '/nows\\'\n");
        goto fail;
    }
    printf("PASS\n");
    return 0;
 fail:
    printf("FAIL\n");
    return -1;
}

static int run_tests(struct test *tests, int argc, char **argv) {
    char *lensdir;
    struct augeas *aug = NULL;
    int r, result = EXIT_SUCCESS;

    if (asprintf(&lensdir, "%s/lenses", abs_top_srcdir) < 0)
        die("asprintf lensdir failed");

    aug = aug_init(root, lensdir, AUG_NO_STDINC|AUG_SAVE_NEWFILE);
    if (aug == NULL)
        die("aug_init");
    r = aug_defvar(aug, "hosts", "/files/etc/hosts/*");
    if (r != 6)
        die("aug_defvar $hosts");
    r = aug_defvar(aug, "localhost", "'127.0.0.1'");
    if (r != 0)
        die("aug_defvar $localhost");
    r = aug_defvar(aug, "php", "/files/etc/php.ini");
    if (r != 1)
        die("aug_defvar $php");

    list_for_each(t, tests) {
        if (! should_run(t->name, argc, argv))
            continue;
        if (run_one_test(aug, t) < 0)
            result = EXIT_FAILURE;
    }

    if (argc == 0) {
        if (test_rm_var(aug) < 0)
            result = EXIT_FAILURE;

        if (test_defvar_nonexistent(aug) < 0)
            result = EXIT_FAILURE;

        if (test_defnode_nonexistent(aug) < 0)
            result = EXIT_FAILURE;

        if (test_invalid_regexp(aug) < 0)
            result = EXIT_FAILURE;

        if (test_wrong_regexp_flag(aug) < 0)
            result = EXIT_FAILURE;

        if (test_trailing_ws_in_name(aug) < 0)
            result = EXIT_FAILURE;
    }
    aug_close(aug);

    return result;
}

int main(int argc, char **argv) {
    struct test *tests;

    abs_top_srcdir = getenv("abs_top_srcdir");
    if (abs_top_srcdir == NULL)
        die("env var abs_top_srcdir must be set");

    if (asprintf(&root, "%s/tests/root", abs_top_srcdir) < 0) {
        die("failed to set root");
    }

    tests = read_tests();
    return run_tests(tests, argc - 1, argv + 1);
    /*
    list_for_each(t, tests) {
        printf("Test %s\n", t->name);
        printf("match %s\n", t->match);
        list_for_each(e, t->entries) {
            if (e->value)
                printf("    %s = %s\n", e->path, e->value);
            else
                printf("    %s\n", e->path);
        }
    }
    */
}

/*
 * Local variables:
 *  indent-tabs-mode: nil
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  tab-width: 4
 * End:
 */