• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "distributed_device_profile_constants.h"
26 #include "distributed_device_profile_log.h"
27 #include "distributed_device_profile_errors.h"
28 #include "rdb_open_callback.h"
29 #include "distributed_device_profile_client.h"
30 
31 namespace OHOS {
32 namespace DistributedDeviceProfile {
33 using namespace testing::ext;
34 using namespace OHOS::NativeRdb;
35 using namespace std;
36 namespace {
37     const std::string TAG = "DistributedDeviceProfileClientRdbTest";
38     const uint32_t TEST_SA_ID = 4801;
39 }
40 class DistributedDeviceProfileClientRdbTest : public testing::Test {
41 public:
42     static void SetUpTestCase();
43     static void TearDownTestCase();
44     void SetUp();
45     void TearDown();
46     int ResultSize(std::shared_ptr<ResultSet>& resultSet);
47 
48     class SubscribeDPChangeListener : public ProfileChangeListenerStub {
49     public:
SubscribeDPChangeListener()50         SubscribeDPChangeListener()
51         {
52             cout << "SubscribeDPChangeListener constructor" << endl;
53         }
~SubscribeDPChangeListener()54         ~SubscribeDPChangeListener()
55         {
56             cout << "SubscribeDPChangeListener destructor" << endl;
57         }
OnTrustDeviceProfileAdd(const TrustDeviceProfile & profile)58         int32_t OnTrustDeviceProfileAdd(const TrustDeviceProfile& profile)
59         {
60             cout << "OnTrustDeviceProfileAdd" << profile.dump() <<endl;
61             return 0;
62         }
OnTrustDeviceProfileDelete(const TrustDeviceProfile & profile)63         int32_t OnTrustDeviceProfileDelete(const TrustDeviceProfile& profile)
64         {
65             cout << "OnTrustDeviceProfileDelete" << profile.dump() <<endl;
66             return 0;
67         }
OnTrustDeviceProfileUpdate(const TrustDeviceProfile & oldProfile,const TrustDeviceProfile & newProfile)68         int32_t OnTrustDeviceProfileUpdate(const TrustDeviceProfile& oldProfile, const TrustDeviceProfile& newProfile)
69         {
70             cout << "OnTrustDeviceProfileUpdate: oldProfile " << oldProfile.dump() <<endl;
71             cout << "OnTrustDeviceProfileUpdate: newProfile " << newProfile.dump() <<endl;
72             return 0;
73         }
OnDeviceProfileAdd(const DeviceProfile & profile)74         int32_t OnDeviceProfileAdd(const DeviceProfile& profile)
75         {
76             return 0;
77         }
OnDeviceProfileDelete(const DeviceProfile & profile)78         int32_t OnDeviceProfileDelete(const DeviceProfile& profile)
79         {
80             return 0;
81         }
OnDeviceProfileUpdate(const DeviceProfile & oldProfile,const DeviceProfile & newProfile)82         int32_t OnDeviceProfileUpdate(const DeviceProfile& oldProfile, const DeviceProfile& newProfile)
83         {
84             return 0;
85         }
OnServiceProfileAdd(const ServiceProfile & profile)86         int32_t OnServiceProfileAdd(const ServiceProfile& profile)
87         {
88             return 0;
89         }
OnServiceProfileDelete(const ServiceProfile & profile)90         int32_t OnServiceProfileDelete(const ServiceProfile& profile)
91         {
92             return 0;
93         }
OnServiceProfileUpdate(const ServiceProfile & oldProfile,const ServiceProfile & newProfile)94         int32_t OnServiceProfileUpdate(const ServiceProfile& oldProfile, const ServiceProfile& newProfile)
95         {
96             return 0;
97         }
OnCharacteristicProfileAdd(const CharacteristicProfile & profile)98         int32_t OnCharacteristicProfileAdd(const CharacteristicProfile& profile)
99         {
100             return 0;
101         }
OnCharacteristicProfileDelete(const CharacteristicProfile & profile)102         int32_t OnCharacteristicProfileDelete(const CharacteristicProfile& profile)
103         {
104             return 0;
105         }
OnCharacteristicProfileUpdate(const CharacteristicProfile & oldProfile,const CharacteristicProfile & newProfile)106         int32_t OnCharacteristicProfileUpdate(const CharacteristicProfile& oldProfile,
107             const CharacteristicProfile& newProfile)
108         {
109             return 0;
110         }
111     };
112 };
113 
SetUpTestCase()114 void DistributedDeviceProfileClientRdbTest::SetUpTestCase()
115 {
116     uint32_t saId = TEST_SA_ID;
117     std::string subscribekey = "trust_device_profile";
118     std::unordered_set<ProfileChangeType> subscribeTypes = {ProfileChangeType::TRUST_DEVICE_PROFILE_ADD,
119         ProfileChangeType::TRUST_DEVICE_PROFILE_UPDATE, ProfileChangeType::TRUST_DEVICE_PROFILE_DELETE};
120 
121     OHOS::sptr<IProfileChangeListener> subscribeDPChangeListener =
122         OHOS::sptr<ProfileChangeListenerStub>(new SubscribeDPChangeListener);
123     SubscribeInfo subscribeInfo(saId, subscribekey, subscribeTypes, subscribeDPChangeListener);
124     DistributedDeviceProfileClient::GetInstance().SubscribeDeviceProfile(subscribeInfo);
125 }
126 
TearDownTestCase()127 void DistributedDeviceProfileClientRdbTest::TearDownTestCase()
128 {
129 }
130 
SetUp()131 void DistributedDeviceProfileClientRdbTest::SetUp()
132 {
133 }
134 
TearDown()135 void DistributedDeviceProfileClientRdbTest::TearDown()
136 {
137 }
138 
ResultSize(std::shared_ptr<ResultSet> & resultSet)139 int DistributedDeviceProfileClientRdbTest::ResultSize(std::shared_ptr<ResultSet> &resultSet)
140 {
141     if (resultSet == nullptr || resultSet->GoToFirstRow() != E_OK) {
142         return 0;
143     }
144     int count;
145     resultSet->GetRowCount(count);
146     return count;
147 }
148 
149 /*
150  * @tc.name: PutAccessControlProfile_001
151  * @tc.desc: Normal testCase of DistributedDeviceProfileClientRdbTest for CRUD
152  * @tc.type: FUNC
153  */
154 HWTEST_F(DistributedDeviceProfileClientRdbTest, PutAccessControlProfile_001, TestSize.Level1)
155 {
156     Accesser accesser;
157     accesser.SetAccesserDeviceId("acer4");
158     accesser.SetAccesserUserId(77);
159     accesser.SetAccesserAccountId("a4");
160     accesser.SetAccesserTokenId(777);
161     accesser.SetAccesserBundleName("b4");
162     accesser.SetAccesserHapSignature("h4");
163     accesser.SetAccesserBindLevel(1);
164 
165     Accessee accessee;
166     accessee.SetAccesseeDeviceId("acee4");
167     accessee.SetAccesseeUserId(88);
168     accessee.SetAccesseeAccountId("a4");
169     accessee.SetAccesseeTokenId(888);
170     accessee.SetAccesseeBundleName("bb4");
171     accessee.SetAccesseeHapSignature("h4");
172     accessee.SetAccesseeBindLevel(1);
173 
174     AccessControlProfile profile;
175     profile.SetTrustDeviceId("6666");
176     profile.SetSessionKey("key1");
177     profile.SetBindType(4);
178     profile.SetAuthenticationType(99);
179     profile.SetDeviceIdType(1);
180     profile.SetDeviceIdHash("abcd");
181     profile.SetStatus(0);
182     profile.SetValidPeriod(66);
183     profile.SetLastAuthTime(20);
184     profile.SetBindLevel(0);
185 
186     profile.SetAccesser(accesser);
187     profile.SetAccessee(accessee);
188     int32_t ret = OHOS::DistributedDeviceProfile::DistributedDeviceProfileClient::
189         GetInstance().PutAccessControlProfile(profile);
190     ValuesBucket value;
191     ProfileUtils::AccessControlProfileToEntries(profile, value);
192     ProfileUtils::EntriesToAccessControlProfile(value, profile);
193     value.Clear();
194     ProfileUtils::AccesserToEntries(profile, value);
195     ProfileUtils::EntriesToAccesser(value, accesser);
196     value.Clear();
197     ProfileUtils::AccesseeToEntries(profile, value);
198     ProfileUtils::EntriesToAccessee(value, accessee);
199     EXPECT_EQ(ret, DP_PERMISSION_DENIED);
200 }
201 
202 /*
203  * @tc.name: GetAccessControlProfile_001
204  * @tc.desc: Normal testCase of DistributedDeviceProfileClientRdbTest for CRUD
205  * @tc.type: FUNC
206  */
207 HWTEST_F(DistributedDeviceProfileClientRdbTest, GetAccessControlProfile_001, TestSize.Level1)
208 {
209     std::vector<AccessControlProfile> profile;
210     std::map<std::string, std::string> parms;
211     parms.insert({{"userId", "22"}, {"bundleName", "bb1"}, {"bindType", "1"}, {"status", "0"}});
212     int32_t ret = OHOS::DistributedDeviceProfile::DistributedDeviceProfileClient::
213         GetInstance().GetAccessControlProfile(parms, profile);
214     EXPECT_EQ(ret, DP_PERMISSION_DENIED);
215 }
216 
217 /*
218  * @tc.name: UpdateAccessControlProfile_001
219  * @tc.desc: Normal testCase of DistributedDeviceProfileClientRdbTest for CRUD
220  * @tc.type: FUNC
221  */
222 HWTEST_F(DistributedDeviceProfileClientRdbTest, UpdateAccessControlProfile_001, TestSize.Level1)
223 {
224     AccessControlProfile profile;
225     int32_t ret = OHOS::DistributedDeviceProfile::DistributedDeviceProfileClient::
226         GetInstance().UpdateAccessControlProfile(profile);
227     EXPECT_EQ(ret, DP_PERMISSION_DENIED);
228 }
229 
230 /*
231  * @tc.name: DeleteAccessControlProfile_001
232  * @tc.desc: Normal testCase of DistributedDeviceProfileClientRdbTest for CRUD
233  * @tc.type: FUNC
234  */
235 HWTEST_F(DistributedDeviceProfileClientRdbTest, DeleteAccessControlProfile_001, TestSize.Level1)
236 {
237     int32_t ret = OHOS::DistributedDeviceProfile::DistributedDeviceProfileClient::
238         GetInstance().DeleteAccessControlProfile(9);
239     EXPECT_EQ(ret, DP_PERMISSION_DENIED);
240 }
241 
242 /*
243  * @tc.name: GetAllAccessControlProfile_001
244  * @tc.desc: Normal testCase of DistributedDeviceProfileClientRdbTest for CRUD
245  * @tc.type: FUNC
246  */
247 HWTEST_F(DistributedDeviceProfileClientRdbTest, GetAllAccessControlProfile_001, TestSize.Level1)
248 {
249     std::vector<AccessControlProfile> profile;
250     int32_t ret = OHOS::DistributedDeviceProfile::DistributedDeviceProfileClient::
251         GetInstance().GetAllAccessControlProfile(profile);
252     EXPECT_EQ(ret, DP_PERMISSION_DENIED);
253 }
254 
255 /*
256  * @tc.name: GetAllAclIncludeLnnAcl_001
257  * @tc.desc: Normal testCase of DistributedDeviceProfileClientRdbTest for CRUD
258  * @tc.type: FUNC
259  */
260 HWTEST_F(DistributedDeviceProfileClientRdbTest, GetAllAclIncludeLnnAcl_001, TestSize.Level1)
261 {
262     std::vector<AccessControlProfile> profile;
263     int32_t ret = OHOS::DistributedDeviceProfile::DistributedDeviceProfileClient::
264         GetInstance().GetAllAclIncludeLnnAcl(profile);
265     EXPECT_EQ(ret, DP_PERMISSION_DENIED);
266 }
267 
268 /*
269  * @tc.name: GetAllTrustDeviceProfile_001
270  * @tc.desc: Normal testCase of DistributedDeviceProfileClientRdbTest for CRUD
271  * @tc.type: FUNC
272  */
273 HWTEST_F(DistributedDeviceProfileClientRdbTest, GetAllTrustDeviceProfile_001, TestSize.Level1)
274 {
275     std::vector<TrustDeviceProfile> profile;
276     int32_t ret = OHOS::DistributedDeviceProfile::DistributedDeviceProfileClient::
277         GetInstance().GetAllTrustDeviceProfile(profile);
278     EXPECT_EQ(ret, DP_PERMISSION_DENIED);
279 }
280 
281 /*
282  * @tc.name: GetTrustDeviceProfile_001
283  * @tc.desc: Normal testCase of DistributedDeviceProfileClientRdbTest for CRUD
284  * @tc.type: FUNC
285  */
286 HWTEST_F(DistributedDeviceProfileClientRdbTest, GetTrustDeviceProfile_001, TestSize.Level1)
287 {
288     TrustDeviceProfile profile;
289     int32_t ret = OHOS::DistributedDeviceProfile::DistributedDeviceProfileClient::
290         GetInstance().GetTrustDeviceProfile("123456", profile);
291     ValuesBucket value;
292     ProfileUtils::TrustDeviceProfileToEntries(profile, value);
293     ProfileUtils::EntriesToTrustDeviceProfile(value, profile);
294     EXPECT_EQ(ret, DP_PERMISSION_DENIED);
295 }
296 
297 HWTEST_F(DistributedDeviceProfileClientRdbTest, OnRemoveSystemAbility_001, TestSize.Level1)
298 {
299     TrustDeviceProfile profile;
300     int32_t ret = OHOS::DistributedDeviceProfile::DistributedDeviceProfileClient::
301         GetInstance().GetTrustDeviceProfile("123456", profile);
302     DistributedDeviceProfileClient::SystemAbilityListener saListeneer;
303     int32_t systemAbilityId = 1;
304     std::string deviceId = "deviceId_test";
305     saListeneer.OnRemoveSystemAbility(systemAbilityId, deviceId);
306     EXPECT_EQ(ret, DP_PERMISSION_DENIED);
307 }
308 } // namespace DistributedDeviceProfile
309 } // namespace OHOS
310