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_client_api_test.h"
17 
18 #include "thermal_mgr_client.h"
19 
20 using namespace OHOS::PowerMgr;
21 using namespace OHOS;
22 using namespace testing::ext;
23 
24 namespace {
25 /*
26  * @tc.number: ThermalClientApiTest_001
27  * @tc.name: ThermalClientApiTest_001
28  * @tc.desc: Test possible security issue caused by ShellDump API
29  */
30 HWTEST_F(ThermalClientApiTest, ThermalClientApiTest_001, TestSize.Level0)
31 {
32     THERMAL_HILOGD(LABEL_TEST, "ThermalClientApiTest_001:: Test Start!!");
33     size_t size = 1000;
34     std::vector<std::string> cmdsList;
35     for (size_t i = 0; i < size; i++) {
36         std::string cmd = "test_cmd" + std::to_string(i);
37         cmdsList.push_back(cmd);
38     }
39     auto& thermalMgrClient = ThermalMgrClient::GetInstance();
40     std::string result = thermalMgrClient.Dump(cmdsList);
41 
42     THERMAL_HILOGD(LABEL_TEST, "result : %{public}s", result.c_str());
43     EXPECT_EQ(true, result == "remote error");
44     THERMAL_HILOGD(LABEL_TEST, "ThermalClientApiTest_001:: Test End!!");
45 }
46 
47 /*
48  * @tc.number: ThermalClientApiTest_002
49  * @tc.name: ThermalClientApiTest_002
50  * @tc.desc: Test possible security issue caused by ShellDump API
51  */
52 HWTEST_F(ThermalClientApiTest, ThermalClientApiTest_002, TestSize.Level0)
53 {
54     THERMAL_HILOGD(LABEL_TEST, "ThermalClientApiTest_002:: Test Start!!");
55     size_t size = 5;
56     std::vector<std::string> cmdsList;
57     for (size_t i = 0; i < size; i++) {
58         std::string cmd = "test_cmd" + std::to_string(i);
59         cmdsList.push_back(cmd);
60     }
61     auto& thermalMgrClient = ThermalMgrClient::GetInstance();
62     std::string result = thermalMgrClient.Dump(cmdsList);
63 
64     THERMAL_HILOGD(LABEL_TEST, "result : %{public}s", result.c_str());
65     EXPECT_EQ(true, result.empty());
66     THERMAL_HILOGD(LABEL_TEST, "ThermalClientApiTest_002:: Test End!!");
67 }
68 
69 /*
70  * @tc.number: ThermalClientApiTest_003
71  * @tc.name: ThermalClientApiTest_003
72  * @tc.desc: Test get thermal level
73  */
74 HWTEST_F(ThermalClientApiTest, ThermalClientApiTest_003, TestSize.Level0)
75 {
76     THERMAL_HILOGD(LABEL_TEST, "ThermalClientApiTest_003:: Test Start!!");
77 
78     auto& thermalMgrClient = ThermalMgrClient::GetInstance();
79     ThermalLevel level = thermalMgrClient.GetThermalLevel();
80 
81     EXPECT_EQ(true, level <= ThermalLevel::ESCAPE && level >= ThermalLevel::COOL);
82     THERMAL_HILOGD(LABEL_TEST, "ThermalClientApiTest_003:: Test End!!");
83 }
84 }