#include #include #include #include #include #include #include class ProcessStatsTest : public ::testing::Test { protected: QList m_processes; void SetUp() override { ProcessStats::clearHistory(); } void TearDown() override { ProcessStats::clearHistory(); for (QProcess* process : m_processes) { if (process) { process->terminate(); process->waitForFinished(1000); delete process; } } m_processes.clear(); } }; // ============================================================================= // getProcessStats Tests // ============================================================================= // Verifies that getProcessStats() returns zeroed stats for negative PID TEST_F(ProcessStatsTest, GetProcessStats_ReturnsZeroedStatsForNegativePid) { ProcessStats::ProcessStatsData stats = ProcessStats::getProcessStats(-1); EXPECT_EQ(stats.cpuPercent, 0.0); EXPECT_EQ(stats.cpuTimeSeconds, 0.0); EXPECT_EQ(stats.memoryMB, 0.0); } // Verifies that getProcessStats() returns zeroed stats for zero PID TEST_F(ProcessStatsTest, GetProcessStats_ReturnsZeroedStatsForZeroPid) { ProcessStats::ProcessStatsData stats = ProcessStats::getProcessStats(0); EXPECT_EQ(stats.cpuPercent, 0.0); EXPECT_EQ(stats.cpuTimeSeconds, 0.0); EXPECT_EQ(stats.memoryMB, 0.0); } // Verifies that getProcessStats() returns valid stats for the current process TEST_F(ProcessStatsTest, GetProcessStats_ReturnsValidStatsForCurrentProcess) { qint64 currentPid = getpid(); ProcessStats::ProcessStatsData stats = ProcessStats::getProcessStats(currentPid); EXPECT_GT(stats.memoryMB, 0.0); EXPECT_GE(stats.cpuTimeSeconds, 0.0); } // Verifies that memory usage is non-negative for a valid process TEST_F(ProcessStatsTest, GetProcessStats_MemoryIsNonNegative) { qint64 currentPid = getpid(); ProcessStats::ProcessStatsData stats = ProcessStats::getProcessStats(currentPid); EXPECT_GE(stats.memoryMB, 0.0); } // Verifies that CPU time is non-negative for a valid process TEST_F(ProcessStatsTest, GetProcessStats_CpuTimeIsNonNegative) { qint64 currentPid = getpid(); ProcessStats::ProcessStatsData stats = ProcessStats::getProcessStats(currentPid); EXPECT_GE(stats.cpuTimeSeconds, 0.0); } // Verifies that CPU percent is zero on first call (no previous data) TEST_F(ProcessStatsTest, GetProcessStats_CpuPercentIsZeroOnFirstCall) { qint64 currentPid = getpid(); ProcessStats::ProcessStatsData stats = ProcessStats::getProcessStats(currentPid); EXPECT_EQ(stats.cpuPercent, 0.0); } // Verifies that CPU percent is calculated after the initial call TEST_F(ProcessStatsTest, GetProcessStats_CpuPercentUpdatesOnSecondCall) { qint64 currentPid = getpid(); ProcessStats::getProcessStats(currentPid); volatile double sum = 0.0; for (int i = 0; i < 1000000; ++i) { sum += i * 0.1; } usleep(10000); // 10ms ProcessStats::ProcessStatsData stats = ProcessStats::getProcessStats(currentPid); EXPECT_GE(stats.cpuPercent, 0.0); } // ============================================================================= // getModuleStats Tests // ============================================================================= // Verifies that getModuleStats() returns an empty JSON array when no plugins are loaded TEST_F(ProcessStatsTest, GetModuleStats_ReturnsEmptyArrayWhenNoPlugins) { QHash processes; char* result = ProcessStats::getModuleStats(processes); ASSERT_NE(result, nullptr); nlohmann::json doc = nlohmann::json::parse(result); EXPECT_TRUE(doc.is_array()); EXPECT_EQ(doc.size(), 0); delete[] result; } // Verifies that getModuleStats() returns a non-null pointer TEST_F(ProcessStatsTest, GetModuleStats_ReturnsNonNullPointer) { QHash processes; char* result = ProcessStats::getModuleStats(processes); ASSERT_NE(result, nullptr); delete[] result; } // Verifies that getModuleStats() returns valid JSON structure with correct fields TEST_F(ProcessStatsTest, GetModuleStats_ReturnsValidJsonStructure) { QProcess* dummyProcess = new QProcess(); m_processes.append(dummyProcess); dummyProcess->start("sleep", QStringList() << "1"); dummyProcess->waitForStarted(); qint64 pid = dummyProcess->processId(); ASSERT_GT(pid, 0); QHash processes; processes.insert("test_plugin", pid); char* result = ProcessStats::getModuleStats(processes); ASSERT_NE(result, nullptr); nlohmann::json doc = nlohmann::json::parse(result); EXPECT_TRUE(doc.is_array()); ASSERT_EQ(doc.size(), 1); auto moduleObj = doc[0]; EXPECT_TRUE(moduleObj.contains("name")); EXPECT_TRUE(moduleObj.contains("cpu_percent")); EXPECT_TRUE(moduleObj.contains("cpu_time_seconds")); EXPECT_TRUE(moduleObj.contains("memory_mb")); EXPECT_EQ(moduleObj["name"].get(), "test_plugin"); EXPECT_GE(moduleObj["cpu_percent"].get(), 0.0); EXPECT_GE(moduleObj["cpu_time_seconds"].get(), 0.0); EXPECT_GE(moduleObj["memory_mb"].get(), 0.0); delete[] result; }