Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move common validation methods to base class #37151

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions src/python_testing/TC_DGSW_2_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,10 @@

import chip.clusters as Clusters
from chip.testing.matter_testing import MatterBaseTest, TestStep, async_test_body, default_matter_test_main
from mobly import asserts


class TC_DGSW_2_1(MatterBaseTest):

@staticmethod
def is_valid_uint64_value(value):
return isinstance(value, int) and 0 <= value <= 0xFFFFFFFFFFFFFFFF

@staticmethod
def is_valid_uint32_value(value):
return isinstance(value, int) and 0 <= value <= 0xFFFFFFFF

@staticmethod
def is_valid_str_value(value):
return isinstance(value, str) and len(value) > 0

def assert_valid_uint64(self, value, field_name):
"""Asserts that the value is a valid uint64."""
asserts.assert_true(self.is_valid_uint64_value(value), f"{field_name} field should be a uint64 type")

def assert_valid_uint32(self, value, field_name):
"""Asserts that the value is a valid uint32."""
asserts.assert_true(self.is_valid_uint32_value(value), f"{field_name} field should be a uint32 type")

def assert_valid_str(self, value, field_name):
"""Asserts that the value is a non-empty string."""
asserts.assert_true(self.is_valid_str_value(value), f"{field_name} field should be a non-empty string")

async def read_dgsw_attribute_expect_success(self, endpoint, attribute):
cluster = Clusters.Objects.SoftwareDiagnostics
return await self.read_single_attribute_check_success(endpoint=endpoint, cluster=cluster, attribute=attribute)
Expand Down
6 changes: 1 addition & 5 deletions src/python_testing/TC_DGSW_2_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@

class TC_DGSW_2_2(MatterBaseTest):

@staticmethod
def is_valid_uint64_value(value):
return isinstance(value, int) and 0 <= value <= 0xFFFFFFFFFFFFFFFF

@staticmethod
def is_valid_octet_string(value):
return isinstance(value, (bytes, bytearray))
Expand All @@ -71,7 +67,7 @@ def validate_soft_fault_event_data(self, event_data):

# Validate 'Id' field: Ensure it is a uint64 type
asserts.assert_true(
self.is_valid_uint64_value(event_data.id),
self.is_valid_uint_value(event_data.id, bit_count=64),
"The 'Id' field must be a uint64 type"
)

Expand Down
24 changes: 0 additions & 24 deletions src/python_testing/TC_DGSW_2_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,6 @@

class TC_DGSW_2_3(MatterBaseTest):

@staticmethod
def is_valid_uint64_value(value):
return isinstance(value, int) and 0 <= value <= 0xFFFFFFFFFFFFFFFF

@staticmethod
def is_valid_uint32_value(value):
return isinstance(value, int) and 0 <= value <= 0xFFFFFFFF

@staticmethod
def is_valid_str_value(value):
return isinstance(value, str) and len(value) > 0

def assert_valid_uint64(self, value, field_name):
"""Asserts that the value is a valid uint64."""
asserts.assert_true(self.is_valid_uint64_value(value), f"{field_name} field should be a uint64 type")

def assert_valid_uint32(self, value, field_name):
"""Asserts that the value is a valid uint32."""
asserts.assert_true(self.is_valid_uint32_value(value), f"{field_name} field should be a uint32 type")

def assert_valid_str(self, value, field_name):
"""Asserts that the value is a non-empty string."""
asserts.assert_true(self.is_valid_str_value(value), f"{field_name} field should be a non-empty string")

async def read_dgsw_attribute_expect_success(self, endpoint, attribute):
cluster = Clusters.Objects.SoftwareDiagnostics
return await self.read_single_attribute_check_success(endpoint=endpoint, cluster=cluster, attribute=attribute)
Expand Down
32 changes: 0 additions & 32 deletions src/python_testing/TC_DGWIFI_2_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,6 @@ def is_valid_bssid(bssid) -> bool:

return False

