1 /*
2 * Copyright (c) 2023-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 #include "accesstoken_kit.h"
18 #include "nativetoken_kit.h"
19 #include "token_setproc.h"
20
21 #define private public
22 #define protected public
23 #include "distributed_device_profile_service_new.h"
24 #include "system_ability_definition.h"
25 #include "profile_cache.h"
26 #include "distributed_device_profile_errors.h"
27 #include "trust_profile_manager.h"
28 #include "permission_manager.h"
29 #include "subscribe_profile_manager.h"
30 #include "event_handler_factory.h"
31 #include "distributed_device_profile_constants.h"
32 #include "dp_inited_callback_stub.h"
33 #include "i_dp_inited_callback.h"
34 #undef private
35 #undef protected
36
37
38 namespace OHOS {
39 namespace DistributedDeviceProfile {
40 using namespace testing;
41 using namespace testing::ext;
42 namespace {
43 const std::string PERMISSION_JSON_PATH = "/system/etc/deviceprofile/permission.json";
44 const int32_t PERMS_NUM = 2;
45 }
46 class DpProfileServiceTest : public testing::Test {
47 public:
48 static void SetUpTestCase();
49 static void TearDownTestCase();
50 void SetUp();
51 void TearDown();
52 };
53
SetUpTestCase()54 void DpProfileServiceTest::SetUpTestCase()
55 {
56 }
57
TearDownTestCase()58 void DpProfileServiceTest::TearDownTestCase()
59 {
60 }
61
SetUp()62 void DpProfileServiceTest::SetUp()
63 {
64 const char *perms[PERMS_NUM] = {
65 "ohos.permission.DISTRIBUTED_DATASYNC",
66 "ohos.permission.DISTRIBUTED_SOFTBUS_CENTER"
67 };
68 uint64_t tokenId;
69 NativeTokenInfoParams infoInstance = {
70 .dcapsNum = 0,
71 .permsNum = PERMS_NUM,
72 .aclsNum = 0,
73 .dcaps = nullptr,
74 .perms = perms,
75 .acls = nullptr,
76 .processName = "deviceprofile",
77 .aplStr = "system_core",
78 };
79 tokenId = GetAccessTokenId(&infoInstance);
80 SetSelfTokenID(tokenId);
81 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
82 }
83
TearDown()84 void DpProfileServiceTest::TearDown()
85 {
86 }
87
88 class DpInitedCallback : public DpInitedCallbackStub {
89 public:
DpInitedCallback()90 DpInitedCallback()
91 {
92 }
~DpInitedCallback()93 ~DpInitedCallback()
94 {
95 }
OnDpInited()96 int32_t OnDpInited()
97 {
98 return 0;
99 }
100 };
101
102 HWTEST_F(DpProfileServiceTest, Init_001, TestSize.Level1)
103 {
104 int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().Init();
105 EXPECT_EQ(DP_SUCCESS, ret);
106 }
107
108 HWTEST_F(DpProfileServiceTest, UnInit_001, TestSize.Level1)
109 {
110 int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().UnInit();
111 EXPECT_EQ(DP_DEVICE_PROFILE_MANAGER_UNINIT_FAIL, ret);
112 }
113
114 HWTEST_F(DpProfileServiceTest, CreateUnloadHandler_001, TestSize.Level1)
115 {
116 DistributedDeviceProfileServiceNew::GetInstance().unloadHandler_ = nullptr;
117 int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().CreateUnloadHandler();
118 EXPECT_EQ(DP_SUCCESS, ret);
119
120 ret = DistributedDeviceProfileServiceNew::GetInstance().DestroyUnloadHandler();
121 EXPECT_EQ(DP_SUCCESS, ret);
122
123 EventHandlerFactory::GetInstance().Init();
124 ret = DistributedDeviceProfileServiceNew::GetInstance().CreateUnloadHandler();
125 EXPECT_EQ(DP_SUCCESS, ret);
126
127 ret = DistributedDeviceProfileServiceNew::GetInstance().DestroyUnloadHandler();
128 EXPECT_EQ(DP_SUCCESS, ret);
129 }
130
131 HWTEST_F(DpProfileServiceTest, PutAccessControlProfile_001, TestSize.Level1)
132 {
133 AccessControlProfile accessControlProfile;
134 int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().PutAccessControlProfile(accessControlProfile);
135 EXPECT_EQ(DP_PERMISSION_DENIED, ret);
136 DistributedDeviceProfileServiceNew::GetInstance().DelayUnloadTask();
137 }
138
139 HWTEST_F(DpProfileServiceTest, UpdateAccessControlProfile_001, TestSize.Level1)
140 {
141 AccessControlProfile accessControlProfile;
142 int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().UpdateAccessControlProfile(accessControlProfile);
143 EXPECT_EQ(DP_PERMISSION_DENIED, ret);
144 }
145
146 HWTEST_F(DpProfileServiceTest, GetTrustDeviceProfile_001, TestSize.Level1)
147 {
148 std::string devid;
149 TrustDeviceProfile trustDeviceProfile;
150 int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().GetTrustDeviceProfile(devid, trustDeviceProfile);
151 EXPECT_EQ(DP_PERMISSION_DENIED, ret);
152 }
153
154 HWTEST_F(DpProfileServiceTest, GetAllTrustDeviceProfile_001, TestSize.Level1)
155 {
156 std::vector<TrustDeviceProfile> trustDeviceProfiles;
157 int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().GetAllTrustDeviceProfile(trustDeviceProfiles);
158 EXPECT_EQ(DP_PERMISSION_DENIED, ret);
159 }
160
161 HWTEST_F(DpProfileServiceTest, GetAccessControlProfile_001, TestSize.Level1)
162 {
163 std::map<std::string, std::string> queryParams;
164 std::vector<AccessControlProfile> accessControlProfiles;
165 int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().GetAccessControlProfile(queryParams,
166 accessControlProfiles);
167 EXPECT_EQ(DP_PERMISSION_DENIED, ret);
168 }
169
170 HWTEST_F(DpProfileServiceTest, GetAllAccessControlProfile_001, TestSize.Level1)
171 {
172 std::vector<AccessControlProfile> accessControlProfiles;
173 int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().GetAllAccessControlProfile(accessControlProfiles);
174 EXPECT_EQ(DP_PERMISSION_DENIED, ret);
175 }
176
177 HWTEST_F(DpProfileServiceTest, DeleteAccessControlProfile_001, TestSize.Level1)
178 {
179 int32_t accessControlId = 0;
180 int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().DeleteAccessControlProfile(accessControlId);
181 EXPECT_EQ(DP_PERMISSION_DENIED, ret);
182 }
183
184 HWTEST_F(DpProfileServiceTest, PutServiceProfile_001, TestSize.Level1)
185 {
186 std::string devid = "";
187 std::string serName = "serName_test";
188 std::string serviceType = "serviceType_test";
189 ServiceProfile serviceProfile(devid, serName, serviceType);
190 int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().PutServiceProfile(serviceProfile);
191 EXPECT_EQ(DP_PERMISSION_DENIED, ret);
192 }
193
194 HWTEST_F(DpProfileServiceTest, PutServiceProfileBatch_001, TestSize.Level1)
195 {
196 std::vector<ServiceProfile> serviceProfiles;
197 int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().PutServiceProfileBatch(serviceProfiles);
198 EXPECT_EQ(DP_PERMISSION_DENIED, ret);
199 }
200
201 HWTEST_F(DpProfileServiceTest, PutCharacteristicProfile_001, TestSize.Level1)
202 {
203 CharacteristicProfile charProfile;
204 int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().PutCharacteristicProfile(charProfile);
205 EXPECT_EQ(DP_PERMISSION_DENIED, ret);
206 }
207
208 HWTEST_F(DpProfileServiceTest, PutCharacteristicProfileBatch_001, TestSize.Level1)
209 {
210 std::string devid = "";
211 std::string serName = "serName_test";
212 std::string charKey = "charKey_test";
213 std::string characteristicValue = "characteristicValue_test";
214 CharacteristicProfile charProfile(devid, serName, charKey, characteristicValue);
215 std::vector<CharacteristicProfile> charProfiles;
216 charProfiles.push_back(charProfile);
217 int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().PutCharacteristicProfileBatch(charProfiles);
218 EXPECT_EQ(DP_PERMISSION_DENIED, ret);
219 }
220
221 HWTEST_F(DpProfileServiceTest, GetDeviceProfile_001, TestSize.Level1)
222 {
223 std::string devid = "";
224 DeviceProfile deviceProfile;
225 int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().GetDeviceProfile(devid, deviceProfile);
226 EXPECT_EQ(DP_PERMISSION_DENIED, ret);
227 }
228
229 HWTEST_F(DpProfileServiceTest, GetServiceProfile_001, TestSize.Level1)
230 {
231 std::string devid = "";
232 std::string serName = "";
233 ServiceProfile serviceProfile;
234 int32_t ret =
235 DistributedDeviceProfileServiceNew::GetInstance().GetServiceProfile(devid, serName, serviceProfile);
236 EXPECT_EQ(DP_PERMISSION_DENIED, ret);
237 }
238
239 HWTEST_F(DpProfileServiceTest, GetCharacteristicProfile_001, TestSize.Level1)
240 {
241 std::string devid = "";
242 std::string serName = "";
243 std::string charKey = "";
244 CharacteristicProfile charProfile;
245 int32_t ret =
246 DistributedDeviceProfileServiceNew::GetInstance().GetCharacteristicProfile(devid,
247 serName, charKey, charProfile);
248 EXPECT_EQ(DP_PERMISSION_DENIED, ret);
249 }
250
251 HWTEST_F(DpProfileServiceTest, DeleteServiceProfile_001, TestSize.Level1)
252 {
253 std::string devid = "";
254 std::string serName = "";
255 int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().DeleteServiceProfile(devid, serName);
256 EXPECT_EQ(DP_PERMISSION_DENIED, ret);
257 }
258
259 HWTEST_F(DpProfileServiceTest, DeleteCharacteristicProfile_001, TestSize.Level1)
260 {
261 std::string devid = "";
262 std::string serName = "";
263 std::string charKey = "";
264 int32_t ret =
265 DistributedDeviceProfileServiceNew::GetInstance().DeleteCharacteristicProfile(devid, serName, charKey);
266 EXPECT_EQ(DP_PERMISSION_DENIED, ret);
267 }
268
269 HWTEST_F(DpProfileServiceTest, SubscribeDeviceProfile_001, TestSize.Level1)
270 {
271 SubscribeInfo subscriberInfo;
272 int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().SubscribeDeviceProfile(subscriberInfo);
273 EXPECT_EQ(DP_PERMISSION_DENIED, ret);
274 }
275
276 HWTEST_F(DpProfileServiceTest, UnSubscribeDeviceProfile_001, TestSize.Level1)
277 {
278 SubscribeInfo subscriberInfo;
279 int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().UnSubscribeDeviceProfile(subscriberInfo);
280 EXPECT_EQ(DP_PERMISSION_DENIED, ret);
281 }
282
283 HWTEST_F(DpProfileServiceTest, SyncDeviceProfile_001, TestSize.Level1)
284 {
285 DistributedDeviceProfile::DpSyncOptions syncOptions;
286 sptr<IRemoteObject> syncCallback = nullptr;
287 int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().SyncDeviceProfile(syncOptions, syncCallback);
288 EXPECT_EQ(DP_PERMISSION_DENIED, ret);
289 }
290
291 HWTEST_F(DpProfileServiceTest, Dump_001, TestSize.Level1)
292 {
293 int32_t fd = -1;
294 std::vector<std::u16string> args;
295 int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().Dump(fd, args);
296 EXPECT_EQ(DP_INVALID_PARAMS, ret);
297 }
298
299 HWTEST_F(DpProfileServiceTest, SubscribeDeviceProfileInited_001, TestSize.Level1)
300 {
301 int32_t saId = 1000;
302 OHOS::sptr<IRemoteObject> initedCb = sptr<IRemoteObject>(new DpInitedCallback());
303 int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().SubscribeDeviceProfileInited(saId, initedCb);
304 EXPECT_EQ(DP_PERMISSION_DENIED, ret);
305 }
306
307 HWTEST_F(DpProfileServiceTest, UnSubscribeDeviceProfileInited_001, TestSize.Level1)
308 {
309 int32_t saId = 1000;
310 int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().UnSubscribeDeviceProfileInited(saId);
311 EXPECT_EQ(DP_PERMISSION_DENIED, ret);
312 }
313 }
314 }
315