Exemple #1
0
    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
Exemple #2
0
    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
Exemple #3
0
 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
Exemple #4
0
 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
Exemple #5
0
 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
Exemple #6
0
 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)
Exemple #7
0
 def test_pass(self, args, encoded):
     stream = BytesIO(bytearray.fromhex(encoded))
     record = Record._decode(stream, 'strict', {})[0]
     assert record == Record(*args)
Exemple #8
0
 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
Exemple #9
0
 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)
Exemple #10
0
 def test_pass(self, args, encoded):
     stream = BytesIO(bytearray.fromhex(encoded))
     record = Record._decode(stream, 'strict', {})[0]
     assert record == Record(*args)