Exemplo n.º 1
0
 def test_update(self):
     record = Record('unknown', '', 'abc')
     assert record.data == b'abc'
     record.data.extend(b'def')
     assert record.data == b'abcdef'
     with pytest.raises(AttributeError):
         Record().data = bytearray(b'')
Exemplo n.º 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
Exemplo n.º 3
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
Exemplo n.º 4
0
 def test_update(self):
     record = Record()
     assert record.name == ''
     record.name = 255 * 'a'
     assert record.name == 255 * 'a'
     with pytest.raises(TypeError):
         record.name = 1
     with pytest.raises(ValueError):
         record.name = 256 * 'a'
Exemplo n.º 5
0
 def test_update(self):
     record = Record()
     assert record.name == ''
     record.name = 255 * 'a'
     assert record.name == 255 * 'a'
     with pytest.raises(TypeError):
         record.name = 1
     with pytest.raises(ValueError):
         record.name = 256 * 'a'
Exemplo n.º 6
0
 def test_limit(self):
     stream = BytesIO()
     record = Record('unknown', '', 0x100000 * b'\0')
     octets = bytearray.fromhex('050000100000') + 0x100000 * b'\0'
     assert record._encode(stream=stream) == len(octets)
     assert stream.getvalue() == octets
     record = Record('unknown', '', 0x100001 * b'\0')
     errstr = "payload of more than 1048576 octets can not be encoded"
     with pytest.raises(ndef.EncodeError) as excinfo:
         record._encode(stream=stream)
     assert str(excinfo.value) == 'ndef.record.Record ' + errstr
Exemplo n.º 7
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
Exemplo n.º 8
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
Exemplo n.º 9
0
def test_handover_initiate_record_attributes():
    record = HandoverInitiateRecord()
    record.add_alternative_carrier('active', 'wifi', 'a1', 'a2')
    record.add_alternative_carrier('inactive', 'bt31', 'a3')
    record.unknown_records.append(Record('text/plain', 'txt', 'Hello'))
    octets = b''.join(message_encoder([record]))
    record = list(message_decoder(octets))[0]
    assert isinstance(record, HandoverInitiateRecord)
    assert record.type == 'urn:nfc:wkt:Hi'
    assert record.name == ''
    assert record.hexversion == 0x13
    assert record.version_info == (1, 3)
    assert record.version_string == "1.3"
    assert len(record.alternative_carriers) == 2
    assert len(record.alternative_carriers[0].auxiliary_data_reference) == 2
    assert len(record.alternative_carriers[1].auxiliary_data_reference) == 1
    assert record.alternative_carriers[0].carrier_power_state == 'active'
    assert record.alternative_carriers[0].carrier_data_reference == 'wifi'
    assert record.alternative_carriers[0].auxiliary_data_reference[0] == 'a1'
    assert record.alternative_carriers[0].auxiliary_data_reference[1] == 'a2'
    assert record.alternative_carriers[1].carrier_power_state == 'inactive'
    assert record.alternative_carriers[1].carrier_data_reference == 'bt31'
    assert record.alternative_carriers[1].auxiliary_data_reference[0] == 'a3'
    assert len(record.unknown_records) == 1
    assert record.unknown_records[0].type == 'text/plain'
    assert record.unknown_records[0].name == 'txt'
    assert record.unknown_records[0].data == b'Hello'
Exemplo n.º 10
0
 def test_limit(self):
     stream = BytesIO()
     record = Record('unknown', '', 0x100000 * b'\0')
     octets = bytearray.fromhex('050000100000') + 0x100000 * b'\0'
     assert record._encode(stream=stream) == len(octets)
     assert stream.getvalue() == octets
     record = Record('unknown', '', 0x100001 * b'\0')
     errstr = "payload of more than 1048576 octets can not be encoded"
     with pytest.raises(ndef.EncodeError) as excinfo:
         record._encode(stream=stream)
     assert str(excinfo.value) == 'ndef.record.Record ' + errstr
Exemplo n.º 11
0
 def test_pass(self, record_type, TNF, TYPE):
     assert Record._decode_type(TNF, TYPE) == record_type
     assert type(Record._decode_type(TNF, TYPE)) == str
Exemplo n.º 12
0
 def test_decode_payload_is_not_implemented(self):
     errstr = "must implement the _decode_payload() method"
     with pytest.raises(NotImplementedError) as excinfo:
         Record._decode_payload(b'', 'strict')
     assert str(excinfo.value) == 'ndef.record.Record ' + errstr
Exemplo n.º 13
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
Exemplo n.º 14
0
 def test_pass(self, args, encoded):
     stream = BytesIO(bytearray.fromhex(encoded))
     record = Record._decode(stream, 'strict', {})[0]
     assert record == Record(*args)
Exemplo n.º 15
0
 def test_pass(self, args, _type, _name, _data):
     record = Record(*args)
     assert record.type == _type
     assert record.name == _name
     assert record.data == _data
Exemplo n.º 16
0
 def test_pass(self, value):
     assert Record._value_to_unicode(value, 'value') == u'abc'
Exemplo n.º 17
0
 def test_pass(self, value):
     assert Record._value_to_unicode(value, 'value') == u'abc'
Exemplo n.º 18
0
 def test_fail(self, value, errstr):
     with pytest.raises((TypeError, ValueError)) as excinfo:
         Record._value_to_ascii(value, 'value')
     assert str(excinfo.value) == "ndef.record.Record value " + errstr
Exemplo n.º 19
0
 def test_pass(self, value):
     assert Record._value_to_latin(value, 'value') == '\xe4bc'
Exemplo n.º 20
0
 def test_pass(self, value):
     assert Record._value_to_ascii(value, 'value') == 'abc'
