Пример #1
0
 def test_init(self):
     obj = ndef.BluetoothLowEnergyRecord()
     assert isinstance(obj, eval(self.cls))
     assert len(obj.keys()) == len(obj.values()) == len(obj.items()) == 0
     obj = ndef.BluetoothLowEnergyRecord((0x08, b'My Blue'), (0xFF, b'abc'))
     assert len(obj.keys()) == len(obj.values()) == len(obj.items()) == 2
     assert obj.get(0x08) == b'My Blue'
     assert obj.get('Shortened Local Name') == b'My Blue'
     assert obj.get(0xFF) == b'abc'
     assert obj.get('Manufacturer Specific Data') == b'abc'
     assert 0x01 not in obj
     assert obj.setdefault('Flags', b'\x00') == b'\x00'
     for key in obj:
         assert obj.get(key) is not None
Пример #2
0
 def test_attr_secure_connections_random_value(self):
     obj = ndef.BluetoothLowEnergyRecord()
     assert obj.secure_connections_random_value is None
     obj[0x23] = b'\1' + 15 * b'\0'
     assert obj.secure_connections_random_value == 1
     obj.secure_connections_random_value = 2
     assert obj[0x23] == b'\2' + 15 * b'\0'
Пример #3
0
 def test_attr_security_manager_tk_value(self):
     obj = ndef.BluetoothLowEnergyRecord()
     assert obj.security_manager_tk_value is None
     obj[0x10] = b'\1' + 15 * b'\0'
     assert obj.security_manager_tk_value == 1
     obj.security_manager_tk_value = 2
     assert obj[0x10] == b'\2' + 15 * b'\0'
Пример #4
0
 def test_encode(self):
     obj = ndef.BluetoothLowEnergyRecord()
     assert obj.type == 'application/vnd.bluetooth.le.oob'
     assert obj.data == b''
     obj.device_address = ("01:02:03:04:05:06", "public")
     assert obj.data == b'\x08\x1b\x06\x05\x04\x03\x02\x01\x00'
     obj['Manufacturer Specific Data'] = b'abc'
     assert obj.data == b'\x08\x1b\x06\x05\x04\x03\x02\x01\x00\x04\xffabc'
Пример #5
0
 def test_attr_role_capabilities(self, octets, string):
     obj = ndef.BluetoothLowEnergyRecord()
     assert obj.role_capabilities is None
     obj['LE Role'] = octets
     assert obj.role_capabilities == string
     del obj['LE Role']
     if not string.startswith("Reserved"):
         obj.role_capabilities = string
         assert obj['LE Role'] == octets
     else:
         with pytest.raises(ValueError) as excinfo:
             obj.role_capabilities = string
         assert "undefined role capability" in str(excinfo.value)
Пример #6
0
 def test_attr_flags(self):
     obj = ndef.BluetoothLowEnergyRecord()
     assert obj.flags is None
     for index, string in enumerate(obj._flags):
         obj['Flags'] = struct.pack('B', 1 << index)
         assert obj.flags == (1 << index, string)
     for index, string in enumerate(obj._flags):
         obj.flags = 1 << index
         assert obj['Flags'] == struct.pack('B', 1 << index)
     for index, string in enumerate(obj._flags):
         obj.flags = (string,)
         assert obj['Flags'] == struct.pack('B', 1 << index)
     obj.flags = int(len(obj._flags) * '1', 2)
     assert obj.flags == (int(len(obj._flags) * '1', 2),) + obj._flags
     assert obj['Flags'] == struct.pack('B', int(len(obj._flags) * '1', 2))
Пример #7
0
 def test_attr_device_name(self):
     obj = ndef.BluetoothLowEnergyRecord()
     assert obj.device_name == ''
     obj[0x08] = b'My Blue'
     assert 0x08 in obj and 0x09 not in obj
     assert obj.device_name == 'My Blue'
     obj[0x09] = b'My Bluetooth Device'
     assert 0x08 in obj and 0x09 in obj
     assert obj.device_name == 'My Bluetooth Device'
     obj.device_name = 'My Bluetooth Device'
     assert obj[0x09] == b'My Bluetooth Device'
     assert 0x08 not in obj
     obj.device_name = 'My Device'
     assert obj[0x09] == b'My Device'
     assert 0x08 not in obj
Пример #8
0
 def test_attr_appearance(self):
     obj = ndef.BluetoothLowEnergyRecord()
     assert obj.appearance is None
     for number, string in obj._appearance_map.items():
         obj['Appearance'] = struct.pack('<H', number)
         assert obj.appearance == (number, string)
     for number, string in obj._appearance_map.items():
         obj.appearance = number
         assert obj['Appearance'] == struct.pack('<H', number)
     for number, string in obj._appearance_map.items():
         obj.appearance = string
         assert obj['Appearance'] == struct.pack('<H', number)
     with pytest.raises(ValueError) as excinfo:
         obj.appearance = "abcdef"
     assert "unknown appearance value string 'abcdef'" in str(excinfo.value)
     with pytest.raises(ndef.EncodeError):
         obj.appearance = -1
     with pytest.raises(ndef.EncodeError):
         obj.appearance = tuple()
Пример #9
0
 def test_attr_appearance_strings(self):
     obj = ndef.BluetoothLowEnergyRecord()
     assert isinstance(obj.appearance_strings, list)
     assert len(obj.appearance_strings) == 49
     assert obj.appearance_strings[0:2] == ["Unknown", "Phone"]
Пример #10
0
 def test_attr_device_address(self, device_address):
     obj = ndef.BluetoothLowEnergyRecord()
     assert obj.device_address is None
     obj.device_address = device_address
     assert obj.device_address.addr == "01:02:03:04:05:06"
     assert obj.device_address.type == "public"
Пример #11
0
 def test_key_value_error(self):
     with pytest.raises(ValueError) as excinfo:
         ndef.BluetoothLowEnergyRecord().get('invalid name')
     assert str(excinfo.value) == "unknown attribute name 'invalid name'"
Пример #12
0
 def test_key_name_mapping(self, key, name):
     obj = ndef.BluetoothLowEnergyRecord((key, b'abc'))
     assert name in obj.attribute_names
     assert obj[name] == b'abc'