diff options
author | Nick Mathewson <nickm@torproject.org> | 2008-11-22 17:39:57 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2008-11-22 17:39:57 +0000 |
commit | 6c78fd8ff6b3d39fbae85103bda0651ca2f764d9 (patch) | |
tree | f392cef3d3e0b3810964d8f8a3f594229f8bf90d /lib/thandy/tests.py | |
parent | 06b83a522f6f05aed23b6dca4bfd4eeb6e6b057a (diff) |
more docs and tests
git-svn-id: file:///home/or/svnrepo/updater/trunk@17364 55e972cd-5a19-0410-ae62-a4d7a52db4cd
Diffstat (limited to 'lib/thandy/tests.py')
-rw-r--r-- | lib/thandy/tests.py | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/lib/thandy/tests.py b/lib/thandy/tests.py index e2fb767..924fc82 100644 --- a/lib/thandy/tests.py +++ b/lib/thandy/tests.py @@ -9,9 +9,25 @@ import thandy.keys import thandy.formats import thandy.repository import thandy.checkJson +import thandy.util import thandy.tests +def deltree(top): + for dirpath, dirnames, filenames in os.walk(top, topdown=False): + for f in filenames: + os.unlink(os.path.join(dirpath, f)) + for d in dirnames: + os.rmdir(os.path.join(dirpath, d)) + os.rmdir(top) + +def contents(fn, mode='rb'): + f = open(fn, mode) + try: + return f.read() + finally: + f.close() + class CanonicalEncodingTest(unittest.TestCase): def test_encode(self): enc = thandy.formats.encodeCanonical @@ -47,6 +63,59 @@ class CryptoTests(unittest.TestCase): ks2.load(passwd) self.assertEquals(key1.key.n, ks2.getKey(key1.getKeyID()).key.n) +class UtilTests(unittest.TestCase): + def setUp(self): + self._dir = tempfile.mkdtemp() + def tearDown(self): + deltree(self._dir) + + def test_replaceFile(self): + fn1 = os.path.join(self._dir, "File1") + S1="Why do you curtsey, commoner? I presumed this would be anonymous." + S2="I am simply guaranteeing your commitment to my anonymity." + # -- WIGU adventures, 24 March 2005. + thandy.util.replaceFile(fn1, S1) + self.assertEquals(contents(fn1), S1) + thandy.util.replaceFile(fn1, S2) + self.assertEquals(contents(fn1), S2) + + self.assertEquals(os.listdir(self._dir), [ "File1" ]) + + def test_moveFile(self): + d = self._dir + os.mkdir(os.path.join(d, "subdir")) + fn1 = os.path.join(d, "f1") + fn2 = os.path.join(d, "f2") + fn3 = os.path.join(d, "subdir", "f3") + S1="""We monitor all citizens constantly to detect insider baddies! + Isn't it wondersome?!""" + S2="""Wondersome yes... But could such a tactic instill a sense of + distrust and fear in a populace that is overwhelmingly true and + pious?""" + S3="""I think the fact that we are not currently under siege by + unscrupulous minions speaks for itself.""" + # -- WIGU adventures, 22 January 2004 + + thandy.util.replaceFile(fn1, S1) + thandy.util.replaceFile(fn2, S2) + thandy.util.replaceFile(fn3, S3) + + self.assertEquals(contents(fn1), S1) + self.assertTrue(os.path.exists(fn2)) + self.assertTrue(os.path.exists(fn3)) + + thandy.util.moveFile(fn2, fn1) + self.assertEquals(contents(fn1), S2) + self.assertFalse(os.path.exists(fn2)) + + thandy.util.moveFile(fn1, fn3) + self.assertEquals(contents(fn3), S2) + self.assertFalse(os.path.exists(fn1)) + + self.assertEquals(os.listdir(d), ["subdir"]) + self.assertEquals(os.listdir(os.path.join(d, "subdir")), ["f3"]) + + def suite(): suite = unittest.TestSuite() |