separated unittest

This commit is contained in:
age 2006-08-22 04:29:53 +00:00
parent 04c2719c8e
commit 12868beb21
4 changed files with 82 additions and 75 deletions

View file

@ -47,7 +47,7 @@ class CryptoBox:
logging.basicConfig(level=logging.DEBUG, logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(module)s %(levelname)s %(message)s', format='%(asctime)s %(module)s %(levelname)s %(message)s',
filename='./cryptobox.log', filename='./cryptobox.log',
filemode='w') filemode='a')
self.log = logging.getLogger("CryptoBoxProps") self.log = logging.getLogger("CryptoBoxProps")
self.log.info("loggingsystem is up'n running") self.log.info("loggingsystem is up'n running")
## from now on everything can be logged via self.log... ## from now on everything can be logged via self.log...
@ -279,73 +279,6 @@ class CryptoBoxProps(CryptoBox):
return [] 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__": if __name__ == "__main__":
unittest.main() cb = CryptoBox()
#cb = CryptoBoxProps()

View file

@ -1,4 +0,0 @@
[CryptoBox] - 6
[CryptoBox] - 6
[CryptoBox] - 6
[CryptoBox] - 6

View file

@ -12,6 +12,7 @@ error handling:
- unspecific error handling is evil (try: "grep -r except: .") - unspecific error handling is evil (try: "grep -r except: .")
unit testing: unit testing:
- write a unittest and then write the relating code until the unittest stops failing :) - first 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) - '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 ;) ))

View file

@ -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()