def test_crcelement_init_crc(crc: bytes) -> None: """Test CRCElement crc attr initialization.""" if (b'\x00\x00\x00\x00' <= crc <= b'\xff\xff\xff\xff') and (len(crc) == 4): crcelements.CRCElement(crc=crc) else: with pt.raises(ValueError): crcelements.CRCElement(crc=crc)
def test_crcelement_roundtrip(crc: bytes, payload: bytes, body: bytes) -> None: """Test CRCElement generate/parse roundtrip.""" generate_crc_element = crcelements.CRCElement() generate_crc_element.crc = crc generate_crc_element.payload = payload generate_crc_element.parse(generate_crc_element.get_body()) assert generate_crc_element.crc == crc assert generate_crc_element.payload == payload parse_crc_element = crcelements.CRCElement() parse_crc_element.parse(generate_crc_element.get_body()) assert parse_crc_element.crc == crc assert parse_crc_element.payload == payload assert parse_crc_element.get_body() == body
def test_crcelement_integrity_fail(crc: bytes, payload: bytes, _: bytes) -> None: """Test CRCElement data integrity fail check.""" crc_element = crcelements.CRCElement() crc_element.crc = crc crc_element.payload = payload with pt.raises(exceptions.ProtocolDataError): crc_element.check_integrity()
def test_crcelement_integrity_pass(crc: bytes, payload: bytes, _: bytes) -> None: """Test CRCElement data integrity pass check.""" crc_element = crcelements.CRCElement() crc_element.crc = crc crc_element.payload = payload try: crc_element.check_integrity() except exceptions.ProtocolDataError as err: pt.fail("Integrity check failed: {0}".format(err))
def test_crcelement_init_valid_crc(crc: bytes) -> None: """ Scenario: Valid CRC of data is generated/parsed from the input. """ # Given: A CRC constructor value which is within the allowed # range for 32-bit CRCs if (b'\x00\x00\x00\x00' <= crc <= b'\xff\xff\xff\xff') and (len(crc) == 4): # When: A CRCElement is constructed using it. # Then: ValueError should not be raised. crcelements.CRCElement(crc=crc)
def test_crcelement_parse(crc: bytes, payload: bytes, body: bytes) -> None: """ Scenario: The system reads file/processes message from mcu/frontend. """ # Given: A CRC element. crc_element = crcelements.CRCElement() # When: The CRC Element parses a body of valid size. crc_element.parse(body) # Then: The CRC checksum is parsed correctly from the header. # And: The payload is parsed correctly. assert crc_element.crc == crc assert crc_element.payload == payload
def test_crcelement_init_invalid_crc(crc: bytes) -> None: # pylint: disable=invalid-name """ Scenario: Invalid CRC of data is generated/parsed from the input. """ # Given: A CRC constructor value which is outside the allowed # range for 32-bit CRCs if not ( (b'\x00\x00\x00\x00' <= crc <= b'\xff\xff\xff\xff')\ and (len(crc) == 4) ): # When: A CRCElement is constructed using it. # Then: ValueError should be raised. with pt.raises(ValueError): crcelements.CRCElement(crc=crc)
def test_crcelement_integrity_pass(crc: bytes, payload: bytes, _: bytes) -> None: """ Scenario: The system reads file/processes message from mcu/frontend. The message has been parsed """ # Given: We have CRCElement object. crc_element = crcelements.CRCElement() crc_element.crc = crc crc_element.payload = payload try: # When: The CRCElement object checks the integrity of a payload and # the expected CRC, which are consistent with each other. crc_element.check_integrity() # Then: ProtocolDataError should not be raised. except exceptions.ProtocolDataError as err: pt.fail("Integrity check failed: {0}".format(err))
def test_crcelement_parse(crc: bytes, payload: bytes, body: bytes) -> None: """Test CRCElement parsing from body.""" crc_element = crcelements.CRCElement() crc_element.parse(body) assert crc_element.crc == crc assert crc_element.payload == payload
def test_crcelement_invalid_crc_input(payload: bytes) -> None: # pylint: disable=invalid-name """Test CRCElement CRC-32C invalid input.""" crc_element = crcelements.CRCElement() crc_element.payload = payload with pt.raises(exceptions.ProtocolDataError): _ = crc_element._compute_crc() # pylint: disable=protected-access
def test_crcelement_compute_crc(payload: bytes, expected: bytes) -> None: """Test CRCElement CRC-32C computation.""" crc_element = crcelements.CRCElement() crc_element.payload = payload assert crc_element._compute_crc() == expected # pylint: disable=protected-access
def test_crcelement_generate(crc: bytes, payload: bytes, body: bytes) -> None: """Test CRCElement generation of body.""" crc_element = crcelements.CRCElement() crc_element.crc = crc crc_element.payload = payload assert crc_element.get_body() == body
def test_crcelement_parse_invalids(body: bytes) -> None: """Test CRCElement parse invalids.""" crc_element = crcelements.CRCElement() with pt.raises(exceptions.ProtocolDataError): crc_element.parse(body)
def test_crcelement_parse_invariants(crc: bytes, payload: bytes) -> None: """Test CRCElement parse invariants.""" crc_element = crcelements.CRCElement() crc_element.parse(crc + payload) assert crc_element.crc == crc assert crc_element.payload == payload