Annotation of XML/testOasis.c, revision 1.6

1.3       veillard    1: #include "config.h"
1.1       daniel      2: #include <stdio.h>
                      3: #include <string.h>
                      4: #include <stdio.h>
                      5: #include <stdarg.h>
1.3       veillard    6: #ifdef HAVE_SYS_TYPES_H
                      7: #include <sys/types.h>
                      8: #endif
                      9: #ifdef HAVE_FCNTL_H
                     10: #include <fcntl.h>
                     11: #endif
                     12: #ifdef HAVE_SYS_MMAN_H
                     13: #include <sys/mman.h>
                     14: /* seems needed for Solaris */
                     15: #ifndef MAP_FAIL
                     16: #define MAP_FAIL ((void *) -1)
                     17: #endif
                     18: #endif
                     19: #ifdef HAVE_SYS_STAT_H
                     20: #include <sys/stat.h>
                     21: #endif
                     22: #ifdef HAVE_UNISTD_H
                     23: #include <unistd.h>
                     24: #endif
1.1       daniel     25: 
                     26: 
                     27: #include <libxml/xmlmemory.h>
                     28: #include <libxml/parser.h>
                     29: #include <libxml/parserInternals.h>
                     30: #include <libxml/tree.h>
1.3       veillard   31: #include <libxml/uri.h>
1.1       daniel     32: 
                     33: 
                     34: #define OASIS_TEST "conf/xmlconf.xml"
                     35: 
1.3       veillard   36: #define TEST_OK                0
                     37: #define TEST_FAILED    -1
                     38: 
                     39: #define TYPE_ERROR     0
                     40: #define TYPE_NOTWF     1
                     41: #define TYPE_INVALID   2
                     42: #define TYPE_VALID     3
                     43: 
                     44: static const char *types[] = {
                     45:     "error",
                     46:     "not-wf",
                     47:     "invalid",
                     48:     "valid",
                     49:     NULL
                     50: };
                     51: 
                     52: static int verbose = 0;
                     53: 
                     54: static int wfWarning = 0;
                     55: static int wfError = 0;
                     56: static int validWarning = 0;
                     57: static int validError = 0;
                     58: 
                     59: static int passed = 0;
                     60: static int failed = 0;
1.1       daniel     61: 
                     62: /************************************************************************
                     63:  *                                                                     *
1.3       veillard   64:  *                     Test ouput                                      *
1.1       daniel     65:  *                                                                     *
                     66:  ************************************************************************/
                     67: /**
1.3       veillard   68:  * xmlTestError:
1.1       daniel     69:  * @ctx:  an XML parser context
                     70:  * @msg:  the message to display/transmit
                     71:  * @...:  extra parameters for the message display
                     72:  * 
                     73:  * Display and format an error messages, gives file, line, position and
                     74:  * extra parameters.
                     75:  */
                     76: void
1.3       veillard   77: xmlTestError(void *ctx, const char *msg, ...)
1.1       daniel     78: {
1.3       veillard   79:     wfError++;
1.1       daniel     80: }
                     81: 
                     82: /**
1.3       veillard   83:  * xmlTestWarning:
1.1       daniel     84:  * @ctx:  an XML parser context
                     85:  * @msg:  the message to display/transmit
                     86:  * @...:  extra parameters for the message display
                     87:  * 
                     88:  * Display and format a warning messages, gives file, line, position and
                     89:  * extra parameters.
                     90:  */
                     91: void
1.3       veillard   92: xmlTestWarning(void *ctx, const char *msg, ...)
1.1       daniel     93: {
1.3       veillard   94:     wfWarning++;
1.1       daniel     95: }
                     96: 
                     97: /**
1.3       veillard   98:  * xmlTestValidityError:
1.1       daniel     99:  * @ctx:  an XML parser context
                    100:  * @msg:  the message to display/transmit
                    101:  * @...:  extra parameters for the message display
                    102:  * 
                    103:  * Display and format an validity error messages, gives file,
                    104:  * line, position and extra parameters.
                    105:  */
                    106: void
1.3       veillard  107: xmlTestValidityError(void *ctx, const char *msg, ...)
1.1       daniel    108: {
1.3       veillard  109:     validError++;
1.1       daniel    110: }
                    111: 
                    112: /**
1.3       veillard  113:  * xmlTestValidityWarning:
1.1       daniel    114:  * @ctx:  an XML parser context
                    115:  * @msg:  the message to display/transmit
                    116:  * @...:  extra parameters for the message display
                    117:  * 
                    118:  * Display and format a validity warning messages, gives file, line,
                    119:  * position and extra parameters.
                    120:  */
                    121: void
