Florian Weimer
2010-May-08 09:15 UTC
[Secure-testing-commits] r14637 - in lib/python: . sectracker sectracker_test
Author: fw Date: 2010-05-08 09:15:06 +0000 (Sat, 08 May 2010) New Revision: 14637 Added: lib/python/sectracker_test/ lib/python/sectracker_test/run.py lib/python/sectracker_test/test_regexpcase.py lib/python/sectracker_test/test_xpickle.py Modified: lib/python/sectracker/regexpcase.py lib/python/sectracker/xpickle.py Log: Create separate Python test suite This is necessary because we need some logic to set up the correct module search path (the Python intepreter does not even offer a command line flag for that). Modified: lib/python/sectracker/regexpcase.py ==================================================================--- lib/python/sectracker/regexpcase.py 2010-05-08 04:26:34 UTC (rev 14636) +++ lib/python/sectracker/regexpcase.py 2010-05-08 09:15:06 UTC (rev 14637) @@ -85,43 +85,3 @@ def rule(regexp): """Add a regular expression to the function, for the rule list""" return lambda f: (regexp, f) - -if __name__ == "__main__": - import unittest - - class TestRegexpCase(unittest.TestCase): - def testempty(self): - self.assertRaises(ValueError, RegexpCase, ()) - self.assertRaises(ValueError, RegexpCase, (), prefix="foo") - self.assertRaises(ValueError, RegexpCase, (), suffix="foo") - self.assertRaises(ValueError, RegexpCase, (), default="foo") - self.assertRaises(ValueError, RegexpCase, (("two", 2)), - prefix="(f)oo") - self.assertRaises(ValueError, RegexpCase, (("two", 2)), - suffix="(f)oo") - - def teststrings(self): - rc = RegexpCase((("two", 2), - ("three", 3), - ("five", 5))) - self.assertEqual(2, rc["two"]) - self.assertEqual(3, rc["three"]) - self.assertEqual(5, rc["five"]) - self.assertEqual(None, rc["seven"]) - self.assertEquals((None, None), rc.match("seven")) - self.assertRaises(TypeError, rc.__call__, ()) - - def testcallstrings(self): - rc = RegexpCase((("(two)", lambda groups, x: (groups, x)), - ("three", lambda groups, x: (groups, x)), - ("f(i)v(e)", lambda groups, x : (groups, x)))) - self.assertEqual((("two",), -2), rc("two", -2)) - self.assertEqual(((), -3), rc("three", -3)) - self.assertEqual((tuple("ie"), -5), rc("five", -5)) - self.assertEqual(None, rc("seven", -1)) - def testcallstringsdefault(self): - rc = RegexpCase([("f(i)v(e)", lambda groups, x : (groups, x))], - default=lambda key, x: (key, x)) - self.assertEqual(("seven", -1), rc("seven", -1)) - - unittest.main() Modified: lib/python/sectracker/xpickle.py ==================================================================--- lib/python/sectracker/xpickle.py 2010-05-08 04:26:34 UTC (rev 14636) +++ lib/python/sectracker/xpickle.py 2010-05-08 09:15:06 UTC (rev 14637) @@ -103,22 +103,3 @@ The function takes two arguments, the file name and a file object. file_type is an arbitrary string, also useful for versioninging.""" return lambda f: _wraploader(file_type, f) - -def _test(): - with _tempfile.NamedTemporaryFile() as t: - try: - data = "foo bar baz\n" - t.write(data) - t.flush() - - l = _wraploader("foo", lambda p, f: f.read()) - assert l(t.name) == data - assert l(t.name) == data - t.write(data) - t.flush() - assert l(t.name) == (data + data) - finally: - safeunlink(t.name + EXTENSION) - -if __name__ == "__main__": - _test() Added: lib/python/sectracker_test/run.py ==================================================================--- lib/python/sectracker_test/run.py (rev 0) +++ lib/python/sectracker_test/run.py 2010-05-08 09:15:06 UTC (rev 14637) @@ -0,0 +1,57 @@ +# sectracker_tests/run.py -- run Python tests with the correct search path +# Copyright (C) 2010 Florian Weimer <fw at deneb.enyo.de> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +if __name__ != "__main__": + raise Exception("run must be executed directly") + +import os.path +import subprocess +import sys + +def pathsetup(): + prefix = sys.path[0] + trailer = "/sectracker_test" + if not os.path.exists(prefix + "/run.py") \ + or prefix[-len(trailer):] != trailer: + raise Exception("cannot find path to ourselves") + path = sys.path[:] + path[0] = prefix[:-len(trailer)] + return (prefix, path) +(ourpath, pythonpath) = pathsetup() +os.chdir(ourpath + "/..") + +env = {} +env.update(os.environ) +env["PYTHONPATH"] = ":".join(pythonpath) + +files = os.listdir(ourpath) +files.sort() +errors = False +for name in files: + if name[-3:] != ".py" or name == "run.py": + continue + fullpath = "%s/%s" % (ourpath, name) + print "* Running", name + p = subprocess.Popen(("python", "--", fullpath), env=env) + ret = p.wait() + if ret != 0: + print "Test exited with status", ret + print + errors = errors or ret != 0 +if errors: + print "ERROR: some tests aborted with errors" + sys.exit(1) Added: lib/python/sectracker_test/test_regexpcase.py ==================================================================--- lib/python/sectracker_test/test_regexpcase.py (rev 0) +++ lib/python/sectracker_test/test_regexpcase.py 2010-05-08 09:15:06 UTC (rev 14637) @@ -0,0 +1,57 @@ +# Tests for sectracker.regexpcase +# Copyright (C) 2009, 2010 Florian Weimer <fw at deneb.enyo.de> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +import unittest + +from sectracker.regexpcase import * + +class TestRegexpCase(unittest.TestCase): + def testempty(self): + self.assertRaises(ValueError, RegexpCase, ()) + self.assertRaises(ValueError, RegexpCase, (), prefix="foo") + self.assertRaises(ValueError, RegexpCase, (), suffix="foo") + self.assertRaises(ValueError, RegexpCase, (), default="foo") + self.assertRaises(ValueError, RegexpCase, (("two", 2)), + prefix="(f)oo") + self.assertRaises(ValueError, RegexpCase, (("two", 2)), + suffix="(f)oo") + + def teststrings(self): + rc = RegexpCase((("two", 2), + ("three", 3), + ("five", 5))) + self.assertEqual(2, rc["two"]) + self.assertEqual(3, rc["three"]) + self.assertEqual(5, rc["five"]) + self.assertEqual(None, rc["seven"]) + self.assertEquals((None, None), rc.match("seven")) + self.assertRaises(TypeError, rc.__call__, ()) + + def testcallstrings(self): + rc = RegexpCase((("(two)", lambda groups, x: (groups, x)), + ("three", lambda groups, x: (groups, x)), + ("f(i)v(e)", lambda groups, x : (groups, x)))) + self.assertEqual((("two",), -2), rc("two", -2)) + self.assertEqual(((), -3), rc("three", -3)) + self.assertEqual((tuple("ie"), -5), rc("five", -5)) + self.assertEqual(None, rc("seven", -1)) + def testcallstringsdefault(self): + rc = RegexpCase([("f(i)v(e)", lambda groups, x : (groups, x))], + default=lambda key, x: (key, x)) + self.assertEqual(("seven", -1), rc("seven", -1)) + +unittest.main() Added: lib/python/sectracker_test/test_xpickle.py ==================================================================--- lib/python/sectracker_test/test_xpickle.py (rev 0) +++ lib/python/sectracker_test/test_xpickle.py 2010-05-08 09:15:06 UTC (rev 14637) @@ -0,0 +1,36 @@ +# Tests for sectracker.xpickle +# Copyright (C) 2010 Florian Weimer <fw at deneb.enyo.de> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +from __future__ import with_statement + +import tempfile +import sectracker.xpickle as x + +with tempfile.NamedTemporaryFile() as t: + try: + data = "foo bar baz\n" + t.write(data) + t.flush() + + l = x._wraploader("foo", lambda p, f: f.read()) + assert l(t.name) == data + assert l(t.name) == data + t.write(data) + t.flush() + assert l(t.name) == (data + data) + finally: + x.safeunlink(t.name + x.EXTENSION)