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'')
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_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'
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
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_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'
def test_pass(self, record_type, TNF, TYPE): assert Record._decode_type(TNF, TYPE) == record_type assert type(Record._decode_type(TNF, TYPE)) == str
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
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_pass(self, args, encoded): stream = BytesIO(bytearray.fromhex(encoded)) record = Record._decode(stream, 'strict', {})[0] assert record == Record(*args)
def test_pass(self, args, _type, _name, _data): record = Record(*args) assert record.type == _type assert record.name == _name assert record.data == _data
def test_pass(self, value): assert Record._value_to_unicode(value, 'value') == u'abc'
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
def test_pass(self, value): assert Record._value_to_latin(value, 'value') == '\xe4bc'
def test_pass(self, value): assert Record._value_to_ascii(value, 'value') == 'abc'
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
def test_struct(self, fmt, octets, offset, values): octets = bytearray.fromhex(octets) assert Record._decode_struct(fmt, octets, offset) == values
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
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
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
def test_instance(self): assert isinstance(Record().name, str)
def test_struct(self, fmt, values, octets): octets = bytearray.fromhex(octets) assert Record._encode_struct(fmt, *values) == octets
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_instance(self): assert isinstance(Record().data, bytearray)
def test_format_args(self, args, string): assert "{:args}".format(Record(*args)) == string
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
def test_format_str(self, args, string): assert "{!s}".format(Record(*args)) == "NDEF Record " + string assert "{}".format(Record(*args)) == "NDEF Record " + string
def test_equal(self, args): assert Record(*args) == Record(*args)
def test_noteq(self, args1, args2): assert Record(*args1) != Record(*args2)
def test_format_repr(self, args, string): string = "ndef.record.Record({})".format(string) assert "{!r}".format(Record(*args)) == string