Skip to content

Commit ab74c0b

Browse files
silabs-aydoganerzr
authored andcommitted
uic: zpc: Pull request #2929: UIC-3600: Add unit tests to zwave_transports module
Merge in UIC/uic from test/UIC-3600-zwave_api_transport-unit-test to develop (cherry picked from commit 58ce627a32f50a0bd78c9853d8d6f317fde07c0a) Forwarded: #11 Signed-off-by: Philippe Coval <philippe.coval@silabs.com>
1 parent 75d2c02 commit ab74c0b

File tree

4 files changed

+234
-0
lines changed

4 files changed

+234
-0
lines changed

applications/zpc/components/zwave/zwave_transports/s2/test/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ target_add_unittest(
6363
zwave_tx_mock
6464
zwave_network_management_mock)
6565

66+
# ####################### S2 Protocol CC Encryption test ##################################
67+
target_add_unittest(
68+
zwave_s2
69+
NAME
70+
zwave_s2_protocol_cc_encryption_test
71+
SOURCES
72+
zwave_s2_protocol_cc_encryption_test.c
73+
DEPENDS
74+
zwave_controller ## Non-mock, we use the real thing here
75+
libs2_mock
76+
zwave_api_mock
77+
uic_contiki_stub
78+
zwave_tx_mock
79+
zwave_network_management_mock)
80+
6681
# ####################### S2 Nonce Management test ##################################
6782
target_add_unittest(
6883
zwave_s2_nonce_management
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/******************************************************************************
2+
* # License
3+
* <b>Copyright 2021 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 <string.h>
14+
15+
#include "contiki_test_helper.h"
16+
17+
#include "zwave_s2_protocol_cc_encryption.h"
18+
19+
#include "zwave_controller_connection_info.h"
20+
21+
#include "zwave_tx_mock.h"
22+
23+
#include "sl_log.h"
24+
#include "sl_status.h"
25+
26+
#include "unity.h"
27+
28+
/// Setup the test suite (called once before all test_xxx functions are called)
29+
void suiteSetUp() {}
30+
31+
/// Teardown the test suite (called once after all test_xxx functions are called)
32+
int suiteTearDown(int num_failures)
33+
{
34+
return num_failures;
35+
}
36+
37+
void setUp()
38+
{
39+
contiki_test_helper_init();
40+
}
41+
42+
void tearDown() {}
43+
44+
void test_zwave_s2_on_protocol_cc_encryption_request_happy_case()
45+
{
46+
zwave_node_id_t destination_node_id = 2;
47+
uint8_t payload[] = {0x01, 0x02, 0x03};
48+
uint8_t payload_length = sizeof(payload);
49+
uint8_t protocol_metadata[] = {0xA1, 0xA2, 0xA3};
50+
uint8_t protocol_metadata_length = sizeof(protocol_metadata);
51+
uint8_t use_supervision = 1;
52+
uint8_t session_id = 1;
53+
zwave_controller_connection_info_t connection_info = {0};
54+
zwave_tx_options_t tx_options = {0};
55+
zwave_tx_session_id_t tx_session_id = NULL;
56+
protocol_metadata_t metadata = {0};
57+
58+
// Expected TX options
59+
tx_options.transport.is_protocol_frame = true;
60+
tx_options.number_of_responses = 1;
61+
tx_options.discard_timeout_ms = 5000;
62+
tx_options.qos_priority = ZWAVE_TX_QOS_MAX_PRIORITY;
63+
64+
// Expected connection info
65+
connection_info.encapsulation = ZWAVE_CONTROLLER_ENCAPSULATION_NONE;
66+
connection_info.local.node_id = 0;
67+
connection_info.remote.node_id = 2;
68+
69+
// Expected protocol metadata
70+
metadata.session_id = session_id;
71+
metadata.data_length = protocol_metadata_length;
72+
memcpy(metadata.data, protocol_metadata, protocol_metadata_length);
73+
74+
zwave_tx_send_data_ExpectWithArrayAndReturn(&connection_info,
75+
sizeof(connection_info),
76+
payload_length,
77+
payload,
78+
sizeof(payload),
79+
&tx_options,
80+
sizeof(tx_options),
81+
NULL,
82+
(void *)&metadata,
83+
sizeof(protocol_metadata_t),
84+
&tx_session_id,
85+
sizeof(zwave_tx_session_id_t),
86+
SL_STATUS_OK);
87+
zwave_tx_send_data_IgnoreArg_on_send_complete();
88+
89+
zwave_s2_on_protocol_cc_encryption_request(destination_node_id,
90+
payload_length,
91+
payload,
92+
protocol_metadata_length,
93+
protocol_metadata,
94+
use_supervision,
95+
session_id);
96+
}

applications/zpc/components/zwave/zwave_transports/s2/test/zwave_s2_transport_test.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ int suiteTearDown(int num_failures)
6060

6161
void setUp()
6262
{
63+
num_callbacks = 0;
6364
contiki_test_helper_init();
6465
}
6566

@@ -148,6 +149,56 @@ void test_zwave_s2_send_data()
148149
TEST_ASSERT_EQUAL(TRANSMIT_COMPLETE_VERIFIED, my_status);
149150
}
150151

152+
void test_zwave_s2_send_protocol_data()
153+
{
154+
zwave_controller_connection_info_t connection = {};
155+
156+
uint8_t cmd_data[] = "HelloWorld";
157+
zwave_tx_options_t tx_options = {};
158+
zwave_tx_session_id_t session;
159+
protocol_metadata_t metadata = {0};
160+
161+
connection.encapsulation = ZWAVE_CONTROLLER_ENCAPSULATION_SECURITY_2_ACCESS;
162+
connection.local.node_id = 1;
163+
connection.remote.node_id = 2;
164+
165+
tx_options.transport.is_protocol_frame = true;
166+
167+
s2_connection_t s2c = {};
168+
s2c.l_node = 1;
169+
s2c.r_node = 2;
170+
s2c.zw_tx_options = 0;
171+
s2c.class_id = 2;
172+
s2c.tx_options = S2_TXOPTION_VERIFY_DELIVERY;
173+
174+
S2_send_data_singlecast_with_keyset_ExpectWithArrayAndReturn(
175+
0,
176+
0,
177+
&s2c,
178+
sizeof(s2_connection_t),
179+
UNKNOWN_KEYSET,
180+
cmd_data,
181+
sizeof(cmd_data),
182+
sizeof(cmd_data),
183+
1);
184+
185+
zwave_tx_get_number_of_responses_ExpectAndReturn(&session, 0);
186+
TEST_ASSERT_EQUAL(SL_STATUS_OK,
187+
zwave_s2_send_data(&connection,
188+
sizeof(cmd_data),
189+
cmd_data,
190+
&tx_options,
191+
on_zwave_tx_send_data_complete,
192+
(void *)&metadata,
193+
&session));
194+
195+
//Check the callback
196+
S2_send_done_event(0, S2_TRANSMIT_COMPLETE_VERIFIED);
197+
TEST_ASSERT_EQUAL_PTR((void *)&metadata, my_user);
198+
TEST_ASSERT_EQUAL(1, num_callbacks);
199+
TEST_ASSERT_EQUAL(TRANSMIT_COMPLETE_VERIFIED, my_status);
200+
}
201+
151202
void test_zwave_s2_send_data_nonce_report_sos()
152203
{
153204
uint8_t nonce_report[]

applications/zpc/components/zwave/zwave_transports/zwave_api_transport/test/zwave_api_transport_test.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ static zwave_controller_transport_t zwave_api_transport;
3838
static zwave_api_started_callback_t on_zwave_api_started_callback;
3939

4040
static cmock_zwapi_protocol_transport_func_ptr2 zwave_api_send_data_callback;
41+
static cmock_zwapi_protocol_transport_func_ptr3
42+
zwave_api_send_protocol_data_callback;
4143
static cmock_zwapi_protocol_transport_func_ptr4
4244
zwave_api_send_data_multi_callback;
4345
static cmock_zwapi_protocol_basis_func_ptr2 zwave_api_send_test_frame_callback;
@@ -87,6 +89,18 @@ static sl_status_t zwapi_send_data_stub(
8789
return SL_STATUS_OK;
8890
}
8991

92+
static sl_status_t zwapi_send_protocol_data_stub(
93+
zwave_node_id_t destination_node_id,
94+
const uint8_t *data,
95+
uint8_t data_length,
96+
void *metadata,
97+
cmock_zwapi_protocol_transport_func_ptr3 callback_function,
98+
int cmock_num_calls)
99+
{
100+
zwave_api_send_protocol_data_callback = callback_function;
101+
return SL_STATUS_OK;
102+
}
103+
90104
static sl_status_t zwapi_send_data_multi_stub(
91105
zwave_nodemask_t destination_node_mask,
92106
const uint8_t *data,
@@ -672,6 +686,64 @@ void test_zwave_api_transport_send_test_frame_happy_case()
672686
TEST_ASSERT_EQUAL(TRANSMIT_COMPLETE_VERIFIED, received_status);
673687
}
674688

689+
void test_zwave_api_transport_send_protocol_frame_happy_case()
690+
{
691+
TEST_ASSERT_NOT_NULL(zwave_api_transport.send_data);
692+
693+
// Prepare data:
694+
info.remote.node_id = 23;
695+
tx_options.fasttrack = false;
696+
tx_options.transport.is_protocol_frame = true;
697+
const uint8_t frame[] = {0x56, 0x32, 0x9F, 0x22, 0x01, 0xFF};
698+
parent_session_id = (void *)53;
699+
protocol_metadata_t metadata = {0};
700+
701+
zwapi_send_protocol_data_AddCallback(zwapi_send_protocol_data_stub);
702+
zwapi_send_protocol_data_ExpectAndReturn(info.remote.node_id,
703+
frame,
704+
sizeof(frame),
705+
(void *)&metadata,
706+
NULL,
707+
SL_STATUS_OK);
708+
zwapi_send_protocol_data_IgnoreArg_callback_function();
709+
710+
TEST_ASSERT_EQUAL(SL_STATUS_OK,
711+
zwave_api_transport.send_data(&info,
712+
sizeof(frame),
713+
frame,
714+
&tx_options,
715+
&on_send_data_complete,
716+
(void *)&metadata,
717+
parent_session_id));
718+
719+
//Run the contiki events
720+
contiki_test_helper_run(0);
721+
722+
//Additional test: Node info event are to be ignored when sending data.
723+
TEST_ASSERT_NOT_NULL(zwave_controller_callbacks.on_node_information);
724+
TEST_ASSERT_NOT_NULL(zwave_controller_callbacks.on_node_info_req_failed);
725+
zwave_controller_callbacks.on_node_information(info.remote.node_id, NULL);
726+
zwave_controller_callbacks.on_node_info_req_failed(info.remote.node_id);
727+
728+
contiki_test_helper_run(0);
729+
730+
// Expect to notify of a frame transmission success
731+
zwapi_tx_report_t report = {};
732+
zwave_controller_on_frame_transmission_Expect(true,
733+
&report,
734+
info.remote.node_id);
735+
736+
// callback from the Z-Wave API
737+
TEST_ASSERT_NOT_NULL(zwave_api_send_protocol_data_callback);
738+
zwave_api_send_protocol_data_callback(TRANSMIT_COMPLETE_OK, &report);
739+
740+
TEST_ASSERT_EQUAL_PTR((void *)&metadata, received_user);
741+
TEST_ASSERT_EQUAL_MEMORY(&report,
742+
&received_tx_report,
743+
sizeof(zwapi_tx_report_t));
744+
TEST_ASSERT_EQUAL(TRANSMIT_COMPLETE_OK, received_status);
745+
}
746+
675747
void test_zwave_api_transport_send_test_frame_rejected()
676748
{
677749
TEST_ASSERT_NOT_NULL(zwave_api_transport.send_data);

0 commit comments

Comments
 (0)