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 <sys/stat.h>
18 #include <sys/types.h>
19 #include <string>
20 #include <vector>
21 #include <iostream>
22
23 #include "profile_utils.h"
24 #include "trust_profile_manager.h"
25 #include "dp_subscribe_info.h"
26 #include "distributed_device_profile_constants.h"
27 #include "distributed_device_profile_log.h"
28 #include "distributed_device_profile_errors.h"
29 #include "distributed_device_profile_client.h"
30
31 namespace OHOS {
32 namespace DistributedDeviceProfile {
33 using namespace testing::ext;
34 using namespace std;
35 namespace {
36 const std::string TAG = "DPSubscribeInfoTest";
37 }
38 class DPSubscribeInfoTest : public testing::Test {
39 public:
40 static void SetUpTestCase();
41 static void TearDownTestCase();
42 void SetUp();
43 void TearDown();
44
45 class SubscribeDPChangeListener : public ProfileChangeListenerStub {
46 public:
SubscribeDPChangeListener()47 SubscribeDPChangeListener()
48 {
49 cout << "SubscribeDPChangeListener constructor" << endl;
50 }
~SubscribeDPChangeListener()51 ~SubscribeDPChangeListener()
52 {
53 cout << "SubscribeDPChangeListener destructor" << endl;
54 }
OnTrustDeviceProfileAdd(const TrustDeviceProfile & profile)55 int32_t OnTrustDeviceProfileAdd(const TrustDeviceProfile& profile)
56 {
57 cout << "OnTrustDeviceProfileAdd" << profile.dump() <<endl;
58 return 0;
59 }
OnTrustDeviceProfileDelete(const TrustDeviceProfile & profile)60 int32_t OnTrustDeviceProfileDelete(const TrustDeviceProfile& profile)
61 {
62 cout << "OnTrustDeviceProfileDelete" << profile.dump() <<endl;
63 return 0;
64 }
OnTrustDeviceProfileUpdate(const TrustDeviceProfile & oldProfile,const TrustDeviceProfile & newProfile)65 int32_t OnTrustDeviceProfileUpdate(const TrustDeviceProfile& oldProfile, const TrustDeviceProfile& newProfile)
66 {
67 cout << "OnTrustDeviceProfileUpdate: oldProfile " << oldProfile.dump() <<endl;
68 cout << "OnTrustDeviceProfileUpdate: newProfile " << newProfile.dump() <<endl;
69 return 0;
70 }
OnTrustDeviceProfileActive(const TrustDeviceProfile & profile)71 int32_t OnTrustDeviceProfileActive(const TrustDeviceProfile& profile)
72 {
73 cout << "OnTrustDeviceProfileActive" << profile.dump() <<endl;
74 return 0;
75 }
OnTrustDeviceProfileInactive(const TrustDeviceProfile & profile)76 int32_t OnTrustDeviceProfileInactive(const TrustDeviceProfile& profile)
77 {
78 cout << "OnTrustDeviceProfileInactive" << profile.dump() <<endl;
79 return 0;
80 }
OnDeviceProfileAdd(const DeviceProfile & profile)81 int32_t OnDeviceProfileAdd(const DeviceProfile& profile)
82 {
83 return 0;
84 }
OnDeviceProfileDelete(const DeviceProfile & profile)85 int32_t OnDeviceProfileDelete(const DeviceProfile& profile)
86 {
87 return 0;
88 }
OnDeviceProfileUpdate(const DeviceProfile & oldProfile,const DeviceProfile & newProfile)89 int32_t OnDeviceProfileUpdate(const DeviceProfile& oldProfile, const DeviceProfile& newProfile)
90 {
91 return 0;
92 }
OnServiceProfileAdd(const ServiceProfile & profile)93 int32_t OnServiceProfileAdd(const ServiceProfile& profile)
94 {
95 return 0;
96 }
OnServiceProfileDelete(const ServiceProfile & profile)97 int32_t OnServiceProfileDelete(const ServiceProfile& profile)
98 {
99 return 0;
100 }
OnServiceProfileUpdate(const ServiceProfile & oldProfile,const ServiceProfile & newProfile)101 int32_t OnServiceProfileUpdate(const ServiceProfile& oldProfile, const ServiceProfile& newProfile)
102 {
103 return 0;
104 }
OnCharacteristicProfileAdd(const CharacteristicProfile & profile)105 int32_t OnCharacteristicProfileAdd(const CharacteristicProfile& profile)
106 {
107 return 0;
108 }
OnCharacteristicProfileDelete(const CharacteristicProfile & profile)109 int32_t OnCharacteristicProfileDelete(const CharacteristicProfile& profile)
110 {
111 return 0;
112 }
OnCharacteristicProfileUpdate(const CharacteristicProfile & oldProfile,const CharacteristicProfile & newProfile)113 int32_t OnCharacteristicProfileUpdate(const CharacteristicProfile& oldProfile,
114 const CharacteristicProfile& newProfile)
115 {
116 return 0;
117 }
118 };
119 };
120
SetUpTestCase()121 void DPSubscribeInfoTest::SetUpTestCase()
122 {
123 }
124
TearDownTestCase()125 void DPSubscribeInfoTest::TearDownTestCase()
126 {
127 }
128
SetUp()129 void DPSubscribeInfoTest::SetUp()
130 {
131 }
132
TearDown()133 void DPSubscribeInfoTest::TearDown()
134 {
135 }
136
137 /*
138 * @tc.name: Get_001
139 * @tc.desc: Normal testCase of DPSubscribeInfoTest for CRUD
140 * @tc.type: FUNC
141 */
142 HWTEST_F(DPSubscribeInfoTest, Get_001, TestSize.Level1)
143 {
144 uint32_t saId = 4801;
145 std::string subscribeKey = "trust_device_profile";
146 std::unordered_set<ProfileChangeType> subscribeTypes = {ProfileChangeType::TRUST_DEVICE_PROFILE_ADD,
147 ProfileChangeType::TRUST_DEVICE_PROFILE_UPDATE, ProfileChangeType::TRUST_DEVICE_PROFILE_DELETE};
148 OHOS::sptr<IProfileChangeListener> subscribeDPChangeListener =
149 sptr<IProfileChangeListener>(new DPSubscribeInfoTest::SubscribeDPChangeListener);
150 SubscribeInfo subscribeInfo(saId, subscribeKey, subscribeTypes, subscribeDPChangeListener);
151 uint32_t id = subscribeInfo.GetSaId();
152 subscribeKey = subscribeInfo.GetSubscribeKey();
153 subscribeTypes = subscribeInfo.GetProfileChangeTypes();
154 subscribeInfo.GetListener();
155 EXPECT_EQ(id, saId);
156 }
157
158 /*
159 * @tc.name: Set_001
160 * @tc.desc: Normal testCase of DPSubscribeInfoTest for CRUD
161 * @tc.type: FUNC
162 */
163 HWTEST_F(DPSubscribeInfoTest, Set_001, TestSize.Level1)
164 {
165 uint32_t saId = 4801;
166 std::string subscribekey = "trust_device_profile";
167 OHOS::sptr<IProfileChangeListener> subscribeDPChangeListener = nullptr;
168 SubscribeInfo subscribeInfo;
169 subscribeInfo.SetSaId(saId);
170 subscribeInfo.SetSubscribeKey(subscribekey);
171 subscribeInfo.SetListener(subscribeDPChangeListener);
172 subscribeInfo.dump();
173 EXPECT_EQ(subscribeInfo.saId_, saId);
174 }
175
176 /*
177 * @tc.name: Set_002
178 * @tc.desc: Normal testCase of DPSubscribeInfoTest for CRUD
179 * @tc.type: FUNC
180 */
181 HWTEST_F(DPSubscribeInfoTest, Set_002, TestSize.Level1)
182 {
183 SubscribeInfo subscribeInfo;
184 std::string deviceId = "";
185 std::string deviceAttribute = "";
186 subscribeInfo.SetSubscribeKey(deviceId, deviceAttribute);
187 std::string serviceName = "";
188 std::string serviceAttribute = "";
189 subscribeInfo.SetSubscribeKey(deviceId, serviceName, serviceAttribute);
190 std::string characteristicKey = "";
191 std::string characteristicAttribute = "";
192 subscribeInfo.SetSubscribeKey(deviceId, serviceName, characteristicKey, characteristicAttribute);
193 std::string subscribeKey = CHAR_PREFIX + SEPARATOR + deviceId + SEPARATOR + serviceName + SEPARATOR+
194 characteristicKey + SEPARATOR + characteristicAttribute;
195 EXPECT_EQ(subscribeInfo.subscribeKey_, subscribeKey);
196
197 subscribeInfo.AddProfileChangeType(ProfileChangeType::TRUST_DEVICE_PROFILE_ADD);
198 }
199
200 /*
201 * @tc.name: Marshalling_001
202 * @tc.desc: Normal testCase of DPSubscribeInfoTest for CRUD
203 * @tc.type: FUNC
204 */
205 HWTEST_F(DPSubscribeInfoTest, Marshalling_001, TestSize.Level1)
206 {
207 SubscribeInfo subscribeInfo;
208 SubscribeInfo subscribeInfo_2;
209 SubscribeCompare cmp;
210 cmp(subscribeInfo, subscribeInfo_2);
211 SubscribeHash hash;
212 hash(subscribeInfo_2);
213 subscribeInfo.SetListener(nullptr);
214 OHOS::MessageParcel parcel;
215 TrustDeviceProfile profile;
216 AccessControlProfile aclProfile;
217 Accesser accesser;
218 Accessee accessee;
219 accesser.Marshalling(parcel);
220 accesser.UnMarshalling(parcel);
221 accessee.Marshalling(parcel);
222 accessee.UnMarshalling(parcel);
223 aclProfile.Marshalling(parcel);
224 aclProfile.UnMarshalling(parcel);
225 profile.Marshalling(parcel);
226 profile.UnMarshalling(parcel);
227 bool ret = subscribeInfo.Marshalling(parcel);
228 EXPECT_EQ(ret, false);
229 ret = subscribeInfo.UnMarshalling(parcel);
230 EXPECT_EQ(ret, false);
231 }
232
233 /*
234 * @tc.name: Stub_001
235 * @tc.desc: Normal testCase of DPSubscribeInfoTest for CRUD
236 * @tc.type: FUNC
237 */
238 HWTEST_F(DPSubscribeInfoTest, Stub_001, TestSize.Level1)
239 {
240 uint32_t saId = 4801;
241 std::string subscribeKey = "trust_device_profile";
242 std::unordered_set<ProfileChangeType> subscribeTypes = {ProfileChangeType::TRUST_DEVICE_PROFILE_ADD,
243 ProfileChangeType::TRUST_DEVICE_PROFILE_UPDATE, ProfileChangeType::TRUST_DEVICE_PROFILE_DELETE};
244 OHOS::sptr<IProfileChangeListener> subscribeDPChangeListener =
245 sptr<IProfileChangeListener>(new DPSubscribeInfoTest::SubscribeDPChangeListener);
246 SubscribeInfo subscribeInfo(saId, subscribeKey, subscribeTypes, subscribeDPChangeListener);
247 OHOS::sptr<IProfileChangeListener> proxy = OHOS::iface_cast<IProfileChangeListener>(subscribeInfo.GetListener());
248 TrustDeviceProfile oldTrustProfile;
249 TrustDeviceProfile newTrustProfile;
250 ASSERT_NE(proxy, nullptr);
251 int32_t ret = proxy->OnTrustDeviceProfileAdd(oldTrustProfile);
252 EXPECT_EQ(ret, DP_SUCCESS);
253 ret = proxy->OnTrustDeviceProfileDelete(oldTrustProfile);
254 EXPECT_EQ(ret, DP_SUCCESS);
255 ret = proxy->OnTrustDeviceProfileUpdate(oldTrustProfile, newTrustProfile);
256 EXPECT_EQ(ret, DP_SUCCESS);
257
258 DeviceProfile oldDeviceProfile;
259 DeviceProfile newDeviceProfile;
260 ret = proxy->OnDeviceProfileAdd(oldDeviceProfile);
261 EXPECT_EQ(ret, DP_SUCCESS);
262 ret = proxy->OnDeviceProfileDelete(oldDeviceProfile);
263 EXPECT_EQ(ret, DP_SUCCESS);
264 ret = proxy->OnDeviceProfileUpdate(oldDeviceProfile, newDeviceProfile);
265 EXPECT_EQ(ret, DP_SUCCESS);
266
267 ServiceProfile oldServiceProfile;
268 ServiceProfile newServiceProfile;
269 ret = proxy->OnServiceProfileAdd(oldServiceProfile);
270 EXPECT_EQ(ret, DP_SUCCESS);
271 ret = proxy->OnServiceProfileDelete(oldServiceProfile);
272 EXPECT_EQ(ret, DP_SUCCESS);
273 ret = proxy->OnServiceProfileUpdate(oldServiceProfile, newServiceProfile);
274 EXPECT_EQ(ret, DP_SUCCESS);
275
276 CharacteristicProfile oldChaProfile;
277 CharacteristicProfile newChaProfile;
278 ret = proxy->OnCharacteristicProfileAdd(oldChaProfile);
279 EXPECT_EQ(ret, DP_SUCCESS);
280 ret = proxy->OnCharacteristicProfileDelete(oldChaProfile);
281 EXPECT_EQ(ret, DP_SUCCESS);
282 ret = proxy->OnCharacteristicProfileUpdate(oldChaProfile, newChaProfile);
283 EXPECT_EQ(ret, DP_SUCCESS);
284 }
285
286 /*
287 * @tc.name: Stub_002
288 * @tc.desc: Normal testCase of DPSubscribeInfoTest for CRUD
289 * @tc.type: FUNC
290 */
291 HWTEST_F(DPSubscribeInfoTest, Stub_002, TestSize.Level1)
292 {
293 uint32_t saId = 4801;
294 std::string subscribeKey = "trust_device_profile";
295 std::unordered_set<ProfileChangeType> subscribeTypes = {ProfileChangeType::TRUST_DEVICE_PROFILE_ACTIVE,
296 ProfileChangeType::TRUST_DEVICE_PROFILE_INACTIVE};
297 OHOS::sptr<IProfileChangeListener> subscribeDPChangeListener =
298 sptr<IProfileChangeListener>(new DPSubscribeInfoTest::SubscribeDPChangeListener);
299 SubscribeInfo subscribeInfo(saId, subscribeKey, subscribeTypes, subscribeDPChangeListener);
300 OHOS::sptr<IProfileChangeListener> proxy = OHOS::iface_cast<IProfileChangeListener>(subscribeInfo.GetListener());
301 TrustDeviceProfile profile;
302 profile.SetPeerUserId(1001);
303 profile.GetPeerUserId();
304 ASSERT_NE(proxy, nullptr);
305 int32_t ret = proxy->OnTrustDeviceProfileActive(profile);
306 EXPECT_EQ(ret, DP_SUCCESS);
307 ret = proxy->OnTrustDeviceProfileInactive(profile);
308 EXPECT_EQ(ret, DP_SUCCESS);
309 }
310
311 /*
312 * @tc.name: IProfileChangeListener_001
313 * @tc.desc: Normal testCase of DPSubscribeInfoTest for CRUD
314 * @tc.type: FUNC
315 */
316 HWTEST_F(DPSubscribeInfoTest, IProfileChangeListener_001, TestSize.Level1)
317 {
318 OHOS::sptr<IProfileChangeListener> subscribeDPChangeListener =
319 new(std::nothrow) DPSubscribeInfoTest::SubscribeDPChangeListener;
320 TrustDeviceProfile profile;
321 profile.SetPeerUserId(1001);
322 int32_t ret = subscribeDPChangeListener->OnTrustDeviceProfileActive(profile);
323 EXPECT_EQ(ret, DP_SUCCESS);
324 ret = subscribeDPChangeListener->OnTrustDeviceProfileInactive(profile);
325 EXPECT_EQ(ret, DP_SUCCESS);
326 }
327 } // namespace DistributedDeviceProfile
328 } // namespace OHOS
329