def test_decode_known_type(self): class MyRecord(Record): _type = 'urn:nfc:wkt:x' _decode_min_payload_length = 1 _decode_max_payload_length = 1 @classmethod def _decode_payload(cls, octets, errors): return MyRecord() known_types = {MyRecord._type: MyRecord} stream = BytesIO(bytearray.fromhex('1101017800')) record = Record._decode(stream, 'strict', known_types)[0] assert type(record) == MyRecord errstr = 'payload length can not be less than 1' stream = BytesIO(bytearray.fromhex('11010078')) with pytest.raises(ndef.DecodeError) as excinfo: Record._decode(stream, 'strict', known_types) assert str(excinfo.value) == 'test_record.MyRecord ' + errstr errstr = 'payload length can not be more than 1' stream = BytesIO(bytearray.fromhex('110102780000')) with pytest.raises(ndef.DecodeError) as excinfo: Record._decode(stream, 'strict', known_types) assert str(excinfo.value) == 'test_record.MyRecord ' + errstr
def test_limit(self): octets = bytearray.fromhex('') record = Record._decode(BytesIO(octets), 'strict', {})[0] assert record is None octets = bytearray.fromhex('050000100000') + 0x100000 * b'\0' record = Record._decode(BytesIO(octets), 'strict', {})[0] assert len(record.data) == 0x100000 errstr = "payload of more than 1048576 octets can not be decoded" octets = bytearray.fromhex('050000100001') + 0x100001 * b'\0' with pytest.raises(ndef.DecodeError) as excinfo: Record._decode(BytesIO(octets), 'strict', {}) assert str(excinfo.value) == 'ndef.record.Record ' + errstr
def test_flags(self, encoded, _mb, _me, _cf): stream = BytesIO(bytearray.fromhex(encoded)) record, mb, me, cf = Record._decode(stream, 'strict', {}) assert mb == _mb assert me == _me assert cf == _cf
def test_fail(self, encoded, errstr): with pytest.raises(ndef.DecodeError) as excinfo: stream = BytesIO(bytearray.fromhex(encoded)) Record._decode(stream, 'strict', {}) assert errstr in str(excinfo.value)
def test_pass(self, args, encoded): stream = BytesIO(bytearray.fromhex(encoded)) record = Record._decode(stream, 'strict', {})[0] assert record == Record(*args)