• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "thermal_utils_test.h"
17 
18 #include "errors.h"
19 #include "file_operation.h"
20 #include "string_operation.h"
21 #include "thermal_hisysevent.h"
22 #include "thermal_log.h"
23 
24 using namespace testing::ext;
25 using namespace OHOS::PowerMgr;
26 using namespace OHOS;
27 using namespace std;
28 
29 namespace {
30 constexpr int32_t BUF_LEN = 64;
31 constexpr int32_t NUM_ZERO = 0;
32 }
33 
34 namespace {
35 /**
36  * @tc.name: ThermalUtilsTest001
37  * @tc.desc: utils test
38  * @tc.type: FUNC
39  * @tc.require: issueI5YZQ2
40  */
41 HWTEST_F(ThermalUtilsTest, ThermalUtilsTest001, TestSize.Level0)
42 {
43     THERMAL_HILOGI(LABEL_TEST, "ThermalUtilsTest001 function start!");
44     std::string dir = "";
45     char buf[BUF_LEN];
46     EXPECT_EQ(true, FileOperation::CreateNodeDir(dir) != ERR_OK);
47     EXPECT_EQ(true, FileOperation::CreateNodeFile(dir) != ERR_OK);
48     dir = "/data/thermaltest/";
49     EXPECT_EQ(true, FileOperation::CreateNodeDir(dir) == ERR_OK);
50     dir = "/data/thermaltest/thermaltest.txt";
51     EXPECT_EQ(true, FileOperation::CreateNodeFile(dir) == ERR_OK);
52     size_t size = 1;
53     EXPECT_EQ(true, FileOperation::WriteFile(dir, dir, size) == ERR_OK);
54     dir = "";
55     EXPECT_EQ(true, FileOperation::ReadFile(dir.c_str(), buf, size) != ERR_OK);
56     dir = "/data/thermaltest/thermaltest.txt";
57     EXPECT_EQ(true, FileOperation::ReadFile(dir.c_str(), buf, size) == ERR_OK);
58     dir = "";
59     EXPECT_EQ(true, FileOperation::WriteFile(dir, dir, size) != ERR_OK);
60     dir = "1";
61     EXPECT_EQ(1, FileOperation::ConvertInt(dir));
62     THERMAL_HILOGI(LABEL_TEST, "ThermalUtilsTest001 function end!");
63 }
64 
65 /**
66  * @tc.name: ThermalUtilsTest002
67  * @tc.desc: utils test
68  * @tc.type: FUNC
69  * @tc.require: issueIBYAS8
70  */
71 HWTEST_F(ThermalUtilsTest, ThermalUtilsTest002, TestSize.Level0)
72 {
73     THERMAL_HILOGI(LABEL_TEST, "ThermalUtilsTest002 function start!");
74     std::string dir = "";
75     char buf[BUF_LEN];
76     EXPECT_EQ(true, FileOperation::CreateNodeDir(dir) != ERR_OK);
77     EXPECT_EQ(true, FileOperation::CreateNodeFile(dir) != ERR_OK);
78     dir = "/data/thermaltest/";
79     EXPECT_EQ(true, FileOperation::CreateNodeDir(dir) == ERR_OK);
80     dir = "/data/thermaltest/thermaltest.txt";
81     EXPECT_EQ(true, FileOperation::CreateNodeFile(dir) == ERR_OK);
82     size_t size = dir.size()+1;
83     EXPECT_EQ(true, FileOperation::WriteFile(dir, dir, size) == ERR_OK);
84     EXPECT_EQ(true, FileOperation::ReadFile(dir.c_str(), buf, size) == ERR_OK);
85     EXPECT_EQ(true, dir.compare(buf) == NUM_ZERO);
86     dir = "";
87     EXPECT_EQ(true, FileOperation::ReadFile(dir.c_str(), buf, size) != ERR_OK);
88     EXPECT_EQ(true, FileOperation::WriteFile(dir, dir, size) != ERR_OK);
89     THERMAL_HILOGI(LABEL_TEST, "ThermalUtilsTest002 function end!");
90 }
91 
92 /**
93  * @tc.name: ThermalUtilsTest003
94  * @tc.desc: utils test
95  * @tc.require: issueI5YZQ2
96  * @tc.type: FUNC
97  */
98 HWTEST_F(ThermalUtilsTest, ThermalUtilsTest003, TestSize.Level0)
99 {
100     THERMAL_HILOGI(LABEL_TEST, "ThermalUtilsTest003 function start!");
101     std::string str;
102     std::vector<std::string> ret;
103     std::string sep;
104     StringOperation::SplitString(str, ret, sep);
105     StringOperation::ClearAllSpace(str);
106     str = "str";
107     uint32_t val = 1;
108     double value = 2;
109     StringOperation::StrToUint("", val);
110     StringOperation::StrToDouble("", value);
111     StringOperation::ClearAllSpace(str);
112     EXPECT_EQ(false, StringOperation::Compare(str, sep));
113     sep = "str";
114     EXPECT_EQ(true, StringOperation::Compare(str, sep));
115     EXPECT_EQ(true, StringOperation::Find(str, sep));
116     str = "";
117     EXPECT_EQ(false, StringOperation::Find(str, sep));
118     std::string configDir = "/data/service/el0/thermal/config/%s";
119     WriteLevelChangedHiSysEvent(true, 1);
120     WriteLevelChangedHiSysEvent(false, 1);
121     WriteActionTriggeredHiSysEvent(true, configDir, 1);
122     WriteActionTriggeredHiSysEvent(false, configDir, 1);
123     WriteActionTriggeredHiSysEventWithRatio(true, configDir, 1);
124     WriteActionTriggeredHiSysEventWithRatio(false, configDir, 1);
125     THERMAL_HILOGI(LABEL_TEST, "ThermalUtilsTest003 function end!");
126 }
127 
128 /**
129  * @tc.name: ThermalUtilsTest004
130  * @tc.desc: utils test ParseStrtollResult
131  * @tc.require: issueI5YZQ2
132  * @tc.type: FUNC
133  */
134 HWTEST_F(ThermalUtilsTest, ThermalUtilsTest004, TestSize.Level0)
135 {
136     THERMAL_HILOGI(LABEL_TEST, "ThermalUtilsTest004 function start!");
137     int64_t result = 0;
138     std::string strfirst = "";
139     EXPECT_EQ(false, StringOperation::ParseStrtollResult(strfirst, result));
140     result = 0;
141     std::string strsecond = "abc";
142     EXPECT_EQ(false, StringOperation::ParseStrtollResult(strsecond, result));
143     result = 0;
144     std::string strthird = "abc123";
145     EXPECT_EQ(false, StringOperation::ParseStrtollResult(strthird, result));
146     result = 0;
147     std::string strfourth = "123abc";
148     EXPECT_EQ(true, StringOperation::ParseStrtollResult(strfourth, result));
149     result = 0;
150     std::string strfifth = "123";
151     EXPECT_EQ(true, StringOperation::ParseStrtollResult(strfifth, result));
152     result = 0;
153     std::string strsixth = "12345678999987654321";
154     EXPECT_EQ(false, StringOperation::ParseStrtollResult(strsixth, result));
155     THERMAL_HILOGI(LABEL_TEST, "ThermalUtilsTest004 function end!");
156 }
157 
158 /**
159  * @tc.name: ThermalUtilsTest005
160  * @tc.desc: utils test ParseStrtoulResult
161  * @tc.require: issueI5YZQ2
162  * @tc.type: FUNC
163  */
164 HWTEST_F(ThermalUtilsTest, ThermalUtilsTest005, TestSize.Level0)
165 {
166     THERMAL_HILOGI(LABEL_TEST, "ThermalUtilsTest005 function start!");
167     unsigned long result = 0;
168     std::string strfirst = "";
169     EXPECT_EQ(false, StringOperation::ParseStrtoulResult(strfirst, result));
170     result = 0;
171     std::string strsecond = "abc";
172     EXPECT_EQ(false, StringOperation::ParseStrtoulResult(strsecond, result));
173     result = 0;
174     std::string strthird = "abc123";
175     EXPECT_EQ(false, StringOperation::ParseStrtoulResult(strthird, result));
176     result = 0;
177     std::string strfourth = "123abc";
178     EXPECT_EQ(true, StringOperation::ParseStrtoulResult(strfourth, result));
179     result = 0;
180     std::string strfifth = "123";
181     EXPECT_EQ(true, StringOperation::ParseStrtoulResult(strfifth, result));
182     result = 0;
183     std::string strsixth = "123456789999987654321";
184     EXPECT_EQ(false, StringOperation::ParseStrtoulResult(strsixth, result));
185     THERMAL_HILOGI(LABEL_TEST, "ThermalUtilsTest005 function end!");
186 }
187 } // namespace
188