Пример #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_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
Пример #3
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'
Пример #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_pass(self, args, encoded):
     stream = BytesIO(bytearray.fromhex(encoded))
     record = Record._decode(stream, 'strict', {})[0]
     assert record == Record(*args)
Пример #6
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
Пример #7
0
 def test_noteq(self, args1, args2):
     assert Record(*args1) != Record(*args2)
Пример #8
0
 def test_equal(self, args):
     assert Record(*args) == Record(*args)
Пример #9
0
 def test_format_str(self, args, string):
     assert "{!s}".format(Record(*args)) == "NDEF Record " + string
     assert "{}".format(Record(*args)) == "NDEF Record " + string
Пример #10
0
 def test_pass(self):
     record_1 = Record(type='text/plain', name='name', data='hello')
     record_2 = Record(b'text/plain', b'name', b'hello')
     assert record_1.type == record_2.type
     assert record_1.name == record_2.name
     assert record_1.data == record_2.data
Пример #11
0
 def test_format_args(self, args, string):
     assert "{:args}".format(Record(*args)) == string
Пример #12
0
 def test_instance(self):
     assert isinstance(Record().data, bytearray)
Пример #13
0
 def test_instance(self):
     assert isinstance(Record().name, str)
Пример #14
0
 def test_update(self):
     with pytest.raises(AttributeError):
         Record().type = ''
Пример #15
0
 def test_fail(self):
     with pytest.raises(TypeError):
         Record(undefined_keyword='abc')
Пример #16
0
 def test_pass(self, args, _type, _name, _data):
     record = Record(*args)
     assert record.type == _type
     assert record.name == _name
     assert record.data == _data
Пример #17
0
 def test_format_repr(self, args, string):
     string = "ndef.record.Record({})".format(string)
     assert "{!r}".format(Record(*args)) == string
Пример #18
0
 def test_fail(self, args, errstr):
     with pytest.raises((TypeError, ValueError)) as excinfo:
         Record(*args)
     assert str(excinfo.value) == "ndef.record.Record" + errstr