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 "disable_bluetooth_plugin.h"
18 #include "bluetooth_def.h"
19 #include "bluetooth_errorcode.h"
20 #include "bluetooth_host.h"
21 #include "parameters.h"
22 #include "edm_ipc_interface_code.h"
23 #include "iplugin_manager.h"
24 #include "plugin_singleton.h"
25 #include "utils.h"
26 
27 using namespace testing::ext;
28 using namespace testing;
29 
30 namespace OHOS {
31 namespace EDM {
32 namespace TEST {
33 class DisableBluetoothPluginTest : public testing::Test {
34 protected:
35     static void SetUpTestSuite(void);
36 
37     static void TearDownTestSuite(void);
38 };
39 
SetUpTestSuite(void)40 void DisableBluetoothPluginTest::SetUpTestSuite(void)
41 {
42     Utils::SetEdmServiceEnable();
43     Utils::SetEdmInitialEnv();
44 }
45 
TearDownTestSuite(void)46 void DisableBluetoothPluginTest::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: TestDisableBluetoothPluginTestCloseSetTrue
56  * @tc.desc: Test DisableBluetoothPluginTest::OnSetPolicy function.
57  * @tc.type: FUNC
58  */
59 HWTEST_F(DisableBluetoothPluginTest, TestDisableBluetoothPluginTestCloseSetTrue, TestSize.Level1)
60 {
61     if (Bluetooth::BluetoothHost::GetDefaultHost().IsBrEnabled()) {
62         Bluetooth::BluetoothHost::GetDefaultHost().DisableBt();
63     }
64     MessageParcel data;
65     MessageParcel reply;
66     data.WriteBool(true);
67     std::shared_ptr<IPlugin> plugin = DisableBluetoothPlugin::GetPlugin();
68     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISABLE_BLUETOOTH);
69     HandlePolicyData handlePolicyData{"false", false};
70     ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
71     ASSERT_TRUE(ret == ERR_OK);
72     ASSERT_TRUE(handlePolicyData.isChanged);
73     ASSERT_TRUE(OHOS::system::GetBoolParameter(DisableBluetoothPlugin::PERSIST_BLUETOOTH_CONTROL, false));
74 }
75 
76 /**
77  * @tc.name: TestDisableBluetoothPluginTestSetFalse
78  * @tc.desc: Test DisableBluetoothPluginTest::OnSetPolicy function.
79  * @tc.type: FUNC
80  */
81 HWTEST_F(DisableBluetoothPluginTest, TestDisableBluetoothPluginTestSetFalse, TestSize.Level1)
82 {
83     MessageParcel data;
84     MessageParcel reply;
85     data.WriteBool(false);
86     std::shared_ptr<IPlugin> plugin = DisableBluetoothPlugin::GetPlugin();
87     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISABLE_BLUETOOTH);
88     HandlePolicyData handlePolicyData{"false", false};
89     ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
90     ASSERT_TRUE(ret == ERR_OK);
91     ASSERT_TRUE(handlePolicyData.isChanged);
92     ASSERT_FALSE(OHOS::system::GetBoolParameter(DisableBluetoothPlugin::PERSIST_BLUETOOTH_CONTROL, true));
93 }
94 
95 /**
96  * @tc.name: TestDisableBluetoothPluginTestOpenSetTrue
97  * @tc.desc: Test DisableBluetoothPluginTest::OnSetPolicy function.
98  * @tc.type: FUNC
99  */
100 HWTEST_F(DisableBluetoothPluginTest, TestDisableBluetoothPluginTestOpenSetTrue, TestSize.Level1)
101 {
102     MessageParcel data;
103     MessageParcel reply;
104     data.WriteBool(false);
105     std::shared_ptr<IPlugin> plugin = DisableBluetoothPlugin::GetPlugin();
106     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISABLE_BLUETOOTH);
107     HandlePolicyData handlePolicyData{"false", false};
108     ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
109     ASSERT_TRUE(ret == ERR_OK);
110     ASSERT_TRUE(handlePolicyData.isChanged);
111     ASSERT_FALSE(OHOS::system::GetBoolParameter(DisableBluetoothPlugin::PERSIST_BLUETOOTH_CONTROL, true));
112     Bluetooth::BluetoothHost::GetDefaultHost().EnableBle();
113     data.WriteBool(true);
114     handlePolicyData.isChanged = false;
115     ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
116     ASSERT_TRUE(ret == ERR_OK);
117     ASSERT_TRUE(handlePolicyData.isChanged);
118     ASSERT_TRUE(OHOS::system::GetBoolParameter(DisableBluetoothPlugin::PERSIST_BLUETOOTH_CONTROL, false));
119 }
120 
121 /**
122  * @tc.name: TestDisableBluetoothPluginTestGet
123  * @tc.desc: Test DisableBluetoothPluginTest::OnGetPolicy function.
124  * @tc.type: FUNC
125  */
126 HWTEST_F(DisableBluetoothPluginTest, TestDisableBluetoothPluginTestGet, TestSize.Level1)
127 {
128     std::shared_ptr<IPlugin> plugin = DisableBluetoothPlugin::GetPlugin();
129     std::string policyData{"false"};
130     MessageParcel data;
131     MessageParcel reply;
132     ErrCode ret = plugin->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
133     ASSERT_TRUE(ret == ERR_OK);
134     int32_t flag = ERR_INVALID_VALUE;
135     ASSERT_TRUE(reply.ReadInt32(flag) && (flag == ERR_OK));
136     ASSERT_EQ(OHOS::system::GetBoolParameter(DisableBluetoothPlugin::PERSIST_BLUETOOTH_CONTROL, false),
137         reply.ReadBool());
138 }
139 } // namespace TEST
140 } // namespace EDM
141 } // namespace OHOS