Skip to content

Commit 6f3ef4a

Browse files
silabs-aydoganerzr
authored andcommitted
uic: zpc: Pull request #2924: UIC-3596: ZWAPI unit tests for S2V2
Merge in UIC/uic from test/UIC-3596-zwapi-unit-test to develop (cherry picked from commit 9618e09374014c9653f732bfd8d1647305bbf08d) Forwarded: #11 Signed-off-by: Philippe Coval <philippe.coval@silabs.com>
1 parent 970b209 commit 6f3ef4a

File tree

5 files changed

+273
-4
lines changed

5 files changed

+273
-4
lines changed

applications/zpc/components/zwave_api/test/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ EXCLUDE zwapi_session.c
3939
zwapi_init.c
4040
)
4141

42+
target_add_unittest(zwave_api
43+
NAME zwapi_protocol_controller_test
44+
SOURCES zwapi_protocol_controller_test.c
45+
zwapi_internal_init_mock.c
46+
DEPENDS zwapi_internal_mock
47+
zwave_api_mock
48+
EXCLUDE zwapi_session.c
49+
zwapi_init.c
50+
)
51+
4252
target_add_unittest(zwave_api
4353
NAME zwapi_protocol_transport_test
4454
SOURCES zwapi_protocol_transport_test.c

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@ void test_zwapi_soft_reset()
3636
TEST_ASSERT_EQUAL(SL_STATUS_OK, zwapi_soft_reset());
3737
}
3838

