From a6b427983b0d2eb1e4214becd70adc8d90fe015f Mon Sep 17 00:00:00 2001 From: wenlong12 Date: Fri, 26 Jul 2024 14:01:47 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E8=A1=A5=E5=85=85=E7=94=A8=E4=BE=8B=20Sign?= =?UTF-8?q?ed-off-by:wenlong12=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wenlong12 --- OAT.xml | 4 +- .../common/native/debug_logger_test.cpp | 6 ++ .../common/native/option_debug_test.cpp | 71 +++++++++++++++- test/unittest/common/native/register_test.cpp | 19 +++++ .../common/native/subcommand_report_test.cpp | 42 ++++++++++ .../common/native/subcommand_stat_test.cpp | 77 ++++++++++++++++++ test/unittest/resource/ohos_test.xml | 2 + .../resource/testdata/invalid.perf.data | 15 ++++ .../testdata/invalid_gzip_perf.data.tar.gz | Bin 0 -> 1954 bytes .../resource/testdata/invalid_perf.data | 15 ++++ .../testdata/invalid_perf.data.tar.gz | Bin 0 -> 2559 bytes .../resource/testdata/report_test.data | Bin 10538 -> 10538 bytes 12 files changed, 247 insertions(+), 4 deletions(-) create mode 100644 test/unittest/resource/testdata/invalid.perf.data create mode 100644 test/unittest/resource/testdata/invalid_gzip_perf.data.tar.gz create mode 100644 test/unittest/resource/testdata/invalid_perf.data create mode 100644 test/unittest/resource/testdata/invalid_perf.data.tar.gz diff --git a/OAT.xml b/OAT.xml index aa0b13c..ccb6670 100644 --- a/OAT.xml +++ b/OAT.xml @@ -56,7 +56,7 @@ Note:If the text contains special characters, please escape them according to th - + @@ -72,6 +72,8 @@ Note:If the text contains special characters, please escape them according to th + + diff --git a/test/unittest/common/native/debug_logger_test.cpp b/test/unittest/common/native/debug_logger_test.cpp index 91c4864..d3178c2 100644 --- a/test/unittest/common/native/debug_logger_test.cpp +++ b/test/unittest/common/native/debug_logger_test.cpp @@ -220,6 +220,7 @@ HWTEST_F(DebugLoggerTest, SetLogTags, TestSize.Level1) const std::string errorLogTag = "errtag"; const std::string errorLogTags = "errtag,errtag,errtag"; const std::string mixLogTags = "errtag,errtag,DebugTest,errtag"; + const std::string LogTags = "DebugTest:T,DebugTest:V"; DebugLogger::GetInstance()->SetLogTags("DebugTest"); @@ -242,6 +243,11 @@ HWTEST_F(DebugLoggerTest, SetLogTags, TestSize.Level1) EXPECT_EQ(DebugLogger::GetInstance()->ShouldLog(LEVEL_MUCH, "DebugTest"), true); EXPECT_EQ(DebugLogger::GetInstance()->ShouldLog(LEVEL_DEBUG, "DebugTest"), true); + DebugLogger::GetInstance()->SetLogTags(LogTags); + + EXPECT_EQ(DebugLogger::GetInstance()->ShouldLog(LEVEL_MUCH, "DebugTest"), false); + EXPECT_EQ(DebugLogger::GetInstance()->ShouldLog(LEVEL_VERBOSE, "DebugTest"), true); + // back to default DebugLogger::GetInstance()->SetLogTags(""); } diff --git a/test/unittest/common/native/option_debug_test.cpp b/test/unittest/common/native/option_debug_test.cpp index 8fef34b..3fcf31a 100644 --- a/test/unittest/common/native/option_debug_test.cpp +++ b/test/unittest/common/native/option_debug_test.cpp @@ -35,7 +35,7 @@ public: void SetUp(); void TearDown(); const std::string TEST_LOG_MESSAGE = ""; - void LogLevelTest(std::vector args, DebugLevel level); + void LogLevelTest(std::vector args, DebugLevel level, bool result = true); default_random_engine rnd_; }; @@ -63,11 +63,14 @@ void OptionDebugTest::TearDown() Option::ClearMainOptions(); } -void OptionDebugTest::LogLevelTest(std::vector args, const DebugLevel testlevel) +void OptionDebugTest::LogLevelTest(std::vector args, const DebugLevel testlevel, bool result) { // backup DebugLevel oldLevel = DebugLogger::GetInstance()->GetLogLevel(); - EXPECT_EQ(Command::DispatchCommands(args), true); + EXPECT_EQ(Command::DispatchCommands(args), result); + if (!result) { + return; + } const std::string logMessage = TEST_LOG_MESSAGE + std::to_string(rnd_()) + "_" + std::to_string(testlevel); @@ -139,6 +142,36 @@ HWTEST_F(OptionDebugTest, much, TestSize.Level1) LogLevelTest({"--much", TEST_CMD_NOTHING}, LEVEL_MUCH); } +/** + * @tc.name: undebug + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(OptionDebugTest, undebug, TestSize.Level1) +{ + LogLevelTest({"--debug"}, LEVEL_DEBUG, false); +} + +/** + * @tc.name: unverbose + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(OptionDebugTest, unverbose, TestSize.Level1) +{ + LogLevelTest({"--verbose"}, LEVEL_VERBOSE, false); +} + +/** + * @tc.name: unmuch + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(OptionDebugTest, unmuch, TestSize.Level1) +{ + LogLevelTest({"--much"}, LEVEL_MUCH, false); +} + /** * @tc.name: mixlog * @tc.desc: @@ -170,6 +203,16 @@ HWTEST_F(OptionDebugTest, logpath, TestSize.Level1) EXPECT_EQ(Command::DispatchCommands({"--logpath", DEFAULT_LOG_PATH, TEST_CMD_NOTHING}), true); } +/** + * @tc.name: unlogpath + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(OptionDebugTest, unlogpath, TestSize.Level1) +{ + EXPECT_EQ(Command::DispatchCommands({"--logpath"}), false); +} + /** * @tc.name: logtag * @tc.desc: @@ -181,6 +224,16 @@ HWTEST_F(OptionDebugTest, logtag, TestSize.Level1) LogLevelTest({"--logtag", "123", TEST_CMD_NOTHING}, DebugLogger::GetInstance()->GetLogLevel()); } +/** + * @tc.name: unlogtag + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(OptionDebugTest, unlogtag, TestSize.Level1) +{ + LogLevelTest({"--logtag"}, LEVEL_MUCH, false); +} + /** * @tc.name: logDisabled * @tc.desc: @@ -193,6 +246,18 @@ HWTEST_F(OptionDebugTest, logDisabled, TestSize.Level1) DebugLogger::GetInstance()->Disable(false); } +/** + * @tc.name: unlogDisabled + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(OptionDebugTest, unlogDisabled, TestSize.Level1) +{ + // no log will save in log file. + LogLevelTest({"--nodebug"}, LEVEL_FATAL, false); + DebugLogger::GetInstance()->Disable(false); +} + /** * @tc.name: hilog * @tc.desc: diff --git a/test/unittest/common/native/register_test.cpp b/test/unittest/common/native/register_test.cpp index 716c18c..82d569f 100644 --- a/test/unittest/common/native/register_test.cpp +++ b/test/unittest/common/native/register_test.cpp @@ -169,6 +169,25 @@ HWTEST_F(RegisterTest, RegisterGetName, TestSize.Level1) EXPECT_EQ(RegisterGetName(i).empty(), false); } } + +/** + * @tc.name: GetArchName + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(RegisterTest, GetArchName, TestSize.Level1) +{ + RegisterGetSP(ArchType::ARCH_ARM); + EXPECT_STREQ(GetArchName(ArchType::ARCH_ARM).c_str(), "ARM"); + RegisterGetSP(ArchType::ARCH_ARM64); + EXPECT_STREQ(GetArchName(ArchType::ARCH_ARM64).c_str(), "ARM64"); + RegisterGetSP(ArchType::ARCH_X86); + EXPECT_STREQ(GetArchName(ArchType::ARCH_X86).c_str(), "X86_32"); + RegisterGetSP(ArchType::ARCH_X86_64); + EXPECT_STREQ(GetArchName(ArchType::ARCH_X86_64).c_str(), "X86_64"); + RegisterGetSP(ArchType::ARCH_UNKNOWN); + EXPECT_STREQ(GetArchName(ArchType::ARCH_UNKNOWN).c_str(), "Unsupport"); +} } // namespace HiPerf } // namespace Developtools } // namespace OHOS diff --git a/test/unittest/common/native/subcommand_report_test.cpp b/test/unittest/common/native/subcommand_report_test.cpp index 1475090..903997f 100644 --- a/test/unittest/common/native/subcommand_report_test.cpp +++ b/test/unittest/common/native/subcommand_report_test.cpp @@ -205,6 +205,48 @@ HWTEST_F(SubCommandReportTest, TestOnSubCommand_i, TestSize.Level1) EXPECT_EQ(FileCompare(stringOut, targetFile), true); } +/** + * @tc.name: TestOnSubCommand_zip_fail + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(SubCommandReportTest, TestOnSubCommand_gzip_fail, TestSize.Level1) +{ + StdoutRecord stdoutRecord; + stdoutRecord.Start(); + const auto startTime = chrono::steady_clock::now(); + EXPECT_EQ(Command::DispatchCommand("report -i " + RESOURCE_PATH + "invalid_perf.data.tar.gz"), false); + const auto costMs = std::chrono::duration_cast( + chrono::steady_clock::now() - startTime); + EXPECT_LE(costMs.count(), DEFAULT_RUN_TIMEOUT_MS); + + std::string stringOut = stdoutRecord.Stop(); + if (HasFailure()) { + printf("output:\n%s", stringOut.c_str()); + } +} + +/** + * @tc.name: TestOnSubCommand_zip_fail1 + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(SubCommandReportTest, TestOnSubCommand_gzip_fail1, TestSize.Level1) +{ + StdoutRecord stdoutRecord; + stdoutRecord.Start(); + const auto startTime = chrono::steady_clock::now(); + EXPECT_EQ(Command::DispatchCommand("report -i " + RESOURCE_PATH + "invalid_gzip_perf.data.tar.gz"), false); + const auto costMs = std::chrono::duration_cast( + chrono::steady_clock::now() - startTime); + EXPECT_LE(costMs.count(), DEFAULT_RUN_TIMEOUT_MS); + + std::string stringOut = stdoutRecord.Stop(); + if (HasFailure()) { + printf("output:\n%s", stringOut.c_str()); + } +} + /** * @tc.name: TestOnSubCommand_i1 * @tc.desc: diff --git a/test/unittest/common/native/subcommand_stat_test.cpp b/test/unittest/common/native/subcommand_stat_test.cpp index 603fc5a..3ff4736 100644 --- a/test/unittest/common/native/subcommand_stat_test.cpp +++ b/test/unittest/common/native/subcommand_stat_test.cpp @@ -951,6 +951,83 @@ HWTEST_F(SubCommandStatTest, TestOnSubCommand_d4, TestSize.Level1) t1.join(); } + +/** + * @tc.name: TestOnSubCommand_d5 + * @tc.desc: -d + * @tc.type: FUNC + */ +HWTEST_F(SubCommandStatTest, TestOnSubCommand_d5, TestSize.Level1) +{ + int tid1 = 0; + std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1)); + while (tid1 == 0) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + + std::string cmdstr = "stat -p "; + cmdstr += std::to_string(tid1); + cmdstr += " -c 0 -d 3 --dumpoptions --per-core"; + + StdoutRecord stdoutRecord; + stdoutRecord.Start(); + const auto startTime = chrono::steady_clock::now(); + g_wait = true; + EXPECT_EQ(Command::DispatchCommand(cmdstr), true); + g_wait = false; + { + std::unique_lock lock(mtx); + cv.notify_all(); + } + const auto costMs = std::chrono::duration_cast( + chrono::steady_clock::now() - startTime); + EXPECT_LE(costMs.count(), defaultRunTimeoutMs); + + std::string stringOut = stdoutRecord.Stop(); + if (HasFailure()) { + printf("output:\n%s", stringOut.c_str()); + } + t1.join(); +} + +/** + * @tc.name: TestOnSubCommand_d6 + * @tc.desc: -d + * @tc.type: FUNC + */ +HWTEST_F(SubCommandStatTest, TestOnSubCommand_d6, TestSize.Level1) +{ + int tid1 = 0; + std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1)); + while (tid1 == 0) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + + std::string cmdstr = "stat -p "; + cmdstr += std::to_string(tid1); + cmdstr += " -c 0 -d 3 --dumpoptions --per-thread"; + + StdoutRecord stdoutRecord; + stdoutRecord.Start(); + const auto startTime = chrono::steady_clock::now(); + g_wait = true; + EXPECT_EQ(Command::DispatchCommand(cmdstr), true); + g_wait = false; + { + std::unique_lock lock(mtx); + cv.notify_all(); + } + const auto costMs = std::chrono::duration_cast( + chrono::steady_clock::now() - startTime); + EXPECT_LE(costMs.count(), defaultRunTimeoutMs); + + std::string stringOut = stdoutRecord.Stop(); + if (HasFailure()) { + printf("output:\n%s", stringOut.c_str()); + } + t1.join(); +} + /** * @tc.name: TestOnSubCommand_i * @tc.desc: -i diff --git a/test/unittest/resource/ohos_test.xml b/test/unittest/resource/ohos_test.xml index e3354ca..a17e162 100644 --- a/test/unittest/resource/ohos_test.xml +++ b/test/unittest/resource/ohos_test.xml @@ -79,6 +79,8 @@ diff --git a/test/unittest/common/native/subcommand_report_test.cpp b/test/unittest/common/native/subcommand_report_test.cpp index 903997f..e818133 100644 --- a/test/unittest/common/native/subcommand_report_test.cpp +++ b/test/unittest/common/native/subcommand_report_test.cpp @@ -206,7 +206,7 @@ HWTEST_F(SubCommandReportTest, TestOnSubCommand_i, TestSize.Level1) } /** - * @tc.name: TestOnSubCommand_zip_fail + * @tc.name: TestOnSubCommand_gzip_fail * @tc.desc: * @tc.type: FUNC */ @@ -227,7 +227,7 @@ HWTEST_F(SubCommandReportTest, TestOnSubCommand_gzip_fail, TestSize.Level1) } /** - * @tc.name: TestOnSubCommand_zip_fail1 + * @tc.name: TestOnSubCommand_gzip_fail1 * @tc.desc: * @tc.type: FUNC */ From b1b59cb25f060518467ef3c0956aae953d1b8048 Mon Sep 17 00:00:00 2001 From: wenlong_12 Date: Fri, 26 Jul 2024 08:12:30 +0000 Subject: [PATCH 6/7] =?UTF-8?q?=E8=A1=A5=E5=85=85=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wenlong_12 --- OAT.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/OAT.xml b/OAT.xml index b428bdf..0635876 100644 --- a/OAT.xml +++ b/OAT.xml @@ -72,8 +72,6 @@ Note:If the text contains special characters, please escape them according to th - - @@ -121,6 +119,8 @@ Note:If the text contains special characters, please escape them according to th + + From 103ffb6f554ddaec5e9ba0bbc1ff9df6ccb88050 Mon Sep 17 00:00:00 2001 From: wenlong_12 Date: Mon, 29 Jul 2024 06:35:37 +0000 Subject: [PATCH 7/7] =?UTF-8?q?=E8=A1=A5=E5=85=85=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wenlong_12 --- test/unittest/common/native/subcommand_stat_test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unittest/common/native/subcommand_stat_test.cpp b/test/unittest/common/native/subcommand_stat_test.cpp index 3ff4736..c5fb11c 100644 --- a/test/unittest/common/native/subcommand_stat_test.cpp +++ b/test/unittest/common/native/subcommand_stat_test.cpp @@ -962,7 +962,7 @@ HWTEST_F(SubCommandStatTest, TestOnSubCommand_d5, TestSize.Level1) int tid1 = 0; std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1)); while (tid1 == 0) { - std::this_thread::sleep_for(std::chrono::milliseconds(10)); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); // 10: sleep 10ms } std::string cmdstr = "stat -p "; @@ -1000,7 +1000,7 @@ HWTEST_F(SubCommandStatTest, TestOnSubCommand_d6, TestSize.Level1) int tid1 = 0; std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1)); while (tid1 == 0) { - std::this_thread::sleep_for(std::chrono::milliseconds(10)); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); // 10: sleep 10ms } std::string cmdstr = "stat -p ";