Skip to content

Commit 1acd1cc

Browse files
author
Arto Kinnunen
committed
Add unit tests for monitor
1 parent 64c969e commit 1acd1cc

File tree

9 files changed

+393
-0
lines changed

9 files changed

+393
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
include ../../makefile_defines.txt
2+
3+
COMPONENT_NAME = monitor_unit
4+
5+
#This must be changed manually
6+
SRC_FILES = \
7+
../../../../../source/Core/ns_monitor.c
8+
9+
TEST_SRC_FILES = \
10+
main.cpp \
11+
monitortest.cpp \
12+
test_monitor.c \
13+
../../stub/nsdynmemLIB_stub.c \
14+
../../stub/mbed_trace_stub.c \
15+
../../stub/common_functions_stub.c \
16+
../../stub/multicast_api_stub.c \
17+
../../stub/ns_sha256_stub.c \
18+
../../stub/ns_list_stub.c \
19+
../../stub/thread_common_stub.c \
20+
../../stub/icmpv6_stub.c \
21+
../../stub/mld_stub.c \
22+
../../stub/ipv6_routing_table_stub.c \
23+
../../stub/protocol_core_stub.c \
24+
../../stub/ip6tos_stub.c \
25+
26+
include ../../MakefileWorker.mk
27+
28+
CPPUTESTFLAGS += -DFEA_TRACE_SUPPORT
29+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2019, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "CppUTest/CommandLineTestRunner.h"
19+
#include "CppUTest/TestPlugin.h"
20+
#include "CppUTest/TestRegistry.h"
21+
#include "CppUTestExt/MockSupportPlugin.h"
22+
23+
int main(int ac, char **av)
24+
{
25+
return CommandLineTestRunner::RunAllTests(ac, av);
26+
}
27+
28+
IMPORT_TEST_GROUP(monitor);
29+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2019, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
#include "CppUTest/TestHarness.h"
18+
#include "test_monitor.h"
19+
20+
TEST_GROUP(monitor)
21+
{
22+
void setup() {
23+
}
24+
25+
void teardown() {
26+
}
27+
};
28+
29+
TEST(monitor, test_ns_monitor_init)
30+
{
31+
CHECK(test_ns_monitor_init());
32+
}
33+
34+
TEST(monitor, test_ns_monitor_heap_gc_threshold_set)
35+
{
36+
CHECK(test_ns_monitor_heap_gc_threshold_set());
37+
}
38+
39+
TEST(monitor, test_ns_monitor_timer)
40+
{
41+
CHECK(test_ns_monitor_timer());
42+
}
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/*
2+
* Copyright (c) 2019, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "nsconfig.h"
19+
#include "test_monitor.h"
20+
#include <string.h>
21+
#include "NWK_INTERFACE/Include/protocol.h"
22+
#include "nsdynmemLIB_stub.h"
23+
#include "ipv6_routing_table_stub.h""
24+
25+
bool test_ns_monitor_init()
26+
{
27+
int ret_val;
28+
29+
// Mem allocation error
30+
nsdynmemlib_stub.returnCounter = 0;
31+
nsdynmemlib_stub.expectedPointer = NULL;
32+
ret_val = ns_monitor_init();
33+
if (ret_val == 0) {
34+
return false;
35+
}
36+
37+
// OK init
38+
nsdynmemlib_stub.returnCounter = 1;
39+
ret_val = ns_monitor_init();
40+
if (ret_val != 0) {
41+
return false;
42+
}
43+
44+
// re-init fails
45+
nsdynmemlib_stub.returnCounter = 1;
46+
ret_val = ns_monitor_init();
47+
if (ret_val == 0) {
48+
return false;
49+
}
50+
51+
// clear monitor
52+
ret_val = ns_monitor_clear();
53+
if (ret_val != 0) {
54+
return false;
55+
}
56+
57+
return true;
58+
}
59+
60+
bool test_ns_monitor_heap_gc_threshold_set()
61+
{
62+
int ret_val;
63+
64+
nsdynmemlib_mem_stats_stub.heap_sector_size = 100;
65+
nsdynmemlib_mem_stats_stub.heap_sector_allocated_bytes = 0;
66+
67+
// Init monitor
68+
nsdynmemlib_stub.returnCounter = 1;
69+
ret_val = ns_monitor_init();
70+
if (ret_val != 0) {
71+
return false;
72+
}
73+
74+
// high and critical are equal. NOK
75+
ret_val = ns_monitor_heap_gc_threshold_set(100, 100);
76+
if (ret_val == 0) {
77+
return false;
78+
}
79+
80+
81+
// high is bigger than critical. NOK
82+
ret_val = ns_monitor_heap_gc_threshold_set(100, 80);
83+
if (ret_val == 0) {
84+
return false;
85+
}
86+
87+
88+
// Too big, NOK
89+
ret_val = ns_monitor_heap_gc_threshold_set(80, 101);
90+
if (ret_val == 0) {
91+
return false;
92+
}
93+
94+
// OK
95+
ret_val = ns_monitor_heap_gc_threshold_set(50, 80);
96+
if (ret_val != 0) {
97+
return false;
98+
}
99+
100+
// clear monitor
101+
ret_val = ns_monitor_clear();
102+
if (ret_val != 0) {
103+
return false;
104+
}
105+
106+
// clear monitor again - NOK
107+
ret_val = ns_monitor_clear();
108+
if (ret_val == 0) {
109+
return false;
110+
}
111+
112+
return true;
113+
}
114+
115+
bool test_ns_monitor_timer()
116+
{
117+
int ret_val;
118+
119+
ipv6_routing_table_stub_called = false;
120+
nsdynmemlib_mem_stats_stub.heap_sector_size = 100;
121+
nsdynmemlib_mem_stats_stub.heap_sector_allocated_bytes = 0;
122+
123+
// Init monitor
124+
nsdynmemlib_stub.returnCounter = 1;
125+
ret_val = ns_monitor_init();
126+
if (ret_val != 0) {
127+
return false;
128+
}
129+
130+
// nothing happens
131+
ns_monitor_timer(1);
132+
if (ipv6_routing_table_stub_called) {
133+
return false;
134+
}
135+
136+
// allocation failure - critical gc
137+
nsdynmemlib_mem_stats_stub.heap_alloc_fail_cnt = 1;
138+
ns_monitor_timer(1);
139+
if (!ipv6_routing_table_stub_called) {
140+
return false;
141+
}
142+
ipv6_routing_table_stub_called = false;
143+
144+
// allocation failure again - nothing happens
145+
nsdynmemlib_mem_stats_stub.heap_alloc_fail_cnt = 2;
146+
ns_monitor_timer(1);
147+
if (ipv6_routing_table_stub_called) {
148+
return false;
149+
}
150+
ipv6_routing_table_stub_called = false;
151+
152+
// Monitor state is changed to idle after allocation error
153+
ns_monitor_timer(100);
154+
if (ipv6_routing_table_stub_called) {
155+
return false;
156+
}
157+
158+
// Set thresholds and allocation - High GC
159+
ret_val = ns_monitor_heap_gc_threshold_set(40, 80);
160+
if (ret_val != 0) {
161+
return false;
162+
}
163+
nsdynmemlib_mem_stats_stub.heap_sector_allocated_bytes = 45;
164+
ns_monitor_timer(100);
165+
if (!ipv6_routing_table_stub_called) {
166+
return false;
167+
}
168+
ipv6_routing_table_stub_called = false;
169+
170+
// Timer elapses again - no GC
171+
ns_monitor_timer(100);
172+
if (ipv6_routing_table_stub_called) {
173+
return false;
174+
}
175+
176+
// more memory allocated - Critical GC
177+
nsdynmemlib_mem_stats_stub.heap_sector_allocated_bytes = 95;
178+
ns_monitor_timer(100);
179+
if (!ipv6_routing_table_stub_called) {
180+
return false;
181+
}
182+
ipv6_routing_table_stub_called = false;
183+
184+
// clear monitor
185+
ret_val = ns_monitor_clear();
186+
if (ret_val != 0) {
187+
return false;
188+
}
189+
190+
return true;
191+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2019, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
#ifndef TEST_ADDRESS_H
18+
#define TEST_ADDRESS_H
19+
20+
#ifdef __cplusplus
21+
extern "C" {
22+
#endif
23+
24+
#include <stdbool.h>
25+
26+
bool test_ns_monitor_init();
27+
bool test_ns_monitor_heap_gc_threshold_set();
28+
bool test_ns_monitor_timer();
29+
30+
#ifdef __cplusplus
31+
}
32+
#endif
33+
34+
#endif // TEST_ADDRESS_H
35+

test/nanostack/unittest/nwk_interface/protocol_core/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ TEST_SRC_FILES = \
5252
../../stub/ipv6_stub.c \
5353
../../stub/mac_neighbor_table_stub.c \
5454
../../stub/etx_stub.c \
55+
../../stub/ns_monitor_stub.c \
5556

5657
include ../../MakefileWorker.mk
5758

test/nanostack/unittest/stub/ipv6_routing_table_stub.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
#define NCACHE_GC_AGE 600
5353
#define DCACHE_GC_AGE 30
5454

55+
bool ipv6_routing_table_stub_called;
56+
5557
void ipv6_router_gone(ipv6_neighbour_cache_t *cache, ipv6_neighbour_t *entry)
5658
{
5759
}
@@ -171,6 +173,11 @@ void ipv6_destination_redirect(const uint8_t *dest_addr, const uint8_t *sender_a
171173
{
172174
}
173175

176+
void ipv6_destination_cache_forced_gc(void)
177+
{
178+
ipv6_routing_table_stub_called = true;
179+
}
180+
174181
void ipv6_route_table_set_predicate_fn(ipv6_route_src_t src, ipv6_route_predicate_fn_t fn)
175182
{
176183
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2019, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef __IPV6_ROUTING_TABLE_STUB_H__
19+
#define __IPV6_ROUTING_TABLE_STUB_H__
20+
21+
extern bool ipv6_routing_table_stub_called;
22+
23+
#endif //__IPV6_ROUTING_TABLE_STUB_H__

0 commit comments

Comments
 (0)