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 "power_policy_plugin.h"
17 
18 #include <gtest/gtest.h>
19 
20 #include "edm_data_ability_utils_mock.h"
21 #include "edm_ipc_interface_code.h"
22 #include "iplugin_manager.h"
23 #include "plugin_singleton.h"
24 #include "utils.h"
25 
26 using namespace testing::ext;
27 using namespace testing;
28 
29 namespace OHOS {
30 namespace EDM {
31 namespace TEST {
32 
33 class PowerPolicyPluginTest : public testing::Test {
34 protected:
35     static void SetUpTestSuite(void);
36 
37     static void TearDownTestSuite(void);
38 };
39 
SetUpTestSuite(void)40 void PowerPolicyPluginTest::SetUpTestSuite(void)
41 {
42     Utils::SetEdmServiceEnable();
43     Utils::SetEdmInitialEnv();
44 }
45 
TearDownTestSuite(void)46 void PowerPolicyPluginTest::TearDownTestSuite(void)
47 {
48     Utils::SetEdmServiceDisable();
49     Utils::ResetTokenTypeAndUid();
50     ASSERT_TRUE(Utils::IsOriginalUTEnv());
51     std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
52 }
53 
54 /**
55  * @tc.name: TestOnHandlePolicyFuncCodeError
56  * @tc.desc: Test OnHandlePolicy error
57  * @tc.type: FUNC
58  */
59 HWTEST_F(PowerPolicyPluginTest, TestOnHandlePolicyFuncCodeError, TestSize.Level1)
60 {
61     std::shared_ptr<PowerPolicyPlugin> plugin = std::make_shared<PowerPolicyPlugin>();
62     MessageParcel data;
63     MessageParcel reply;
64     HandlePolicyData handlePolicyData{"TestString", false};
65     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, EdmInterfaceCode::POWER_POLICY);
66     ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
67     ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
68 }
69 
70 /**
71  * @tc.name: TestOnHandlePolicyPowerPolicyUnmarshallingError
72  * @tc.desc: Test OnHandlePolicy PowerPolicy Unmarshalling error
73  * @tc.type: FUNC
74  */
75 HWTEST_F(PowerPolicyPluginTest, TestOnHandlePolicyPowerPolicyUnmarshallingError, TestSize.Level1)
76 {
77     std::shared_ptr<PowerPolicyPlugin> plugin = std::make_shared<PowerPolicyPlugin>();
78     MessageParcel data;
79     data.WriteUint32(static_cast<uint32_t>(PowerScene::TIME_OUT));
80     data.WriteUint32(1000); // error action
81     data.WriteUint32(1000);
82     MessageParcel reply;
83     HandlePolicyData handlePolicyData{"TestString", false};
84     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::POWER_POLICY);
85     ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
86     ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
87 }
88 
89 /**
90  * @tc.name: TestOnHandlePolicyPowerSceneError
91  * @tc.desc: Test OnHandlePolicy Power scene error
92  * @tc.type: FUNC
93  */
94 HWTEST_F(PowerPolicyPluginTest, TestOnHandlePolicyPowerSceneError, TestSize.Level1)
95 {
96     std::shared_ptr<PowerPolicyPlugin> plugin = std::make_shared<PowerPolicyPlugin>();
97     MessageParcel data;
98     data.WriteUint32(1000); // error number
99     PowerPolicy powerPolicy;
100     powerPolicy.Marshalling(data);
101     MessageParcel reply;
102     HandlePolicyData handlePolicyData{"TestString", false};
103     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::POWER_POLICY);
104     ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
105     ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
106 }
107 
108 /**
109  * @tc.name: TestOnHandlePolicyNormal
110  * @tc.desc: Test OnHandlePolicy normal
111  * @tc.type: FUNC
112  */
113 HWTEST_F(PowerPolicyPluginTest, TestOnHandlePolicyNormal, TestSize.Level1)
114 {
115     std::shared_ptr<PowerPolicyPlugin> plugin = std::make_shared<PowerPolicyPlugin>();
116     MessageParcel data;
117     data.WriteUint32(static_cast<uint32_t>(PowerScene::TIME_OUT));
118     PowerPolicy powerPolicy;
119     powerPolicy.Marshalling(data);
120     MessageParcel reply;
121     HandlePolicyData handlePolicyData{"TestString", false};
122     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::POWER_POLICY);
123     EdmDataAbilityUtils::SetResult("power_suspend_normal");
124     ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
125     ASSERT_TRUE(ret == ERR_OK);
126 }
127 
128 /**
129  * @tc.name: TestOnHandlePolicyJsonReaderError
130  * @tc.desc: Test OnHandlePolicy json reader error
131  * @tc.type: FUNC
132  */
133 HWTEST_F(PowerPolicyPluginTest, TestOnHandlePolicyJsonReaderError, TestSize.Level1)
134 {
135     std::shared_ptr<PowerPolicyPlugin> plugin = std::make_shared<PowerPolicyPlugin>();
136     MessageParcel data;
137     data.WriteUint32(static_cast<uint32_t>(PowerScene::TIME_OUT));
138     PowerPolicy powerPolicy;
139     powerPolicy.Marshalling(data);
140     MessageParcel reply;
141     HandlePolicyData handlePolicyData{"TestString", false};
142     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::POWER_POLICY);
143     EdmDataAbilityUtils::SetResult("power_suspend_json_error");
144     ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
145     ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
146 }
147 
148 /**
149  * @tc.name: TestOnHandlePolicyJsonNoPowerSceneKey
150  * @tc.desc: Test OnHandlePolicy json no power scene key
151  */
152 HWTEST_F(PowerPolicyPluginTest, TestOnHandlePolicyJsonNoPowerSceneKey, TestSize.Level1)
153 {
154     std::shared_ptr<PowerPolicyPlugin> plugin = std::make_shared<PowerPolicyPlugin>();
155     MessageParcel data;
156     data.WriteUint32(static_cast<uint32_t>(PowerScene::TIME_OUT));
157     PowerPolicy powerPolicy;
158     powerPolicy.Marshalling(data);
159     MessageParcel reply;
160     HandlePolicyData handlePolicyData{"TestString", false};
161     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::POWER_POLICY);
162     EdmDataAbilityUtils::SetResult("power_suspend_json_no_time_out");
163     ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
164     ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
165 }
166 
167 /**
168  * @tc.name: TestOnHandlePolicyPowerSceneKeyNotObject
169  * @tc.desc: Test OnHandlePolicy power scene not object
170  * @tc.type: FUNC
171  */
172 HWTEST_F(PowerPolicyPluginTest, TestOnHandlePolicyPowerSceneKeyNotObject, TestSize.Level1)
173 {
174     std::shared_ptr<PowerPolicyPlugin> plugin = std::make_shared<PowerPolicyPlugin>();
175     MessageParcel data;
176     data.WriteUint32(static_cast<uint32_t>(PowerScene::TIME_OUT));
177     PowerPolicy powerPolicy;
178     powerPolicy.Marshalling(data);
179     MessageParcel reply;
180     HandlePolicyData handlePolicyData{"TestString", false};
181     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::POWER_POLICY);
182     EdmDataAbilityUtils::SetResult("power_suspend_json_key_not_object");
183     ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
184     ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
185 }
186 
187 /**
188  * @tc.name: TestOnGetPolicyPowerSceneError
189  * @tc.desc: Test OnGetPolicy power scene error
190  * @tc.type: FUNC
191  */
192 HWTEST_F(PowerPolicyPluginTest, TestOnGetPolicyPowerSceneError, TestSize.Level1)
193 {
194     std::shared_ptr<PowerPolicyPlugin> plugin = std::make_shared<PowerPolicyPlugin>();
195     MessageParcel data;
196     data.WriteUint32(1000); // error param
197     MessageParcel reply;
198     std::string policyData{"TestString"};
199     ErrCode ret = plugin->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
200     int32_t replyCode = reply.ReadInt32();
201     ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
202     ASSERT_TRUE(replyCode == EdmReturnErrCode::PARAM_ERROR);
203 }
204 
205 /**
206  * @tc.name: TestOnGetPolicyNormal
207  * @tc.desc: Test OnGetPolicy normal
208  * @tc.type: FUNC
209  */
210 HWTEST_F(PowerPolicyPluginTest, TestOnGetPolicyNormal, TestSize.Level1)
211 {
212     std::shared_ptr<PowerPolicyPlugin> plugin = std::make_shared<PowerPolicyPlugin>();
213     MessageParcel data;
214     data.WriteUint32(static_cast<uint32_t>(PowerScene::TIME_OUT));
215     MessageParcel reply;
216     std::string policyData{"TestString"};
217     EdmDataAbilityUtils::SetResult("power_suspend_normal");
218     ErrCode ret = plugin->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
219     int32_t replyCode = reply.ReadInt32();
220     ASSERT_TRUE(ret == ERR_OK);
221     ASSERT_TRUE(replyCode == ERR_OK);
222     PowerPolicy powerPolicy;
223     bool unmar = PowerPolicy::Unmarshalling(reply, powerPolicy);
224     ASSERT_TRUE(unmar);
225 }
226 
227 /**
228  * @tc.name: TestOnGetPolicyPowerSceneKeyNotObject
229  * @tc.desc: Test OnGetPolicy power scene key not object
230  * @tc.type: FUNC
231  */
232 HWTEST_F(PowerPolicyPluginTest, TestOnGetPolicyPowerSceneKeyNotObject, TestSize.Level1)
233 {
234     std::shared_ptr<PowerPolicyPlugin> plugin = std::make_shared<PowerPolicyPlugin>();
235     MessageParcel data;
236     data.WriteUint32(static_cast<uint32_t>(PowerScene::TIME_OUT));
237     MessageParcel reply;
238     std::string policyData{"TestString"};
239     EdmDataAbilityUtils::SetResult("power_suspend_json_key_not_object");
240     ErrCode ret = plugin->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
241     int32_t replyCode = reply.ReadInt32();
242     ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
243     ASSERT_TRUE(replyCode == EdmReturnErrCode::SYSTEM_ABNORMALLY);
244 }
245 } // namespace TEST
246 } // namespace EDM
247 } // namespace OHOS