xref: /openbmc/phosphor-event/test/utest.cpp (revision 74c09632)
1 #include "message.hpp"
2 #include <gtest/gtest.h>
3 #include <stdlib.h>
4 
5 namespace {
6     uint8_t p[] ={0x3, 0x32, 0x34, 0x36};
7 
build_event_record(const char * message,const char * severity,const char * association,const char * reportedby,const uint8_t * p,size_t n)8 event_record_t build_event_record(
9         const char* message,
10         const char* severity,
11         const char* association,
12         const char* reportedby,
13         const uint8_t* p,
14         size_t n)
15 {
16     return event_record_t{
17             const_cast<char*> (message),
18             const_cast<char*> (severity),
19             const_cast<char*> (association),
20             const_cast<char*> (reportedby),
21             const_cast<uint8_t*> (p),
22             n};
23 
24 }
25 }
26 
27 class TestEnv : public testing::Test
28 {
29 public:
30     char eventsDir[15] = "./eventsXXXXXX";
31     char *eventsDirPoint = nullptr;
TestEnv()32     TestEnv() {
33         eventsDirPoint = mkdtemp(eventsDir);
34         if(eventsDirPoint == NULL)
35         {
36             throw std::bad_alloc();
37         }
38         CreateEventPath();
39     }
~TestEnv()40     ~TestEnv() {
41         RemoveEventPath();
42     }
CreateEventPath()43     void CreateEventPath()
44     {
45         char *cmd = nullptr;
46         int resultAsp = 0;
47         int resultSys = 0;
48         resultAsp = asprintf(&cmd, "exec rm -r %s 2> /dev/null", eventsDir);
49         if(resultAsp == -1){
50            throw std::bad_alloc();
51         }
52         resultSys = system(cmd);
53         if(resultSys != 0){
54            throw std::system_error();
55         }
56         resultAsp = asprintf(&cmd, "exec mkdir  %s 2> /dev/null", eventsDir);
57         if(resultAsp == -1){
58             throw std::bad_alloc();
59         }
60         resultSys = system(cmd);
61         if(resultSys != 0){
62             throw std::system_error();
63         }
64         free(cmd);
65     }
RemoveEventPath()66     void RemoveEventPath()
67     {
68         char *cmd = nullptr;
69         int resultAsp = 0;
70         int resultSys = 0;
71 
72         resultAsp = asprintf(&cmd, "exec rm -r %s 2> /dev/null", eventsDir);
73         if(resultAsp == -1){
74             throw std::bad_alloc();
75         }
76         resultSys = system(cmd);
77         if(resultSys != 0){
78             throw std::system_error();
79         }
80         free(cmd);
81     }
82 };
83 
84 class TestEventManager : public TestEnv
85 {
86 public:
87     event_manager eventManager;
88 
TestEventManager()89     TestEventManager()
90       : eventManager(eventsDir, 0, 0)
91     {
92         // empty
93     }
prepareEventLog1()94     uint16_t prepareEventLog1() {
95         auto rec = build_event_record("Testing Message1", "Info",
96                            "Association", "Test", p, 4);
97         return eventManager.create(&rec);
98     }
prepareEventLog2()99     uint16_t prepareEventLog2() {
100         auto rec = build_event_record("Testing Message2", "Info",
101                             "Association", "Test", p, 4);
102         return eventManager.create(&rec);
103     }
104 };
105 
TEST_F(TestEventManager,ConstructDestruct)106 TEST_F(TestEventManager, ConstructDestruct)
107 {
108 }
109 
TEST_F(TestEventManager,BuildEventLogZero)110 TEST_F(TestEventManager, BuildEventLogZero) {
111    EXPECT_EQ(0, eventManager.get_managed_size());
112    EXPECT_EQ(0, eventManager.next_log());
113    eventManager.next_log_refresh();
114    EXPECT_EQ(0, eventManager.next_log());
115    EXPECT_EQ(0, eventManager.latest_log_id());
116    EXPECT_EQ(0, eventManager.log_count());
117 }
118 
TEST_F(TestEventManager,BuildEventLogOne)119 TEST_F(TestEventManager, BuildEventLogOne) {
120    auto msgId = prepareEventLog1();
121    EXPECT_EQ(1,  msgId);
122    EXPECT_EQ(75, eventManager.get_managed_size());
123    EXPECT_EQ(1,  eventManager.log_count());
124    EXPECT_EQ(1,  eventManager.latest_log_id());
125    eventManager.next_log_refresh();
126    /* next_log() uses readdir which reads differently per
127       system so just make sure its not zero. */
128    EXPECT_NE(0,  eventManager.next_log());
129    eventManager.next_log_refresh();
130 }
131 
TEST_F(TestEventManager,BuildEventLogTwo)132 TEST_F(TestEventManager, BuildEventLogTwo) {
133    auto msgId = prepareEventLog1();
134    EXPECT_EQ(1, msgId);
135    msgId = prepareEventLog2();
136    EXPECT_EQ(2, msgId);
137    EXPECT_EQ(150, eventManager.get_managed_size());
138    EXPECT_EQ(2,   eventManager.log_count());
139    EXPECT_EQ(2,   eventManager.latest_log_id());
140    eventManager.next_log_refresh();
141    /* next_log() uses readdir which reads differently per
142       system so just make sure its not zero. */
143    EXPECT_NE(0,   eventManager.next_log());
144    EXPECT_NE(0,   eventManager.next_log());
145 }
146 
147 /* Read Log 1 and 2 */
TEST_F(TestEventManager,ReadLogs)148 TEST_F(TestEventManager, ReadLogs) {
149    auto msgId = prepareEventLog1();
150    EXPECT_EQ(1, msgId);
151    msgId = prepareEventLog2();
152    EXPECT_EQ(2, msgId);
153    event_record_t *prec;
154 
155    EXPECT_EQ(1, eventManager.open(1, &prec));
156    EXPECT_STREQ("Testing Message1", prec->message);
157    eventManager.close(prec);
158 
159    EXPECT_EQ(2, eventManager.open(2, &prec));
160    EXPECT_STREQ("Testing Message2", prec->message);
161    eventManager.close(prec);
162 }
163 
164 /* Lets delete the earlier log, then create a new event manager
165    the latest_log_id should still be 2 */
TEST_F(TestEventManager,DeleteLogOne)166 TEST_F(TestEventManager, DeleteLogOne) {
167    auto msgId = prepareEventLog1();
168    EXPECT_EQ(1, msgId);
169    msgId = prepareEventLog2();
170    EXPECT_EQ(2, msgId);
171    EXPECT_EQ(0, eventManager.remove(1));
172    EXPECT_EQ(75, eventManager.get_managed_size());
173 
174    event_manager eventq(eventsDir, 0, 0);
175    EXPECT_EQ(2, eventq.latest_log_id());
176    EXPECT_EQ(1, eventq.log_count());
177    eventManager.next_log_refresh();
178 }
179 
180 /* Travese log list stuff */
TEST_F(TestEventManager,TraverseLogListOne)181 TEST_F(TestEventManager, TraverseLogListOne) {
182    event_manager eventa(eventsDir, 0, 0);
183    EXPECT_EQ(0, eventa.next_log());
184 
185    auto rec = build_event_record("Testing list", "Info",
186                             "Association", "Test", p, 4);
187    EXPECT_EQ(1, eventa.create(&rec));
188    EXPECT_EQ(2, eventa.create(&rec));
189 
190    event_manager eventb(eventsDir, 0, 0);
191    /* next_log() uses readdir which reads differently per
192       system so just make sure its not zero. */
193    EXPECT_NE(0, eventb.next_log());
194 }
195 
TEST_F(TestEnv,MaxLimitSize76)196 TEST_F(TestEnv, MaxLimitSize76) {
197 
198    event_manager eventd(eventsDir, 75, 0);
199    auto rec = build_event_record("Testing Message1", "Info",
200                             "Association", "Test", p, 4);
201    EXPECT_EQ(0, eventd.create(&rec));
202 
203    event_manager evente(eventsDir, 76, 0);
204    rec = build_event_record("Testing Message1", "Info",
205                             "Association", "Test", p, 4);
206    EXPECT_EQ(1, evente.create(&rec));
207 }
208 
TEST_F(TestEnv,MaxLimitSize149)209 TEST_F(TestEnv, MaxLimitSize149) {
210    event_manager eventf(eventsDir, 149, 0);
211    auto rec = build_event_record("Testing Message1", "Info",
212                             "Association", "Test", p, 4);
213    EXPECT_EQ(1, eventf.create(&rec));
214    EXPECT_EQ(0, eventf.create(&rec));
215 }
216 
TEST_F(TestEnv,MaxLimitLog1)217 TEST_F(TestEnv, MaxLimitLog1) {
218    event_manager eventg(eventsDir, 300, 1);
219    auto rec = build_event_record("Testing Message1", "Info",
220                             "Association", "Test", p, 4);
221    EXPECT_EQ(1, eventg.create(&rec));
222    EXPECT_EQ(0, eventg.create(&rec));
223    EXPECT_EQ(1, eventg.log_count());
224 }
225 
TEST_F(TestEnv,MaxLimitLog3)226 TEST_F(TestEnv, MaxLimitLog3) {
227    event_manager eventh(eventsDir, 600, 3);
228    auto rec = build_event_record("Testing Message1", "Info",
229                             "Association", "Test", p, 4);
230    EXPECT_EQ(1, eventh.create(&rec));
231    EXPECT_EQ(2, eventh.create(&rec));
232    EXPECT_EQ(3, eventh.create(&rec));
233    EXPECT_EQ(0, eventh.create(&rec));
234    EXPECT_EQ(3, eventh.log_count());
235 }
236 
237 /* Create an abundence of logs, then restart with a limited set  */
238 /* You should not be able to create new logs until the log size  */
239 /* dips below the request number                                 */
TEST_F(TestEnv,CreateLogsRestartSetOne)240 TEST_F(TestEnv, CreateLogsRestartSetOne) {
241    event_manager eventi(eventsDir, 600, 3);
242    auto rec = build_event_record("Testing Message1", "Info",
243                             "Association", "Test", p, 4);
244    EXPECT_EQ(1, eventi.create(&rec));
245    EXPECT_EQ(2, eventi.create(&rec));
246    EXPECT_EQ(3, eventi.create(&rec));
247    EXPECT_EQ(0, eventi.create(&rec));
248    EXPECT_EQ(3, eventi.log_count());
249 
250    event_manager eventj(eventsDir, 600, 1);
251    EXPECT_EQ(3, eventj.log_count());
252    EXPECT_EQ(0, eventj.create(&rec));
253    EXPECT_EQ(3, eventj.log_count());
254 
255    /* Delete logs to dip below the requested limit */
256    EXPECT_EQ(0, eventj.remove(3));
257    EXPECT_EQ(2, eventj.log_count());
258    EXPECT_EQ(0, eventj.create(&rec));
259    EXPECT_EQ(0, eventj.remove(2));
260    EXPECT_EQ(1, eventj.log_count());
261    EXPECT_EQ(0, eventj.create(&rec));
262    EXPECT_EQ(0, eventj.remove(1));
263    EXPECT_EQ(0, eventj.log_count());
264    EXPECT_EQ(7, eventj.create(&rec));
265 }
266 
267 /* Create an abundence of logs, then restart with a limited set  */
268 /* You should not be able to create new logs until the log size  */
269 /* dips below the request number                                 */
TEST_F(TestEnv,CreateLogsRestartSetTwo)270 TEST_F(TestEnv, CreateLogsRestartSetTwo) {
271    event_manager eventk(eventsDir, 600, 100);
272    auto rec = build_event_record("Testing Message1", "Info",
273                             "Association", "Test", p, 4);
274    EXPECT_EQ(1, eventk.create(&rec));
275    EXPECT_EQ(2, eventk.create(&rec));
276    /* Now we have consumed 150 bytes */
277    event_manager eventl(eventsDir, 151, 100);
278    EXPECT_EQ(0, eventl.create(&rec));
279    EXPECT_EQ(0, eventl.remove(2));
280    EXPECT_EQ(4, eventl.create(&rec));
281 }
282