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 bumble.core import AdvertisingData, UUID, get_dict_key_by_value 19 20 21# ----------------------------------------------------------------------------- 22def test_ad_data(): 23 data = bytes([2, AdvertisingData.TX_POWER_LEVEL, 123]) 24 ad = AdvertisingData.from_bytes(data) 25 ad_bytes = bytes(ad) 26 assert data == ad_bytes 27 assert ad.get(AdvertisingData.COMPLETE_LOCAL_NAME, raw=True) is None 28 assert ad.get(AdvertisingData.TX_POWER_LEVEL, raw=True) == bytes([123]) 29 assert ad.get_all(AdvertisingData.COMPLETE_LOCAL_NAME, raw=True) == [] 30 assert ad.get_all(AdvertisingData.TX_POWER_LEVEL, raw=True) == [bytes([123])] 31 32 data2 = bytes([2, AdvertisingData.TX_POWER_LEVEL, 234]) 33 ad.append(data2) 34 ad_bytes = bytes(ad) 35 assert ad_bytes == data + data2 36 assert ad.get(AdvertisingData.COMPLETE_LOCAL_NAME, raw=True) is None 37 assert ad.get(AdvertisingData.TX_POWER_LEVEL, raw=True) == bytes([123]) 38 assert ad.get_all(AdvertisingData.COMPLETE_LOCAL_NAME, raw=True) == [] 39 assert ad.get_all(AdvertisingData.TX_POWER_LEVEL, raw=True) == [ 40 bytes([123]), 41 bytes([234]), 42 ] 43 44 45# ----------------------------------------------------------------------------- 46def test_get_dict_key_by_value(): 47 dictionary = {"A": 1, "B": 2} 48 assert get_dict_key_by_value(dictionary, 1) == "A" 49 assert get_dict_key_by_value(dictionary, 2) == "B" 50 assert get_dict_key_by_value(dictionary, 3) is None 51 52 53# ----------------------------------------------------------------------------- 54def test_uuid_to_hex_str() -> None: 55 assert UUID("b5ea").to_hex_str() == "B5EA" 56 assert UUID("df5ce654").to_hex_str() == "DF5CE654" 57 assert ( 58 UUID("df5ce654-e059-11ed-b5ea-0242ac120002").to_hex_str() 59 == "DF5CE654E05911EDB5EA0242AC120002" 60 ) 61 assert UUID("b5ea").to_hex_str('-') == "B5EA" 62 assert UUID("df5ce654").to_hex_str('-') == "DF5CE654" 63 assert ( 64 UUID("df5ce654-e059-11ed-b5ea-0242ac120002").to_hex_str('-') 65 == "DF5CE654-E059-11ED-B5EA-0242AC120002" 66 ) 67 68 69# ----------------------------------------------------------------------------- 70if __name__ == '__main__': 71 test_ad_data() 72 test_get_dict_key_by_value() 73 test_uuid_to_hex_str() 74