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 #include "set_wifi_disabled_plugin.h"
18 
19 #include "edm_ipc_interface_code.h"
20 #include "iplugin_manager.h"
21 #include "parameters.h"
22 #include "utils.h"
23 
24 using namespace testing::ext;
25 using namespace testing;
26 
27 namespace OHOS {
28 namespace EDM {
29 namespace TEST {
30 const std::string KEY_DISABLE_WIFI = "persist.edm.wifi_enable";
31 
32 class SetWifiDisabledPluginTest : public testing::Test {
33 protected:
34     static void SetUpTestSuite(void);
35 
36     static void TearDownTestSuite(void);
37 };
38 
SetUpTestSuite(void)39 void SetWifiDisabledPluginTest::SetUpTestSuite(void)
40 {
41     Utils::SetEdmServiceEnable();
42     Utils::SetEdmInitialEnv();
43 }
44 
TearDownTestSuite(void)45 void SetWifiDisabledPluginTest::TearDownTestSuite(void)
46 {
47     Utils::SetEdmServiceDisable();
48     Utils::ResetTokenTypeAndUid();
49     ASSERT_TRUE(Utils::IsOriginalUTEnv());
50     std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
51 }
52 
53 /**
54  * @tc.name: TestSetWifiDisabledPluginTestSetTrue
55  * @tc.desc: Test SetWifiDisabledPluginTest::OnSetPolicy function.
56  * @tc.type: FUNC
57  */
58 HWTEST_F(SetWifiDisabledPluginTest, TestSetWifiDisabledPluginTestSetTrue, TestSize.Level1)
59 {
60     MessageParcel data;
61     MessageParcel reply;
62     data.WriteBool(true);
63     std::shared_ptr<IPlugin> plugin = SetWifiDisabledPlugin::GetPlugin();
64     HandlePolicyData handlePolicyData{"false", false};
65     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISABLE_WIFI);
66     ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
67     ASSERT_TRUE(ret == ERR_OK);
68     ASSERT_TRUE(handlePolicyData.isChanged);
69 }
70 
71 /**
72  * @tc.name: TestSetWifiDisabledPluginTestSetFalse
73  * @tc.desc: Test SetWifiDisabledPluginTest::OnSetPolicy function.
74  * @tc.type: FUNC
75  */
76 HWTEST_F(SetWifiDisabledPluginTest, TestSetWifiDisabledPluginTestSetFalse, TestSize.Level1)
77 {
78     MessageParcel data;
79     MessageParcel reply;
80     data.WriteBool(false);
81     std::shared_ptr<IPlugin> plugin = SetWifiDisabledPlugin::GetPlugin();
82     HandlePolicyData handlePolicyData{"false", false};
83     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISABLE_WIFI);
84     ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
85     ASSERT_TRUE(ret == ERR_OK);
86     ASSERT_TRUE(handlePolicyData.isChanged);
87 }
88 
89 /**
90  * @tc.name: TestSetWifiDisabledPluginTestSetFalseFail
91  * @tc.desc: Test SetWifiDisabledPluginTest::OnSetPolicy function.
92  * @tc.type: FUNC
93  */
94 HWTEST_F(SetWifiDisabledPluginTest, TestSetWifiDisabledPluginTestSetFalseFail, TestSize.Level1)
95 {
96     Utils::ResetTokenTypeAndUid();
97     MessageParcel data;
98     MessageParcel reply;
99     data.WriteBool(true);
100     std::shared_ptr<IPlugin> plugin = SetWifiDisabledPlugin::GetPlugin();
101     HandlePolicyData handlePolicyData{"false", false};
102     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISABLE_WIFI);
103     ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
104     ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
105     Utils::SetEdmInitialEnv();
106 }
107 
108 /**
109  * @tc.name: TestSetWifiDisabledPluginTestGet
110  * @tc.desc: Test SetWifiDisabledPluginTest::OnGetPolicy function.
111  * @tc.type: FUNC
112  */
113 HWTEST_F(SetWifiDisabledPluginTest, TestSetWifiDisabledPluginTestGet, TestSize.Level1)
114 {
115     std::shared_ptr<IPlugin> plugin = SetWifiDisabledPlugin::GetPlugin();
116     std::string policyData{"false"};
117     MessageParcel data;
118     MessageParcel reply;
119     ErrCode ret = plugin->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
120     int32_t flag = ERR_INVALID_VALUE;
121     ASSERT_TRUE(reply.ReadInt32(flag) && (flag == ERR_OK));
122     bool result = false;
123     reply.ReadBool(result);
124     ASSERT_TRUE(ret == ERR_OK);
125     ASSERT_TRUE(result == system::GetBoolParameter(KEY_DISABLE_WIFI, false));
126 }
127 } // namespace TEST
128 } // namespace EDM
129 } // namespace OHOS