aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_rparse.py
blob: 11c831c64eeccee17a80e39d541926c5ef042733 (plain)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import os, cStringIO
from libpathod import rparse, utils
import tutils

rparse.TESTING = True


class TestMisc:
    def test_generators(self):
        v = rparse.Value.parseString("'val'")[0]
        g = v.get_generator({})
        assert g[:] == "val"

    def test_randomgenerator(self):
        g = rparse.RandomGenerator("one", 100)
        assert len(g[:10]) == 10
        assert len(g[1:10]) == 9
        assert len(g[:1000]) == 100
        assert len(g[1000:1001]) == 0
        assert g[0]

    def test_literalgenerator(self):
        g = rparse.LiteralGenerator("one")
        assert g == "one"
        assert g[:] == "one"
        assert g[1] == "n"

    def test_filegenerator(self):
        with tutils.tmpdir() as t:
            path = os.path.join(t, "foo")
            f = open(path, "w")
            f.write("x"*10000)
            f.close()
            g = rparse.FileGenerator(path)
            assert len(g) == 10000
            assert g[0] == "x"
            assert g[-1] == "x"
            assert g[0:5] == "xxxxx"

    def test_valueliteral(self):
        v = rparse.ValueLiteral("foo")
        assert v.expr()
        assert v.val == "foo"

        v = rparse.ValueLiteral(r"foo\n")
        assert v.expr()
        assert v.val == "foo\n"

    def test_valuenakedliteral(self):
        v = rparse.ValueNakedLiteral("foo")
        assert v.expr()

    def test_file_value(self):
        v = rparse.Value.parseString("<'one two'")[0]
        assert str(v)
        assert v.path == "one two"

        v = rparse.Value.parseString("<path")[0]
        assert v.path == "path"

        with tutils.tmpdir() as t:
            p = os.path.join(t, "path")
            f = open(p, "w")
            f.write("x"*10000)
            f.close()

            assert v.get_generator(dict(staticdir=t))

            v = rparse.Value.parseString("<path2")[0]
            tutils.raises(rparse.ServerError, v.get_generator, dict(staticdir=t))
            tutils.raises("no static directory", v.get_generator, dict())

    def test_generated_value(self):
        v = rparse.Value.parseString("@10b")[0]
        assert v.usize == 10
        assert v.unit == "b"
        assert v.bytes() == 10
        v = rparse.Value.parseString("@10")[0]
        assert v.unit == "b"
        v = rparse.Value.parseString("@10k")[0]
        assert v.bytes() == 10240
        v = rparse.Value.parseString("@10g")[0]
        assert v.bytes() == 1024**3 * 10

        v = rparse.Value.parseString("@10g,digits")[0]
        assert v.datatype == "digits"
        g = v.get_generator({})
        assert g[:100]

        v = rparse.Value.parseString("@10,digits")[0]
        assert v.unit == "b"
        assert v.datatype == "digits"

    def test_value(self):
        assert rparse.Value.parseString("'val'")[0].val == "val"
        assert rparse.Value.parseString('"val"')[0].val == "val"
        assert rparse.Value.parseString('"\'val\'"')[0].val == "'val'"

    def test_path(self):
        e = rparse.Path.expr()
        assert e.parseString('"/foo"')[0].value.val == "/foo"

    def test_method(self):
        e = rparse.Method.expr()
        assert e.parseString("get")[0].value.val == "GET"
        assert e.parseString("'foo'")[0].value.val == "foo"
        assert e.parseString("'get'")[0].value.val == "get"

    def test_body(self):
        e = rparse.Body.expr()
        v = e.parseString("b'foo'")[0]
        assert v.value.val == "foo"

        v = e.parseString("b@100")[0]
        assert str(v.value) == "@100b,bytes"

        v = e.parseString("b@100g,digits", parseAll=True)[0]
        assert v.value.datatype == "digits"
        assert str(v.value) == "@100g,digits"

    def test_header(self):
        e = rparse.Header.expr()
        v = e.parseString("h'foo'='bar'")[0]
        assert v.key.val == "foo"
        assert v.value.val == "bar"

    def test_code(self):
        e = rparse.Code.expr()
        v = e.parseString("200")[0]
        assert v.code == 200

        v = e.parseString("404'msg'")[0]
        assert v.code == 404
        assert v.msg.val == "msg"

        r = e.parseString("200'foo'")[0]
        assert r.msg.val == "foo"

        r = e.parseString("200'\"foo\"'")[0]
        assert r.msg.val == "\"foo\""

        r = e.parseString('200"foo"')[0]
        assert r.msg.val == "foo"

        r = e.parseString('404')[0]
        assert r.msg.val == "Not Found"

        r = e.parseString('10')[0]
        assert r.msg.val == "Unknown code"

    def test_internal_response(self):
        d = cStringIO.StringIO()
        s = rparse.InternalResponse(400, "foo")
        s.serve(d)


