1 /*
2  * Copyright (C) 2019 Codership Oy <info@codership.com>
3  */
4 
5 #include "check_gcomm.hpp"
6 
7 #include "gu_string_utils.hpp" // strsplit()
8 #include "gu_exception.hpp"
9 #include "gu_logger.hpp"
10 #include "gu_crc32c.h" // gu_crc32c_configure()
11 
12 #include <string>
13 #include <vector>
14 #include <algorithm>
15 #include <cstdlib>
16 #include <check.h>
17 #include <string.h>
18 #include <unistd.h>
19 
20 // <using namespace gcomm;
21 
22 using std::string;
23 using std::vector;
24 
25 typedef Suite* (*suite_creator_f)();
26 
27 struct GCommSuite
28 {
29     string name;
30     suite_creator_f suite;
31 };
32 
33 static GCommSuite suites[] = {
34     {"util_nondet", util_nondet_suite},
35     {"gmcast_nondet", gmcast_suite},
36     {"pc_nondet", pc_nondet_suite},
37     {"", 0}
38 };
39 
40 #define LOG_FILE "check_gcomm_nondet.log"
41 
main(int argc,char * argv[])42 int main(int argc, char* argv[])
43 {
44     SRunner* sr = srunner_create(0);
45     vector<string>* suits = 0;
46     FILE* log_file(0);
47 
48     if (argc > 1 && !strcmp(argv[1],"nofork")) {
49         srunner_set_fork_status(sr, CK_NOFORK);
50     }
51     else if (argc > 1 && strcmp(argv[1], "nolog") == 0)
52     { /* no log redirection */}
53     else { // running in the background, loggin' to file
54         log_file = fopen (LOG_FILE, "w");
55         if (!log_file) return EXIT_FAILURE;
56         gu_conf_set_log_file (log_file);
57 
58         // redirect occasional stderr there as well
59         if (dup2(fileno(log_file), 2) < 0)
60         {
61             perror("dup2() failed: ");
62             return EXIT_FAILURE;
63         }
64     }
65 
66     if (::getenv("CHECK_GCOMM_DEBUG"))
67     {
68         gu_log_max_level = GU_LOG_DEBUG;
69         //gu::Logger::enable_debug(true);
70     }
71 
72     gu_crc32c_configure();
73 
74     log_info << "check_gcomm_nondet, start tests";
75     if (::getenv("CHECK_GCOMM_SUITES"))
76     {
77         suits = new vector<string>(gu::strsplit(::getenv("CHECK_GCOMM_SUITES"), ','));
78     }
79 
80     for (size_t i = 0; suites[i].suite != 0; ++i)
81     {
82         if (suits == 0 ||
83             find(suits->begin(), suits->end(), suites[i].name) != suits->end())
84         {
85             srunner_add_suite(sr, suites[i].suite());
86         }
87     }
88     delete suits;
89     suits = 0;
90 
91     srunner_run_all(sr, CK_NORMAL);
92     log_info << "check_gcomm, run all tests";
93     int n_fail = srunner_ntests_failed(sr);
94     srunner_free(sr);
95 
96     if (0 == n_fail && 0 != log_file) ::unlink(LOG_FILE);
97 
98     return n_fail == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
99 }
100