| 1 |
# SPDX-License-Identifier: MPL-2.0 |
| 2 |
|
| 3 |
import abc |
| 4 |
import base64 |
| 5 |
import collections |
| 6 |
import enum |
| 7 |
import json |
| 8 |
import pathlib |
| 9 |
import unittest |
| 10 |
|
| 11 |
import attr |
| 12 |
|
| 13 |
import pyfakefs.fake_filesystem_unittest |
| 14 |
|
| 15 |
from cryptodatahub.common.exception import InvalidValue |
| 16 |
from cryptodatahub.common.grade import AttackNamed, AttackType, Grade, GradeableVulnerabilities, Vulnerability |
| 17 |
from cryptodatahub.common.types import CryptoDataEnumBase, CryptoDataParamsNamed |
| 18 |
|
| 19 |
from cryptoparser.common.base import ( |
| 20 |
ComposerBinary, |
| 21 |
FourByteEnumComposer, |
| 22 |
FourByteEnumParsable, |
| 23 |
ListParamParsable, |
| 24 |
ListParsable, |
| 25 |
NumericRangeParsableBase, |
| 26 |
OneByteEnumComposer, |
| 27 |
OneByteEnumParsable, |
| 28 |
OpaqueEnumComposer, |
| 29 |
OpaqueEnumParsable, |
| 30 |
OpaqueParam, |
| 31 |
ParsableBase, |
| 32 |
ParserBinary, |
| 33 |
Serializable, |
| 34 |
SerializableTextEncoder, |
| 35 |
StringEnumCaseInsensitiveParsable, |
| 36 |
StringEnumParsable, |
| 37 |
ThreeByteEnumComposer, |
| 38 |
ThreeByteEnumParsable, |
| 39 |
TwoByteEnumComposer, |
| 40 |
TwoByteEnumParsable, |
| 41 |
VariantParsable, |
| 42 |
VariantParsableExact, |
| 43 |
) |
| 44 |
from cryptoparser.common.exception import TooMuchData, InvalidType |
| 45 |
from cryptoparser.common.field import ( |
| 46 |
FieldsJson, |
| 47 |
FieldsSemicolonSeparated, |
| 48 |
FieldValueStringEnum, |
| 49 |
FieldValueComponentBool, |
| 50 |
FieldValueComponentFloat, |
| 51 |
FieldValueComponentNumber, |
| 52 |
FieldValueComponentOption, |
| 53 |
FieldValueComponentPercent, |
| 54 |
FieldValueComponentQuotedString, |
| 55 |
FieldValueComponentString, |
| 56 |
FieldValueComponentStringBase64, |
| 57 |
FieldValueComponentStringEnum, |
| 58 |
FieldValueComponentStringEnumParams, |
| 59 |
FieldValueComponentTimeDelta, |
| 60 |
FieldValueComponentUrl, |
| 61 |
FieldValueStringEnumParams, |
| 62 |
FieldValueTimeDelta, |
| 63 |
NameValuePairListSemicolonSeparated, |
| 64 |
) |
| 65 |
from cryptoparser.common.parse import ParserCRLF |
| 66 |
from cryptoparser.common.x509 import PublicKeyX509 |
| 67 |
|
| 68 |
|
| 69 |
class NByteParsable(ParsableBase): |
| 70 |
def __init__(self, value): |
| 71 |
if value < 0 or value >= 2 ** (8 * self.get_byte_size()): |
| 72 |
raise ValueError |
| 73 |
|
| 74 |
self.value = value |
| 75 |
|
| 76 |
def __int__(self): |
| 77 |
return self.value |
| 78 |
|
| 79 |
@classmethod |
| 80 |
def _parse(cls, parsable): |
| 81 |
parser = ParserBinary(parsable) |
| 82 |
|
| 83 |
parser.parse_numeric('value', cls.get_byte_size()) |
| 84 |
|
| 85 |
return cls(parser['value']), cls.get_byte_size() |
| 86 |
|
| 87 |
def compose(self): |
| 88 |
composer = ComposerBinary() |
| 89 |
|
| 90 |
composer.compose_numeric(self.value, self.get_byte_size()) |
| 91 |
|
| 92 |
return composer.composed_bytes |
| 93 |
|
| 94 |
def __repr__(self): |
| 95 |
return f'0x{self.value:>0{self.get_byte_size() * 2}x}' |
| 96 |
|
| 97 |
def __eq__(self, other): |
| 98 |
return self.get_byte_size() == other.get_byte_size() and self.value == other.value |
| 99 |
|
| 100 |
@classmethod |
| 101 |
def get_byte_size(cls): |
| 102 |
raise NotImplementedError() |
| 103 |
|
| 104 |
|
| 105 |
class OneByteParsable(NByteParsable): |
| 106 |
@classmethod |
| 107 |
def get_byte_size(cls): |
| 108 |
return 1 |
| 109 |
|
| 110 |
|
| 111 |
class TwoByteParsable(NByteParsable): |
| 112 |
@classmethod |
| 113 |
def get_byte_size(cls): |
| 114 |
return 2 |
| 115 |
|
| 116 |
|
| 117 |
class ConditionalParsable(NByteParsable): |
| 118 |
def __int__(self): |
| 119 |
return self.value |
| 120 |
|
| 121 |
@classmethod |
| 122 |
def _parse(cls, parsable): |
| 123 |
parser = ParserBinary(parsable) |
| 124 |
|
| 125 |
parser.parse_numeric('value', cls.get_byte_size()) |
| 126 |
|
| 127 |
cls.check_parsed(parser['value']) |
| 128 |
|
| 129 |
return cls(parser['value']), cls.get_byte_size() |
| 130 |
|
| 131 |
def compose(self): |
| 132 |
composer = ComposerBinary() |
| 133 |
|
| 134 |
composer.compose_numeric(self.value, self.get_byte_size()) |
| 135 |
|
| 136 |
return composer.composed_bytes |
| 137 |
|
| 138 |
@classmethod |
| 139 |
@abc.abstractmethod |
| 140 |
def check_parsed(cls, value): |
| 141 |
raise NotImplementedError() |
| 142 |
|
| 143 |
|
| 144 |
class OneByteOddParsable(ConditionalParsable): |
| 145 |
@classmethod |
| 146 |
def get_byte_size(cls): |
| 147 |
return 1 |
| 148 |
|
| 149 |
@classmethod |
| 150 |
def check_parsed(cls, value): |
| 151 |
if value % 2 == 0: |
| 152 |
raise InvalidValue(value, OneByteOddParsable) |
| 153 |
|
| 154 |
|
| 155 |
class TwoByteEvenParsable(ConditionalParsable): |
| 156 |
@classmethod |
| 157 |
def get_byte_size(cls): |
| 158 |
return 2 |
| 159 |
|
| 160 |
@classmethod |
| 161 |
def check_parsed(cls, value): |
| 162 |
if value % 2 != 0: |
| 163 |
raise InvalidValue(value, TwoByteEvenParsable) |
| 164 |
|
| 165 |
|
| 166 |
class AlwaysUnknowTypeParsable(ParsableBase): |
| 167 |
@classmethod |
| 168 |
def _parse(cls, parsable): |
| 169 |
raise InvalidValue(parsable, AlwaysUnknowTypeParsable) |
| 170 |
|
| 171 |
def compose(self): |
| 172 |
raise TooMuchData() |
| 173 |
|
| 174 |
|
| 175 |
class AlwaysInvalidTypeParsable(ParsableBase): |
| 176 |
@classmethod |
| 177 |
def _parse(cls, parsable): |
| 178 |
raise InvalidType() |
| 179 |
|
| 180 |
def compose(self): |
| 181 |
raise TooMuchData() |
| 182 |
|
| 183 |
|
| 184 |
class AlwaysInvalidTypeVariantParsable(VariantParsable): |
| 185 |
@classmethod |
| 186 |
def _get_variants(cls): |
| 187 |
return collections.OrderedDict([ |
| 188 |
(AlwaysInvalidTypeParsable, (AlwaysInvalidTypeParsable, )) |
| 189 |
]) |
| 190 |
|
| 191 |
|
| 192 |
class SerializableEnumVariantParsable(VariantParsable): |
| 193 |
@classmethod |
| 194 |
def _get_variants(cls): |
| 195 |
return collections.OrderedDict([ |
| 196 |
(SerializableEnum, (SerializableEnum, )) |
| 197 |
]) |
| 198 |
|
| 199 |
|
| 200 |
class AlwaysTestStringComposer(ParsableBase): |
| 201 |
def __eq__(self, other): |
| 202 |
return isinstance(other, AlwaysTestStringComposer) |
| 203 |
|
| 204 |
@classmethod |
| 205 |
def _parse(cls, parsable): |
| 206 |
if parsable[:4] != b'test': |
| 207 |
raise InvalidValue(parsable, cls) |
| 208 |
|
| 209 |
return AlwaysTestStringComposer(), 4 |
| 210 |
|
| 211 |
def compose(self): |
| 212 |
return b'test' |
| 213 |
|
| 214 |
|
| 215 |
@attr.s |
| 216 |
class SerializableEnumValue(Serializable): |
| 217 |
code = attr.ib(validator=attr.validators.instance_of(int)) |
| 218 |
|
| 219 |
def as_json(self): |
| 220 |
return json.dumps({'code': self.code}) |
| 221 |
|
| 222 |
def _as_markdown(self, level): |
| 223 |
return False, self.code |
| 224 |
|
| 225 |
@classmethod |
| 226 |
def get_code_size(cls): |
| 227 |
return 2 |
| 228 |
|
| 229 |
|
| 230 |
class OpaqueEnumFactory(OpaqueEnumParsable): |
| 231 |
@classmethod |
| 232 |
def get_enum_class(cls): |
| 233 |
return OpaqueEnum |
| 234 |
|
| 235 |
@classmethod |
| 236 |
def get_param(cls): |
| 237 |
return OpaqueParam( |
| 238 |
min_byte_num=1, max_byte_num=32 |
| 239 |
) |
| 240 |
|
| 241 |
|
| 242 |
@attr.s |
| 243 |
class OpaqueEnumParams: |
| 244 |
code = attr.ib(validator=attr.validators.instance_of(str)) |
| 245 |
|
| 246 |
|
| 247 |
class OpaqueEnum(OpaqueEnumComposer): |
| 248 |
ALPHA = OpaqueEnumParams(code='άλφα') |
| 249 |
BETA = OpaqueEnumParams(code='βήτα') |
| 250 |
GAMMA = OpaqueEnumParams(code='γάμμα') |
| 251 |
|
| 252 |
|
| 253 |
class TestObject: |
| 254 |
pass |
| 255 |
|
| 256 |
|
| 257 |
class SerializableSimpleTypes(Serializable): # pylint: disable=too-many-instance-attributes |
| 258 |
def __init__(self): |
| 259 |
self.int_value = 1 |
| 260 |
self.float_value = 1.0 |
| 261 |
self.bool_value = False |
| 262 |
self.str_value = 'string' |
| 263 |
self.bytearray_value = bytearray(b'\x00\x01\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f') |
| 264 |
self.none_value = None |
| 265 |
|
| 266 |
|
| 267 |
class SerializableIterables(Serializable): |
| 268 |
def __init__(self): |
| 269 |
self.dict_key = collections.OrderedDict([ |
| 270 |
(1, 'int'), |
| 271 |
('str', 'string'), |
| 272 |
(SerializableStringEnum.FIRST, 'enum'), |
| 273 |
]) |
| 274 |
self.dict_value = collections.OrderedDict([ |
| 275 |
('int', 1), |
| 276 |
('string', 'str'), |
| 277 |
('enum', SerializableStringEnum.FIRST), |
| 278 |
]) |
| 279 |
self.list_value = list(['value', ]) |
| 280 |
self.tuple_value = tuple(['value', ]) |
| 281 |
|
| 282 |
|
| 283 |
class SerializableEnumFactory(TwoByteEnumParsable): |
| 284 |
@classmethod |
| 285 |
def get_enum_class(cls): |
| 286 |
return SerializableEnum |
| 287 |
|
| 288 |
@abc.abstractmethod |
| 289 |
def compose(self): |
| 290 |
raise NotImplementedError() |
| 291 |
|
| 292 |
|
| 293 |
class SerializableEnum(TwoByteEnumComposer): |
| 294 |
FIRST = SerializableEnumValue( |
| 295 |
code=0x0001, |
| 296 |
) |
| 297 |
SECOND = SerializableEnumValue( |
| 298 |
code=0x0002, |
| 299 |
) |
| 300 |
|
| 301 |
|
| 302 |
class SerializableStringEnum(enum.Enum): |
| 303 |
FIRST = '1' |
| 304 |
SECOND = '2' |
| 305 |
|
| 306 |
|
| 307 |
class SerializableEnums(Serializable): |
| 308 |
def __init__(self): |
| 309 |
self.param_enum = SerializableEnum.FIRST |
| 310 |
self.string_enum = SerializableStringEnum.SECOND |
| 311 |
|
| 312 |
|
| 313 |
class SerializableHidden(Serializable): |
| 314 |
def __init__(self): |
| 315 |
self._invisible_value = None |
| 316 |
self.visible_value = 'value' |
| 317 |
|
| 318 |
|
| 319 |
class SerializableSingle(Serializable): |
| 320 |
def _asdict(self): |
| 321 |
return 'single' |
| 322 |
|
| 323 |
|
| 324 |
class SerializableUnhandled(Serializable): |
| 325 |
def __init__(self): |
| 326 |
self.complex_number = 1 + 2j |
| 327 |
|
| 328 |
|
| 329 |
@attr.s |
| 330 |
class SerializableHumanReadable(Serializable): |
| 331 |
attr_2 = attr.ib(default='value 2', metadata={'human_readable_name': 'Human Readable Name 2'}) |
| 332 |
attr_1 = attr.ib(default='value 1', metadata={'human_readable_name': 'Human Readable Name 1'}) |
| 333 |
|
| 334 |
|
| 335 |
@attr.s |
| 336 |
class SerializableHumanFriendly(Serializable): |
| 337 |
human_friendly = attr.ib(default='human-friendly', metadata={'human_friendly': True}) |
| 338 |
human_friendly_by_default = attr.ib(default='human-friendly-by-default') |
| 339 |
non_human_friendly = attr.ib(default='non-human-friendly', metadata={'human_friendly': False}) |
| 340 |
|
| 341 |
|
| 342 |
@attr.s |
| 343 |
class SerializableAttributeOrder(Serializable): |
| 344 |
attr_b = attr.ib(default='b') |
| 345 |
attr_a = attr.ib(default='a') |
| 346 |
|
| 347 |
|
| 348 |
class Class: |
| 349 |
def __init__(self): |
| 350 |
self.attr_b = 'b' |
| 351 |
self.attr_a = 'a' |
| 352 |
|
| 353 |
|
| 354 |
@attr.s |
| 355 |
class ClassAttr: |
| 356 |
attr_b = attr.ib(default='b') |
| 357 |
attr_a = attr.ib(default='a') |
| 358 |
|
| 359 |
|
| 360 |
class ClassAsDict: |
| 361 |
def __init__(self): |
| 362 |
self.attr_a = 'a' |
| 363 |
self.attr_b = 'b' |
| 364 |
|
| 365 |
def _asdict(self): |
| 366 |
return collections.OrderedDict([ |
| 367 |
('attr_b', self.attr_b), |
| 368 |
('attr_a', self.attr_a), |
| 369 |
]) |
| 370 |
|
| 371 |
|
| 372 |
@attr.s |
| 373 |
class ClassAttrAsDict: |
| 374 |
attr_a = attr.ib(default='a') |
| 375 |
attr_b = attr.ib(default='b') |
| 376 |
|
| 377 |
def _asdict(self): |
| 378 |
return collections.OrderedDict([ |
| 379 |
('attr_b', self.attr_b), |
| 380 |
('attr_a', self.attr_a), |
| 381 |
]) |
| 382 |
|
| 383 |
|
| 384 |
class ClassCryptoDataEnum(CryptoDataEnumBase): |
| 385 |
ONE = CryptoDataParamsNamed('one', 'long one') |
| 386 |
|
| 387 |
|
| 388 |
class ClassGradeable(GradeableVulnerabilities): |
| 389 |
@classmethod |
| 390 |
def get_gradeable_name(cls): |
| 391 |
return 'gradeable' |
| 392 |
|
| 393 |
def __str__(self): |
| 394 |
return 'value' |
| 395 |
|
| 396 |
|
| 397 |
class SerializableRecursive(Serializable): # pylint: disable=too-many-instance-attributes |
| 398 |
def __init__(self): |
| 399 |
self.json_object = Class() |
| 400 |
self.json_attr_object = ClassAttr() |
| 401 |
self.json_attr_as_dict = ClassAttrAsDict() |
| 402 |
self.json_asdict_object = ClassAsDict() |
| 403 |
self.json_crypto_data_hub_enum = ClassCryptoDataEnum.ONE |
| 404 |
self.json_gradeable = ClassGradeable([ |
| 405 |
Vulnerability(AttackType.MITM, Grade.INSECURE, AttackNamed.NOFS) |
| 406 |
]) |
| 407 |
self.json_serializable_hidden = SerializableHidden() |
| 408 |
self.json_serializable_single = 'single' |
| 409 |
self.json_serializable_in_list = list([SerializableHidden(), 'single']) |
| 410 |
self.json_serializable_in_tuple = tuple([SerializableHidden(), 'single']) |
| 411 |
self.json_serializable_in_dict = dict({'key1': SerializableHidden(), 'key2': 'single'}) |
| 412 |
|
| 413 |
|
| 414 |
class SerializableEmptyValues(Serializable): |
| 415 |
def __init__(self): |
| 416 |
self.value = None |
| 417 |
self.list = [] |
| 418 |
self.tuple = tuple() |
| 419 |
self.dict = {} |
| 420 |
|
| 421 |
|
| 422 |
class FlagEnum(enum.IntEnum): |
| 423 |
ONE = 1 |
| 424 |
TWO = 2 |
| 425 |
FOUR = 4 |
| 426 |
EIGHT = 8 |
| 427 |
|
| 428 |
|
| 429 |
@attr.s |
| 430 |
class StringEnumParams: |
| 431 |
code = attr.ib() |
| 432 |
|
| 433 |
def _check_code(self, code): |
| 434 |
if code != self.code: |
| 435 |
raise InvalidType() |
| 436 |
|
| 437 |
@classmethod |
| 438 |
def get_code_size(cls): |
| 439 |
return 2 |
| 440 |
|
| 441 |
|
| 442 |
class StringEnum(StringEnumParsable, enum.Enum): |
| 443 |
ONE = StringEnumParams( |
| 444 |
code='one', |
| 445 |
) |
| 446 |
TWO = StringEnumParams( |
| 447 |
code='two', |
| 448 |
) |
| 449 |
THREE = StringEnumParams( |
| 450 |
code='three', |
| 451 |
) |
| 452 |
|
| 453 |
|
| 454 |
class StringEnumA(StringEnumParsable, enum.Enum): |
| 455 |
A = StringEnumParams(code='a') |
| 456 |
|
| 457 |
|
| 458 |
class StringEnumAA(StringEnumParsable, enum.Enum): |
| 459 |
AA = StringEnumParams(code='aa') |
| 460 |
|
| 461 |
|
| 462 |
class StringEnumAAA(StringEnumParsable, enum.Enum): |
| 463 |
AAA = StringEnumParams(code='aaa') |
| 464 |
|
| 465 |
|
| 466 |
class VariantParsableTest(VariantParsable): |
| 467 |
@classmethod |
| 468 |
def _get_variants(cls): |
| 469 |
return collections.OrderedDict([ |
| 470 |
(StringEnumA, [StringEnumA, ]), |
| 471 |
(StringEnumAA, [StringEnumAA, ]), |
| 472 |
(StringEnumAAA, [StringEnumAAA, ]), |
| 473 |
]) |
| 474 |
|
| 475 |
|
| 476 |
class VariantParsableExactTest(VariantParsableExact): |
| 477 |
@classmethod |
| 478 |
def _get_variants(cls): |
| 479 |
return collections.OrderedDict([ |
| 480 |
(StringEnumA, [StringEnumA, ]), |
| 481 |
(StringEnumAA, [StringEnumAA, ]), |
| 482 |
(StringEnumAAA, [StringEnumAAA, ]), |
| 483 |
]) |
| 484 |
|
| 485 |
|
| 486 |
class EnumStringValue(enum.Enum): |
| 487 |
ONE = 'one' |
| 488 |
TWO = 'two' |
| 489 |
THREE = 'three' |
| 490 |
|
| 491 |
|
| 492 |
class ListParamParsableTest(ListParamParsable): |
| 493 |
pass |
| 494 |
|
| 495 |
|
| 496 |
class ListParsableTest(ListParsable): |
| 497 |
@classmethod |
| 498 |
def get_param(cls): |
| 499 |
return ListParamParsableTest( |
| 500 |
item_class=AlwaysTestStringComposer, |
| 501 |
fallback_class=None, |
| 502 |
separator_class=ParserCRLF, |
| 503 |
) |
| 504 |
|
| 505 |
|
| 506 |
@attr.s |
| 507 |
class NByteEnumParam: |
| 508 |
code = attr.ib(validator=attr.validators.instance_of(int)) |
| 509 |
|
| 510 |
|
| 511 |
class NByteEnumTest(enum.Enum): |
| 512 |
ONE = NByteEnumParam(code=1) |
| 513 |
TWO = NByteEnumParam(code=2) |
| 514 |
THREE = NByteEnumParam(code=3) |
| 515 |
FOUR = NByteEnumParam(code=4) |
| 516 |
|
| 517 |
|
| 518 |
class OneByteEnumParsableTest(OneByteEnumParsable): |
| 519 |
@classmethod |
| 520 |
def get_enum_class(cls): |
| 521 |
return NByteEnumTest |
| 522 |
|
| 523 |
@abc.abstractmethod |
| 524 |
def compose(self): |
| 525 |
raise NotImplementedError() |
| 526 |
|
| 527 |
|
| 528 |
class OneByteEnumComposerTest(OneByteEnumComposer, enum.Enum): |
| 529 |
ONE = NByteEnumParam(code=1) |
| 530 |
TWO = NByteEnumParam(code=2) |
| 531 |
THREE = NByteEnumParam(code=3) |
| 532 |
FOUR = NByteEnumParam(code=4) |
| 533 |
|
| 534 |
|
| 535 |
class TwoByteEnumParsableTest(TwoByteEnumParsable): |
| 536 |
@classmethod |
| 537 |
def get_enum_class(cls): |
| 538 |
return NByteEnumTest |
| 539 |
|
| 540 |
@abc.abstractmethod |
| 541 |
def compose(self): |
| 542 |
raise NotImplementedError() |
| 543 |
|
| 544 |
|
| 545 |
class TwoByteEnumComposerTest(TwoByteEnumComposer, enum.Enum): |
| 546 |
ONE = NByteEnumParam(code=1) |
| 547 |
TWO = NByteEnumParam(code=2) |
| 548 |
THREE = NByteEnumParam(code=3) |
| 549 |
FOUR = NByteEnumParam(code=4) |
| 550 |
|
| 551 |
|
| 552 |
class ThreeByteEnumParsableTest(ThreeByteEnumParsable): |
| 553 |
@classmethod |
| 554 |
def get_enum_class(cls): |
| 555 |
return NByteEnumTest |
| 556 |
|
| 557 |
@abc.abstractmethod |
| 558 |
def compose(self): |
| 559 |
raise NotImplementedError() |
| 560 |
|
| 561 |
|
| 562 |
class ThreeByteEnumComposerTest(ThreeByteEnumComposer, enum.Enum): |
| 563 |
ONE = NByteEnumParam(code=1) |
| 564 |
TWO = NByteEnumParam(code=2) |
| 565 |
THREE = NByteEnumParam(code=3) |
| 566 |
FOUR = NByteEnumParam(code=4) |
| 567 |
|
| 568 |
|
| 569 |
class FourByteEnumParsableTest(FourByteEnumParsable): |
| 570 |
@classmethod |
| 571 |
def get_enum_class(cls): |
| 572 |
return NByteEnumTest |
| 573 |
|
| 574 |
@abc.abstractmethod |
| 575 |
def compose(self): |
| 576 |
raise NotImplementedError() |
| 577 |
|
| 578 |
|
| 579 |
class FourByteEnumComposerTest(FourByteEnumComposer, enum.Enum): |
| 580 |
ONE = NByteEnumParam(code=1) |
| 581 |
TWO = NByteEnumParam(code=2) |
| 582 |
THREE = NByteEnumParam(code=3) |
| 583 |
FOUR = NByteEnumParam(code=4) |
| 584 |
|
| 585 |
|
| 586 |
class FieldValueTimeDeltaTest(FieldValueTimeDelta): |
| 587 |
@classmethod |
| 588 |
def get_name(cls): |
| 589 |
return 'testTimeDelta' |
| 590 |
|
| 591 |
|
| 592 |
class FieldValueEnumTest(StringEnumCaseInsensitiveParsable, enum.Enum): |
| 593 |
FIRST = FieldValueStringEnumParams(code='first', human_readable_name='FiRsT') |
| 594 |
SECOND = FieldValueStringEnumParams(code='second') |
| 595 |
|
| 596 |
|
| 597 |
class FieldValueStringEnumTest(FieldValueStringEnum): |
| 598 |
@classmethod |
| 599 |
def _get_value_type(cls): |
| 600 |
return FieldValueEnumTest |
| 601 |
|
| 602 |
|
| 603 |
class FieldValueComponentOptionTest(FieldValueComponentOption): |
| 604 |
@classmethod |
| 605 |
def get_canonical_name(cls): |
| 606 |
return 'testOption' |
| 607 |
|
| 608 |
|
| 609 |
class FieldValueComponentStringTest(FieldValueComponentString): |
| 610 |
@classmethod |
| 611 |
def get_canonical_name(cls): |
| 612 |
return 'testString' |
| 613 |
|
| 614 |
|
| 615 |
class FieldValueComponentUrlTest(FieldValueComponentUrl): |
| 616 |
@classmethod |
| 617 |
def get_canonical_name(cls): |
| 618 |
return 'testUrl' |
| 619 |
|
| 620 |
|
| 621 |
class ComponentStringEnumTest(StringEnumParsable, enum.Enum): |
| 622 |
ONE = FieldValueComponentStringEnumParams( |
| 623 |
code='one' |
| 624 |
) |
| 625 |
TWO = FieldValueComponentStringEnumParams( |
| 626 |
code='two' |
| 627 |
) |
| 628 |
THREE = FieldValueComponentStringEnumParams( |
| 629 |
code='three' |
| 630 |
) |
| 631 |
|
| 632 |
|
| 633 |
class FieldValueComponentStringBase64Test(FieldValueComponentStringBase64): |
| 634 |
@classmethod |
| 635 |
def get_canonical_name(cls): |
| 636 |
return 'testStringBase64' |
| 637 |
|
| 638 |
|
| 639 |
class FieldValueComponentBoolTest(FieldValueComponentBool): |
| 640 |
@classmethod |
| 641 |
def get_canonical_name(cls): |
| 642 |
return 'testBool' |
| 643 |
|
| 644 |
|
| 645 |
class FieldValueComponentFloatTest(FieldValueComponentFloat): |
| 646 |
@classmethod |
| 647 |
def get_canonical_name(cls): |
| 648 |
return 'testFloat' |
| 649 |
|
| 650 |
|
| 651 |
class FieldValueComponentStringEnumTest(FieldValueComponentStringEnum): |
| 652 |
@classmethod |
| 653 |
def get_canonical_name(cls): |
| 654 |
return 'testStringEnum' |
| 655 |
|
| 656 |
@classmethod |
| 657 |
def _get_value_type(cls): |
| 658 |
return ComponentStringEnumTest |
| 659 |
|
| 660 |
|
| 661 |
class FieldValueComponentOptionalStringTest(FieldValueComponentQuotedString): |
| 662 |
@classmethod |
| 663 |
def get_canonical_name(cls): |
| 664 |
return 'testOptionalString' |
| 665 |
|
| 666 |
|
| 667 |
class FieldValueComponentQuotedStringTest(FieldValueComponentQuotedString): |
| 668 |
@classmethod |
| 669 |
def get_canonical_name(cls): |
| 670 |
return 'testQuotedString' |
| 671 |
|
| 672 |
|
| 673 |
class FieldValueComponentNumberTest(FieldValueComponentNumber): |
| 674 |
@classmethod |
| 675 |
def get_canonical_name(cls): |
| 676 |
return 'testNumber' |
| 677 |
|
| 678 |
|
| 679 |
class FieldValueComponentPercentTest(FieldValueComponentPercent): |
| 680 |
@classmethod |
| 681 |
def get_canonical_name(cls): |
| 682 |
return 'testPercent' |
| 683 |
|
| 684 |
|
| 685 |
class FieldValueComponentTimeDeltaTest(FieldValueComponentTimeDelta): |
| 686 |
@classmethod |
| 687 |
def get_canonical_name(cls): |
| 688 |
return 'testTimeDelta' |
| 689 |
|
| 690 |
|
| 691 |
@attr.s |
| 692 |
class FieldValueComplexTestBase: # pylint: disable=too-many-instance-attributes |
| 693 |
time_delta = attr.ib( |
| 694 |
converter=FieldValueComponentTimeDeltaTest.convert, |
| 695 |
validator=attr.validators.instance_of(FieldValueComponentTimeDeltaTest) |
| 696 |
) |
| 697 |
string = attr.ib( |
| 698 |
converter=FieldValueComponentStringTest.convert, |
| 699 |
validator=attr.validators.instance_of(FieldValueComponentStringTest), |
| 700 |
default='default' |
| 701 |
) |
| 702 |
url = attr.ib( |
| 703 |
converter=FieldValueComponentUrlTest.convert, |
| 704 |
validator=attr.validators.instance_of(FieldValueComponentUrlTest), |
| 705 |
default='https://example.com' |
| 706 |
) |
| 707 |
base64_string = attr.ib( |
| 708 |
converter=FieldValueComponentStringBase64Test.convert, |
| 709 |
validator=attr.validators.instance_of(FieldValueComponentStringBase64Test), |
| 710 |
default=base64.b64encode('default'.encode('ascii')).decode('ascii') |
| 711 |
) |
| 712 |
number = attr.ib( |
| 713 |
converter=FieldValueComponentNumberTest.convert, |
| 714 |
validator=attr.validators.instance_of(FieldValueComponentNumberTest), |
| 715 |
default=0 |
| 716 |
) |
| 717 |
percent = attr.ib( |
| 718 |
converter=FieldValueComponentPercentTest.convert, |
| 719 |
validator=attr.validators.instance_of(FieldValueComponentPercentTest), |
| 720 |
default=100 |
| 721 |
) |
| 722 |
|
| 723 |
|
| 724 |
@attr.s |
| 725 |
class FieldValueMultipleTest( # pylint: disable=too-many-instance-attributes |
| 726 |
FieldsSemicolonSeparated, FieldValueComplexTestBase): |
| 727 |
option = attr.ib( |
| 728 |
converter=FieldValueComponentOptionTest.convert, |
| 729 |
validator=attr.validators.instance_of(FieldValueComponentOptionTest), |
| 730 |
default=False |
| 731 |
) |
| 732 |
optional_string = attr.ib( |
| 733 |
converter=attr.converters.optional(FieldValueComponentOptionalStringTest.convert), |
| 734 |
validator=attr.validators.optional( |
| 735 |
attr.validators.instance_of(FieldValueComponentOptionalStringTest) |
| 736 |
), |
| 737 |
default=None |
| 738 |
) |
| 739 |
|
| 740 |
|
| 741 |
class FieldValueJsonTest( # pylint: disable=too-many-instance-attributes |
| 742 |
FieldsJson, FieldValueComplexTestBase): |
| 743 |
pass |
| 744 |
|
| 745 |
|
| 746 |
@attr.s |
| 747 |
class FieldValueMultipleExtendableTest(FieldValueMultipleTest): |
| 748 |
extensions = attr.ib( |
| 749 |
default=None, |
| 750 |
validator=attr.validators.optional(attr.validators.instance_of(NameValuePairListSemicolonSeparated)), |
| 751 |
metadata={'extension': True}, |
| 752 |
) |
| 753 |
|
| 754 |
|
| 755 |
class SerializableUpperCaseEncoder(SerializableTextEncoder): |
| 756 |
def __call__(self, obj, level): |
| 757 |
_, string_result = super().__call__(obj, level) |
| 758 |
|
| 759 |
return False, string_result.upper() |
| 760 |
|
| 761 |
|
| 762 |
class TestClasses: |
| 763 |
class TestKeyBase(pyfakefs.fake_filesystem_unittest.TestCase): |
| 764 |
def setUp(self): |
| 765 |
self.setUpPyfakefs() |
| 766 |
|
| 767 |
self.__certs_dir = pathlib.PurePath(__file__).parent.parent / 'common' / 'certs' |
| 768 |
if not (pathlib.Path(__file__).parent.parent / 'common' / 'certs').exists(): |
| 769 |
raise unittest.SkipTest("path not present") |
| 770 |
self.fs.add_real_directory(str(self.__certs_dir)) |
| 771 |
|
| 772 |
def _get_pem_str(self, public_key_file_name): |
| 773 |
public_key_path = self.__certs_dir / public_key_file_name |
| 774 |
with open(str(public_key_path), encoding='ascii') as pem_file: |
| 775 |
return pem_file.read() |
| 776 |
|
| 777 |
def _get_public_key_x509(self, public_key_file_name): |
| 778 |
return PublicKeyX509.from_pem(self._get_pem_str(public_key_file_name)) |
| 779 |
|
| 780 |
|
| 781 |
class NumericRangeParsableTest(NumericRangeParsableBase): |
| 782 |
@classmethod |
| 783 |
def _get_value_min(cls): |
| 784 |
return 0x01 |
| 785 |
|
| 786 |
@classmethod |
| 787 |
def _get_value_max(cls): |
| 788 |
return 0xfe |
| 789 |
|
| 790 |
@classmethod |
| 791 |
def _get_value_length(cls): |
| 792 |
return 1 |