예제 #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'')
예제 #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
예제 #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
예제 #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'
예제 #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'
예제 #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
예제 #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
예제 #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
예제 #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'
예제 #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
예제 #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
예제 #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
예제 #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
예제 #14
0
 def test_pass(self, args, encoded):
     stream = BytesIO(bytearray.fromhex(encoded))
     record = Record._decode(stream, 'strict', {})[0]
     assert record == Record(*args)
예제 #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
예제 #16
0
 def test_pass(self, value):
     assert Record._value_to_unicode(value, 'value') == u'abc'
예제 #17
0
 def test_pass(self, value):
     assert Record._value_to_unicode(value, 'value') == u'abc'
예제 #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
예제 #19
0
 def test_pass(self, value):
     assert Record._value_to_latin(value, 'value') == '\xe4bc'
예제 #20
0
 def test_pass(self, value):
     assert Record._value_to_ascii(value, 'value') == 'abc'
예제 #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
예제 #22
0
 def test_struct(self, fmt, octets, offset, values):
     octets = bytearray.fromhex(octets)
     assert Record._decode_struct(fmt, octets, offset) == values
예제 #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
예제 #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
예제 #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
예제 #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
예제 #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
예제 #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
예제 #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
예제 #30
0
 def test_instance(self):
     assert isinstance(Record().name, str)
예제 #31
0
 def test_struct(self, fmt, values, octets):
     octets = bytearray.fromhex(octets)
     assert Record._encode_struct(fmt, *values) == octets
예제 #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
예제 #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)
예제 #34
0
 def test_instance(self):
     assert isinstance(Record().data, bytearray)
예제 #35
0
 def test_pass(self, args, encoded):
     stream = BytesIO(bytearray.fromhex(encoded))
     record = Record._decode(stream, 'strict', {})[0]
     assert record == Record(*args)
예제 #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)
예제 #37
0
 def test_struct(self, fmt, values, octets):
     octets = bytearray.fromhex(octets)
     assert Record._encode_struct(fmt, *values) == octets
예제 #38
0
 def test_format_args(self, args, string):
     assert "{:args}".format(Record(*args)) == string
예제 #39
0
 def test_struct(self, fmt, octets, offset, values):
     octets = bytearray.fromhex(octets)
     assert Record._decode_struct(fmt, octets, offset) == values
예제 #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
예제 #41
0
 def test_pass(self, value):
     assert Record._value_to_ascii(value, 'value') == 'abc'
예제 #42
0
 def test_format_str(self, args, string):
     assert "{!s}".format(Record(*args)) == "NDEF Record " + string
     assert "{}".format(Record(*args)) == "NDEF Record " + string
예제 #43
0
 def test_pass(self, value):
     assert Record._value_to_latin(value, 'value') == '\xe4bc'
예제 #44
0
 def test_equal(self, args):
     assert Record(*args) == Record(*args)
예제 #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
예제 #46
0
 def test_noteq(self, args1, args2):
     assert Record(*args1) != Record(*args2)
예제 #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
예제 #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
예제 #49
0
 def test_format_repr(self, args, string):
     string = "ndef.record.Record({})".format(string)
     assert "{!r}".format(Record(*args)) == string
예제 #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