• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "dfx_log_dump_unittest.h"
17 
18 namespace OHOS {
19 namespace Media {
20 using namespace std;
21 using namespace testing;
22 using namespace testing::ext;
23 
24 static const int32_t NUM_0 = 0;
25 static const int32_t NUM_1 = 1;
26 static const int32_t NUM_10 = 10;
27 static const int32_t NUM_100 = 100;
28 static const int32_t NUM_123 = 123;
29 static const int32_t NUM_2048 = 2048;
30 static const int32_t NUM_50000 = 50000;
31 
MakeLabel()32 static OHOS::HiviewDFX::HiLogLabel MakeLabel()
33 {
34     return {LOG_CORE, 0, "TestTag"};
35 }
36 
SetUpTestCase(void)37 void DfxLogDumpUnitTest::SetUpTestCase(void) {}
38 
TearDownTestCase(void)39 void DfxLogDumpUnitTest::TearDownTestCase(void) {}
40 
SetUp(void)41 void DfxLogDumpUnitTest::SetUp(void)
42 {
43     dump_ = std::make_shared<DfxLogDump>();
44 }
45 
TearDown(void)46 void DfxLogDumpUnitTest::TearDown(void)
47 {
48     dump_ = nullptr;
49 }
50 
51 /**
52  * @tc.name  : Test SaveLog
53  * @tc.number: SaveLog_001
54  * @tc.desc  : Test dtsPos != std::string::npos
55  *             Test ret < 0
56  *             Test ret >= 0
57  *             Test lineCount_ >= FILE_LINE_MAX
58  */
59 HWTEST_F(DfxLogDumpUnitTest, SaveLog_001, TestSize.Level0)
60 {
61     ASSERT_NE(dump_, nullptr);
62 
63     // Test dtsPos != std::string::npos
64     // Test ret >= 0
65     dump_->isEnable_ = true;
66     dump_->lineCount_ = NUM_0;
67     dump_->logString_.clear();
68     dump_->SaveLog("I", MakeLabel(), "test {public} log %d", NUM_123);
69     EXPECT_FALSE(dump_->logString_.empty());
70     EXPECT_EQ(dump_->lineCount_, NUM_1);
71 
72     // Test ret < 0
73     char longFmt[NUM_2048] = {NUM_0};
74     std::fill_n(longFmt, sizeof(longFmt) - 1, 'A');
75     longFmt[sizeof(longFmt) - NUM_1] = '\0';
76     dump_->SaveLog("E", MakeLabel(), longFmt);
77     EXPECT_NE(dump_->logString_.find("dump log error"), std::string::npos);
78 
79     // Test lineCount_ >= FILE_LINE_MAX
80     constexpr int32_t FILE_LINE_MAX = NUM_50000;
81     dump_->lineCount_ = FILE_LINE_MAX - NUM_1;
82     dump_->SaveLog("W", MakeLabel(), "last line");
83     EXPECT_EQ(dump_->lineCount_, FILE_LINE_MAX);
84 }
85 
86 /**
87  * @tc.name  : Test UpdateCheckEnable
88  * @tc.number: UpdateCheckEnable_001
89  * @tc.desc  : Test ofStream.is_open() == true
90  */
91 HWTEST_F(DfxLogDumpUnitTest, DoPause_001, TestSize.Level0)
92 {
93     ASSERT_NE(dump_, nullptr);
94     system("mkdir -p /data/test/log");
95     dump_->isEnable_ = false;
96     dump_->UpdateCheckEnable();
97     EXPECT_TRUE(dump_->isEnable_);
98 }
99 
100 /**
101  * @tc.name  : Test TaskProcessor
102  * @tc.number: TaskProcessor_001
103  * @tc.desc  : Test isNewFile_ == false
104  *             Test lineCount < FILE_LINE_MAX
105  */
106 HWTEST_F(DfxLogDumpUnitTest, TaskProcessor_001, TestSize.Level0)
107 {
108     ASSERT_NE(dump_, nullptr);
109 
110     // Test isNewFile_ == false
111     dump_->isNewFile_ = false;
112     dump_->fileCount_ = NUM_0;
113     // Test lineCount < FILE_LINE_MAX
114     dump_->lineCount_ = NUM_10;
115     dump_->logString_ = "test log\n";
116     dump_->isDump_ = true;
117     dump_->isExit_ = false;
118 
119     system("mkdir -p /data/test/log");
120 
__anonab3b8ea00102null121     std::thread t([this] {
122         std::this_thread::sleep_for(std::chrono::milliseconds(NUM_100));
123         dump_->isExit_ = true;
124         dump_->cond_.notify_all();
125     });
126     dump_->TaskProcessor();
127     t.join();
128 
129     EXPECT_FALSE(dump_->isNewFile_);
130     EXPECT_EQ(dump_->fileCount_, NUM_0);
131 }
132 
133 } // namespace Media
134 } // namespace OHOS