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 UUID 19from bumble.sdp import DataElement 20 21# ----------------------------------------------------------------------------- 22# pylint: disable=invalid-name 23# ----------------------------------------------------------------------------- 24 25# ----------------------------------------------------------------------------- 26def basic_check(x): 27 serialized = bytes(x) 28 if len(serialized) < 500: 29 print('Original:', x) 30 print('Serialized:', serialized.hex()) 31 parsed = DataElement.from_bytes(serialized) 32 if len(serialized) < 500: 33 print('Parsed:', parsed) 34 parsed_bytes = bytes(parsed) 35 if len(serialized) < 500: 36 print('Parsed Bytes:', parsed_bytes.hex()) 37 assert parsed_bytes == serialized 38 x_str = str(x) 39 parsed_str = str(parsed) 40 assert x_str == parsed_str 41 42 43# ----------------------------------------------------------------------------- 44def test_data_elements(): 45 e = DataElement(DataElement.NIL, None) 46 basic_check(e) 47 48 e = DataElement(DataElement.UNSIGNED_INTEGER, 12, 1) 49 basic_check(e) 50 51 e = DataElement(DataElement.UNSIGNED_INTEGER, 1234, 2) 52 basic_check(e) 53 54 e = DataElement(DataElement.UNSIGNED_INTEGER, 0x123456, 4) 55 basic_check(e) 56 57 e = DataElement(DataElement.UNSIGNED_INTEGER, 0x123456789, 8) 58 basic_check(e) 59 60 e = DataElement(DataElement.UNSIGNED_INTEGER, 0x0000FFFF, value_size=4) 61 basic_check(e) 62 63 e = DataElement(DataElement.SIGNED_INTEGER, -12, 1) 64 basic_check(e) 65 66 e = DataElement(DataElement.SIGNED_INTEGER, -1234, 2) 67 basic_check(e) 68 69 e = DataElement(DataElement.SIGNED_INTEGER, -0x123456, 4) 70 basic_check(e) 71 72 e = DataElement(DataElement.SIGNED_INTEGER, -0x123456789, 8) 73 basic_check(e) 74 75 e = DataElement(DataElement.SIGNED_INTEGER, 0x0000FFFF, value_size=4) 76 basic_check(e) 77 78 e = DataElement(DataElement.UUID, UUID.from_16_bits(1234)) 79 basic_check(e) 80 81 e = DataElement(DataElement.UUID, UUID.from_32_bits(123456789)) 82 basic_check(e) 83 84 e = DataElement(DataElement.UUID, UUID('61A3512C-09BE-4DDC-A6A6-0B03667AAFC6')) 85 basic_check(e) 86 87 e = DataElement(DataElement.TEXT_STRING, 'hello') 88 basic_check(e) 89 90 e = DataElement(DataElement.TEXT_STRING, 'hello' * 60) 91 basic_check(e) 92 93 e = DataElement(DataElement.TEXT_STRING, 'hello' * 20000) 94 basic_check(e) 95 96 e = DataElement(DataElement.BOOLEAN, True) 97 basic_check(e) 98 99 e = DataElement(DataElement.BOOLEAN, False) 100 basic_check(e) 101 102 e = DataElement(DataElement.SEQUENCE, [DataElement(DataElement.BOOLEAN, True)]) 103 basic_check(e) 104 105 e = DataElement( 106 DataElement.SEQUENCE, 107 [ 108 DataElement(DataElement.BOOLEAN, True), 109 DataElement(DataElement.TEXT_STRING, 'hello'), 110 ], 111 ) 112 basic_check(e) 113 114 e = DataElement(DataElement.ALTERNATIVE, [DataElement(DataElement.BOOLEAN, True)]) 115 basic_check(e) 116 117 e = DataElement( 118 DataElement.ALTERNATIVE, 119 [ 120 DataElement(DataElement.BOOLEAN, True), 121 DataElement(DataElement.TEXT_STRING, 'hello'), 122 ], 123 ) 124 basic_check(e) 125 126 e = DataElement(DataElement.URL, 'http://example.com') 127 128 e = DataElement.nil() 129 130 e = DataElement.unsigned_integer(1234, 2) 131 basic_check(e) 132 133 e = DataElement.signed_integer(-1234, 2) 134 basic_check(e) 135 136 e = DataElement.uuid(UUID.from_16_bits(1234)) 137 basic_check(e) 138 139 e = DataElement.text_string('hello') 140 basic_check(e) 141 142 e = DataElement.boolean(True) 143 basic_check(e) 144 145 e = DataElement.sequence( 146 [DataElement.signed_integer(0, 1), DataElement.text_string('hello')] 147 ) 148 basic_check(e) 149 150 e = DataElement.alternative( 151 [DataElement.signed_integer(0, 1), DataElement.text_string('hello')] 152 ) 153 basic_check(e) 154 155 e = DataElement.url('http://foobar.com') 156 basic_check(e) 157 158 159# ----------------------------------------------------------------------------- 160if __name__ == '__main__': 161 test_data_elements() 162