• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 <gtest/gtest.h>
17 
18 #define private   public
19 #define protected public
20 #include "thermal_dfx.h"
21 #undef private
22 #undef protected
23 #include "file_ex.h"
24 #include "thermal_log.h"
25 
26 using namespace OHOS::HDI;
27 using namespace OHOS::HDI::Thermal::V1_1;
28 using namespace testing::ext;
29 
30 class HdfThermalLogTest : public testing::Test {
31 public:
32     static bool CheckThread(const std::string &threadName);
33     static void TearDownTestCase();
34 };
35 
CheckThread(const std::string & threadName)36 bool HdfThermalLogTest::CheckThread(const std::string &threadName)
37 {
38     std::string file = "/data/local/tmp/psTp";
39     std::string cmd = "ps -T -p " + std::to_string(getpid()) + " > " + file;
40     system(cmd.c_str());
41     std::string content;
42     OHOS::LoadStringFromFile(file, content);
43     return (std::string::npos != content.find(threadName));
44 }
45 
TearDownTestCase()46 void HdfThermalLogTest::TearDownTestCase()
47 {
48     system("rm -rf /data/local/tmp/psTp");
49 }
50 
51 namespace {
52 constexpr int32_t DEFAULT_WIDTH = 20;
53 constexpr int32_t DEFAULT_INTERVAL = 5000;
54 constexpr int32_t MIN_INTERVAL = 100;
55 const std::string LOG_THREAD_NAME = "thermal_log";
56 } // namespace
57 
58 namespace {
59 /**
60  * @tc.name: HdfThermalLogTest001
61  * @tc.desc: Tests that the created thread is running properly
62  * @tc.type: FUNC
63  */
64 HWTEST_F(HdfThermalLogTest, HdfThermalLogTest001, TestSize.Level1)
65 {
66     THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest001: start.");
67     auto &hdfLog = ThermalDfx::GetInstance();
68     hdfLog.Init();
69     // thermal log off skipped tests
70     if (!hdfLog.enable_) {
71         ThermalDfx::DestroyInstance();
72         THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest001: thermal log off skipped tests.");
73         return;
74     }
75     ASSERT_TRUE(hdfLog.logThread_ != nullptr);
76     ASSERT_TRUE(hdfLog.logThread_->joinable());
77     ASSERT_TRUE(CheckThread(LOG_THREAD_NAME));
78 
79     ThermalDfx::DestroyInstance();
80     ASSERT_TRUE(hdfLog.logThread_ == nullptr);
81     ASSERT_FALSE(CheckThread(LOG_THREAD_NAME));
82     THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest001: end.");
83 }
84 
85 /**
86  * @tc.name: HdfThermalLogTest002
87  * @tc.desc: Tests that the GetIntParameter Limiting minimum
88  * @tc.type: FUNC
89  */
90 HWTEST_F(HdfThermalLogTest, HdfThermalLogTest002, TestSize.Level1)
91 {
92     THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest002: start.");
93     auto &hdfLog = ThermalDfx::GetInstance();
94     uint32_t def = 888;
95     // The obtained value is less than the default value. Return the default value
96     uint32_t minVal = hdfLog.width_ + 1;
97     uint32_t width = hdfLog.GetIntParameter("persist.thermal.log.width", def, minVal);
98     ASSERT_EQ(def, width);
99 
100     // The value obtained is greater than the value obtained by default
101     minVal = hdfLog.width_ - 1;
102     width = hdfLog.GetIntParameter("persist.thermal.log.width", def, minVal);
103     ASSERT_EQ(hdfLog.width_, width);
104     THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest002: end.");
105 }
106 
107 /**
108  * @tc.name: HdfThermalLogTest003
109  * @tc.desc: Tests that the WidthWatchCallback Limiting minimum
110  * @tc.type: FUNC
111  */
112 HWTEST_F(HdfThermalLogTest, HdfThermalLogTest003, TestSize.Level1)
113 {
114     THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest003: start.");
115     auto &hdfLog = ThermalDfx::GetInstance();
116     int32_t minVal = -5;
117     int32_t maxVal = DEFAULT_WIDTH + 10;
118     for (int32_t i = minVal; i < maxVal; ++i) {
119         std::string value = std::to_string(i);
120         hdfLog.WidthWatchCallback(value);
121         if (i <= DEFAULT_WIDTH) {
122             ASSERT_EQ(hdfLog.width_.load(), static_cast<uint8_t>(DEFAULT_WIDTH));
123         } else {
124             ASSERT_EQ(hdfLog.width_.load(), static_cast<uint8_t>(i));
125         }
126     }
127     THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest003: end.");
128 }
129 
130 /**
131  * @tc.name: HdfThermalLogTest004
132  * @tc.desc: Tests that the WidthWatchCallback abnormal value
133  * @tc.type: FUNC
134  */
135 HWTEST_F(HdfThermalLogTest, HdfThermalLogTest004, TestSize.Level1)
136 {
137     THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest004: start.");
138     auto &hdfLog = ThermalDfx::GetInstance();
139     std::vector<std::string> abnormal = {"", "abc", "123abc", "890,0"};
140     for (auto& it : abnormal) {
141         hdfLog.WidthWatchCallback(it);
142         ASSERT_EQ(hdfLog.width_.load(), static_cast<uint8_t>(DEFAULT_WIDTH)) <<
143             "HdfThermalLogTest004 failed value = " << it;
144     }
145     THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest004: end.");
146 }
147 
148 /**
149  * @tc.name: HdfThermalLogTest005
150  * @tc.desc: Tests that the IntervalWatchCallback Limiting minimum
151  * @tc.type: FUNC
152  */
153 HWTEST_F(HdfThermalLogTest, HdfThermalLogTest005, TestSize.Level1)
154 {
155     THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest005: start.");
156     auto &hdfLog = ThermalDfx::GetInstance();
157     int32_t minVal = -5;
158     int32_t maxVal = DEFAULT_INTERVAL + 10;
159     for (int32_t i = minVal; i < maxVal; ++i) {
160         std::string value = std::to_string(i);
161         hdfLog.IntervalWatchCallback(value);
162         if (i <= MIN_INTERVAL) {
163             ASSERT_EQ(hdfLog.interval_.load(), static_cast<uint32_t>(MIN_INTERVAL));
164         } else {
165             ASSERT_EQ(hdfLog.interval_.load(), static_cast<uint32_t>(i));
166         }
167     }
168     THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest005: end.");
169 }
170 
171 /**
172  * @tc.name: HdfThermalLogTest006
173  * @tc.desc: Tests that the IntervalWatchCallback abnormal value
174  * @tc.type: FUNC
175  */
176 HWTEST_F(HdfThermalLogTest, HdfThermalLogTest006, TestSize.Level1)
177 {
178     THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest006: start.");
179     auto &hdfLog = ThermalDfx::GetInstance();
180     std::vector<std::string> abnormal = {"", "abc", "123abc", "890,0"};
181     for (auto& it : abnormal) {
182         hdfLog.IntervalWatchCallback(it);
183         ASSERT_EQ(hdfLog.interval_.load(), static_cast<uint32_t>(DEFAULT_INTERVAL)) <<
184             "HdfThermalLogTest006 failed value = " << it;
185     }
186     THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest006: end.");
187 }
188 
189 /**
190  * @tc.name: HdfThermalLogTest007
191  * @tc.desc: Tests that the EnableWatchCallback The thread starts and stops normally
192  * @tc.type: FUNC
193  */
194 HWTEST_F(HdfThermalLogTest, HdfThermalLogTest007, TestSize.Level1)
195 {
196     THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest007: start.");
197     auto &hdfLog = ThermalDfx::GetInstance();
198     hdfLog.Init();
199     // thermal log off skipped tests
200     if (!hdfLog.enable_) {
201         ThermalDfx::DestroyInstance();
202         THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest007: thermal log off skipped tests.");
203         return;
204     }
205     // Check that the thread is running properly
206     ASSERT_TRUE(hdfLog.logThread_ != nullptr);
207     ASSERT_TRUE(hdfLog.logThread_->joinable());
208     ASSERT_TRUE(CheckThread(LOG_THREAD_NAME));
209     // Stop
210     hdfLog.EnableWatchCallback("false");
211     ASSERT_TRUE(hdfLog.logThread_ == nullptr);
212     ASSERT_FALSE(CheckThread(LOG_THREAD_NAME));
213     // Run
214     hdfLog.EnableWatchCallback("true");
215     ASSERT_TRUE(hdfLog.logThread_ != nullptr);
216     ASSERT_TRUE(hdfLog.logThread_->joinable());
217     ASSERT_TRUE(CheckThread(LOG_THREAD_NAME));
218     ThermalDfx::DestroyInstance();
219     THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest007: end.");
220 }
221 } // namespace
222