1 /*
2 * Copyright (c) 2023 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 }
OnDeviceProfileAdd(const DeviceProfile & profile)71 int32_t OnDeviceProfileAdd(const DeviceProfile& profile)
72 {
73 return 0;
74 }
OnDeviceProfileDelete(const DeviceProfile & profile)75 int32_t OnDeviceProfileDelete(const DeviceProfile& profile)
76 {
77 return 0;
78 }
OnDeviceProfileUpdate(const DeviceProfile & oldProfile,const DeviceProfile & newProfile)79 int32_t OnDeviceProfileUpdate(const DeviceProfile& oldProfile, const DeviceProfile& newProfile)
80 {
81 return 0;
82 }
OnServiceProfileAdd(const ServiceProfile & profile)83 int32_t OnServiceProfileAdd(const ServiceProfile& profile)
84 {
85 return 0;
86 }
OnServiceProfileDelete(const ServiceProfile & profile)87 int32_t OnServiceProfileDelete(const ServiceProfile& profile)
88 {
89 return 0;
90 }
OnServiceProfileUpdate(const ServiceProfile & oldProfile,const ServiceProfile & newProfile)91 int32_t OnServiceProfileUpdate(const ServiceProfile& oldProfile, const ServiceProfile& newProfile)
92 {
93 return 0;
94 }
OnCharacteristicProfileAdd(const CharacteristicProfile & profile)95 int32_t OnCharacteristicProfileAdd(const CharacteristicProfile& profile)
96 {
97 return 0;
98 }
OnCharacteristicProfileDelete(const CharacteristicProfile & profile)99 int32_t OnCharacteristicProfileDelete(const CharacteristicProfile& profile)
100 {
101 return 0;
102 }
OnCharacteristicProfileUpdate(const CharacteristicProfile & oldProfile,const CharacteristicProfile & newProfile)103 int32_t OnCharacteristicProfileUpdate(const CharacteristicProfile& oldProfile,
104 const CharacteristicProfile& newProfile)
105 {
106 return 0;
107 }
108 };
109 };
110
SetUpTestCase()111 void DPSubscribeInfoTest::SetUpTestCase()
112 {
113 }
114
TearDownTestCase()115 void DPSubscribeInfoTest::TearDownTestCase()
116 {
117 }
118
SetUp()119 void DPSubscribeInfoTest::SetUp()
120 {
121 }
122
TearDown()123 void DPSubscribeInfoTest::TearDown()
124 {
125 }
126
127 /*
128 * @tc.name: Get_001
129 * @tc.desc: Normal testCase of DPSubscribeInfoTest for CRUD
130 * @tc.type: FUNC
131 */
132 HWTEST_F(DPSubscribeInfoTest, Get_001, TestSize.Level1)
133 {
134 uint32_t saId = 4801;
135 std::string subscribeKey = "trust_device_profile";
136 std::unordered_set<ProfileChangeType> subscribeTypes = {ProfileChangeType::TRUST_DEVICE_PROFILE_ADD,
137 ProfileChangeType::TRUST_DEVICE_PROFILE_UPDATE, ProfileChangeType::TRUST_DEVICE_PROFILE_DELETE};
138 OHOS::sptr<IProfileChangeListener> subscribeDPChangeListener =
139 new(std::nothrow) DPSubscribeInfoTest::SubscribeDPChangeListener;
140 SubscribeInfo subscribeInfo(saId, subscribeKey, subscribeTypes, subscribeDPChangeListener);
141 saId = subscribeInfo.GetSaId();
142 subscribeKey = subscribeInfo.GetSubscribeKey();
143 subscribeTypes = subscribeInfo.GetProfileChangeTypes();
144 subscribeInfo.GetListener();
145 int32_t ret = DP_SUCCESS;
146 EXPECT_EQ(ret, DP_SUCCESS);
147 }
148
149 /*
150 * @tc.name: Set_001
151 * @tc.desc: Normal testCase of DPSubscribeInfoTest for CRUD
152 * @tc.type: FUNC
153 */
154 HWTEST_F(DPSubscribeInfoTest, Set_001, TestSize.Level1)
155 {
156 uint32_t saId = 4801;
157 std::string subscribekey = "trust_device_profile";
158 OHOS::sptr<IProfileChangeListener> subscribeDPChangeListener = nullptr;
159 SubscribeInfo subscribeInfo;
160 subscribeInfo.SetSaId(saId);
161 subscribeInfo.SetSubscribeKey(subscribekey);
162 subscribeInfo.SetListener(subscribeDPChangeListener);
163 subscribeInfo.dump();
164 int32_t ret = DP_SUCCESS;
165 EXPECT_EQ(ret, DP_SUCCESS);
166 }
167
168 /*
169 * @tc.name: Set_002
170 * @tc.desc: Normal testCase of DPSubscribeInfoTest for CRUD
171 * @tc.type: FUNC
172 */
173 HWTEST_F(DPSubscribeInfoTest, Set_002, TestSize.Level1)
174 {
175 SubscribeInfo subscribeInfo;
176 std::string deviceId = "";
177 std::string deviceAttribute = "";
178 subscribeInfo.SetSubscribeKey(deviceId, deviceAttribute);
179 std::string serviceName = "";
180 std::string serviceAttribute = "";
181 subscribeInfo.SetSubscribeKey(deviceId, serviceName, serviceAttribute);
182 std::string characteristicKey = "";
183 std::string characteristicAttribute = "";
184 subscribeInfo.SetSubscribeKey(deviceId, serviceName, characteristicKey, characteristicAttribute);
185
186 int32_t ret = DP_SUCCESS;
187 EXPECT_EQ(ret, DP_SUCCESS);
188 }
189
190 /*
191 * @tc.name: AddProfileChangeType_001
192 * @tc.desc: Normal testCase of DPSubscribeInfoTest for CRUD
193 * @tc.type: FUNC
194 */
195 HWTEST_F(DPSubscribeInfoTest, AddProfileChangeType_001, TestSize.Level1)
196 {
197 SubscribeInfo subscribeInfo;
198 subscribeInfo.AddProfileChangeType(ProfileChangeType::TRUST_DEVICE_PROFILE_ADD);
199 int32_t ret = DP_SUCCESS;
200 EXPECT_EQ(ret, DP_SUCCESS);
201 }
202
203 /*
204 * @tc.name: Marshalling_001
205 * @tc.desc: Normal testCase of DPSubscribeInfoTest for CRUD
206 * @tc.type: FUNC
207 */
208 HWTEST_F(DPSubscribeInfoTest, Marshalling_001, TestSize.Level1)
209 {
210 SubscribeInfo subscribeInfo;
211 SubscribeInfo subscribeInfo_2;
212 SubscribeCompare cmp;
213 cmp(subscribeInfo, subscribeInfo_2);
214 SubscribeHash hash;
215 hash(subscribeInfo_2);
216 subscribeInfo.SetListener(nullptr);
217 OHOS::MessageParcel parcel;
218 TrustDeviceProfile profile;
219 AccessControlProfile aclProfile;
220 Accesser accesser;
221 Accessee accessee;
222 accesser.Marshalling(parcel);
223 accesser.UnMarshalling(parcel);
224 accessee.Marshalling(parcel);
225 accessee.UnMarshalling(parcel);
226 aclProfile.Marshalling(parcel);
227 aclProfile.UnMarshalling(parcel);
228 profile.Marshalling(parcel);
229 profile.UnMarshalling(parcel);
230 bool ret = subscribeInfo.Marshalling(parcel);
231 EXPECT_EQ(ret, false);
232 ret = subscribeInfo.UnMarshalling(parcel);
233 EXPECT_EQ(ret, false);
234 }
235
236 /*
237 * @tc.name: Stub_001
238 * @tc.desc: Normal testCase of DPSubscribeInfoTest for CRUD
239 * @tc.type: FUNC
240 */
241 HWTEST_F(DPSubscribeInfoTest, Stub_001, TestSize.Level1)
242 {
243 uint32_t saId = 4801;
244 std::string subscribeKey = "trust_device_profile";
245 std::unordered_set<ProfileChangeType> subscribeTypes = {ProfileChangeType::TRUST_DEVICE_PROFILE_ADD,
246 ProfileChangeType::TRUST_DEVICE_PROFILE_UPDATE, ProfileChangeType::TRUST_DEVICE_PROFILE_DELETE};
247 OHOS::sptr<IProfileChangeListener> subscribeDPChangeListener =
248 new(std::nothrow) DPSubscribeInfoTest::SubscribeDPChangeListener;
249 SubscribeInfo subscribeInfo(saId, subscribeKey, subscribeTypes, subscribeDPChangeListener);
250 OHOS::sptr<IProfileChangeListener> proxy = OHOS::iface_cast<IProfileChangeListener>(subscribeInfo.GetListener());
251 TrustDeviceProfile oldTrustProfile;
252 TrustDeviceProfile newTrustProfile;
253 ASSERT_NE(proxy, nullptr);
254 int32_t ret = proxy->OnTrustDeviceProfileAdd(oldTrustProfile);
255 EXPECT_EQ(ret, DP_SUCCESS);
256 ret = proxy->OnTrustDeviceProfileDelete(oldTrustProfile);
257 EXPECT_EQ(ret, DP_SUCCESS);
258 ret = proxy->OnTrustDeviceProfileUpdate(oldTrustProfile, newTrustProfile);
259 EXPECT_EQ(ret, DP_SUCCESS);
260
261 DeviceProfile oldDeviceProfile;
262 DeviceProfile newDeviceProfile;
263 ret = proxy->OnDeviceProfileAdd(oldDeviceProfile);
264 EXPECT_EQ(ret, DP_SUCCESS);
265 ret = proxy->OnDeviceProfileDelete(oldDeviceProfile);
266 EXPECT_EQ(ret, DP_SUCCESS);
267 ret = proxy->OnDeviceProfileUpdate(oldDeviceProfile, newDeviceProfile);
268 EXPECT_EQ(ret, DP_SUCCESS);
269
270 ServiceProfile oldServiceProfile;
271 ServiceProfile newServiceProfile;
272 ret = proxy->OnServiceProfileAdd(oldServiceProfile);
273 EXPECT_EQ(ret, DP_SUCCESS);
274 ret = proxy->OnServiceProfileDelete(oldServiceProfile);
275 EXPECT_EQ(ret, DP_SUCCESS);
276 ret = proxy->OnServiceProfileUpdate(oldServiceProfile, newServiceProfile);
277 EXPECT_EQ(ret, DP_SUCCESS);
278
279 CharacteristicProfile oldChaProfile;
280 CharacteristicProfile newChaProfile;
281 ret = proxy->OnCharacteristicProfileAdd(oldChaProfile);
282 EXPECT_EQ(ret, DP_SUCCESS);
283 ret = proxy->OnCharacteristicProfileDelete(oldChaProfile);
284 EXPECT_EQ(ret, DP_SUCCESS);
285 ret = proxy->OnCharacteristicProfileUpdate(oldChaProfile, newChaProfile);
286 EXPECT_EQ(ret, DP_SUCCESS);
287 }
288 } // namespace DistributedDeviceProfile
289 } // namespace OHOS
290