From 35295b4171bbbfa32233a3d4b23ef8378a8b1c49 Mon Sep 17 00:00:00 2001 From: Rob Austein Date: Mon, 21 Sep 2015 11:00:52 -0400 Subject: First step towards unit tests. --- GNUmakefile | 3 ++ unit_tests.py | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 unit_tests.py diff --git a/GNUmakefile b/GNUmakefile index 4f8193e..89f713c 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -90,6 +90,9 @@ tags: TAGS TAGS: *.[ch] etags $^ +test: + sudo python unit_tests.py + # Kudge for testing, gussy this up if we decide to keep it HSMBULLY := $(firstword $(wildcard $(addsuffix /hsmbully,$(subst :, ,.:${PATH})))) diff --git a/unit_tests.py b/unit_tests.py new file mode 100644 index 0000000..a59b731 --- /dev/null +++ b/unit_tests.py @@ -0,0 +1,110 @@ +#!/usr/bin/env python + +import unittest +import os + +from py11 import * +from py11.mutex import MutexDB + +p11 = None + +class TestInit(unittest.TestCase): + + def test_no_lock(self): + p11.C_Initialize() + + def test_os_lock(self): + p11.C_Initialize(CKF_OS_LOCKING_OK) + + def test_mutex(self): + mdb = MutexDB() + p11.C_Initialize(0, mdb.create, mdb.destroy, mdb.lock, mdb.unlock) + + def test_both(self): + mdb = MutexDB() + p11.C_Initialize(CKF_OS_LOCKING_OK, mdb.create, mdb.destroy, mdb.lock, mdb.unlock) + + def tearDown(self): + p11.C_Finalize() + +class TestDevice(unittest.TestCase): + + def test_slots(self): + slots = p11.C_GetSlotList() + self.assertIsInstance(slots, (list, tuple)) + self.assertEqual(len(slots), 1) + self.assertIsInstance(slots[0], (int, long)) + +@unittest.skip("This was just an example") +class Test1(unittest.TestCase): + + @classmethod + def setUpClass(cls): + pass #print "Class-wide setup" + + @classmethod + def tearDownClass(cls): + pass #print "Class-wide teardown" + + def setUp(self): + pass #print "Per-test setup" + + def tearDown(self): + pass #print "Per-test teardown" + + def test_one(self): + pass #print "Test one" + self.assertTrue(True) + + def test_two(self): + pass #print "Test two" + self.assertTrue(True) + +@unittest.skip("This was also just an example") +class Test2(unittest.TestCase): + + @classmethod + def setUpClass(cls): + pass #print "Class-wide setup" + + @classmethod + def tearDownClass(cls): + pass #print "Class-wide teardown" + + def setUp(self): + pass #print "Per-test setup" + + def tearDown(self): + pass #print "Per-test teardown" + + def test_three(self): + pass #print "Test three" + self.assertTrue(True) + +def setUpModule(): + from os import environ + from os.path import abspath + global p11 + p11 = PKCS11() + environ["PKCS11_DATABASE"] = abspath("unit_tests.db") + delete_db() + set_pin() + +def tearDownModule(): + delete_db() + +def delete_db(): + from os import environ, unlink + try: + unlink(environ["PKCS11_DATABASE"]) + except OSError, e: + if e.errno != 2: + raise + +def set_pin(user_pin = "fnord", so_pin = "fnord"): + from subprocess import Popen, PIPE + Popen(("./p11util", "-sup"), stdin = PIPE).communicate("fnord\nfnord\n") + +if __name__ == "__main__": + unittest.main(verbosity = 2) + -- cgit v1.2.3