-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathfota_module_test.c
More file actions
202 lines (158 loc) · 5.28 KB
/
fota_module_test.c
File metadata and controls
202 lines (158 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#include <unity.h>
#include <zephyr/fff.h>
#include <zephyr/zbus/zbus.h>
#include <zephyr/task_wdt/task_wdt.h>
#include <zephyr/logging/log.h>
#include <net/nrf_cloud_fota_poll.h>
#include "message_channel.h"
#include "fota.h"
DEFINE_FFF_GLOBALS;
FAKE_VALUE_FUNC(int, task_wdt_feed, int);
FAKE_VALUE_FUNC(int, task_wdt_add, uint32_t, task_wdt_callback_t, void *);
FAKE_VALUE_FUNC(int, nrf_cloud_fota_poll_init, struct nrf_cloud_fota_poll_ctx *);
FAKE_VALUE_FUNC(int, nrf_cloud_fota_poll_process_pending, struct nrf_cloud_fota_poll_ctx *);
FAKE_VALUE_FUNC(int, nrf_cloud_fota_poll_process, struct nrf_cloud_fota_poll_ctx *);
FAKE_VALUE_FUNC(int, nrf_cloud_fota_poll_update_apply, struct nrf_cloud_fota_poll_ctx *);
FAKE_VALUE_FUNC(int, fota_download_cancel);
FAKE_VOID_FUNC1(callback_t, int);
ZBUS_MSG_SUBSCRIBER_DEFINE(fota_subscriber);
ZBUS_CHAN_ADD_OBS(FOTA_CHAN, fota_subscriber, 0);
LOG_MODULE_REGISTER(fota_module_test, 4);
static struct nrf_cloud_fota_poll_ctx test_fota_ctx;
/* Forward declarations */
static void event_expect(enum fota_msg_type expected_fota_type);
static void no_events_expect(uint32_t time_in_seconds);
static void event_send(enum fota_msg_type msg);
int init_custom_fake(struct nrf_cloud_fota_poll_ctx *ctx)
{
LOG_DBG("Setup stub for internal nRF Cloud FOTA poll callback handler");
test_fota_ctx = *ctx;
return 0;
}
void setUp(void)
{
RESET_FAKE(task_wdt_feed);
RESET_FAKE(task_wdt_add);
RESET_FAKE(nrf_cloud_fota_poll_init);
RESET_FAKE(nrf_cloud_fota_poll_process_pending);
RESET_FAKE(nrf_cloud_fota_poll_process);
RESET_FAKE(nrf_cloud_fota_poll_update_apply);
RESET_FAKE(fota_download_cancel);
FFF_RESET_HISTORY();
nrf_cloud_fota_poll_init_fake.custom_fake = init_custom_fake;
}
void tearDown(void)
{
/* Check that no events are sent on FOTA_CHAN for some elongated amount of time */
no_events_expect(3600);
/* Reset DuTs internal state between each test case */
event_send(FOTA_CANCEL);
event_expect(FOTA_CANCEL);
}
static void event_expect(enum fota_msg_type expected_fota_type)
{
int err;
const struct zbus_channel *chan;
enum fota_msg_type fota_msg;
err = zbus_sub_wait_msg(&fota_subscriber, &chan, &fota_msg, K_MSEC(1000));
if (err == -ENOMSG) {
LOG_ERR("No fota event received");
TEST_FAIL();
} else if (err) {
LOG_ERR("zbus_sub_wait, error: %d", err);
SEND_FATAL_ERROR();
return;
}
if (chan != &FOTA_CHAN) {
LOG_ERR("Received message from wrong channel");
TEST_FAIL();
}
TEST_ASSERT_EQUAL(expected_fota_type, fota_msg);
}
static void no_events_expect(uint32_t time_in_seconds)
{
int err;
const struct zbus_channel *chan;
enum fota_msg_type fota_msg;
/* Allow the test thread to sleep so that the DUT's thread is allowed to run. */
k_sleep(K_SECONDS(time_in_seconds));
err = zbus_sub_wait_msg(&fota_subscriber, &chan, &fota_msg, K_MSEC(1000));
if (err == 0) {
LOG_ERR("Received fota event with type %d", fota_msg);
TEST_FAIL();
}
}
static void event_send(enum fota_msg_type msg)
{
int err = zbus_chan_pub(&FOTA_CHAN, &msg, K_SECONDS(1));
TEST_ASSERT_EQUAL(0, err);
}
static void invoke_nrf_cloud_fota_callback_stub_status(enum nrf_cloud_fota_status status)
{
test_fota_ctx.status_fn(status, NULL);
}
static void invoke_nrf_cloud_fota_callback_stub_reboot(enum nrf_cloud_fota_reboot_status status)
{
test_fota_ctx.reboot_fn(status);
}
void test_fota_module_should_return_no_available_job(void)
{
/* Given */
nrf_cloud_fota_poll_process_fake.return_val = -EAGAIN;
event_send(FOTA_POLL_REQUEST);
event_expect(FOTA_POLL_REQUEST);
event_expect(FOTA_NO_AVAILABLE_UPDATE);
/* Expect */
TEST_ASSERT(nrf_cloud_fota_poll_process_fake.call_count == 1);
}
void test_fota_module_should_succeed(void)
{
/* Given */
nrf_cloud_fota_poll_process_fake.return_val = 0;
nrf_cloud_fota_poll_update_apply_fake.return_val = 0;
/* 1. Poll for update */
event_send(FOTA_POLL_REQUEST);
event_expect(FOTA_POLL_REQUEST);
/* 2. Downloading update */
invoke_nrf_cloud_fota_callback_stub_status(NRF_CLOUD_FOTA_DOWNLOADING);
event_expect(FOTA_DOWNLOADING_UPDATE);
/* 3. Download succeeded, validation needed */
invoke_nrf_cloud_fota_callback_stub_status(NRF_CLOUD_FOTA_FMFU_VALIDATION_NEEDED);
event_expect(FOTA_IMAGE_APPLY_NEEDED);
/* 4. Apply image */
event_send(FOTA_IMAGE_APPLY);
event_expect(FOTA_IMAGE_APPLY);
/* 5. Reboot needed */
invoke_nrf_cloud_fota_callback_stub_reboot(FOTA_REBOOT_SUCCESS);
event_expect(FOTA_SUCCESS_REBOOT_NEEDED);
}
void test_fota_module_should_fail_on_timeout(void)
{
/* Given */
nrf_cloud_fota_poll_process_fake.return_val = 0;
/* 1. Poll for update */
event_send(FOTA_POLL_REQUEST);
event_expect(FOTA_POLL_REQUEST);
/* 2. Downloading update */
invoke_nrf_cloud_fota_callback_stub_status(NRF_CLOUD_FOTA_DOWNLOADING);
event_expect(FOTA_DOWNLOADING_UPDATE);
/* 3. Download timed out */
invoke_nrf_cloud_fota_callback_stub_status(NRF_CLOUD_FOTA_TIMED_OUT);
event_expect(FOTA_DOWNLOAD_TIMED_OUT);
}
/* This is required to be added to each test. That is because unity's
* main may return nonzero, while zephyr's main currently must
* return 0 in all cases (other values are reserved).
*/
extern int unity_main(void);
int main(void)
{
/* use the runner from test_runner_generate() */
(void)unity_main();
return 0;
}