Пример #1
0
 def test_encode(self):
     obj = ndef.BluetoothEasyPairingRecord('01:02:03:04:05:06')
     assert obj.type == 'application/vnd.bluetooth.ep.oob'
     assert obj.data == b'\x08\x00\x06\x05\x04\x03\x02\x01'
     obj = ndef.BluetoothEasyPairingRecord('01:02:03:04:05:06', (255, b'a'))
     assert obj.type == 'application/vnd.bluetooth.ep.oob'
     assert obj.data == b'\x0b\x00\x06\x05\x04\x03\x02\x01\x02\xffa'
Пример #2
0
    def test_08(self, llc):
        """Skip meaningless records"""

        client = handover_connect(llc, self.options)
        try:
            bt_record = ndef.BluetoothEasyPairingRecord("01:02:03:04:05:06")
            bt_record.name = "carrier-1"
            bt_record.device_name = "Handover Test Client"
            bt_record.device_class = 0x10010C
            bt_record.add_service_class(0x1105)
            bt_record.add_service_class(0x1106)

            hr_record = ndef.HandoverRequestRecord("1.2", os.urandom(2))
            hr_record.add_alternative_carrier("active", bt_record.name)

            handover_send(client, [hr_record, ndef.TextRecord("X"), bt_record])
            records = handover_recv(client, timeout=3.0)
            info("received {}".format(records[0].type))
            hs_record, bt_record = records

            if len(hs_record.alternative_carriers) != 1:
                raise TestFail("one selected carrier is expected")
            if bt_record.type != "application/vnd.bluetooth.ep.oob":
                raise TestFail("a Bluetooth carrier is expected")
        finally:
            client.close()
Пример #3
0
 def test_attr_simple_pairing_hash_192(self):
     obj = ndef.BluetoothEasyPairingRecord('01:02:03:04:05:06')
     assert obj.simple_pairing_hash_192 is None
     obj[0x0E] = b'\1' + 15 * b'\0'
     assert obj.simple_pairing_hash_192 == 1
     obj.simple_pairing_hash_192 = 2
     assert obj[0x0E] == b'\2' + 15 * b'\0'
Пример #4
0
 def test_attr_simple_pairing_randomizer_256(self):
     obj = ndef.BluetoothEasyPairingRecord('01:02:03:04:05:06')
     assert obj.simple_pairing_randomizer_256 is None
     obj[0x1E] = b'\1' + 15 * b'\0'
     assert obj.simple_pairing_randomizer_256 == 1
     obj.simple_pairing_randomizer_256 = 2
     assert obj[0x1E] == b'\2' + 15 * b'\0'
Пример #5
0
    def test_03(self, llc):
        """Version handling"""

        bt_record = ndef.BluetoothEasyPairingRecord('01:02:03:04:05:06')
        bt_record.name = 'carrier-1'

        client = handover_connect(llc, self.options)
        try:
            info("send handover request message with version 1.2")
            hr_record = ndef.HandoverRequestRecord('1.2', os.urandom(2))
            hr_record.add_alternative_carrier('active', bt_record.name)
            handover_send(client, [hr_record, bt_record])
            records = handover_recv(client, timeout=3.0)
            if records[0].version_string != "1.2":
                raise TestFail("handover select message version is not 1.2")
            info("received handover select message version 1.2")
        finally:
            client.close()

        client = handover_connect(llc, self.options)
        try:
            info("send handover request message with version 1.1")
            hr_record = ndef.HandoverRequestRecord('1.1', os.urandom(2))
            hr_record.add_alternative_carrier('active', bt_record.name)
            handover_send(client, [hr_record, bt_record])
            records = handover_recv(client, timeout=3.0)
            if records[0].version_string != "1.2":
                raise TestFail("handover select message version is not 1.2")
            info("received handover select message version 1.2")
        finally:
            client.close()

        client = handover_connect(llc, self.options)
        try:
            info("send handover request message with version 1.15")
            hr_record = ndef.HandoverRequestRecord('1.15', os.urandom(2))
            hr_record.add_alternative_carrier('active', bt_record.name)
            handover_send(client, [hr_record, bt_record])
            records = handover_recv(client, timeout=3.0)
            if records[0].version_string != "1.2":
                raise TestFail("handover select message version is not 1.2")
            info("received handover select message version 1.2")
        finally:
            client.close()

        client = handover_connect(llc, self.options)
        try:
            info("send handover request message with version 15.0")
            hr_record = ndef.HandoverRequestRecord('15.0', os.urandom(2))
            hr_record.add_alternative_carrier('active', bt_record.name)
            handover_send(client, [hr_record, bt_record])
            records = handover_recv(client, timeout=3.0)
            if records[0].version_string != "1.2":
                raise TestFail("handover select message version is not 1.2")
            info("received handover select message version 1.2")
        finally:
            client.close()
