Skip to content

Commit 6550e32

Browse files
Damian-Nordicnordicjm
authored andcommitted
net: openthread: rpc: unit tests for ot_rpc_diag.c...
...and ot_rpc_netdata.c 1. Implement missing unit tests for OT API serialization implemented in ot_rpc_diag.c and ot_rpc_netdata.c 2. Fix discovered bugs. Signed-off-by: Damian Krolik <[email protected]>
1 parent 4ab6eb8 commit 6550e32

File tree

18 files changed

+530
-11
lines changed

18 files changed

+530
-11
lines changed

subsys/net/openthread/rpc/common/ot_rpc_types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ enum ot_rpc_netif_address_offsets {
1717
OT_RPC_NETIF_ADDRESS_VALID_OFFSET = 1,
1818
OT_RPC_NETIF_ADDRESS_SCOPE_VALID_OFFSET = 2,
1919
OT_RPC_NETIF_ADDRESS_SCOPE_OFFSET = 3,
20-
OT_RPC_NETIF_ADDRESS_SCOPE_MASK = 7 << OT_RPC_NETIF_ADDRESS_SCOPE_OFFSET,
20+
OT_RPC_NETIF_ADDRESS_SCOPE_MASK = 0xf << OT_RPC_NETIF_ADDRESS_SCOPE_OFFSET,
2121
OT_RPC_NETIF_ADDRESS_RLOC_OFFSET = 7,
2222
OT_RPC_NETIF_ADDRESS_MESH_LOCAL_OFFSET = 8,
2323
};

