From 5b48c69a6a0f1eeac9e8981d59d556e565163fba Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 2 May 2015 16:53:48 +1200 Subject: [PATCH] Split language test suite: generators Also remove some unused code --- libpathod/language/base.py | 16 --------- libpathod/pathoc.py | 6 ++-- test/test_language.py | 59 +------------------------------- test/test_language_generators.py | 38 ++++++++++++++++++++ 4 files changed, 43 insertions(+), 76 deletions(-) create mode 100644 test/test_language_generators.py diff --git a/libpathod/language/base.py b/libpathod/language/base.py index 407d5473a..6de8c086e 100644 --- a/libpathod/language/base.py +++ b/libpathod/language/base.py @@ -733,19 +733,3 @@ class _Message(object): 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() diff --git a/libpathod/pathoc.py b/libpathod/pathoc.py index e874412da..53ba44471 100644 --- a/libpathod/pathoc.py +++ b/libpathod/pathoc.py @@ -347,7 +347,8 @@ class Pathoc(tcp.TCPClient): """ 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. @@ -386,7 +387,8 @@ class Pathoc(tcp.TCPClient): """ 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. diff --git a/test/test_language.py b/test/test_language.py index 514c2180f..f72e677bf 100644 --- a/test/test_language.py +++ b/test/test_language.py @@ -1,7 +1,7 @@ import os import cStringIO 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 language.TESTING = True @@ -163,35 +163,6 @@ class TestMisc: g = v.get_generator({}) 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): 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()) res = r.resolve(language.Settings(websocket_key="foo")) 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") diff --git a/test/test_language_generators.py b/test/test_language_generators.py new file mode 100644 index 000000000..3db1ac91d --- /dev/null +++ b/test/test_language_generators.py @@ -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