39+
void test_zwapi_supports_nls()
40+
{
41+
//Happy case
42+
zwapi_support_command_func_ExpectAndReturn(FUNC_ID_ZW_TRANSFER_PROTOCOL_CC, true);
43+
zwapi_support_command_func_ExpectAndReturn(FUNC_ID_ZW_ENABLE_NODE_NLS, true);
44+
zwapi_support_command_func_ExpectAndReturn(FUNC_ID_ZW_GET_NODE_NLS_STATE, true);
45+
zwapi_support_command_func_ExpectAndReturn(FUNC_ID_ZW_REQUEST_PROTOCOL_CC_ENCRYPTION, true);
46+
zwapi_support_command_func_ExpectAndReturn(FUNC_ID_ZW_SEND_PROTOCOL_DATA, true);
47+
TEST_ASSERT_EQUAL(true, zwapi_supports_nls());
48+
49+
//Bad case
50+
zwapi_support_command_func_ExpectAndReturn(FUNC_ID_ZW_TRANSFER_PROTOCOL_CC, true);
51+
zwapi_support_command_func_ExpectAndReturn(FUNC_ID_ZW_ENABLE_NODE_NLS, true);
52+
zwapi_support_command_func_ExpectAndReturn(FUNC_ID_ZW_GET_NODE_NLS_STATE, false);
53+
TEST_ASSERT_EQUAL(false, zwapi_supports_nls());
54+
}
55+
3956
void test_zwapi_get_zw_protocol_status()
4057
{
4158
//Happy case
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/******************************************************************************
2+
* # License
3+
* <b>Copyright 2024 Silicon Laboratories Inc. www.silabs.com</b>
4+
******************************************************************************
5+
* The licensor of this software is Silicon Laboratories Inc. Your use of this
6+
* software is governed by the terms of Silicon Labs Master Software License
7+
* Agreement (MSLA) available at
8+
* www.silabs.com/about-us/legal/master-software-license-agreement. This
9+
* software is distributed to you in Source Code format and is governed by the
10+
* sections of the MSLA applicable to Source Code.
11+
*
12+
*****************************************************************************/
13+
#include "unity.h"
14+
#include "zwapi_protocol_controller.h"
15+
#include "zwapi_func_ids.h"
16+
17+
// Session mock:
18+
#include "zwapi_session_mock.h"
19+
#include "zwapi_init_mock.h"
20+
#include "zwapi_utils.h"
21+
22+
/// Setup the test suite (called once before all test_xxx functions are called)
23+
void suiteSetUp() {}
24+
25+
/// Teardown the test suite (called once after all test_xxx functions are called)
26+
int suiteTearDown(int num_failures)
27+
{
28+
return num_failures;
29+
}
30+
31+
/// Called before each and every test
32+
void setUp() {}
33+
34+
void test_zwapi_enable_node_nls(void)
35+
{
36+
zwave_node_id_t node_id = 2;
37+
uint8_t response_buffer[] = {0x04 /* length = len(payload) + 3 */,
38+
0x01 /* type: response */,
39+
FUNC_ID_ZW_ENABLE_NODE_NLS /* cmd */,
40+
0x01 /* payload */};
41+
uint8_t response_length = 4;
42+
uint8_t payload_buffer[] = {0x02};
43+
uint8_t payload_buffer_length = 1;
44+
45+
zwapi_session_send_frame_with_response_ExpectAndReturn(
46+
FUNC_ID_ZW_ENABLE_NODE_NLS,
47+
payload_buffer,
48+
payload_buffer_length,
49+
response_buffer,
50+
&response_length,
51+
SL_STATUS_OK);
52+
zwapi_session_send_frame_with_response_IgnoreArg_response_buf();
53+
zwapi_session_send_frame_with_response_IgnoreArg_response_len();
54+
zwapi_session_send_frame_with_response_ReturnMemThruPtr_response_buf(
55+
response_buffer,
56+
response_length);
57+
zwapi_session_send_frame_with_response_ReturnThruPtr_response_len(
58+
&response_length);
59+
60+
TEST_ASSERT_EQUAL(SL_STATUS_OK, zwapi_enable_node_nls(node_id));
61+
}
62+
63+
void test_zwapi_get_node_nls(void)
64+
{
65+
zwave_node_id_t node_id = 2;
66+
uint8_t nls_enabled = 1;
67+
uint8_t response_buffer[] = {0x04 /* length = len(payload) + 3 */,
68+
0x01 /* type: response */,
69+
FUNC_ID_ZW_GET_NODE_NLS_STATE /* cmd */,
70+
nls_enabled /* payload */};
71+
uint8_t response_length = 4;
72+
uint8_t payload_buffer[] = {0x02};
73+
uint8_t payload_buffer_length = 1;
74+
uint8_t node_nls_state = 99;
75+
76+
zwapi_session_send_frame_with_response_ExpectAndReturn(
77+
FUNC_ID_ZW_GET_NODE_NLS_STATE,
78+
payload_buffer,
79+
payload_buffer_length,
80+
response_buffer,
81+
&response_length,
82+
SL_STATUS_OK);
83+
zwapi_session_send_frame_with_response_IgnoreArg_response_buf();
84+
zwapi_session_send_frame_with_response_IgnoreArg_response_len();
85+
zwapi_session_send_frame_with_response_ReturnMemThruPtr_response_buf(
86+
response_buffer,
87+
response_length);
88+
zwapi_session_send_frame_with_response_ReturnThruPtr_response_len(
89+
&response_length);
90+
91+
TEST_ASSERT_EQUAL(SL_STATUS_OK, zwapi_get_node_nls(node_id, &node_nls_state));
92+
TEST_ASSERT_EQUAL(nls_enabled, node_nls_state);
93+
}
94+
95+
void test_zwapi_transfer_protocol_cc(void)
96+
{
97+
zwave_node_id_t node_id = 2;
98+
uint8_t decryption_key = 3;
99+
uint8_t tpcc_payload[] = {0xAA, 0xBB};
100+
uint8_t tpcc_payload_length = 2;
101+
uint8_t response_buffer[] = {0x04 /* length = len(payload) + 3 */,
102+
0x01 /* type: response */,
103+
FUNC_ID_ZW_TRANSFER_PROTOCOL_CC /* cmd */,
104+
0x01 /* payload */};
105+
uint8_t response_length = 4;
106+
uint8_t payload_buffer[] = {0x02, 0x03, 0x02, 0xAA, 0xBB};
107+
uint8_t payload_buffer_length = 5;
108+
109+
zwapi_session_send_frame_with_response_ExpectAndReturn(
110+
FUNC_ID_ZW_TRANSFER_PROTOCOL_CC,
111+
payload_buffer,
112+
payload_buffer_length,
113+
response_buffer,
114+
&response_length,
115+
SL_STATUS_OK);
116+
zwapi_session_send_frame_with_response_IgnoreArg_response_buf();
117+
zwapi_session_send_frame_with_response_IgnoreArg_response_len();
118+
zwapi_session_send_frame_with_response_ReturnMemThruPtr_response_buf(
119+
response_buffer,
120+
response_length);
121+
zwapi_session_send_frame_with_response_ReturnThruPtr_response_len(
122+
&response_length);
123+
124+
TEST_ASSERT_EQUAL(SL_STATUS_OK, zwapi_transfer_protocol_cc(node_id, decryption_key, tpcc_payload_length, tpcc_payload));
125+
}
126+
127+
void test_zwapi_request_protocol_cc_encryption_callback(void)
128+
{
129+
zwave_node_id_t node_id = 2;
130+
zwapi_tx_report_t tx_report = {0};
131+
uint8_t session_id = 1;
132+
133+
TEST_ASSERT_EQUAL(SL_STATUS_OK, zwapi_request_protocol_cc_encryption_callback(node_id, &tx_report, session_id));
134+
}

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

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
#include <string.h>
1919

2020
// List of static test variables
21-
static int zwapi_send_data_test_callback_call_count = 0;
22-
static int zwapi_application_controller_update_test_function_call_count = 0;
23-
static int zwapi_zwave_api_started_test_function_call_count = 0;
24-
static int zwapi_application_command_handler_test_function_call_count = 0;
21+
static int zwapi_send_data_test_callback_call_count = 0;
22+
static int zwapi_application_controller_update_test_function_call_count = 0;
23+
static int zwapi_zwave_api_started_test_function_call_count = 0;
24+
static int zwapi_application_command_handler_test_function_call_count = 0;
25+
static int zwapi_protocol_cc_encryption_request_test_function_call_count = 0;
2526

2627
// book-keeping variables
2728
static uint8_t received_status = 0;
@@ -66,6 +67,18 @@ void zwapi_application_command_handler_test_function(
6667
received_node_id = source_node_id;
6768
}
6869

70+
void zwapi_protocol_cc_encryption_request_test_function(
71+
const zwave_node_id_t destination_node_id,
72+
const uint8_t payload_length,
73+
const uint8_t *const payload,
74+
const uint8_t protocol_metadata_length,
75+
const uint8_t *const protocol_metadata,
76+
const uint8_t use_supervision,
77+
const uint8_t session_id)
78+
{
79+
zwapi_protocol_cc_encryption_request_test_function_call_count += 1;
80+
}
81+
6982
void zwapi_zwave_api_started_test_function(const uint8_t *buffer,
7083
uint8_t buffer_length)
7184
{
@@ -137,6 +150,35 @@ void test_zwapi_protocol_rx_dispatch_test_send_data_callback_tx_report_happy()
137150
received_tx_report.destination_ack_mpdu_measured_noise_floor);
138151
}
139152