subsys/net/openthread/rpc/server/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ zephyr_library()
99
zephyr_library_sources(
1010
ot_rpc_cli.c
1111
ot_rpc_coap.c
12+
ot_rpc_diag.c
1213
ot_rpc_dataset.c
1314
ot_rpc_instance.c
1415
ot_rpc_ip6.c
@@ -19,7 +20,6 @@ zephyr_library_sources(
1920
)
2021

2122
zephyr_library_sources_ifdef(CONFIG_NET_L2_OPENTHREAD
22-
ot_rpc_diag.c
2323
ot_rpc_udp.c
2424
)
2525

subsys/net/openthread/rpc/server/ot_rpc_diag.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static bool serialize_mesh_local_prefix(zcbor_state_t *state, const otMeshLocalP
8484
static void func_name(const struct nrf_rpc_group *group, struct nrf_rpc_cbor_ctx *ctx, \
8585
void *handler_data) \
8686
{ \
87-
size_t length = 1; \
87+
size_t length; \
8888
const type *data; \
8989
struct nrf_rpc_cbor_ctx rsp_ctx; \
9090
\
@@ -94,7 +94,7 @@ static bool serialize_mesh_local_prefix(zcbor_state_t *state, const otMeshLocalP
9494
data = func_to_call(ptr_call); \
9595
openthread_api_mutex_unlock(openthread_get_default_context()); \
9696
\
97-
length += ret_size(data); \
97+
length = 2 + ret_size(data); \
9898
\
9999
NRF_RPC_CBOR_ALLOC(group, rsp_ctx, length); \
100100
serialize_bstr(rsp_ctx.zs, data); \

subsys/net/openthread/rpc/server/ot_rpc_ip6.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static void ot_rpc_cmd_ip6_get_unicast_addrs(const struct nrf_rpc_group *group,
4040
((uint16_t)addr->mValid << OT_RPC_NETIF_ADDRESS_VALID_OFFSET) |
4141
((uint16_t)addr->mScopeOverrideValid
4242
<< OT_RPC_NETIF_ADDRESS_SCOPE_VALID_OFFSET) |
43-
((uint16_t)addr->mScopeOverride << OT_RPC_NETIF_ADDRESS_VALID_OFFSET) |
43+
((uint16_t)addr->mScopeOverride << OT_RPC_NETIF_ADDRESS_SCOPE_OFFSET) |
4444
((uint16_t)addr->mRloc << OT_RPC_NETIF_ADDRESS_RLOC_OFFSET) |
4545
((uint16_t)addr->mMeshLocal << OT_RPC_NETIF_ADDRESS_MESH_LOCAL_OFFSET);
4646

tests/mocks/nrf_rpc/mock_nrf_rpc_transport.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ typedef struct mock_nrf_rpc_tr_ctx {
3131

3232
static void log_payload(const char *caption, const uint8_t *payload, size_t length)
3333
{
34-
char payload_str[32];
34+
char payload_str[128];
3535

3636
if (!bin2hex(payload, length, payload_str, sizeof(payload_str))) {
3737
/*

tests/subsys/net/openthread/rpc/client/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CONFIG_TEST_RANDOM_GENERATOR=y
1111
CONFIG_OPENTHREAD_RPC=y
1212
CONFIG_OPENTHREAD_RPC_CLIENT=y
1313
CONFIG_NETWORKING=y
14+
CONFIG_NRF_RPC_ZCBOR_BACKUPS=1
1415
CONFIG_NRF_RPC_CBKPROXY_OUT_SLOTS=0
1516

1617
CONFIG_MOCK_NRF_RPC=y

tests/subsys/net/openthread/rpc/client/src/instance_suite.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,18 @@ ZTEST(ot_rpc_instance, test_otInstanceErasePersistentInfo_error)
152152
zassert_equal(error, OT_ERROR_INVALID_STATE);
153153
}
154154

155+
/* Test serialization of otGetVersionString() */
156+
ZTEST(ot_rpc_instance, test_otGetVersionString)
157+
{
158+
const char exp_version[] = {VERSION_STR, '\0'};
159+
const char *version;
160+
161+
mock_nrf_rpc_tr_expect_add(RPC_CMD(OT_RPC_CMD_GET_VERSION_STRING),
162+
RPC_RSP(0x58, 64, VERSION_STR));
163+
version = otGetVersionString();
164+
mock_nrf_rpc_tr_expect_done();
165+
166+
zassert_str_equal(version, exp_version);
167+
}
168+
155169
ZTEST_SUITE(ot_rpc_instance, NULL, NULL, tc_setup, NULL, NULL);

tests/subsys/net/openthread/rpc/client/src/ip6_suite.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <mock_nrf_rpc_transport.h>
88
#include <ot_rpc_ids.h>
9+
#include <ot_rpc_types.h>
910
#include <test_rpc_env.h>
1011

1112
#include <zephyr/ztest.h>
@@ -132,4 +133,77 @@ ZTEST(ot_rpc_ip6, test_otIp6UnsubscribeMulticastAddress_failed)
132133
zassert_equal(error, OT_ERROR_FAILED);
133134
}
134135

136+
/* Test serialization of otIp6GetUnicastAddresses() */
137+
ZTEST(ot_rpc_ip6, test_otIp6GetUnicastAddresses)
138+
{
139+
const otIp6Address exp_addr1 = {.mFields.m8 = {ADDR_1}};
140+
const otIp6Address exp_addr2 = {.mFields.m8 = {ADDR_2}};
141+
const uint16_t exp_flags1 = BIT(OT_RPC_NETIF_ADDRESS_PREFERRED_OFFSET) |
142+
BIT(OT_RPC_NETIF_ADDRESS_VALID_OFFSET) |
143+
BIT(OT_RPC_NETIF_ADDRESS_RLOC_OFFSET) |
144+
BIT(OT_RPC_NETIF_ADDRESS_MESH_LOCAL_OFFSET);
145+
const uint16_t exp_flags2 = BIT(OT_RPC_NETIF_ADDRESS_SCOPE_VALID_OFFSET) |
146+
(ADDR_SCOPE << OT_RPC_NETIF_ADDRESS_SCOPE_OFFSET);
147+
148+
const otNetifAddress *addr;
149+
150+
mock_nrf_rpc_tr_expect_add(RPC_CMD(OT_RPC_CMD_IP6_GET_UNICAST_ADDRESSES),
151+
RPC_RSP(0x9f, 0x50, ADDR_1, CBOR_UINT8(ADDR_PREFIX_LENGTH),
152+
OT_ADDRESS_ORIGIN_MANUAL, CBOR_UINT16(exp_flags1), 0xff,
153+
0x9f, 0x50, ADDR_2, CBOR_UINT8(ADDR_PREFIX_LENGTH),
154+
OT_ADDRESS_ORIGIN_DHCPV6, CBOR_UINT16(exp_flags2),
155+
0xff));
156+
addr = otIp6GetUnicastAddresses(NULL);
157+
mock_nrf_rpc_tr_expect_done();
158+
159+
zassert_not_null(addr);
160+
zassert_mem_equal(&addr->mAddress, &exp_addr1, OT_IP6_ADDRESS_SIZE);
161+
zassert_equal(addr->mPrefixLength, ADDR_PREFIX_LENGTH);
162+
zassert_equal(addr->mAddressOrigin, OT_ADDRESS_ORIGIN_MANUAL);
163+
zassert_true(addr->mPreferred);
164+
zassert_true(addr->mValid);
165+
zassert_false(addr->mScopeOverrideValid);
166+
zassert_true(addr->mRloc);
167+
zassert_true(addr->mMeshLocal);
168+
addr = addr->mNext;
169+
170+
zassert_not_null(addr);
171+
zassert_mem_equal(&addr->mAddress, &exp_addr2, OT_IP6_ADDRESS_SIZE);
172+
zassert_equal(addr->mPrefixLength, ADDR_PREFIX_LENGTH);
173+
zassert_equal(addr->mAddressOrigin, OT_ADDRESS_ORIGIN_DHCPV6);
174+
zassert_false(addr->mPreferred);
175+
zassert_false(addr->mValid);
176+
zassert_true(addr->mScopeOverrideValid);
177+
zassert_equal(addr->mScopeOverride, ADDR_SCOPE);
178+
zassert_false(addr->mRloc);
179+
zassert_false(addr->mMeshLocal);
180+
addr = addr->mNext;
181+
182+
zassert_is_null(addr);
183+
}
184+
185+
/* Test serialization of otIp6GetMulticastAddresses() */
186+
ZTEST(ot_rpc_ip6, test_otIp6GetMulticastAddresses)
187+
{
188+
const otIp6Address exp_addr1 = {.mFields.m8 = {MADDR_FF02_1}};
189+
const otIp6Address exp_addr2 = {.mFields.m8 = {MADDR_FF02_2}};
190+
191+
const otNetifMulticastAddress *maddr;
192+
193+
mock_nrf_rpc_tr_expect_add(RPC_CMD(OT_RPC_CMD_IP6_GET_MULTICAST_ADDRESSES),
194+
RPC_RSP(0x50, MADDR_FF02_1, 0x50, MADDR_FF02_2));
195+
maddr = otIp6GetMulticastAddresses(NULL);
196+
mock_nrf_rpc_tr_expect_done();
197+
198+
zassert_not_null(maddr);
199+
zassert_mem_equal(&maddr->mAddress, &exp_addr1, OT_IP6_ADDRESS_SIZE);
200+
maddr = maddr->mNext;
201+
202+
zassert_not_null(maddr);
203+
zassert_mem_equal(&maddr->mAddress, &exp_addr2, OT_IP6_ADDRESS_SIZE);
204+
maddr = maddr->mNext;
205+
206+
zassert_is_null(maddr);
207+
}
208+
135209
ZTEST_SUITE(ot_rpc_ip6, NULL, NULL, tc_setup, NULL, NULL);

tests/subsys/net/openthread/rpc/client/src/link_suite.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,44 @@ ZTEST(ot_rpc_link, test_otLinkGetFactoryAssignedIeeeEui64)
116116
zassert_mem_equal(expected_ext_addr.m8, ext_addr.m8, OT_EXT_ADDRESS_SIZE);
117117
}
118118

119+
/* Test serialization of otLinkGetChannel() */
120+
ZTEST(ot_rpc_link, test_otLinkGetChannel)
121+
{
122+
uint8_t channel;
123+
124+
mock_nrf_rpc_tr_expect_add(RPC_CMD(OT_RPC_CMD_LINK_GET_CHANNEL),
125+
RPC_RSP(CBOR_UINT8(UINT8_MAX)));
126+
channel = otLinkGetChannel(NULL);
127+
mock_nrf_rpc_tr_expect_done();
128+
129+
zassert_equal(channel, UINT8_MAX);
130+
}
131+
132+
/* Test serialization of otLinkGetExtendedAddress() */
133+
ZTEST(ot_rpc_link, test_otLinkGetExtendedAddress)
134+
{
135+
const otExtAddress exp_extaddr = {{INT_SEQUENCE(OT_EXT_ADDRESS_SIZE)}};
136+
const otExtAddress *extaddr;
137+
138+
mock_nrf_rpc_tr_expect_add(RPC_CMD(OT_RPC_CMD_LINK_GET_EXTENDED_ADDRESS),
139+
RPC_RSP(EXT_ADDR));
140+
extaddr = otLinkGetExtendedAddress(NULL);
141+
mock_nrf_rpc_tr_expect_done();
142+
143+
zassert_mem_equal(extaddr, &exp_extaddr, OT_EXT_ADDRESS_SIZE);
144+
}
145+
146+
/* Test serialization of otLinkGetPanId() */
147+
ZTEST(ot_rpc_link, test_otLinkGetPanId)
148+
{
149+
otPanId panid;
150+
151+
mock_nrf_rpc_tr_expect_add(RPC_CMD(OT_RPC_CMD_LINK_GET_PAN_ID),
152+
RPC_RSP(CBOR_UINT16(UINT16_MAX)));
153+
panid = otLinkGetPanId(NULL);
154+
mock_nrf_rpc_tr_expect_done();
155+
156+
zassert_equal(panid, UINT16_MAX);
157+
}
158+
119159
ZTEST_SUITE(ot_rpc_link, NULL, NULL, tc_setup, NULL, NULL);

tests/subsys/net/openthread/rpc/client/src/netdata_suite.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,30 @@ ZTEST(ot_rpc_netdata, test_otNetDataGetNextOnMeshPrefix)
123123
zassert_true(br.mDp);
124124
}
125125

126+
/* Test serialization of otNetDataGetStableVersion() */
127+
ZTEST(ot_rpc_netdata, test_otNetDataGetStableVersion)
128+
{
129+
uint8_t version;
130+
131+
mock_nrf_rpc_tr_expect_add(RPC_CMD(OT_RPC_CMD_NET_DATA_GET_STABLE_VERSION),
132+
RPC_RSP(CBOR_UINT8(UINT8_MAX)));
133+
version = otNetDataGetStableVersion(NULL);
134+
mock_nrf_rpc_tr_expect_done();
135+
136+
zassert_equal(version, UINT8_MAX);
137+
}
138+
139+
/* Test serialization of otNetDataGetVersion() */
140+
ZTEST(ot_rpc_netdata, test_otNetDataGetVersion)
141+
{
142+
uint8_t version;
143+
144+
mock_nrf_rpc_tr_expect_add(RPC_CMD(OT_RPC_CMD_NET_DATA_GET_VERSION),
145+
RPC_RSP(CBOR_UINT8(UINT8_MAX)));
146+
version = otNetDataGetVersion(NULL);
147+
mock_nrf_rpc_tr_expect_done();
148+
149+
zassert_equal(version, UINT8_MAX);
150+
}
151+
126152
ZTEST_SUITE(ot_rpc_netdata, NULL, NULL, tc_setup, NULL, NULL);

0 commit comments

Comments
 (0)