Skip to content

Commit

Permalink
TC-RR-1.1: Add optional local session ID checking (#22118)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjerryjohns authored and pull[bot] committed Nov 13, 2023
1 parent ccba924 commit 05240dc
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
23 changes: 23 additions & 0 deletions src/controller/python/ChipDeviceController-ScriptBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ void pychip_Stack_SetLogFunct(LogMessageFunct logFunct);
ChipError::StorageType pychip_GetConnectedDeviceByNodeId(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId,
DeviceAvailableFunc callback);
ChipError::StorageType pychip_FreeOperationalDeviceProxy(chip::OperationalDeviceProxy * deviceProxy);
ChipError::StorageType pychip_GetLocalSessionId(chip::OperationalDeviceProxy * deviceProxy, uint16_t * localSessionId);
ChipError::StorageType pychip_GetNumSessionsToPeer(chip::OperationalDeviceProxy * deviceProxy, uint32_t * numSessions);
ChipError::StorageType pychip_GetDeviceBeingCommissioned(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId,
CommissioneeDeviceProxy ** proxy);
ChipError::StorageType pychip_ExpireSessions(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId);
Expand Down Expand Up @@ -699,6 +701,27 @@ ChipError::StorageType pychip_FreeOperationalDeviceProxy(chip::OperationalDevice
return CHIP_NO_ERROR.AsInteger();
}

ChipError::StorageType pychip_GetLocalSessionId(chip::OperationalDeviceProxy * deviceProxy, uint16_t * localSessionId)
{
VerifyOrReturnError(deviceProxy->GetSecureSession().HasValue(), CHIP_ERROR_MISSING_SECURE_SESSION.AsInteger());
VerifyOrReturnError(localSessionId != nullptr, CHIP_ERROR_INVALID_ARGUMENT.AsInteger());

*localSessionId = deviceProxy->GetSecureSession().Value()->AsSecureSession()->GetLocalSessionId();
return CHIP_NO_ERROR.AsInteger();
}

ChipError::StorageType pychip_GetNumSessionsToPeer(chip::OperationalDeviceProxy * deviceProxy, uint32_t * numSessions)
{
VerifyOrReturnError(deviceProxy->GetSecureSession().HasValue(), CHIP_ERROR_MISSING_SECURE_SESSION.AsInteger());
VerifyOrReturnError(numSessions != nullptr, CHIP_ERROR_INVALID_ARGUMENT.AsInteger());

*numSessions = 0;
deviceProxy->GetExchangeManager()->GetSessionManager()->ForEachMatchingSession(
deviceProxy->GetPeerScopedNodeId(), [numSessions](auto * session) { (*numSessions)++; });

return CHIP_NO_ERROR.AsInteger();
}

ChipError::StorageType pychip_GetDeviceBeingCommissioned(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId,
CommissioneeDeviceProxy ** proxy)
{
Expand Down
28 changes: 27 additions & 1 deletion src/controller/python/chip/ChipDeviceCtrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def __init__(self, deviceProxy: ctypes.c_void_p, dmLib=None):
self._dmLib = dmLib

def __del__(self):
if (self._dmLib is not None and builtins.chipStack is not None):
if (self._dmLib is not None and hasattr(builtins, 'chipStack') and builtins.chipStack is not None):
# This destructor is called from any threading context, including on the Matter threading context.
# So, we cannot call chipStack.Call or chipStack.CallAsync which waits for the posted work to
# actually be executed. Instead, we just post/schedule the work and move on.
Expand All @@ -140,6 +140,32 @@ def __del__(self):
def deviceProxy(self) -> ctypes.c_void_p:
return self._deviceProxy

@property
def localSessionId(self) -> int:
self._dmLib.pychip_GetLocalSessionId.argtypes = [ctypes.c_void_p, POINTER(ctypes.c_uint16)]
self._dmLib.pychip_GetLocalSessionId.restype = ctypes.c_uint32

localSessionId = ctypes.c_uint16(0)

builtins.chipStack.Call(
lambda: self._dmLib.pychip_GetLocalSessionId(self._deviceProxy, pointer(localSessionId))
)

return localSessionId.value

@property
def numTotalSessions(self) -> int:
self._dmLib.pychip_GetNumSessionsToPeer.argtypes = [ctypes.c_void_p, POINTER(ctypes.c_uint32)]
self._dmLib.pychip_GetNumSessionsToPeer.restype = ctypes.c_uint32

numSessions = ctypes.c_uint32(0)

builtins.chipStack.Call(
lambda: self._dmLib.pychip_GetNumSessionsToPeer(self._deviceProxy, pointer(numSessions))
)

return numSessions.value


class DiscoveryFilterType(enum.IntEnum):
# These must match chip::Dnssd::DiscoveryFilterType values (barring the naming convention)
Expand Down
30 changes: 27 additions & 3 deletions src/python_testing/TC_RR_1_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ async def test_TC_RR_1_1(self):
timeout_delay_sec = self.user_params.get("timeout_delay_sec", max_report_interval_sec * 2)
# Whether to skip filling the UserLabel clusters
skip_user_label_cluster_steps = self.user_params.get("skip_user_label_cluster_steps", False)
# Whether to do the local session ID comparison checks to prove new sessions have not been established.
check_local_session_id_unchanged = self.user_params.get("check_local_session_id_unchanged", False)

BEFORE_LABEL = "Before Subscriptions 12345678912"
AFTER_LABEL = "After Subscriptions 123456789123"
Expand Down Expand Up @@ -143,6 +145,8 @@ async def test_TC_RR_1_1(self):
num_controllers_per_fabric, "Must have the right number of clients")

client_by_name = {client.name: client for client in client_list}
local_session_id_by_client_name = {client.name: client.GetConnectedDeviceSync(
self.dut_node_id).localSessionId for client in client_list}

# Step 2: Set the Label field for each fabric and BasicInformation.NodeLabel to 32 characters
logging.info("Step 2: Setting the Label field for each fabric and BasicInformation.NodeLabel to 32 characters")
Expand Down Expand Up @@ -311,15 +315,35 @@ async def test_TC_RR_1_1(self):
asserts.fail("Failed step 7 !")

# Step 8: Validate sessions have not changed by doing a read on NodeLabel from all clients
logging.info("Step 8: Read back NodeLabel directly from all clients")
logging.info("Step 8a: Read back NodeLabel directly from all clients")
for sub_idx, client in enumerate(client_list):
logging.info("Reading NodeLabel (%d/%d) from controller node %s" % (sub_idx + 1, len(client_list), client.name))

label_readback = await self.read_single_attribute(client, node_id=self.dut_node_id, endpoint=0, attribute=Clusters.Basic.Attributes.NodeLabel)
asserts.assert_equal(label_readback, AFTER_LABEL)

# TODO: Compare before/after session IDs. Requires more native changes, and the
# subcription method above is actually good enough we think.
# On each client, read back the local session id for the CASE session to the DUT and ensure it's the same as that of the session established right at the
# beginning of the test. In tandem with checking that the number of sessions to the DUT is exactly one, this ensures we have not established any new CASE
# sessions in this test.
if check_local_session_id_unchanged:
logging.info("Step 8b: Validate that the local CASE session ID hasn't changed")
num_failed_clients = 0

for client in client_list:
beginning_session_id = local_session_id_by_client_name[client.name]
end_session_id = client.GetConnectedDeviceSync(self.dut_node_id).localSessionId
total_sessions = client.GetConnectedDeviceSync(self.dut_node_id).numTotalSessions

if (beginning_session_id != end_session_id):
logging.error(
f"Test ended with a different session ID created from what we had before for {client.name} (total sessions = {total_sessions})")
num_failed_clients = num_failed_clients + 1
elif (total_sessions != 1):
logging.error(f"Test ended with more than 1 session for {client.name}")
num_failed_clients = num_failed_clients + 1

if (num_failed_clients > 0):
asserts.fail(f"Failed Step 8b: ({num_failed_clients} / {len(client_list)} failed)")

# Step 9: Fill user label list
if has_user_labels and not skip_user_label_cluster_steps:
Expand Down

0 comments on commit 05240dc

Please sign in to comment.