1#!/usr/bin/env python3 2import sys 3import time 4import os 5sys.path.insert(0, "python") 6import libxml2 7 8# 9# the testsuite description 10# 11DIR="xinclude-test-suite" 12CONF="testdescr.xml" 13LOG="check-xinclude-test-suite.log" 14 15log = open(LOG, "w") 16 17os.chdir(DIR) 18 19test_nr = 0 20test_succeed = 0 21test_failed = 0 22test_error = 0 23# 24# Error and warning handlers 25# 26error_nr = 0 27error_msg = '' 28 29def errorHandler(ctx, str): 30 global error_nr 31 global error_msg 32 33 if str.find("error:") >= 0: 34 error_nr = error_nr + 1 35 if len(error_msg) < 300: 36 if len(error_msg) == 0 or error_msg[-1] == '\n': 37 error_msg = error_msg + " >>" + str 38 else: 39 error_msg = error_msg + str 40 41libxml2.registerErrorHandler(errorHandler, None) 42 43def testXInclude(filename, id): 44 global error_nr 45 global error_msg 46 global log 47 48 error_nr = 0 49 error_msg = '' 50 51 print("testXInclude(%s, %s)" % (filename, id)) 52 return 1 53 54def runTest(test, basedir): 55 global test_nr 56 global test_failed 57 global test_error 58 global test_succeed 59 global error_msg 60 global log 61 62 fatal_error = 0 63 uri = test.prop('href') 64 id = test.prop('id') 65 type = test.prop('type') 66 if uri is None: 67 print("Test without ID:", uri) 68 return -1 69 if id is None: 70 print("Test without URI:", id) 71 return -1 72 if type is None: 73 print("Test without URI:", id) 74 return -1 75 if basedir != None: 76 URI = basedir + "/" + uri 77 else: 78 URI = uri 79 if os.access(URI, os.R_OK) == 0: 80 print("Test %s missing: base %s uri %s" % (URI, basedir, uri)) 81 return -1 82 83 expected = None 84 outputfile = None 85 diff = None 86 if type != 'error': 87 output = test.xpathEval('string(output)') 88 if output == 'No output file.': 89 output = None 90 if output == '': 91 output = None 92 if output != None: 93 if basedir != None: 94 output = basedir + "/" + output 95 if os.access(output, os.R_OK) == 0: 96 print("Result for %s missing: %s" % (id, output)) 97 output = None 98 else: 99 try: 100 f = open(output) 101 expected = f.read() 102 outputfile = output 103 except: 104 print("Result for %s unreadable: %s" % (id, output)) 105 106 try: 107 # print("testing %s" % (URI)) 108 doc = libxml2.parseFile(URI) 109 except: 110 doc = None 111 if doc != None: 112 res = doc.xincludeProcess() 113 if res >= 0 and expected != None: 114 result = doc.serialize() 115 if result != expected: 116 print("Result for %s differs" % (id)) 117 open("xinclude.res", "w").write(result) 118 diff = os.popen("diff %s xinclude.res" % outputfile).read() 119 120 doc.freeDoc() 121 else: 122 print("Failed to parse %s" % (URI)) 123 res = -1 124 125 126 127 test_nr = test_nr + 1 128 if type == 'success': 129 if res > 0: 130 test_succeed = test_succeed + 1 131 elif res == 0: 132 test_failed = test_failed + 1 133 print("Test %s: no substitution done ???" % (id)) 134 elif res < 0: 135 test_error = test_error + 1 136 print("Test %s: failed valid XInclude processing" % (id)) 137 elif type == 'error': 138 if res > 0: 139 test_error = test_error + 1 140 print("Test %s: failed to detect invalid XInclude processing" % (id)) 141 elif res == 0: 142 test_failed = test_failed + 1 143 print("Test %s: Invalid but no substitution done" % (id)) 144 elif res < 0: 145 test_succeed = test_succeed + 1 146 elif type == 'optional': 147 if res > 0: 148 test_succeed = test_succeed + 1 149 else: 150 print("Test %s: failed optional test" % (id)) 151 152 # Log the ontext 153 if res != 1: 154 log.write("Test ID %s\n" % (id)) 155 log.write(" File: %s\n" % (URI)) 156 content = test.content.strip() 157 while content[-1] == '\n': 158 content = content[0:-1] 159 log.write(" %s:%s\n\n" % (type, content)) 160 if error_msg != '': 161 log.write(" ----\n%s ----\n" % (error_msg)) 162 error_msg = '' 163 log.write("\n") 164 if diff != None: 165 log.write("diff from test %s:\n" %(id)) 166 log.write(" -----------\n%s\n -----------\n" % (diff)); 167 168 return 0 169 170 171def runTestCases(case): 172 creator = case.prop('creator') 173 if creator != None: 174 print("=>", creator) 175 base = case.getBase(None) 176 basedir = case.prop('basedir') 177 if basedir != None: 178 base = libxml2.buildURI(basedir, base) 179 test = case.children 180 while test != None: 181 if test.name == 'testcase': 182 runTest(test, base) 183 if test.name == 'testcases': 184 runTestCases(test) 185 test = test.next 186 187conf = libxml2.parseFile(CONF) 188if conf is None: 189 print("Unable to load %s" % CONF) 190 sys.exit(1) 191 192testsuite = conf.getRootElement() 193if testsuite.name != 'testsuite': 194 print("Expecting TESTSUITE root element: aborting") 195 sys.exit(1) 196 197profile = testsuite.prop('PROFILE') 198if profile != None: 199 print(profile) 200 201start = time.time() 202 203case = testsuite.children 204while case != None: 205 if case.name == 'testcases': 206 old_test_nr = test_nr 207 old_test_succeed = test_succeed 208 old_test_failed = test_failed 209 old_test_error = test_error 210 runTestCases(case) 211 print(" Ran %d tests: %d succeeded, %d failed and %d generated an error" % ( 212 test_nr - old_test_nr, test_succeed - old_test_succeed, 213 test_failed - old_test_failed, test_error - old_test_error)) 214 case = case.next 215 216conf.freeDoc() 217log.close() 218 219print("Ran %d tests: %d succeeded, %d failed and %d generated an error in %.2f s." % ( 220 test_nr, test_succeed, test_failed, test_error, time.time() - start)) 221