Skip to content

Commit 66a4fab

Browse files
Jarno Lämsäfabiobaltieri
authored andcommitted
tests: lwm2m_registry: Add ZTest tests for lwm2m_registry
These tests test the basic functionality of creating object and resource instances, setting buffers, writing and reading to and from resources, and setting and triggering callbacks. Signed-off-by: Jarno Lämsä <[email protected]>
1 parent 8512bd1 commit 66a4fab

File tree

4 files changed

+343
-0
lines changed

4 files changed

+343
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#
2+
# Copyright (c) 2022 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
cmake_minimum_required(VERSION 3.20.0)
8+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
9+
project(lwm2m_registry)
10+
11+
FILE(GLOB app_sources src/*.c)
12+
target_sources(app
13+
PRIVATE
14+
${app_sources}
15+
)
16+
17+
set(includes
18+
"$ENV{ZEPHYR_BASE}/subsys/net/lib/lwm2m/"
19+
"src/"
20+
)
21+
22+
target_include_directories(app
23+
PRIVATE
24+
${includes}
25+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
CONFIG_NETWORKING=y
2+
CONFIG_NET_TEST=y
3+
CONFIG_ZTEST=y
4+
CONFIG_ZTEST_NEW_API=y
5+
6+
CONFIG_ENTROPY_GENERATOR=y
7+
CONFIG_TEST_RANDOM_GENERATOR=y
8+
CONFIG_NEWLIB_LIBC=y
9+
10+
CONFIG_LWM2M=y
11+
CONFIG_LWM2M_COAP_MAX_MSG_SIZE=512
12+
CONFIG_LWM2M_IPSO_SUPPORT=y
13+
CONFIG_LWM2M_IPSO_TEMP_SENSOR=y
14+
CONFIG_LWM2M_IPSO_TEMP_SENSOR_VERSION_1_1=y
15+
CONFIG_LWM2M_IPSO_TEMP_SENSOR_INSTANCE_COUNT=1
16+
CONFIG_LWM2M_CONN_MON_OBJ_SUPPORT=y
17+
CONFIG_LWM2M_CONNMON_OBJECT_VERSION_1_2=y
Lines changed: 296 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,296 @@
1+
/*
2+
* Copyright (c) 2023 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/ztest.h>
8+
9+
#include "lwm2m_engine.h"
10+
#include "lwm2m_util.h"
11+
12+
static uint32_t callback_checker;
13+
static char pre_write_cb_buf[10];
14+
15+
static void *pre_write_cb(uint16_t obj_inst_id,
16+
uint16_t res_id,
17+
uint16_t res_inst_id,
18+
size_t *data_len)
19+
{
20+
callback_checker |= 0x01;
21+
return pre_write_cb_buf;
22+
}
23+
24+
static int post_write_cb(uint16_t obj_inst_id,
25+
uint16_t res_id, uint16_t res_inst_id,
26+
uint8_t *data, uint16_t data_len,
27+
bool last_block, size_t total_size)
28+
{
29+
callback_checker |= 0x02;
30+
return 0;
31+
}
32+
33+
static void *read_cb(uint16_t obj_inst_id,
34+
uint16_t res_id,
35+
uint16_t res_inst_id,
36+
size_t *data_len)
37+
{
38+
callback_checker |= 0x04;
39+
return 0;
40+
}
41+
42+
static int validate_cb(uint16_t obj_inst_id,
43+
uint16_t res_id, uint16_t res_inst_id,
44+
uint8_t *data, uint16_t data_len,
45+
bool last_block, size_t total_size)
46+
{
47+
callback_checker |= 0x08;
48+
return 0;
49+
}
50+
51+
static int obj_create_cb(uint16_t obj_inst_id)
52+
{
53+
callback_checker |= 0x10;
54+
return 0;
55+
}
56+
57+
static int obj_delete_cb(uint16_t obj_inst_id)
58+
{
59+
callback_checker |= 0x20;
60+
return 0;
61+
}
62+
63+
static int exec_cb(uint16_t obj_inst_id, uint8_t *args, uint16_t args_len)
64+
{
65+
callback_checker |= 0x40;
66+
return 0;
67+
}
68+
69+
ZTEST_SUITE(lwm2m_registry, NULL, NULL, NULL, NULL, NULL);
70+
71+
ZTEST(lwm2m_registry, test_object_creation_and_deletion)
72+
{
73+
int ret;
74+
75+
ret = lwm2m_create_object_inst(&LWM2M_OBJ(3303, 0));
76+
zassert_equal(ret, 0);
77+
78+
ret = lwm2m_delete_object_inst(&LWM2M_OBJ(3303, 0));
79+
zassert_equal(ret, 0);
80+
}
81+
82+
ZTEST(lwm2m_registry, test_create_unknown_object)
83+
{
84+
int ret;
85+
86+
ret = lwm2m_create_object_inst(&LWM2M_OBJ(49999, 0));
87+
zassert_equal(ret, -ENOENT);
88+
}
89+
90+
ZTEST(lwm2m_registry, test_resource_buf)
91+
{
92+
int ret;
93+
uint8_t resource_buf = 0;
94+
95+
ret = lwm2m_create_object_inst(&LWM2M_OBJ(3303, 0));
96+
zassert_equal(ret, 0);
97+
98+
ret = lwm2m_set_res_buf(&LWM2M_OBJ(3303, 0, 6042), &resource_buf, sizeof(resource_buf),
99+
sizeof(resource_buf), 0);
100+
zassert_equal(ret, 0);
101+
102+
ret = lwm2m_set_u8(&LWM2M_OBJ(3303, 0, 6042), 0x5A);
103+
zassert_equal(ret, 0);
104+
105+
zassert_equal(resource_buf, 0x5A);
106+
107+
ret = lwm2m_delete_object_inst(&LWM2M_OBJ(3303, 0));
108+
zassert_equal(ret, 0);
109+
}
110+
111+
ZTEST(lwm2m_registry, test_unknown_res)
112+
{
113+
int ret;
114+
uint8_t resource_buf = 0;
115+
116+
ret = lwm2m_create_object_inst(&LWM2M_OBJ(3303, 0));
117+
zassert_equal(ret, 0);
118+
119+
ret = lwm2m_set_res_buf(&LWM2M_OBJ(3303, 0, 49999), &resource_buf, sizeof(resource_buf),
120+
sizeof(resource_buf), 0);
121+
zassert_equal(ret, -ENOENT);
122+
123+
ret = lwm2m_delete_object_inst(&LWM2M_OBJ(3303, 0));
124+
zassert_equal(ret, 0);
125+
}
126+
127+
/* Different objects employ different resources, test some of those*/
128+
ZTEST(lwm2m_registry, test_connmon)
129+
{
130+
int ret;
131+
uint16_t u16_buf = 0;
132+
uint32_t u32_buf = 0;
133+
int8_t s8_buf = 0;
134+
int32_t s32_buf = 0;
135+
136+
uint16_t u16_getbuf = 0;
137+
uint32_t u32_getbuf = 0;
138+
int8_t s8_getbuf = 0;
139+
int32_t s32_getbuf = 0;
140+
141+
ret = lwm2m_set_res_buf(&LWM2M_OBJ(4, 0, 9), &u16_buf, sizeof(u16_buf),
142+
sizeof(u16_buf), 0);
143+
zassert_equal(ret, 0);
144+
ret = lwm2m_set_res_buf(&LWM2M_OBJ(4, 0, 8), &u32_buf, sizeof(u32_buf),
145+
sizeof(u32_buf), 0);
146+
zassert_equal(ret, 0);
147+
ret = lwm2m_set_res_buf(&LWM2M_OBJ(4, 0, 2), &s8_buf, sizeof(s8_buf),
148+
sizeof(s8_buf), 0);
149+
zassert_equal(ret, 0);
150+
ret = lwm2m_set_res_buf(&LWM2M_OBJ(4, 0, 11), &s32_buf, sizeof(s32_buf),
151+
sizeof(s32_buf), 0);
152+
zassert_equal(ret, 0);
153+
154+
ret = lwm2m_set_u16(&LWM2M_OBJ(4, 0, 9), 0x5A5A);
155+
zassert_equal(ret, 0);
156+
ret = lwm2m_set_u32(&LWM2M_OBJ(4, 0, 8), 0xDEADBEEF);
157+
zassert_equal(ret, 0);
158+
ret = lwm2m_set_s8(&LWM2M_OBJ(4, 0, 2), -5);
159+
zassert_equal(ret, 0);
160+
ret = lwm2m_set_s32(&LWM2M_OBJ(4, 0, 11), 0xCC00CC00);
161+
zassert_equal(ret, 0);
162+
163+
zassert_equal(u16_buf, 0x5A5A);
164+
zassert_equal(u32_buf, 0xDEADBEEF);
165+
zassert_equal(s8_buf, -5);
166+
zassert_equal(s32_buf, 0xCC00CC00);
167+
168+
ret = lwm2m_get_u16(&LWM2M_OBJ(4, 0, 9), &u16_getbuf);
169+
zassert_equal(ret, 0);
170+
ret = lwm2m_get_u32(&LWM2M_OBJ(4, 0, 8), &u32_getbuf);
171+
zassert_equal(ret, 0);
172+
ret = lwm2m_get_s8(&LWM2M_OBJ(4, 0, 2), &s8_getbuf);
173+
zassert_equal(ret, 0);
174+
ret = lwm2m_get_s32(&LWM2M_OBJ(4, 0, 11), &s32_getbuf);
175+
zassert_equal(ret, 0);
176+
177+
zassert_equal(u16_buf, u16_getbuf);
178+
zassert_equal(u32_buf, u32_getbuf);
179+
zassert_equal(s8_buf, s8_getbuf);
180+
zassert_equal(s32_buf, s32_getbuf);
181+
}
182+
183+
ZTEST(lwm2m_registry, test_temp_sensor)
184+
{
185+
int ret;
186+
uint8_t u8_buf = 0;
187+
time_t time_buf = 0;
188+
double dbl_buf = 0;
189+
char char_buf[10];
190+
191+
uint8_t u8_getbuf = 0;
192+
time_t time_getbuf = 0;
193+
double dbl_getbuf = 0;
194+
char char_getbuf[10];
195+
196+
ret = lwm2m_create_object_inst(&LWM2M_OBJ(3303, 0));
197+
zassert_equal(ret, 0);
198+
199+
ret = lwm2m_set_res_buf(&LWM2M_OBJ(3303, 0, 6042), &u8_buf, sizeof(u8_buf),
200+
sizeof(u8_buf), 0);
201+
zassert_equal(ret, 0);
202+
ret = lwm2m_set_res_buf(&LWM2M_OBJ(3303, 0, 5518), &time_buf, sizeof(time_buf),
203+
sizeof(time_buf), 0);
204+
zassert_equal(ret, 0);
205+
ret = lwm2m_set_res_buf(&LWM2M_OBJ(3303, 0, 5601), &dbl_buf, sizeof(dbl_buf),
206+
sizeof(dbl_buf), 0);
207+
zassert_equal(ret, 0);
208+
ret = lwm2m_set_res_buf(&LWM2M_OBJ(3303, 0, 5701), &char_buf, sizeof(char_buf),
209+
sizeof(char_buf), 0);
210+
zassert_equal(ret, 0);
211+
212+
ret = lwm2m_set_u8(&LWM2M_OBJ(3303, 0, 6042), 0x5A);
213+
zassert_equal(ret, 0);
214+
ret = lwm2m_set_time(&LWM2M_OBJ(3303, 0, 5518), 1674118825);
215+
zassert_equal(ret, 0);
216+
ret = lwm2m_set_f64(&LWM2M_OBJ(3303, 0, 5601), 5.89);
217+
zassert_equal(ret, 0);
218+
ret = lwm2m_set_string(&LWM2M_OBJ(3303, 0, 5701), "test");
219+
zassert_equal(ret, 0);
220+
221+
zassert_equal(u8_buf, 0x5A);
222+
zassert_equal(time_buf, 1674118825);
223+
zassert_within(dbl_buf, 5.89, 0.01);
224+
zassert_equal(strncmp(char_buf, "test", 10), 0);
225+
226+
ret = lwm2m_get_u8(&LWM2M_OBJ(3303, 0, 6042), &u8_getbuf);
227+
zassert_equal(ret, 0);
228+
ret = lwm2m_get_time(&LWM2M_OBJ(3303, 0, 5518), &time_getbuf);
229+
zassert_equal(ret, 0);
230+
ret = lwm2m_get_f64(&LWM2M_OBJ(3303, 0, 5601), &dbl_getbuf);
231+
zassert_equal(ret, 0);
232+
ret = lwm2m_get_string(&LWM2M_OBJ(3303, 0, 5701), &char_getbuf, 10);
233+
zassert_equal(ret, 0);
234+
235+
zassert_equal(u8_buf, u8_getbuf);
236+
zassert_equal(time_buf, time_getbuf);
237+
zassert_within(dbl_buf, dbl_getbuf, 0.01);
238+
zassert_equal(strncmp(char_buf, char_getbuf, 10), 0);
239+
240+
ret = lwm2m_delete_object_inst(&LWM2M_OBJ(3303, 0));
241+
zassert_equal(ret, 0);
242+
}
243+
244+
ZTEST(lwm2m_registry, test_resource_instance_creation_and_deletion)
245+
{
246+
int ret;
247+
248+
ret = lwm2m_create_res_inst(&LWM2M_OBJ(4, 0, 1, 0));
249+
zassert_equal(ret, 0);
250+
251+
ret = lwm2m_delete_res_inst(&LWM2M_OBJ(4, 0, 1, 0));
252+
zassert_equal(ret, 0);
253+
}
254+
255+
ZTEST(lwm2m_registry, test_callbacks)
256+
{
257+
int ret;
258+
double sensor_val;
259+
struct lwm2m_engine_res *exec_res;
260+
261+
callback_checker = 0;
262+
ret = lwm2m_register_create_callback(3303, obj_create_cb);
263+
zassert_equal(ret, 0);
264+
lwm2m_register_delete_callback(3303, obj_delete_cb);
265+
zassert_equal(ret, 0);
266+
267+
ret = lwm2m_create_object_inst(&LWM2M_OBJ(3303, 0));
268+
zassert_equal(ret, 0);
269+
zassert_equal(callback_checker, 0x10);
270+
271+
ret = lwm2m_register_exec_callback(&LWM2M_OBJ(3303, 0, 5605), exec_cb);
272+
zassert_equal(ret, 0);
273+
ret = lwm2m_register_read_callback(&LWM2M_OBJ(3303, 0, 5700), read_cb);
274+
zassert_equal(ret, 0);
275+
ret = lwm2m_register_validate_callback(&LWM2M_OBJ(3303, 0, 5701), validate_cb);
276+
zassert_equal(ret, 0);
277+
ret = lwm2m_register_pre_write_callback(&LWM2M_OBJ(3303, 0, 5701), pre_write_cb);
278+
zassert_equal(ret, 0);
279+
ret = lwm2m_register_post_write_callback(&LWM2M_OBJ(3303, 0, 5701), post_write_cb);
280+
zassert_equal(ret, 0);
281+
282+
exec_res = lwm2m_engine_get_res(&LWM2M_OBJ(3303, 0, 5605));
283+
exec_res->execute_cb(0, 0, 0);
284+
285+
ret = lwm2m_set_string(&LWM2M_OBJ(3303, 0, 5701), "test");
286+
zassert_equal(ret, 0);
287+
zassert_equal(callback_checker, 0x5B);
288+
289+
ret = lwm2m_get_f64(&LWM2M_OBJ(3303, 0, 5700), &sensor_val);
290+
zassert_equal(ret, 0);
291+
zassert_equal(callback_checker, 0x5F);
292+
293+
ret = lwm2m_delete_object_inst(&LWM2M_OBJ(3303, 0));
294+
zassert_equal(ret, 0);
295+
zassert_equal(callback_checker, 0x7F);
296+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
common:
2+
depends_on: netif
3+
tests:
4+
subsys.net.lib.lwm2m.lwm2m_registry:
5+
tags: lwm2m net

0 commit comments

Comments
 (0)