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 <gtest/gtest.h>
17
18 #include "disallowed_bluetooth_protocols_plugin.h"
19
20 #include "edm_constants.h"
21 #include "edm_data_ability_utils_mock.h"
22 #include "edm_ipc_interface_code.h"
23 #include "iplugin_manager.h"
24 #include "utils.h"
25
26 using namespace testing::ext;
27 using namespace testing;
28
29 namespace OHOS {
30 namespace EDM {
31 namespace TEST {
32 class DisallowedBluetoothProtocolsPluginTest : public testing::Test {
33 protected:
34 static void SetUpTestSuite(void);
35
36 static void TearDownTestSuite(void);
37 };
38
SetUpTestSuite(void)39 void DisallowedBluetoothProtocolsPluginTest::SetUpTestSuite(void)
40 {
41 Utils::SetEdmServiceEnable();
42 Utils::SetEdmInitialEnv();
43 }
TearDownTestSuite(void)44 void DisallowedBluetoothProtocolsPluginTest::TearDownTestSuite(void)
45 {
46 Utils::SetEdmServiceDisable();
47 Utils::ResetTokenTypeAndUid();
48 ASSERT_TRUE(Utils::IsOriginalUTEnv());
49 std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
50 }
51
52 /**
53 * @tc.name: TestInitPluginSuccess
54 * @tc.desc: Test DisallowedBluetoothProtocolsPlugin::InitPlugin success.
55 * @tc.type: FUNC
56 */
57 HWTEST_F(DisallowedBluetoothProtocolsPluginTest, TestInitPluginSuccess, TestSize.Level1)
58 {
59 std::shared_ptr<IPlugin> plugin = DisallowedBluetoothProtocolsPlugin::GetPlugin();
60 ASSERT_TRUE(plugin->IsGlobalPolicy());
61 ASSERT_TRUE(plugin->NeedSavePolicy());
62 ASSERT_TRUE(plugin->GetCode() == (std::uint32_t)EdmInterfaceCode::DISALLOWED_BLUETOOTH_PROTOCOLS);
63 ASSERT_EQ(plugin->GetPolicyName(), PolicyName::POLICY_DISALLOWED_BLUETOOTH_PROTOCOLS);
64 ASSERT_EQ(plugin->GetPermission(FuncOperateType::SET, IPlugin::PermissionType::SUPER_DEVICE_ADMIN),
65 EdmPermission::PERMISSION_ENTERPRISE_MANAGE_BLUETOOTH);
66 }
67
68 /**
69 * @tc.name: TestSetDisallowedBluetoothProtocolsEmpty
70 * @tc.desc: Test set disallowed nearlink protocols function.
71 * @tc.type: FUNC
72 */
73 HWTEST_F(DisallowedBluetoothProtocolsPluginTest, TestSetDisallowedBluetoothProtocolsEmpty, TestSize.Level1)
74 {
75 Utils::SetBluetoothEnable();
76 DisallowedBluetoothProtocolsPlugin plugin;
77 std::vector<std::int32_t> policyData;
78 std::vector<std::int32_t> currentData;
79 std::vector<std::int32_t> mergeData;
80 ErrCode ret = plugin.OnSetPolicy(policyData, currentData, mergeData, DEFAULT_USER_ID);
81 ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
82 }
83
84 /**
85 * @tc.name: TestSetDisallowedBluetoothProtocolsWithDataAndCurrentData
86 * @tc.desc: Test DisallowedBluetoothProtocolsPluginTest::OnSetPolicy function.
87 * @tc.type: FUNC
88 */
89 HWTEST_F(DisallowedBluetoothProtocolsPluginTest, TestSetDisallowedBluetoothProtocolsWithDataAndCurrentData,
90 TestSize.Level1)
91 {
92 Utils::SetBluetoothEnable();
93 DisallowedBluetoothProtocolsPlugin plugin;
94 std::vector<std::int32_t> policyData = { 0, 1};
95 std::vector<std::int32_t> currentData;
96 std::vector<std::int32_t> mergeData;
97 ErrCode ret = plugin.OnSetPolicy(policyData, currentData, mergeData, DEFAULT_USER_ID);
98 plugin.OnChangedPolicyDone(true);
99 ASSERT_TRUE(ret == ERR_OK);
100 }
101
102 /**
103 * @tc.name: TestRemoveDisallowedBluetoothProtocolsEmpty
104 * @tc.desc: Test DisallowedBluetoothProtocolsPluginTest::OnRemovePolicy function.
105 * @tc.type: FUNC
106 */
107 HWTEST_F(DisallowedBluetoothProtocolsPluginTest, TestRemoveDisallowedBluetoothProtocolsEmpty, TestSize.Level1)
108 {
109 DisallowedBluetoothProtocolsPlugin plugin;
110 std::vector<std::int32_t> policyData;
111 std::vector<std::int32_t> currentData;
112 std::vector<std::int32_t> mergeData;
113 ErrCode ret = plugin.OnRemovePolicy(policyData, currentData, mergeData, DEFAULT_USER_ID);
114 ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
115 }
116
117 /**
118 * @tc.name: TestRemoveDisallowedBluetoothProtocolsWithDataAndCurrentData
119 * @tc.desc: Test DisallowedBluetoothProtocolsPluginTest::OnRemovePolicy function.
120 * @tc.type: FUNC
121 */
122 HWTEST_F(DisallowedBluetoothProtocolsPluginTest, TestRemoveDisallowedBluetoothProtocolsWithDataAndCurrentData,
123 TestSize.Level1)
124 {
125 DisallowedBluetoothProtocolsPlugin plugin;
126 std::vector<std::int32_t> policyData = { 0, 1 };
127 std::vector<std::int32_t> currentData;
128 std::vector<std::int32_t> mergeData;
129 ErrCode ret = plugin.OnRemovePolicy(policyData, currentData, mergeData, DEFAULT_USER_ID);
130 plugin.OnChangedPolicyDone(false);
131 ASSERT_TRUE(ret == ERR_OK);
132 }
133
134 /**
135 * @tc.name: TestAdminRemoveDisallowedBluetoothProtocolsEmpty
136 * @tc.desc: Test DisallowedBluetoothProtocolsPluginTest::OnAdminRemovePolicy function.
137 * @tc.type: FUNC
138 */
139 HWTEST_F(DisallowedBluetoothProtocolsPluginTest, TestAdminRemoveDisallowedBluetoothProtocolsEmpty, TestSize.Level1)
140 {
141 DisallowedBluetoothProtocolsPlugin plugin;
142 std::string adminName{"testAdminName"};
143 std::vector<std::int32_t> policyData;
144 std::vector<std::int32_t> mergeData;
145 ErrCode ret = plugin.OnAdminRemove(adminName, policyData, mergeData, DEFAULT_USER_ID);
146 ASSERT_TRUE(ret == ERR_OK);
147 }
148
149 /**
150 * @tc.name: TestGetPolicyDisallowedBluetoothProtocolsEmpty
151 * @tc.desc: Test DisallowedBluetoothProtocolsPluginTest::OnGetPolicy function.
152 * @tc.type: FUNC
153 */
154 HWTEST_F(DisallowedBluetoothProtocolsPluginTest, TestGetPolicyDisallowedBluetoothProtocolsEmpty, TestSize.Level1)
155 {
156 DisallowedBluetoothProtocolsPlugin plugin;
157 std::string policyData;
158 MessageParcel data;
159 MessageParcel reply;
160 ErrCode ret = plugin.OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
161 ASSERT_TRUE(ret == ERR_OK);
162 }
163 }
164 }
165 }