From da820b126ec5dad9f2297306ee91722e858f085f Mon Sep 17 00:00:00 2001 From: jiangwei Date: Thu, 17 Jul 2025 19:48:13 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A2=9E=E5=8A=A0=E7=94=A8=E4=BE=8B?= =?UTF-8?q?=EF=BC=8C=E6=8F=90=E5=8D=87=E8=A6=86=E7=9B=96=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: jiangwei --- src/subcommand.cpp | 6 +- .../common/native/spe_decoder_test.cpp | 39 ++++++++ .../common/native/subcommand_record_test.cpp | 25 +++++ .../common/native/subcommand_test.cpp | 95 +++++++++++++++++++ 4 files changed, 160 insertions(+), 5 deletions(-) diff --git a/src/subcommand.cpp b/src/subcommand.cpp index 53da549..722d112 100644 --- a/src/subcommand.cpp +++ b/src/subcommand.cpp @@ -77,11 +77,7 @@ bool SubCommand::CheckRestartOption(std::string &appPackage, bool targetSystemWi printf("option --restart and -p/-a is conflict, please check usage\n"); return false; } - - if (!appPackage.empty()) { - return IsRestarted(appPackage); - } - return false; + return IsRestarted(appPackage); } bool SubCommand::HandleSubCommandExclude(const std::vector &excludeTids, const std::vector diff --git a/test/unittest/common/native/spe_decoder_test.cpp b/test/unittest/common/native/spe_decoder_test.cpp index b31f3b4..df3499b 100644 --- a/test/unittest/common/native/spe_decoder_test.cpp +++ b/test/unittest/common/native/spe_decoder_test.cpp @@ -445,6 +445,45 @@ HWTEST_F(SpeDecoderTest, TestSpeDumpRawData2, TestSize.Level1) 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; EXPECT_EQ(SpeDumpRawData(rawData, dataDize, 1, nullptr), true); } + +/** + * @tc.name: TestSpeGetEnd_NullPacket + * @tc.desc: test SpeGetEnd() packet is nullptr + * @tc.type: FUNC + */ +HWTEST_F(SpeDecoderTest, TestSpeGetEnd_NullPacket, TestSize.Level2) +{ + const unsigned char rawData[] = {0x01}; + size_t dataSize = 0; + + SpeDecoder* decoder = SpeDecoderDataNew(rawData, dataSize); + ASSERT_NE(decoder, nullptr); + + int ret = SpeDecode(decoder); + EXPECT_LT(ret, 0); + + SpeDecoderFree(decoder); +} + +/** + * @tc.name: TestSpeGetAlignment_AllBranches_Indirect + * @tc.desc: test SpeGetAlignment() buf is nullptr + * @tc.type: FUNC + */ +HWTEST_F(SpeDecoderTest, TestSpeDecoder_NullBufferHandling, TestSize.Level2) +{ + struct SpePkt packet; + packet.type = PERF_SPE_BAD; + packet.index = 0; + packet.payload = 0; + + const unsigned char* nullBuf = nullptr; + SpeDecoder* nullDecoder = SpeDecoderDataNew(nullBuf, 10); + ASSERT_EQ(nullDecoder, nullptr); + int ret = SpeDecode(nullDecoder); + EXPECT_EQ(ret, -1); + SpeDecoderFree(nullDecoder); +} } // namespace HiPerf } // namespace Developtools } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/common/native/subcommand_record_test.cpp b/test/unittest/common/native/subcommand_record_test.cpp index 6d4f8e4..6ca9907 100644 --- a/test/unittest/common/native/subcommand_record_test.cpp +++ b/test/unittest/common/native/subcommand_record_test.cpp @@ -2334,6 +2334,31 @@ HWTEST_F(SubCommandRecordTest, OutPutFileName, TestSize.Level2) EXPECT_EQ(CheckTraceCommandOutput("hiperf record -d 3 -a -o /data/log/hiperflog/perf.data", {"Invalid output file path, permission denied"}), true); } + +/** + * @tc.name: TestOnSubCommand_InputLengthExceedLimit + * @tc.type: FUNC + */ +HWTEST_F(SubCommandRecordTest, InputLengthExceedLimit, TestSize.Level2) +{ + std::string cmd = "hiperf"; + for (int i = 0; i < 128; i++) { // 128: input max length + cmd += " "; + cmd += std::to_string(i); + } + EXPECT_EQ(CheckTraceCommandOutput(cmd, + {"The number of input arguments exceeds the upper limit"}), true); +} + +/** + * @tc.name: TestOnSubCommand_InputEmpty + * @tc.type: FUNC + */ +HWTEST_F(SubCommandRecordTest, InputEmpty, TestSize.Level2) +{ + EXPECT_EQ(CheckTraceCommandOutput("hiperf", + {"no command input"}), true); +} } // namespace HiPerf } // namespace Developtools } // namespace OHOS diff --git a/test/unittest/common/native/subcommand_test.cpp b/test/unittest/common/native/subcommand_test.cpp index d399121..f63a5bc 100644 --- a/test/unittest/common/native/subcommand_test.cpp +++ b/test/unittest/common/native/subcommand_test.cpp @@ -37,6 +37,11 @@ public: { return HiperfError::NO_ERR; } + + bool IsRestarted(const std::string& app) + { + return false; + } }; void HiPerfSubcommandTest::SetUpTestCase() {} @@ -193,6 +198,96 @@ HWTEST_F(HiPerfSubcommandTest, TestRegisterSubCommand2, TestSize.Level1) EXPECT_EQ(SubCommand::RegisterSubCommand("null", func), true); EXPECT_EQ(SubCommand::RegisterSubCommand("null", func), false); } + +/** + * @tc.name: HandleSubCommandExclude_ConflictOptions + * @tc.desc: Verify return false when both excludeTids and excludeThreadNames are non-empty + * @tc.type: FUNC + */ +HWTEST_F(HiPerfSubcommandTest, HandleSubCommandExclude_ConflictOptions, TestSize.Level2) +{ + std::vector excludeTids = {123, 456}; + std::vector excludeThreadNames = {"threadA", "threadB"}; + std::vector selectTids = {123, 456, 789}; + SubcommandObj subCmd; + testing::internal::CaptureStdout(); + bool result = subCmd.HandleSubCommandExclude(excludeTids, excludeThreadNames, selectTids); + std::string output = testing::internal::GetCapturedStdout(); + EXPECT_FALSE(result); + EXPECT_THAT(output, testing::HasSubstr("option --exclude-thread and --exclude-tid is conflict")); +} + +/** + * @tc.name: HandleSubCommandExclude_EmptySelectTids + * @tc.desc: Verify return true when selectTids is empty with non-empty excludeTids + * @tc.type: FUNC + */ +HWTEST_F(HiPerfSubcommandTest, HandleSubCommandExclude_EmptySelectTids, TestSize.Level2) +{ + std::vector excludeTids = {123}; + std::vector excludeThreadNames; + std::vector selectTids; + SubcommandObj subCmd; + testing::internal::CaptureStdout(); + bool result = subCmd.HandleSubCommandExclude(excludeTids, excludeThreadNames, selectTids); + std::string output = testing::internal::GetCapturedStdout(); + EXPECT_TRUE(result); + EXPECT_THAT(output, testing::HasSubstr("No thread is Monitored")); +} + +/** + * @tc.name: HandleSubCommandExclude_ExcludeTids + * @tc.desc: Verify ExcludeTidsFromSelectTids is called when excludeTids is non-empty + * @tc.type: FUNC + */ +HWTEST_F(HiPerfSubcommandTest, HandleSubCommandExclude_ExcludeTids, TestSize.Level2) +{ + std::vector excludeTids = {123, 456}; + std::vector excludeThreadNames; + std::vector selectTids = {123, 456, 789, 1011}; + SubcommandObj subCmd; + bool result = subCmd.HandleSubCommandExclude(excludeTids, excludeThreadNames, selectTids); + EXPECT_TRUE(result); + std::vector expected = {789, 1011}; + EXPECT_EQ(selectTids, expected); +} + +/** + * @tc.name: ExcludeTidsFromSelectTids_NormalCase + * @tc.desc: Verify ExcludeTidsFromSelectTids via HandleSubCommandExclude with valid tids + * @tc.type: FUNC + */ +HWTEST_F(HiPerfSubcommandTest, ExcludeTidsFromSelectTids_NormalCase, TestSize.Level2) +{ + std::vector excludeTids = {100, 200}; + std::vector excludeNames; + std::vector selectTids = {100, 200, 300}; + SubcommandObj subCmd; + + bool result = subCmd.HandleSubCommandExclude(excludeTids, excludeNames, selectTids); + EXPECT_TRUE(result); + EXPECT_EQ(selectTids, std::vector({300})); +} + +/** + * @tc.name: ExcludeTidsFromSelectTids_NoMatch + * @tc.desc: Verify ExcludeTidsFromSelectTids outputs message when no tid matches + * @tc.type: FUNC + */ +HWTEST_F(HiPerfSubcommandTest, ExcludeTidsFromSelectTids_NoMatch, TestSize.Level2) +{ + std::vector excludeTids = {999}; + std::vector excludeNames; + std::vector selectTids = {100, 200}; + SubcommandObj subCmd; + + testing::internal::CaptureStdout(); + subCmd.HandleSubCommandExclude(excludeTids, excludeNames, selectTids); + std::string output = testing::internal::GetCapturedStdout(); + + EXPECT_THAT(output, testing::HasSubstr("No thread id 999 was found to exclude.")); + EXPECT_EQ(selectTids, std::vector({100, 200})); +} } // namespace HiPerf } // namespace Developtools } // namespace OHOS -- Gitee