Skip to content

Commit eb732ac

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#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 b63d24c commit eb732ac

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,25 @@
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+
43+
3044
/// Setup the test suite (called once before all test_xxx functions are called)
3145
void suiteSetUp() {}
3246

@@ -233,6 +247,111 @@ void test_zwapi_init()
233247
TEST_ASSERT_EQUAL(3, serial_fd);
234248
}
235249

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

0 commit comments

Comments
 (0)