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