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 #define private public
19 #define protected public
20 #include "disallowed_bluetooth_devices_plugin.h"
21 #undef protected
22 #undef private
23
24 #include "edm_constants.h"
25 #include "edm_data_ability_utils_mock.h"
26 #include "edm_ipc_interface_code.h"
27 #include "iplugin_manager.h"
28 #include "utils.h"
29
30 using namespace testing::ext;
31 using namespace testing;
32
33 namespace OHOS {
34 namespace EDM {
35 namespace TEST {
36 class DisallowedBluetoothDevicesPluginTest : public testing::Test {
37 protected:
38 static void SetUpTestSuite(void);
39
40 static void TearDownTestSuite(void);
41 };
42
SetUpTestSuite(void)43 void DisallowedBluetoothDevicesPluginTest::SetUpTestSuite(void)
44 {
45 Utils::SetEdmServiceEnable();
46 Utils::SetEdmInitialEnv();
47 }
TearDownTestSuite(void)48 void DisallowedBluetoothDevicesPluginTest::TearDownTestSuite(void)
49 {
50 Utils::SetEdmServiceDisable();
51 Utils::ResetTokenTypeAndUid();
52 ASSERT_TRUE(Utils::IsOriginalUTEnv());
53 std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
54 }
55
56 /**
57 * @tc.name: TestSetDisallowedBluetoothDevicesEmpty
58 * @tc.desc: Test set disallowed bluetooth devices function.
59 * @tc.type: FUNC
60 */
61 HWTEST_F(DisallowedBluetoothDevicesPluginTest, TestSetDisallowedBluetoothDevicesEmpty, TestSize.Level1)
62 {
63 Utils::SetBluetoothEnable();
64 DisallowedBluetoothDevicesPlugin plugin;
65 plugin.maxListSize_ = EdmConstants::BLUETOOTH_LIST_MAX_SIZE;
66 std::vector<std::string> policyData;
67 std::vector<std::string> currentData;
68 std::vector<std::string> mergeData;
69 ErrCode ret = plugin.OnBasicSetPolicy(policyData, currentData, mergeData, DEFAULT_USER_ID);
70 ASSERT_TRUE(ret == ERR_OK);
71 }
72
73 /**
74 * @tc.name: TestSetDisallowedBluetoothDevicesWithDataAndCurrentData
75 * @tc.desc: Test DisallowedBluetoothDevicesPluginTest::OnSetPolicy function.
76 * @tc.type: FUNC
77 */
78 HWTEST_F(DisallowedBluetoothDevicesPluginTest, TestSetDisallowedBluetoothDevicesWithDataAndCurrentData, TestSize.Level1)
79 {
80 Utils::SetBluetoothEnable();
81 DisallowedBluetoothDevicesPlugin plugin;
82 plugin.maxListSize_ = EdmConstants::BLUETOOTH_LIST_MAX_SIZE;
83 std::vector<std::string> policyData = { "00:1A:2B:3C:4D:5E", "AA:BB:CC:DD:EE:FF" };
84 std::vector<std::string> currentData;
85 std::vector<std::string> mergeData;
86 ErrCode ret = plugin.OnBasicSetPolicy(policyData, currentData, mergeData, DEFAULT_USER_ID);
87 plugin.OnChangedPolicyDone(true);
88 ASSERT_TRUE(ret == ERR_OK);
89 }
90
91 /**
92 * @tc.name: TestSetDisallowedBluetoothDevicesFail
93 * @tc.desc: Test set disallowed bluetooth devices function.
94 * @tc.type: FUNC
95 */
96 HWTEST_F(DisallowedBluetoothDevicesPluginTest, TestSetDisallowedBluetoothDevicesFail, TestSize.Level1)
97 {
98 Utils::SetBluetoothDisable();
99 DisallowedBluetoothDevicesPlugin devicesPlugin;
100 devicesPlugin.maxListSize_ = EdmConstants::BLUETOOTH_LIST_MAX_SIZE;
101 std::vector<std::string> policyData{ "00:1A:2B:3C:4D:5E", "AA:BB:CC:DD:EE:FF" };
102 std::vector<std::string> currentData;
103 std::vector<std::string> mergeData;
104 ErrCode ret = devicesPlugin.OnBasicSetPolicy(policyData, currentData, mergeData, DEFAULT_USER_ID);
105 ASSERT_TRUE(ret == EdmReturnErrCode::CONFIGURATION_CONFLICT_FAILED);
106 }
107
108 /**
109 * @tc.name: TestSetDisallowedBluetoothDevicesFail
110 * @tc.desc: Test set disallowed bluetooth devices function.
111 * @tc.type: FUNC
112 */
113 HWTEST_F(DisallowedBluetoothDevicesPluginTest, TestSetDisallowedBluetoothDevicesCountFail, TestSize.Level1)
114 {
115 Utils::SetBluetoothEnable();
116 DisallowedBluetoothDevicesPlugin devicesPlugin;
117 devicesPlugin.maxListSize_ = EdmConstants::BLUETOOTH_LIST_MAX_SIZE;
118 std::vector<std::string> policyData(EdmConstants::BLUETOOTH_LIST_MAX_SIZE + 1);
119 for (int i = 0; i < EdmConstants::BLUETOOTH_LIST_MAX_SIZE + 1; ++i) {
120 std::stringstream ss;
121 ss << i;
122 policyData[i] = ss.str();
123 }
124 std::vector<std::string> currentData;
125 std::vector<std::string> mergeData;
126 ErrCode ret = devicesPlugin.OnBasicSetPolicy(policyData, currentData, mergeData, DEFAULT_USER_ID);
127 ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
128 }
129
130 /**
131 * @tc.name: TestRemoveDisallowedBluetoothDevicesEmpty
132 * @tc.desc: Test DisallowedBluetoothDevicesPluginTest::OnRemovePolicy function.
133 * @tc.type: FUNC
134 */
135 HWTEST_F(DisallowedBluetoothDevicesPluginTest, TestRemoveDisallowedBluetoothDevicesEmpty, TestSize.Level1)
136 {
137 DisallowedBluetoothDevicesPlugin plugin;
138 plugin.maxListSize_ = EdmConstants::BLUETOOTH_LIST_MAX_SIZE;
139 std::vector<std::string> policyData;
140 std::vector<std::string> currentData;
141 std::vector<std::string> mergeData;
142 ErrCode ret = plugin.OnBasicRemovePolicy(policyData, currentData, mergeData, DEFAULT_USER_ID);
143 ASSERT_TRUE(ret == ERR_OK);
144 }
145
146 /**
147 * @tc.name: TestRemoveDisallowedBluetoothDevicesWithDataAndCurrentData
148 * @tc.desc: Test DisallowedBluetoothDevicesPluginTest::OnRemovePolicy function.
149 * @tc.type: FUNC
150 */
151 HWTEST_F(DisallowedBluetoothDevicesPluginTest, TestRemoveDisallowedBluetoothDevicesWithDataAndCurrentData,
152 TestSize.Level1)
153 {
154 DisallowedBluetoothDevicesPlugin plugin;
155 plugin.maxListSize_ = EdmConstants::BLUETOOTH_LIST_MAX_SIZE;
156 std::vector<std::string> policyData = { "00:1A:2B:3C:4D:5E", "AA:BB:CC:DD:EE:FF" };
157 std::vector<std::string> currentData;
158 std::vector<std::string> mergeData;
159 ErrCode ret = plugin.OnBasicRemovePolicy(policyData, currentData, mergeData, DEFAULT_USER_ID);
160 plugin.OnChangedPolicyDone(false);
161 ASSERT_TRUE(ret == ERR_OK);
162 }
163 }
164 }
165 }