-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathtestparser.py
261 lines (210 loc) · 11.4 KB
/
testparser.py
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
import os
import sys, random
from twisted.trial import unittest
import time
from twisted.web import server, resource, static, http, client
from twisted.words.protocols.jabber import jid
from twisted.internet import defer, protocol, reactor
from twisted.application import internet, service
from twisted.words.xish import domish, xpath
from twisted.python import log
from punjab.httpb import HttpbParse
class ParseTestCase(unittest.TestCase):
"""
Tests for Punjab compatability with http://www.xmpp.org/extensions/xep-0124.html
"""
def assertXMLEqual(self, expected_str, elem_str):
hp = HttpbParse(use_t=True)
_, ex = hp.parse("<body>{}</body>".format(expected_str))
hp._reset()
_, e = hp.parse("<body>{}</body>".format(elem_str))
self.assertTrue(len(e)>0)
self.assertEqual(len(ex), len(e))
def assert_equal_elements(e1, e2):
if isinstance(e1, str):
self.assertEqual(e1, e2)
else:
self.assertEqual(e1.name, e2.name)
for k,v in e1.attributes.items():
self.assertEqual(v, e2.attributes[k])
for i, c in enumerate(e1.children):
assert_equal_elements(c, e2.children[i])
assert_equal_elements(ex[0], e[0])
def testTime(self):
XML = """
<body rid='4019888743' xmlns='http://jabber.org/protocol/httpbind' sid='948972a64d524f862107cdbd748d1d16'><iq id='980:getprefs' type='get'><query xmlns='jabber:iq:private'><preferences xmlns='http://chesspark.com/xml/chesspark-01'/></query></iq><iq id='981:getallignorelists' type='get'><query xmlns='jabber:iq:privacy'/></iq><test/><testing><ha/></testing></body>
"""
t = time.time()
for i in range(0, 10000):
hp = HttpbParse(use_t=True)
b, elems = hp.parse(XML)
for e in elems:
x = e.toXml()
td = time.time() - t
t = time.time()
for i in range(0, 10000):
hp = HttpbParse()
b, elems = hp.parse(XML)
for e in elems:
if type(u'') == type(e):
x = e
ntd = time.time() - t
self.failUnless(td>ntd, 'Not faster')
def testGtBug(self):
XML = """ <body rid='1445008480' xmlns='http://jabber.org/protocol/httpbind' sid='1f2f8585f41e2dacf1f1f0ad83f8833d'><presence type='unavailable' from='[email protected]/cpc' to='[email protected]/[email protected]'/><iq id='10059:enablepush' to='search.chesspark.com' type='set'><search xmlns='http://onlinegamegroup.com/xml/chesspark-01' node='play'><filter><relative-rating>500</relative-rating><time-control-range name='speed'/></filter></search></iq><iq id='10060:enablepush' to='search.chesspark.com' type='set'><search xmlns='http://onlinegamegroup.com/xml/chesspark-01' node='play'><filter><relative-rating>500</relative-rating><time-control-range name='speed'/></filter></search></iq></body>
"""
hp = HttpbParse()
b, e = hp.parse(XML)
# need tests here
self.assertXMLEqual(
e[0],
"<presence from='[email protected]/cpc' type='unavailable' to='[email protected]/[email protected]'/>")
self.assertXMLEqual(e[1],
"<iq to='search.chesspark.com' type='set' id='10059:enablepush'><search xmlns='http://onlinegamegroup.com/xml/chesspark-01' node='play'><filter><relative-rating>500</relative-rating><time-control-range name='speed'/></filter></search></iq>")
self.assertXMLEqual(e[2],
"<iq to='search.chesspark.com' type='set' id='10060:enablepush'><search xmlns='http://onlinegamegroup.com/xml/chesspark-01' node='play'><filter><relative-rating>500</relative-rating><time-control-range name='speed'/></filter></search></iq>")
def testParse(self):
XML = """
<body rid='4019888743' xmlns='http://jabber.org/protocol/httpbind' sid='948972a64d524f862107cdbd748d1d16'><iq id='980:getprefs' type='get'><query xmlns='jabber:iq:private'><preferences xmlns='http://chesspark.com/xml/chesspark-01'/></query></iq><iq id='981:getallignorelists' type='get'><query xmlns='jabber:iq:privacy'/></iq></body>
"""
hp = HttpbParse()
b, e = hp.parse(XML)
# need tests here
self.assertXMLEqual(e[0],
"<iq type='get' id='980:getprefs'><query xmlns='jabber:iq:private'><preferences xmlns='http://chesspark.com/xml/chesspark-01'/></query></iq>")
self.assertXMLEqual(e[1],
"<iq type='get' id='981:getallignorelists'><query xmlns='jabber:iq:privacy'/></iq>")
def testParseEscapedAttribute(self):
XML = """<body rid='4019888743' xmlns='http://jabber.org/protocol/httpbind' sid='948972a64d524f862107cdbd748d1d16'><presence from='[email protected]' to='[email protected]/D'Artagnan Longfellow'/></body>"""
hp = HttpbParse()
b, e = hp.parse(XML)
ex = "<presence from='[email protected]' to='[email protected]/D'Artagnan Longfellow'/>"
self.assertXMLEqual(e[0], ex)
def testPrefixes(self):
XML = """<body rid='384852951' xmlns='http://jabber.org/protocol/httpbind' sid='e46501b24abd334c062598498a8e02ba'><auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='DIGEST-MD5'/></body>"""
hp = HttpbParse()
b, e = hp.parse(XML)
self.failUnless(e[0]=="<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='DIGEST-MD5'/>", 'invalid xml')
def testPrefixesLang(self):
XML = """<body rid='384852951' xmlns='http://jabber.org/protocol/httpbind' sid='e46501b24abd334c062598498a8e02ba'><message xml:lang='fr' to='[email protected]'><body>test</body></message></body>"""
hp = HttpbParse()
b, e = hp.parse(XML)
self.failUnlessEqual(e[0], "<message xml:lang='fr' to='[email protected]'><body>test</body></message>", 'XML does not match {}'.format(e[0]))
def testEscapedCDATA(self):
XML = """<body rid='384852951' xmlns='http://jabber.org/protocol/httpbind' sid='e46501b24abd334c062598498a8e02ba'><message><body>> </body></message></body>"""
hp = HttpbParse()
b, e = hp.parse(XML)
XML = """ <body rid='1484853516' xmlns='http://jabber.org/protocol/httpbind' sid='4dc131a03346bf94b0d2565dda02de36'><message to='[email protected]' from='[email protected]/cpc' type='groupchat' id='2900'><body xmlns='jabber:client'>i type > and i see >>></body></message></body>"""
hp = HttpbParse()
b, e = hp.parse(XML)
self.assertXMLEqual(e[0],
"<message to='[email protected]' from='[email protected]/cpc' id='2900' type='groupchat'><body xmlns='jabber:client'>i type > and i see >>></body></message>")
def testCDATA(self):
XML = """<body rid='3116008962' xmlns='http://jabber.org/protocol/httpbind' sid='88be95e7ebbd8c12465e311ce73fb8ac'><response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dXNlcm5hbWU9InRvZnUiLHJlYWxtPSJkZXYuY2hlc3NwYXJrLmNvbSIsbm9uY2U9Ik5SaW5HQkNaWjg0U09Ea1BzMWpxd1E9PSIsY25vbmNlPSJkNDFkOGNkOThmMDBiMjA0ZTk4MDA5OThlY2Y4NDI3ZSIsbmM9IjAwMDAwMDAxIixxb3A9ImF1dGgiLGRpZ2VzdC11cmk9InhtcHAvZGV2LmNoZXNzcGFyay5jb20iLHJlc3BvbnNlPSIxNGQ3NWE5YmU2MzdkOTdkOTM1YjU2Y2M4ZWZhODk4OSIsY2hhcnNldD0idXRmLTgi</response></body>"""
hp = HttpbParse()
b, e = hp.parse(XML)
self.failUnless(e[0]=="<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dXNlcm5hbWU9InRvZnUiLHJlYWxtPSJkZXYuY2hlc3NwYXJrLmNvbSIsbm9uY2U9Ik5SaW5HQkNaWjg0U09Ea1BzMWpxd1E9PSIsY25vbmNlPSJkNDFkOGNkOThmMDBiMjA0ZTk4MDA5OThlY2Y4NDI3ZSIsbmM9IjAwMDAwMDAxIixxb3A9ImF1dGgiLGRpZ2VzdC11cmk9InhtcHAvZGV2LmNoZXNzcGFyay5jb20iLHJlc3BvbnNlPSIxNGQ3NWE5YmU2MzdkOTdkOTM1YjU2Y2M4ZWZhODk4OSIsY2hhcnNldD0idXRmLTgi</response>", 'Invalid xml')
def testPrefsCdata(self):
XML = """<body rid='4017760695' xmlns='http://jabber.org/protocol/httpbind' sid='74a730628186b053a953999bc2ae7dba'>
<iq id='6161:setprefs' type='set' xmlns='jabber:client'>
<query xmlns='jabber:iq:private'>
<preferences xmlns='http://chesspark.com/xml/chesspark-01'>
<statuses>
<away>test2</away>
<available>test1</available>
</statuses>
<favorite-channels>
<channel jid='[email protected]' autojoin='no'/>
<channel jid='[email protected]' autojoin='no'/>
<channel jid='[email protected]' autojoin='no'/>
</favorite-channels>
<time-controls/>
<searchfilters>
<filter node='play'>
<variant name='standard'/>
</filter>
<filter open='yes' node='watch'>
<computer/>
</filter>
<filter node='adjourned'>
<computer/>
</filter>
<filter node='myads'>
<computer/>
</filter>
</searchfilters>
<loginrooms>
<room>[email protected]</room>
</loginrooms>
<noinitialroster/>
<boardsize size='61'/>
<volume setting='100'/>
<hidewelcomedialog/>
<showoffline/>
<showavatars/>
<showmucpresenceinchat/>
<hideparticipants/>
<newlineonshift/>
<nochatnotify/>
<no-gameboard-autoresize/>
<messagewhenplaying/>
<hidegamefinderhelp/>
<hidewarningondisconnect/>
<disablesounds/>
<nogamesearchonlogin/>
</preferences>
</query>
</iq>
</body>"""
hp = HttpbParse()
b, e = hp.parse(XML)
expected = """<iq xmlns='jabber:client' id='6161:setprefs' type='set'>
<query xmlns='jabber:iq:private'>
<preferences xmlns='http://chesspark.com/xml/chesspark-01'>
<statuses>
<away>test2</away>
<available>test1</available>
</statuses>
<favorite-channels>
<channel jid='[email protected]' autojoin='no'/>
<channel jid='[email protected]' autojoin='no'/>
<channel jid='[email protected]' autojoin='no'/>
</favorite-channels>
<time-controls/>
<searchfilters>
<filter node='play'>
<variant name='standard'/>
</filter>
<filter node='watch' open='yes'>
<computer/>
</filter>
<filter node='adjourned'>
<computer/>
</filter>
<filter node='myads'>
<computer/>
</filter>
</searchfilters>
<loginrooms>
<room>[email protected]</room>
</loginrooms>
<noinitialroster/>
<boardsize size='61'/>
<volume setting='100'/>
<hidewelcomedialog/>
<showoffline/>
<showavatars/>
<showmucpresenceinchat/>
<hideparticipants/>
<newlineonshift/>
<nochatnotify/>
<no-gameboard-autoresize/>
<messagewhenplaying/>
<hidegamefinderhelp/>
<hidewarningondisconnect/>
<disablesounds/>
<nogamesearchonlogin/>
</preferences>
</query>
</iq>"""
self.assertXMLEqual(expected, e[0])