forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_backwards_compatibility.py
executable file
·368 lines (308 loc) · 15.9 KB
/
test_backwards_compatibility.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/env python3
# Copyright (c) 2023 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import unittest
from enum import Flag, auto
try:
from matter_idl.matter_idl_parser import CreateParser
except ImportError:
import os
import sys
sys.path.append(os.path.abspath(
os.path.join(os.path.dirname(__file__), '..')))
from matter_idl.matter_idl_parser import CreateParser
from matter_idl.backwards_compatibility import CompatibilityChecker, is_backwards_compatible
from matter_idl.matter_idl_types import Idl
class Compatibility(Flag):
ALL_OK = auto() # placeholder allowing anything
FORWARD_FAIL = auto() # old -> new is wrong
BACKWARD_FAIL = auto() # new -> old is wrong
class DisableLogger():
def __enter__(self):
logging.disable(logging.CRITICAL)
def __exit__(self, exit_type, exit_value, exit_traceback):
logging.disable(logging.NOTSET)
class TestCompatibilityChecks(unittest.TestCase):
def _AssumeCompatiblity(self, old: str, new: str, old_idl: Idl, new_idl: Idl, expect_compatible: bool):
with DisableLogger():
if expect_compatible == is_backwards_compatible(old_idl, new_idl):
return
# re-run to figure out reasons:
checker = CompatibilityChecker(old_idl, new_idl)
checker.check()
if expect_compatible:
reason = "EXPECTED COMPATIBLE, but failed"
fail_reasons = "\nREASONS:" + "\n - ".join([""] + checker.errors)
else:
reason = "EXPECTED NOT COMPATIBLE, but succeeded"
fail_reasons = ""
self.fail(f"""Failed compatibility test
{reason}:
/-------------------- OLD ----------------/
{old}
/-------------------- NEW ----------------/
{new}
/-----------------------------------------/{fail_reasons}""")
def ValidateUpdate(self, name: str, old: str, new: str, flags: Compatibility):
old_idl = CreateParser(skip_meta=True).parse(old)
new_idl = CreateParser(skip_meta=True).parse(new)
with self.subTest(validate=name):
# Validate compatibility and that no changes are always compatible
with self.subTest(direction="Forward"):
self._AssumeCompatiblity(
old, new, old_idl, new_idl, Compatibility.FORWARD_FAIL not in flags)
with self.subTest(direction="Backward"):
self._AssumeCompatiblity(
new, old, new_idl, old_idl, Compatibility.BACKWARD_FAIL not in flags)
with self.subTest(direction="NEW-to-NEW"):
self._AssumeCompatiblity(new, new, new_idl, new_idl, True)
with self.subTest(direction="OLD-to-OLD"):
self._AssumeCompatiblity(old, old, old_idl, old_idl, True)
def test_basic_clusters_enum(self):
self.ValidateUpdate(
"Adding an enum is ok. Also validates code formatting",
"server cluster A = 16 {}",
"server cluster A = 0x10 { enum X : ENUM8 {} }",
Compatibility.BACKWARD_FAIL)
def test_clusters_enum_add_remove(self):
self.ValidateUpdate(
"Adding an enum is ok. Also validates code formatting",
"server cluster A = 16 { enum X : ENUM8 { A = 1; B = 2; }}",
"server cluster A = 16 { enum X : ENUM8 { A = 1; }}",
Compatibility.FORWARD_FAIL)
def test_provisional_cluster(self):
self.ValidateUpdate(
"Provisional cluster changes are ok.",
"provisional server cluster A = 16 { enum X : ENUM8 { A = 1; B = 2; } info event A = 1 { int8u x = 1;} }",
"provisional server cluster A = 16 { enum X : ENUM8 { A = 1; B = 3; } info event A = 2 { int16u x = 1;} }",
Compatibility.ALL_OK)
def test_clusters_enum_code(self):
self.ValidateUpdate(
"Adding an enum is ok. Also validates code formatting",
"server cluster A = 16 { enum X : ENUM8 { A = 1; B = 2; }}",
"server cluster A = 16 { enum X : ENUM8 { A = 1; B = 3; }}",
Compatibility.FORWARD_FAIL | Compatibility.BACKWARD_FAIL)
def test_clusters_enum_delete(self):
self.ValidateUpdate(
"Adding an enum is ok. Also validates code formatting",
"server cluster A = 16 { enum X : ENUM8 {}}",
"server cluster A = 16 { }",
Compatibility.FORWARD_FAIL)
def test_clusters_enum_change(self):
self.ValidateUpdate(
"Adding an enum is ok. Also validates code formatting",
"server cluster A = 16 { enum X : ENUM16 {}}",
"server cluster A = 16 { enum X : ENUM8 {}}",
Compatibility.FORWARD_FAIL | Compatibility.BACKWARD_FAIL)
def test_bitmaps_delete(self):
self.ValidateUpdate(
"Deleting a bitmap is not ok",
"client Cluster X = 1 { bitmap A: BITMAP8{} bitmap B: BITMAP8{} }",
"client Cluster X = 1 { bitmap A: BITMAP8{} }",
Compatibility.FORWARD_FAIL)
def test_bitmaps_type(self):
self.ValidateUpdate(
"Changing a bitmap type is never ok",
" client cluster X = 1 { bitmap A: BITMAP8{} }",
" client cluster X = 1 { bitmap A: BITMAP16{} }",
Compatibility.FORWARD_FAIL | Compatibility.BACKWARD_FAIL)
def test_bitmaps_values(self):
self.ValidateUpdate(
"Adding bitmap values is ok, removing values is not",
" client cluster X = 1 { bitmap A: BITMAP8 { kA = 0x01; kB = 0x02; } }",
" client cluster X = 1 { bitmap A: BITMAP8 { kA = 0x01; } }",
Compatibility.FORWARD_FAIL)
def test_bitmaps_code(self):
self.ValidateUpdate(
"Switching bitmap codes is never ok",
" client cluster X = 1 { bitmap A: BITMAP8 { kA = 0x01; kB = 0x02; } }",
" client cluster X = 1 { bitmap A: BITMAP8 { kA = 0x01; kB = 0x04; } }",
Compatibility.FORWARD_FAIL | Compatibility.BACKWARD_FAIL)
def test_events_delete(self):
self.ValidateUpdate(
"Deleting an event is not ok",
"client Cluster X = 1 { info event A = 1 {} info event B = 2 {} }",
"client Cluster X = 1 { info event B = 2 {} }",
Compatibility.FORWARD_FAIL)
def test_events_code(self):
self.ValidateUpdate(
"Changing event code is never ok",
"client Cluster X = 1 { info event A = 1 {} }",
"client Cluster X = 1 { info event A = 2 {} }",
Compatibility.FORWARD_FAIL | Compatibility.BACKWARD_FAIL)
def test_events_struct_content_change(self):
self.ValidateUpdate(
"Changing event struct is never ok",
"client Cluster X = 1 { info event A = 1 { int8u x = 1; } }",
"client Cluster X = 1 { info event A = 1 { int8u x = 1; int8u y = 2; } }",
Compatibility.FORWARD_FAIL | Compatibility.BACKWARD_FAIL)
def test_events_struct_renames(self):
self.ValidateUpdate(
"Renaming struct content is ok",
"client Cluster X = 1 { info event A = 1 { int8u a = 1; int8u b = 2; } }",
"client Cluster X = 1 { info event A = 1 { int8u x = 1; int8u y = 2; } }",
Compatibility.ALL_OK)
def test_events_struct_reorders_renames(self):
self.ValidateUpdate(
"Renaming struct content is ok",
"client Cluster X = 1 { info event A = 1 { int8u a = 1; int16u b = 2; } }",
"client Cluster X = 1 { info event A = 1 { int16u x = 2; int8u a = 1; } }",
Compatibility.ALL_OK)
def test_events_struct_type_change(self):
self.ValidateUpdate(
"Changing event struct is never ok",
"client Cluster X = 1 { info event A = 1 { int8u x = 1; } }",
"client Cluster X = 1 { info event A = 1 { int16u x = 1; } }",
Compatibility.FORWARD_FAIL | Compatibility.BACKWARD_FAIL)
def test_events_struct_id_change(self):
self.ValidateUpdate(
"Changing event struct is never ok",
"client Cluster X = 1 { info event A = 1 { int8u x = 1; } }",
"client Cluster X = 1 { info event A = 1 { int8u x = 2; } }",
Compatibility.FORWARD_FAIL | Compatibility.BACKWARD_FAIL)
def test_events_severity(self):
self.ValidateUpdate(
"Changing event severity is ok",
"client Cluster X = 1 { info event A = 1 { int8u x = 1; } }",
"client Cluster X = 1 { critical event A = 1 { int8u x = 1; } }",
Compatibility.ALL_OK)
def test_commands_remove(self):
self.ValidateUpdate(
"Removing commands is not ok",
"client Cluster X = 1 { command A() : DefaulSuccess = 0; command B() : DefaultSuccess = 1; }",
"client Cluster X = 1 { command A() : DefaulSuccess = 0; }",
Compatibility.FORWARD_FAIL)
def test_commands_id(self):
self.ValidateUpdate(
"Changing command IDs is never ok",
"client Cluster X = 1 { command A() : DefaulSuccess = 1; }",
"client Cluster X = 1 { command A() : DefaulSuccess = 2; }",
Compatibility.FORWARD_FAIL | Compatibility.BACKWARD_FAIL)
def test_commands_input(self):
self.ValidateUpdate(
"Changing command Inputs is never ok",
"client Cluster X = 1 { command A() : DefaulSuccess = 1; }",
"client Cluster X = 1 { command A(ARequest) : DefaulSuccess = 1; }",
Compatibility.FORWARD_FAIL | Compatibility.BACKWARD_FAIL)
def test_commands_output(self):
self.ValidateUpdate(
"Changing command Outputs is never ok",
"client Cluster X = 1 { command A() : DefaultSuccess = 1; }",
"client Cluster X = 1 { timed command A() : DefaultSuccess = 1; }",
Compatibility.FORWARD_FAIL | Compatibility.BACKWARD_FAIL)
def test_commands_quality_fabric(self):
self.ValidateUpdate(
"Changing command quality is not ok",
"client Cluster X = 1 { command A() : DefaultSuccess = 1; }",
"client Cluster X = 1 { fabric command A() : DefaultSuccess = 1; }",
Compatibility.FORWARD_FAIL | Compatibility.BACKWARD_FAIL)
def test_commands_quality_timed(self):
self.ValidateUpdate(
"Changing command qualities is never ok",
"client Cluster X = 1 { fabric command A() : DefaultSuccess = 1; }",
"client Cluster X = 1 { fabric timed command A() : DefaultSuccess = 1; }",
Compatibility.FORWARD_FAIL | Compatibility.BACKWARD_FAIL)
def test_commands_change_acl(self):
self.ValidateUpdate(
"Switching access is ok",
"client Cluster X = 1 { command A() : DefaultSuccess = 1; }",
"client Cluster X = 1 { command access(invoke: administer) A() : DefaultSuccess = 1; }",
Compatibility.ALL_OK)
def test_struct_removal(self):
self.ValidateUpdate(
"Structure removal is not ok, but adding is ok",
"client Cluster X = 1 { struct Foo {} struct Bar {} }",
"client Cluster X = 1 { struct Foo {} }",
Compatibility.FORWARD_FAIL)
def test_struct_content_type_change(self):
self.ValidateUpdate(
"Changing structure data types is never ok",
"client Cluster X = 1 { struct Foo { int32u x = 1; } }",
"client Cluster X = 1 { struct Foo { int64u x = 1; } }",
Compatibility.FORWARD_FAIL | Compatibility.BACKWARD_FAIL)
def test_struct_content_rename_reorder(self):
self.ValidateUpdate(
"Structure content renames and reorder is ok.",
"client Cluster X = 1 { struct Foo { int32u x = 1; int8u y = 2; } }",
"client Cluster X = 1 { struct Foo { int8u a = 2; int32u y = 1; } }",
Compatibility.ALL_OK)
def test_struct_content_add_remove(self):
self.ValidateUpdate(
"Structure content change is not ok.",
"client Cluster X = 1 { struct Foo { int32u x = 1; } }",
"client Cluster X = 1 { struct Foo { int32u x = 1; int8u y = 2; } }",
Compatibility.FORWARD_FAIL | Compatibility.BACKWARD_FAIL)
def test_struct_tag_change_request(self):
self.ValidateUpdate(
"Structure type (request/regular) change is not ok",
"client Cluster X = 1 { struct Foo { int32u x = 1; } }",
"client Cluster X = 1 { request struct Foo { int32u x = 1; } }",
Compatibility.FORWARD_FAIL | Compatibility.BACKWARD_FAIL)
def test_struct_tag_change_response(self):
self.ValidateUpdate(
"Structure type (request/response) change is not ok",
"client Cluster X = 1 { request struct Foo { int32u x = 1; } }",
"client Cluster X = 1 { response struct Foo = 1 { int32u x = 1; } }",
Compatibility.FORWARD_FAIL | Compatibility.BACKWARD_FAIL)
def test_struct_code(self):
self.ValidateUpdate(
"Changing struct code is not ok",
"client Cluster X = 1 { response struct Foo = 1 { int32u x = 1; } }",
"client Cluster X = 1 { response struct Foo = 2 { int32u x = 1; } }",
Compatibility.FORWARD_FAIL | Compatibility.BACKWARD_FAIL)
def test_attribute_add_remove(self):
self.ValidateUpdate(
"Attribute removal is not ok.",
"client Cluster X = 1 { readonly attribute int8u a = 1; readonly attribute int8u b = 2; }",
"client Cluster X = 1 { readonly attribute int8u a = 1; }",
Compatibility.FORWARD_FAIL)
def test_attribute_change_type(self):
self.ValidateUpdate(
"Attribute type cannot be changed.",
"client Cluster X = 1 { readonly attribute int8u a = 1; }",
"client Cluster X = 1 { readonly attribute int32u a = 1; }",
Compatibility.FORWARD_FAIL | Compatibility.BACKWARD_FAIL)
def test_attribute_change_code(self):
self.ValidateUpdate(
"Attribute codes cannot be changed.",
"client Cluster X = 1 { readonly attribute int8u a = 1; }",
"client Cluster X = 1 { readonly attribute int8u a = 2; }",
Compatibility.FORWARD_FAIL | Compatibility.BACKWARD_FAIL)
def test_attribute_change_optionality(self):
self.ValidateUpdate(
"Attribute optionality must stay the same",
"client Cluster X = 1 { readonly attribute int8u a = 1; }",
"client Cluster X = 1 { readonly attribute optional int8u a = 1; }",
Compatibility.FORWARD_FAIL | Compatibility.BACKWARD_FAIL)
def test_attribute_change_list(self):
self.ValidateUpdate(
"Attribute list type must stay the same",
"client Cluster X = 1 { readonly attribute int8u a = 1; }",
"client Cluster X = 1 { readonly attribute int8u a[] = 1; }",
Compatibility.FORWARD_FAIL | Compatibility.BACKWARD_FAIL)
def test_attribute_change_rw(self):
self.ValidateUpdate(
"Attribute read/write status must stay the same",
"client Cluster X = 1 { readonly attribute int8u a = 1; }",
"client Cluster X = 1 { attribute int8u a = 1; }",
Compatibility.FORWARD_FAIL | Compatibility.BACKWARD_FAIL)
def test_attribute_type(self):
self.ValidateUpdate(
"Attribute data type must stay the same",
"client Cluster X = 1 { attribute int8u a = 1; }",
"client Cluster X = 1 { attribute char_string a = 1; }",
Compatibility.FORWARD_FAIL | Compatibility.BACKWARD_FAIL)
if __name__ == '__main__':
unittest.main()