/usr/share/caldavtester/verifiers/xmlElementMatch.py is in caldav-tester 7.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ##
# Copyright (c) 2010-2015 Apple Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##
"""
Verifier that checks the response body for an exact match to data in a file.
"""
from pycalendar.icalendar.calendar import Calendar
from xml.etree.cElementTree import ElementTree
import json
import re
import StringIO
class Verifier(object):
def verify(self, manager, uri, response, respdata, args): #@UnusedVariable
# Get arguments
parent = args.get("parent", [])
exists = args.get("exists", [])
notexists = args.get("notexists", [])
# status code must be 200, 207
if response.status not in (200, 207):
return False, " HTTP Status Code Wrong: %d" % (response.status,)
# look for response data
if not respdata:
return False, " No response body"
# Read in XML
try:
tree = ElementTree(file=StringIO.StringIO(respdata))
except Exception, e:
return False, " Response data is not xml data: %s" % (e,)
def _splitPathTests(path):
if '[' in path:
return path.split('[', 1)
else:
return path, None
if parent:
nodes = self.nodeForPath(tree.getroot(), parent[0])
if len(nodes) == 0:
return False, " Response data is missing parent node: %s" % (parent[0],)
elif len(nodes) > 1:
return False, " Response data has too many parent nodes: %s" % (parent[0],)
root = nodes[0]
else:
root = tree.getroot()
result = True
resulttxt = ""
for path in exists:
matched, txt = self.matchNode(root, path)
result &= matched
resulttxt += txt
for path in notexists:
matched, _ignore_txt = self.matchNode(root, path)
if matched:
resulttxt += " Items returned in XML for %s\n" % (path,)
result = False
return result, resulttxt
def nodeForPath(self, root, path):
if '[' in path:
actual_path, tests = path.split('[', 1)
else:
actual_path = path
tests = None
# Handle absolute root element
if actual_path[0] == '/':
actual_path = actual_path[1:]
r = re.search("(\{[^\}]+\}[^/]+)(.*)", actual_path)
if r.group(2):
root_path = r.group(1)
child_path = r.group(2)[1:]
if root.tag != root_path:
return None
nodes = root.findall(child_path)
else:
root_path = actual_path
child_path = None
nodes = (root,)
if len(nodes) == 0:
return None
results = []
if tests:
tests = [item[:-1] for item in tests.split('[')]
for test in tests:
for node in nodes:
testresult = self.testNode(node, path, test)
if testresult is None:
results.append(node)
else:
results = nodes
return results
@classmethod
def testNode(cls, node, node_path, test):
result = None
if test[0] == '@':
if '=' in test:
attr, value = test[1:].split('=')
value = value[1:-1]
else:
attr = test[1:]
value = None
if attr not in node.keys():
result = " Missing attribute returned in XML for %s\n" % (node_path,)
if value is not None and node.get(attr) != value:
result = " Incorrect attribute value returned in XML for %s\n" % (node_path,)
elif test[0] == '=':
if node.text != test[1:]:
result = " Incorrect value returned in XML for %s\n" % (node_path,)
elif test[0] == '!':
if node.text == test[1:]:
result = " Incorrect value returned in XML for %s\n" % (node_path,)
elif test[0] == '*':
if node.text is None or node.text.find(test[1:]) == -1:
result = " Incorrect value returned in XML for %s\n" % (node_path,)
elif test[0] == '$':
if node.text is None or node.text.find(test[1:]) != -1:
result = " Incorrect value returned in XML for %s\n" % (node_path,)
elif test[0] == '+':
if node.text is None or not node.text.startswith(test[1:]):
result = " Incorrect value returned in XML for %s\n" % (node_path,)
elif test[0] == '^':
if "=" in test:
element, value = test[1:].split("=", 1)
else:
element = test[1:]
value = None
for child in node.getchildren():
if child.tag == element and (value is None or child.text == value):
break
else:
result = " Missing child returned in XML for %s\n" % (node_path,)
elif test[0] == '|':
if len(test) == 2 and test[1] == "|":
if node.text is None and len(node.getchildren()) == 0:
result = " Empty element returned in XML for %s\n" % (node_path,)
else:
if node.text is not None or len(node.getchildren()) != 0:
result = " Non-empty element returned in XML for %s\n" % (node_path,)
# Try to parse as iCalendar
elif test == 'icalendar':
try:
Calendar.parseText(node.text)
except:
result = " Incorrect value returned in iCalendar for %s\n" % (node_path,)
# Try to parse as JSON
elif test == 'json':
try:
json.loads(node.text)
except:
result = " Incorrect value returned in XML for %s\n" % (node_path,)
return result
@classmethod
def matchNode(cls, root, xpath, parent_map=None, title=None):
if title is None:
title = xpath
result = True
resulttxt = ""
# Find the first test in the xpath
if '[' in xpath:
actual_xpath, tests = xpath.split('[', 1)
else:
actual_xpath = xpath
tests = None
if parent_map is None:
parent_map = dict((c, p) for p in root.getiterator() for c in p)
# Handle parents
if actual_xpath.startswith("../"):
root = parent_map[root]
actual_xpath = "./" + actual_xpath[3:]
# Handle absolute root element and find all matching nodes
r = re.search("(/?\{[^\}]+\}[^/]+|\.)(.*)", actual_xpath)
if r.group(2):
root_path = r.group(1)
child_path = r.group(2)[1:]
if root_path != "." and root.tag != root_path.lstrip("/"):
resulttxt += " Items not returned in XML for %s\n" % (title,)
result = False
return result, resulttxt
nodes = root.findall(child_path)
else:
nodes = (root,)
if len(nodes) == 0:
resulttxt += " Items not returned in XML for %s\n" % (title,)
result = False
return result, resulttxt
if tests:
# Split the tests into tests plus additional path
pos = tests.find("]/")
if pos != -1:
node_tests = tests[:pos + 1]
next_path = tests[pos + 1:]
else:
node_tests = tests
next_path = None
node_tests = [item[:-1] for item in node_tests.split('[')]
for test in node_tests:
for node in nodes:
testresult = cls.testNode(node, title, test)
if testresult is None:
if next_path:
next_result, testresult = cls.matchNode(node, next_path[1:], parent_map, title)
if next_result:
break
else:
break
if testresult:
resulttxt += testresult
result = False
break
return result, resulttxt
# Tests
if __name__ == '__main__':
xmldata = """
<D:test xmlns:D="DAV:">
<D:a>A</D:a>
<D:b>
<D:c>C</D:c>
<D:d>D</D:d>
</D:b>
<D:b>
<D:c>C</D:c>
<D:d>F</D:d>
</D:b>
</D:test>
"""
node = ElementTree(file=StringIO.StringIO(xmldata)).getroot()
assert Verifier.matchNode(node, "/{DAV:}test/{DAV:}b/{DAV:}c[=C]/../{DAV:}d[=D]")[0]
assert not Verifier.matchNode(node, "/{DAV:}test/{DAV:}b/{DAV:}c[=C]/../{DAV:}d[=E]")[0]
|