1 #include "config.h"
2 
3 #include "elog_serialize.hpp"
4 #include "log_manager.hpp"
5 #include "paths.hpp"
6 #include "xyz/openbmc_project/Common/error.hpp"
7 
8 #include <phosphor-logging/elog-errors.hpp>
9 #include <phosphor-logging/elog.hpp>
10 #include <phosphor-logging/log.hpp>
11 #include <sdbusplus/bus.hpp>
12 
13 #include <filesystem>
14 
15 #include <gmock/gmock.h>
16 #include <gtest/gtest.h>
17 
18 namespace phosphor
19 {
20 namespace logging
21 {
22 namespace internal
23 {
24 
25 namespace fs = std::filesystem;
26 
27 class journalInterface
28 {
29   public:
30     virtual ~journalInterface() = default;
31 
32     virtual void journalSync() = 0;
33     virtual int sd_journal_open(sd_journal** j, int k) = 0;
34     virtual int sd_journal_get_data(sd_journal* j, const char* transactionIdVar,
35                                     const void** data, size_t length) = 0;
36     virtual void sd_journal_close(sd_journal* j) = 0;
37 };
38 
39 class journalImpl : public journalInterface
40 {
41   public:
42     void journalSync() override;
43     int sd_journal_open(sd_journal** j, int k) override;
44     int sd_journal_get_data(sd_journal* j, const char* transactionIdVar,
45                             const void** data, size_t length) override;
46     void sd_journal_close(sd_journal* j) override;
47 };
48 
sd_journal_open(sd_journal **,int)49 int journalImpl::sd_journal_open(sd_journal**, int)
50 {
51     return 1;
52 }
53 
journalSync()54 void journalImpl::journalSync()
55 {
56     return;
57 }
58 
sd_journal_get_data(sd_journal *,const char *,const void **,size_t)59 int journalImpl::sd_journal_get_data(sd_journal*, const char*, const void**,
60                                      size_t)
61 {
62     return 1;
63 }
64 
sd_journal_close(sd_journal *)65 void journalImpl::sd_journal_close(sd_journal*)
66 {
67     return;
68 }
69 
70 class MockJournal : public Manager
71 {
72   public:
MockJournal(sdbusplus::bus_t & bus,const char * objPath)73     MockJournal(sdbusplus::bus_t& bus, const char* objPath) :
74         Manager(bus, objPath) {};
75     MOCK_METHOD0(journalSync, void());
76     MOCK_METHOD2(sd_journal_open, int(sd_journal**, int));
77     MOCK_METHOD4(sd_journal_get_data,
78                  int(sd_journal*, const char*, const void**, size_t));
79     MOCK_METHOD1(sd_journal_close, void(sd_journal*));
80 };
81 
82 class TestLogManager : public testing::Test
83 {
84   public:
85     sdbusplus::bus_t bus;
86     MockJournal manager;
TestLogManager()87     TestLogManager() :
88         bus(sdbusplus::bus::new_default()),
89         manager(bus, "/xyz/openbmc_test/abc")
90     {
91         fs::create_directories(paths::error());
92     }
93 
~TestLogManager()94     ~TestLogManager()
95     {
96         // Leave the directory as other testcases use it and they
97         // may be running in parallel from other jobs.
98     }
99 };
100 
101 } // namespace internal
102 } // namespace logging
103 } // namespace phosphor
104