Bump to 1.14.1
[platform/upstream/augeas.git] / tests / test-xpath.c
index 42a4b91..8298647 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * test-xpath.c: check that XPath expressions yield the expected result
  *
- * Copyright (C) 2007-2011 Red Hat Inc.
+ * Copyright (C) 2007-2016 David Lutterkort
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -30,6 +30,8 @@
 #include <internal.h>
 #include <memory.h>
 
+#include "cutest.h"
+
 static const char *abs_top_srcdir;
 static char *root;
 
@@ -91,6 +93,24 @@ static char *findpath(char *s, char **p) {
     return s;
 }
 
+static void free_tests(struct test *test) {
+    while (test != NULL) {
+        struct test *del = test;
+        test = test->next;
+        struct entry *entry = del->entries;
+        while (entry != NULL) {
+            struct entry *e = entry;
+            entry = entry->next;
+            free(e->path);
+            free(e->value);
+            free(e);
+        }
+        free(del->name);
+        free(del->match);
+        free(del);
+    }
+}
+
 static struct test *read_tests(void) {
     char *fname;
     FILE *fp;
@@ -137,6 +157,8 @@ static struct test *read_tests(void) {
             die("xpath.tests has incorrect format");
         }
     }
+    fclose(fp);
+    free(fname);
     return result;
 }
 
@@ -204,6 +226,10 @@ static int run_one_test(struct augeas *aug, struct test *t) {
             print_pv(matches[i], val);
         }
     }
+    for (int i=0; i < nact; i++) {
+        free(matches[i]);
+    }
+    free(matches);
     return result;
 }
 
@@ -314,6 +340,77 @@ static int test_invalid_regexp(struct augeas *aug) {
     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;
@@ -336,11 +433,7 @@ static int run_tests(struct test *tests, int argc, char **argv) {
         die("aug_defvar $php");
 
     list_for_each(t, tests) {
-        int skip = (argc > 0);
-        for (int i=0; i < argc; i++)
-            if (STREQ(argv[i], t->name))
-                skip = 0;
-        if (skip)
+        if (! should_run(t->name, argc, argv))
             continue;
         if (run_one_test(aug, t) < 0)
             result = EXIT_FAILURE;
@@ -358,8 +451,15 @@ static int run_tests(struct test *tests, int argc, char **argv) {
 
         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);
+    free(lensdir);
 
     return result;
 }
@@ -376,7 +476,7 @@ int main(int argc, char **argv) {
     }
 
     tests = read_tests();
-    return run_tests(tests, argc - 1, argv + 1);
+    int result = run_tests(tests, argc - 1, argv + 1);
     /*
     list_for_each(t, tests) {
         printf("Test %s\n", t->name);
@@ -389,6 +489,8 @@ int main(int argc, char **argv) {
         }
     }
     */
+    free_tests(tests);
+    return result;
 }
 
 /*