Debian Codegraph @

t/unit/security/test_key.py

raw
1 import pytest
2 from kombu.utils.encoding import ensure_bytes
3
4 from celery.exceptions import SecurityError
5 from celery.security.key import PrivateKey
6 from celery.security.utils import get_digest_algorithm
7
8 from . import CERT1, ENCKEY1, ENCKEY2, KEY1, KEY2, KEY_ECDSA, KEYPASSWORD
9 from .case import SecurityCase
10
11
12 class test_PrivateKey(SecurityCase):
13
14 def test_valid_private_key(self):
15 PrivateKey(KEY1)
16 PrivateKey(KEY2)
17 PrivateKey(ENCKEY1, KEYPASSWORD)
18 PrivateKey(ENCKEY2, KEYPASSWORD)
19
20 def test_invalid_private_key(self):
21 with pytest.raises((SecurityError, TypeError)):
22 PrivateKey(None)
23 with pytest.raises(SecurityError):
24 PrivateKey('')
25 with pytest.raises(SecurityError):
26 PrivateKey('foo')
27 with pytest.raises(SecurityError):
28 PrivateKey(KEY1[:20] + KEY1[21:])
29 with pytest.raises(SecurityError):
30 PrivateKey(ENCKEY1, KEYPASSWORD+b"wrong")
31 with pytest.raises(SecurityError):
32 PrivateKey(ENCKEY2, KEYPASSWORD+b"wrong")
33 with pytest.raises(SecurityError):
34 PrivateKey(CERT1)
35 with pytest.raises(SecurityError):
36 PrivateKey(KEY_ECDSA)
37
38 def test_sign(self):
39 pkey = PrivateKey(KEY1)
40 pkey.sign(ensure_bytes('test'), get_digest_algorithm())
41 with pytest.raises(AttributeError):
42 pkey.sign(ensure_bytes('test'), get_digest_algorithm('unknown'))
43
44 # pkey = PrivateKey(KEY_ECDSA)
45 # pkey.sign(ensure_bytes('test'), get_digest_algorithm())