foxBMS - Unit Tests  1.4.0
The foxBMS Unit Tests API Documentation
test_sys_mon.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2022, 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 2022-07-28 (date of last update)
47  * @version v1.4.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 /*========== Definitions and Implementations for Unit Test ==================*/
68 #define DUMMY_TASK_ID_0 (0)
69 #define DUMMY_TASK_ID_1 (1)
70 #define DUMMY_TASK_ID_2 (2)
71 #define DUMMY_CYCLE_TIME (10)
72 #define DUMMY_MAX_JITTER (1)
73 
75  TEST_ASSERT_EQUAL(DUMMY_TASK_ID_0, taskId);
76 }
77 
79  TEST_ASSERT_EQUAL(DUMMY_TASK_ID_1, taskId);
80 }
81 
83  TEST_ASSERT_EQUAL(DUMMY_TASK_ID_2, taskId);
84 }
85 
102  SYSM_ENABLED,
108 };
109 
110 /** placeholder variable for the FRAM entry of sys mon */
112 
113 /*========== Setup and Teardown =============================================*/
114 void setUp(void) {
116  notifications[DUMMY_TASK_ID_0].timestampEnter = 0;
117  notifications[DUMMY_TASK_ID_0].timestampExit = 0;
118  notifications[DUMMY_TASK_ID_0].duration = 0;
119  notifications[DUMMY_TASK_ID_1].timestampEnter = 0;
120  notifications[DUMMY_TASK_ID_1].timestampExit = 0;
121  notifications[DUMMY_TASK_ID_1].duration = 0;
122  notifications[DUMMY_TASK_ID_2].timestampEnter = 0;
123  notifications[DUMMY_TASK_ID_2].timestampExit = 0;
124  notifications[DUMMY_TASK_ID_2].duration = 0;
125 
137 }
138 
139 void tearDown(void) {
140 }
141 
142 /*========== Test Cases =====================================================*/
144  /* Internal timestamp should be 0, expect an early return
145  (if not returning early, test would fail since other functions
146  are called; also make sure that the code path that would call the
147  DIAG_Handler would be hit if not returning early) */
149  notifications[DUMMY_TASK_ID_0].timestampEnter = 0;
150  notifications[DUMMY_TASK_ID_0].timestampExit = 100;
151  notifications[DUMMY_TASK_ID_0].duration = 100;
152  OS_GetTickCount_ExpectAndReturn(0u);
154 }
155 
157  /* call a task with Monitoring disabled */
158  OS_GetTickCount_ExpectAndReturn(0u);
160 
161  OS_GetTickCount_ExpectAndReturn(100u);
163 }
164 
166  OS_EnterTaskCritical_Ignore();
167  OS_ExitTaskCritical_Ignore();
168 
169  /* provoke the violation of the task duration */
170  OS_GetTickCount_ExpectAndReturn(0u);
172 
174  notifications[DUMMY_TASK_ID_0].timestampEnter = 0;
175  notifications[DUMMY_TASK_ID_0].timestampExit = 100;
176  notifications[DUMMY_TASK_ID_0].duration = 100;
177 
178  OS_GetTickCount_ExpectAndReturn(100u);
179  DIAG_Handler_ExpectAndReturn(
182 }
183 
184 /** same test as #testSYSM_CheckNotificationsProvokeDurationViolation() but with recording enabled */
186  OS_EnterTaskCritical_Ignore();
187  OS_ExitTaskCritical_Ignore();
188 
189  /* provoke the violation of the task duration */
190  OS_GetTickCount_ExpectAndReturn(0u);
192 
194  notifications[DUMMY_TASK_ID_0].timestampEnter = 0;
195  notifications[DUMMY_TASK_ID_0].timestampExit = 100;
196  notifications[DUMMY_TASK_ID_0].duration = 100;
197 
198  OS_GetTickCount_ExpectAndReturn(100u);
199  DIAG_Handler_ExpectAndReturn(
202 
203  /* check if violation has been recorded */
204  FRAM_WriteData_ExpectAndReturn(FRAM_BLOCK_ID_SYS_MON_RECORD, STD_OK);
206  TEST_ASSERT_EQUAL(true, fram_sys_mon_record.anyTimingIssueOccurred);
207 }
208 
210  /* give an invalid Task ID to Notify */
213  TEST_ASSERT_NOT_EQUAL(424242, notifications[SYSM_TASK_ID_MAX + 1u].timestampEnter);
214 }
215 
217  /* check whether Notify properly sets the entry timestamp */
218  OS_EnterTaskCritical_Expect();
219  OS_ExitTaskCritical_Expect();
222  TEST_ASSERT_EQUAL(UINT32_MAX, notifications[DUMMY_TASK_ID_0].timestampEnter);
223 
224  OS_EnterTaskCritical_Expect();
225  OS_ExitTaskCritical_Expect();
227  notifications = TEST_SYSM_GetNotifications();
228  TEST_ASSERT_EQUAL(0, notifications[DUMMY_TASK_ID_0].timestampEnter);
229 }
230 
232  /* check whether Notify properly sets the exit timestamp and computes duration */
233  OS_EnterTaskCritical_Expect();
234  OS_ExitTaskCritical_Expect();
237  TEST_ASSERT_EQUAL(UINT32_MAX, notifications[DUMMY_TASK_ID_0].timestampEnter);
238 
239  const uint32 exitTime = 100;
240  OS_EnterTaskCritical_Expect();
241  OS_ExitTaskCritical_Expect();
243  notifications = TEST_SYSM_GetNotifications();
244  TEST_ASSERT_EQUAL(exitTime, notifications[DUMMY_TASK_ID_0].timestampExit);
245  /* we go over the overflow, so it should be exit time plus one */
246  TEST_ASSERT_EQUAL(exitTime + 1, notifications[DUMMY_TASK_ID_0].duration);
247 }
248 
250  /* This test hits the assert with an illegal notify type */
251  OS_EnterTaskCritical_Ignore();
252  /* use a notify type of INT8_MAX, as this is likely an illegal notify type */
253  TEST_ASSERT_FAIL_ASSERT(SYSM_Notify(DUMMY_TASK_ID_0, INT8_MAX, UINT32_MAX));
255  /* check that nothing has been written */
256  TEST_ASSERT_NOT_EQUAL(UINT32_MAX, notifications[DUMMY_TASK_ID_0].timestampEnter);
257  TEST_ASSERT_NOT_EQUAL(UINT32_MAX, notifications[DUMMY_TASK_ID_0].timestampExit);
258 }
259 
260 /** test the edge cases of the function that can return recorded violations */
262  OS_EnterTaskCritical_Ignore();
263  OS_ExitTaskCritical_Ignore();
264 
265  /* default state is no violation, therefore no flag should be set */
266  SYSM_TIMING_VIOLATION_RESPONSE_s violationResponse = {0};
267  SYSM_GetRecordedTimingViolations(&violationResponse);
268  TEST_ASSERT_FALSE(violationResponse.recordedViolationEngine);
269  TEST_ASSERT_FALSE(violationResponse.recordedViolation1ms);
270  TEST_ASSERT_FALSE(violationResponse.recordedViolation10ms);
271  TEST_ASSERT_FALSE(violationResponse.recordedViolation100ms);
272  TEST_ASSERT_FALSE(violationResponse.recordedViolation100msAlgo);
273 
274  /* when the general flag is set and one deviates in duration or entry, the violation should be set */
278  SYSM_GetRecordedTimingViolations(&violationResponse);
279  TEST_ASSERT_TRUE(violationResponse.recordedViolationAny);
280  TEST_ASSERT_TRUE(violationResponse.recordedViolationEngine);
281  TEST_ASSERT_TRUE(violationResponse.recordedViolation1ms);
282  TEST_ASSERT_FALSE(violationResponse.recordedViolation10ms);
283  TEST_ASSERT_FALSE(violationResponse.recordedViolation100ms);
284  TEST_ASSERT_FALSE(violationResponse.recordedViolation100msAlgo);
285 }
286 
287 /** test #SYSM_CopyFramStruct() and its reaction to invalid input */
289  FRAM_SYS_MON_RECORD_s dummy = {0};
292 }
293 
294 /** test copy function of #SYSM_CopyFramStruct() */
296  FRAM_SYS_MON_RECORD_s input = {
297  .anyTimingIssueOccurred = true,
298  .task100msAlgorithmEnterTimestamp = 1,
299  .task100msAlgorithmViolatingDuration = 2,
300  .task100msEnterTimestamp = 3,
301  .task100msViolatingDuration = 4,
302  .task10msEnterTimestamp = 5,
303  .task10msViolatingDuration = 6,
304  .task1msEnterTimestamp = 7,
305  .task1msViolatingDuration = 8,
306  .taskEngineEnterTimestamp = 9,
307  .taskEngineViolatingDuration = 10,
308  };
309  FRAM_SYS_MON_RECORD_s output = {
310  .anyTimingIssueOccurred = false,
311  .task100msAlgorithmEnterTimestamp = 0,
312  .task100msAlgorithmViolatingDuration = 0,
313  .task100msEnterTimestamp = 0,
314  .task100msViolatingDuration = 0,
315  .task10msEnterTimestamp = 0,
316  .task10msViolatingDuration = 0,
317  .task1msEnterTimestamp = 0,
318  .task1msViolatingDuration = 0,
319  .taskEngineEnterTimestamp = 0,
320  .taskEngineViolatingDuration = 0,
321  };
322 
323  TEST_ASSERT_NOT_EQUAL(input.anyTimingIssueOccurred, output.anyTimingIssueOccurred);
324  SYSM_CopyFramStruct(&input, &output);
325  TEST_ASSERT_EQUAL(input.anyTimingIssueOccurred, output.anyTimingIssueOccurred);
328  TEST_ASSERT_EQUAL(input.task100msEnterTimestamp, output.task100msEnterTimestamp);
329  TEST_ASSERT_EQUAL(input.task100msViolatingDuration, output.task100msViolatingDuration);
330  TEST_ASSERT_EQUAL(input.task10msEnterTimestamp, output.task10msEnterTimestamp);
331  TEST_ASSERT_EQUAL(input.task10msViolatingDuration, output.task10msViolatingDuration);
332  TEST_ASSERT_EQUAL(input.task1msEnterTimestamp, output.task1msEnterTimestamp);
333  TEST_ASSERT_EQUAL(input.task1msViolatingDuration, output.task1msViolatingDuration);
334  TEST_ASSERT_EQUAL(input.taskEngineEnterTimestamp, output.taskEngineEnterTimestamp);
335  TEST_ASSERT_EQUAL(input.taskEngineViolatingDuration, output.taskEngineViolatingDuration);
336 }
@ DIAG_HANDLER_RETURN_OK
Definition: diag.h:65
@ DIAG_EVENT_NOT_OK
Definition: diag_cfg.h:239
@ DIAG_SYSTEM
Definition: diag_cfg.h:251
@ DIAG_ID_SYSTEM_MONITORING
Definition: diag_cfg.h:159
Assert macro implementation.
@ FRAM_BLOCK_ID_SYS_MON_RECORD
Definition: fram_cfg.h:106
@ STD_OK
Definition: fstd_types.h:82
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:76
struct that stores for each task the last violation of timing
Definition: fram_cfg.h:164
uint32_t taskEngineViolatingDuration
Definition: fram_cfg.h:168
uint32_t taskEngineEnterTimestamp
Definition: fram_cfg.h:170
uint32_t task10msViolatingDuration
Definition: fram_cfg.h:176
uint32_t task100msViolatingDuration
Definition: fram_cfg.h:180
uint32_t task100msEnterTimestamp
Definition: fram_cfg.h:182
uint32_t task1msViolatingDuration
Definition: fram_cfg.h:172
uint32_t task100msAlgorithmEnterTimestamp
Definition: fram_cfg.h:186
uint32_t task10msEnterTimestamp
Definition: fram_cfg.h:178
uint32_t task1msEnterTimestamp
Definition: fram_cfg.h:174
uint32_t task100msAlgorithmViolatingDuration
Definition: fram_cfg.h:184
uint32_t duration
Definition: sys_mon.h:74
uint32_t timestampEnter
Definition: sys_mon.h:72
uint32_t timestampExit
Definition: sys_mon.h:73
SYSM_NOTIFICATION_s * TEST_SYSM_GetNotifications(void)
Definition: sys_mon.c:290
void SYSM_GetRecordedTimingViolations(SYSM_TIMING_VIOLATION_RESPONSE_s *pAnswer)
Returns the timing violation flags determined from fram state.
Definition: sys_mon.c:200
void SYSM_CheckNotifications(void)
overall system monitoring
Definition: sys_mon.c:150
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:183
void SYSM_UpdateFramData(void)
Commits the stored changes to FRAM if necessary.
Definition: sys_mon.c:256
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:270
system monitoring module
@ SYSM_NOTIFY_ENTER
Definition: sys_mon.h:65
@ SYSM_NOTIFY_EXIT
Definition: sys_mon.h:66
@ SYSM_RECORDING_ENABLED
Definition: sys_mon_cfg.h:84
@ SYSM_RECORDING_DISABLED
Definition: sys_mon_cfg.h:85
@ SYSM_HANDLING_SWITCH_OFF_CONTACTOR
Definition: sys_mon_cfg.h:66
@ SYSM_ENABLED
Definition: sys_mon_cfg.h:90
@ SYSM_DISABLED
Definition: sys_mon_cfg.h:91
SYSM_TASK_ID_e
Definition: sys_mon_cfg.h:73
@ SYSM_TASK_ID_MAX
Definition: sys_mon_cfg.h:79
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:68
FRAM_SYS_MON_RECORD_s fram_sys_mon_record
Definition: test_sys_mon.c:111
#define DUMMY_TASK_ID_2
Definition: test_sys_mon.c:70
void TEST_SYSM_DummyCallback_1(SYSM_TASK_ID_e taskId)
Definition: test_sys_mon.c:78
void TEST_SYSM_DummyCallback_2(SYSM_TASK_ID_e taskId)
Definition: test_sys_mon.c:82
void testSYSM_NotifyHitAssertWithIllegalNotifyType(void)
Definition: test_sys_mon.c:249
void testSYSM_NotifyInvalidTaskID(void)
Definition: test_sys_mon.c:209
void testSYSM_CheckNotificationsSYSMDisabled(void)
Definition: test_sys_mon.c:156
void testSYSM_CheckNotificationsProvokeDurationViolation(void)
Definition: test_sys_mon.c:165
void TEST_SYSM_DummyCallback_0(SYSM_TASK_ID_e taskId)
Definition: test_sys_mon.c:74
#define DUMMY_MAX_JITTER
Definition: test_sys_mon.c:72
#define DUMMY_CYCLE_TIME
Definition: test_sys_mon.c:71
void testSYSM_GetRecordedTimingViolations(void)
Definition: test_sys_mon.c:261
void testSYSM_CopyFramStructInvalidInput(void)
Definition: test_sys_mon.c:288
void testSYSM_CheckNotificationsEarlyExitOnTimestampEquality(void)
Definition: test_sys_mon.c:143
void testSYSM_CheckNotificationsProvokeDurationViolationWithRecording(void)
Definition: test_sys_mon.c:185
void setUp(void)
Definition: test_sys_mon.c:114
void tearDown(void)
Definition: test_sys_mon.c:139
SYSM_MONITORING_CFG_s sysm_ch_cfg[3]
Definition: test_sys_mon.c:86
void testSYSM_NotifyEnterTimestampProperlySet(void)
Definition: test_sys_mon.c:216
void testSYSM_CopyFramStruct(void)
Definition: test_sys_mon.c:295
#define DUMMY_TASK_ID_1
Definition: test_sys_mon.c:69
void testSYSM_NotifyExitTimestampProperlySetAndDurationCalculated(void)
Definition: test_sys_mon.c:231