1.3       veillard  122: xmlTestValidityWarning(void *ctx, const char *msg, ...)
1.1       daniel    123: {
1.3       veillard  124:     validWarning++;
                    125: }
1.1       daniel    126: 
1.3       veillard  127: /************************************************************************
                    128:  *                                                                     *
                    129:  *                     The code running the tests                      *
                    130:  *                                                                     *
                    131:  ************************************************************************/
                    132: 
                    133: int runOasisTest(const xmlChar *id, int type,
                    134:                 const char *filename, const xmlChar *sections) {
                    135:     xmlDocPtr doc = NULL;
                    136:     int ret;
                    137:     xmlParserCtxtPtr ctxt;
                    138:     xmlSAXHandler silent, *old;
                    139: 
                    140:     if (verbose) {
                    141:        printf("Test: %s, type %s, URI %s", id, types[type], filename);
                    142:        if (sections != NULL) {
                    143:            printf(", sections %s\n", sections);
                    144:        } else 
                    145:            printf("\n");
                    146:     }
                    147: 
                    148:     wfWarning = 0;
                    149:     wfError = 0;
                    150:     validWarning = 0;
                    151:     validError = 0;
                    152: 
                    153:     ctxt = xmlCreateFileParserCtxt(filename);
                    154:     if (ctxt == NULL) {
                    155:        printf("Test: %s, cannot open %s\n", id, filename);
                    156:        return(-1);
                    157:     }
                    158:     memcpy(&silent, ctxt->sax, sizeof(silent));
                    159:     old = ctxt->sax;
                    160:     silent.error = xmlTestError;
                    161:     silent.warning = xmlTestWarning;
                    162:     silent.fatalError = xmlTestError;
                    163:     ctxt->sax = &silent;
                    164:     ctxt->vctxt.error = xmlTestValidityError;
                    165:     ctxt->vctxt.warning = xmlTestValidityWarning;
1.4       veillard  166:     if ((type == TYPE_INVALID) || (type == TYPE_VALID)) {
                    167:        ctxt->validate = 1;
                    168:        /* Allocate the Node stack */
                    169:        ctxt->vctxt.nodeTab = (xmlNodePtr *) xmlMalloc(4 * sizeof(xmlNodePtr));
                    170:        ctxt->vctxt.nodeNr = 0;
                    171:        ctxt->vctxt.nodeMax = 4;
                    172:        ctxt->vctxt.node = NULL;
                    173:     }
1.3       veillard  174: 
                    175:     xmlParseDocument(ctxt);
                    176: 
                    177:     ret = ctxt->wellFormed;
                    178:     doc = ctxt->myDoc;
                    179:     ctxt->sax = old;
                    180:     xmlFreeParserCtxt(ctxt);
                    181:     if (!ret) {
                    182:        xmlFreeDoc(doc);
                    183:        doc = NULL;
                    184:     }
                    185: 
                    186:     /*
                    187:      * Analyze
                    188:      */
                    189:     ret = 1;
                    190:     switch (type) {
                    191:        case TYPE_NOTWF:
                    192:            if (wfError == 0) {
1.4       veillard  193:                printf("test %s failed : didn't found WF error\n",filename);
1.3       veillard  194:                ret = 0;
                    195:            } else if (doc != NULL) {
1.4       veillard  196:                printf("test %s : WF error but return result\n",filename);
1.3       veillard  197:            }
                    198:            break;
                    199:        case TYPE_INVALID:
1.4       veillard  200:            if (wfError != 0) {
                    201:                printf(
                    202:        "test %s failed : found WF error in just invalid document\n",filename);
                    203:                ret = 0;
                    204:            } else if (validError == 0) {
                    205:                printf("test %s : didn't found vality errors \n",filename);
                    206:                ret = 0;
                    207:            }
1.3       veillard  208:            break;
                    209:        case TYPE_VALID:
1.4       veillard  210:            if (wfError != 0) {
                    211:                printf("test %s failed : found WF error while valid\n",filename);
                    212:                ret = 0;
                    213:            } else if (validError != 0) {
                    214:                printf(
                    215:                "test %s : found vality errors while document is valid\n",filename);
                    216:                ret = 0;
                    217:            }
1.3       veillard  218:            break;
                    219:        case TYPE_ERROR:
                    220:            break;
                    221:     }
                    222: 
                    223:     /*
                    224:      * free it.
                    225:      */
                    226:     if (doc != NULL)
                    227:        xmlFreeDoc(doc);
1.1       daniel    228: 
1.3       veillard  229:     if ((verbose) && (ret == 1)) {
1.4       veillard  230:        printf("test %s Ok\n",filename);
1.3       veillard  231:     }
1.4       veillard  232:     if (ret == 1)
                    233:        passed++;
                    234:     else
                    235:        failed++;
1.3       veillard  236:     return(0);
1.1       daniel    237: }
                    238: 
