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