Skip to content

Commit 575a894

Browse files
committed
fix(zwapi): Add test to check for injection in caps
This change is isolated to revert the previous fix for testing Test will fail when the fix is reverted as: zwapi_init_test.c:250:test_zwapi_refresh_capabilities_bitmask:PASS zwapi_init_test.c:302:test_zwapi_refresh_capabilities_bitmask_injection:FAIL:Function zwapi_session_flush_queue. Called more times than expected. 2025-Jun-20 16:39:06.270076 <E> [zwapi_init] Failed to fetch capabilities from the Z-Wave module zwapi_init_test.c:355:test_zwapi_refresh_capabilities_bad_cases:PASS Origin: SiliconLabsSoftware#126 Relate-to: SiliconLabsSoftware#125 Bug-SiliconLabs: UIC-3664 Relate-to: SLVDBBP-3162484 Relate-to: SiliconLabsSoftware/z-wave-engine-application-layer#42 Signed-off-by: Philippe Coval <[email protected]>
1 parent 7a0ff98 commit 575a894

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

applications/zpc/components/zwave_api/test/zwapi_init_test.c

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,24 @@
2222
#include "zwapi_func_ids.h"
2323
#include "zwapi_session_mock.h"
2424
#include "zwapi_protocol_basis_mock.h"
25+
#include "zwapi_internal.h"
2526

2627
// Static variables
2728
static uint8_t response_length = 0;
2829
static uint8_t response_buffer[255] = {0};
2930

31+
// Unmocked stubs functions:
32+
33+
uint8_t *zwapi_get_zw_library_version_stub(uint8_t *dst, int cmock_num_calls)
34+
{
35+
return 0; //defaut, not valid
36+
}
37+
38+
zwave_rf_region_t zwapi_get_rf_region_stub(int cmock_num_calls)
39+
{
40+
return 0; // default
41+
}
42+
3043
/// Setup the test suite (called once before all test_xxx functions are called)
3144
void suiteSetUp() {}
3245

@@ -233,6 +246,113 @@ void test_zwapi_init()
233246
TEST_ASSERT_EQUAL(3, serial_fd);
234247
}
235248

249+
void test_zwapi_refresh_capabilities_bitmask(void)
250+
{
251+
zwapi_chip_data_t chip;
252+
uint8_t response_buffer[FRAME_LENGTH_MAX] = {0};
253+
uint8_t response_length = IDX_DATA + 7 + sizeof(chip.supported_bitmask);
254+
memset(response_buffer,
255+
0xAA,
256+
sizeof(response_buffer)); // Fill with dummy data
257+
258+
// Simulate a valid response with sufficient length for supported_bitmask
259+
zwapi_session_send_frame_with_response_ExpectAndReturn(
260+
FUNC_ID_SERIAL_API_GET_CAPABILITIES,
261+
NULL,
262+
0,
263+
NULL,
264+
NULL,
265+
SL_STATUS_OK);
266+
zwapi_session_send_frame_with_response_IgnoreArg_response_buf();
267+
zwapi_session_send_frame_with_response_IgnoreArg_response_len();
268+
zwapi_session_send_frame_with_response_ReturnMemThruPtr_response_buf(
269+
response_buffer,
270+
response_length);
271+
zwapi_session_send_frame_with_response_ReturnThruPtr_response_len(
272+
&response_length);
273+
274+
zwapi_get_zw_library_version_Stub(
275+
(CMOCK_zwapi_get_zw_library_version_CALLBACK)
276+
zwapi_get_zw_library_version_stub);
277+
278+
zwapi_session_flush_queue_Expect();
279+
{
280+
uint8_t response_buffer[FRAME_LENGTH_MAX] = {0};
281+
uint8_t response_length = IDX_DATA + 1;
282+
283+
zwapi_session_send_frame_with_response_ExpectAndReturn(
284+
FUNC_ID_SERIAL_API_GET_INIT_DATA,
285+
NULL,
286+
0,
287+
NULL,
288+
NULL,
289+
SL_STATUS_OK);
290+
zwapi_session_send_frame_with_response_IgnoreArg_response_buf();
291+
zwapi_session_send_frame_with_response_IgnoreArg_response_len();
292+
zwapi_session_send_frame_with_response_ReturnMemThruPtr_response_buf(
293+
response_buffer,
294+
response_length);
295+
zwapi_session_send_frame_with_response_ReturnThruPtr_response_len(
296+
&response_length);
297+
}
298+
299+
zwapi_get_rf_region_Stub(zwapi_get_rf_region_stub);
300+
TEST_ASSERT_EQUAL(SL_STATUS_OK, zwapi_refresh_capabilities());
301+
}
302+
303+
void test_zwapi_refresh_capabilities_bitmask_injection(void)
304+
{
305+
uint8_t response_buffer[FRAME_LENGTH_MAX] = {0};
306+
uint8_t response_length = FRAME_LENGTH_MAX;
307+
memset(response_buffer,
308+
0xAA,
309+
sizeof(response_buffer)); // Fill with dummy data
310+
311+
// Simulate a valid response with sufficient length for supported_bitmask
312+
zwapi_session_send_frame_with_response_ExpectAndReturn(
313+
FUNC_ID_SERIAL_API_GET_CAPABILITIES,
314+
NULL,
315+
0,
316+
NULL,
317+
NULL,
318+
SL_STATUS_OK);
319+
zwapi_session_send_frame_with_response_IgnoreArg_response_buf();
320+
zwapi_session_send_frame_with_response_IgnoreArg_response_len();
321+
zwapi_session_send_frame_with_response_ReturnMemThruPtr_response_buf(
322+
response_buffer,
323+
response_length);
324+
zwapi_session_send_frame_with_response_ReturnThruPtr_response_len(
325+
&response_length);
326+
327+
zwapi_get_zw_library_version_Stub(
328+
(CMOCK_zwapi_get_zw_library_version_CALLBACK)
329+
zwapi_get_zw_library_version_stub);
330+
zwapi_session_flush_queue_Expect();
331+
{
332+
uint8_t response_buffer[FRAME_LENGTH_MAX] = {0};
333+
uint8_t response_length = IDX_DATA + 1;
334+
335+
zwapi_session_send_frame_with_response_ExpectAndReturn(
336+
FUNC_ID_SERIAL_API_GET_INIT_DATA,
337+
NULL,
338+
0,
339+
NULL,
340+
NULL,
341+
SL_STATUS_OK);
342+
zwapi_session_send_frame_with_response_IgnoreArg_response_buf();
343+
zwapi_session_send_frame_with_response_IgnoreArg_response_len();
344+
zwapi_session_send_frame_with_response_ReturnMemThruPtr_response_buf(
345+
response_buffer,
346+
response_length);
347+
zwapi_session_send_frame_with_response_ReturnThruPtr_response_len(
348+
&response_length);
349+
}
350+
351+
zwapi_get_rf_region_Stub(zwapi_get_rf_region_stub);
352+
TEST_ASSERT_EQUAL(SL_STATUS_OK, zwapi_refresh_capabilities());
353+
354+
}
355+
236356
void test_zwapi_refresh_capabilities_bad_cases()
237357
{
238358
//Capablities status fail

0 commit comments

Comments
 (0)