1.2       daniel    239: /************************************************************************
                    240:  *                                                                     *
                    241:  *                     OASIS testsuite interface                       *
                    242:  *                                                                     *
                    243:  ************************************************************************/
1.1       daniel    244: xmlDocPtr readOasisSuite(const char *filename) {
                    245:     int ret;
                    246:     xmlParserCtxtPtr ctxt;
                    247:     xmlSAXHandler silent, *old;
                    248: 
                    249:     xmlDocPtr doc;
                    250: 
                    251:     /*
                    252:      * strip blanks from the test result tree and read in the external
                    253:      * entities.
                    254:      */
                    255:     xmlKeepBlanksDefault(0);
                    256:     xmlSubstituteEntitiesDefault(1);
                    257: 
                    258:     /*
                    259:      * Do a silent parsing
                    260:      */
                    261:     ctxt = xmlCreateFileParserCtxt(filename);
                    262:     memcpy(&silent, ctxt->sax, sizeof(silent));
                    263:     old = ctxt->sax;
                    264:     silent.error = NULL;
                    265:     silent.warning = NULL;
                    266:     silent.fatalError = NULL;
                    267: 
                    268:     xmlParseDocument(ctxt);
                    269: 
                    270:     ret = ctxt->wellFormed;
                    271:     doc = ctxt->myDoc;
                    272:     ctxt->sax = old;
                    273:     xmlFreeParserCtxt(ctxt);
                    274:     if (!ret) {
                    275:         xmlFreeDoc(doc);
                    276:        doc = NULL;
                    277:     }
                    278: 
                    279:     /*
                    280:      * switch back to decent defaults.
                    281:      */
                    282:     xmlKeepBlanksDefault(1);
                    283:     xmlSubstituteEntitiesDefault(0);
                    284:     return(doc);
                    285: }
                    286: 
1.3       veillard  287: int analyzeTest(xmlDocPtr doc, xmlNodePtr test) {
1.1       daniel    288:     xmlChar *type;
                    289:     xmlChar *sections;
                    290:     xmlChar *id;
                    291:     xmlChar *URI;
1.3       veillard  292:     xmlChar *uri = NULL;
                    293:     int typ = -1;
1.1       daniel    294: 
1.3       veillard  295:     /*
                    296:      * Scan the infos
                    297:      */
1.1       daniel    298:     id = xmlGetProp(test, (xmlChar *) "ID");
                    299:     if (id == NULL) {
                    300:         fprintf(stderr, "Test without ID\n");
                    301:        return(0);
                    302:     }
                    303:     type = xmlGetProp(test, (xmlChar *) "TYPE");
                    304:     if (type == NULL) {
                    305:         fprintf(stderr, "Test %s without TYPE\n", id);
                    306:        xmlFree(id);
                    307:        return(0);
                    308:     }
1.6     ! veillard  309:     if (xmlStrEqual(type, (const xmlChar *) "not-wf"))
1.3       veillard  310:        typ = TYPE_NOTWF;
1.6     ! veillard  311:     else if (xmlStrEqual(type, (const xmlChar *) "error"))
1.3       veillard  312:        typ = TYPE_ERROR;
1.6     ! veillard  313:     else if (xmlStrEqual(type, (const xmlChar *) "valid"))
1.3       veillard  314:        typ = TYPE_VALID;
1.6     ! veillard  315:     else if (xmlStrEqual(type, (const xmlChar *) "invalid"))
1.3       veillard  316:        typ = TYPE_INVALID;
                    317:     else {
                    318:         fprintf(stderr, "Test %s : unknown TYPE %s\n", id, type);
                    319:        xmlFree(id);
                    320:        xmlFree(type);
                    321:        return(0);
                    322:     }
                    323: 
1.1       daniel    324:     URI = xmlGetProp(test, (xmlChar *) "URI");
                    325:     if (URI == NULL) {
                    326:         fprintf(stderr, "Test %s without URI\n", id);
                    327:        xmlFree(id);
                    328:        xmlFree(type);
                    329:        return(0);
                    330:     }
1.3       veillard  331:     uri = xmlBuildURI(URI, doc->URL);
1.1       daniel    332:     sections = xmlGetProp(test, (xmlChar *) "SECTIONS");
1.3       veillard  333: 
                    334:     /*
                    335:      * Launch the test
                    336:      */
                    337:     if (uri != NULL)
                    338:        runOasisTest(id, typ, (const char *) uri, sections);
                    339:     else
                    340:        runOasisTest(id, typ, (const char *) URI, sections);
                    341: 
                    342:     /*
                    343:      * Cleanup
                    344:      */
                    345:     if (sections != NULL)
1.1       daniel    346:        xmlFree(sections);
                    347:     xmlFree(id);
                    348:     xmlFree(type);
                    349:     xmlFree(URI);
1.3       veillard  350:     if (uri != NULL)
                    351:        xmlFree(uri);
1.1       daniel    352:     return(1);
                    353: }
                    354: 
