blob: 5f153c1e4216db107234f1963522d80731a1cc28 [file] [log] [blame]
Andrew Geissler328889d2016-10-10 12:43:48 -05001// A basic unit test that runs on a BMC (qemu or hardware)
2
3#include <iostream>
4#include <elog.hpp>
5#include <log.hpp>
6#include <systemd/sd-journal.h>
7
8using namespace phosphor;
9using namespace logging;
10
11// validate the journal metadata equals the input value
12int validate_journal(const char *i_entry, const char *i_value)
13{
14 sd_journal *journal;
15 const void *data;
16 size_t l;
17 int rc;
18 bool validated = false;
19
20 rc = sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY);
21 if (rc < 0) {
22 std::cerr << "Failed to open journal: " << strerror(-rc) << "\n";
23 return 1;
24 }
25 rc = sd_journal_query_unique(journal, i_entry);
26 if (rc < 0) {
27 std::cerr << "Failed to query journal: " << strerror(-rc) << "\n";
28 return 1;
29 }
30 SD_JOURNAL_FOREACH_UNIQUE(journal, data, l)
31 {
32 std::string journ_entry((const char*)data);
33 std::cout << journ_entry << "\n";
34 if(journ_entry.find(i_value) != std::string::npos)
35 {
36 std::cout << "We found it!\n";
37 validated = true;
38 break;
39 }
40 }
41
42 sd_journal_close(journal);
43
44 rc = (validated) ? 0 : 1;
45 if(rc)
46 {
47 std::cerr << "Failed to find " << i_entry << " in journal!" << "\n";
48 }
49
50 return rc;
51}
52
53int main()
54{
55 // TEST 1 - Basic log
56 log<level::DEBUG>("Basic phosphor logging test");
57
58 // TEST 2 - Log with metadata field
59 const char *file_name = "phosphor_logging_test.txt";
60 int number = 0xFEFE;
61 log<level::DEBUG>("phosphor logging test with attribute",
62 entry("FILE_NAME_WITH_NUM_TEST=%s_%x", file_name, number));
63
64 // Now read back and verify our data made it into the journal
65 int rc = validate_journal("FILE_NAME_WITH_NUM_TEST",
66 "phosphor_logging_test.txt_fefe");
67 if(rc)
68 return(rc);
69
70 // TEST 3 - Create error log with 2 meta data fields (rvalue and lvalue)
71 number = 0x1234;
72 const char *test_string = "/tmp/test_string/";
73 elog<file_not_found>(file_not_found::errnum(number),
74 file_not_found::file_path(test_string),
75 file_not_found::file_name("elog_test_3.txt"));
76
77 // Now read back and verify our data made it into the journal
78 rc = validate_journal(file_not_found::errnum::str_short,
79 std::to_string(number).c_str());
80 if(rc)
81 return(rc);
82
83 rc = validate_journal(file_not_found::file_path::str_short,
84 test_string);
85 if(rc)
86 return(rc);
87
88 rc = validate_journal(file_not_found::file_name::str_short,
89 "elog_test_3.txt");
90 if(rc)
91 return(rc);
92
93 // TEST 4 - Create error log with previous entry use
94 number = 0xFEDC;
95 elog<file_not_found>(file_not_found::errnum(number),
96 prev_entry<file_not_found::file_path>(),
97 file_not_found::file_name("elog_test_4.txt"));
98
99 // Now read back and verify our data made it into the journal
100 rc = validate_journal(file_not_found::errnum::str_short,
101 std::to_string(number).c_str());
102 if(rc)
103 return(rc);
104
105 // This should just be equal to what we put in test 3
106 rc = validate_journal(file_not_found::file_path::str_short,
107 test_string);
108 if(rc)
109 return(rc);
110
111 rc = validate_journal(file_not_found::file_name::str_short,
112 "elog_test_4.txt");
113 if(rc)
114 return(rc);
115
116 // Compile fail tests
117
118 // Simple test to prove we fail to compile due to missing param
119 //elog<file_not_found>(file_not_found::errnum(1),
120 // file_not_found::file_path("test"));
121
122 // Simple test to prove we fail to compile due to invalid param
123 //elog<file_not_found>(file_not_found::errnum(1),
124 // file_not_found::file_path("test"),
125 // file_not_found::file_name(1));
126
127 return 0;
128}
129
130