diff --git a/src/subcommand.cpp b/src/subcommand.cpp index 53da549c03687ed44ca4bffbffe15084befd204f..722d1129fb5fe1c11ea0ee6fd6fed52611b5ff2d 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 b31f3b46a36ec685b4f84d2f9eaac1ff73a6b759..df3499be258d2029f4cd2f9fcfcc7560a08a5370 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 6d4f8e4633167528d4b35cde5d55b73a9a261173..6ca99070f75664314366d24b71d7e5424a0dd564 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 d3991216009773d364533452ca0a7908795957b3..f63a5bcb8590244f9163ec6559ba9630e345a6e8 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