@staticmethod
def is_valid_uint_value(value, bit_count=64):
"""
Checks if 'value' is a non-negative integer fitting into 'bit_count' bits.
For example, bit_count=32 => must fit within 0 <= value <= 0xFFFFFFFF
"""
if not isinstance(value, int):
return False
if value < 0:
return False
return value < 2**bit_count

def assert_valid_bssid(self, value, field_name):
"""Asserts that the value is a valid BSSID (MAC address), None, or NullValue."""
if isinstance(value, Nullable):
Expand All @@ -86,26 +74,6 @@ def assert_valid_bssid(self, value, field_name):
asserts.assert_true(self.is_valid_bssid(value),
f"{field_name} should be a valid BSSID string (e.g., '00:11:22:33:44:55') or None/NullValue.")

def assert_valid_uint64(self, value, field_name):
"""Asserts that the value is a valid uint64 or None (if attribute can return NULL)."""
asserts.assert_true(value is None or self.is_valid_uint_value(value, bit_count=64),
f"{field_name} should be a uint64 or NULL.")

def assert_valid_uint32(self, value, field_name):
"""Asserts that the value is a valid uint32 or None (if attribute can return NULL)."""
asserts.assert_true(value is None or self.is_valid_uint_value(value, bit_count=32),
f"{field_name} should be a uint32 or NULL.")

def assert_valid_uint16(self, value, field_name):
"""Asserts that the value is a valid uint16 or None (if attribute can return NULL)."""
asserts.assert_true(value is None or self.is_valid_uint_value(value, bit_count=16),
f"{field_name} should be a uint16 or NULL.")

def assert_valid_uint8(self, value, field_name):
"""Asserts that the value is a valid uint16 or None (if attribute can return NULL)."""
asserts.assert_true(value is None or self.is_valid_uint_value(value, bit_count=8),
f"{field_name} should be a uint8 or NULL.")

async def read_dgwifi_attribute_expect_success(self, endpoint, attribute):
cluster = Clusters.Objects.WiFiNetworkDiagnostics
return await self.read_single_attribute_check_success(endpoint=endpoint, cluster=cluster, attribute=attribute)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,46 @@ def __init__(self, *args):
# The named pipe name must be set in the derived classes
self.app_pipe = None

@staticmethod
def is_valid_uint_value(value, bit_count=64):
"""
Checks if 'value' is a non-negative integer fitting into 'bit_count' bits.
For example, bit_count=32 => must fit within 0 <= value <= 0xFFFFFFFF
"""
if not isinstance(value, int):
return False
if value < 0:
return False
return value < 2**bit_count

@staticmethod
def is_valid_str_value(value):
return isinstance(value, str) and len(value) > 0

def assert_valid_uint64(self, value, field_name):
"""Asserts that the value is a valid uint64."""
asserts.assert_true(self.is_valid_uint_value(value, bit_count=64),
f"{field_name} should be a uint64 or NULL.")

def assert_valid_uint32(self, value, field_name):
"""Asserts that the value is a valid uint32."""
asserts.assert_true(self.is_valid_uint_value(value, bit_count=32),
f"{field_name} should be a uint32 or NULL.")

def assert_valid_uint16(self, value, field_name):
"""Asserts that the value is a valid uint16."""
asserts.assert_true(self.is_valid_uint_value(value, bit_count=16),
f"{field_name} should be a uint16 or NULL.")

def assert_valid_uint8(self, value, field_name):
"""Asserts that the value is a valid uint16."""
asserts.assert_true(self.is_valid_uint_value(value, bit_count=8),
f"{field_name} should be a uint8 or NULL.")

def assert_valid_str(self, value, field_name):
"""Asserts that the value is a non-empty string."""
asserts.assert_true(self.is_valid_str_value(value), f"{field_name} field should be a non-empty string")

def get_test_steps(self, test: str) -> list[TestStep]:
''' Retrieves the test step list for the given test

Expand Down
Loading