LCOV - code coverage report
Current view: top level - test - test_log_utils.c (source / functions) Coverage Total Hit
Test: combined.info Lines: 98.4 % 62 61
Test Date: 2025-05-05 14:46:49 Functions: 100.0 % 9 9
Branches: 80.0 % 10 8
MC/DC: 80.0 % 10 8

             Branch data      MC/DC data    Line data    Source code
       1                 :              :             : #include <sys/stat.h>
       2                 :              :             : #include "unity.h"
       3                 :              :             : #include "log_utils.h"
       4                 :              :             : #include "actuators.h"
       5                 :              :             : #include "dbc.h"
       6                 :              :             : #include <time.h>
       7                 :              :             : #include <string.h>
       8                 :              :             : 
       9                 :              :             : static bool wrap_fopen_fail = false;
      10                 :              :             : static bool wrap_perror_called = false;
      11                 :              :             : 
      12                 :              :             : can_msg can_frame_test = {
      13                 :              :             :     .identifier = ID_AEB_S,
      14                 :              :             :     .dataFrame = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}};
      15                 :              :             : 
      16                 :              :             : can_msg can_frame_empty = {
      17                 :              :             :     .identifier = ID_EMPTY,
      18                 :              :             :     .dataFrame = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}};
      19                 :              :             : 
      20                 :              :             : actuators_abstraction actuators_test = {
      21                 :              :             :     .belt_tightness = false,
      22                 :              :             :     .door_lock = true,
      23                 :              :             :     .should_activate_abs = false,
      24                 :              :             :     .alarm_led = false,
      25                 :              :             :     .alarm_buzzer = false
      26                 :              :             : };
      27                 :              :             : 
      28                 :              :             : actuators_abstraction actuators_try;
      29                 :              :             : 
      30                 :              :             : // FOpen function mock -> Forwards unmocked calls to the real fopen
      31                 :              :             : FILE *__real_fopen(const char *path, const char *mode);
      32                 :              :           6 : FILE *__wrap_fopen(const char *path, const char *mode) {
      33                 :              :             :     // Intercepts ONLY the path “log/log.txt”
      34         [ +  + ]:      [ T  F ]:           6 :     if (strcmp(path, "log/log.txt") == 0) {
      35         [ +  + ]:      [ T  F ]:           4 :         if (wrap_fopen_fail) {
      36                 :              :           1 :             return NULL; // Força falha
      37                 :              :             :         } else {
      38                 :              :             :             // Redirects, using real fopen
      39                 :              :           3 :             return __real_fopen("test/test_log.txt", mode); 
      40                 :              :             :         }
      41                 :              :             :     }
      42                 :              :             :     
      43                 :              :             :     // For other paths, also uses real fopen
      44                 :              :           2 :     return __real_fopen(path, mode);
      45                 :              :             : }
      46                 :              :             : 
      47                 :              :             : // Perror mock:  
      48                 :              :             : void __real_perror(const char *s);
      49                 :              :           1 : void __wrap_perror(const char *s) {
      50                 :              :           1 :     wrap_perror_called = true;
      51                 :              :           1 :     __real_perror(s); 
      52                 :              :           1 : }
      53                 :              :             : 
      54                 :              :           3 : void setUp(){
      55                 :              :           3 :     wrap_fopen_fail = false; // std -> fopen don't fail
      56                 :              :           3 :     wrap_perror_called = false; // Resets perror state
      57                 :              :           3 : }
      58                 :              :             : 
      59                 :              :           3 : void tearDown(){
      60                 :              :             :     // clean stuff up here
      61                 :              :           3 :     remove("test/test_log.txt");
      62                 :              :           3 : }
      63                 :              :             : 
      64                 :              :             : /**
      65                 :              :             :  * @test
      66                 :              :             :  * @brief Verifies if a fopen error is catchable by a test (using mock functions)
      67                 :              :             :  * 
      68                 :              :             :  * \anchor test_log_event_fopen_fail
      69                 :              :             :  * test ID [TC_LOG_UTILS_001](@ref TC_LOG_UTILS_001)
      70                 :              :             :  */
      71                 :              :           1 : void test_log_event_fopen_fail(){
      72                 :              :             :     // Try to write:
      73                 :              :             :     // Verify if write
      74                 :              :             :     // If didn't write, assert test
      75                 :              :           1 :     wrap_fopen_fail = true;
      76                 :              :           1 :     wrap_perror_called = false;
      77                 :              :             : 
      78                 :              :             :     // Chama a função sob teste
      79                 :              :           1 :     log_event("Fopen_Fail_Test", can_frame_test.identifier, actuators_test);
      80                 :              :             : 
      81                 :              :             :     // Verificações
      82         [ -  + ]:      [ T  f ]:           1 :     TEST_ASSERT_TRUE(wrap_perror_called);
      83                 :              :           1 : }
      84                 :              :             : 
      85                 :              :             : /**
      86                 :              :             :  * 
      87                 :              :             :  * @brief Helper function, used to capture the last file of the file
      88                 :              :             :  * @return Type 'actuators_abstraction', according to the data in the file's last line.
      89                 :              :             :  * @note The test may fail if, for any reason, the file cannot be opened.
      90                 :              :             :  *
      91                 :              :             :  */
      92                 :              :           2 : actuators_abstraction read_line_test(){
      93                 :              :           2 :     FILE *file = fopen("test/test_log.txt", "r"); // Abrir o arquivo para leitura
      94                 :              :             : 
      95                 :              :             :     char line[256];
      96                 :              :             : 
      97         [ +  + ]:      [ T  F ]:           7 :     while (fgets(line, 256, file) != NULL) {  // Ler cada linha do arquivo
      98                 :              :             :         int v1, v2, v3, v4, v5;  // Variáveis para armazenar os números
      99         [ -  + ]:      [ t  F ]:           5 :         if (sscanf(line, "%*[^|] | %d | %d | %d | %d | %d", &v1, &v2, &v3, &v4, &v5) == 5) {
     100                 :              :           0 :             printf("Valores extraídos: %d %d %d %d %d\n", v1, v2, v3, v4, v5);
     101                 :              :             :         }
     102                 :              :           5 :         actuators_test.belt_tightness = v1;
     103                 :              :           5 :         actuators_test.door_lock = v2;
     104                 :              :           5 :         actuators_test.should_activate_abs = v3;
     105                 :              :           5 :         actuators_test.alarm_led = v4;
     106                 :              :           5 :         actuators_test.alarm_buzzer = v5;
     107                 :              :             :     }
     108                 :              :           2 :     fclose(file);
     109                 :              :             : 
     110                 :              :           2 :     return actuators_test;
     111                 :              :             : }
     112                 :              :             : 
     113                 :              :             : /**
     114                 :              :             :  * @test
     115                 :              :             :  * @brief Verifies if the line written is as expected.
     116                 :              :             :  * 
     117                 :              :             :  * \anchor test_log_event_check_writing_no1
     118                 :              :             :  * test ID [TC_LOG_UTILS_002](@ref TC_LOG_UTILS_002)
     119                 :              :             :  *
     120                 :              :             :  * @note This test depends on the correct operation of file reading and writing functions. 
     121                 :              :             :  * If one of the two fails, this test will also fail.
     122                 :              :             :  *
     123                 :              :             :  */
     124                 :              :           1 : void test_log_event_check_writing_no1(){
     125                 :              :             :     // Try to write: done
     126                 :              :             :     // Verify if write: done
     127                 :              :             :     // If write correctly, read last line and assert test -> to do
     128                 :              :           1 :     wrap_fopen_fail = false;
     129                 :              :             : 
     130                 :              :           1 :     log_event("Check_Writing", can_frame_test.identifier, actuators_test);
     131                 :              :             :     
     132                 :              :           1 :     actuators_try = read_line_test();
     133                 :              :             : 
     134                 :              :           1 :     TEST_ASSERT_EQUAL(actuators_test.belt_tightness, actuators_try.belt_tightness);
     135                 :              :           1 :     TEST_ASSERT_EQUAL(actuators_test.door_lock, actuators_try.door_lock);
     136                 :              :           1 :     TEST_ASSERT_EQUAL(actuators_test.should_activate_abs, actuators_try.should_activate_abs);
     137                 :              :           1 :     TEST_ASSERT_EQUAL(actuators_test.alarm_led, actuators_try.alarm_led);
     138                 :              :           1 :     TEST_ASSERT_EQUAL(actuators_test.alarm_buzzer, actuators_try.alarm_buzzer);
     139                 :              :           1 : }
     140                 :              :             : 
     141                 :              :           1 : void test_log_event_file_already_exists(){
     142                 :              :           1 :     wrap_fopen_fail = false;
     143                 :              :             :     // First call, file created
     144                 :              :           1 :     log_event("Check_Writing", can_frame_test.identifier, actuators_test);
     145                 :              :             :     // Second call, file created before
     146                 :              :           1 :     log_event("Check_Writing", can_frame_test.identifier, actuators_test);
     147                 :              :             : 
     148                 :              :             :     // The same as the previous test 
     149                 :              :           1 :     actuators_try = read_line_test();
     150                 :              :           1 :     TEST_ASSERT_EQUAL(actuators_test.belt_tightness, actuators_try.belt_tightness);
     151                 :              :           1 :     TEST_ASSERT_EQUAL(actuators_test.door_lock, actuators_try.door_lock);
     152                 :              :           1 :     TEST_ASSERT_EQUAL(actuators_test.should_activate_abs, actuators_try.should_activate_abs);
     153                 :              :           1 :     TEST_ASSERT_EQUAL(actuators_test.alarm_led, actuators_try.alarm_led);
     154                 :              :           1 :     TEST_ASSERT_EQUAL(actuators_test.alarm_buzzer, actuators_try.alarm_buzzer);
     155                 :              :           1 : }
     156                 :              :             : 
     157                 :              :           1 : int main(){
     158                 :              :           1 :     UNITY_BEGIN();
     159                 :              :           1 :     RUN_TEST(test_log_event_fopen_fail);
     160                 :              :           1 :     RUN_TEST(test_log_event_check_writing_no1);
     161                 :              :           1 :     RUN_TEST(test_log_event_file_already_exists);
     162                 :              :           1 :     return UNITY_END();
     163                 :              :             : }
        

Generated by: LCOV version 2.3.1-beta