from xml dom minidom import parseString from xml sax saxutils import e

 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
from xml.dom.minidom import parseString
from xml.sax.saxutils import escape, quoteattr
from os import urandom
def xml_gen(attr, message):
attr = quoteattr(attr.decode('utf-8', 'ignore').encode('utf-8'))
message = escape(message.decode('utf-8', 'ignore').encode('utf-8'))
return "<?xml version='1.0'?><message attr='%s'>%s</message>" % (attr, message)
def is_valid_xml(xml_data):
try:
parseString(xml_data)
except Exception, ex:
return False
else:
return True
def test_empty_values():
assert True == is_valid_xml(xml_gen('', ''))
def test_bad_symbols():
for sym in ['<', '&', '"', '\'']:
assert True == is_valid_xml(xml_gen('<', '<'))
def test_trash():
for x in xrange(10):
assert True == is_valid_xml(xml_gen(urandom(50), urandom(50)))