class TestDisconnects:
    def test_parse_response(self):
        assert (0, "disconnect") in rparse.parse_response({}, "400:d0").actions
        assert ("r", "disconnect") in rparse.parse_response({}, "400:dr").actions

    def test_at(self):
        e = rparse.DisconnectAt.expr()
        v = e.parseString("d0")[0]
        assert isinstance(v, rparse.DisconnectAt)
        assert v.value == 0

        v = e.parseString("d100")[0]
        assert v.value == 100

        e = rparse.DisconnectAt.expr()
        v = e.parseString("dr")[0]
        assert v.value == "r"


class TestInject:
    def test_parse_response(self):
        a = rparse.parse_response({}, "400:ir,@100").actions[0]
        assert a[0] == "r"
        assert a[1] == "inject"

        a = rparse.parse_response({}, "400:ia,@100").actions[0]
        assert a[0] == "a"
        assert a[1] == "inject"

    def test_at(self):
        e = rparse.InjectAt.expr()
        v = e.parseString("i0,'foo'")[0]
        assert v.value.val == "foo"
        assert v.offset == 0
        assert isinstance(v, rparse.InjectAt)

        v = e.parseString("ir,'foo'")[0]
        assert v.offset == "r"

    def test_serve(self):
        s = cStringIO.StringIO()
        r = rparse.parse_response({}, "400:i0,'foo'")
        assert r.serve(s)


class TestShortcuts:
    def test_parse_response(self):
        assert rparse.parse_response({}, "400:c'foo'").headers[0][0][:] == "Content-Type"
        assert rparse.parse_response({}, "400:l'foo'").headers[0][0][:] == "Location"


class TestPauses:
    def test_parse_response(self):
        e = rparse.PauseAt.expr()
        v = e.parseString("p10,10")[0]
        assert v.seconds == 10
        assert v.offset == 10

        v = e.parseString("pf,10")[0]
        assert v.seconds == "f"

        v = e.parseString("pf,r")[0]
        assert v.offset == "r"

        v = e.parseString("pf,a")[0]
        assert v.offset == "a"

    def test_request(self):
        r = rparse.parse_response({}, '400:p10,10')
        assert r.actions[0] == (10, "pause", 10)


class TestParseRequest:
    def test_err(self):
        tutils.raises(rparse.ParseException, rparse.parse_request, {}, 'GET')

    def test_simple(self):
        r = rparse.parse_request({}, 'GET:"/foo"')
        assert r.method == "GET"
        assert r.path == "/foo"
        r = rparse.parse_request({}, 'GET:/foo')
        assert r.path == "/foo"
        r = rparse.parse_request({}, 'GET:@1k')
        assert len(r.path) == 1024

    def test_render(self):
        s = cStringIO.StringIO()
        r = rparse.parse_request({}, "GET:'/foo'")
        assert r.serve(s)

    def test_str(self):
        r = rparse.parse_request({}, 'GET:"/foo"')
        assert str(r)


class TestParseResponse:
    def test_parse_err(self):
        tutils.raises(rparse.ParseException, rparse.parse_response, {}, "400:msg,b:")
        try:
            rparse.parse_response({}, "400'msg':b:")
        except rparse.ParseException, v:
            assert v.marked()
            assert str(v)

    def test_parse_header(self):
        r = rparse.parse_response({}, '400:h"foo"="bar"')
        assert utils.get_header("foo", r.headers)

    def test_parse_pause_before(self):
        r = rparse.parse_response({}, "400:p10,0")
        assert (0, "pause", 10) in r.actions

    def test_parse_pause_after(self):
        r = rparse.parse_response({}, "400:p10,a")
        assert ("a", "pause", 10) in r.actions

    def test_parse_pause_random(self):
        r = rparse.parse_response({}, "400:p10,r")
        assert ("r", "pause", 10) in r.actions

    def test_parse_stress(self):
        r = rparse.parse_response({}, "400:b@100g")
        assert r.length()


