File: test_gc.py

package info (click to toggle)
pykcs11 1.5.18-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 808 kB
  • sloc: python: 6,520; cpp: 1,424; ansic: 1,360; makefile: 77; sh: 18
file content (40 lines) | stat: -rw-r--r-- 1,087 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# pylint: disable=missing-module-docstring
# pylint: disable=missing-class-docstring
# pylint: disable=missing-function-docstring

import gc
import unittest

from PyKCS11 import PyKCS11


class TestUtil(unittest.TestCase):
    def test_gc(self):
        res = []
        # we must use at least 2 sessions
        for _ in range(2):
            p11, session = self.createSession()
            res.append([p11, session])

        for p11, session in res:
            self.closeSession(session)
            del p11

            # force the call to __del__() on p11 now
            gc.collect()

    def createSession(self):
        pkcs11 = PyKCS11.PyKCS11Lib()
        pkcs11.load()

        slot = pkcs11.getSlotList(tokenPresent=True)[0]
        session = pkcs11.openSession(
            slot, PyKCS11.CKF_SERIAL_SESSION | PyKCS11.CKF_RW_SESSION
        )

        return (pkcs11, session)

    def closeSession(self, session):
        # this call generated a CKR_CRYPTOKI_NOT_INITIALIZED for the
        # second session because C_Finalize() was already called
        session.logout()