• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

LudovicRousseau / PyKCS11 / 12855273166

19 Jan 2025 04:26PM UTC coverage: 86.678% (-0.008%) from 86.686%
12855273166

push

github

LudovicRousseau
test_derive: document test_deriveKey_CKM_EXTRACT_KEY_FROM_KEY

2993 of 3453 relevant lines covered (86.68%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.81
/test/test_load.py
1
import gc
1✔
2
import os
1✔
3
import platform
1✔
4
import shutil
1✔
5
import unittest
1✔
6
from pathlib import Path
1✔
7
from tempfile import TemporaryDirectory
1✔
8

9
from PyKCS11 import PyKCS11
1✔
10

11

12
class TestUtil(unittest.TestCase):
1✔
13
    def setUp(self):
1✔
14
        self.pkcs11 = PyKCS11.PyKCS11Lib()
1✔
15
        self.tmpdir = TemporaryDirectory()
1✔
16
        self.lib1_name = os.environ["PYKCS11LIB"]
1✔
17
        # create a tmp copy of the main lib
18
        # to use as a different library in tests
19
        self.lib2_name = str(Path(self.tmpdir.name) / Path(self.lib1_name).name)
1✔
20
        shutil.copy(self.lib1_name, self.tmpdir.name)
1✔
21

22
    def tearDown(self):
1✔
23
        del self.pkcs11
1✔
24
        if platform.system() != "Windows":
1✔
25
            self.tmpdir.cleanup()
1✔
26
        del self.tmpdir
1✔
27
        del self.lib1_name
1✔
28
        del self.lib2_name
1✔
29

30
    def openSession(self, lib):
1✔
31
        slot = lib.getSlotList(tokenPresent=True)[0]
1✔
32
        return lib.openSession(slot, PyKCS11.CKF_SERIAL_SESSION)
1✔
33

34
    def test_load(self):
1✔
35
        # create two instances with default library
36
        lib1 = PyKCS11.PyKCS11Lib().load()
1✔
37
        lib2 = PyKCS11.PyKCS11Lib().load()
1✔
38

39
        # expect two instances with the same library loaded
40
        self.assertTrue(hasattr(lib1, "pkcs11dll_filename"))
1✔
41
        self.assertTrue(hasattr(lib2, "pkcs11dll_filename"))
1✔
42
        self.assertEqual(len(lib1._loaded_libs), 1)
1✔
43
        self.assertEqual(len(lib2._loaded_libs), 1)
1✔
44

45
        self.openSession(lib1)
1✔
46

47
        # unload the first library
48
        del lib1
1✔
49
        gc.collect()
1✔
50

51
        # one instance remaining, the library is still in use
52
        self.openSession(lib2)
1✔
53
        self.assertEqual(len(lib2._loaded_libs), 1)
1✔
54

55
    def test_multiple_load(self):
1✔
56
        # load two different libraries
57
        lib1 = PyKCS11.PyKCS11Lib().load(self.lib1_name)
1✔
58
        lib2 = PyKCS11.PyKCS11Lib().load(self.lib2_name)
1✔
59

60
        # _loaded_libs is shared across all instances
61
        # check the value in self.pkcs11
62
        self.assertEqual(len(self.pkcs11._loaded_libs), 2)
1✔
63
        lib1 = PyKCS11.PyKCS11Lib()  # unload lib1
1✔
64
        self.assertEqual(len(self.pkcs11._loaded_libs), 1)
1✔
65
        lib2 = PyKCS11.PyKCS11Lib()  # unload lib2
1✔
66
        self.assertEqual(len(self.pkcs11._loaded_libs), 0)
1✔
67

68
    def test_invalid_load(self):
1✔
69
        # Library not found
70
        lib = "nolib"
1✔
71
        with self.assertRaises(PyKCS11.PyKCS11Error) as cm:
1✔
72
            self.pkcs11.load(lib)
1✔
73
        the_exception = cm.exception
1✔
74
        self.assertEqual(the_exception.value, -1)
1✔
75
        self.assertEqual(the_exception.text, lib)
1✔
76
        self.assertEqual(str(the_exception), "Load (%s)" % lib)
1✔
77
        self.assertEqual(len(self.pkcs11._loaded_libs), 0)
1✔
78

79
        # C_GetFunctionList() not found
80
        if platform.system() == "Linux":
1✔
81
            # GNU/Linux
82
            lib = "libc.so.6"
1✔
83
        elif platform.system() == "Darwin":
×
84
            # macOS
85
            lib = "/usr/lib/libSystem.B.dylib"
×
86
        else:
87
            # Windows
88
            lib = "WinSCard.dll"
×
89

90
        with self.assertRaises(PyKCS11.PyKCS11Error) as cm:
1✔
91
            self.pkcs11.load(lib)
1✔
92
        the_exception = cm.exception
1✔
93
        self.assertEqual(the_exception.value, -4)
1✔
94
        self.assertEqual(the_exception.text, lib)
1✔
95
        self.assertEqual(str(the_exception), "C_GetFunctionList() not found (%s)" % lib)
1✔
96
        self.assertEqual(len(self.pkcs11._loaded_libs), 0)
1✔
97

98
        # try to load the improper lib another time
99
        with self.assertRaises(PyKCS11.PyKCS11Error) as cm:
1✔
100
            self.pkcs11.load(lib)
1✔
101
        the_exception = cm.exception
1✔
102
        self.assertEqual(the_exception.value, -4)
1✔
103
        self.assertEqual(the_exception.text, lib)
1✔
104
        self.assertEqual(str(the_exception), "C_GetFunctionList() not found (%s)" % lib)
1✔
105
        self.assertEqual(len(self.pkcs11._loaded_libs), 0)
1✔
106

107
        # finally, load a valid library
108
        self.pkcs11.load()
1✔
109
        self.assertEqual(len(self.pkcs11._loaded_libs), 1)
1✔
110

111
    def test_specific_load(self):
1✔
112
        # load two different libraries sequentially
113
        self.pkcs11.load(self.lib1_name)
1✔
114
        self.pkcs11.load(self.lib2_name)
1✔
115

116
        # the second load should've unloaded the first library
117
        self.assertEqual(len(self.pkcs11._loaded_libs), 1)
1✔
118
        self.assertEqual(self.pkcs11.pkcs11dll_filename, self.lib2_name)
1✔
119

120
        # reload the first library
121
        self.pkcs11 = PyKCS11.PyKCS11Lib()
1✔
122
        self.pkcs11.load(self.lib1_name)
1✔
123

124
        # try to open a session
125
        self.assertIsNotNone(self.openSession(self.pkcs11))
1✔
126

127
    def test_unload(self):
1✔
128
        self.pkcs11.load().unload()
1✔
129
        # no pkcs11dll_filename should remain after unload
130
        self.assertFalse(hasattr(self.pkcs11, "pkcs11dll_filename"))
1✔
131

132
        self.pkcs11.load()
1✔
133
        self.openSession(self.pkcs11)
1✔
134
        # one library has been loaded
135
        self.assertEqual(len(self.pkcs11._loaded_libs), 1)
1✔
136
        self.assertTrue(hasattr(self.pkcs11, "pkcs11dll_filename"))
1✔
137

138
        self.pkcs11.unload()
1✔
139
        gc.collect()
1✔
140
        # manually unloaded the library using gc.collect()
141
        self.assertEqual(len(self.pkcs11._loaded_libs), 0)
1✔
142
        self.assertFalse(hasattr(self.pkcs11, "pkcs11dll_filename"))
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc