|
| 1 | +# |
| 2 | +# Copyright (c) 2024 Project CHIP Authors |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +from global_attribute_ids import (AttributeIdType, ClusterIdType, DeviceTypeIdType, attribute_id_type, cluster_id_type, |
| 19 | + device_type_id_type, is_valid_attribute_id, is_valid_cluster_id, is_valid_device_type_id) |
| 20 | +from matter_testing_support import MatterBaseTest, default_matter_test_main |
| 21 | +from mobly import asserts |
| 22 | + |
| 23 | + |
| 24 | +class TestIdChecks(MatterBaseTest): |
| 25 | + def test_device_type_ids(self): |
| 26 | + standard_good = [0x0000_0000, 0x0000_BFFF] |
| 27 | + standard_bad = [0x0000_C000] |
| 28 | + |
| 29 | + manufacturer_good = [0x0001_0000, 0x0001_BFFF, 0xFFF0_0000, 0xFFF0_BFFF] |
| 30 | + manufacturer_bad = [0x0001_C000, 0xFFF0_C000] |
| 31 | + |
| 32 | + test_good = [0xFFF1_0000, 0xFFF1_BFFF, 0xFFF4_0000, 0xFFF4_BFFF] |
| 33 | + test_bad = [0xFFF1_C000, 0xFFF4_C000] |
| 34 | + |
| 35 | + prefix_bad = [0xFFF5_0000, 0xFFF5_BFFFF, 0xFFF5_C000] |
| 36 | + |
| 37 | + def check_standard(id): |
| 38 | + id_type = device_type_id_type(id) |
| 39 | + msg = f"Incorrect device type range assessment, expecting standard {id:08x}, type = {id_type}" |
| 40 | + asserts.assert_equal(device_type_id_type(id), DeviceTypeIdType.kStandard, msg) |
| 41 | + asserts.assert_true(is_valid_device_type_id(id_type, allow_test=True), msg) |
| 42 | + asserts.assert_true(is_valid_device_type_id(id_type, allow_test=False), msg) |
| 43 | + |
| 44 | + def check_manufacturer(id): |
| 45 | + id_type = device_type_id_type(id) |
| 46 | + msg = f"Incorrect device type range assessment, expecting manufacturer {id:08x}, type = {id_type}" |
| 47 | + asserts.assert_equal(device_type_id_type(id), DeviceTypeIdType.kManufacturer, msg) |
| 48 | + asserts.assert_true(is_valid_device_type_id(id_type, allow_test=True), msg) |
| 49 | + asserts.assert_true(is_valid_device_type_id(id_type, allow_test=False), msg) |
| 50 | + |
| 51 | + def check_test(id): |
| 52 | + id_type = device_type_id_type(id) |
| 53 | + msg = f"Incorrect device type range assessment, expecting test {id:08x}, type = {id_type}" |
| 54 | + asserts.assert_equal(device_type_id_type(id), DeviceTypeIdType.kTest, msg) |
| 55 | + asserts.assert_true(is_valid_device_type_id(id_type, allow_test=True), msg) |
| 56 | + asserts.assert_false(is_valid_device_type_id(id_type, allow_test=False), msg) |
| 57 | + |
| 58 | + def check_all_bad(id): |
| 59 | + id_type = device_type_id_type(id) |
| 60 | + msg = f"Incorrect device type range assessment, expecting invalid {id:08x}, type = {id_type}" |
| 61 | + asserts.assert_equal(device_type_id_type(id), DeviceTypeIdType.kInvalid, msg) |
| 62 | + asserts.assert_false(is_valid_device_type_id(id_type, allow_test=True), msg) |
| 63 | + asserts.assert_false(is_valid_device_type_id(id_type, allow_test=False), msg) |
| 64 | + |
| 65 | + for id in standard_good: |
| 66 | + check_standard(id) |
| 67 | + |
| 68 | + for id in standard_bad: |
| 69 | + check_all_bad(id) |
| 70 | + |
| 71 | + for id in manufacturer_good: |
| 72 | + check_manufacturer(id) |
| 73 | + |
| 74 | + for id in manufacturer_bad: |
| 75 | + check_all_bad(id) |
| 76 | + |
| 77 | + for id in test_good: |
| 78 | + check_test(id) |
| 79 | + |
| 80 | + for id in test_bad: |
| 81 | + check_all_bad(id) |
| 82 | + |
| 83 | + for id in prefix_bad: |
| 84 | + check_all_bad(id) |
| 85 | + |
| 86 | + def test_cluster_ids(self): |
| 87 | + standard_good = [0x0000_0000, 0x0000_7FFF] |
| 88 | + standard_bad = [0x0000_8000] |
| 89 | + |
| 90 | + manufacturer_good = [0x0001_FC00, 0x0001_FFFE, 0xFFF0_FC00, 0xFFF0_FFFE] |
| 91 | + manufacturer_bad = [0x0001_0000, 0x0001_7FFF, 0x0001_FFFF, 0xFFF0_0000, 0xFFF0_7FFF, 0xFFF0_FFFF] |
| 92 | + |
| 93 | + test_good = [0xFFF1_FC00, 0xFFF1_FFFE, 0xFFF4_FC00, 0xFFF4_FFFE] |
| 94 | + test_bad = [0xFFF1_0000, 0xFFF1_7FFF, 0xFFF1_FFFF, 0xFFF4_0000, 0xFFF4_7FFF, 0xFFF4_FFFF] |
| 95 | + |
| 96 | + prefix_bad = [0xFFF5_0000, 0xFFF5_FC00, 0xFFF5_FFFF] |
| 97 | + |
| 98 | + def check_standard(id): |
| 99 | + id_type = cluster_id_type(id) |
| 100 | + msg = f"Incorrect cluster range assessment, expecting standard {id:08x}, type = {id_type}" |
| 101 | + asserts.assert_equal(id_type, ClusterIdType.kStandard, msg) |
| 102 | + asserts.assert_true(is_valid_cluster_id(id_type, allow_test=True), msg) |
| 103 | + asserts.assert_true(is_valid_cluster_id(id_type, allow_test=False), msg) |
| 104 | + |
| 105 | + def check_manufacturer(id): |
| 106 | + id_type = cluster_id_type(id) |
| 107 | + msg = f"Incorrect cluster range assessment, expecting manufacturer {id:08x}, type = {id_type}" |
| 108 | + asserts.assert_equal(id_type, ClusterIdType.kManufacturer, msg) |
| 109 | + asserts.assert_true(is_valid_cluster_id(id_type, allow_test=True), msg) |
| 110 | + asserts.assert_true(is_valid_cluster_id(id_type, allow_test=False), msg) |
| 111 | + |
| 112 | + def check_test(id): |
| 113 | + id_type = cluster_id_type(id) |
| 114 | + msg = f"Incorrect cluster range assessment, expecting test {id:08x}, type = {id_type}" |
| 115 | + asserts.assert_equal(id_type, ClusterIdType.kTest, msg) |
| 116 | + asserts.assert_true(is_valid_cluster_id(id_type, allow_test=True), msg) |
| 117 | + asserts.assert_false(is_valid_cluster_id(id_type, allow_test=False), msg) |
| 118 | + |
| 119 | + def check_all_bad(id): |
| 120 | + id_type = cluster_id_type(id) |
| 121 | + msg = f"Incorrect cluster range assessment, expecting invalid {id:08x}, type = {id_type}" |
| 122 | + asserts.assert_equal(id_type, ClusterIdType.kInvalid, msg) |
| 123 | + asserts.assert_false(is_valid_cluster_id(id_type, allow_test=True), msg) |
| 124 | + asserts.assert_false(is_valid_cluster_id(id_type, allow_test=False), msg) |
| 125 | + |
| 126 | + for id in standard_good: |
| 127 | + check_standard(id) |
| 128 | + |
| 129 | + for id in standard_bad: |
| 130 | + check_all_bad(id) |
| 131 | + |
| 132 | + for id in manufacturer_good: |
| 133 | + check_manufacturer(id) |
| 134 | + |
| 135 | + for id in manufacturer_bad: |
| 136 | + check_all_bad(id) |
| 137 | + |
| 138 | + for id in test_good: |
| 139 | + check_test(id) |
| 140 | + |
| 141 | + for id in test_bad: |
| 142 | + check_all_bad(id) |
| 143 | + |
| 144 | + for id in prefix_bad: |
| 145 | + check_all_bad(id) |
| 146 | + |
| 147 | + def test_attribute_ids(self): |
| 148 | + standard_global_good = [0x0000_F000, 0x0000_FFFE] |
| 149 | + standard_global_bad = [0x0000_FFFF] |
| 150 | + standard_non_global_good = [0x0000_0000, 0x0000_4FFF] |
| 151 | + standard_non_global_bad = [0x0000_5000] |
| 152 | + manufacturer_good = [0x0001_0000, 0x0001_4FFF, 0xFFF0_0000, 0xFFF0_4FFF] |
| 153 | + manufacturer_bad = [0x0001_5000, 0x0001_F000, 0x0001_FFFFF, 0xFFF0_5000, 0xFFF0_F000, 0xFFF0_FFFF] |
| 154 | + test_good = [0xFFF1_0000, 0xFFF1_4FFF, 0xFFF4_0000, 0xFFF4_4FFF] |
| 155 | + test_bad = [0xFFF1_5000, 0xFFF1_F000, 0xFFF1_FFFFF, 0xFFF4_5000, 0xFFF4_F000, 0xFFF4_FFFF] |
| 156 | + prefix_bad = [0xFFF5_0000, 0xFFF5_4FFF, 0xFFF5_5000, 0xFFF5_F000, 0xFFF5_FFFF] |
| 157 | + |
| 158 | + def check_standard_global(id): |
| 159 | + id_type = attribute_id_type(id) |
| 160 | + msg = f"Incorrect attribute range assessment, expecting standard global {id:08x}, type = {id_type}" |
| 161 | + asserts.assert_equal(id_type, AttributeIdType.kStandardGlobal, msg) |
| 162 | + asserts.assert_true(is_valid_attribute_id(id_type, allow_test=True), msg) |
| 163 | + asserts.assert_true(is_valid_attribute_id(id_type, allow_test=False), msg) |
| 164 | + |
| 165 | + def check_standard_non_global(id): |
| 166 | + id_type = attribute_id_type(id) |
| 167 | + msg = f"Incorrect attribute range assessment, expecting standard non-global {id:08x}, type = {id_type}" |
| 168 | + asserts.assert_equal(id_type, AttributeIdType.kStandardNonGlobal, msg) |
| 169 | + asserts.assert_true(is_valid_attribute_id(id_type, allow_test=True), msg) |
| 170 | + asserts.assert_true(is_valid_attribute_id(id_type, allow_test=False), msg) |
| 171 | + |
| 172 | + def check_manufacturer(id): |
| 173 | + id_type = attribute_id_type(id) |
| 174 | + msg = f"Incorrect attribute range assessment, expecting manufacturer {id:08x}, type = {id_type}" |
| 175 | + asserts.assert_equal(id_type, AttributeIdType.kManufacturer, msg) |
| 176 | + asserts.assert_true(is_valid_attribute_id(id_type, allow_test=True), msg) |
| 177 | + asserts.assert_true(is_valid_attribute_id(id_type, allow_test=False), msg) |
| 178 | + |
| 179 | + def check_test(id): |
| 180 | + id_type = attribute_id_type(id) |
| 181 | + msg = f"Incorrect attribute range assessment, expecting test {id:08x}, type = {id_type}" |
| 182 | + asserts.assert_equal(id_type, AttributeIdType.kTest, msg) |
| 183 | + asserts.assert_true(is_valid_attribute_id(id_type, allow_test=True), msg) |
| 184 | + asserts.assert_false(is_valid_attribute_id(id_type, allow_test=False), msg) |
| 185 | + |
| 186 | + def check_all_bad(id): |
| 187 | + id_type = attribute_id_type(id) |
| 188 | + msg = f"Incorrect attribute range assessment, expecting invalid {id:08x}, type = {id_type}" |
| 189 | + asserts.assert_equal(id_type, AttributeIdType.kInvalid, msg) |
| 190 | + asserts.assert_false(is_valid_attribute_id(id_type, allow_test=True), msg) |
| 191 | + asserts.assert_false(is_valid_attribute_id(id_type, allow_test=False), msg) |
| 192 | + |
| 193 | + for id in standard_global_good: |
| 194 | + check_standard_global(id) |
| 195 | + for id in standard_global_bad: |
| 196 | + check_all_bad(id) |
| 197 | + for id in standard_non_global_good: |
| 198 | + check_standard_non_global(id) |
| 199 | + for id in standard_non_global_bad: |
| 200 | + check_all_bad(id) |
| 201 | + for id in manufacturer_good: |
| 202 | + check_manufacturer(id) |
| 203 | + for id in manufacturer_bad: |
| 204 | + check_all_bad(id) |
| 205 | + for id in test_good: |
| 206 | + check_test(id) |
| 207 | + for id in test_bad: |
| 208 | + check_all_bad(id) |
| 209 | + for id in prefix_bad: |
| 210 | + check_all_bad(id) |
| 211 | + |
| 212 | + |
| 213 | +if __name__ == "__main__": |
| 214 | + default_matter_test_main() |
0 commit comments