Split language test suite: generators

Also remove some unused code
This commit is contained in:
Aldo Cortesi 2015-05-02 16:53:48 +12:00
parent 9109b3cc8c
commit 5b48c69a6a
4 changed files with 43 additions and 76 deletions

View File

@ -733,19 +733,3 @@ class _Message(object):
Sep = pp.Optional(pp.Literal(":")).suppress() Sep = pp.Optional(pp.Literal(":")).suppress()
def read_file(settings, s):
uf = settings.get("unconstrained_file_access")
sd = settings.get("staticdir")
if not sd:
raise exceptions.FileAccessDenied("File access disabled.")
sd = os.path.normpath(os.path.abspath(sd))
s = s[1:]
s = os.path.expanduser(s)
s = os.path.normpath(os.path.abspath(os.path.join(sd, s)))
if not uf and not s.startswith(sd):
raise exceptions.FileAccessDenied("File access outside of configured directory")
if not os.path.isfile(s):
raise exceptions.FileAccessDenied("File not readable")
return file(s, "rb").read()

View File

@ -347,7 +347,8 @@ class Pathoc(tcp.TCPClient):
""" """
Performs a single request. Performs a single request.
r: A language.http.Request object, or a string representing one request. r: A language.http.Request object, or a string representing one
request.
Returns Response if we have a non-ignored response. Returns Response if we have a non-ignored response.
@ -386,7 +387,8 @@ class Pathoc(tcp.TCPClient):
""" """
Performs a single request. Performs a single request.
r: A language.http.Request object, or a string representing one request. r: A language.http.Request object, or a string representing one
request.
Returns Response if we have a non-ignored response. Returns Response if we have a non-ignored response.

View File

@ -1,7 +1,7 @@
import os import os
import cStringIO import cStringIO
from libpathod import language from libpathod import language
from libpathod.language import generators, base, http, websockets, writer, exceptions from libpathod.language import base, http, websockets, writer, exceptions
import tutils import tutils
language.TESTING = True language.TESTING = True
@ -163,35 +163,6 @@ class TestMisc:
g = v.get_generator({}) g = v.get_generator({})
assert g[:] == "val" assert g[:] == "val"
def test_randomgenerator(self):
g = generators.RandomGenerator("bytes", 100)
assert repr(g)
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 = generators.LiteralGenerator("one")
assert repr(g)
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, "wb")
f.write("x"*10000)
f.close()
g = generators.FileGenerator(path)
assert len(g) == 10000
assert g[0] == "x"
assert g[-1] == "x"
assert g[0:5] == "xxxxx"
assert repr(g)
del g # remove all references to FileGenerator instance to close the file handle.
def test_value(self): def test_value(self):
assert base.Value.parseString("'val'")[0].val == "val" assert base.Value.parseString("'val'")[0].val == "val"
assert base.Value.parseString('"val"')[0].val == "val" assert base.Value.parseString('"val"')[0].val == "val"
@ -848,31 +819,3 @@ class TestResponse:
tutils.raises("no websocket key", r.resolve, language.Settings()) tutils.raises("no websocket key", r.resolve, language.Settings())
res = r.resolve(language.Settings(websocket_key="foo")) res = r.resolve(language.Settings(websocket_key="foo"))
assert res.code.string() == "101" assert res.code.string() == "101"
def test_read_file():
tutils.raises(exceptions.FileAccessDenied, base.read_file, {}, "=/foo")
p = tutils.test_data.path("data")
d = dict(staticdir=p)
assert base.read_file(d, "+./file").strip() == "testfile"
assert base.read_file(d, "+file").strip() == "testfile"
tutils.raises(
exceptions.FileAccessDenied,
base.read_file,
d,
"+./nonexistent"
)
tutils.raises(
exceptions.FileAccessDenied,
base.read_file,
d,
"+/nonexistent"
)
tutils.raises(
exceptions.FileAccessDenied,
base.read_file,
d,
"+../test_language.py"
)
d["unconstrained_file_access"] = True
assert base.read_file(d, "+../test_language.py")

View File

@ -0,0 +1,38 @@
import os
from libpathod.language import generators
import tutils
def test_randomgenerator():
g = generators.RandomGenerator("bytes", 100)
assert repr(g)
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():
g = generators.LiteralGenerator("one")
assert repr(g)
assert g[:] == "one"
assert g[1] == "n"
def test_filegenerator():
with tutils.tmpdir() as t:
path = os.path.join(t, "foo")
f = open(path, "wb")
f.write("x" * 10000)
f.close()
g = generators.FileGenerator(path)
assert len(g) == 10000
assert g[0] == "x"
assert g[-1] == "x"
assert g[0:5] == "xxxxx"
assert repr(g)
# remove all references to FileGenerator instance to close the file
# handle.
del g