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 "switch_wifi_plugin_test.h"
17 #include "parameters.h"
18 #include "edm_ipc_interface_code.h"
19 #include "plugin_singleton.h"
20 #include "utils.h"
21
22 using namespace testing::ext;
23
24 namespace OHOS {
25 namespace EDM {
26 namespace TEST {
27 const std::string PARAM_FORCE_OPEN_WIFI = "persist.edm.force_open_wifi";
SetUpTestSuite(void)28 void SwitchWifiPluginTest::SetUpTestSuite(void)
29 {
30 Utils::SetEdmInitialEnv();
31 }
32
TearDownTestSuite(void)33 void SwitchWifiPluginTest::TearDownTestSuite(void)
34 {
35 Utils::ResetTokenTypeAndUid();
36 ASSERT_TRUE(Utils::IsOriginalUTEnv());
37 std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
38 }
39
40 /**
41 * @tc.name: TestForceTurnOnWifiSuccess
42 * @tc.desc: Test SwitchWifiPlugin::OnSetPolicy function sucess.
43 * @tc.type: FUNC
44 */
45 HWTEST_F(SwitchWifiPluginTest, TestForceTurnOnWifiSuccess, TestSize.Level1)
46 {
47 std::shared_ptr<IPlugin> plugin = SwitchWifiPlugin::GetPlugin();
48 uint32_t code = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::SWITCH_WIFI);
49 HandlePolicyData handlePolicyData{"false", "", false};
50 MessageParcel data;
51 MessageParcel reply;
52 data.WriteBool(true);
53 ErrCode ret = plugin->OnHandlePolicy(code, data, reply, handlePolicyData, DEFAULT_USER_ID);
54 ASSERT_TRUE(ret == ERR_OK);
55 }
56
57 /**
58 * @tc.name: TestSwitchWifiPluginOnAdminRemove
59 * @tc.desc: Test SwitchWifiPlugin::OnAdminRemove function
60 * @tc.type: FUNC
61 */
62 HWTEST_F(SwitchWifiPluginTest, TestSwitchWifiPluginOnAdminRemove, TestSize.Level1)
63 {
64 std::shared_ptr<IPlugin> plugin = SwitchWifiPlugin::GetPlugin();
65 std::string currentPolicy = "true";
66 std::string mergePolicy = "false";
67 ErrCode ret = plugin->OnAdminRemove("test", currentPolicy, mergePolicy, DEFAULT_USER_ID);
68 ASSERT_TRUE(ret == ERR_OK);
69 ASSERT_FALSE(OHOS::system::GetBoolParameter(PARAM_FORCE_OPEN_WIFI, true));
70 }
71
72 /**
73 * @tc.name: TestTurnOnWifiSuccess
74 * @tc.desc: Test SwitchWifiPlugin::OnSetPolicy function sucess.
75 * @tc.type: FUNC
76 */
77 HWTEST_F(SwitchWifiPluginTest, TestTurnOnWifiSuccess, TestSize.Level1)
78 {
79 std::shared_ptr<IPlugin> plugin = SwitchWifiPlugin::GetPlugin();
80 uint32_t code = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::SWITCH_WIFI);
81 HandlePolicyData handlePolicyData{"false", "", false};
82 MessageParcel data;
83 MessageParcel reply;
84 data.WriteBool(false);
85 ErrCode ret = plugin->OnHandlePolicy(code, data, reply, handlePolicyData, DEFAULT_USER_ID);
86 ASSERT_TRUE(ret == ERR_OK);
87 }
88
89 /**
90 * @tc.name: TestTurnOffWifiSuccess
91 * @tc.desc: Test SwitchWifiPlugin::OnSetPolicy function sucess.
92 * @tc.type: FUNC
93 */
94 HWTEST_F(SwitchWifiPluginTest, TestTurnOffWifiSuccess, TestSize.Level1)
95 {
96 std::shared_ptr<IPlugin> plugin = SwitchWifiPlugin::GetPlugin();
97 uint32_t code = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, EdmInterfaceCode::SWITCH_WIFI);
98 HandlePolicyData handlePolicyData{"false", "", false};
99 MessageParcel data;
100 MessageParcel reply;
101 ErrCode ret = plugin->OnHandlePolicy(code, data, reply, handlePolicyData, DEFAULT_USER_ID);
102 ASSERT_TRUE(ret == ERR_OK);
103 }
104 } // namespace TEST
105 } // namespace EDM
106 } // namespace OHOS