153+
void test_zwapi_protocol_rx_dispatch_test_send_protocol_data_callback_tx_report_happy()
154+
{
155+
// Manually add our callback:
156+
zwave_api_get_callbacks_IgnoreAndReturn(&test_zwapi_callbacks);
157+
zwapi_send_data_callback = &zwapi_send_data_test_callback;
158+
uint8_t frame[] = {0x1D, 0x00, 0xAC, 0x00, 0x00, 0x12, 0x23, 0x05, 0xCF, 0x7D,
159+
0x7F, 0x6F, 0x7F, 0x01, 0x01, 0x03, 0x00, 0x00, 0x00, 0x00,
160+
0x02, 0x01, 0x00, 0x00, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE};
161+
zwave_api_protocol_rx_dispatch(frame, sizeof(frame));
162+
163+
TEST_ASSERT_EQUAL(1, zwapi_send_data_test_callback_call_count);
164+
TEST_ASSERT_EQUAL(TRANSMIT_COMPLETE_OK, received_status);
165+
TEST_ASSERT_EQUAL(0x1223, received_tx_report.transmit_ticks);
166+
TEST_ASSERT_EQUAL(0x05, received_tx_report.number_of_repeaters);
167+
TEST_ASSERT_EQUAL((int8_t)0xCF, received_tx_report.ack_rssi);
168+
TEST_ASSERT_EQUAL(0x7D, received_tx_report.rssi_values.incoming[0]);
169+
TEST_ASSERT_EQUAL(0x7F, received_tx_report.rssi_values.incoming[1]);
170+
TEST_ASSERT_EQUAL(0x6F, received_tx_report.rssi_values.incoming[2]);
171+
TEST_ASSERT_EQUAL((int8_t)0xAA, received_tx_report.tx_power);
172+
TEST_ASSERT_EQUAL((int8_t)0xBB, received_tx_report.measured_noise_floor);
173+
TEST_ASSERT_EQUAL((int8_t)0xCC,
174+
received_tx_report.destination_ack_mpdu_tx_power);
175+
TEST_ASSERT_EQUAL((int8_t)0xDD,
176+
received_tx_report.destination_ack_mpdu_measured_rssi);
177+
TEST_ASSERT_EQUAL(
178+
(int8_t)0xEE,
179+
received_tx_report.destination_ack_mpdu_measured_noise_floor);
180+
}
181+
140182
void test_zwapi_protocol_rx_dispatch_test_send_data_callback_tx_report_no_long_range_data()
141183
{
142184
// Manually add our callback:
@@ -465,3 +507,18 @@ void test_zwapi_protocol_rx_dispatch_zwave_api_started_overflow()
465507

466508
TEST_ASSERT_EQUAL(1, zwapi_zwave_api_started_test_function_call_count);
467509
}
510+
511+
void test_zwapi_protocol_rx_dispatch_protocol_cc_encryption_request()
512+
{
513+
// Manually add our callback:
514+
test_zwapi_callbacks.protocol_cc_encryption_request
515+
= &zwapi_protocol_cc_encryption_request_test_function;
516+
zwave_api_get_callbacks_IgnoreAndReturn(&test_zwapi_callbacks);
517+
518+
uint8_t frame[]
519+
= {0x0C, 0x00, 0x6C, 0x00, 0x01, 0x02, 0x03, 0x02, 0xAA, 0xCC, 0x01, 0x01};
520+
zwave_api_protocol_rx_dispatch(frame, sizeof(frame));
521+
522+
TEST_ASSERT_EQUAL(1,
523+
zwapi_protocol_cc_encryption_request_test_function_call_count);
524+
}

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,57 @@ void test_zwapi_send_data_happy_case()
219219
callback_function_test));
220220
}
221221

