Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 97 additions & 7 deletions tests/dbusproxy/ut_dldbusinterface.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
// SPDX-FileCopyrightText: 2019 - 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2019-2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "dldbusinterface.h"
#include "qtcompat.h"

#include <stub.h>

Check warning on line 8 in tests/dbusproxy/ut_dldbusinterface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <stub.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <QDebug>

Check warning on line 10 in tests/dbusproxy/ut_dldbusinterface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDebug> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QTemporaryFile>

Check warning on line 11 in tests/dbusproxy/ut_dldbusinterface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QTemporaryFile> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDBusUnixFileDescriptor>

Check warning on line 12 in tests/dbusproxy/ut_dldbusinterface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDBusUnixFileDescriptor> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <gtest/gtest.h>

Check warning on line 14 in tests/dbusproxy/ut_dldbusinterface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <gtest/gtest.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

class DlDbusinterface_UT : public testing::Test
{
public:
//添加日志
static void SetUpTestCase()

Check warning on line 19 in tests/dbusproxy/ut_dldbusinterface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'SetUpTestCase' is never used.
{
qDebug() << "SetUpTestCase" << ENDL;
}
Expand All @@ -23,23 +24,112 @@
{
qDebug() << "TearDownTestCase" << ENDL;
}
void SetUp() //TEST跑之前会执行SetUp
void SetUp()

Check warning on line 27 in tests/dbusproxy/ut_dldbusinterface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'SetUp' is never used.
{
m_interface = new DeepinLogviewerInterface("test", "test", QDBusConnection::systemBus());
qDebug() << "SetUp" << ENDL;
}
void TearDown() //TEST跑完之后会执行TearDown
void TearDown()

Check warning on line 32 in tests/dbusproxy/ut_dldbusinterface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'TearDown' is never used.
{
delete m_interface;
}
DeepinLogviewerInterface *m_interface;
};

TEST_F(DlDbusinterface_UT, Compare_UT)
// 构造/析构随 fixture 调用,此处显式验证 staticInterfaceName。
TEST_F(DlDbusinterface_UT, StaticInterfaceName)
{
EXPECT_STREQ(DeepinLogviewerInterface::staticInterfaceName(), "com.deepin.logviewer");
}

TEST_F(DlDbusinterface_UT, exitCode)
{
m_interface->exitCode();
m_interface->exportLog("path", "path1", false);
}

TEST_F(DlDbusinterface_UT, getFileInfo)
{
m_interface->getFileInfo("path", false);
}

TEST_F(DlDbusinterface_UT, getOtherFileInfo)
{
m_interface->getOtherFileInfo("path", false);
}

TEST_F(DlDbusinterface_UT, quit)
{
m_interface->quit();
m_interface->readLog("Test");
}

TEST_F(DlDbusinterface_UT, readLogByFilePath)
{
m_interface->readLog(QString("path"));
}

TEST_F(DlDbusinterface_UT, readLogByFd)
{
QDBusUnixFileDescriptor fd(::fileno(stdout));
m_interface->readLog(fd);
}

TEST_F(DlDbusinterface_UT, readLogLinesInRangeByFilePath)
{
m_interface->readLogLinesInRange(QString("path"), 0, 10, false);
}

TEST_F(DlDbusinterface_UT, readLogLinesInRangeByFd)
{
QDBusUnixFileDescriptor fd(::fileno(stdout));
m_interface->readLogLinesInRange(fd, 0, 10, false);
}

TEST_F(DlDbusinterface_UT, openLogStream)
{
m_interface->openLogStream("path");
}

TEST_F(DlDbusinterface_UT, readLogInStream)
{
m_interface->readLogInStream("token");
}

TEST_F(DlDbusinterface_UT, isFileExist)
{
m_interface->isFileExist("path");
}

TEST_F(DlDbusinterface_UT, getFileSize)
{
m_interface->getFileSize("path");
}

TEST_F(DlDbusinterface_UT, getLineCount)
{
m_interface->getLineCount("path");
}

TEST_F(DlDbusinterface_UT, executeCmd)
{
m_interface->executeCmd("cmd");
}

