From 12868beb2183791facda0531c2f46f8f1ac75edc Mon Sep 17 00:00:00 2001 From: age Date: Tue, 22 Aug 2006 04:29:53 +0000 Subject: [PATCH] separated unittest --- pythonrewrite/bin2/CryptoBox.py | 71 +-------------------- pythonrewrite/bin2/cbox.log | 4 -- pythonrewrite/bin2/coding_guidelines.txt | 5 +- pythonrewrite/bin2/unittests.CryptoBox.py | 77 +++++++++++++++++++++++ 4 files changed, 82 insertions(+), 75 deletions(-) delete mode 100644 pythonrewrite/bin2/cbox.log create mode 100644 pythonrewrite/bin2/unittests.CryptoBox.py diff --git a/pythonrewrite/bin2/CryptoBox.py b/pythonrewrite/bin2/CryptoBox.py index 29bcf6d..5ec35c2 100644 --- a/pythonrewrite/bin2/CryptoBox.py +++ b/pythonrewrite/bin2/CryptoBox.py @@ -47,7 +47,7 @@ class CryptoBox: logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(module)s %(levelname)s %(message)s', filename='./cryptobox.log', - filemode='w') + filemode='a') self.log = logging.getLogger("CryptoBoxProps") self.log.info("loggingsystem is up'n running") ## from now on everything can be logged via self.log... @@ -279,73 +279,6 @@ class CryptoBoxProps(CryptoBox): return [] - -# *************** test class ********************* - -class CryptoBoxPropsTest(unittest.TestCase): - import filehandling - - configFile = filehandling.gen_temp_file("cbox-test.conf") - nameDBFile = filehandling.gen_temp_file("cryptobox_names.db") - logFile = filehandling.gen_temp_file("cryptobox.log") - tmpdir = filehandling.gen_temp_dir("cryptobox-mnt") - configContent = """ -[Main] -AllowedDevices = /dev/loop -DefaultVolumePrefix = "Data " -DataDir = /tmp -NameDatabase = cryptobox_names.db -[System] -User = 1000 -Group = 1000 -MountParentDir = /tmp/mnt -DefaultCipher = aes-cbc-essiv:sha256 -[Log] -Level = debug -Facility = file -Destination = /tmp/cryptobox.log -[Programs] -blkid = /sbin/blkid -cryptsetup = /sbin/cryptsetup -""" - - def setUp(self): - if not os.path.exists(self.tmpdir): os.mkdir(self.tmpdir) - self.filehandling.write_file(self.configFile,self.configContent) - - - def tearDown(self): - if os.path.exists(self.configFile): os.remove(self.configFile) - if os.path.exists(self.logFile): os.remove(self.logFile) - if os.path.exists(self.nameDBFile): os.remove(self.nameDBFile) - if os.path.exists(self.tmpdir): os.rmdir(self.tmpdir) - - - def testConfigFile(self): - # RFC: which 'age' did a commit with a broken unittest? [l] - # the class has changed, it reads the formerly given - # configfile now out of the parent class - # unittest works for me [a] - CryptoBoxProps() - self.assertTrue(os.path.exists(self.nameDBFile)) - self.assertTrue(os.path.exists(self.logFile)) - - - def testDeviceCheck(self): - cb = CryptoBoxProps() - self.assertTrue(cb.isDeviceAllowed("/dev/loop")) - self.assertTrue(cb.isDeviceAllowed("/dev/loop1")) - self.assertTrue(cb.isDeviceAllowed("/dev/loop/urgd")) - self.assertFalse(cb.isDeviceAllowed("/dev/hda")) - self.assertFalse(cb.isDeviceAllowed("/dev/loopa/../hda")) - self.assertTrue(cb.isDeviceAllowed("/dev/usb/../loop1")) - self.assertFalse(cb.isDeviceAllowed("/")) - - "a lot of tests are still missing - how can we provide a prepared environment?" - - # ********************* run unittest **************************** - if __name__ == "__main__": - unittest.main() - #cb = CryptoBoxProps() + cb = CryptoBox() diff --git a/pythonrewrite/bin2/cbox.log b/pythonrewrite/bin2/cbox.log deleted file mode 100644 index 8546490..0000000 --- a/pythonrewrite/bin2/cbox.log +++ /dev/null @@ -1,4 +0,0 @@ -[CryptoBox] - 6 -[CryptoBox] - 6 -[CryptoBox] - 6 -[CryptoBox] - 6 diff --git a/pythonrewrite/bin2/coding_guidelines.txt b/pythonrewrite/bin2/coding_guidelines.txt index 6345a0d..a6fb47c 100644 --- a/pythonrewrite/bin2/coding_guidelines.txt +++ b/pythonrewrite/bin2/coding_guidelines.txt @@ -12,6 +12,7 @@ error handling: - unspecific error handling is evil (try: "grep -r except: .") unit testing: - - write a unittest and then write the relating code until the unittest stops failing :) - - commits with broken unit tests are evil (options: fix or disable) + - first write a unittest and then write the relating code until the unittest stops failing :) + - 'unittests.ClassName.py' should contain all tests for 'ClassName.py' + - commits with broken unit tests are evil (fix or disable the code (not the test ;) )) diff --git a/pythonrewrite/bin2/unittests.CryptoBox.py b/pythonrewrite/bin2/unittests.CryptoBox.py new file mode 100644 index 0000000..79e804b --- /dev/null +++ b/pythonrewrite/bin2/unittests.CryptoBox.py @@ -0,0 +1,77 @@ +import unittest + +class CryptoBoxPropsDeviceTests(unittest.TestCase): + import CryptoBox + cb = CryptoBox.CryptoBoxProps() + + def testAllowedDevices(self): + '''isDeviceAllowed should accept permitted devices''' + self.assertTrue(self.cb.isDeviceAllowed("/dev/loop")) + self.assertTrue(self.cb.isDeviceAllowed("/dev/loop1")) + self.assertTrue(self.cb.isDeviceAllowed("/dev/loop/urgd")) + self.assertTrue(self.cb.isDeviceAllowed("/dev/usb/../loop1")) + + def testDeniedDevices(self): + '''isDeviceAllowed should fail with not explecitly allowed devices''' + self.assertFalse(self.cb.isDeviceAllowed("/dev/hda")) + self.assertFalse(self.cb.isDeviceAllowed("/dev/loopa/../hda")) + self.assertFalse(self.cb.isDeviceAllowed("/")) + + +class CryptoBoxPropsConfigTests(unittest.TestCase): + '''test here if everything with the config turns right''' + import filehandling + import os + import CryptoBox + + files = { "configFile" : "cbox-test.conf", + "nameDBFile" : "cryptobox_names.db", + "logFile" : "cryptobox.log", + "tmpdir" : "cryptobox-mnt" } + tmpdirname = "" + filenames = {} + configContent = """ +[Main] +AllowedDevices = /dev/loop +DefaultVolumePrefix = "Data " +DataDir = /tmp +NameDatabase = cryptobox_names.db +[System] +User = 1000 +Group = 1000 +MountParentDir = /tmp/mnt +DefaultCipher = aes-cbc-essiv:sha256 +[Log] +Level = debug +Facility = file +Destination = /tmp/cryptobox.log +[Programs] +blkid = /sbin/blkid +cryptsetup = /sbin/cryptsetup +""" + + def setUp(self): + '''generate all files in tmp and remember the names''' + os = self.os + self.tmpdirname = self.filehandling.gen_temp_dir(self.files["tmpdir"]) + os.chdir(self.tmpdirname) + for file in self.files.keys(): + #TODO: files are not really created in tmpdirname + self.filenames[file] = self.filehandling.gen_temp_file(self.files[file]) + + def tearDown(self): + '''remove the created tmpfiles''' + os = self.os + os.chdir(self.tmpdirname) + for file in self.filenames.values(): + if os.path.exists(file): + #os.remove(file) + pass + + def testConfigFile(self): + '''testConfigFile TODO''' + self.assertTrue(1) + + +if __name__ == "__main__": + unittest.main()