diff --git a/src/virtual_runtime.cpp b/src/virtual_runtime.cpp index 6dbdf836519062b3fedec09c5d342610c8bb0431..acb054f55e908ef280bfe9d3f471b64105a55663 100644 --- a/src/virtual_runtime.cpp +++ b/src/virtual_runtime.cpp @@ -64,6 +64,9 @@ std::string VirtualRuntime::ReadFromSavedCmdLines(pid_t tid) std::string line; std::string threadid = std::to_string(tid); while (getline(savedCmdLines_, line)) { +#ifdef TEST_LINE_NULL + line = ""; +#endif if (line.find(threadid) != std::string::npos) { constexpr size_t sizeLimit {2}; std::vector linesToken = StringSplit(line, " "); diff --git a/test/unittest/common/native/virtual_runtime_test.cpp b/test/unittest/common/native/virtual_runtime_test.cpp index 9eec14de35091522eb1b92ae7708a29eb19ffbf7..f5a58847d58c0bf8ec6608be7b5651f68b3b04ae 100644 --- a/test/unittest/common/native/virtual_runtime_test.cpp +++ b/test/unittest/common/native/virtual_runtime_test.cpp @@ -398,6 +398,54 @@ HWTEST_F(VirtualRuntimeTest, Update, TestSize.Level1) runtime_->UpdateServiceSymbols(); runtime_->UpdateDevhostSymbols(); } + +/** + * @tc.name: ReadFromSavedCmdLines + * @tc.desc: Test ReadFromSavedCmdLines fun + * @tc.type: FUNC + */ +HWTEST_F(VirtualRuntimeTest, ReadFromSavedCmdLines_line_null, TestSize.Level1) +{ + std::string testProcesses = "com.ohos.sceneboard"; + if (!CheckTestApp(testProcesses)) { + testProcesses = "com.ohos.launcher"; + } + FILE *fp = nullptr; + char buf[128] = {0}; // 128: buf size + std::string cmd = "pidof " + testProcesses; + fp = popen(cmd.c_str(), "r"); + ASSERT_EQ(fp != nullptr, true); + if (fgets(buf, sizeof(buf), fp) != nullptr) { +#define TEST_LINE_NULL + std::string res = runtime_->ReadFromSavedCmdLines(std::stoi(buf)); + printf("res: %s", res.c_str()); + } + pclose(fp); +} + +/** + * @tc.name: ReadFromSavedCmdLines + * @tc.desc: Test ReadFromSavedCmdLines fun + * @tc.type: FUNC + */ +HWTEST_F(VirtualRuntimeTest, ReadFromSavedCmdLines_threadid, TestSize.Level1) +{ + std::string testProcesses = "com.ohos.sceneboard"; + if (!CheckTestApp(testProcesses)) { + testProcesses = "com.ohos.launcher"; + } + FILE *fp = nullptr; + char buf[128] = {0}; // 128: buf size + std::string cmd = "pidof " + testProcesses; + fp = popen(cmd.c_str(), "r"); + ASSERT_EQ(fp != nullptr, true); + if (fgets(buf, sizeof(buf), fp) != nullptr) { +#define TEST_THREADID + std::string res = runtime_->ReadFromSavedCmdLines(std::stoi(buf)); + printf("res: %s", res.c_str()); + } + pclose(fp); +} } // namespace HiPerf } // namespace Developtools } // namespace OHOS