foxBMS - Unit Tests  1.5.0
The foxBMS Unit Tests API Documentation
test_sys_mon.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2023, Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * We kindly request you to use one or more of the following phrases to refer to
34  * foxBMS in your hardware, software, documentation or advertising materials:
35  *
36  * - ″This product uses parts of foxBMS®″
37  * - ″This product includes parts of foxBMS®″
38  * - ″This product is derived from foxBMS®″
39  *
40  */
41 
42 /**
43  * @file test_sys_mon.c
44  * @author foxBMS Team
45  * @date 2020-04-02 (date of creation)
46  * @updated 2023-02-03 (date of last update)
47  * @version v1.5.0
48  * @ingroup UNIT_TEST_IMPLEMENTATION
49  * @prefix TEST
50  *
51  * @brief Tests for the sys_mon module
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "unity.h"
57 #include "Mockdiag.h"
58 #include "Mockfram.h"
59 #include "Mockfram_cfg.h"
60 #include "Mockos.h"
61 #include "Mocksys_mon_cfg.h"
62 
63 #include "fassert.h"
64 #include "sys_mon.h"
65 #include "test_assert_helper.h"
66 
67 #include <stdbool.h>
68 #include <stdint.h>
69 
70 /*========== Definitions and Implementations for Unit Test ==================*/
71 #define DUMMY_TASK_ID_0 (0)
72 #define DUMMY_TASK_ID_1 (1)
73 #define DUMMY_TASK_ID_2 (2)
74 #define DUMMY_CYCLE_TIME (10)
75 #define DUMMY_MAX_JITTER (1)
76 
78  TEST_ASSERT_EQUAL(DUMMY_TASK_ID_0, taskId);
79 }
80 
82  TEST_ASSERT_EQUAL(DUMMY_TASK_ID_1, taskId);
83 }
84 
86  TEST_ASSERT_EQUAL(DUMMY_TASK_ID_2, taskId);
87 }
88 
105  SYSM_ENABLED,
111 };
112 
113 /** placeholder variable for the FRAM entry of sys mon */
115 
116 /*========== Setup and Teardown =============================================*/
117 void setUp(void) {
119  notifications[DUMMY_TASK_ID_0].timestampEnter = 0;
120  notifications[DUMMY_TASK_ID_0].timestampExit = 0;
121  notifications[DUMMY_TASK_ID_0].duration = 0;
122  notifications[DUMMY_TASK_ID_1].timestampEnter = 0;
123  notifications[DUMMY_TASK_ID_1].timestampExit = 0;
124  notifications[DUMMY_TASK_ID_1].duration = 0;
125  notifications[DUMMY_TASK_ID_2].timestampEnter = 0;
126  notifications[DUMMY_TASK_ID_2].timestampExit = 0;
127  notifications[DUMMY_TASK_ID_2].duration = 0;
128 
140 }
141 
142 void tearDown(void) {
143 }
144 
145 /*========== Test Cases =====================================================*/
147  /* Internal timestamp should be 0, expect an early return
148  (if not returning early, test would fail since other functions
149  are called; also make sure that the code path that would call the
150  DIAG_Handler would be hit if not returning early) */
152  notifications[DUMMY_TASK_ID_0].timestampEnter = 0;
153  notifications[DUMMY_TASK_ID_0].timestampExit = 100;
154  notifications[DUMMY_TASK_ID_0].duration = 100;
155  OS_GetTickCount_ExpectAndReturn(0u);
157 }
158 
160  /* call a task with Monitoring disabled */
161  OS_GetTickCount_ExpectAndReturn(0u);
163 
164  OS_GetTickCount_ExpectAndReturn(100u);
166 }
167 
169  OS_EnterTaskCritical_Ignore();
170  OS_ExitTaskCritical_Ignore();
171 
172  /* provoke the violation of the task duration */
173  OS_GetTickCount_ExpectAndReturn(0u);
175 
177  notifications[DUMMY_TASK_ID_0].timestampEnter = 0;
178  notifications[DUMMY_TASK_ID_0].timestampExit = 100;
179  notifications[DUMMY_TASK_ID_0].duration = 100;
180 
181  OS_GetTickCount_ExpectAndReturn(100u);
182  DIAG_Handler_ExpectAndReturn(
185 }
186 
187 /** same test as #testSYSM_CheckNotificationsProvokeDurationViolation() but with recording enabled */
189  OS_EnterTaskCritical_Ignore();
190  OS_ExitTaskCritical_Ignore();
191 
192  /* provoke the violation of the task duration */
193  OS_GetTickCount_ExpectAndReturn(0u);
195 
197  notifications[DUMMY_TASK_ID_0].timestampEnter = 0;
198  notifications[DUMMY_TASK_ID_0].timestampExit = 100;
199  notifications[DUMMY_TASK_ID_0].duration = 100;
200 
201  OS_GetTickCount_ExpectAndReturn(100u);
202  DIAG_Handler_ExpectAndReturn(
205 
206  /* check if violation has been recorded */
207  FRAM_WriteData_ExpectAndReturn(FRAM_BLOCK_ID_SYS_MON_RECORD, STD_OK);
209  TEST_ASSERT_EQUAL(true, fram_sys_mon_record.anyTimingIssueOccurred);
210 }
211 
213  /* give an invalid Task ID to Notify */
216  TEST_ASSERT_NOT_EQUAL(424242, notifications[SYSM_TASK_ID_MAX + 1u].timestampEnter);
217 }
218 
220  /* check whether Notify properly sets the entry timestamp */
221  OS_EnterTaskCritical_Expect();
222  OS_ExitTaskCritical_Expect();
225  TEST_ASSERT_EQUAL(UINT32_MAX, notifications[DUMMY_TASK_ID_0].timestampEnter);
226 
227  OS_EnterTaskCritical_Expect();
228  OS_ExitTaskCritical_Expect();
230  notifications = TEST_SYSM_GetNotifications();
231  TEST_ASSERT_EQUAL(0, notifications[DUMMY_TASK_ID_0].timestampEnter);
232 }
233 
235  /* check whether Notify properly sets the exit timestamp and computes duration */
236  OS_EnterTaskCritical_Expect();
237  OS_ExitTaskCritical_Expect();
240  TEST_ASSERT_EQUAL(UINT32_MAX, notifications[DUMMY_TASK_ID_0].timestampEnter);
241 
242  const uint32_t exitTime = 100;
243  OS_EnterTaskCritical_Expect();
244  OS_ExitTaskCritical_Expect();
246  notifications = TEST_SYSM_GetNotifications();
247  TEST_ASSERT_EQUAL(exitTime, notifications[DUMMY_TASK_ID_0].timestampExit);
248  /* we go over the overflow, so it should be exit time plus one */
249  TEST_ASSERT_EQUAL(exitTime + 1, notifications[DUMMY_TASK_ID_0].duration);
250 }
251 
253  /* This test hits the assert with an illegal notify type */
254  OS_EnterTaskCritical_Ignore();
255  /* use a notify type of INT8_MAX, as this is likely an illegal notify type */
256  TEST_ASSERT_FAIL_ASSERT(SYSM_Notify(DUMMY_TASK_ID_0, INT8_MAX, UINT32_MAX));
258  /* check that nothing has been written */
259  TEST_ASSERT_NOT_EQUAL(UINT32_MAX, notifications[DUMMY_TASK_ID_0].timestampEnter);
260  TEST_ASSERT_NOT_EQUAL(UINT32_MAX, notifications[DUMMY_TASK_ID_0].timestampExit);
261 }
262 
263 /** test the edge cases of the function that can return recorded violations */
265  OS_EnterTaskCritical_Ignore();
266  OS_ExitTaskCritical_Ignore();
267 
268  /* default state is no violation, therefore no flag should be set */
269  SYSM_TIMING_VIOLATION_RESPONSE_s violationResponse = {0};
270  SYSM_GetRecordedTimingViolations(&violationResponse);
271  TEST_ASSERT_FALSE(violationResponse.recordedViolationEngine);
272  TEST_ASSERT_FALSE(violationResponse.recordedViolation1ms);
273  TEST_ASSERT_FALSE(violationResponse.recordedViolation10ms);
274  TEST_ASSERT_FALSE(violationResponse.recordedViolation100ms);
275  TEST_ASSERT_FALSE(violationResponse.recordedViolation100msAlgo);
276 
277  /* when the general flag is set and one deviates in duration or entry, the violation should be set */
281  SYSM_GetRecordedTimingViolations(&violationResponse);
282  TEST_ASSERT_TRUE(violationResponse.recordedViolationAny);
283  TEST_ASSERT_TRUE(violationResponse.recordedViolationEngine);
284  TEST_ASSERT_TRUE(violationResponse.recordedViolation1ms);
285  TEST_ASSERT_FALSE(violationResponse.recordedViolation10ms);
286  TEST_ASSERT_FALSE(violationResponse.recordedViolation100ms);
287  TEST_ASSERT_FALSE(violationResponse.recordedViolation100msAlgo);
288 }
289 
290 /** test #SYSM_CopyFramStruct() and its reaction to invalid input */
292  FRAM_SYS_MON_RECORD_s dummy = {0};
295 }
296 
297 /** test copy function of #SYSM_CopyFramStruct() */
299  FRAM_SYS_MON_RECORD_s input = {
300  .anyTimingIssueOccurred = true,
301  .task100msAlgorithmEnterTimestamp = 1,
302  .task100msAlgorithmViolatingDuration = 2,
303  .task100msEnterTimestamp = 3,
304  .task100msViolatingDuration = 4,
305  .task10msEnterTimestamp = 5,
306  .task10msViolatingDuration = 6,
307  .task1msEnterTimestamp = 7,
308  .task1msViolatingDuration = 8,
309  .taskEngineEnterTimestamp = 9,
310  .taskEngineViolatingDuration = 10,
311  };
312  FRAM_SYS_MON_RECORD_s output = {
313  .anyTimingIssueOccurred = false,
314  .task100msAlgorithmEnterTimestamp = 0,
315  .task100msAlgorithmViolatingDuration = 0,
316  .task100msEnterTimestamp = 0,
317  .task100msViolatingDuration = 0,
318  .task10msEnterTimestamp = 0,
319  .task10msViolatingDuration = 0,
320  .task1msEnterTimestamp = 0,
321  .task1msViolatingDuration = 0,
322  .taskEngineEnterTimestamp = 0,
323  .taskEngineViolatingDuration = 0,
324  };
325 
326  TEST_ASSERT_NOT_EQUAL(input.anyTimingIssueOccurred, output.anyTimingIssueOccurred);
327  SYSM_CopyFramStruct(&input, &output);
328  TEST_ASSERT_EQUAL(input.anyTimingIssueOccurred, output.anyTimingIssueOccurred);
331  TEST_ASSERT_EQUAL(input.task100msEnterTimestamp, output.task100msEnterTimestamp);
332  TEST_ASSERT_EQUAL(input.task100msViolatingDuration, output.task100msViolatingDuration);
333  TEST_ASSERT_EQUAL(input.task10msEnterTimestamp, output.task10msEnterTimestamp);
334  TEST_ASSERT_EQUAL(input.task10msViolatingDuration, output.task10msViolatingDuration);
335  TEST_ASSERT_EQUAL(input.task1msEnterTimestamp, output.task1msEnterTimestamp);
336  TEST_ASSERT_EQUAL(input.task1msViolatingDuration, output.task1msViolatingDuration);
337  TEST_ASSERT_EQUAL(input.taskEngineEnterTimestamp, output.taskEngineEnterTimestamp);
338  TEST_ASSERT_EQUAL(input.taskEngineViolatingDuration, output.taskEngineViolatingDuration);
339 }
@ DIAG_HANDLER_RETURN_OK
Definition: diag.h:69
@ DIAG_EVENT_NOT_OK
Definition: diag_cfg.h:266
@ DIAG_SYSTEM
Definition: diag_cfg.h:278
@ DIAG_ID_SYSTEM_MONITORING
Definition: diag_cfg.h:178
Assert macro implementation.
@ FRAM_BLOCK_ID_SYS_MON_RECORD
Definition: fram_cfg.h:110
@ STD_OK
Definition: fstd_types.h:83
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:77
struct that stores for each task the last violation of timing
Definition: fram_cfg.h:170
uint32_t taskEngineViolatingDuration
Definition: fram_cfg.h:174
uint32_t taskEngineEnterTimestamp
Definition: fram_cfg.h:176
uint32_t task10msViolatingDuration
Definition: fram_cfg.h:182
uint32_t task100msViolatingDuration
Definition: fram_cfg.h:186
uint32_t task100msEnterTimestamp
Definition: fram_cfg.h:188
uint32_t task1msViolatingDuration
Definition: fram_cfg.h:178
uint32_t task100msAlgorithmEnterTimestamp
Definition: fram_cfg.h:192
uint32_t task10msEnterTimestamp
Definition: fram_cfg.h:184
uint32_t task1msEnterTimestamp
Definition: fram_cfg.h:180
uint32_t task100msAlgorithmViolatingDuration
Definition: fram_cfg.h:190
uint32_t duration
Definition: sys_mon.h:76
uint32_t timestampEnter
Definition: sys_mon.h:74
uint32_t timestampExit
Definition: sys_mon.h:75
SYSM_NOTIFICATION_s * TEST_SYSM_GetNotifications(void)
Definition: sys_mon.c:292
void SYSM_GetRecordedTimingViolations(SYSM_TIMING_VIOLATION_RESPONSE_s *pAnswer)
Returns the timing violation flags determined from fram state.
Definition: sys_mon.c:202
void SYSM_CheckNotifications(void)
overall system monitoring
Definition: sys_mon.c:152
void SYSM_Notify(SYSM_TASK_ID_e taskId, SYSM_NOTIFY_TYPE_e state, uint32_t timestamp)
Sets needed bits to indicate that a task is running.
Definition: sys_mon.c:185
void SYSM_UpdateFramData(void)
Commits the stored changes to FRAM if necessary.
Definition: sys_mon.c:258
void SYSM_CopyFramStruct(const FRAM_SYS_MON_RECORD_s *const kpkFrom, FRAM_SYS_MON_RECORD_s *pTo)
Copy from the from entry to the to entry.
Definition: sys_mon.c:272
system monitoring module
@ SYSM_NOTIFY_ENTER
Definition: sys_mon.h:67
@ SYSM_NOTIFY_EXIT
Definition: sys_mon.h:68
@ SYSM_RECORDING_ENABLED
Definition: sys_mon_cfg.h:85
@ SYSM_RECORDING_DISABLED
Definition: sys_mon_cfg.h:86
@ SYSM_HANDLING_SWITCH_OFF_CONTACTOR
Definition: sys_mon_cfg.h:67
@ SYSM_ENABLED
Definition: sys_mon_cfg.h:91
@ SYSM_DISABLED
Definition: sys_mon_cfg.h:92
SYSM_TASK_ID_e
Definition: sys_mon_cfg.h:74
@ SYSM_TASK_ID_MAX
Definition: sys_mon_cfg.h:80
Helper for unit tests.
#define TEST_ASSERT_PASS_ASSERT(_code_under_test)
assert whether assert macro has passed
#define TEST_ASSERT_FAIL_ASSERT(_code_under_test)
assert whether assert macro has failed
#define DUMMY_TASK_ID_0
Definition: test_sys_mon.c:71
FRAM_SYS_MON_RECORD_s fram_sys_mon_record
Definition: test_sys_mon.c:114
#define DUMMY_TASK_ID_2
Definition: test_sys_mon.c:73
void TEST_SYSM_DummyCallback_1(SYSM_TASK_ID_e taskId)
Definition: test_sys_mon.c:81
void TEST_SYSM_DummyCallback_2(SYSM_TASK_ID_e taskId)
Definition: test_sys_mon.c:85
void testSYSM_NotifyHitAssertWithIllegalNotifyType(void)
Definition: test_sys_mon.c:252
void testSYSM_NotifyInvalidTaskID(void)
Definition: test_sys_mon.c:212
void testSYSM_CheckNotificationsSYSMDisabled(void)
Definition: test_sys_mon.c:159
void testSYSM_CheckNotificationsProvokeDurationViolation(void)
Definition: test_sys_mon.c:168
void TEST_SYSM_DummyCallback_0(SYSM_TASK_ID_e taskId)
Definition: test_sys_mon.c:77
#define DUMMY_MAX_JITTER
Definition: test_sys_mon.c:75
#define DUMMY_CYCLE_TIME
Definition: test_sys_mon.c:74
void testSYSM_GetRecordedTimingViolations(void)
Definition: test_sys_mon.c:264
void testSYSM_CopyFramStructInvalidInput(void)
Definition: test_sys_mon.c:291
void testSYSM_CheckNotificationsEarlyExitOnTimestampEquality(void)
Definition: test_sys_mon.c:146
void testSYSM_CheckNotificationsProvokeDurationViolationWithRecording(void)
Definition: test_sys_mon.c:188
void setUp(void)
Definition: test_sys_mon.c:117
void tearDown(void)
Definition: test_sys_mon.c:142
SYSM_MONITORING_CFG_s sysm_ch_cfg[3]
Definition: test_sys_mon.c:89
void testSYSM_NotifyEnterTimestampProperlySet(void)
Definition: test_sys_mon.c:219
void testSYSM_CopyFramStruct(void)
Definition: test_sys_mon.c:298
#define DUMMY_TASK_ID_1
Definition: test_sys_mon.c:72
void testSYSM_NotifyExitTimestampProperlySetAndDurationCalculated(void)
Definition: test_sys_mon.c:234