blob: 96d2a36ad160c1ba35355df8faf407529e3e7796 [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>
Andrew Geissler328889d2016-10-10 12:43:48 -05004#include <systemd/sd-journal.h>
Andrew Geisslerc830e0f2016-10-18 12:51:29 -05005#include <sstream>
Saqib Khan2bb15192017-02-13 13:19:55 -06006#include <phosphor-logging/elog.hpp>
7#include <phosphor-logging/log.hpp>
Andrew Geissler328889d2016-10-10 12:43:48 -05008
9using namespace phosphor;
10using namespace logging;
11
12// validate the journal metadata equals the input value
13int validate_journal(const char *i_entry, const char *i_value)
14{
15 sd_journal *journal;
16 const void *data;
17 size_t l;
18 int rc;
19 bool validated = false;
20
21 rc = sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY);
22 if (rc < 0) {
23 std::cerr << "Failed to open journal: " << strerror(-rc) << "\n";
24 return 1;
25 }
26 rc = sd_journal_query_unique(journal, i_entry);
27 if (rc < 0) {
28 std::cerr << "Failed to query journal: " << strerror(-rc) << "\n";
29 return 1;
30 }
31 SD_JOURNAL_FOREACH_UNIQUE(journal, data, l)
32 {
33 std::string journ_entry((const char*)data);
34 std::cout << journ_entry << "\n";
35 if(journ_entry.find(i_value) != std::string::npos)
36 {
37 std::cout << "We found it!\n";
38 validated = true;
39 break;
40 }
41 }
42
43 sd_journal_close(journal);
44
45 rc = (validated) ? 0 : 1;
46 if(rc)
47 {
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050048 std::cerr << "Failed to find " << i_entry << " with value " << i_value
49 <<" in journal!" << "\n";
Andrew Geissler328889d2016-10-10 12:43:48 -050050 }
51
52 return rc;
53}
54
55int main()
56{
57 // TEST 1 - Basic log
58 log<level::DEBUG>("Basic phosphor logging test");
59
60 // TEST 2 - Log with metadata field
61 const char *file_name = "phosphor_logging_test.txt";
62 int number = 0xFEFE;
63 log<level::DEBUG>("phosphor logging test with attribute",
64 entry("FILE_NAME_WITH_NUM_TEST=%s_%x", file_name, number));
65
66 // Now read back and verify our data made it into the journal
67 int rc = validate_journal("FILE_NAME_WITH_NUM_TEST",
68 "phosphor_logging_test.txt_fefe");
69 if(rc)
70 return(rc);
71
72 // TEST 3 - Create error log with 2 meta data fields (rvalue and lvalue)
73 number = 0x1234;
74 const char *test_string = "/tmp/test_string/";
Andrew Geissler6d910ad2016-10-16 20:49:14 -050075 try
76 {
Deepak Kodihalli160d3e02017-01-17 04:25:22 -060077 elog<example::xyz::openbmc_project::Example::TestErrorOne>(
78 example::xyz::openbmc_project::Example::TestErrorOne::
Andrew Geisslerdf048c12016-11-10 16:50:35 -060079 ERRNUM(number),
Deepak Kodihalli160d3e02017-01-17 04:25:22 -060080 example::xyz::openbmc_project::Example::TestErrorOne::
Andrew Geisslerdf048c12016-11-10 16:50:35 -060081 FILE_PATH(test_string),
Deepak Kodihalli160d3e02017-01-17 04:25:22 -060082 example::xyz::openbmc_project::Example::TestErrorOne::
Deepak Kodihallif2462f02017-01-19 03:40:12 -060083 FILE_NAME("elog_test_3.txt"),
84 example::xyz::openbmc_project::Example::TestErrorTwo::
Deepak Kodihalliac784cc2017-01-20 02:59:22 -060085 DEV_ADDR(0xDEADDEAD),
Deepak Kodihallif2462f02017-01-19 03:40:12 -060086 example::xyz::openbmc_project::Example::TestErrorTwo::
Deepak Kodihalliac784cc2017-01-20 02:59:22 -060087 DEV_ID(100),
Deepak Kodihallif2462f02017-01-19 03:40:12 -060088 example::xyz::openbmc_project::Example::TestErrorTwo::
89 DEV_NAME("test case 3"));
Andrew Geissler6d910ad2016-10-16 20:49:14 -050090 }
Deepak Kodihalli160d3e02017-01-17 04:25:22 -060091 catch (elogException<example::xyz::openbmc_project::Example::TestErrorOne>& e)
Andrew Geissler6d910ad2016-10-16 20:49:14 -050092 {
93 std::cout << "elog exception caught: " << e.what() << std::endl;
94 }
Andrew Geissler328889d2016-10-10 12:43:48 -050095
Andrew Geisslerdf048c12016-11-10 16:50:35 -060096 // Reduce our error namespaces
Deepak Kodihalli160d3e02017-01-17 04:25:22 -060097 using namespace example::xyz::openbmc_project::Example;
Andrew Geisslerdf048c12016-11-10 16:50:35 -060098
Andrew Geissler328889d2016-10-10 12:43:48 -050099 // Now read back and verify our data made it into the journal
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500100 std::stringstream stream;
101 stream << std::hex << number;
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600102 rc = validate_journal(TestErrorOne::ERRNUM::str_short,
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500103 std::string(stream.str()).c_str());
Andrew Geissler328889d2016-10-10 12:43:48 -0500104 if(rc)
105 return(rc);
106
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600107 rc = validate_journal(TestErrorOne::FILE_PATH::str_short,
Andrew Geissler328889d2016-10-10 12:43:48 -0500108 test_string);
109 if(rc)
110 return(rc);
111
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600112 rc = validate_journal(TestErrorOne::FILE_NAME::str_short,
Andrew Geissler328889d2016-10-10 12:43:48 -0500113 "elog_test_3.txt");
114 if(rc)
115 return(rc);
116
Deepak Kodihalliac784cc2017-01-20 02:59:22 -0600117 rc = validate_journal(TestErrorTwo::DEV_ADDR::str_short,
118 "0xDEADDEAD");
119 if(rc)
120 return(rc);
121
122 rc = validate_journal(TestErrorTwo::DEV_ID::str_short,
123 "100");
124 if(rc)
125 return(rc);
126
127 rc = validate_journal(TestErrorTwo::DEV_NAME::str_short,
128 "test case 3");
129 if(rc)
130 return(rc);
131
Andrew Geissler328889d2016-10-10 12:43:48 -0500132 // TEST 4 - Create error log with previous entry use
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500133 number = 0x9876;
Andrew Geissler6d910ad2016-10-16 20:49:14 -0500134 try
135 {
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600136 elog<TestErrorOne>(TestErrorOne::ERRNUM(number),
137 prev_entry<TestErrorOne::FILE_PATH>(),
Deepak Kodihallif2462f02017-01-19 03:40:12 -0600138 TestErrorOne::FILE_NAME("elog_test_4.txt"),
Deepak Kodihalliac784cc2017-01-20 02:59:22 -0600139 TestErrorTwo::DEV_ADDR(0xDEADDEAD),
140 TestErrorTwo::DEV_ID(100),
Deepak Kodihallif2462f02017-01-19 03:40:12 -0600141 TestErrorTwo::DEV_NAME("test case 4"));
Andrew Geissler6d910ad2016-10-16 20:49:14 -0500142 }
143 catch (elogExceptionBase& e)
144 {
145 std::cout << "elog exception caught: " << e.what() << std::endl;
146 }
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500147
Andrew Geissler328889d2016-10-10 12:43:48 -0500148 // Now read back and verify our data made it into the journal
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500149 stream.str("");
150 stream << std::hex << number;
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600151 rc = validate_journal(TestErrorOne::ERRNUM::str_short,
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500152 std::string(stream.str()).c_str());
Andrew Geissler328889d2016-10-10 12:43:48 -0500153 if(rc)
154 return(rc);
155
156 // This should just be equal to what we put in test 3
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600157 rc = validate_journal(TestErrorOne::FILE_PATH::str_short,
Andrew Geissler328889d2016-10-10 12:43:48 -0500158 test_string);
159 if(rc)
160 return(rc);
161
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600162 rc = validate_journal(TestErrorOne::FILE_NAME::str_short,
Andrew Geissler328889d2016-10-10 12:43:48 -0500163 "elog_test_4.txt");
164 if(rc)
165 return(rc);
166
Deepak Kodihalliac784cc2017-01-20 02:59:22 -0600167 rc = validate_journal(TestErrorTwo::DEV_ADDR::str_short,
168 "0xDEADDEAD");
169 if(rc)
170 return(rc);
171
172 rc = validate_journal(TestErrorTwo::DEV_ID::str_short,
173 "100");
174 if(rc)
175 return(rc);
176
177 rc = validate_journal(TestErrorTwo::DEV_NAME::str_short,
178 "test case 4");
179 if(rc)
180 return(rc);
181
Andrew Geissler328889d2016-10-10 12:43:48 -0500182 // Compile fail tests
183
184 // Simple test to prove we fail to compile due to missing param
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600185 //elog<TestErrorOne>(TestErrorOne::ERRNUM(1),
186 // TestErrorOne::FILE_PATH("test"));
Andrew Geissler328889d2016-10-10 12:43:48 -0500187
188 // Simple test to prove we fail to compile due to invalid param
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600189 //elog<TestErrorOne>(TestErrorOne::ERRNUM(1),
190 // TestErrorOne::FILE_PATH("test"),
191 // TestErrorOne::FILE_NAME(1));
Andrew Geissler328889d2016-10-10 12:43:48 -0500192
193 return 0;
194}
195
196