class TestWriteValues:
    def test_send_chunk(self):
        v = "foobarfoobar"
        for bs in range(1, len(v)+2):
            s = cStringIO.StringIO()
            rparse.send_chunk(s, v, bs, 0, len(v))
            assert s.getvalue() == v
            for start in range(len(v)):
                for end in range(len(v)):
                    s = cStringIO.StringIO()
                    rparse.send_chunk(s, v, bs, start, end)
                    assert s.getvalue() == v[start:end]

    def test_write_values_inject(self):
        tst = "foo"

        s = cStringIO.StringIO()
        rparse.write_values(s, [tst], [(0, "inject", "aaa")], blocksize=5)
        assert s.getvalue() == "aaafoo"

        s = cStringIO.StringIO()
        rparse.write_values(s, [tst], [(1, "inject", "aaa")], blocksize=5)
        assert s.getvalue() == "faaaoo"

        s = cStringIO.StringIO()
        rparse.write_values(s, [tst], [(1, "inject", "aaa")], blocksize=5)
        assert s.getvalue() == "faaaoo"

    def test_write_values_disconnects(self):
        s = cStringIO.StringIO()
        tst = "foo"*100
        rparse.write_values(s, [tst], [(0, "disconnect")], blocksize=5)
        assert not s.getvalue()

    def test_write_values(self):
        tst = "foobarvoing"
        s = cStringIO.StringIO()
        rparse.write_values(s, [tst], [])
        assert s.getvalue() == tst

        for bs in range(1, len(tst) + 2):
            for off in range(len(tst)):
                s = cStringIO.StringIO()
                rparse.write_values(s, [tst], [(off, "disconnect")], blocksize=bs)
                assert s.getvalue() == tst[:off]

    def test_write_values_pauses(self):
        tst = "".join(str(i) for i in range(10))
        for i in range(2, 10):
            s = cStringIO.StringIO()
            rparse.write_values(s, [tst], [(2, "pause", 0), (1, "pause", 0)], blocksize=i)
            assert s.getvalue() == tst

        for i in range(2, 10):
            s = cStringIO.StringIO()
            rparse.write_values(s, [tst], [(1, "pause", 0)], blocksize=i)
            assert s.getvalue() == tst

        tst = ["".join(str(i) for i in range(10))]*5
        for i in range(2, 10):
            s = cStringIO.StringIO()
            rparse.write_values(s, tst[:], [(1, "pause", 0)], blocksize=i)
            assert s.getvalue() == "".join(tst)


def test_ready_actions():
    x = [(0, 5)]
    assert rparse.ready_actions(100, x) == x

    x = [("r", 5)]
    ret = rparse.ready_actions(100, x)
    assert 0 <= ret[0][0] < 100

    x = [("a", "pause", 5)]
    ret = rparse.ready_actions(100, x)
    assert ret[0][0] > 100

    x = [(1, 5), (0, 5)]
    assert rparse.ready_actions(100, x) == sorted(x)


class TestResponse:
    def dummy_response(self):
        return rparse.parse_response({}, "400'msg'")

    def test_response(self):
        r = rparse.parse_response({}, "400'msg'")
        assert r.code == 400
        assert r.msg == "msg"

        r = rparse.parse_response({}, "400'msg':b@100b")
        assert r.msg == "msg"
        assert r.body[:]
        assert str(r)

    def test_render(self):
        s = cStringIO.StringIO()
        r = rparse.parse_response({}, "400'msg'")
        assert r.serve(s)

    def test_length(self):
        def testlen(x):
            s = cStringIO.StringIO()
            x.serve(s)
            assert x.length() == len(s.getvalue())
        testlen(rparse.parse_response({}, "400'msg'"))
        testlen(rparse.parse_response({}, "400'msg':h'foo'='bar'"))
        testlen(rparse.parse_response({}, "400'msg':h'foo'='bar':b@100b"))