222+
void test_zwapi_send_protocol_data_happy_case()
223+
{
224+
zwapi_get_library_type_IgnoreAndReturn(ZWAVE_LIBRARY_TYPE_CONTROLLER_STATIC);
225+
226+
zwave_node_id_t destination_node_id = 5;
227+
protocol_metadata_t metadata = {0};
228+
metadata.session_id = 3;
229+
metadata.data_length = 2;
230+
metadata.data[0] = 0x01;
231+
metadata.data[1] = 0x02;
232+
const uint8_t data[] = {1, 2, 3, 5};
233+
uint8_t payload_buffer[20] = {0};
234+
payload_buffer[0] = destination_node_id; // node_id
235+
payload_buffer[1] = 4; // sizeof(data)
236+
payload_buffer[2] = 1; // data[0]
237+
payload_buffer[3] = 2; // data[1]
238+
payload_buffer[4] = 3; // data[2]
239+
payload_buffer[5] = 5; // data[3]
240+
payload_buffer[6] = 2; // metadata.data_length
241+
payload_buffer[7] = 0x01; // metadata.data[0]
242+
payload_buffer[8] = 0x02; // metadata.data[1]
243+
payload_buffer[9] = 3; // session_id
244+
response_length = 3 + IDX_DATA;
245+
response_buffer[3] = ZW_COMMAND_RETURN_VALUE_TRUE;
246+
zwapi_session_send_frame_with_response_ExpectWithArrayAndReturn(
247+
FUNC_ID_ZW_SEND_PROTOCOL_DATA,
248+
payload_buffer,
249+
sizeof(payload_buffer),
250+
10,
251+
response_buffer,
252+
sizeof(response_buffer),
253+
&response_length,
254+
sizeof(response_length),
255+
SL_STATUS_OK);
256+
zwapi_session_send_frame_with_response_IgnoreArg_response_buf();
257+
zwapi_session_send_frame_with_response_IgnoreArg_response_len();
258+
259+
zwapi_session_send_frame_with_response_ReturnMemThruPtr_response_buf(
260+
response_buffer,
261+
response_length);
262+
zwapi_session_send_frame_with_response_ReturnThruPtr_response_len(
263+
&response_length);
264+
265+
TEST_ASSERT_EQUAL(SL_STATUS_OK,
266+
zwapi_send_protocol_data(destination_node_id,
267+
data,
268+
sizeof(data),
269+
(void *)&metadata,
270+
callback_function_test));
271+
}
272+
222273
void test_zwapi_send_data_too_long_frame()
223274
{
224275
zwapi_get_library_type_IgnoreAndReturn(ZWAVE_LIBRARY_TYPE_CONTROLLER_STATIC);

0 commit comments

Comments
 (0)