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 std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
57 }
58
59 /**
60 * @tc.name: TestDisableBluetoothPluginTestCloseSetTrue
61 * @tc.desc: Test DisableBluetoothPluginTest::OnSetPolicy function.
62 * @tc.type: FUNC
63 */
64 HWTEST_F(DisableBluetoothPluginTest, TestDisableBluetoothPluginTestCloseSetTrue, TestSize.Level1)
65 {
66 if (Bluetooth::BluetoothHost::GetDefaultHost().IsBrEnabled()) {
67 Bluetooth::BluetoothHost::GetDefaultHost().DisableBt();
68 }
69 MessageParcel data;
70 MessageParcel reply;
71 data.WriteBool(true);
72 std::shared_ptr<IPlugin> plugin = DisableBluetoothPlugin::GetPlugin();
73 std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISABLE_BLUETOOTH);
74 HandlePolicyData handlePolicyData{"false", "", false};
75 ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
76 ASSERT_TRUE(ret == ERR_OK);
77 ASSERT_TRUE(handlePolicyData.isChanged);
78 ASSERT_TRUE(OHOS::system::GetBoolParameter(PERSIST_BLUETOOTH_CONTROL, false));
79 }
80
81 /**
82 * @tc.name: TestDisableBluetoothPluginTestSetFalse
83 * @tc.desc: Test DisableBluetoothPluginTest::OnSetPolicy function.
84 * @tc.type: FUNC
85 */
86 HWTEST_F(DisableBluetoothPluginTest, TestDisableBluetoothPluginTestSetFalse, TestSize.Level1)
87 {
88 MessageParcel data;
89 MessageParcel reply;
90 data.WriteBool(false);
91 std::shared_ptr<IPlugin> plugin = DisableBluetoothPlugin::GetPlugin();
92 std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISABLE_BLUETOOTH);
93 HandlePolicyData handlePolicyData{"false", "", false};
94 ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
95 ASSERT_TRUE(ret == ERR_OK);
96 ASSERT_TRUE(handlePolicyData.isChanged);
97 ASSERT_FALSE(OHOS::system::GetBoolParameter(PERSIST_BLUETOOTH_CONTROL, true));
98 }
99
100 /**
101 * @tc.name: TestDisableBluetoothPluginTestOpenSetTrue
102 * @tc.desc: Test DisableBluetoothPluginTest::OnSetPolicy function.
103 * @tc.type: FUNC
104 */
105 HWTEST_F(DisableBluetoothPluginTest, TestDisableBluetoothPluginTestOpenSetTrue, TestSize.Level1)
106 {
107 MessageParcel data;
108 MessageParcel reply;
109 data.WriteBool(false);
110 std::shared_ptr<IPlugin> plugin = DisableBluetoothPlugin::GetPlugin();
111 std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISABLE_BLUETOOTH);
112 HandlePolicyData handlePolicyData{"false", "", false};
113 ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
114 ASSERT_TRUE(ret == ERR_OK);
115 ASSERT_TRUE(handlePolicyData.isChanged);
116 ASSERT_FALSE(OHOS::system::GetBoolParameter(PERSIST_BLUETOOTH_CONTROL, true));
117 Bluetooth::BluetoothHost::GetDefaultHost().EnableBle();
118 data.WriteBool(true);
119 handlePolicyData.isChanged = false;
120 ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
121 ASSERT_TRUE(ret == ERR_OK);
122 ASSERT_TRUE(handlePolicyData.isChanged);
123 ASSERT_TRUE(OHOS::system::GetBoolParameter(PERSIST_BLUETOOTH_CONTROL, false));
124 }
125
126 /**
127 * @tc.name: TestDisableBluetoothPluginTestOnAdminRemove
128 * @tc.desc: Test DisableBluetoothPluginTest::OnAdminRemove function.
129 * @tc.type: FUNC
130 */
131 HWTEST_F(DisableBluetoothPluginTest, TestDisableBluetoothPluginTestOnAdminRemove, TestSize.Level1)
132 {
133 std::shared_ptr<IPlugin> plugin = DisableBluetoothPlugin::GetPlugin();
134 std::string currentPolicy = "true";
135 std::string mergePolicy = "false";
136 ErrCode ret = plugin->OnAdminRemove("test", currentPolicy, mergePolicy, DEFAULT_USER_ID);
137 ASSERT_TRUE(ret == ERR_OK);
138 ASSERT_FALSE(OHOS::system::GetBoolParameter(PERSIST_BLUETOOTH_CONTROL, true));
139 }
140 } // namespace TEST
141 } // namespace EDM
142 } // namespace OHOS