Пример #6
0
 def test_init(self):
     obj = ndef.BluetoothEasyPairingRecord('01:02:03:04:05:06')
     assert isinstance(obj, eval(self.cls))
     assert obj.device_address.addr == '01:02:03:04:05:06'
     adr = ndef.bluetooth.DeviceAddress('01:02:03:04:05:06')
     eir = ((0x08, b'My Blue'), (0x09, b'My Bluetooth Device'))
     obj = ndef.BluetoothEasyPairingRecord(adr, *eir)
     assert isinstance(obj, eval(self.cls))
     assert obj.device_address.addr == '01:02:03:04:05:06'
     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(0x09) == b'My Bluetooth Device'
     assert obj.get('Complete Local Name') == b'My Bluetooth Device'
     assert 0x01 not in obj
     assert obj.setdefault('Flags', b'\x00') == b'\x00'
     for key in obj:
         assert obj.get(key) is not None
Пример #7
0
 def test_format(self):
     adr = ndef.bluetooth.DeviceAddress('01:02:03:04:05:06')
     eir = ((0x08, b'Blue'), (0x09, b'Blue Device'))
     obj = ndef.BluetoothEasyPairingRecord(adr, *eir)
     if sys.version_info < (3,):
         txt = "(0x08, 'Blue'), (0x09, 'Blue Device')"
     else:
         txt = "(0x08, b'Blue'), (0x09, b'Blue Device')"
     assert format(obj, 'args') == txt
     txt = "Attributes 0x08 0x09"
     assert format(obj, 'data') == txt
     txt = "NDEF Bluetooth Easy Pairing Record ID '' Attributes 0x08 0x09"
     assert format(obj) == txt
Пример #8
0
    def test_05(self, llc):
        """Bluetooth secure pairing"""

        client = handover_connect(llc, self.options)
        try:
            bt_record = ndef.BluetoothEasyPairingRecord("01:02:03:04:05:06")
            bt_record.name = "carrier-1"
            bt_record.device_name = "Handover Test Client"
            bt_record.device_class = 0x10010C
            bt_record.add_service_class(0x1105)
            bt_record.add_service_class(0x1106)
            bt_record.simple_pairing_hash_256 = \
                0x1234567890ABCDEF1234567890ABCDEF
            bt_record.simple_pairing_randomizer_256 = \
                0x010203040506070809000A0B0C0D0E0F

            hr_record = ndef.HandoverRequestRecord("1.2", os.urandom(2))
            hr_record.add_alternative_carrier("active", bt_record.name)

            handover_send(client, [hr_record, bt_record])
            records = handover_recv(client, timeout=3.0)
            info("received {}".format(records[0].type))
            hs_record, bt_record = records

            if len(hs_record.alternative_carriers) != 1:
                raise TestFail("one selected carrier is expected")
            if bt_record.type != "application/vnd.bluetooth.ep.oob":
                raise TestFail("a Bluetooth carrier is expected")
            if bt_record.device_name is None:
                if self.options.relax:
                    log.warning("no local device name attribute")
                else:
                    raise TestFail("no local device name attribute")
            if bt_record.device_name == "":
                raise TestFail("empty local device name attribute")
            if bt_record.device_class is None:
                log.warning("there is no class of device attribute")
            if len(bt_record.service_class_list) == 0:
                log.warning("there are no service class UUIDs")
            if bt_record.simple_pairing_hash_256 is None:
                if self.options.relax:
                    log.warning("ssp hash required for secure pairing")
                else:
                    raise TestFail("ssp hash required for secure pairing")
            if bt_record.simple_pairing_randomizer_256 is None:
                if self.options.relax:
                    log.warning("ssp rand required for secure pairing")
                else:
                    raise TestFail("ssp rand required for secure pairing")
        finally:
            client.close()
Пример #9
0
 def test_attr_device_class(self):
     obj = ndef.BluetoothEasyPairingRecord('01:02:03:04:05:06')
     assert isinstance(obj.device_class, ndef.bluetooth.DeviceClass)
     assert obj.device_class.major_device_class == 'Miscellaneous'
     assert obj.device_class.minor_device_class == 'Uncategorized'
     assert obj.device_class.major_service_class == ()
     obj.device_class = 0x20041C
     assert obj.device_class.major_device_class == 'Audio / Video'
     assert obj.device_class.minor_device_class == 'Portable Audio'
     assert obj.device_class.major_service_class == ('Audio',)
     obj.device_class = ndef.bluetooth.DeviceClass(0x0C06C0)
     assert obj.device_class.major_device_class == 'Imaging'
     assert obj.device_class.minor_device_class == 'Scanner/Printer'
     assert obj.device_class.major_service_class == \
         ('Rendering', 'Capturing')
Пример #10
0
 def test_attr_device_name(self):
     obj = ndef.BluetoothEasyPairingRecord('01:02:03:04:05:06')
     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
