2014-10-25 01:24:05 +00:00
|
|
|
import os
|
|
|
|
import cStringIO
|
2015-05-02 04:17:00 +00:00
|
|
|
from libpathod import language
|
2015-05-02 04:53:48 +00:00
|
|
|
from libpathod.language import base, http, websockets, writer, exceptions
|
2012-06-09 03:08:51 +00:00
|
|
|
import tutils
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2012-04-28 10:28:28 +00:00
|
|
|
|
2014-10-25 03:20:23 +00:00
|
|
|
def parse_request(s):
|
|
|
|
return language.parse_requests(s)[0]
|
|
|
|
|
|
|
|
|
2015-05-02 09:27:11 +00:00
|
|
|
def test_caseless_literal():
|
|
|
|
class CL(base.CaselessLiteral):
|
|
|
|
TOK = "foo"
|
|
|
|
v = CL("foo")
|
|
|
|
assert v.expr()
|
|
|
|
assert v.values(language.Settings())
|
2015-04-23 19:35:17 +00:00
|
|
|
|
|
|
|
|
2012-10-23 22:32:53 +00:00
|
|
|
class TestValueNakedLiteral:
|
|
|
|
def test_expr(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.ValueNakedLiteral("foo")
|
2012-10-23 22:32:53 +00:00
|
|
|
assert v.expr()
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2012-10-23 22:32:53 +00:00
|
|
|
def test_spec(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.ValueNakedLiteral("foo")
|
2012-10-23 22:32:53 +00:00
|
|
|
assert v.spec() == repr(v) == "foo"
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.ValueNakedLiteral("f\x00oo")
|
2012-10-23 22:32:53 +00:00
|
|
|
assert v.spec() == repr(v) == r"f\x00oo"
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2012-04-28 01:16:51 +00:00
|
|
|
|
2012-10-23 22:32:53 +00:00
|
|
|
class TestValueLiteral:
|
|
|
|
def test_espr(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.ValueLiteral("foo")
|
2012-04-28 00:42:03 +00:00
|
|
|
assert v.expr()
|
2012-07-21 02:12:45 +00:00
|
|
|
assert v.val == "foo"
|
|
|
|
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.ValueLiteral("foo\n")
|
2012-07-21 02:12:45 +00:00
|
|
|
assert v.expr()
|
|
|
|
assert v.val == "foo\n"
|
2012-07-23 07:25:57 +00:00
|
|
|
assert repr(v)
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2012-10-23 22:32:53 +00:00
|
|
|
def test_spec(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.ValueLiteral("foo")
|
2014-10-25 23:56:28 +00:00
|
|
|
assert v.spec() == r"'foo'"
|
2012-10-23 22:32:53 +00:00
|
|
|
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.ValueLiteral("f\x00oo")
|
2014-10-25 23:56:28 +00:00
|
|
|
assert v.spec() == repr(v) == r"'f\x00oo'"
|
|
|
|
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.ValueLiteral("\"")
|
2014-10-25 23:56:28 +00:00
|
|
|
assert v.spec() == repr(v) == '\'"\''
|
2012-10-23 22:32:53 +00:00
|
|
|
|
2014-10-26 03:27:25 +00:00
|
|
|
def roundtrip(self, spec):
|
2015-05-02 04:17:00 +00:00
|
|
|
e = base.ValueLiteral.expr()
|
|
|
|
v = base.ValueLiteral(spec)
|
2014-10-26 03:27:25 +00:00
|
|
|
v2 = e.parseString(v.spec())
|
|
|
|
assert v.val == v2[0].val
|
|
|
|
assert v.spec() == v2[0].spec()
|
|
|
|
|
|
|
|
def test_roundtrip(self):
|
|
|
|
self.roundtrip("'")
|
|
|
|
self.roundtrip('\'')
|
|
|
|
self.roundtrip("a")
|
|
|
|
self.roundtrip("\"")
|
|
|
|
self.roundtrip(r"\\")
|
|
|
|
self.roundtrip("200:b'foo':i23,'\\''")
|
2012-10-30 20:32:21 +00:00
|
|
|
|
2012-10-23 22:32:53 +00:00
|
|
|
|
|
|
|
class TestValueGenerate:
|
|
|
|
def test_basic(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.Value.parseString("@10b")[0]
|
2012-10-23 22:32:53 +00:00
|
|
|
assert v.usize == 10
|
|
|
|
assert v.unit == "b"
|
|
|
|
assert v.bytes() == 10
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.Value.parseString("@10")[0]
|
2012-10-23 22:32:53 +00:00
|
|
|
assert v.unit == "b"
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.Value.parseString("@10k")[0]
|
2012-10-23 22:32:53 +00:00
|
|
|
assert v.bytes() == 10240
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.Value.parseString("@10g")[0]
|
2012-10-23 22:32:53 +00:00
|
|
|
assert v.bytes() == 1024**3 * 10
|
2012-06-28 22:42:15 +00:00
|
|
|
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.Value.parseString("@10g,digits")[0]
|
2012-10-23 22:32:53 +00:00
|
|
|
assert v.datatype == "digits"
|
|
|
|
g = v.get_generator({})
|
|
|
|
assert g[:100]
|
|
|
|
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.Value.parseString("@10,digits")[0]
|
2012-10-23 22:32:53 +00:00
|
|
|
assert v.unit == "b"
|
|
|
|
assert v.datatype == "digits"
|
|
|
|
|
|
|
|
def test_spec(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.ValueGenerate(1, "b", "bytes")
|
2012-10-23 22:32:53 +00:00
|
|
|
assert v.spec() == repr(v) == "@1"
|
|
|
|
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.ValueGenerate(1, "k", "bytes")
|
2012-10-23 22:32:53 +00:00
|
|
|
assert v.spec() == repr(v) == "@1k"
|
|
|
|
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.ValueGenerate(1, "k", "ascii")
|
2012-10-23 22:32:53 +00:00
|
|
|
assert v.spec() == repr(v) == "@1k,ascii"
|
|
|
|
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.ValueGenerate(1, "b", "ascii")
|
2012-10-23 22:32:53 +00:00
|
|
|
assert v.spec() == repr(v) == "@1,ascii"
|
|
|
|
|
2012-10-30 20:32:21 +00:00
|
|
|
def test_freeze(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.ValueGenerate(100, "b", "ascii")
|
2015-04-22 03:49:17 +00:00
|
|
|
f = v.freeze(language.Settings())
|
2012-10-30 20:32:21 +00:00
|
|
|
assert len(f.val) == 100
|
|
|
|
|
2012-10-23 22:32:53 +00:00
|
|
|
|
|
|
|
class TestValueFile:
|
2012-04-28 01:16:51 +00:00
|
|
|
def test_file_value(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.Value.parseString("<'one two'")[0]
|
2012-04-28 10:28:28 +00:00
|
|
|
assert str(v)
|
2012-04-28 01:16:51 +00:00
|
|
|
assert v.path == "one two"
|
|
|
|
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.Value.parseString("<path")[0]
|
2012-04-28 01:16:51 +00:00
|
|
|
assert v.path == "path"
|
|
|
|
|
2012-10-23 22:32:53 +00:00
|
|
|
def test_access_control(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.Value.parseString("<path")[0]
|
2012-06-09 03:08:51 +00:00
|
|
|
with tutils.tmpdir() as t:
|
|
|
|
p = os.path.join(t, "path")
|
2013-12-16 03:47:29 +00:00
|
|
|
with open(p, "wb") as f:
|
|
|
|
f.write("x" * 10000)
|
2012-04-28 01:16:51 +00:00
|
|
|
|
2015-04-22 03:49:17 +00:00
|
|
|
assert v.get_generator(language.Settings(staticdir=t))
|
2012-04-28 01:16:51 +00:00
|
|
|
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.Value.parseString("<path2")[0]
|
2015-04-20 03:42:33 +00:00
|
|
|
tutils.raises(
|
2015-05-02 04:17:00 +00:00
|
|
|
exceptions.FileAccessDenied,
|
2015-04-22 03:49:17 +00:00
|
|
|
v.get_generator,
|
|
|
|
language.Settings(staticdir=t)
|
|
|
|
)
|
|
|
|
tutils.raises(
|
|
|
|
"access disabled",
|
|
|
|
v.get_generator,
|
|
|
|
language.Settings()
|
2015-04-20 03:42:33 +00:00
|
|
|
)
|
2012-07-22 11:46:56 +00:00
|
|
|
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.Value.parseString("</outside")[0]
|
2015-04-22 03:49:17 +00:00
|
|
|
tutils.raises(
|
|
|
|
"outside",
|
|
|
|
v.get_generator,
|
|
|
|
language.Settings(staticdir=t)
|
|
|
|
)
|
2012-04-28 01:16:51 +00:00
|
|
|
|
2012-10-23 22:32:53 +00:00
|
|
|
def test_spec(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.Value.parseString("<'one two'")[0]
|
|
|
|
v2 = base.Value.parseString(v.spec())[0]
|
2012-10-23 22:32:53 +00:00
|
|
|
assert v2.path == "one two"
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2012-10-30 20:32:21 +00:00
|
|
|
def test_freeze(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.Value.parseString("<'one two'")[0]
|
2012-10-30 20:32:21 +00:00
|
|
|
v2 = v.freeze({})
|
|
|
|
assert v2.path == v.path
|
|
|
|
|
2012-10-23 22:32:53 +00:00
|
|
|
|
|
|
|
class TestMisc:
|
|
|
|
def test_generators(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
v = base.Value.parseString("'val'")[0]
|
2012-04-28 00:42:03 +00:00
|
|
|
g = v.get_generator({})
|
2012-10-23 22:32:53 +00:00
|
|
|
assert g[:] == "val"
|
2012-04-28 00:42:03 +00:00
|
|
|
|
|
|
|
def test_value(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
assert base.Value.parseString("'val'")[0].val == "val"
|
|
|
|
assert base.Value.parseString('"val"')[0].val == "val"
|
|
|
|
assert base.Value.parseString('"\'val\'"')[0].val == "'val'"
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2015-05-02 09:27:11 +00:00
|
|
|
def test_simplevalue(self):
|
|
|
|
e = base.SimpleValue.expr()
|
2012-06-24 06:38:22 +00:00
|
|
|
assert e.parseString('"/foo"')[0].value.val == "/foo"
|
2012-07-24 00:18:14 +00:00
|
|
|
|
2015-05-02 09:27:11 +00:00
|
|
|
v = base.SimpleValue("/foo")
|
2012-10-30 20:32:21 +00:00
|
|
|
assert v.value.val == "/foo"
|
|
|
|
|
|
|
|
v = e.parseString("@100")[0]
|
|
|
|
v2 = v.freeze({})
|
|
|
|
v3 = v2.freeze({})
|
|
|
|
assert v2.value.val == v3.value.val
|
2012-10-30 20:48:55 +00:00
|
|
|
assert len(v2.value.val) == 100
|
2012-10-30 20:32:21 +00:00
|
|
|
|
|
|
|
s = v.spec()
|
|
|
|
assert s == v.expr().parseString(s)[0].spec()
|
2012-10-29 03:31:35 +00:00
|
|
|
|
2012-04-28 00:42:03 +00:00
|
|
|
def test_body(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
e = base.Body.expr()
|
2012-04-28 05:12:39 +00:00
|
|
|
v = e.parseString("b'foo'")[0]
|
2012-04-28 00:42:03 +00:00
|
|
|
assert v.value.val == "foo"
|
|
|
|
|
2012-04-29 02:20:27 +00:00
|
|
|
v = e.parseString("b@100")[0]
|
2012-10-23 22:32:53 +00:00
|
|
|
assert str(v.value) == "@100"
|
2012-10-30 20:32:21 +00:00
|
|
|
v2 = v.freeze({})
|
|
|
|
v3 = v2.freeze({})
|
|
|
|
assert v2.value.val == v3.value.val
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2012-04-29 02:20:27 +00:00
|
|
|
v = e.parseString("b@100g,digits", parseAll=True)[0]
|
2012-04-28 00:42:03 +00:00
|
|
|
assert v.value.datatype == "digits"
|
2012-04-29 02:20:27 +00:00
|
|
|
assert str(v.value) == "@100g,digits"
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2012-10-29 03:31:35 +00:00
|
|
|
s = v.spec()
|
|
|
|
assert s == e.parseString(s)[0].spec()
|
|
|
|
|
2014-10-25 21:50:32 +00:00
|
|
|
def test_pathodspec(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
e = base.PathodSpec.expr()
|
2014-10-25 21:50:32 +00:00
|
|
|
v = e.parseString("s'200'")[0]
|
|
|
|
assert v.value.val == "200"
|
|
|
|
tutils.raises(
|
|
|
|
language.ParseException,
|
|
|
|
e.parseString,
|
|
|
|
"s'foo'"
|
|
|
|
)
|
|
|
|
|
|
|
|
v = e.parseString('s"200:b@1"')[0]
|
|
|
|
assert "@1" in v.spec()
|
|
|
|
f = v.freeze({})
|
|
|
|
assert "@1" not in f.spec()
|
|
|
|
|
2014-10-25 23:56:28 +00:00
|
|
|
def test_pathodspec_freeze(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
e = base.PathodSpec(
|
|
|
|
base.ValueLiteral(
|
2014-10-26 03:27:25 +00:00
|
|
|
"200:b'foo':i10,'\\''".encode(
|
|
|
|
"string_escape"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
assert e.freeze({})
|
2015-04-18 04:36:45 +00:00
|
|
|
assert e.values({})
|
2014-10-25 21:50:32 +00:00
|
|
|
|
2012-10-29 03:31:35 +00:00
|
|
|
def test_code(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
e = base.Code.expr()
|
2012-10-29 03:31:35 +00:00
|
|
|
v = e.parseString("200")[0]
|
|
|
|
assert v.string() == "200"
|
|
|
|
assert v.spec() == "200"
|
|
|
|
|
2012-10-30 20:32:21 +00:00
|
|
|
assert v.freeze({}).code == v.code
|
|
|
|
|
2012-10-29 03:31:35 +00:00
|
|
|
def test_reason(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
e = base.Reason.expr()
|
2012-10-29 03:31:35 +00:00
|
|
|
v = e.parseString("m'msg'")[0]
|
|
|
|
assert v.value.val == "msg"
|
|
|
|
|
|
|
|
s = v.spec()
|
|
|
|
assert s == e.parseString(s)[0].spec()
|
|
|
|
|
2012-10-30 20:32:21 +00:00
|
|
|
v = e.parseString("m@100")[0]
|
|
|
|
v2 = v.freeze({})
|
|
|
|
v3 = v2.freeze({})
|
|
|
|
assert v2.value.val == v3.value.val
|
|
|
|
|
2012-10-29 03:31:35 +00:00
|
|
|
|
|
|
|
class TestHeaders:
|
2012-04-28 00:42:03 +00:00
|
|
|
def test_header(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
e = base.Header.expr()
|
2012-04-28 05:28:40 +00:00
|
|
|
v = e.parseString("h'foo'='bar'")[0]
|
2012-04-28 00:42:03 +00:00
|
|
|
assert v.key.val == "foo"
|
|
|
|
assert v.value.val == "bar"
|
|
|
|
|
2012-10-29 03:31:35 +00:00
|
|
|
v2 = e.parseString(v.spec())[0]
|
|
|
|
assert v2.key.val == v.key.val
|
|
|
|
assert v2.value.val == v.value.val
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2012-10-29 03:31:35 +00:00
|
|
|
s = v.spec()
|
|
|
|
assert s == e.parseString(s)[0].spec()
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2012-10-30 20:32:21 +00:00
|
|
|
def test_header_freeze(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
e = base.Header.expr()
|
2012-10-30 20:32:21 +00:00
|
|
|
v = e.parseString("h@10=@10'")[0]
|
|
|
|
v2 = v.freeze({})
|
|
|
|
v3 = v2.freeze({})
|
|
|
|
assert v2.key.val == v3.key.val
|
|
|
|
assert v2.value.val == v3.value.val
|
|
|
|
|
2012-10-29 03:31:35 +00:00
|
|
|
def test_ctype_shortcut(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
e = base.ShortcutContentType.expr()
|
2012-10-29 03:31:35 +00:00
|
|
|
v = e.parseString("c'foo'")[0]
|
|
|
|
assert v.key.val == "Content-Type"
|
|
|
|
assert v.value.val == "foo"
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2012-10-29 03:31:35 +00:00
|
|
|
s = v.spec()
|
|
|
|
assert s == e.parseString(s)[0].spec()
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2015-05-02 04:17:00 +00:00
|
|
|
e = base.ShortcutContentType.expr()
|
2012-10-30 20:32:21 +00:00
|
|
|
v = e.parseString("c@100")[0]
|
|
|
|
v2 = v.freeze({})
|
|
|
|
v3 = v2.freeze({})
|
|
|
|
assert v2.value.val == v3.value.val
|
|
|
|
|
2012-10-29 03:31:35 +00:00
|
|
|
def test_location_shortcut(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
e = base.ShortcutLocation.expr()
|
2012-10-29 03:31:35 +00:00
|
|
|
v = e.parseString("l'foo'")[0]
|
|
|
|
assert v.key.val == "Location"
|
|
|
|
assert v.value.val == "foo"
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2012-10-29 03:31:35 +00:00
|
|
|
s = v.spec()
|
|
|
|
assert s == e.parseString(s)[0].spec()
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2015-05-02 04:17:00 +00:00
|
|
|
e = base.ShortcutLocation.expr()
|
2012-10-30 20:32:21 +00:00
|
|
|
v = e.parseString("l@100")[0]
|
|
|
|
v2 = v.freeze({})
|
|
|
|
v3 = v2.freeze({})
|
|
|
|
assert v2.value.val == v3.value.val
|
|
|
|
|
2012-11-15 22:31:04 +00:00
|
|
|
def test_shortcuts(self):
|
2014-10-24 19:18:39 +00:00
|
|
|
assert language.parse_response("400:c'foo'").headers[0].key.val == "Content-Type"
|
|
|
|
assert language.parse_response("400:l'foo'").headers[0].key.val == "Location"
|
2012-11-16 01:41:02 +00:00
|
|
|
|
2014-10-25 03:20:23 +00:00
|
|
|
assert 'Android' in parse_request("get:/:ua").headers[0].value.val
|
|
|
|
assert parse_request("get:/:ua").headers[0].key.val == "User-Agent"
|
2012-04-28 00:42:03 +00:00
|
|
|
|
|
|
|
|
2012-11-15 22:31:04 +00:00
|
|
|
class TestShortcutUserAgent:
|
|
|
|
def test_location_shortcut(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
e = base.ShortcutUserAgent.expr()
|
2012-11-15 22:31:04 +00:00
|
|
|
v = e.parseString("ua")[0]
|
|
|
|
assert "Android" in str(v.value)
|
|
|
|
assert v.spec() == "ua"
|
|
|
|
assert v.key.val == "User-Agent"
|
|
|
|
|
|
|
|
v = e.parseString("u'foo'")[0]
|
|
|
|
assert "foo" in str(v.value)
|
|
|
|
assert "foo" in v.spec()
|
|
|
|
|
|
|
|
v = e.parseString("u@100'")[0]
|
|
|
|
assert len(str(v.freeze({}).value)) > 100
|
|
|
|
v2 = v.freeze({})
|
|
|
|
v3 = v2.freeze({})
|
|
|
|
assert v2.value.val == v3.value.val
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2012-10-30 20:32:21 +00:00
|
|
|
|
2012-10-04 22:23:30 +00:00
|
|
|
class Test_Action:
|
|
|
|
def test_cmp(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
a = base.DisconnectAt(0)
|
|
|
|
b = base.DisconnectAt(1)
|
|
|
|
c = base.DisconnectAt(0)
|
2012-10-04 22:23:30 +00:00
|
|
|
assert a < b
|
|
|
|
assert a == c
|
|
|
|
l = [b, a]
|
|
|
|
l.sort()
|
|
|
|
assert l[0].offset == 0
|
|
|
|
|
2012-10-30 02:22:53 +00:00
|
|
|
def test_resolve(self):
|
2014-10-25 03:20:23 +00:00
|
|
|
r = parse_request('GET:"/foo"')
|
2015-05-02 04:17:00 +00:00
|
|
|
e = base.DisconnectAt("r")
|
2015-04-28 22:02:16 +00:00
|
|
|
ret = e.resolve({}, r)
|
2012-10-23 22:32:53 +00:00
|
|
|
assert isinstance(ret.offset, int)
|
|
|
|
|
2012-10-24 21:59:18 +00:00
|
|
|
def test_repr(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
e = base.DisconnectAt("r")
|
2012-10-24 21:59:18 +00:00
|
|
|
assert repr(e)
|
|
|
|
|
2012-10-30 20:32:21 +00:00
|
|
|
def test_freeze(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
l = base.DisconnectAt(5)
|
2012-10-30 20:32:21 +00:00
|
|
|
assert l.freeze({}).spec() == l.spec()
|
|
|
|
|
2012-10-04 22:23:30 +00:00
|
|
|
|
2012-06-09 03:08:51 +00:00
|
|
|
class TestDisconnects:
|
2012-06-24 05:01:04 +00:00
|
|
|
def test_parse_response(self):
|
2014-10-24 19:18:39 +00:00
|
|
|
a = language.parse_response("400:d0").actions[0]
|
2012-10-24 20:45:55 +00:00
|
|
|
assert a.spec() == "d0"
|
2014-10-24 19:18:39 +00:00
|
|
|
a = language.parse_response("400:dr").actions[0]
|
2012-10-24 20:45:55 +00:00
|
|
|
assert a.spec() == "dr"
|
2012-04-28 10:51:36 +00:00
|
|
|
|
2012-04-29 02:20:27 +00:00
|
|
|
def test_at(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
e = base.DisconnectAt.expr()
|
2012-04-29 02:20:27 +00:00
|
|
|
v = e.parseString("d0")[0]
|
2015-05-02 04:17:00 +00:00
|
|
|
assert isinstance(v, base.DisconnectAt)
|
2012-10-04 22:23:30 +00:00
|
|
|
assert v.offset == 0
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2012-04-29 02:20:27 +00:00
|
|
|
v = e.parseString("d100")[0]
|
2012-10-04 22:23:30 +00:00
|
|
|
assert v.offset == 100
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2015-05-02 04:17:00 +00:00
|
|
|
e = base.DisconnectAt.expr()
|
2012-04-28 00:42:03 +00:00
|
|
|
v = e.parseString("dr")[0]
|
2012-10-04 22:23:30 +00:00
|
|
|
assert v.offset == "r"
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2012-10-23 22:32:53 +00:00
|
|
|
def test_spec(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
assert base.DisconnectAt("r").spec() == "dr"
|
|
|
|
assert base.DisconnectAt(10).spec() == "d10"
|
2012-10-23 22:32:53 +00:00
|
|
|
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2012-07-20 11:36:39 +00:00
|
|
|
class TestInject:
|
|
|
|
def test_parse_response(self):
|
2014-10-24 19:18:39 +00:00
|
|
|
a = language.parse_response("400:ir,@100").actions[0]
|
2012-10-24 20:45:55 +00:00
|
|
|
assert a.offset == "r"
|
|
|
|
assert a.value.datatype == "bytes"
|
|
|
|
assert a.value.usize == 100
|
2012-07-20 11:36:39 +00:00
|
|
|
|
2014-10-24 19:18:39 +00:00
|
|
|
a = language.parse_response("400:ia,@100").actions[0]
|
2012-10-24 20:45:55 +00:00
|
|
|
assert a.offset == "a"
|
2012-07-20 11:47:34 +00:00
|
|
|
|
2012-07-20 11:36:39 +00:00
|
|
|
def test_at(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
e = base.InjectAt.expr()
|
2012-07-20 11:36:39 +00:00
|
|
|
v = e.parseString("i0,'foo'")[0]
|
|
|
|
assert v.value.val == "foo"
|
|
|
|
assert v.offset == 0
|
2015-05-02 04:17:00 +00:00
|
|
|
assert isinstance(v, base.InjectAt)
|
2012-07-20 11:36:39 +00:00
|
|
|
|
|
|
|
v = e.parseString("ir,'foo'")[0]
|
|
|
|
assert v.offset == "r"
|
|
|
|
|
2012-07-20 11:47:34 +00:00
|
|
|
def test_serve(self):
|
|
|
|
s = cStringIO.StringIO()
|
2014-10-24 19:18:39 +00:00
|
|
|
r = language.parse_response("400:i0,'foo'")
|
2012-10-30 01:46:18 +00:00
|
|
|
assert language.serve(r, s, {})
|
2012-07-20 11:47:34 +00:00
|
|
|
|
2012-10-23 22:32:53 +00:00
|
|
|
def test_spec(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
e = base.InjectAt.expr()
|
2012-10-23 22:32:53 +00:00
|
|
|
v = e.parseString("i0,'foo'")[0]
|
|
|
|
assert v.spec() == 'i0,"foo"'
|
2012-10-24 21:59:18 +00:00
|
|
|
|
2012-10-30 20:32:21 +00:00
|
|
|
def test_spec(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
e = base.InjectAt.expr()
|
2012-10-30 20:32:21 +00:00
|
|
|
v = e.parseString("i0,@100")[0]
|
|
|
|
v2 = v.freeze({})
|
|
|
|
v3 = v2.freeze({})
|
|
|
|
assert v2.value.val == v3.value.val
|
|
|
|
|
2012-04-28 10:51:36 +00:00
|
|
|
|
2012-06-09 03:08:51 +00:00
|
|
|
class TestPauses:
|
2012-06-24 05:01:04 +00:00
|
|
|
def test_parse_response(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
e = base.PauseAt.expr()
|
2012-04-29 02:20:27 +00:00
|
|
|
v = e.parseString("p10,10")[0]
|
|
|
|
assert v.seconds == 10
|
|
|
|
assert v.offset == 10
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2012-07-24 11:57:18 +00:00
|
|
|
v = e.parseString("p10,f")[0]
|
2012-04-29 02:20:27 +00:00
|
|
|
assert v.seconds == "f"
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2012-07-24 11:57:18 +00:00
|
|
|
v = e.parseString("pr,f")[0]
|
2012-04-29 02:20:27 +00:00
|
|
|
assert v.offset == "r"
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2012-07-24 11:57:18 +00:00
|
|
|
v = e.parseString("pa,f")[0]
|
2012-04-29 02:20:27 +00:00
|
|
|
assert v.offset == "a"
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2012-04-29 02:20:27 +00:00
|
|
|
def test_request(self):
|
2014-10-24 19:18:39 +00:00
|
|
|
r = language.parse_response('400:p10,10')
|
2012-10-24 20:45:55 +00:00
|
|
|
assert r.actions[0].spec() == "p10,10"
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2012-10-23 22:32:53 +00:00
|
|
|
def test_spec(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
assert base.PauseAt("r", 5).spec() == "pr,5"
|
|
|
|
assert base.PauseAt(0, 5).spec() == "p0,5"
|
|
|
|
assert base.PauseAt(0, "f").spec() == "p0,f"
|
2012-10-23 22:32:53 +00:00
|
|
|
|
2012-10-30 20:32:21 +00:00
|
|
|
def test_freeze(self):
|
2015-05-02 04:17:00 +00:00
|
|
|
l = base.PauseAt("r", 5)
|
2012-10-30 20:32:21 +00:00
|
|
|
assert l.freeze({}).spec() == l.spec()
|
2015-05-02 09:27:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_options_or_value():
|
|
|
|
class TT(base.OptionsOrValue):
|
|
|
|
options = [
|
|
|
|
"one",
|
|
|
|
"two",
|
|
|
|
"three"
|
|
|
|
]
|
|
|
|
e = TT.expr()
|
|
|
|
assert e.parseString("one")[0].value.val == "ONE"
|
|
|
|
assert e.parseString("'foo'")[0].value.val == "foo"
|
|
|
|
assert e.parseString("'get'")[0].value.val == "get"
|
|
|
|
|
|
|
|
assert e.parseString("one")[0].spec() == "one"
|
|
|
|
assert e.parseString("'foo'")[0].spec() == "'foo'"
|
|
|
|
|
|
|
|
s = e.parseString("one")[0].spec()
|
|
|
|
assert s == e.parseString(s)[0].spec()
|
|
|
|
|
|
|
|
s = e.parseString("'foo'")[0].spec()
|
|
|
|
assert s == e.parseString(s)[0].spec()
|
|
|
|
|
|
|
|
v = e.parseString("@100")[0]
|
|
|
|
v2 = v.freeze({})
|
|
|
|
v3 = v2.freeze({})
|
|
|
|
assert v2.value.val == v3.value.val
|