Exemplo n.º 21
0
 def test_fail(self):
     errstr = "ndef.record.Record NDEF Record TNF values must be 0 to 6"
     with pytest.raises((TypeError, ValueError)) as excinfo:
         Record._decode_type(7, b'')
     assert str(excinfo.value) == errstr
Exemplo n.º 22
0
 def test_struct(self, fmt, octets, offset, values):
     octets = bytearray.fromhex(octets)
     assert Record._decode_struct(fmt, octets, offset) == values
Exemplo n.º 23
0
 def test_fail(self):
     errstr = "ndef.record.Record NDEF Record TNF values must be 0 to 6"
     with pytest.raises((TypeError, ValueError)) as excinfo:
         Record._decode_type(7, b'')
     assert str(excinfo.value) == errstr
Exemplo n.º 24
0
 def test_fail(self, value, errstr):
     errstr = errstr.format(b=('b', '')[ndef.record._PY2])
     with pytest.raises((TypeError, ValueError)) as excinfo:
         Record._value_to_unicode(value, 'value')
     assert str(excinfo.value) == "ndef.record.Record value " + errstr
Exemplo n.º 25
0
 def test_fail(self, value, errstr):
     with pytest.raises((TypeError, ValueError)) as excinfo:
         Record._value_to_ascii(value, 'value')
     assert str(excinfo.value) == "ndef.record.Record value " + errstr
Exemplo n.º 26
0
 def test_pass(self, record_type, TNF, TYPE):
     assert Record._encode_type(record_type) == (TNF, TYPE)
     assert type(Record._encode_type(record_type)[1]) == bytes
Exemplo n.º 27
0
 def test_pass(self, record_type, TNF, TYPE):
     assert Record._encode_type(record_type) == (TNF, TYPE)
     assert type(Record._encode_type(record_type)[1]) == bytes
Exemplo n.º 28
0
 def test_fail(self, record_type, errstr):
     with pytest.raises((TypeError, ValueError)) as excinfo:
         Record._encode_type(record_type)
     assert str(excinfo.value) == "ndef.record.Record " + errstr
Exemplo n.º 29
0
 def test_pass(self, record_type, TNF, TYPE):
     assert Record._decode_type(TNF, TYPE) == record_type
     assert type(Record._decode_type(TNF, TYPE)) == str
Exemplo n.º 30
0
 def test_instance(self):
     assert isinstance(Record().name, str)
Exemplo n.º 31
0
 def test_struct(self, fmt, values, octets):
     octets = bytearray.fromhex(octets)
     assert Record._encode_struct(fmt, *values) == octets
Exemplo n.º 32
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
Exemplo n.º 33
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)
Exemplo n.º 34
0
 def test_instance(self):
     assert isinstance(Record().data, bytearray)
Exemplo n.º 35
0
 def test_pass(self, args, encoded):
     stream = BytesIO(bytearray.fromhex(encoded))
     record = Record._decode(stream, 'strict', {})[0]
     assert record == Record(*args)
Exemplo n.º 36
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)
Exemplo n.º 37
0
 def test_struct(self, fmt, values, octets):
     octets = bytearray.fromhex(octets)
     assert Record._encode_struct(fmt, *values) == octets
Exemplo n.º 38
0
 def test_format_args(self, args, string):
     assert "{:args}".format(Record(*args)) == string
Exemplo n.º 39
0
 def test_struct(self, fmt, octets, offset, values):
     octets = bytearray.fromhex(octets)
     assert Record._decode_struct(fmt, octets, offset) == values
Exemplo n.º 40
0
 def test_pass(self, args, encoded):
     stream = BytesIO()
     record = Record(*args)
     octets = bytearray.fromhex(encoded)
     assert record._encode(stream=stream) == len(octets)
     assert stream.getvalue() == octets
Exemplo n.º 41
0
 def test_pass(self, value):
     assert Record._value_to_ascii(value, 'value') == 'abc'
Exemplo n.º 42
0
 def test_format_str(self, args, string):
     assert "{!s}".format(Record(*args)) == "NDEF Record " + string
     assert "{}".format(Record(*args)) == "NDEF Record " + string
Exemplo n.º 43
0
 def test_pass(self, value):
     assert Record._value_to_latin(value, 'value') == '\xe4bc'
Exemplo n.º 44
0
 def test_equal(self, args):
     assert Record(*args) == Record(*args)
Exemplo n.º 45
0
 def test_fail(self, value, errstr):
     errstr = errstr.format(b=('b', '')[ndef.record._PY2])
     with pytest.raises((TypeError, ValueError)) as excinfo:
         Record._value_to_unicode(value, 'value')
     assert str(excinfo.value) == "ndef.record.Record value " + errstr
Exemplo n.º 46
0
 def test_noteq(self, args1, args2):
     assert Record(*args1) != Record(*args2)
Exemplo n.º 47
0
 def test_fail(self, record_type, errstr):
     with pytest.raises((TypeError, ValueError)) as excinfo:
         Record._encode_type(record_type)
     assert str(excinfo.value) == "ndef.record.Record " + errstr
Exemplo n.º 48
0
 def test_pass(self, args, encoded):
     stream = BytesIO()
     record = Record(*args)
     octets = bytearray.fromhex(encoded)
     assert record._encode(stream=stream) == len(octets)
     assert stream.getvalue() == octets
Exemplo n.º 49
0
 def test_format_repr(self, args, string):
     string = "ndef.record.Record({})".format(string)
     assert "{!r}".format(Record(*args)) == string
Exemplo n.º 50
0
 def test_decode_payload_is_not_implemented(self):
     errstr = "must implement the _decode_payload() method"
     with pytest.raises(NotImplementedError) as excinfo:
         Record._decode_payload(b'', 'strict')
     assert str(excinfo.value) == 'ndef.record.Record ' + errstr