Branch data MC/DC data Line data Source code
1 : : : #include "unity.h"
2 : : : #include "actuators.h"
3 : : : #include "dbc.h"
4 : : : #include "constants.h"
5 : : : #include "mq_utils.h"
6 : : : #include <unistd.h>
7 : : :
8 : : : // Declare the actuatorsResponseLoop function if it's defined elsewhere
9 : : : void *actuatorsResponseLoop(void *arg);
10 : : : #include <pthread.h>
11 : : :
12 : : : #define LOOP_EMPTY_ITERATIONS_MAX 11
13 : : :
14 : : :
15 : : : // Mock to open_mq function
16 : : 1 : mqd_t open_mq(char *mq_name) {
17 : : 1 : printf("[MOCK] open_mq called with name: %s\n", mq_name);
18 : : 1 : return (mqd_t)1; // Return a mock mqd_t value
19 : : : }
20 : : :
21 : : : // Mock to read_mq
22 : : 5 : int read_mq(mqd_t mq, can_msg *msg) {
23 : : : static int counter = 0;
24 [ + + ]: [ T F ]: 5 : if (counter == 0) {
25 : : 1 : msg->identifier = ID_AEB_S;
26 : : 1 : msg->dataFrame[0] = 0x01;
27 : : 1 : msg->dataFrame[1] = 0x01;
28 : : 1 : counter++;
29 : : 1 : return 0;
30 [ + + ]: [ T F ]: 4 : } else if (counter == 1) {
31 : : 1 : msg->identifier = ID_EMPTY;
32 : : 1 : counter++;
33 : : 1 : return 0;
34 : : : }
35 : : 3 : return -1; // EMpty queue
36 : : : }
37 : : :
38 : : :
39 : : :
40 : : : // Mock to log_event
41 : : 5 : void log_event(const char *id_aeb, uint32_t event_id, actuators_abstraction actuators) {
42 : : 5 : printf("[MOCK LOG] ID: %s, Event: 0x%X, BELT: %d, DOOR: %d, ABS: %d, LED: %d, BUZZ: %d\n",
43 : : 5 : id_aeb, event_id, actuators.belt_tightness, actuators.door_lock,
44 : : 5 : actuators.should_activate_abs, actuators.alarm_led, actuators.alarm_buzzer);
45 : : 5 : }
46 : : :
47 : : : // Mock to global variable actuators_state
48 : : : extern actuators_abstraction actuators_state;
49 : : :
50 : : : // Funções de setup e teardown
51 : : 9 : void setUp(void) {
52 : : 9 : actuators_state.belt_tightness = 0;
53 : : 9 : actuators_state.door_lock = 0;
54 : : 9 : actuators_state.should_activate_abs = 0;
55 : : 9 : actuators_state.alarm_led = 0;
56 : : 9 : actuators_state.alarm_buzzer = 0;
57 : : 9 : }
58 : : :
59 : : 9 : void tearDown(void) {
60 : : : // Nothing to do here
61 : : : // This function is called after each test
62 : : 9 : }
63 : : :
64 : : : /**
65 : : : * @test
66 : : : * @brief Verifies if actuatorsTranslateCanMsg correctly processes a message with ID_AEB_S
67 : : : *
68 : : : * \anchor test_actuatorsTranslateCanMsg_AEB_S_Identifier
69 : : : * test ID [TC_AEB_A__001](@ref TC_AEB_A__001)
70 : : : */
71 : : 1 : void test_actuatorsTranslateCanMsg_AEB_S_Identifier(void) {
72 : : 1 : can_msg test_msg = {
73 : : : .identifier = ID_AEB_S,
74 : : : .dataFrame = {0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
75 : : : };
76 : : :
77 : : 1 : actuatorsTranslateCanMsg(test_msg);
78 : : :
79 : : : // Checks if the actuators' state was updated correctly
80 : : : //// Test case ID: TC_AEB_A__001
81 [ - + ]: [ T f ]: 1 : TEST_ASSERT_TRUE(actuators_state.belt_tightness);
82 [ - + ]: [ T f ]: 1 : TEST_ASSERT_FALSE(actuators_state.door_lock);
83 [ - + ]: [ T f ]: 1 : TEST_ASSERT_TRUE(actuators_state.should_activate_abs);
84 [ - + ]: [ T f ]: 1 : TEST_ASSERT_TRUE(actuators_state.alarm_led);
85 [ - + ]: [ T f ]: 1 : TEST_ASSERT_TRUE(actuators_state.alarm_buzzer);
86 : : 1 : }
87 : : :
88 : : : /**
89 : : : * @test
90 : : : * @brief Verifies if actuatorsTranslateCanMsg handles a message with ID_EMPTY correctly
91 : : : * \anchor test_actuatorsTranslateCanMsg_Empty_Identifier
92 : : : * test ID [TC_AEB_A__002](@ref TC_AEB_A__002)
93 : : : */
94 : : 1 : void test_actuatorsTranslateCanMsg_Empty_Identifier(void) {
95 : : 1 : can_msg test_msg = {
96 : : : .identifier = ID_EMPTY,
97 : : : .dataFrame = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
98 : : : };
99 : : :
100 : : 1 : actuatorsTranslateCanMsg(test_msg);
101 : : :
102 : : : // Checks that the actuators' state remains unchanged
103 : : : //// Test case ID: TC_AEB_A__002
104 [ - + ]: [ T f ]: 1 : TEST_ASSERT_FALSE(actuators_state.belt_tightness);
105 [ - + ]: [ T f ]: 1 : TEST_ASSERT_FALSE(actuators_state.door_lock);
106 [ - + ]: [ T f ]: 1 : TEST_ASSERT_FALSE(actuators_state.should_activate_abs);
107 [ - + ]: [ T f ]: 1 : TEST_ASSERT_FALSE(actuators_state.alarm_led);
108 [ - + ]: [ T f ]: 1 : TEST_ASSERT_FALSE(actuators_state.alarm_buzzer);
109 : : 1 : }
110 : : :
111 : : : /**
112 : : : * @test
113 : : : * @brief Verifies if actuatorsTranslateCanMsg handles a message with ID_EMPTY correctly
114 : : : * \anchor test_updateInternalActuatorsState_Correct_State
115 : : : * test ID [TC_AEB_A__003](@ref TC_AEB_A__003)
116 : : : */
117 : : 1 : void test_updateInternalActuatorsState_Correct_State(void) {
118 : : : // Verifies if updateInternalActuatorsState correctly updates the actuators' state
119 : : 1 : can_msg test_msg = {
120 : : : .identifier = ID_AEB_S,
121 : : : .dataFrame = {0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
122 : : : };
123 : : :
124 : : 1 : updateInternalActuatorsState(test_msg);
125 : : :
126 : : : // Checks the expected state of the actuators
127 : : : //// Test case ID: TC_AEB_A__003
128 [ - + ]: [ T f ]: 1 : TEST_ASSERT_TRUE(actuators_state.belt_tightness);
129 [ - + ]: [ T f ]: 1 : TEST_ASSERT_FALSE(actuators_state.door_lock);
130 [ - + ]: [ T f ]: 1 : TEST_ASSERT_TRUE(actuators_state.should_activate_abs);
131 [ - + ]: [ T f ]: 1 : TEST_ASSERT_TRUE(actuators_state.alarm_led);
132 [ - + ]: [ T f ]: 1 : TEST_ASSERT_TRUE(actuators_state.alarm_buzzer);
133 : : 1 : }
134 : : :
135 : : : /**
136 : : : * @test
137 : : : * @brief Verifies if actuatorsTranslateCanMsg correctly processes a valid message.
138 : : : * \anchor test_actuatorsTranslateCanMsg
139 : : : * test ID [TC_AEB_A__004](@ref TC_AEB_A__004)
140 : : : */
141 : : 1 : void test_actuatorsTranslateCanMsg(void) {
142 : : 1 : can_msg test_msg = {
143 : : : .identifier = ID_AEB_S,
144 : : : .dataFrame = {0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
145 : : : };
146 : : :
147 : : 1 : actuatorsTranslateCanMsg(test_msg);
148 : : :
149 : : : // Checks if the state was updated correctly
150 : : : //// Test case ID: TC_AEB_A__004
151 [ - + ]: [ T f ]: 1 : TEST_ASSERT_TRUE(actuators_state.belt_tightness);
152 [ - + ]: [ T f ]: 1 : TEST_ASSERT_FALSE(actuators_state.door_lock);
153 [ - + ]: [ T f ]: 1 : TEST_ASSERT_TRUE(actuators_state.should_activate_abs);
154 [ - + ]: [ T f ]: 1 : TEST_ASSERT_TRUE(actuators_state.alarm_led);
155 [ - + ]: [ T f ]: 1 : TEST_ASSERT_TRUE(actuators_state.alarm_buzzer);
156 : : 1 : }
157 : : :
158 : : : /**
159 : : : * @test
160 : : : * @brief Verifies if actuatorsTranslateCanMsg correctly processes a valid message.
161 : : : * \anchor test_actuatorsTranslateCanMsg_Unknown_Identifier
162 : : : * test ID [TC_AEB_A__005](@ref TC_AEB_A__005)
163 : : : */
164 : : 1 : void test_actuatorsTranslateCanMsg_Unknown_Identifier(void) {
165 : : : // Verifies if actuatorsTranslateCanMsg handles an unknown identifier correctly
166 : : 1 : can_msg test_msg = {
167 : : : .identifier = 0xFFFF, // Invalid identifier
168 : : : .dataFrame = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
169 : : : };
170 : : :
171 : : 1 : actuatorsTranslateCanMsg(test_msg);
172 : : :
173 : : : // Checks that the actuators' state remains unchanged
174 : : : //// Test case ID: TC_AEB_A__005
175 [ - + ]: [ T f ]: 1 : TEST_ASSERT_FALSE(actuators_state.belt_tightness);
176 [ - + ]: [ T f ]: 1 : TEST_ASSERT_FALSE(actuators_state.door_lock);
177 [ - + ]: [ T f ]: 1 : TEST_ASSERT_FALSE(actuators_state.should_activate_abs);
178 [ - + ]: [ T f ]: 1 : TEST_ASSERT_FALSE(actuators_state.alarm_led);
179 [ - + ]: [ T f ]: 1 : TEST_ASSERT_FALSE(actuators_state.alarm_buzzer);
180 : : 1 : }
181 : : :
182 : : : /** @test
183 : : : * @brief Verifies if updateInternalActuatorsState processes correctly when dataFrame[0] == 0x01
184 : : : * \anchor test_updateInternalActuatorsState_DataFrame0_Active
185 : : : * test ID [TC_AEB_A__006](@ref TC_AEB_A__006)
186 : : : */
187 : : 1 : void test_updateInternalActuatorsState_DataFrame0_Active(void) {
188 : : : // Verifies if updateInternalActuatorsState processes correctly when dataFrame[0] == 0x01
189 : : 1 : can_msg test_msg = {
190 : : : .identifier = ID_AEB_S,
191 : : : .dataFrame = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
192 : : : };
193 : : :
194 : : 1 : updateInternalActuatorsState(test_msg);
195 : : :
196 : : : // Checks the expected state of the actuators
197 : : : //// Test case ID: TC_AEB_A__006
198 [ - + ]: [ T f ]: 1 : TEST_ASSERT_FALSE(actuators_state.belt_tightness);
199 [ - + ]: [ T f ]: 1 : TEST_ASSERT_TRUE(actuators_state.door_lock);
200 [ - + ]: [ T f ]: 1 : TEST_ASSERT_FALSE(actuators_state.should_activate_abs);
201 [ - + ]: [ T f ]: 1 : TEST_ASSERT_TRUE(actuators_state.alarm_led);
202 [ - + ]: [ T f ]: 1 : TEST_ASSERT_TRUE(actuators_state.alarm_buzzer);
203 : : 1 : }
204 : : :
205 : : :
206 : : :
207 : : :
208 : : :
209 : : : /** @test
210 : : : * @brief Verifies the initial state of the actuators
211 : : : * \anchor test_InitialActuatorsState
212 : : : * test ID [TC_AEB_A__007](@ref TC_AEB_A__007)
213 : : : */
214 : : 1 : void test_InitialActuatorsState(void) {
215 : : : // Verifies that the initial state of the actuators is zero
216 : : : //// Test case ID: TC_AEB_A__007
217 [ - + ]: [ T f ]: 1 : TEST_ASSERT_FALSE(actuators_state.belt_tightness);
218 [ - + ]: [ T f ]: 1 : TEST_ASSERT_FALSE(actuators_state.door_lock);
219 [ - + ]: [ T f ]: 1 : TEST_ASSERT_FALSE(actuators_state.should_activate_abs);
220 [ - + ]: [ T f ]: 1 : TEST_ASSERT_FALSE(actuators_state.alarm_led);
221 [ - + ]: [ T f ]: 1 : TEST_ASSERT_FALSE(actuators_state.alarm_buzzer);
222 : : 1 : }
223 : : :
224 : : : /** @test
225 : : : * @brief Verifies if actuatorsTranslateCanMsg handles an unexpected dataFrame correctly
226 : : : * \anchor test_actuatorsTranslateCanMsg_Unexpected_DataFrame
227 : : : * test ID [TC_AEB_A__008](@ref TC_AEB_A__008)
228 : : : */
229 : : 1 : void test_actuatorsTranslateCanMsg_Unexpected_DataFrame(void) {
230 : : : // Verifies if actuatorsTranslateCanMsg handles an unexpected dataFrame correctly
231 : : 1 : can_msg test_msg = {
232 : : : .identifier = ID_AEB_S,
233 : : : .dataFrame = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00, 0x11}
234 : : : };
235 : : :
236 : : 1 : actuatorsTranslateCanMsg(test_msg);
237 : : :
238 : : : // Expected state: should follow the general rule of the `updateInternalActuatorsState` function
239 : : : //// Test case ID: TC_AEB_A__008
240 [ - + ]: [ T f ]: 1 : TEST_ASSERT_FALSE(actuators_state.belt_tightness);
241 [ - + ]: [ T f ]: 1 : TEST_ASSERT_TRUE(actuators_state.door_lock);
242 [ - + ]: [ T f ]: 1 : TEST_ASSERT_FALSE(actuators_state.should_activate_abs);
243 [ - + ]: [ T f ]: 1 : TEST_ASSERT_FALSE(actuators_state.alarm_led);
244 [ - + ]: [ T f ]: 1 : TEST_ASSERT_FALSE(actuators_state.alarm_buzzer);
245 : : 1 : }
246 : : :
247 : : : /**
248 : : : * @test
249 : : : * @brief Verifies if the actuators response loop handles an empty queue correctly
250 : : : * \anchor test_actuatorsResponseLoop_EmptyQueue
251 : : : * test ID [TC_AEB_A__009](@ref TC_AEB_A__009) */
252 : : 0 : void test_actuatorsResponseLoop_EmptyQueue(void) {
253 : : : //// Test case ID: TC_AEB_A__009
254 : : :
255 : : 0 : int initial_empty_mq_counter = 0;
256 : : : pthread_t thread;
257 : : :
258 : : : // Starts the actuators response loop thread
259 : : 0 : pthread_create(&thread, NULL, actuatorsResponseLoop, NULL);
260 : : :
261 : : : // Waits enough time to reach the limit
262 : : 0 : sleep((LOOP_EMPTY_ITERATIONS_MAX + 1) * 0.2);
263 : : :
264 : : : // Checks if the empty iterations counter reached the limit
265 : : :
266 : : 0 : TEST_ASSERT_EQUAL(LOOP_EMPTY_ITERATIONS_MAX, initial_empty_mq_counter);
267 : : :
268 : : : // Cancels and joins the thread
269 : : 0 : pthread_cancel(thread);
270 : : 0 : pthread_join(thread, NULL);
271 : : 0 : }
272 : : :
273 : : : // Mock for mock_mq_send
274 : : 1 : int mock_mq_send(mqd_t mq, const can_msg *msg) {
275 : : 1 : printf("[MOCK] mock_mq_send called with identifier: 0x%X\n", msg->identifier);
276 : : 1 : return 0; // Simula sucesso no envio da mensagem
277 : : : }
278 : : :
279 : : : /**
280 : : : * @test
281 : : : * @brief Verifies if the actuators response loop handles unknown messages correctly
282 : : : * \anchor test_actuatorsResponseLoop_UnknownMessages
283 : : : * test ID [TC_AEB_A__010](@ref TC_AEB_A__010)
284 : : : */
285 : : 1 : void test_actuatorsResponseLoop_UnknownMessages(void) {
286 : : : //// Test case ID: TC_AEB_A__010
287 : : 1 : actuators_state.belt_tightness = false;
288 : : 1 : actuators_state.door_lock = true;
289 : : 1 : actuators_state.should_activate_abs = false;
290 : : 1 : actuators_state.alarm_led = false;
291 : : 1 : actuators_state.alarm_buzzer = false;
292 : : :
293 : : 1 : can_msg unknown_msg = {
294 : : : .identifier = ID_AEB_S, // Invalid identifier
295 : : : .dataFrame = {0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
296 : : : };
297 : : :
298 : : : // Mock to simulate sending a message to the queue
299 : : 1 : mqd_t actuators_mq = open_mq("/mock_actuators_mq");
300 : : 1 : mock_mq_send(actuators_mq, &unknown_msg);
301 : : :
302 : : : // Creates a thread to execute the response loop
303 : : : pthread_t thread;
304 : : 1 : pthread_create(&thread, NULL, actuatorsResponseLoop, NULL);
305 : : :
306 : : : // Waits for the loop to process the message
307 : : 1 : sleep(1);
308 : : :
309 : : : // Checks that the actuators' state did NOT change
310 : : : //// Test case ID: TC_AEB_A__011
311 [ - + ]: [ T f ]: 1 : TEST_ASSERT_TRUE(actuators_state.belt_tightness);
312 [ - + ]: [ T f ]: 1 : TEST_ASSERT_FALSE(actuators_state.door_lock);
313 [ - + ]: [ T f ]: 1 : TEST_ASSERT_TRUE(actuators_state.should_activate_abs);
314 [ - + ]: [ T f ]: 1 : TEST_ASSERT_TRUE(actuators_state.alarm_led);
315 [ - + ]: [ T f ]: 1 : TEST_ASSERT_TRUE(actuators_state.alarm_buzzer);
316 : : :
317 : : : // Cancels and joins the thread
318 : : 1 : pthread_cancel(thread);
319 : : 1 : pthread_join(thread, NULL);
320 : : 1 : }
321 : : :
322 : : :
323 : : 1 : int main(void) {
324 : : 1 : UNITY_BEGIN();
325 : : 1 : RUN_TEST(test_actuatorsTranslateCanMsg_AEB_S_Identifier);
326 : : 1 : RUN_TEST(test_actuatorsTranslateCanMsg_Empty_Identifier);
327 : : 1 : RUN_TEST(test_updateInternalActuatorsState_Correct_State);
328 : : 1 : RUN_TEST(test_actuatorsTranslateCanMsg);
329 : : 1 : RUN_TEST(test_actuatorsTranslateCanMsg_Unknown_Identifier);
330 : : 1 : RUN_TEST(test_updateInternalActuatorsState_DataFrame0_Active);
331 : : 1 : RUN_TEST(test_InitialActuatorsState);
332 : : 1 : RUN_TEST(test_actuatorsTranslateCanMsg_Unexpected_DataFrame);
333 : : 1 : RUN_TEST(test_actuatorsResponseLoop_UnknownMessages);
334 : : 1 : return UNITY_END();
335 : : : }
|