1.3       veillard  355: int analyzeCases(xmlDocPtr doc, xmlNodePtr cases) {
1.1       daniel    356:     xmlChar *profile;
                    357:     int nbtests = 0;
                    358:     xmlNodePtr child;
                    359: 
                    360:     profile = xmlGetProp(cases, (xmlChar *) "PROFILE");
                    361: 
                    362:     if (profile) {
                    363:        printf("\n  Suite: %s\n\n", (char *) profile);
                    364:     } else {
                    365:        printf("\n  Suite: unknown tests\n\n");
                    366:     }
                    367: 
                    368:     child = cases->children;
                    369:     while (child != NULL) {
                    370:         if ((child->type == XML_ELEMENT_NODE) &&
1.6     ! veillard  371:             (xmlStrEqual(child->name, (const xmlChar *)"TEST"))) {
1.3       veillard  372:            nbtests += analyzeTest(doc, child);
1.1       daniel    373:        }
                    374:        child = child->next;
                    375:     }
                    376: 
                    377:     if (profile) {
                    378:        printf("\n  Suite: %s : %d tests\n\n", (char *) profile, nbtests);
                    379:        xmlFree(profile);
                    380:     } else {
                    381:        printf("\n  Suite: %d tests\n\n", nbtests);
                    382:     }
                    383: 
                    384:     return(nbtests);
                    385: }
                    386: 
1.3       veillard  387: int analyzeSuite(xmlDocPtr doc, xmlNodePtr suite) {
1.1       daniel    388:     xmlChar *profile;
                    389:     int nbtests = 0;
                    390:     xmlNodePtr child;
                    391: 
1.6     ! veillard  392:     if ((suite == NULL) ||
        !           393:        (!xmlStrEqual(suite->name, (const xmlChar *)"TESTSUITE"))) {
1.1       daniel    394:         fprintf(stderr, "No TESTSUITE root\n");
                    395:        return(-1);
                    396:     }
                    397:     profile = xmlGetProp(suite, (xmlChar *) "PROFILE");
                    398: 
                    399:     if (profile) {
                    400:        printf("\n     Testing: %s\n\n", (char *) profile);
                    401:     } else {
                    402:        printf("\n     Testing: unknown test suite\n\n");
                    403:     }
                    404: 
                    405:     child = suite->children;
                    406:     while (child != NULL) {
                    407:         if ((child->type == XML_ELEMENT_NODE) &&
1.6     ! veillard  408:             (xmlStrEqual(child->name, (const xmlChar *)"TESTCASES"))) {
1.3       veillard  409:            nbtests += analyzeCases(doc, child);
1.1       daniel    410:        }
                    411:        child = child->next;
                    412:     }
                    413: 
                    414:     if (profile) {
1.4       veillard  415:        printf("\n     Testing: %s : %d tests\n", (char *) profile, nbtests);
1.1       daniel    416:        xmlFree(profile);
                    417:     } else {
1.4       veillard  418:        printf("\n     Testing: test suite : %d tests\n", nbtests);
1.1       daniel    419:     }
1.4       veillard  420:     printf("\n     Result: %d tests passed, %d tests failed\n", passed, failed);
1.1       daniel    421: 
                    422:     return(nbtests);
                    423: }
                    424: 
                    425: int main(int argc, char ** argv) {
                    426:     xmlDocPtr tree;
                    427: 
                    428:     tree = readOasisSuite(OASIS_TEST);
                    429:     
                    430:     if (tree == NULL) {
                    431:         fprintf(stderr, "failed to load %s\n", OASIS_TEST);
                    432:         return(-1);
                    433:     }
1.3       veillard  434:     analyzeSuite(tree, xmlDocGetRootElement(tree));
1.1       daniel    435:     return(0);
                    436: }

Webmaster