Debian Codegraph @

test/common/test_algorithm.py

raw
1 # SPDX-License-Identifier: MPL-2.0
2
3 import unittest
4
5 from test.common.classes import TestClasses
6
7 from cryptodatahub.common.algorithm import (
8 Authentication,
9 BlockCipher,
10 BlockCipherMode,
11 Hash,
12 IpProtocolNumber,
13 KeyExchange,
14 MAC,
15 MACModeParams,
16 MACParams,
17 Signature,
18 )
19
20
21 class TestAuthentication(TestClasses.TestJsonBase):
22 @classmethod
23 def _get_class(cls):
24 return Authentication
25
26
27 class TestBlockCipher(TestClasses.TestJsonBase):
28 @classmethod
29 def _get_class(cls):
30 return BlockCipher
31
32 def test_aead(self):
33 self.assertFalse(BlockCipher.CHACHA20.value.aead)
34 self.assertFalse(BlockCipher.AES_128.value.aead)
35
36
37 class TestBlockCipherMode(TestClasses.TestJsonBase):
38 @classmethod
39 def _get_class(cls):
40 return BlockCipherMode
41
42 def test_aead(self):
43 for mode in (BlockCipherMode.GCM, BlockCipherMode.GCM_8,
44 BlockCipherMode.CCM, BlockCipherMode.CCM_8,
45 BlockCipherMode.MGM, BlockCipherMode.GMAC,
46 BlockCipherMode.EAX):
47 self.assertTrue(mode.value.aead, msg=mode.name)
48 for mode in (BlockCipherMode.CBC, BlockCipherMode.CTR,
49 BlockCipherMode.CFB, BlockCipherMode.OFB,
50 BlockCipherMode.ECB, BlockCipherMode.CNT):
51 self.assertFalse(mode.value.aead, msg=mode.name)
52
53
54 class TestHash(TestClasses.TestJsonBase):
55 @classmethod
56 def _get_class(cls):
57 return Hash
58
59
60 class TestKeyExchange(TestClasses.TestJsonBase):
61 @classmethod
62 def _get_class(cls):
63 return KeyExchange
64
65
66 class TestMAC(TestClasses.TestJsonBase):
67 @classmethod
68 def _get_class(cls):
69 return MAC
70
71
72 class TestMACParams(unittest.TestCase):
73 def test_error_both_attributes_are_null(self):
74 with self.assertRaises(ValueError):
75 MACParams('name', 'long_name', None, None, None, None)
76
77
78 class TestMACModeParams(unittest.TestCase):
79 def test_gradeable(self):
80 self.assertTrue(len(MACModeParams.get_gradeable_name()))
81
82
83 class TestSignature(TestClasses.TestJsonBase):
84 @classmethod
85 def _get_class(cls):
86 return Signature
87
88
89 class TestIpProtocolNumber(TestClasses.TestJsonCodeNumericBase):
90 @classmethod
91 def _get_class(cls):
92 return IpProtocolNumber
93
94 def test_str(self):
95 self.assertEqual(str(IpProtocolNumber.TCP.value), 'Transmission Control')