TEST_F(DlDbusinterface_UT, exportOpsLog)
{
QDBusUnixFileDescriptor fd(::fileno(stdout));
m_interface->exportOpsLog(fd);
}

// exportLog 成功打开目标文件分支(fileFd >= 0)。
TEST_F(DlDbusinterface_UT, exportLogWritablePath)
{
QTemporaryFile tmp;
ASSERT_TRUE(tmp.open());
m_interface->exportLog(tmp.fileName(), "path1", false);
}

// exportLog 目标文件打开失败分支(fileFd < 0)。
TEST_F(DlDbusinterface_UT, exportLogInvalidPath)
{
m_interface->exportLog("/nonexistent_dir_ut_abcdef/out.log", "path1", false);
}
69 changes: 67 additions & 2 deletions tests/src/ut_dbusmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,86 @@
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include <gtest/gtest.h>

Check warning on line 5 in tests/src/ut_dbusmanager.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <gtest/gtest.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include "../application/dbusmanager.h"

Check warning on line 6 in tests/src/ut_dbusmanager.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "../application/dbusmanager.h" not found.

#include <stub.h>

#include <QString>

// 1) 构造函数
TEST(DBusManager_Constructor_UT, DBusManager_Constructor_UT_001)
{
DBusManager *p = new DBusManager(nullptr);
EXPECT_NE(p, nullptr);
delete p;
}
TEST(DBusManager_GetSystemInfo_UT, DBusManager_GetSystemInfo_UT_001)

TEST(DBusManager_Constructor_UT, DBusManager_Constructor_UT_002)
{
DBusManager *p = new DBusManager(nullptr);
QObject parent;
DBusManager *p = new DBusManager(&parent);
EXPECT_NE(p, nullptr);
EXPECT_EQ(p->parent(), &parent);
// parent 接管删除,不再手动 delete
}

// 2) isSEOpen —— 无 DBus 服务环境下接口不可用,应返回 false
TEST(DBusManager_isSEOpen_UT, DBusManager_isSEOpen_UT_001)
{
bool rs = DBusManager::isSEOpen();
EXPECT_EQ(rs, false);
}

TEST(DBusManager_isSEOpen_UT, DBusManager_isSEOpen_UT_002)
{
DBusManager *p = new DBusManager(nullptr);
bool rs = p->isSEOpen();
EXPECT_FALSE(rs);
delete p;
}

// 3) isAuditAdmin —— 无 DBus 服务环境下接口不可用,应返回 false
TEST(DBusManager_isAuditAdmin_UT, DBusManager_isAuditAdmin_UT_001)
{
bool rs = DBusManager::isAuditAdmin();
EXPECT_EQ(rs, false);
}

TEST(DBusManager_isAuditAdmin_UT, DBusManager_isAuditAdmin_UT_002)
{
DBusManager *p = new DBusManager(nullptr);
bool rs = p->isAuditAdmin();
EXPECT_FALSE(rs);
delete p;
}

// 4) getHomePathByFreeDesktop —— 无 DBus 属性时返回空串
TEST(DBusManager_getHomePathByFreeDesktop_UT, DBusManager_getHomePathByFreeDesktop_UT_001)
{
QString home = DBusManager::getHomePathByFreeDesktop();
EXPECT_TRUE(home.isEmpty());
}

TEST(DBusManager_getHomePathByFreeDesktop_UT, DBusManager_getHomePathByFreeDesktop_UT_002)
{
DBusManager *p = new DBusManager(nullptr);
QString home = p->getHomePathByFreeDesktop();
EXPECT_EQ(home, QString());
delete p;
}

// 5) isScreenLocked —— 锁屏服务不可用时视为未锁屏,应返回 false
TEST(DBusManager_isScreenLocked_UT, DBusManager_isScreenLocked_UT_001)
{
bool rs = DBusManager::isScreenLocked();
EXPECT_EQ(rs, false);
}

TEST(DBusManager_isScreenLocked_UT, DBusManager_isScreenLocked_UT_002)
{
DBusManager *p = new DBusManager(nullptr);
bool rs = p->isScreenLocked();
EXPECT_FALSE(rs);
delete p;
}
Loading
Loading