| 1 | // SPDX-FileCopyrightText: 2019-2026 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "journalbootwork.h" |
| 6 | #include "ut_stuballthread.h" |
| 7 | #include "qtcompat.h" |
| 8 | #include <stub.h> |
| 9 | |
| 10 | #include <DApplication> |
| 11 | |
| 12 | #include <QDebug> |
| 13 | #include <QThreadPool> |
| 14 | #include <QThread> |
| 15 | |
| 16 | #include <gtest/gtest.h> |
| 17 | #include <systemd/sd-journal.h> |
| 18 | |
| 19 | int stub_sd_journal_get_data_bootwork001(sd_journal *j, const char *field, const void **data, size_t *l) |
| 20 | { |
| 21 | return 0; |
| 22 | } |
| 23 | |
| 24 | QString stub_getReplaceColorStr_bootwork001(const char *d) |
| 25 | { |
| 26 | return "testaaaa"; |
| 27 | } |
| 28 | TEST(JournalBootWork_Constructor_UT, JournalBootWork_Constructor_UT_001) |
| 29 | { |
| 30 | JournalBootWork *p = new JournalBootWork(nullptr); |
| 31 | EXPECT_NE(p, nullptr); |
| 32 | p->deleteLater(); |
| 33 | } |
| 34 | TEST(JournalBootWork_Constructor_UT, JournalBootWork_Constructor_UT_002) |
| 35 | { |
| 36 | JournalBootWork *p = new JournalBootWork(QStringList() << "", nullptr); |
| 37 | EXPECT_NE(p, nullptr); |
| 38 | p->deleteLater(); |
| 39 | } |
| 40 | |
| 41 | TEST(JournalBootWork_Constructor_UT, JournalBootWork_Constructor_UT_003) |
| 42 | { |
| 43 | JournalBootWork *p = new JournalBootWork(QStringList() << "PRIORITY=1", nullptr); |
| 44 | EXPECT_NE(p, nullptr); |
| 45 | EXPECT_EQ(p->m_arg.contains("PRIORITY=1"), true); |
| 46 | p->deleteLater(); |
| 47 | } |
| 48 | |
| 49 | TEST(JournalBootWork_Destructor_UT, JournalBootWork_Destructor_UT_001) |
| 50 | { |
| 51 | JournalBootWork *p = new JournalBootWork(nullptr); |
| 52 | EXPECT_NE(p, nullptr); |
| 53 | delete p; |
| 54 | } |
| 55 | |
| 56 | TEST(JournalBootWork_stopWork_UT, JournalBootWork_stopWork_UT_001) |
| 57 | { |
| 58 | JournalBootWork *p = new JournalBootWork(nullptr); |
| 59 | EXPECT_NE(p, nullptr); |
| 60 | p->stopWork(); |
| 61 | EXPECT_EQ(p->m_canRun, false); |
| 62 | p->deleteLater(); |
| 63 | } |
| 64 | |
| 65 | TEST(JournalBootWork_getIndex_UT, JournalBootWork_getIndex_UT_001) |
| 66 | { |
| 67 | JournalBootWork *p = new JournalBootWork(nullptr); |
| 68 | EXPECT_NE(p, nullptr); |
| 69 | EXPECT_EQ(p->getIndex(), p->getPublicIndex()); |
| 70 | p->deleteLater(); |
| 71 | } |
| 72 | TEST(JournalBootWork_setArg_UT, JournalBootWork_setArg_UT_001) |
| 73 | { |
| 74 | JournalBootWork *p = new JournalBootWork(nullptr); |
| 75 | EXPECT_NE(p, nullptr); |
| 76 | QStringList testArg; |
| 77 | testArg << "PRIORITY=1"; |
| 78 | p->setArg(testArg); |
| 79 | EXPECT_EQ(p->m_arg.contains("PRIORITY=1"), true); |
| 80 | p->deleteLater(); |
| 81 | } |
| 82 | |
| 83 | TEST(JournalBootWork_setArg_UT, JournalBootWork_setArg_UT_002) |
| 84 | { |
| 85 | JournalBootWork *p = new JournalBootWork(nullptr); |
| 86 | EXPECT_NE(p, nullptr); |
| 87 | QStringList testArg; |
| 88 | p->setArg(testArg); |
| 89 | EXPECT_EQ(p->m_arg.isEmpty(), true); |
| 90 | p->deleteLater(); |
| 91 | } |
| 92 | |
| 93 | TEST(JournalBootWork_doWork_UT, JournalBootWork_doWork_UT) |
| 94 | { |
| 95 | Stub stub; |
| 96 | stub.set(sd_journal_get_data, stub_sd_journal_get_data_bootwork001); |
| 97 | stub.set(ADDR(JournalBootWork, getReplaceColorStr), stub_getReplaceColorStr_bootwork001); |
| 98 | JournalBootWork *p = new JournalBootWork(nullptr); |
| 99 | p->doWork(); |
| 100 | EXPECT_NE(p, nullptr); |
| 101 | p->deleteLater(); |
| 102 | } |
| 103 | |
| 104 | TEST(JournalBootWork_getReplaceColorStr_UT, JournalBootWork_getReplaceColorStr_UT) |
| 105 | { |
| 106 | JournalBootWork *p = new JournalBootWork(nullptr); |
| 107 | EXPECT_NE(p, nullptr); |
| 108 | EXPECT_EQ(p->getReplaceColorStr("\033[40;37m"), ""); |
| 109 | p->deleteLater(); |
| 110 | } |
| 111 | |
| 112 | TEST(JournalBootWork_getDateTimeFromStamp_UT, JournalBootWork_getDateTimeFromStamp_UT) |
| 113 | { |
| 114 | JournalBootWork *p = new JournalBootWork(nullptr); |
| 115 | EXPECT_NE(p, nullptr); |
| 116 | // QString s_data = "2020-10-20 10:29:55"; |
| 117 | QString s_dataLine = "1603160995838863"; |
| 118 | // EXPECT_EQ(p->getDateTimeFromStamp(s_dataLine), s_data) << "check the status after getDateTimeFromStamp()"; |
| 119 | EXPECT_NE(p->getDateTimeFromStamp(s_dataLine), nullptr) << "check the status after getDateTimeFromStamp()"; |
| 120 | // EXPECT_EQ(rs, true); |
| 121 | p->deleteLater(); |
| 122 | } |
| 123 | |
| 124 | TEST(JournalBootWork_initMap_UT, JournalBootWork_initMap_UT) |
| 125 | { |
| 126 | JournalBootWork *p = new JournalBootWork(nullptr); |
| 127 | EXPECT_NE(p, nullptr); |
| 128 | p->initMap(); |
| 129 | QMap<int, QString> map; |
| 130 | map.insert(0, Dtk::Widget::DApplication::translate("Level", "Emergency")); |
| 131 | map.insert(1, Dtk::Widget::DApplication::translate("Level", "Alert")); |
| 132 | map.insert(2, Dtk::Widget::DApplication::translate("Level", "Critical")); |
| 133 | map.insert(3, Dtk::Widget::DApplication::translate("Level", "Error")); |
| 134 | map.insert(4, Dtk::Widget::DApplication::translate("Level", "Warning")); |
| 135 | map.insert(5, Dtk::Widget::DApplication::translate("Level", "Notice")); |
| 136 | map.insert(6, Dtk::Widget::DApplication::translate("Level", "Info")); |
| 137 | map.insert(7, Dtk::Widget::DApplication::translate("Level", "Debug")); |
| 138 | EXPECT_EQ(map, p->m_map); |
| 139 | p->deleteLater(); |
| 140 | } |
| 141 | |
| 142 | TEST(JournalBootWork_i2str_UT, JournalBootWork_i2str_UT) |
| 143 | { |
| 144 | JournalBootWork *p = new JournalBootWork(nullptr); |
| 145 | EXPECT_NE(p, nullptr); |
| 146 | QMap<int, QString> map; |
| 147 | map.insert(0, Dtk::Widget::DApplication::translate("Level", "Emergency")); |
| 148 | map.insert(1, Dtk::Widget::DApplication::translate("Level", "Alert")); |
| 149 | map.insert(2, Dtk::Widget::DApplication::translate("Level", "Critical")); |
| 150 | map.insert(3, Dtk::Widget::DApplication::translate("Level", "Error")); |
| 151 | map.insert(4, Dtk::Widget::DApplication::translate("Level", "Warning")); |
| 152 | map.insert(5, Dtk::Widget::DApplication::translate("Level", "Notice")); |
| 153 | map.insert(6, Dtk::Widget::DApplication::translate("Level", "Info")); |
| 154 | map.insert(7, Dtk::Widget::DApplication::translate("Level", "Debug")); |
| 155 | foreach (int key, map.keys()) { |
| 156 | EXPECT_EQ(p->i2str(key), map.value(key)); |
| 157 | } |
| 158 | |
| 159 | p->deleteLater(); |
| 160 | } |
| 161 | |
| 162 | int stub_sd_journal_open(sd_journal **ret, int flags) |
| 163 | { |
| 164 | return -1; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | class JournalBootWork_UT : public testing::Test |
| 169 | { |
| 170 | public: |
| 171 | //添加日志 |
| 172 | static void SetUpTestCase() |
| 173 | { |
| 174 | qDebug() << "SetUpTestCase" << ENDL; |
| 175 | } |
| 176 | static void TearDownTestCase() |
| 177 | { |
| 178 | qDebug() << "TearDownTestCase" << ENDL; |
| 179 | } |
| 180 | void SetUp() //TEST跑之前会执行SetUp |
| 181 | { |
| 182 | m_bootWork = new JournalBootWork(nullptr); |
| 183 | qDebug() << "SetUp" << ENDL; |
| 184 | } |
| 185 | void TearDown() //TEST跑完之后会执行TearDown |
| 186 | { |
| 187 | delete m_bootWork; |
| 188 | } |
| 189 | JournalBootWork *m_bootWork; |
| 190 | }; |
| 191 | |
| 192 | |
| 193 | TEST_F(JournalBootWork_UT, testBootWork_UT001) |
| 194 | { |
| 195 | Stub stub; |
| 196 | stub.set(sd_journal_open, stub_sd_journal_open); |
| 197 | stub.set(sd_journal_seek_tail, stub_sd_journal_open); |
| 198 | stub.set(sd_journal_add_match, stub_sd_journal_open); |
| 199 | stub.set(sd_journal_add_conjunction, stub_sd_journal_open); |
| 200 | stub.set(sd_journal_get_data, stub_sd_journal_open); |
| 201 | m_bootWork->m_arg = QStringList() << "emg"<< "all"; |
| 202 | m_bootWork->doWork(); |
| 203 | EXPECT_EQ(m_bootWork->m_canRun, true); |
| 204 | EXPECT_EQ(m_bootWork->logList.count(), 0); |
| 205 | } |