File: testcatalog.c

package info (click to toggle)
libxml2 2.15.2%2Bdfsg-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 31,036 kB
  • sloc: xml: 305,167; ansic: 138,598; python: 6,692; javascript: 5,897; sh: 4,736; makefile: 1,308
file content (96 lines) | stat: -rw-r--r-- 2,216 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
/*
 * testcatalog.c: C program to run libxml2 catalog.c unit tests
 *
 * To compile on Unixes:
 * cc -o testcatalog `xml2-config --cflags` testcatalog.c `xml2-config --libs` -lpthread
 *
 * See Copyright for the status of this software.
 *
 * Author: Daniel Garcia <dani@danigm.net>
 */


#include "libxml.h"
#include <stdio.h>

#ifdef LIBXML_CATALOG_ENABLED
#include <libxml/catalog.h>

/* Test catalog resolve uri with recursive catalog */
static int
testRecursiveDelegateUri(void) {
    int ret = 0;
    const char *cat = "test/catalogs/catalog-recursive.xml";
    const char *entity = "/foo.ent";
    xmlChar *resolved = NULL;

    xmlInitParser();
    xmlLoadCatalog(cat);

    /* This should trigger recursive error */
    resolved = xmlCatalogResolveURI(BAD_CAST entity);
    if (resolved != NULL) {
        fprintf(stderr, "CATALOG-FAILURE: Catalog %s entity should fail to resolve\n", entity);
        ret = 1;
    }
    xmlCatalogCleanup();

    return ret;
}

/* Test parsing repeated NextCatalog */
static int
testRepeatedNextCatalog(void) {
    int ret = 0;
    int i = 0;
    const char *cat = "test/catalogs/repeated-next-catalog.xml";
    const char *entity = "/foo.ent";
    xmlDocPtr doc = NULL;
    xmlNodePtr node = NULL;

    xmlInitParser();

    xmlLoadCatalog(cat);
    /* To force the complete recursive load */
    xmlCatalogResolveURI(BAD_CAST entity);
    /**
     * Ensure that the doc doesn't contain the same nextCatalog
     */
    doc = xmlCatalogDumpDoc();
    xmlCatalogCleanup();

    if (doc == NULL) {
        fprintf(stderr, "CATALOG-FAILURE: Failed to dump the catalog\n");
        return 1;
    }

    /* Just the root "catalog" node with a series of nextCatalog */
    node = xmlDocGetRootElement(doc);
    node = node->children;
    for (i=0; node != NULL; node=node->next, i++) {}
    if (i > 1) {
        fprintf(stderr, "CATALOG-FAILURE: Found %d nextCatalog entries and should be 1\n", i);
        ret = 1;
    }

    xmlFreeDoc(doc);

    return ret;
}

int
main(void) {
    int err = 0;

    err |= testRecursiveDelegateUri();
    err |= testRepeatedNextCatalog();

    return err;
}
#else
/* No catalog, so everything okay */
int
main(void) {
    return 0;
}
#endif