1# Copyright 2021-2022 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# -----------------------------------------------------------------------------
16# Imports
17# -----------------------------------------------------------------------------
18from enum import IntEnum
19
20from bumble.core import AdvertisingData, Appearance, UUID, get_dict_key_by_value
21
22
23# -----------------------------------------------------------------------------
24def test_ad_data():
25    data = bytes([2, AdvertisingData.TX_POWER_LEVEL, 123])
26    ad = AdvertisingData.from_bytes(data)
27    ad_bytes = bytes(ad)
28    assert data == ad_bytes
29    assert ad.get(AdvertisingData.COMPLETE_LOCAL_NAME, raw=True) is None
30    assert ad.get(AdvertisingData.TX_POWER_LEVEL, raw=True) == bytes([123])
31    assert ad.get_all(AdvertisingData.COMPLETE_LOCAL_NAME, raw=True) == []
32    assert ad.get_all(AdvertisingData.TX_POWER_LEVEL, raw=True) == [bytes([123])]
33
34    data2 = bytes([2, AdvertisingData.TX_POWER_LEVEL, 234])
35    ad.append(data2)
36    ad_bytes = bytes(ad)
37    assert ad_bytes == data + data2
38    assert ad.get(AdvertisingData.COMPLETE_LOCAL_NAME, raw=True) is None
39    assert ad.get(AdvertisingData.TX_POWER_LEVEL, raw=True) == bytes([123])
40    assert ad.get_all(AdvertisingData.COMPLETE_LOCAL_NAME, raw=True) == []
41    assert ad.get_all(AdvertisingData.TX_POWER_LEVEL, raw=True) == [
42        bytes([123]),
43        bytes([234]),
44    ]
45
46
47# -----------------------------------------------------------------------------
48def test_get_dict_key_by_value():
49    dictionary = {"A": 1, "B": 2}
50    assert get_dict_key_by_value(dictionary, 1) == "A"
51    assert get_dict_key_by_value(dictionary, 2) == "B"
52    assert get_dict_key_by_value(dictionary, 3) is None
53
54
55# -----------------------------------------------------------------------------
56def test_uuid_to_hex_str() -> None:
57    assert UUID("b5ea").to_hex_str() == "B5EA"
58    assert UUID("df5ce654").to_hex_str() == "DF5CE654"
59    assert (
60        UUID("df5ce654-e059-11ed-b5ea-0242ac120002").to_hex_str()
61        == "DF5CE654E05911EDB5EA0242AC120002"
62    )
63    assert UUID("b5ea").to_hex_str('-') == "B5EA"
64    assert UUID("df5ce654").to_hex_str('-') == "DF5CE654"
65    assert (
66        UUID("df5ce654-e059-11ed-b5ea-0242ac120002").to_hex_str('-')
67        == "DF5CE654-E059-11ED-B5EA-0242AC120002"
68    )
69
70
71# -----------------------------------------------------------------------------
72def test_appearance() -> None:
73    a = Appearance(Appearance.Category.COMPUTER, Appearance.ComputerSubcategory.LAPTOP)
74    assert str(a) == 'COMPUTER/LAPTOP'
75    assert int(a) == 0x0083
76
77    a = Appearance(Appearance.Category.HUMAN_INTERFACE_DEVICE, 0x77)
78    assert str(a) == 'HUMAN_INTERFACE_DEVICE/HumanInterfaceDeviceSubcategory[119]'
79    assert int(a) == 0x03C0 | 0x77
80
81    a = Appearance.from_int(0x0381)
82    assert a.category == Appearance.Category.BLOOD_PRESSURE
83    assert a.subcategory == Appearance.BloodPressureSubcategory.ARM_BLOOD_PRESSURE
84    assert int(a) == 0x381
85
86    a = Appearance.from_int(0x038A)
87    assert a.category == Appearance.Category.BLOOD_PRESSURE
88    assert a.subcategory == 0x0A
89    assert int(a) == 0x038A
90
91    a = Appearance.from_int(0x3333)
92    assert a.category == 0xCC
93    assert a.subcategory == 0x33
94    assert int(a) == 0x3333
95
96
97# -----------------------------------------------------------------------------
98if __name__ == '__main__':
99    test_ad_data()
100    test_get_dict_key_by_value()
101    test_uuid_to_hex_str()
102    test_appearance()
103