1 /*
2 * Copyright (c) 2024 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 "allowed_bluetooth_devices_plugin.h"
19 #include "edm_constants.h"
20 #include "edm_data_ability_utils_mock.h"
21 #include "edm_ipc_interface_code.h"
22 #include "iplugin_manager.h"
23 #include "utils.h"
24
25 using namespace testing::ext;
26 using namespace testing;
27
28 namespace OHOS {
29 namespace EDM {
30 namespace TEST {
31 class AllowedBluetoothDevicesPluginTest : public testing::Test {
32 protected:
33 static void SetUpTestSuite(void);
34
35 static void TearDownTestSuite(void);
36 };
37
SetUpTestSuite(void)38 void AllowedBluetoothDevicesPluginTest::SetUpTestSuite(void)
39 {
40 Utils::SetEdmServiceEnable();
41 Utils::SetEdmInitialEnv();
42 }
43
TearDownTestSuite(void)44 void AllowedBluetoothDevicesPluginTest::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: TestSetBluetoothDevicesEmpty
54 * @tc.desc: Test set bluetooth devices function.
55 * @tc.type: FUNC
56 */
57 HWTEST_F(AllowedBluetoothDevicesPluginTest, TestSetBluetoothDevicesEmpty, TestSize.Level1)
58 {
59 Utils::SetBluetoothEnable();
60 AllowedBluetoothDevicesPlugin plugin;
61 std::vector<std::string> policyData;
62 std::vector<std::string> currentData;
63 ErrCode ret = plugin.OnSetPolicy(policyData, currentData, DEFAULT_USER_ID);
64 ASSERT_TRUE(ret == ERR_OK);
65 }
66
67 /**
68 * @tc.name: TestSetBluetoothDevicesWithDataAndCurrentData
69 * @tc.desc: Test AllowedBluetoothDevicesPluginTest::OnSetPolicy function.
70 * @tc.type: FUNC
71 */
72 HWTEST_F(AllowedBluetoothDevicesPluginTest, TestSetBluetoothDevicesWithDataAndCurrentData, TestSize.Level1)
73 {
74 Utils::SetBluetoothEnable();
75 AllowedBluetoothDevicesPlugin plugin;
76 std::vector<std::string> policyData = { "00:1A:2B:3C:4D:5E", "AA:BB:CC:DD:EE:FF" };
77 std::vector<std::string> currentData = { "00:00:5E:00:53:00", "FF:FF:FF:FF:FF:FF" };
78 ErrCode ret = plugin.OnSetPolicy(policyData, currentData, DEFAULT_USER_ID);
79 plugin.OnChangedPolicyDone(true);
80 ASSERT_TRUE(ret == ERR_OK);
81 }
82
83 /**
84 * @tc.name: TestSetBluetoothDevicesWithDataWithoutCurrentData
85 * @tc.desc: Test AllowedBluetoothDevicesPluginTest::OnSetPolicy function.
86 * @tc.type: FUNC
87 */
88 HWTEST_F(AllowedBluetoothDevicesPluginTest, TestSetBluetoothDevicesWithDataWithoutCurrentData, TestSize.Level1)
89 {
90 Utils::SetBluetoothEnable();
91 AllowedBluetoothDevicesPlugin plugin;
92 std::vector<std::string> policyData = { "00:1A:2B:3C:4D:5E", "AA:BB:CC:DD:EE:FF" };
93 std::vector<std::string> currentData;
94 ErrCode ret = plugin.OnSetPolicy(policyData, currentData, DEFAULT_USER_ID);
95 ASSERT_TRUE(ret == ERR_OK);
96 }
97
98 /**
99 * @tc.name: TestSetBluetoothDevicesFail
100 * @tc.desc: Test set bluetooth devices function.
101 * @tc.type: FUNC
102 */
103 HWTEST_F(AllowedBluetoothDevicesPluginTest, TestSetBluetoothDevicesFail, TestSize.Level1)
104 {
105 Utils::SetBluetoothDisable();
106 AllowedBluetoothDevicesPlugin devicesPlugin;
107 std::vector<std::string> policyData{ "00:1A:2B:3C:4D:5E", "AA:BB:CC:DD:EE:FF" };
108 std::vector<std::string> currentData;
109 ErrCode ret = devicesPlugin.OnSetPolicy(policyData, currentData, DEFAULT_USER_ID);
110 ASSERT_TRUE(ret == EdmReturnErrCode::CONFIGURATION_CONFLICT_FAILED);
111 }
112
113 /**
114 * @tc.name: TestOnChangedPolicyDoneSuc
115 * @tc.desc: Test set bluetooth devices function.
116 * @tc.type: FUNC
117 */
118 HWTEST_F(AllowedBluetoothDevicesPluginTest, TestOnChangedPolicyDoneSuc, TestSize.Level1)
119 {
120 Utils::SetBluetoothDisable();
121 AllowedBluetoothDevicesPlugin devicesPlugin;
122 devicesPlugin.OnChangedPolicyDone(false);
123 ASSERT_TRUE(true);
124 }
125
126 /**
127 * @tc.name: TestSetBluetoothDevicesFail
128 * @tc.desc: Test set bluetooth devices function.
129 * @tc.type: FUNC
130 */
131 HWTEST_F(AllowedBluetoothDevicesPluginTest, TestSetBluetoothDevicesCountFail, TestSize.Level1)
132 {
133 Utils::SetBluetoothEnable();
134 AllowedBluetoothDevicesPlugin devicesPlugin;
135 std::vector<std::string> policyData(EdmConstants::BLUETOOTH_WHITELIST_MAX_SIZE + 1);
136 for (int i = 0; i < EdmConstants::BLUETOOTH_WHITELIST_MAX_SIZE + 1; ++i) {
137 std::stringstream ss;
138 ss << i;
139 policyData[i] = ss.str();
140 }
141 std::vector<std::string> currentData;
142 ErrCode ret = devicesPlugin.OnSetPolicy(policyData, currentData, DEFAULT_USER_ID);
143 ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
144 }
145
146 /**
147 * @tc.name: TestGetBluetoothDevicesSuc
148 * @tc.desc: Test AllowedBluetoothDevicesPluginTest::OnGetPolicy function.
149 * @tc.type: FUNC
150 */
151 HWTEST_F(AllowedBluetoothDevicesPluginTest, TestGetBluetoothDevicesSuc, TestSize.Level1)
152 {
153 std::shared_ptr<IPlugin> plugin = AllowedBluetoothDevicesPlugin::GetPlugin();
154 std::string policyValue{ "GetBluetoothDevices" };
155 MessageParcel data;
156 MessageParcel reply;
157 ErrCode ret = plugin->OnGetPolicy(policyValue, data, reply, DEFAULT_USER_ID);
158 int32_t flag = ERR_INVALID_VALUE;
159 ASSERT_TRUE(reply.ReadInt32(flag) && (flag == ERR_OK));
160 ASSERT_TRUE(ret == ERR_OK);
161 }
162
163 /**
164 * @tc.name: TestRemoveBluetoothDevicesEmpty
165 * @tc.desc: Test AllowedBluetoothDevicesPluginTest::OnRemovePolicy function.
166 * @tc.type: FUNC
167 */
168 HWTEST_F(AllowedBluetoothDevicesPluginTest, TestRemoveBluetoothDevicesEmpty, TestSize.Level1)
169 {
170 AllowedBluetoothDevicesPlugin plugin;
171 std::vector<std::string> policyData;
172 std::vector<std::string> currentData;
173 ErrCode ret = plugin.OnRemovePolicy(policyData, currentData, DEFAULT_USER_ID);
174 ASSERT_TRUE(ret == ERR_OK);
175 }
176
177 /**
178 * @tc.name: TestRemoveBluetoothDevicesWithDataAndCurrentData
179 * @tc.desc: Test AllowedBluetoothDevicesPluginTest::OnRemovePolicy function.
180 * @tc.type: FUNC
181 */
182 HWTEST_F(AllowedBluetoothDevicesPluginTest, TestRemoveBluetoothDevicesWithDataAndCurrentData, TestSize.Level1)
183 {
184 AllowedBluetoothDevicesPlugin plugin;
185 std::vector<std::string> policyData = { "00:1A:2B:3C:4D:5E", "AA:BB:CC:DD:EE:FF" };
186 std::vector<std::string> currentData = { "00:1A:2B:3C:4D:5E", "AA:BB:CC:DD:EE:FF" };
187 ErrCode ret = plugin.OnRemovePolicy(policyData, currentData, DEFAULT_USER_ID);
188 ASSERT_TRUE(ret == ERR_OK);
189 }
190
191 /**
192 * @tc.name: TestRemoveBluetoothDevicesWithDataWithoutCurrentData
193 * @tc.desc: Test AllowedBluetoothDevicesPluginTest::OnRemovePolicy function.
194 * @tc.type: FUNC
195 */
196 HWTEST_F(AllowedBluetoothDevicesPluginTest, TestRemoveBluetoothDevicesWithDataWithoutCurrentData, TestSize.Level1)
197 {
198 AllowedBluetoothDevicesPlugin plugin;
199 std::vector<std::string> policyData = { "00:1A:2B:3C:4D:5E", "AA:BB:CC:DD:EE:FF" };
200 std::vector<std::string> currentData;
201 ErrCode ret = plugin.OnRemovePolicy(policyData, currentData, DEFAULT_USER_ID);
202 ASSERT_TRUE(ret == ERR_OK);
203 }
204 } // namespace TEST
205 } // namespace EDM
206 } // namespace OHOS