| 1 |
# SPDX-License-Identifier: MPL-2.0 |
| 2 |
|
| 3 |
import abc |
| 4 |
import collections |
| 5 |
import enum |
| 6 |
import json |
| 7 |
import pathlib |
| 8 |
import unittest |
| 9 |
|
| 10 |
import attr |
| 11 |
import pyfakefs.fake_filesystem_unittest |
| 12 |
|
| 13 |
from cryptodatahub.common.grade import Grade, Gradeable, GradeableSimple, GradeableVulnerabilities |
| 14 |
from cryptodatahub.common.key import PublicKey, PublicKeyX509Base |
| 15 |
from cryptodatahub.common.types import ( |
| 16 |
CryptoDataParamsEnumNumeric, |
| 17 |
CryptoDataParamsEnumString, |
| 18 |
CryptoDataParamsOIDOptional, |
| 19 |
CryptoDataParamsBase, |
| 20 |
CryptoDataParamsNamed, |
| 21 |
HexData, |
| 22 |
convert_dict_to_object, |
| 23 |
convert_hex_data, |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
TEST_URL_PREFIX = '/'.join([ |
| 28 |
'https://gist.githubusercontent.com', |
| 29 |
'c0r0n3r', |
| 30 |
'54386701406df6e7299bd95c46a4c8d1', |
| 31 |
'raw', |
| 32 |
'e0b9cf606739d3fc1d97cc7f21e501e118bc4e07', |
| 33 |
'' |
| 34 |
]) |
| 35 |
|
| 36 |
|
| 37 |
@attr.s |
| 38 |
class TestJsonObjectComplex(CryptoDataParamsBase): |
| 39 |
attr = attr.ib() |
| 40 |
|
| 41 |
|
| 42 |
@attr.s |
| 43 |
class TestJsonObject(CryptoDataParamsBase): |
| 44 |
attr_simple = attr.ib() |
| 45 |
attr_iterable = attr.ib() |
| 46 |
attr_complex = attr.ib(converter=convert_dict_to_object(TestJsonObjectComplex)) |
| 47 |
no_init_attr = attr.ib(init=False, default=None) |
| 48 |
_attr_hidden = attr.ib(init=False, default=None) |
| 49 |
|
| 50 |
|
| 51 |
class EnumTest(enum.Enum): |
| 52 |
NONE = 'NONE' |
| 53 |
STRING = 'STRING' |
| 54 |
INT = 'INT' |
| 55 |
FLOAT = 'FLOAT' |
| 56 |
BOOL = 'BOOL' |
| 57 |
|
| 58 |
|
| 59 |
class EnumIntTest(enum.IntEnum): |
| 60 |
ONE = 1 |
| 61 |
TEN = 10 |
| 62 |
|
| 63 |
|
| 64 |
@attr.s(frozen=True) |
| 65 |
class TestEnumNamedParams(CryptoDataParamsNamed): |
| 66 |
pass |
| 67 |
|
| 68 |
|
| 69 |
class TestEnumNumericParams(CryptoDataParamsEnumNumeric): |
| 70 |
@classmethod |
| 71 |
def get_code_size(cls): |
| 72 |
return 1 |
| 73 |
|
| 74 |
|
| 75 |
class TestEnumStringParams(CryptoDataParamsEnumString): |
| 76 |
pass |
| 77 |
|
| 78 |
|
| 79 |
class TestEnumOidParams(CryptoDataParamsOIDOptional): |
| 80 |
pass |
| 81 |
|
| 82 |
|
| 83 |
@attr.s(frozen=True) |
| 84 |
class TestEnumBinaryParams(CryptoDataParamsBase): |
| 85 |
binary = attr.ib( |
| 86 |
converter=convert_hex_data(), |
| 87 |
validator=attr.validators.instance_of(HexData) |
| 88 |
) |
| 89 |
|
| 90 |
|
| 91 |
class TestGradeableSimple(GradeableSimple): |
| 92 |
@property |
| 93 |
def grade(self): |
| 94 |
return Grade.INSECURE |
| 95 |
|
| 96 |
def __str__(self): |
| 97 |
return self.grade.value.name |
| 98 |
|
| 99 |
|
| 100 |
class TestClasses: |
| 101 |
class TestJsonBase(unittest.TestCase): |
| 102 |
@classmethod |
| 103 |
@abc.abstractmethod |
| 104 |
def _get_class(cls): |
| 105 |
raise NotImplementedError() |
| 106 |
|
| 107 |
@classmethod |
| 108 |
def _get_protected_names(cls): |
| 109 |
return set(['_standard', ]) |
| 110 |
|
| 111 |
@classmethod |
| 112 |
def _get_params_class(cls): |
| 113 |
return type(list(cls._get_class())[0].value) |
| 114 |
|
| 115 |
def setUp(self): |
| 116 |
self.json_file = open( # pylint: disable=consider-using-with |
| 117 |
str(self._get_class().get_json_path(self._get_params_class())), encoding='ascii' |
| 118 |
) |
| 119 |
self.json_data = json.load(self.json_file, object_pairs_hook=collections.OrderedDict) |
| 120 |
|
| 121 |
def tearDown(self): |
| 122 |
self.json_file.close() |
| 123 |
|
| 124 |
def test_json_file_format(self): |
| 125 |
wrong_indents = [] |
| 126 |
tab_in_lines = [] |
| 127 |
wrong_number_of_spces_after_colon = [] |
| 128 |
whitespace_only_lines = [] |
| 129 |
trailing_whitespaces = [] |
| 130 |
|
| 131 |
self.json_file.seek(0) |
| 132 |
for index, line in enumerate(self.json_file): |
| 133 |
if not line.strip(): |
| 134 |
whitespace_only_lines.append(index + 1) |
| 135 |
try: |
| 136 |
colon_index = line.index(':') |
| 137 |
if line[colon_index + 1] != ' ': |
| 138 |
wrong_number_of_spces_after_colon.append(index + 1) |
| 139 |
if line[colon_index + 2] == ' ': |
| 140 |
wrong_number_of_spces_after_colon.append(index + 1) |
| 141 |
except ValueError: |
| 142 |
pass |
| 143 |
if '\t' in line: |
| 144 |
tab_in_lines.append(index + 1) |
| 145 |
if line.startswith(' ') and (len(line) - len(line.lstrip())) % 4 != 0: |
| 146 |
wrong_indents.append(index + 1) |
| 147 |
if len(line) - 1 != len(line.rstrip()): |
| 148 |
trailing_whitespaces.append(index + 1) |
| 149 |
|
| 150 |
self.assertEqual(whitespace_only_lines, []) |
| 151 |
self.assertEqual(tab_in_lines, []) |
| 152 |
self.assertEqual(wrong_number_of_spces_after_colon, []) |
| 153 |
self.assertEqual(trailing_whitespaces, []) |
| 154 |
self.assertEqual(wrong_indents, []) |
| 155 |
|
| 156 |
def test_param_list_len(self): |
| 157 |
self.assertEqual(set(map(lambda item: item.name, self._get_class())), set(self.json_data)) |
| 158 |
|
| 159 |
def test_param_attribute_names_protected(self): |
| 160 |
protected_names = self._get_protected_names() |
| 161 |
|
| 162 |
self.assertEqual([ |
| 163 |
name |
| 164 |
for name, item in self.json_data.items() |
| 165 |
if set(key for key in item.keys() if key.startswith('_')) in protected_names |
| 166 |
], []) |
| 167 |
|
| 168 |
def test_param_attribute_order(self): |
| 169 |
attribute_names = self._get_params_class().get_init_attribute_names() |
| 170 |
|
| 171 |
items_with_surplus_attributes = [] |
| 172 |
for name, item in self.json_data.items(): |
| 173 |
item_keys = item.keys() |
| 174 |
json_attr_names = [key for key in item_keys if not key.startswith('_')] |
| 175 |
|
| 176 |
item_keys = set(item_keys) |
| 177 |
param_calss_attr_names = [ |
| 178 |
attribute_name for attribute_name in attribute_names if attribute_name in item_keys |
| 179 |
] |
| 180 |
|
| 181 |
if json_attr_names != param_calss_attr_names: |
| 182 |
items_with_surplus_attributes.append(name) |
| 183 |
self.assertEqual(items_with_surplus_attributes, []) |
| 184 |
|
| 185 |
attribute_names_not_ordered = [] |
| 186 |
for _name, item in self.json_data.items(): |
| 187 |
protected_count = 0 |
| 188 |
for key in item.keys(): |
| 189 |
protected_count += 1 |
| 190 |
if not key.startswith('_'): |
| 191 |
break |
| 192 |
|
| 193 |
public_count = 0 |
| 194 |
for key in list(item.keys())[protected_count:]: |
| 195 |
public_count += 1 |
| 196 |
if key.startswith('_'): |
| 197 |
break |
| 198 |
|
| 199 |
if protected_count + public_count != len(item.keys()): |
| 200 |
attribute_names_not_ordered.append(key) |
| 201 |
|
| 202 |
self.assertEqual(attribute_names_not_ordered, []) |
| 203 |
|
| 204 |
def test_str_works_for_all_items(self): |
| 205 |
if '__str__' not in dict(self._get_params_class().__dict__.items()): |
| 206 |
return |
| 207 |
|
| 208 |
for enum_item in self._get_class(): |
| 209 |
self.assertTrue(str(enum_item)) |
| 210 |
|
| 211 |
def test_gradeable(self): |
| 212 |
gradeable_class = self._get_params_class() |
| 213 |
if not issubclass(gradeable_class, Gradeable): |
| 214 |
return |
| 215 |
|
| 216 |
self.assertTrue( |
| 217 |
not issubclass(gradeable_class, GradeableVulnerabilities) or len(gradeable_class.get_gradeable_name()) |
| 218 |
) |
| 219 |
|
| 220 |
class TestJsonCodeStringBase(TestJsonBase): |
| 221 |
@classmethod |
| 222 |
@abc.abstractmethod |
| 223 |
def _get_class(cls): |
| 224 |
raise NotImplementedError() |
| 225 |
|
| 226 |
class TestJsonCodeNumericBase(TestJsonBase): |
| 227 |
@classmethod |
| 228 |
@abc.abstractmethod |
| 229 |
def _get_class(cls): |
| 230 |
raise NotImplementedError() |
| 231 |
|
| 232 |
def test_code_size(self): |
| 233 |
self.assertGreater(self._get_params_class().get_code_size(), 0) |
| 234 |
|
| 235 |
@classmethod |
| 236 |
def _get_protected_names(cls): |
| 237 |
return super()._get_protected_names() | set(['_code_in_hex', ]) |
| 238 |
|
| 239 |
def test_param_attribute_value_code_in_hex(self): |
| 240 |
self.assertEqual([ |
| 241 |
item['_code_in_hex'] |
| 242 |
for item in self.json_data.values() |
| 243 |
if int(item['code']) != int(item['_code_in_hex'], 16) |
| 244 |
], []) |
| 245 |
|
| 246 |
class TestKeyBase(pyfakefs.fake_filesystem_unittest.TestCase): |
| 247 |
def setUp(self): |
| 248 |
self.setUpPyfakefs() |
| 249 |
|
| 250 |
self.__certs_dir = pathlib.PurePath(__file__).parent.parent / 'common' / 'certs' |
| 251 |
self.fs.add_real_directory(str(self.__certs_dir)) |
| 252 |
|
| 253 |
def _get_public_key_pem(self, public_key_file_name): |
| 254 |
public_key_path = self.__certs_dir / (public_key_file_name + '.pem') |
| 255 |
with open(str(public_key_path), encoding='ascii') as pem_file: |
| 256 |
return pem_file.read() |
| 257 |
|
| 258 |
def _get_public_key(self, public_key_file_name): |
| 259 |
return PublicKey.from_pem(self._get_public_key_pem(public_key_file_name)) |
| 260 |
|
| 261 |
def _get_public_key_x509(self, public_key_file_name): |
| 262 |
return PublicKeyX509Base.from_pem(self._get_public_key_pem(public_key_file_name)) |