Skip to content

Commit

Permalink
python testing: Add PICS parser for xml (#31493)
Browse files Browse the repository at this point in the history
* python testing: Add PICS parser for xml

Code blatently stolen from Jon Rhees.

* Restyled by isort

* remove some debug

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
cecille and restyled-commits authored Feb 5, 2024
1 parent d9460ec commit fea1604
Show file tree
Hide file tree
Showing 3 changed files with 315 additions and 7 deletions.
39 changes: 38 additions & 1 deletion src/python_testing/TestMatterTestingSupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# limitations under the License.
#

import os
import time
import typing
from datetime import datetime, timedelta, timezone
Expand All @@ -23,7 +24,8 @@
from chip.clusters.Types import Nullable, NullValue
from chip.tlv import uint
from matter_testing_support import (MatterBaseTest, async_test_body, compare_time, default_matter_test_main,
get_wait_seconds_from_set_time, parse_pics, type_matches, utc_time_in_matter_epoch)
get_wait_seconds_from_set_time, parse_pics, parse_pics_xml, type_matches,
utc_time_in_matter_epoch)
from mobly import asserts, signals
from taglist_and_topology_test_support import (TagProblem, create_device_type_list_for_root, create_device_type_lists,
find_tag_list_problems, find_tree_roots, get_all_children,
Expand Down Expand Up @@ -584,6 +586,41 @@ def test_root_node_tag_list_functions(self):
problems = find_tag_list_problems(roots=[0], device_types={0: device_type_list}, endpoint_dict=endpoints)
asserts.assert_equal(len(problems.keys()), 0, 'Unexpected problems found in root endpoint')

def pics_assert(self, pics: str, support: bool):
asserts.assert_equal(self.check_pics(pics), support,
f'Unexpected PICS value for {pics} - expected {support}, got {self.check_pics(pics)}')

def test_xml_pics(self):
script_dir = os.path.dirname(os.path.realpath(__file__))
with open(f'{script_dir}/test_testing/example_pics_xml_basic_info.xml') as f:
pics = parse_pics_xml(f.read())
print(pics)
# force the parsed pics here to be in the config so we can check the check_pics function
self.matter_test_config.pics = pics
self.pics_assert('BINFO.S', True)
self.pics_assert('BINFO.S.A0000', True)
self.pics_assert('BINFO.S.A0001', True)
self.pics_assert('BINFO.S.A0002', True)
self.pics_assert('BINFO.S.A0003', True)
self.pics_assert('BINFO.S.A0004', True)
self.pics_assert('BINFO.S.A0005', True)
self.pics_assert('BINFO.S.A0006', True)
self.pics_assert('BINFO.S.A0007', True)
self.pics_assert('BINFO.S.A0008', True)
self.pics_assert('BINFO.S.A0009', True)
self.pics_assert('BINFO.S.A000a', True)
self.pics_assert('BINFO.S.A000b', True)
self.pics_assert('BINFO.S.A000c', True)
self.pics_assert('BINFO.S.A000d', True)
self.pics_assert('BINFO.S.A000e', True)
self.pics_assert('BINFO.S.A000f', True)
self.pics_assert('BINFO.S.A0010', True)
self.pics_assert('BINFO.S.A0011', False)
self.pics_assert('BINFO.S.A0012', True)
self.pics_assert('BINFO.S.A0013', True)
self.pics_assert('BINFO.S.A0014', False)
self.pics_assert('PICSDOESNOTEXIST', False)


if __name__ == "__main__":
default_matter_test_main()
33 changes: 27 additions & 6 deletions src/python_testing/matter_testing_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import argparse
import asyncio
import builtins
import glob
import inspect
import json
import logging
Expand All @@ -29,6 +30,7 @@
import sys
import typing
import uuid
import xml.etree.ElementTree as ET
from binascii import hexlify, unhexlify
from dataclasses import asdict as dataclass_asdict
from dataclasses import dataclass, field
Expand Down Expand Up @@ -138,7 +140,7 @@ def get_default_paa_trust_store(root_path: pathlib.Path) -> pathlib.Path:
return pathlib.Path.cwd()


def parse_pics(lines=typing.List[str]) -> dict[str, bool]:
def parse_pics(lines: typing.List[str]) -> dict[str, bool]:
pics = {}
for raw in lines:
line, _, _ = raw.partition("#")
Expand All @@ -156,11 +158,30 @@ def parse_pics(lines=typing.List[str]) -> dict[str, bool]:
return pics


def read_pics_from_file(filename: str) -> dict[str, bool]:
""" Reads a dictionary of PICS from a file. """
with open(filename, 'r') as f:
lines = f.readlines()
return parse_pics(lines)
def parse_pics_xml(contents: str) -> dict[str, bool]:
pics = {}
mytree = ET.fromstring(contents)
for pi in mytree.iter('picsItem'):
name = pi.find('itemNumber').text
support = pi.find('support').text
pics[name] = int(json.loads(support.lower())) == 1
return pics


def read_pics_from_file(path: str) -> dict[str, bool]:
""" Reads a dictionary of PICS from a file (ci format) or directory (xml format). """
if os.path.isdir(os.path.abspath(path)):
pics_dict = {}
for filename in glob.glob(f'{path}/*.xml'):
with open(filename, 'r') as f:
contents = f.read()
pics_dict.update(parse_pics_xml(contents))
return pics_dict

else:
with open(path, 'r') as f:
lines = f.readlines()
return parse_pics(lines)


def type_matches(received_value, desired_type):
Expand Down
250 changes: 250 additions & 0 deletions src/python_testing/test_testing/example_pics_xml_basic_info.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
<?xml version='1.0' encoding='utf-8'?>
<!--
Autogenerated xml file - Version No:V_24_SVE_1.2
Generated date:2023-09-09 07:41:59
Cluster Name -Basic Information Test Plan
XML PICS -Ref Document:
version master 0cb6035,
Draft
2023-09-08 12:46:22 -0700
-->
<clusterPICS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Generic-PICS-XML-Schema.xsd">
<!--General cluster information-->
<name>Basic Information Test Plan</name>
<clusterId> </clusterId>
<picsRoot> </picsRoot>
<!--Cluster role information-->
<usage>
<picsItem>
<itemNumber>BINFO.S</itemNumber>
<feature>Does the device implement the Basic Information Cluster as a server?</feature>
<reference>9.1. Role - index.html[pdf]</reference>
<status>O</status>
<support>true</support>
</picsItem>
</usage>
<!--PIXIT-->
<pixit>
<pixitItem>
<itemNumber>PIXIT.BINFO.PrimaryColor</itemNumber>
<feature>ProductAppearance.PrimaryColor should reflect the product&#8217;s color</feature>
<reference>10. PIXIT Definition - index.html[pdf]</reference>
<status cond="BINFO.S.A0014">M</status>
<support>0x00</support>
</pixitItem>
<pixitItem>
<itemNumber>PIXIT.BINFO.Finish</itemNumber>
<feature>ProductAppearance.Finish should reflect the product&#8217;s finish</feature>
<reference>10. PIXIT Definition - index.html[pdf]</reference>
<status cond="BINFO.S.A0014">M</status>
<support>0x00</support>
</pixitItem>
</pixit>
<!--Server side PICS-->
<clusterSide type="Server">
<!--Attributes PICS write-->
<attributes>
<picsItem>
<itemNumber>BINFO.S.A0000</itemNumber>
<feature>Does the DUT(server) support the DataModelRevision attribute?</feature>
<reference>9.2.1. Attributes - index.html[pdf]</reference>
<status cond="BINFO.S">M</status>
<support>true</support>
</picsItem>
<picsItem>
<itemNumber>BINFO.S.A0001</itemNumber>
<feature>Does the DUT(server) support the VendorName attribute?</feature>
<reference>9.2.1. Attributes - index.html[pdf]</reference>
<status cond="BINFO.S">M</status>
<support>true</support>
</picsItem>
<picsItem>
<itemNumber>BINFO.S.A0002</itemNumber>
<feature>Does the DUT(server) support the VendorID attribute?</feature>
<reference>9.2.1. Attributes - index.html[pdf]</reference>
<status cond="BINFO.S">M</status>
<support>true</support>
</picsItem>
<picsItem>
<itemNumber>BINFO.S.A0003</itemNumber>
<feature>Does the DUT(server) support the ProductName attribute?</feature>
<reference>9.2.1. Attributes - index.html[pdf]</reference>
<status cond="BINFO.S">M</status>
<support>true</support>
</picsItem>
<picsItem>
<itemNumber>BINFO.S.A0004</itemNumber>
<feature>Does the DUT(server) support the ProductID attribute?</feature>
<reference>9.2.1. Attributes - index.html[pdf]</reference>
<status cond="BINFO.S">M</status>
<support>true</support>
</picsItem>
<picsItem>
<itemNumber>BINFO.S.A0005</itemNumber>
<feature>Does the DUT(server) support the NodeLabel attribute?</feature>
<reference>9.2.1. Attributes - index.html[pdf]</reference>
<status cond="BINFO.S">M</status>
<support>true</support>
</picsItem>
<picsItem>
<itemNumber>BINFO.S.A0006</itemNumber>
<feature>Does the DUT(server) support the Location attribute?</feature>
<reference>9.2.1. Attributes - index.html[pdf]</reference>
<status cond="BINFO.S">M</status>
<support>true</support>
</picsItem>
<picsItem>
<itemNumber>BINFO.S.A0007</itemNumber>
<feature>Does the DUT(server) support the HardwareVersion attribute?</feature>
<reference>9.2.1. Attributes - index.html[pdf]</reference>
<status cond="BINFO.S">M</status>
<support>true</support>
</picsItem>
<picsItem>
<itemNumber>BINFO.S.A0008</itemNumber>
<feature>Does the DUT(server) support the HardwareVersionString attribute?</feature>
<reference>9.2.1. Attributes - index.html[pdf]</reference>
<status cond="BINFO.S">M</status>
<support>true</support>
</picsItem>
<picsItem>
<itemNumber>BINFO.S.A0009</itemNumber>
<feature>Does the DUT(server) support the SoftwareVersion attribute?</feature>
<reference>9.2.1. Attributes - index.html[pdf]</reference>
<status cond="BINFO.S">M</status>
<support>true</support>
</picsItem>
<picsItem>
<itemNumber>BINFO.S.A000a</itemNumber>
<feature>Does the DUT(server) support the SoftwareVersionString attribute?</feature>
<reference>9.2.1. Attributes - index.html[pdf]</reference>
<status cond="BINFO.S">M</status>
<support>true</support>
</picsItem>
<picsItem>
<itemNumber>BINFO.S.A000b</itemNumber>
<feature>Does the DUT(server) support the ManufacturingDate attribute?</feature>
<reference>9.2.1. Attributes - index.html[pdf]</reference>
<status cond="BINFO.S">O</status>
<support>true</support>
</picsItem>
<picsItem>
<itemNumber>BINFO.S.A000c</itemNumber>
<feature>Does the DUT(server) support the PartNumber attribute?</feature>
<reference>9.2.1. Attributes - index.html[pdf]</reference>
<status cond="BINFO.S">O</status>
<support>true</support>
</picsItem>
<picsItem>
<itemNumber>BINFO.S.A000d</itemNumber>
<feature>Does the DUT(server) support the ProductURL attribute?</feature>
<reference>9.2.1. Attributes - index.html[pdf]</reference>
<status cond="BINFO.S">O</status>
<support>true</support>
</picsItem>
<picsItem>
<itemNumber>BINFO.S.A000e</itemNumber>
<feature>Does the DUT(server) support the ProductLabel attribute?</feature>
<reference>9.2.1. Attributes - index.html[pdf]</reference>
<status cond="BINFO.S">O</status>
<support>true</support>
</picsItem>
<picsItem>
<itemNumber>BINFO.S.A000f</itemNumber>
<feature>Does the DUT(server) support the SerialNumber attribute?</feature>
<reference>9.2.1. Attributes - index.html[pdf]</reference>
<status cond="BINFO.S">O</status>
<support>true</support>
</picsItem>
<picsItem>
<itemNumber>BINFO.S.A0010</itemNumber>
<feature>Does the DUT(server) support the LocalConfigDisabled attribute?</feature>
<reference>9.2.1. Attributes - index.html[pdf]</reference>
<status cond="BINFO.S">O</status>
<support>true</support>
</picsItem>
<picsItem>
<itemNumber>BINFO.S.A0011</itemNumber>
<feature>Does the DUT(server) support the Reachable attribute?</feature>
<reference>9.2.1. Attributes - index.html[pdf]</reference>
<status cond="BINFO.S">O</status>
<support>false</support>
</picsItem>
<picsItem>
<itemNumber>BINFO.S.A0012</itemNumber>
<feature>Does the DUT(server) support the UniqueID attribute?</feature>
<reference>9.2.1. Attributes - index.html[pdf]</reference>
<status cond="BINFO.S">O</status>
<support>true</support>
</picsItem>
<picsItem>
<itemNumber>BINFO.S.A0013</itemNumber>
<feature>Does the DUT(server) support the CapabilityMinima attribute?</feature>
<reference>9.2.1. Attributes - index.html[pdf]</reference>
<status cond="BINFO.S">M</status>
<support>true</support>
</picsItem>
<picsItem>
<itemNumber>BINFO.S.A0014</itemNumber>
<feature>Does the DUT(server) support the ProductAppearance attribute?</feature>
<reference>9.2.1. Attributes - index.html[pdf]</reference>
<status cond="BINFO.S">O</status>
<support>false</support>
</picsItem>
</attributes>
<!--Events PICS write-->
<events>
<picsItem>
<itemNumber>BINFO.S.E00</itemNumber>
<feature>Does the DUT(server) support the StartUp event?</feature>
<reference>9.2.2. Events - index.html[pdf]</reference>
<status cond="BINFO.S">M</status>
<support>true</support>
</picsItem>
<picsItem>
<itemNumber>BINFO.S.E01</itemNumber>
<feature>Does the DUT(server) support the ShutDown event?</feature>
<reference>9.2.2. Events - index.html[pdf]</reference>
<status cond="BINFO.S">O</status>
<support>false</support>
</picsItem>
<picsItem>
<itemNumber>BINFO.S.E02</itemNumber>
<feature>Does the DUT(server) support the Leave event?</feature>
<reference>9.2.2. Events - index.html[pdf]</reference>
<status cond="BINFO.S">O</status>
<support>false</support>
</picsItem>
<picsItem>
<itemNumber>BINFO.S.E03</itemNumber>
<feature>Does the DUT(server) support the ReachableChanged event?</feature>
<reference>9.2.2. Events - index.html[pdf]</reference>
<status cond="BINFO.S.A0011">M</status>
<support>false</support>
</picsItem>
</events>
<!--Commands generated PICS write-->
<commandsGenerated />
<!--Commands received PICS write-->
<commandsReceived />
<!--Features PICS write-->
<features />
<!--Manual controllable PICS write-->
<manually />
</clusterSide>
<!--Client side PICS-->
<clusterSide type="Client">
<!--Attributes PICS write-->
<attributes />
<!--Events PICS write-->
<events />
<!--Commands generated PICS write-->
<commandsGenerated />
<!--Commands received PICS write-->
<commandsReceived />
<!--Features PICS write-->
<features />
<!--Manual controllable PICS write-->
<manually />
</clusterSide>
</clusterPICS>

0 comments on commit fea1604

Please sign in to comment.