Пример #11
0
    def test_07(self, llc):
        """Two handover requests"""

        client = handover_connect(llc, self.options)
        try:
            unknown_carrier = "urn:nfc:ext:nfcpy.org:unknown-carrier-type"
            records = [
                ndef.HandoverRequestRecord("1.2", os.urandom(2)),
                ndef.Record(unknown_carrier, "unknown-carrier")
            ]
            records[0].add_alternative_carrier("active", records[1].name)

            info("request carrier {}".format(records[1].type))
            handover_send(client, records)
            records = handover_recv(client, timeout=3.0)
            info("received {}".format(records[0].type))

            if records[0].version_info.major != 1:
                raise TestFail("handover major version is not 1")
            if len(records[0].alternative_carriers) != 0:
                raise TestFail("an empty carrier selection is expected first")

            bt_record = ndef.BluetoothEasyPairingRecord("01:02:03:04:05:06")
            bt_record.name = "carrier-1"
            bt_record.device_name = "Handover Test Client"
            bt_record.device_class = 0x10010C
            bt_record.add_service_class(0x1105)
            bt_record.add_service_class(0x1106)

            hr_record = ndef.HandoverRequestRecord("1.2", os.urandom(2))
            hr_record.add_alternative_carrier("active", bt_record.name)

            info("propose carrier {}".format(bt_record.type))
            handover_send(client, [hr_record, bt_record])
            records = handover_recv(client, timeout=3.0)
            info("received {}".format(records[0].type))

        finally:
            client.close()
Пример #12
0
 def test_meth_add_service_class(self):
     obj = ndef.BluetoothEasyPairingRecord('01:02:03:04:05:06')
     assert obj.service_class_list == []
     obj.add_service_class(0x1101)
     assert obj.get(0x02) == b'\x01\x11'
     assert obj.get(0x03) is None
     obj.add_service_class(0x1102, complete=True)
     assert obj.get(0x02) is None
     assert obj.get(0x03) == b'\x01\x11\x02\x11'
     obj.add_service_class(0x10001101)
     assert obj.get(0x04) == b'\x01\x11\x00\x10'
     assert obj.get(0x05) is None
     obj.add_service_class(0x10001102, complete=True)
     assert obj.get(0x04) is None
     assert obj.get(0x05) == b'\x01\x11\x00\x10\x02\x11\x00\x10'
     sc_1 = ndef.bluetooth.ServiceClass(str(uuid.uuid4()))
     sc_2 = ndef.bluetooth.ServiceClass(str(uuid.uuid4()))
     obj.add_service_class(sc_1)
     assert obj.get(0x06) == sc_1.uuid.bytes_le
     assert obj.get(0x07) is None
     obj.add_service_class(sc_2, complete=True)
     assert obj.get(0x06) is None
     assert obj.get(0x07) == sc_1.uuid.bytes_le + sc_2.uuid.bytes_le
Пример #13
0
 def test_attr_service_class_list(self):
     obj = ndef.BluetoothEasyPairingRecord('01:02:03:04:05:06')
     assert obj.service_class_list == []
     obj[0x02] = b'\x01\x11'
     obj[0x03] = b'\x02\x11'
     obj[0x04] = b'\x01\x11\x00\x10'
     obj[0x05] = b'\x02\x11\x00\x10'
     obj[0x06] = b'\0\0\0\0\1\1\2\2\3\3\4\4\4\4\4\4'
     obj[0x07] = b'\1\0\0\0\1\1\2\2\3\3\4\4\4\4\4\4'
     assert obj.service_class_list == [
         ndef.bluetooth.ServiceClass(
             '00001101-0000-1000-8000-00805f9b34fb'),
         ndef.bluetooth.ServiceClass(
             '00001102-0000-1000-8000-00805f9b34fb'),
         ndef.bluetooth.ServiceClass(
             '10001101-0000-1000-8000-00805f9b34fb'),
         ndef.bluetooth.ServiceClass(
             '10001102-0000-1000-8000-00805f9b34fb'),
         ndef.bluetooth.ServiceClass(
             '00000000-0101-0202-0303-040404040404'),
         ndef.bluetooth.ServiceClass(
             '00000001-0101-0202-0303-040404040404'),
     ]
Пример #14
0
 def test_key_value_error(self):
     adr = '01:02:03:04:05:06'
     with pytest.raises(ValueError) as excinfo:
         ndef.BluetoothEasyPairingRecord(adr).get('invalid name')
     assert str(excinfo.value) == "unknown attribute name 'invalid name'"
Пример #15
0
 def test_key_name_mapping(self, key, name):
     adr = '01:02:03:04:05:06'
     obj = ndef.BluetoothEasyPairingRecord(adr, (key, b'abc'))
     assert name in obj.attribute_names
     assert obj[name] == b'abc'