test_services.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # test_services.py/Open GoPro, Version 2.0 (C) Copyright 2021 GoPro, Inc. (http://gopro.com/OpenGoPro).
  2. # This copyright was auto-generated on Wed, Sep 1, 2021 5:05:54 PM
  3. # pylint: disable = redefined-outer-name
  4. import uuid
  5. import pytest
  6. from open_gopro.network.ble import BleUUID, Characteristic, Descriptor, GattDB, Service
  7. from open_gopro.network.ble.services import BLE_BASE_UUID, UUIDs, UUIDsMeta
  8. def test_128_bit_uuid():
  9. u = BleUUID("128 bit from str", hex="12345678123456781234567812345678")
  10. assert len(u.hex)
  11. u = BleUUID("128 bit from int", int=0x12345678123456781234567812345678)
  12. assert len(u.hex)
  13. u = BleUUID("128 bit from bytes", bytes=b"\x12\x34\x56\x78" * 4)
  14. assert len(u.hex)
  15. u = BleUUID(
  16. "128 bit from byte le",
  17. bytes_le=b"\x78\x56\x34\x12\x34\x12\x78\x56" + b"\x12\x34\x56\x78\x12\x34\x56\x78",
  18. )
  19. assert len(u.hex)
  20. def test_16_bit_uuid():
  21. u = BleUUID("16 bit from str", format=BleUUID.Format.BIT_16, hex="FEA6")
  22. assert len(u.hex)
  23. u = BleUUID("16 bit from int", format=BleUUID.Format.BIT_16, int=1234)
  24. assert len(u.hex)
  25. u = BleUUID("16 bit from bytes", format=BleUUID.Format.BIT_16, bytes=bytes([0xAB, 0xCD]))
  26. assert len(u.hex)
  27. def test_uuid_negative():
  28. with pytest.raises(ValueError):
  29. u = BleUUID("16 bit from bytes le", format=BleUUID.Format.BIT_16, bytes_le=bytes([0xCD, 0xAB]))
  30. with pytest.raises(ValueError):
  31. u = BleUUID("Multiple inputs", format=BleUUID.Format.BIT_16, hex="", int=1)
  32. with pytest.raises(ValueError):
  33. u = BleUUID("Bad string", format=BleUUID.Format.BIT_16, hex="AB")
  34. with pytest.raises(ValueError):
  35. u = BleUUID("Bad bytes", format=BleUUID.Format.BIT_16, bytes=bytes([0xAB, 0xCD, 0xEF]))
  36. def test_ble_uuids():
  37. UUID_STR = "00001801-0000-1000-8000-00805f9b34fb"
  38. UUID_INT = 486857058725721441610830112830715
  39. class TestUUIDs(metaclass=UUIDsMeta):
  40. TEST_UUID = BleUUID("Test", hex=UUID_STR)
  41. assert UUID_STR in TestUUIDs
  42. assert BLE_BASE_UUID.format("ABCD") not in TestUUIDs
  43. assert UUID_INT in TestUUIDs
  44. assert 0xABCD not in TestUUIDs
  45. assert TestUUIDs.TEST_UUID in TestUUIDs
  46. assert uuid.UUID(hex=UUID_STR) in TestUUIDs
  47. assert TestUUIDs[UUID_STR].hex
  48. assert TestUUIDs[UUID_INT].hex
  49. assert TestUUIDs.TEST_UUID.hex
  50. assert TestUUIDs[uuid.UUID(hex=UUID_STR)].hex
  51. assert len([x for x in TestUUIDs]) == 1
  52. def test_ble_uuids_negative():
  53. with pytest.raises(TypeError):
  54. class TestUUIDs(UUIDs):
  55. BAD_ATTRIBUTE = 1
  56. def test_descriptor(mock_descriptor: Descriptor):
  57. assert mock_descriptor.handle > 0
  58. assert mock_descriptor.name == mock_descriptor.uuid.name
  59. def test_characteristic(mock_characteristic: Characteristic):
  60. assert mock_characteristic.handle > 0
  61. assert mock_characteristic.name == mock_characteristic.uuid.name
  62. assert len(mock_characteristic.descriptors)
  63. assert mock_characteristic.is_readable
  64. assert not mock_characteristic.is_writeable
  65. assert not mock_characteristic.is_notifiable
  66. assert not mock_characteristic.is_indicatable
  67. def test_service(mock_service: Service):
  68. assert mock_service.start_handle > 0
  69. assert mock_service.name == mock_service.uuid.name
  70. assert len(mock_service.characteristics) > 0
  71. def test_characteristic_view(mock_gatt_db: GattDB):
  72. # Get all attributes by nested looping through services
  73. chars: list[Characteristic] = []
  74. for service in mock_gatt_db.services.values():
  75. for char in service.characteristics.values():
  76. chars.append(char)
  77. assert len(chars) == len(mock_gatt_db.characteristics)
  78. for char in chars:
  79. assert char.uuid in mock_gatt_db.characteristics
  80. for char in mock_gatt_db.characteristics:
  81. assert len(char.uuid.hex)
  82. assert list(mock_gatt_db.characteristics.keys()) == [c.uuid for c in chars]
  83. assert list([c.uuid for c in mock_gatt_db.characteristics.values()]) == [c.uuid for c in chars]
  84. def test_gatt_db(mock_gatt_db: GattDB):
  85. handles = set([c.handle for c in mock_gatt_db.characteristics])
  86. uuids = set([c.uuid for c in mock_gatt_db.characteristics])
  87. assert handles == set([mock_gatt_db.uuid2handle(uuid) for uuid in uuids])
  88. assert uuids == set([mock_gatt_db.handle2uuid(handle) for handle in handles])
  89. mock_gatt_db.dump_to_csv()