blob: 63b59b7180303814dde4f3ba6acc5b25a24b1044 [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 Geisslerc830e0f2016-10-18 12:51:29 -05008#include "elog-gen.hpp"
Andrew Geissler328889d2016-10-10 12:43:48 -05009
10using namespace phosphor;
11using namespace logging;
12
13// validate the journal metadata equals the input value
14int validate_journal(const char *i_entry, const char *i_value)
15{
16 sd_journal *journal;
17 const void *data;
18 size_t l;
19 int rc;
20 bool validated = false;
21
22 rc = sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY);
23 if (rc < 0) {
24 std::cerr << "Failed to open journal: " << strerror(-rc) << "\n";
25 return 1;
26 }
27 rc = sd_journal_query_unique(journal, i_entry);
28 if (rc < 0) {
29 std::cerr << "Failed to query journal: " << strerror(-rc) << "\n";
30 return 1;
31 }
32 SD_JOURNAL_FOREACH_UNIQUE(journal, data, l)
33 {
34 std::string journ_entry((const char*)data);
35 std::cout << journ_entry << "\n";
36 if(journ_entry.find(i_value) != std::string::npos)
37 {
38 std::cout << "We found it!\n";
39 validated = true;
40 break;
41 }
42 }
43
44 sd_journal_close(journal);
45
46 rc = (validated) ? 0 : 1;
47 if(rc)
48 {
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050049 std::cerr << "Failed to find " << i_entry << " with value " << i_value
50 <<" in journal!" << "\n";
Andrew Geissler328889d2016-10-10 12:43:48 -050051 }
52
53 return rc;
54}
55
56int main()
57{
58 // TEST 1 - Basic log
59 log<level::DEBUG>("Basic phosphor logging test");
60
61 // TEST 2 - Log with metadata field
62 const char *file_name = "phosphor_logging_test.txt";
63 int number = 0xFEFE;
64 log<level::DEBUG>("phosphor logging test with attribute",
65 entry("FILE_NAME_WITH_NUM_TEST=%s_%x", file_name, number));
66
67 // Now read back and verify our data made it into the journal
68 int rc = validate_journal("FILE_NAME_WITH_NUM_TEST",
69 "phosphor_logging_test.txt_fefe");
70 if(rc)
71 return(rc);
72
73 // TEST 3 - Create error log with 2 meta data fields (rvalue and lvalue)
74 number = 0x1234;
75 const char *test_string = "/tmp/test_string/";
Andrew Geissler6d910ad2016-10-16 20:49:14 -050076 try
77 {
Deepak Kodihalli160d3e02017-01-17 04:25:22 -060078 elog<example::xyz::openbmc_project::Example::TestErrorOne>(
79 example::xyz::openbmc_project::Example::TestErrorOne::
Andrew Geisslerdf048c12016-11-10 16:50:35 -060080 ERRNUM(number),
Deepak Kodihalli160d3e02017-01-17 04:25:22 -060081 example::xyz::openbmc_project::Example::TestErrorOne::
Andrew Geisslerdf048c12016-11-10 16:50:35 -060082 FILE_PATH(test_string),
Deepak Kodihalli160d3e02017-01-17 04:25:22 -060083 example::xyz::openbmc_project::Example::TestErrorOne::
Deepak Kodihallif2462f02017-01-19 03:40:12 -060084 FILE_NAME("elog_test_3.txt"),
85 example::xyz::openbmc_project::Example::TestErrorTwo::
Deepak Kodihalliac784cc2017-01-20 02:59:22 -060086 DEV_ADDR(0xDEADDEAD),
Deepak Kodihallif2462f02017-01-19 03:40:12 -060087 example::xyz::openbmc_project::Example::TestErrorTwo::
Deepak Kodihalliac784cc2017-01-20 02:59:22 -060088 DEV_ID(100),
Deepak Kodihallif2462f02017-01-19 03:40:12 -060089 example::xyz::openbmc_project::Example::TestErrorTwo::
90 DEV_NAME("test case 3"));
Andrew Geissler6d910ad2016-10-16 20:49:14 -050091 }
Deepak Kodihalli160d3e02017-01-17 04:25:22 -060092 catch (elogException<example::xyz::openbmc_project::Example::TestErrorOne>& e)
Andrew Geissler6d910ad2016-10-16 20:49:14 -050093 {
94 std::cout << "elog exception caught: " << e.what() << std::endl;
95 }
Andrew Geissler328889d2016-10-10 12:43:48 -050096
Andrew Geisslerdf048c12016-11-10 16:50:35 -060097 // Reduce our error namespaces
Deepak Kodihalli160d3e02017-01-17 04:25:22 -060098 using namespace example::xyz::openbmc_project::Example;
Andrew Geisslerdf048c12016-11-10 16:50:35 -060099
Andrew Geissler328889d2016-10-10 12:43:48 -0500100 // Now read back and verify our data made it into the journal
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500101 std::stringstream stream;
102 stream << std::hex << number;
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600103 rc = validate_journal(TestErrorOne::ERRNUM::str_short,
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500104 std::string(stream.str()).c_str());
Andrew Geissler328889d2016-10-10 12:43:48 -0500105 if(rc)
106 return(rc);
107
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600108 rc = validate_journal(TestErrorOne::FILE_PATH::str_short,
Andrew Geissler328889d2016-10-10 12:43:48 -0500109 test_string);
110 if(rc)
111 return(rc);
112
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600113 rc = validate_journal(TestErrorOne::FILE_NAME::str_short,
Andrew Geissler328889d2016-10-10 12:43:48 -0500114 "elog_test_3.txt");
115 if(rc)
116 return(rc);
117
Deepak Kodihalliac784cc2017-01-20 02:59:22 -0600118 rc = validate_journal(TestErrorTwo::DEV_ADDR::str_short,
119 "0xDEADDEAD");
120 if(rc)
121 return(rc);
122
123 rc = validate_journal(TestErrorTwo::DEV_ID::str_short,
124 "100");
125 if(rc)
126 return(rc);
127
128 rc = validate_journal(TestErrorTwo::DEV_NAME::str_short,
129 "test case 3");
130 if(rc)
131 return(rc);
132
Andrew Geissler328889d2016-10-10 12:43:48 -0500133 // TEST 4 - Create error log with previous entry use
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500134 number = 0x9876;
Andrew Geissler6d910ad2016-10-16 20:49:14 -0500135 try
136 {
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600137 elog<TestErrorOne>(TestErrorOne::ERRNUM(number),
138 prev_entry<TestErrorOne::FILE_PATH>(),
Deepak Kodihallif2462f02017-01-19 03:40:12 -0600139 TestErrorOne::FILE_NAME("elog_test_4.txt"),
Deepak Kodihalliac784cc2017-01-20 02:59:22 -0600140 TestErrorTwo::DEV_ADDR(0xDEADDEAD),
141 TestErrorTwo::DEV_ID(100),
Deepak Kodihallif2462f02017-01-19 03:40:12 -0600142 TestErrorTwo::DEV_NAME("test case 4"));
Andrew Geissler6d910ad2016-10-16 20:49:14 -0500143 }
144 catch (elogExceptionBase& e)
145 {
146 std::cout << "elog exception caught: " << e.what() << std::endl;
147 }
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500148
Andrew Geissler328889d2016-10-10 12:43:48 -0500149 // Now read back and verify our data made it into the journal
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500150 stream.str("");
151 stream << std::hex << number;
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600152 rc = validate_journal(TestErrorOne::ERRNUM::str_short,
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500153 std::string(stream.str()).c_str());
Andrew Geissler328889d2016-10-10 12:43:48 -0500154 if(rc)
155 return(rc);
156
157 // This should just be equal to what we put in test 3
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600158 rc = validate_journal(TestErrorOne::FILE_PATH::str_short,
Andrew Geissler328889d2016-10-10 12:43:48 -0500159 test_string);
160 if(rc)
161 return(rc);
162
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600163 rc = validate_journal(TestErrorOne::FILE_NAME::str_short,
Andrew Geissler328889d2016-10-10 12:43:48 -0500164 "elog_test_4.txt");
165 if(rc)
166 return(rc);
167
Deepak Kodihalliac784cc2017-01-20 02:59:22 -0600168 rc = validate_journal(TestErrorTwo::DEV_ADDR::str_short,
169 "0xDEADDEAD");
170 if(rc)
171 return(rc);
172
173 rc = validate_journal(TestErrorTwo::DEV_ID::str_short,
174 "100");
175 if(rc)
176 return(rc);
177
178 rc = validate_journal(TestErrorTwo::DEV_NAME::str_short,
179 "test case 4");
180 if(rc)
181 return(rc);
182
Andrew Geissler328889d2016-10-10 12:43:48 -0500183 // Compile fail tests
184
185 // Simple test to prove we fail to compile due to missing param
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600186 //elog<TestErrorOne>(TestErrorOne::ERRNUM(1),
187 // TestErrorOne::FILE_PATH("test"));
Andrew Geissler328889d2016-10-10 12:43:48 -0500188
189 // Simple test to prove we fail to compile due to invalid param
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600190 //elog<TestErrorOne>(TestErrorOne::ERRNUM(1),
191 // TestErrorOne::FILE_PATH("test"),
192 // TestErrorOne::FILE_NAME(1));
Andrew Geissler328889d2016-10-10 12:43:48 -0500193
194 return 0;
195}
196
197