File: test_pkcs11.py

package info (click to toggle)
python-xmlsec 1.3.14-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 656 kB
  • sloc: ansic: 4,118; python: 2,045; xml: 461; makefile: 4
file content (57 lines) | stat: -rw-r--r-- 1,753 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import xmlsec
from tests import base
from xmlsec import constants as consts

KEY_URL = "pkcs11;pkcs11:token=test;object=test;pin-value=secret1"


def setUpModule():
    from tests import softhsm_setup

    softhsm_setup.setup()


def tearDownModule():
    from tests import softhsm_setup

    softhsm_setup.teardown()


class TestKeys(base.TestMemoryLeaks):
    def test_del_key(self):
        ctx = xmlsec.SignatureContext(manager=xmlsec.KeysManager())
        ctx.key = xmlsec.Key.from_engine(KEY_URL)
        del ctx.key
        self.assertIsNone(ctx.key)

    def test_set_key(self):
        ctx = xmlsec.SignatureContext(manager=xmlsec.KeysManager())
        ctx.key = xmlsec.Key.from_engine(KEY_URL)
        self.assertIsNotNone(ctx.key)

    def test_sign_bad_args(self):
        ctx = xmlsec.SignatureContext()
        ctx.key = xmlsec.Key.from_engine(KEY_URL)
        with self.assertRaises(TypeError):
            ctx.sign('')

    def test_sign_fail(self):
        ctx = xmlsec.SignatureContext()
        ctx.key = xmlsec.Key.from_engine(KEY_URL)
        with self.assertRaisesRegex(xmlsec.Error, 'failed to sign'):
            ctx.sign(self.load_xml('sign1-in.xml'))

    def test_sign_case1(self):
        """Should sign a pre-constructed template file using a key from a pkcs11 engine."""
        root = self.load_xml("sign1-in.xml")
        sign = xmlsec.tree.find_node(root, consts.NodeSignature)
        self.assertIsNotNone(sign)

        ctx = xmlsec.SignatureContext()
        ctx.key = xmlsec.Key.from_engine(KEY_URL)
        self.assertIsNotNone(ctx.key)
        ctx.key.name = 'rsakey.pem'
        self.assertEqual("rsakey.pem", ctx.key.name)

        ctx.sign(sign)
        self.assertEqual(self.load_xml("sign1-out.xml"), root)