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 #define private public
17 #define protected public
18 #include <string>
19 #include <vector>
20 #include "gtest/gtest.h"
21 #include "refbase.h"
22 #include "rdb_errno.h"
23 #include "profile_utils.h"
24 #include "distributed_device_profile_constants.h"
25 #include "distributed_device_profile_errors.h"
26 #include "distributed_device_profile_enums.h"
27 #include "dm_constants.h"
28 #undef private
29 #undef protected
30
31 namespace OHOS {
32 namespace DistributedDeviceProfile {
33 using namespace testing::ext;
34 using namespace OHOS::NativeRdb;
35 using namespace std;
36
37 class ProfileUtilsTest : public testing::Test {
38 public:
39 static void SetUpTestCase(void);
40 static void TearDownTestCase(void);
41 void SetUp();
42 void TearDown();
43 };
44
SetUpTestCase(void)45 void ProfileUtilsTest::SetUpTestCase(void) {
46 }
47
TearDownTestCase(void)48 void ProfileUtilsTest::TearDownTestCase(void) {
49 }
50
SetUp()51 void ProfileUtilsTest::SetUp() {
52 }
53
TearDown()54 void ProfileUtilsTest::TearDown() {
55 }
56
57 /**
58 * @tc.name: GetDbKeyAnonyString001
59 * @tc.desc: GetDbKeyAnonyString
60 * @tc.type: FUNC
61 * @tc.require:
62 */
63 HWTEST_F(ProfileUtilsTest, GetDbKeyAnonyString001, TestSize.Level1)
64 {
65 string len2 = "";
66 string res = ProfileUtils::GetDbKeyAnonyString(len2);
67 EXPECT_EQ("***", res);
68 }
69
70 /**
71 * @tc.name: GetDbKeyAnonyString002
72 * @tc.desc: GetDbKeyAnonyString
73 * @tc.type: FUNC
74 * @tc.require:
75 */
76 HWTEST_F(ProfileUtilsTest, GetDbKeyAnonyString002, TestSize.Level1)
77 {
78 string len2 = "ab";
79 string res = ProfileUtils::GetDbKeyAnonyString(len2);
80 EXPECT_EQ("***", res);
81 }
82
83 /**
84 * @tc.name: GetDbKeyAnonyString003
85 * @tc.desc: GetDbKeyAnonyString
86 * @tc.type: FUNC
87 * @tc.require:
88 */
89 HWTEST_F(ProfileUtilsTest, GetDbKeyAnonyString003, TestSize.Level1)
90 {
91 string len2 = "ab#abc";
92 string res = ProfileUtils::GetDbKeyAnonyString(len2);
93 EXPECT_EQ("ab#a***c", res);
94 }
95
96 /**
97 * @tc.name: GetAnonyString001
98 * @tc.desc: GetAnonyString failed, length < 3.
99 * @tc.type: FUNC
100 * @tc.require:
101 */
102 HWTEST_F(ProfileUtilsTest, GetAnonyString001, TestSize.Level1)
103 {
104 string len2 = "ab";
105 string res = ProfileUtils::GetAnonyString(len2);
106 EXPECT_EQ("***", res);
107 }
108
109 /**
110 * @tc.name: GetAnonyString002
111 * @tc.desc: GetAnonyString succeed, 3 <= length <= 20.
112 * @tc.type: FUNC
113 * @tc.require:
114 */
115 HWTEST_F(ProfileUtilsTest, GetAnonyString002, TestSize.Level1)
116 {
117 string len5 = "abcde";
118 string res = ProfileUtils::GetAnonyString(len5);
119 EXPECT_EQ("a***e", res);
120 }
121
122 /**
123 * @tc.name: GetAnonyString003
124 * @tc.desc: GetAnonyString succeed, length > 20.
125 * @tc.type: FUNC
126 * @tc.require:
127 */
128 HWTEST_F(ProfileUtilsTest, GetAnonyString003, TestSize.Level1)
129 {
130 string len22 = "abcdefghijklmnopqrstuv";
131 string res = ProfileUtils::GetAnonyString(len22);
132 EXPECT_EQ("abcd***stuv", res);
133 }
134
135 /**
136 * @tc.name: GetOnlineDevices001
137 * @tc.desc: GetOnlineDevices succeed.
138 * @tc.type: FUNC
139 * @tc.require:
140 */
141 HWTEST_F(ProfileUtilsTest, GetOnlineDevices001, TestSize.Level1)
142 {
143 vector<string> res = ProfileUtils::GetOnlineDevices();
144 EXPECT_EQ(0, res.size());
145 }
146
147 /**
148 * @tc.name: FilterAndGroupOnlineDevices001
149 * @tc.desc: FilterAndGroupOnlineDevices failed, deviceList.size() == 0.
150 * @tc.type: FUNC
151 * @tc.require:
152 */
153 HWTEST_F(ProfileUtilsTest, FilterAndGroupOnlineDevices001, TestSize.Level1)
154 {
155 vector<string> deviceList;
156 std::vector<std::string> ohBasedDevices;
157 std::vector<std::string> notOHBasedDevices;
158 bool res = ProfileUtils::FilterAndGroupOnlineDevices(deviceList, ohBasedDevices, notOHBasedDevices);
159 EXPECT_EQ(false, res);
160 }
161
162 /**
163 * @tc.name: FilterAndGroupOnlineDevices002
164 * @tc.desc: FilterAndGroupOnlineDevices failed, deviceList.size() > MAX_DEVICE_SIZE.
165 * @tc.type: FUNC
166 * @tc.require:
167 */
168 HWTEST_F(ProfileUtilsTest, FilterAndGroupOnlineDevices002, TestSize.Level1)
169 {
170 vector<string> deviceList;
171 for (int32_t i = 0; i < MAX_DEVICE_SIZE + 5; i++) {
172 deviceList.emplace_back("deviceId");
173 }
174 std::vector<std::string> ohBasedDevices;
175 std::vector<std::string> notOHBasedDevices;
176 bool res = ProfileUtils::FilterAndGroupOnlineDevices(deviceList, ohBasedDevices, notOHBasedDevices);
177 EXPECT_EQ(false, res);
178 }
179
180 /**
181 * @tc.name: FilterAndGroupOnlineDevices003
182 * @tc.desc: FilterAndGroupOnlineDevices failed
183 * @tc.type: FUNC
184 * @tc.require:
185 */
186 HWTEST_F(ProfileUtilsTest, FilterAndGroupOnlineDevices003, TestSize.Level1)
187 {
188 vector<string> deviceList;
189 for (int32_t i = 0; i < 5; i++) {
190 deviceList.emplace_back("deviceId");
191 }
192 std::vector<std::string> ohBasedDevices;
193 std::vector<std::string> notOHBasedDevices;
194 bool res = ProfileUtils::FilterAndGroupOnlineDevices(deviceList, ohBasedDevices, notOHBasedDevices);
195 EXPECT_EQ(false, res);
196 }
197
198 /**
199 * @tc.name: IsOHBasedDevice001
200 * @tc.desc: IsOHBasedDevice
201 * @tc.type: FUNC
202 * @tc.require:
203 */
204 HWTEST_F(ProfileUtilsTest, IsOHBasedDevice001, TestSize.Level1)
205 {
206 std::string extraData;
207 bool res = ProfileUtils::IsOHBasedDevice(extraData);
208 EXPECT_EQ(false, res);
209 }
210
211 /**
212 * @tc.name: IsOHBasedDevice002
213 * @tc.desc: IsOHBasedDevice failed
214 * @tc.type: FUNC
215 * @tc.require:
216 */
217 HWTEST_F(ProfileUtilsTest, IsOHBasedDevice002, TestSize.Level1)
218 {
219 std::string extraData = "abc";
220 bool res = ProfileUtils::IsOHBasedDevice(extraData);
221 EXPECT_EQ(false, res);
222 }
223
224 /**
225 * @tc.name: IsOHBasedDevice003
226 * @tc.desc: IsOHBasedDevice
227 * @tc.type: FUNC
228 * @tc.require:
229 */
230 HWTEST_F(ProfileUtilsTest, IsOHBasedDevice003, TestSize.Level1)
231 {
232 std::string osType = DistributedHardware::PARAM_KEY_OS_TYPE;
233 std::string extraData = "{\"" + osType + "\":10}";
234 bool res = ProfileUtils::IsOHBasedDevice(extraData);
235 EXPECT_EQ(true, res);
236 }
237
238 /**
239 * @tc.name: GetProfileType001
240 * @tc.desc: GetProfileType succeed.
241 * @tc.type: FUNC
242 * @tc.require:
243 */
244 HWTEST_F(ProfileUtilsTest, GetProfileType001, TestSize.Level1)
245 {
246 string minkey = "";
247 ProfileType res = ProfileUtils::GetProfileType(minkey);
248 EXPECT_EQ(ProfileType::PROFILE_TYPE_MIN, res);
249
250 string maxKey = "";
251 for (int32_t i = 0; i < 5000; i++) {
252 maxKey += 'a';
253 }
254 res = ProfileUtils::GetProfileType(maxKey);
255 EXPECT_EQ(ProfileType::PROFILE_TYPE_MIN, res);
256
257 string devKey = "dev111";
258 res = ProfileUtils::GetProfileType(devKey);
259 EXPECT_EQ(ProfileType::DEVICE_PROFILE, res);
260
261 string serKet = "svr111";
262 res = ProfileUtils::GetProfileType(serKet);
263 EXPECT_EQ(ProfileType::SERVICE_PROFILE, res);
264
265 string charKey = "char111";
266 res = ProfileUtils::GetProfileType(charKey);
267 EXPECT_EQ(ProfileType::CHAR_PROFILE, res);
268
269 string lastKey = "other";
270 res = ProfileUtils::GetProfileType(lastKey);
271 EXPECT_EQ(ProfileType::PROFILE_TYPE_MIN, res);
272 }
273
274 /**
275 * @tc.name: StartsWith001
276 * @tc.desc: StartsWith succeed.
277 * @tc.type: FUNC
278 * @tc.require:
279 */
280 HWTEST_F(ProfileUtilsTest, StartsWith001, TestSize.Level1)
281 {
282 string str = "abcdefg";
283 string prefix = "abc";
284 bool res = ProfileUtils::StartsWith(str, prefix);
285 EXPECT_EQ(true, res);
286 }
287
288 /**
289 * @tc.name: IsKeyValid001
290 * @tc.desc: IsKeyValid.
291 * @tc.type: FUNC
292 * @tc.require:
293 */
294 HWTEST_F(ProfileUtilsTest, IsKeyValid001, TestSize.Level1)
295 {
296 string key = "abc";
297 bool res = ProfileUtils::IsKeyValid(key);
298 EXPECT_EQ(true, res);
299
300 key = "";
301 res = ProfileUtils::IsKeyValid(key);
302 EXPECT_EQ(false, res);
303
304 key = "";
305 for (int32_t i = 0; i < 5000; i++) {
306 key += 'a';
307 }
308 res = ProfileUtils::IsKeyValid(key);
309 EXPECT_EQ(false, res);
310
311 key = "abc#";
312 res = ProfileUtils::IsKeyValid(key);
313 EXPECT_EQ(false, res);
314 }
315
316 /**
317 * @tc.name: GenerateDeviceProfileKey001
318 * @tc.desc: GenerateDeviceProfileKey.
319 * @tc.type: FUNC
320 * @tc.require:
321 */
322 HWTEST_F(ProfileUtilsTest, GenerateDeviceProfileKey001, TestSize.Level1)
323 {
324 string deviceId = "111";
325 string res = ProfileUtils::GenerateDeviceProfileKey(deviceId);
326 EXPECT_EQ("dev#111", res);
327 }
328
329 /**
330 * @tc.name: GenerateServiceProfileKey001
331 * @tc.desc: GenerateServiceProfileKey.
332 * @tc.type: FUNC
333 * @tc.require:
334 */
335 HWTEST_F(ProfileUtilsTest, GenerateServiceProfileKey001, TestSize.Level1)
336 {
337 string deviceId = "111";
338 string serviceName = "222";
339 string res = ProfileUtils::GenerateServiceProfileKey(deviceId, serviceName);
340 EXPECT_EQ("svr#111#222", res);
341 }
342
343 /**
344 * @tc.name: GenerateCharProfileKey001
345 * @tc.desc: GenerateCharProfileKey.
346 * @tc.type: FUNC
347 * @tc.require:
348 */
349 HWTEST_F(ProfileUtilsTest, GenerateCharProfileKey001, TestSize.Level1)
350 {
351 string deviceId = "111";
352 string serviceName = "222";
353 string charKey = "333";
354 string res = ProfileUtils::GenerateCharProfileKey(deviceId, serviceName, charKey);
355 EXPECT_EQ("char#111#222#333", res);
356 }
357
358 /**
359 * @tc.name: TrustDeviceProfileTranslateEntries001
360 * @tc.desc: TrustDeviceProfileToEntries and EntriesToTrustDeviceProfile.
361 * @tc.type: FUNC
362 * @tc.require:
363 */
364 HWTEST_F(ProfileUtilsTest, TrustDeviceProfileTranslateEntries001, TestSize.Level1)
365 {
366 TrustDeviceProfile profile;
367 profile.SetDeviceId("deviceId");
368 profile.SetDeviceIdHash("deviceIdHash");
369 profile.SetDeviceIdType(1);
370 profile.SetStatus(1);
371
372 ValuesBucket values;
373 ValueObject valueObject;
374 string strValue = "";
375 int32_t res1 = ProfileUtils::TrustDeviceProfileToEntries(profile, values);
376 EXPECT_EQ(DP_SUCCESS, res1);
377 values.GetObject(DEVICE_ID, valueObject);
378 valueObject.GetString(strValue);
379 EXPECT_EQ("deviceId", strValue);
380
381 TrustDeviceProfile outProfile;
382 int32_t res2 = ProfileUtils::EntriesToTrustDeviceProfile(values, outProfile);
383 EXPECT_EQ(DP_SUCCESS, res2);
384 EXPECT_EQ("deviceId", outProfile.GetDeviceId());
385 }
386
387 /**
388 * @tc.name: AccessControlProfileTranslateEntries001
389 * @tc.desc: AccessControlProfileToEntries and EntriesToAccessControlProfile.
390 * @tc.type: FUNC
391 * @tc.require:
392 */
393 HWTEST_F(ProfileUtilsTest, AccessControlProfileTranslateEntries001, TestSize.Level1)
394 {
395 Accesser accesser;
396 accesser.SetAccesserDeviceId("acer1");
397 accesser.SetAccesserUserId(11);
398 accesser.SetAccesserAccountId("a1");
399 accesser.SetAccesserTokenId(111);
400 accesser.SetAccesserBundleName("b1");
401 accesser.SetAccesserHapSignature("h1");
402 accesser.SetAccesserBindLevel(1);
403
404 Accessee accessee;
405 accessee.SetAccesseeDeviceId("acee1");
406 accessee.SetAccesseeUserId(22);
407 accessee.SetAccesseeAccountId("a1");
408 accessee.SetAccesseeTokenId(222);
409 accessee.SetAccesseeBundleName("bb1");
410 accessee.SetAccesseeHapSignature("h1");
411 accessee.SetAccesseeBindLevel(1);
412
413 AccessControlProfile profile;
414 profile.SetTrustDeviceId("123456");
415 profile.SetSessionKey("key1");
416 profile.SetBindType(1);
417 profile.SetAuthenticationType(1);
418 profile.SetDeviceIdType(1);
419 profile.SetDeviceIdHash("abcd");
420 profile.SetStatus(0);
421 profile.SetValidPeriod(1);
422 profile.SetLastAuthTime(5);
423 profile.SetBindLevel(0);
424
425 profile.SetAccesser(accesser);
426 profile.SetAccessee(accessee);
427
428 ValuesBucket values;
429 ValueObject valueObject;
430 string strValue = "";
431 int32_t res1 = ProfileUtils::AccessControlProfileToEntries(profile, values);
432 EXPECT_EQ(DP_SUCCESS, res1);
433 values.GetObject(TRUST_DEVICE_ID, valueObject);
434 valueObject.GetString(strValue);
435 EXPECT_EQ("123456", strValue);
436
437 AccessControlProfile outProfile;
438 int32_t res2 = ProfileUtils::EntriesToAccessControlProfile(values, outProfile);
439 EXPECT_EQ(DP_SUCCESS, res2);
440 EXPECT_EQ("123456", outProfile.GetTrustDeviceId());
441 }
442
443 HWTEST_F(ProfileUtilsTest, AccessControlProfileTranslateEntries002, TestSize.Level1)
444 {
445 Accesser accesser;
446 accesser.SetAccesserDeviceId("acer2");
447 accesser.SetAccesserUserId(11);
448 accesser.SetAccesserAccountId("a2");
449 accesser.SetAccesserTokenId(111);
450 accesser.SetAccesserBundleName("b2");
451 accesser.SetAccesserHapSignature("h2");
452 accesser.SetAccesserBindLevel(1);
453
454 Accessee accessee;
455 accessee.SetAccesseeDeviceId("acee2");
456 accessee.SetAccesseeUserId(22);
457 accessee.SetAccesseeAccountId("a2");
458 accessee.SetAccesseeTokenId(222);
459 accessee.SetAccesseeBundleName("bb2");
460 accessee.SetAccesseeHapSignature("h2");
461 accessee.SetAccesseeBindLevel(1);
462
463 AccessControlProfile profile;
464 profile.SetTrustDeviceId("1234567");
465 profile.SetSessionKey("key1");
466 profile.SetBindType(1);
467 profile.SetAuthenticationType(1);
468 profile.SetDeviceIdType(1);
469 profile.SetDeviceIdHash("abcd");
470 profile.SetStatus(0);
471 profile.SetValidPeriod(1);
472 profile.SetLastAuthTime(5);
473 profile.SetBindLevel(0);
474
475 profile.SetAccesser(accesser);
476 profile.SetAccessee(accessee);
477
478 ValuesBucket values;
479 ValueObject valueObject;
480 string strValue = "";
481 int32_t res1 = ProfileUtils::AccessControlProfileToEntries(profile, values);
482 EXPECT_EQ(DP_SUCCESS, res1);
483 values.GetObject(TRUST_DEVICE_ID, valueObject);
484 valueObject.GetString(strValue);
485 EXPECT_EQ("1234567", strValue);
486
487 AccessControlProfile outProfile;
488 int32_t res2 = ProfileUtils::EntriesToAccessControlProfile(values, outProfile);
489 EXPECT_EQ(DP_SUCCESS, res2);
490 EXPECT_EQ("1234567", outProfile.GetTrustDeviceId());
491 }
492
493 /**
494 * @tc.name: AccesserTranslateEntries001
495 * @tc.desc: AccesserToEntries and EntriesToAccesser.
496 * @tc.type: FUNC
497 * @tc.require:
498 */
499 HWTEST_F(ProfileUtilsTest, AccesserTranslateEntries001, TestSize.Level1)
500 {
501 Accesser accesser;
502 accesser.SetAccesserDeviceId("acer1");
503 accesser.SetAccesserUserId(11);
504 accesser.SetAccesserAccountId("a1");
505 accesser.SetAccesserTokenId(111);
506 accesser.SetAccesserBundleName("b1");
507 accesser.SetAccesserHapSignature("h1");
508 accesser.SetAccesserBindLevel(1);
509
510 Accessee accessee;
511 accessee.SetAccesseeDeviceId("acee1");
512 accessee.SetAccesseeUserId(22);
513 accessee.SetAccesseeAccountId("a1");
514 accessee.SetAccesseeTokenId(222);
515 accessee.SetAccesseeBundleName("bb1");
516 accessee.SetAccesseeHapSignature("h1");
517 accessee.SetAccesseeBindLevel(1);
518
519 AccessControlProfile profile;
520 profile.SetTrustDeviceId("123456");
521 profile.SetSessionKey("key1");
522 profile.SetBindType(1);
523 profile.SetAuthenticationType(1);
524 profile.SetDeviceIdType(1);
525 profile.SetDeviceIdHash("abcd");
526 profile.SetStatus(0);
527 profile.SetValidPeriod(1);
528 profile.SetLastAuthTime(5);
529 profile.SetBindLevel(0);
530
531 profile.SetAccesser(accesser);
532 profile.SetAccessee(accessee);
533
534 ValuesBucket values;
535 ValueObject valueObject;
536 string strValue = "";
537 int32_t res1 = ProfileUtils::AccesserToEntries(profile, values);
538 EXPECT_EQ(DP_SUCCESS, res1);
539 values.GetObject(ACCESSER_DEVICE_ID, valueObject);
540 valueObject.GetString(strValue);
541 EXPECT_EQ("acer1", strValue);
542
543 Accesser outAccesser;
544 int32_t res2 = ProfileUtils::EntriesToAccesser(values, outAccesser);
545 EXPECT_EQ(DP_SUCCESS, res2);
546 EXPECT_EQ("acer1", outAccesser.GetAccesserDeviceId());
547 }
548
549 /**
550 * @tc.name: AccesseeTranslateEntries001
551 * @tc.desc: AccesseeToEntries and EntriesToAccessee.
552 * @tc.type: FUNC
553 * @tc.require:
554 */
555 HWTEST_F(ProfileUtilsTest, AccesseeTranslateEntries001, TestSize.Level1)
556 {
557 Accesser accesser;
558 accesser.SetAccesserDeviceId("acer1");
559 accesser.SetAccesserUserId(11);
560 accesser.SetAccesserAccountId("a1");
561 accesser.SetAccesserTokenId(111);
562 accesser.SetAccesserBundleName("b1");
563 accesser.SetAccesserHapSignature("h1");
564 accesser.SetAccesserBindLevel(1);
565
566 Accessee accessee;
567 accessee.SetAccesseeDeviceId("acee1");
568 accessee.SetAccesseeUserId(22);
569 accessee.SetAccesseeAccountId("a1");
570 accessee.SetAccesseeTokenId(222);
571 accessee.SetAccesseeBundleName("bb1");
572 accessee.SetAccesseeHapSignature("h1");
573 accessee.SetAccesseeBindLevel(1);
574
575 AccessControlProfile profile;
576 profile.SetTrustDeviceId("123456");
577 profile.SetSessionKey("key1");
578 profile.SetBindType(1);
579 profile.SetAuthenticationType(1);
580 profile.SetDeviceIdType(1);
581 profile.SetDeviceIdHash("abcd");
582 profile.SetStatus(0);
583 profile.SetValidPeriod(1);
584 profile.SetLastAuthTime(5);
585 profile.SetBindLevel(0);
586
587 profile.SetAccesser(accesser);
588 profile.SetAccessee(accessee);
589
590 ValuesBucket values;
591 ValueObject valueObject;
592 string strValue = "";
593 int32_t res1 = ProfileUtils::AccesseeToEntries(profile, values);
594 EXPECT_EQ(DP_SUCCESS, res1);
595 values.GetObject(ACCESSEE_DEVICE_ID, valueObject);
596 valueObject.GetString(strValue);
597 EXPECT_EQ("acee1", strValue);
598
599 Accessee outAccessee;
600 int32_t res2 = ProfileUtils::EntriesToAccessee(values, outAccessee);
601 EXPECT_EQ(DP_SUCCESS, res2);
602 EXPECT_EQ("acee1", outAccessee.GetAccesseeDeviceId());
603 }
604
605 /**
606 * @tc.name: DeviceProfileTranslateEntries001
607 * @tc.desc: DeviceProfileToEntries and EntriesToDeviceProfile.
608 * @tc.type: FUNC
609 * @tc.require:
610 */
611 HWTEST_F(ProfileUtilsTest, DeviceProfileTranslateEntries001, TestSize.Level1)
612 {
613 DeviceProfile deviceProfile;
614 deviceProfile.SetDeviceId("anything");
615 deviceProfile.SetOsApiLevel(1);
616 deviceProfile.SetOsVersion("anything");
617 deviceProfile.SetOsType(1);
618
619 map<string, string> entries;
620 int32_t res1 = ProfileUtils::DeviceProfileToEntries(deviceProfile, entries);
621 EXPECT_EQ(DP_SUCCESS, res1);
622
623 DeviceProfile outDeviceProfile;
624 int32_t res2 = ProfileUtils::EntriesToDeviceProfile(entries, outDeviceProfile);
625 EXPECT_EQ(DP_SUCCESS, res2);
626 }
627
628 /**
629 * @tc.name: ServiceProfileTranslateEntries001
630 * @tc.desc: ServiceProfileToEntries and EntriesToServiceProfile.
631 * @tc.type: FUNC
632 * @tc.require:
633 */
634 HWTEST_F(ProfileUtilsTest, ServiceProfileTranslateEntries001, TestSize.Level1)
635 {
636 ServiceProfile serviceProfile;
637 serviceProfile.SetDeviceId("deviceId");
638 serviceProfile.SetServiceName("serviceName");
639 serviceProfile.SetServiceType("serviceType");
640
641 map<string, string> entries;
642 int32_t res1 = ProfileUtils::ServiceProfileToEntries(serviceProfile, entries);
643 EXPECT_EQ(DP_SUCCESS, res1);
644
645 ServiceProfile outServiceProfile;
646 int32_t res2 = ProfileUtils::EntriesToServiceProfile(entries, outServiceProfile);
647 EXPECT_EQ(DP_SUCCESS, res2);
648 EXPECT_EQ("deviceId", outServiceProfile.GetDeviceId());
649 }
650
651 HWTEST_F(ProfileUtilsTest, AccesserTranslateEntries002, TestSize.Level1)
652 {
653 Accesser accesser;
654 accesser.SetAccesserDeviceId("acer2");
655 accesser.SetAccesserUserId(11);
656 accesser.SetAccesserAccountId("a1");
657 accesser.SetAccesserTokenId(111);
658 accesser.SetAccesserBundleName("b1");
659 accesser.SetAccesserHapSignature("h1");
660 accesser.SetAccesserBindLevel(1);
661
662 Accessee accessee;
663 accessee.SetAccesseeDeviceId("acee2");
664 accessee.SetAccesseeUserId(22);
665 accessee.SetAccesseeAccountId("a1");
666 accessee.SetAccesseeTokenId(222);
667 accessee.SetAccesseeBundleName("bb1");
668 accessee.SetAccesseeHapSignature("h1");
669 accessee.SetAccesseeBindLevel(1);
670
671 AccessControlProfile profile;
672 profile.SetTrustDeviceId("123456");
673 profile.SetSessionKey("key1");
674 profile.SetBindType(1);
675 profile.SetAuthenticationType(1);
676 profile.SetDeviceIdType(1);
677 profile.SetDeviceIdHash("abcd");
678 profile.SetStatus(0);
679 profile.SetValidPeriod(1);
680 profile.SetLastAuthTime(5);
681 profile.SetBindLevel(0);
682
683 profile.SetAccesser(accesser);
684 profile.SetAccessee(accessee);
685
686 ValuesBucket values;
687 ValueObject valueObject;
688 string strValue = "";
689 int32_t res1 = ProfileUtils::AccesserToEntries(profile, values);
690 EXPECT_EQ(DP_SUCCESS, res1);
691 values.GetObject(ACCESSER_DEVICE_ID, valueObject);
692 valueObject.GetString(strValue);
693 EXPECT_EQ("acer2", strValue);
694
695 Accesser outAccesser;
696 int32_t res2 = ProfileUtils::EntriesToAccesser(values, outAccesser);
697 EXPECT_EQ(DP_SUCCESS, res2);
698 EXPECT_EQ("acer2", outAccesser.GetAccesserDeviceId());
699 }
700
701 HWTEST_F(ProfileUtilsTest, AccesseeTranslateEntries002, TestSize.Level1)
702 {
703 Accesser accesser;
704 accesser.SetAccesserDeviceId("acer2");
705 accesser.SetAccesserUserId(11);
706 accesser.SetAccesserAccountId("a1");
707 accesser.SetAccesserTokenId(111);
708 accesser.SetAccesserBundleName("b1");
709 accesser.SetAccesserHapSignature("h1");
710 accesser.SetAccesserBindLevel(1);
711
712 Accessee accessee;
713 accessee.SetAccesseeDeviceId("acee2");
714 accessee.SetAccesseeUserId(22);
715 accessee.SetAccesseeAccountId("a1");
716 accessee.SetAccesseeTokenId(222);
717 accessee.SetAccesseeBundleName("bb1");
718 accessee.SetAccesseeHapSignature("h1");
719 accessee.SetAccesseeBindLevel(1);
720
721 AccessControlProfile profile;
722 profile.SetTrustDeviceId("123456");
723 profile.SetSessionKey("key1");
724 profile.SetBindType(1);
725 profile.SetAuthenticationType(1);
726 profile.SetDeviceIdType(1);
727 profile.SetDeviceIdHash("abcd");
728 profile.SetStatus(0);
729 profile.SetValidPeriod(1);
730 profile.SetLastAuthTime(5);
731 profile.SetBindLevel(0);
732
733 profile.SetAccesser(accesser);
734 profile.SetAccessee(accessee);
735
736 ValuesBucket values;
737 ValueObject valueObject;
738 string strValue = "";
739 int32_t res1 = ProfileUtils::AccesseeToEntries(profile, values);
740 EXPECT_EQ(DP_SUCCESS, res1);
741 values.GetObject(ACCESSEE_DEVICE_ID, valueObject);
742 valueObject.GetString(strValue);
743 EXPECT_EQ("acee2", strValue);
744
745 Accessee outAccessee;
746 int32_t res2 = ProfileUtils::EntriesToAccessee(values, outAccessee);
747 EXPECT_EQ(DP_SUCCESS, res2);
748 EXPECT_EQ("acee2", outAccessee.GetAccesseeDeviceId());
749 }
750
751 /**
752 * @tc.name: CharacteristicProfileTranslateEntries001
753 * @tc.desc: CharacteristicProfileToEntries and EntriesToCharProfile.
754 * @tc.type: FUNC
755 * @tc.require:
756 */
757 HWTEST_F(ProfileUtilsTest, CharacteristicProfileTranslateEntries001, TestSize.Level1)
758 {
759 CharacteristicProfile charProfile;
760 charProfile.SetDeviceId("deviceId");
761 charProfile.SetServiceName("serviceName");
762 charProfile.SetCharacteristicKey("characteristicKey");
763 charProfile.SetCharacteristicValue("characteristicValue");
764
765 map<string, string> entries;
766 int32_t res1 = ProfileUtils::CharacteristicProfileToEntries(charProfile, entries);
767 EXPECT_EQ(DP_SUCCESS, res1);
768
769 CharacteristicProfile outCharProfile;
770 int32_t res2 = ProfileUtils::EntriesToCharProfile(entries, outCharProfile);
771 EXPECT_EQ(DP_SUCCESS, res2);
772 EXPECT_EQ("deviceId", outCharProfile.GetDeviceId());
773 }
774
775 /**
776 * @tc.name: EntriesToDeviceProfile001
777 * @tc.desc: EntriesToDeviceProfile failed, Entries size is invalid.
778 * @tc.type: FUNC
779 * @tc.require:
780 */
781 HWTEST_F(ProfileUtilsTest, EntriesToDeviceProfile001, TestSize.Level1)
782 {
783 map<string, string> entries;
784 DeviceProfile deviceProfile;
785 int32_t res = ProfileUtils::EntriesToDeviceProfile(entries, deviceProfile);
786 EXPECT_EQ(DP_INVALID_PARAMS, res);
787 }
788
789 /**
790 * @tc.name: EntriesToServiceProfile001
791 * @tc.desc: EntriesToServiceProfile failed, Entries size is invalid.
792 * @tc.type: FUNC
793 * @tc.require:
794 */
795 HWTEST_F(ProfileUtilsTest, EntriesToServiceProfile001, TestSize.Level1)
796 {
797 map<string, string> entries;
798 ServiceProfile serviceProfile;
799 int32_t res = ProfileUtils::EntriesToServiceProfile(entries, serviceProfile);
800 EXPECT_EQ(DP_INVALID_PARAMS, res);
801 }
802
803 /**
804 * @tc.name: EntriesToCharProfile001
805 * @tc.desc: EntriesToCharProfile failed, Entries size is invalid.
806 * @tc.type: FUNC
807 * @tc.require:
808 */
809 HWTEST_F(ProfileUtilsTest, EntriesToCharProfile001, TestSize.Level1)
810 {
811 map<string, string> entries;
812 CharacteristicProfile charProfile;
813 int32_t res = ProfileUtils::EntriesToCharProfile(entries, charProfile);
814 EXPECT_EQ(DP_INVALID_PARAMS, res);
815 }
816
817 /**
818 * @tc.name: SplitString001
819 * @tc.desc: SplitString failed, str == "".
820 * @tc.type: FUNC
821 * @tc.require:
822 */
823 HWTEST_F(ProfileUtilsTest, SplitString001, TestSize.Level1)
824 {
825 string str = "";
826 string splits = "#";
827 vector<string> res;
828 int32_t ret = ProfileUtils::SplitString(str, splits, res);
829 EXPECT_EQ(DP_INVALID_PARAMS, ret);
830 }
831
832 /**
833 * @tc.name: SplitString002
834 * @tc.desc: SplitString succeed.
835 * @tc.type: FUNC
836 * @tc.require:
837 */
838 HWTEST_F(ProfileUtilsTest, SplitString002, TestSize.Level1)
839 {
840 string str = "a#b#c";
841 string splits = "#";
842 vector<string> res;
843 int32_t ret = ProfileUtils::SplitString(str, splits, res);
844 EXPECT_EQ(DP_SUCCESS, ret);
845 EXPECT_EQ(3, res.size());
846 }
847
848 /**
849 * @tc.name: JoinString001
850 * @tc.desc: JoinString failed, str == "".
851 * @tc.type: FUNC
852 * @tc.require:
853 */
854 HWTEST_F(ProfileUtilsTest, JoinString001, TestSize.Level1)
855 {
856 string splits = "#";
857 vector<string> strs;
858 string ret = ProfileUtils::JoinString(strs, splits);
859 EXPECT_EQ(EMPTY_STRING, ret);
860 }
861
862 /**
863 * @tc.name: JoinString002
864 * @tc.desc: JoinString succeed.
865 * @tc.type: FUNC
866 * @tc.require:
867 */
868 HWTEST_F(ProfileUtilsTest, JoinString002, TestSize.Level1)
869 {
870 string str = "a#b#c";
871 string splits = "#";
872 vector<string> strs {"a", "b", "c"};
873 string ret = ProfileUtils::JoinString(strs, splits);
874 EXPECT_EQ(str, ret);
875 }
876
877 /**
878 * @tc.name: GenerateDBKey001
879 * @tc.desc: GenerateDBKey succeed.
880 * @tc.type: FUNC
881 * @tc.require:
882 */
883 HWTEST_F(ProfileUtilsTest, GenerateDBKey001, TestSize.Level1)
884 {
885 string profileKey = "profileKey";
886 string profileProperty = "profileProperty";
887 string res = ProfileUtils::GenerateDBKey(profileKey, profileProperty);
888 EXPECT_EQ("profileKey#profileProperty", res);
889 }
890
891 /**
892 * @tc.name: GetProfileKey001
893 * @tc.desc: GetProfileKey succeed.
894 * @tc.type: FUNC
895 * @tc.require:
896 */
897 HWTEST_F(ProfileUtilsTest, GetProfileKey001, TestSize.Level1)
898 {
899 string dbKey = "profileKey#profileProperty";
900 string res = ProfileUtils::GetProfileKey(dbKey);
901 EXPECT_EQ("profileKey", res);
902 }
903
904 /**
905 * @tc.name: GetProfileKey002
906 * @tc.desc: GetProfileKey failed , dbKey.length() == 0.
907 * @tc.type: FUNC
908 * @tc.require:
909 */
910 HWTEST_F(ProfileUtilsTest, GetProfileKey002, TestSize.Level1)
911 {
912 string dbKey = "";
913 string res = ProfileUtils::GetProfileKey(dbKey);
914 EXPECT_EQ(0, res.length());
915 }
916
917 /**
918 * @tc.name: GetProfileKey003
919 * @tc.desc: GetProfileKey failed , dbKey.length() > 4096.
920 * @tc.type: FUNC
921 * @tc.require:
922 */
923 HWTEST_F(ProfileUtilsTest, GetProfileKey003, TestSize.Level1)
924 {
925 string dbKey = "";
926 for (int32_t i = 0; i < 5000; i++) {
927 dbKey += 'a';
928 }
929 string res = ProfileUtils::GetProfileKey(dbKey);
930 EXPECT_EQ(0, res.length());
931 }
932
933 /**
934 * @tc.name: GetProfileProperty001
935 * @tc.desc: GetProfileProperty succeed.
936 * @tc.type: FUNC
937 * @tc.require:
938 */
939 HWTEST_F(ProfileUtilsTest, GetProfileProperty001, TestSize.Level1)
940 {
941 string dbKey = "profileKey#profileProperty";
942 string res = ProfileUtils::GetProfileProperty(dbKey);
943 EXPECT_EQ("profileProperty", res);
944 }
945
946 /**
947 * @tc.name: GetProfileProperty002
948 * @tc.desc: GetProfileProperty failed , dbKey.length() == 0.
949 * @tc.type: FUNC
950 * @tc.require:
951 */
952 HWTEST_F(ProfileUtilsTest, GetProfileProperty002, TestSize.Level1)
953 {
954 string dbKey = "";
955 string res = ProfileUtils::GetProfileProperty(dbKey);
956 EXPECT_EQ(0, res.length());
957 }
958
959 /**
960 * @tc.name: GetProfileProperty003
961 * @tc.desc: GetProfileProperty failed , dbKey.length() > 4096.
962 * @tc.type: FUNC
963 * @tc.require:
964 */
965 HWTEST_F(ProfileUtilsTest, GetProfileProperty003, TestSize.Level1)
966 {
967 string dbKey = "";
968 for (int32_t i = 0; i < 5000; i++) {
969 dbKey += 'a';
970 }
971 string res = ProfileUtils::GetProfileProperty(dbKey);
972 EXPECT_EQ(0, res.length());
973 }
974
975 /**
976 * @tc.name: GetDeviceIdByDBKey001
977 * @tc.desc: GetDeviceIdByDBKey succeed.
978 * @tc.type: FUNC
979 * @tc.require:
980 */
981 HWTEST_F(ProfileUtilsTest, GetDeviceIdByDBKey001, TestSize.Level1)
982 {
983 string dbKey = "dev#111";
984 string res = ProfileUtils::GetDeviceIdByDBKey(dbKey);
985 EXPECT_EQ("111", res);
986 }
987
988 /**
989 * @tc.name: GetDeviceIdByDBKey002
990 * @tc.desc: GetDeviceIdByDBKey failed , dbKey.length() == 0.
991 * @tc.type: FUNC
992 * @tc.require:
993 */
994 HWTEST_F(ProfileUtilsTest, GetDeviceIdByDBKey002, TestSize.Level1)
995 {
996 string dbKey = "";
997 string res = ProfileUtils::GetDeviceIdByDBKey(dbKey);
998 EXPECT_EQ(0, res.length());
999 }
1000
1001 /**
1002 * @tc.name: GetDeviceIdByDBKey003
1003 * @tc.desc: GetDeviceIdByDBKey failed , res.size() < 1.
1004 * @tc.type: FUNC
1005 * @tc.require:
1006 */
1007 HWTEST_F(ProfileUtilsTest, GetDeviceIdByDBKey003, TestSize.Level1)
1008 {
1009 string dbKey = "#";
1010 string res = ProfileUtils::GetDeviceIdByDBKey(dbKey);
1011 EXPECT_EQ(0, res.length());
1012 }
1013
1014 /**
1015 * @tc.name: GetDeviceIdByDBKey004
1016 * @tc.desc: GetDeviceIdByDBKey failed , dbKey.length() > 4096.
1017 * @tc.type: FUNC
1018 * @tc.require:
1019 */
1020 HWTEST_F(ProfileUtilsTest, GetDeviceIdByDBKey004, TestSize.Level1)
1021 {
1022 string dbKey = "";
1023 for (int32_t i = 0; i < 5000; i++) {
1024 dbKey += 'a';
1025 }
1026 string res = ProfileUtils::GetDeviceIdByDBKey(dbKey);
1027 EXPECT_EQ(0, res.length());
1028 }
1029
1030 /**
1031 * @tc.name: GetServiceNameByDBKey001
1032 * @tc.desc: GetServiceNameByDBKey succeed.
1033 * @tc.type: FUNC
1034 * @tc.require:
1035 */
1036 HWTEST_F(ProfileUtilsTest, GetServiceNameByDBKey001, TestSize.Level1)
1037 {
1038 string dbKey = "ser#111#222";
1039 string res = ProfileUtils::GetServiceNameByDBKey(dbKey);
1040 EXPECT_EQ("222", res);
1041 }
1042
1043 /**
1044 * @tc.name: GetServiceNameByDBKey002
1045 * @tc.desc: GetServiceNameByDBKey failed , dbKey.length() == 0.
1046 * @tc.type: FUNC
1047 * @tc.require:
1048 */
1049 HWTEST_F(ProfileUtilsTest, GetServiceNameByDBKey002, TestSize.Level1)
1050 {
1051 string dbKey = "";
1052 string res = ProfileUtils::GetServiceNameByDBKey(dbKey);
1053 EXPECT_EQ(0, res.length());
1054 }
1055
1056 /**
1057 * @tc.name: GetServiceNameByDBKey003
1058 * @tc.desc: GetServiceNameByDBKey failed , dbKey.length() > 4096.
1059 * @tc.type: FUNC
1060 * @tc.require:
1061 */
1062 HWTEST_F(ProfileUtilsTest, GetServiceNameByDBKey003, TestSize.Level1)
1063 {
1064 string dbKey = "";
1065 for (int32_t i = 0; i < 5000; i++) {
1066 dbKey += 'a';
1067 }
1068 string res = ProfileUtils::GetServiceNameByDBKey(dbKey);
1069 EXPECT_EQ(0, res.length());
1070 }
1071
1072 /**
1073 * @tc.name: GetServiceNameByDBKey004
1074 * @tc.desc: GetDeviceIdByDBKey failed , res.size() < 2.
1075 * @tc.type: FUNC
1076 * @tc.require:
1077 */
1078 HWTEST_F(ProfileUtilsTest, GetServiceNameByDBKey004, TestSize.Level1)
1079 {
1080 string dbKey = "ser";
1081 string res = ProfileUtils::GetServiceNameByDBKey(dbKey);
1082 EXPECT_EQ(0, res.length());
1083 }
1084
1085 /**
1086 * @tc.name: GetCharKeyByDBKey001
1087 * @tc.desc: GetCharKeyByDBKey succeed.
1088 * @tc.type: FUNC
1089 * @tc.require:
1090 */
1091 HWTEST_F(ProfileUtilsTest, GetCharKeyByDBKey001, TestSize.Level1)
1092 {
1093 string dbKey = "char#111#222#333";
1094 string res = ProfileUtils::GetCharKeyByDBKey(dbKey);
1095 EXPECT_EQ("333", res);
1096 }
1097
1098 /**
1099 * @tc.name: GetCharKeyByDBKey002
1100 * @tc.desc: GetCharKeyByDBKey failed , dbKey.length() == 0.
1101 * @tc.type: FUNC
1102 * @tc.require:
1103 */
1104 HWTEST_F(ProfileUtilsTest, GetCharKeyByDBKey002, TestSize.Level1)
1105 {
1106 string dbKey = "";
1107 string res = ProfileUtils::GetCharKeyByDBKey(dbKey);
1108 EXPECT_EQ(0, res.length());
1109 }
1110
1111 /**
1112 * @tc.name: GetCharKeyByDBKey003
1113 * @tc.desc: GetCharKeyByDBKey failed , res.size() < 3.
1114 * @tc.type: FUNC
1115 * @tc.require:
1116 */
1117 HWTEST_F(ProfileUtilsTest, GetCharKeyByDBKey003, TestSize.Level1)
1118 {
1119 string dbKey = "char#";
1120 string res = ProfileUtils::GetCharKeyByDBKey(dbKey);
1121 EXPECT_EQ(0, res.length());
1122 }
1123
1124 /**
1125 * @tc.name: GetCharKeyByDBKey004
1126 * @tc.desc: GetCharKeyByDBKey failed , dbKey.length() > 4096.
1127 * @tc.type: FUNC
1128 * @tc.require:
1129 */
1130 HWTEST_F(ProfileUtilsTest, GetCharKeyByDBKey004, TestSize.Level1)
1131 {
1132 string dbKey = "";
1133 for (int32_t i = 0; i < 5000; i++) {
1134 dbKey += 'a';
1135 }
1136 string res = ProfileUtils::GetCharKeyByDBKey(dbKey);
1137 EXPECT_EQ(0, res.length());
1138 }
1139
1140 /**
1141 * @tc.name: toString001
1142 * @tc.desc: toString succeed.
1143 * @tc.type: FUNC
1144 * @tc.require:
1145 */
1146 HWTEST_F(ProfileUtilsTest, toString001, TestSize.Level1)
1147 {
1148 u16string str16 = u"abc";
1149 string res = ProfileUtils::toString(str16);
1150 EXPECT_EQ("abc", res);
1151 }
1152
1153 /**
1154 * @tc.name: IsPropertyValid001
1155 * @tc.desc: IsPropertyValid failed, propertyMap.size() == 0.
1156 * @tc.type: FUNC
1157 * @tc.require:
1158 */
1159 HWTEST_F(ProfileUtilsTest, IsPropertyValid001, TestSize.Level1)
1160 {
1161 map<string, string> propertyMap;
1162 string property = "property";
1163 int32_t maxValue = 1;
1164 bool res1 = ProfileUtils::IsPropertyValid(propertyMap, property, maxValue);
1165 EXPECT_EQ(false, res1);
1166
1167 int32_t minValue = 0;
1168 bool res2 = ProfileUtils::IsPropertyValid(propertyMap, property, minValue, maxValue);
1169 EXPECT_EQ(false, res2);
1170
1171 uint32_t u32MinValue = 0;
1172 uint32_t u32MaxValue = 1;
1173 bool res3 = ProfileUtils::IsPropertyValid(propertyMap, property, u32MinValue, u32MaxValue);
1174 EXPECT_EQ(false, res3);
1175
1176 int64_t i64MinValue = 0;
1177 int64_t i64MaxValue = 1;
1178 bool res4 = ProfileUtils::IsPropertyValid(propertyMap, property, i64MinValue, i64MaxValue);
1179 EXPECT_EQ(false, res4);
1180 }
1181
1182 /**
1183 * @tc.name: IsPropertyValid002
1184 * @tc.desc: IsPropertyValid overload 1.
1185 * @tc.type: FUNC
1186 * @tc.require:
1187 */
1188 HWTEST_F(ProfileUtilsTest, IsPropertyValid002, TestSize.Level1)
1189 {
1190 map<string, string> propertyMap;
1191 string property = "property";
1192 string value = "value";
1193 propertyMap[property] = value;
1194 int32_t maxValue = 10;
1195 bool res1 = ProfileUtils::IsPropertyValid(propertyMap, property, maxValue);
1196 EXPECT_EQ(true, res1);
1197
1198 maxValue = 0;
1199 bool res2 = ProfileUtils::IsPropertyValid(propertyMap, property, maxValue);
1200 EXPECT_EQ(false, res2);
1201
1202 value = "";
1203 propertyMap[property] = value;
1204 bool res3 = ProfileUtils::IsPropertyValid(propertyMap, property, maxValue);
1205 EXPECT_EQ(false, res3);
1206
1207 propertyMap.erase(property);
1208 bool res4 = ProfileUtils::IsPropertyValid(propertyMap, property, maxValue);
1209 EXPECT_EQ(false, res4);
1210 }
1211
1212 /**
1213 * @tc.name: IsPropertyValid003
1214 * @tc.desc: IsPropertyValid overload 2.
1215 * @tc.type: FUNC
1216 * @tc.require:
1217 */
1218 HWTEST_F(ProfileUtilsTest, IsPropertyValid003, TestSize.Level1)
1219 {
1220 map<string, string> propertyMap;
1221 string property = "property";
1222 string value = "5";
1223 propertyMap[property] = value;
1224 int32_t maxValue = 10;
1225 int32_t minValue = 0;
1226 bool res1 = ProfileUtils::IsPropertyValid(propertyMap, property, minValue, maxValue);
1227 EXPECT_EQ(true, res1);
1228
1229 maxValue = 0;
1230 bool res2 = ProfileUtils::IsPropertyValid(propertyMap, property, minValue, maxValue);
1231 EXPECT_EQ(false, res2);
1232
1233 value = "0";
1234 propertyMap[property] = value;
1235 bool res3 = ProfileUtils::IsPropertyValid(propertyMap, property, minValue, maxValue);
1236 EXPECT_EQ(false, res3);
1237
1238 propertyMap.erase(property);
1239 bool res4 = ProfileUtils::IsPropertyValid(propertyMap, property, minValue, maxValue);
1240 EXPECT_EQ(false, res4);
1241 }
1242
1243 /**
1244 * @tc.name: IsPropertyValid004
1245 * @tc.desc: IsPropertyValid overload 3.
1246 * @tc.type: FUNC
1247 * @tc.require:
1248 */
1249 HWTEST_F(ProfileUtilsTest, IsPropertyValid004, TestSize.Level1)
1250 {
1251 map<string, string> propertyMap;
1252 string property = "property";
1253 string value = "5";
1254 propertyMap[property] = value;
1255 uint32_t maxValue = 10;
1256 uint32_t minValue = 0;
1257 bool res1 = ProfileUtils::IsPropertyValid(propertyMap, property, minValue, maxValue);
1258 EXPECT_EQ(true, res1);
1259
1260 maxValue = 0;
1261 bool res2 = ProfileUtils::IsPropertyValid(propertyMap, property, minValue, maxValue);
1262 EXPECT_EQ(false, res2);
1263
1264 value = "0";
1265 propertyMap[property] = value;
1266 bool res3 = ProfileUtils::IsPropertyValid(propertyMap, property, minValue, maxValue);
1267 EXPECT_EQ(false, res3);
1268
1269 propertyMap.erase(property);
1270 bool res4 = ProfileUtils::IsPropertyValid(propertyMap, property, minValue, maxValue);
1271 EXPECT_EQ(false, res4);
1272 }
1273
1274 /**
1275 * @tc.name: IsPropertyValid005
1276 * @tc.desc: IsPropertyValid overload 4.
1277 * @tc.type: FUNC
1278 * @tc.require:
1279 */
1280 HWTEST_F(ProfileUtilsTest, IsPropertyValid005, TestSize.Level1)
1281 {
1282 map<string, string> propertyMap;
1283 string property = "property";
1284 string value = "5";
1285 propertyMap[property] = value;
1286 int64_t maxValue = 10;
1287 int64_t minValue = 0;
1288 bool res1 = ProfileUtils::IsPropertyValid(propertyMap, property, minValue, maxValue);
1289 EXPECT_EQ(true, res1);
1290
1291 maxValue = 0;
1292 bool res2 = ProfileUtils::IsPropertyValid(propertyMap, property, minValue, maxValue);
1293 EXPECT_EQ(false, res2);
1294
1295 value = "0";
1296 propertyMap[property] = value;
1297 bool res3 = ProfileUtils::IsPropertyValid(propertyMap, property, minValue, maxValue);
1298 EXPECT_EQ(false, res3);
1299
1300 propertyMap.erase(property);
1301 bool res4 = ProfileUtils::IsPropertyValid(propertyMap, property, minValue, maxValue);
1302 EXPECT_EQ(false, res4);
1303
1304 property = "property1";
1305 value = "";
1306 propertyMap[property] = value;
1307 bool res5 = ProfileUtils::IsPropertyValid(propertyMap, property, minValue, maxValue);
1308 EXPECT_EQ(false, res5);
1309 }
1310
1311 /**
1312 * @tc.name: GetIntValue001
1313 * @tc.desc: GetIntValue failed, ValuesBucket is empty.
1314 * @tc.type: FUNC
1315 * @tc.require:
1316 */
1317 HWTEST_F(ProfileUtilsTest, GetIntValue001, TestSize.Level1)
1318 {
1319 ValuesBucket values;
1320 string property = "property";
1321 int32_t value = 0;
1322 bool res = ProfileUtils::GetIntValue(values, property, value);
1323 EXPECT_EQ(false, res);
1324 }
1325
1326 /**
1327 * @tc.name: GetIntValue002
1328 * @tc.desc: GetIntValue all.
1329 * @tc.type: FUNC
1330 * @tc.require:
1331 */
1332 HWTEST_F(ProfileUtilsTest, GetIntValue002, TestSize.Level1)
1333 {
1334 ValuesBucket values;
1335 int32_t intValue = 1;
1336 string property = "property";
1337 values.PutInt(property, intValue);
1338 int32_t outValue = 0;
1339 bool res1 = ProfileUtils::GetIntValue(values, property, outValue);
1340 EXPECT_EQ(true, res1);
1341
1342 string strValue = "strValue";
1343 values.Clear();
1344 values.PutString(property, strValue);
1345 bool res2 = ProfileUtils::GetIntValue(values, property, outValue);
1346 EXPECT_EQ(false, res2);
1347
1348 values.Clear();
1349 bool res3 = ProfileUtils::GetIntValue(values, property, outValue);
1350 EXPECT_EQ(false, res3);
1351 }
1352
1353 /**
1354 * @tc.name: GetStringValue001
1355 * @tc.desc: GetStringValue failed, ValuesBucket is empty.
1356 * @tc.type: FUNC
1357 * @tc.require:
1358 */
1359 HWTEST_F(ProfileUtilsTest, GetStringValue001, TestSize.Level1)
1360 {
1361 ValuesBucket values;
1362 string property = "property";
1363 string value = "";
1364 bool res = ProfileUtils::GetStringValue(values, property, value);
1365 EXPECT_EQ(false, res);
1366 }
1367
1368 /**
1369 * @tc.name: GetStringValue002
1370 * @tc.desc: GetStringValue all.
1371 * @tc.type: FUNC
1372 * @tc.require:
1373 */
1374 HWTEST_F(ProfileUtilsTest, GetStringValue002, TestSize.Level1)
1375 {
1376 ValuesBucket values;
1377 string strValue = "strValue";
1378 string property = "property";
1379 values.PutString(property, strValue);
1380 string outValue = "";
1381 bool res1 = ProfileUtils::GetStringValue(values, property, outValue);
1382 EXPECT_EQ(true, res1);
1383
1384 values.Clear();
1385 values.PutNull(property);
1386 bool res2 = ProfileUtils::GetStringValue(values, property, outValue);
1387 EXPECT_EQ(false, res2);
1388
1389 values.Clear();
1390 bool res3 = ProfileUtils::GetStringValue(values, property, outValue);
1391 EXPECT_EQ(false, res3);
1392 }
1393
1394 /**
1395 * @tc.name: GetLongValue001
1396 * @tc.desc: GetLongValue failed, ValuesBucket is empty.
1397 * @tc.type: FUNC
1398 * @tc.require:
1399 */
1400 HWTEST_F(ProfileUtilsTest, GetLongValue001, TestSize.Level1)
1401 {
1402 ValuesBucket values;
1403 string property = "property";
1404 int64_t value = 0;
1405 bool res = ProfileUtils::GetLongValue(values, property, value);
1406 EXPECT_EQ(false, res);
1407 }
1408
1409 /**
1410 * @tc.name: GetLongValue002
1411 * @tc.desc: GetLongValue all.
1412 * @tc.type: FUNC
1413 * @tc.require:
1414 */
1415 HWTEST_F(ProfileUtilsTest, GetLongValue002, TestSize.Level1)
1416 {
1417 ValuesBucket values;
1418 int64_t longValue = 1;
1419 string strValue = "strValue";
1420 string property = "property";
1421 values.PutLong(property, longValue);
1422 int64_t outValue = 0;
1423 bool res1 = ProfileUtils::GetLongValue(values, property, outValue);
1424 EXPECT_EQ(true, res1);
1425
1426 values.Clear();
1427 values.PutString(property, strValue);
1428 bool res2 = ProfileUtils::GetLongValue(values, property, outValue);
1429 EXPECT_EQ(false, res2);
1430
1431 values.Clear();
1432 bool res3 = ProfileUtils::GetLongValue(values, property, outValue);
1433 EXPECT_EQ(false, res3);
1434 }
1435
1436 /**
1437 * @tc.name: EntriesToTrustDeviceProfile001
1438 * @tc.desc: EntriesToTrustDeviceProfile all.
1439 * @tc.type: FUNC
1440 * @tc.require:
1441 */
1442 HWTEST_F(ProfileUtilsTest, EntriesToTrustDeviceProfile001, TestSize.Level1)
1443 {
1444 ValuesBucket values;
1445 TrustDeviceProfile profile;
1446 int32_t intValue = 1;
1447 string strValue = "strValue";
1448
1449 values.PutString(DEVICE_ID, strValue);
1450 values.PutString(DEVICE_ID_HASH, strValue);
1451 values.PutInt(DEVICE_TYPE_ID, intValue);
1452 values.PutInt(STATUS, intValue);
1453 int32_t ret1 = ProfileUtils::EntriesToTrustDeviceProfile(values, profile);
1454 EXPECT_EQ(DP_SUCCESS, ret1);
1455
1456 values.Delete(STATUS);
1457 values.PutNull(STATUS);
1458 ProfileUtils::EntriesToTrustDeviceProfile(values, profile);
1459 values.Delete(STATUS);
1460 ProfileUtils::EntriesToTrustDeviceProfile(values, profile);
1461
1462 values.Delete(DEVICE_TYPE_ID);
1463 values.PutNull(DEVICE_TYPE_ID);
1464 ProfileUtils::EntriesToTrustDeviceProfile(values, profile);
1465 values.Delete(DEVICE_TYPE_ID);
1466 ProfileUtils::EntriesToTrustDeviceProfile(values, profile);
1467
1468 values.Delete(DEVICE_ID_HASH);
1469 values.PutNull(DEVICE_ID_HASH);
1470 ProfileUtils::EntriesToTrustDeviceProfile(values, profile);
1471 values.Delete(DEVICE_ID_HASH);
1472 ProfileUtils::EntriesToTrustDeviceProfile(values, profile);
1473
1474 values.Delete(DEVICE_ID);
1475 values.PutNull(DEVICE_ID);
1476 ProfileUtils::EntriesToTrustDeviceProfile(values, profile);
1477 values.Delete(DEVICE_ID);
1478 ProfileUtils::EntriesToTrustDeviceProfile(values, profile);
1479 }
1480
1481 /**
1482 * @tc.name: EntriesToAccessControlProfile001
1483 * @tc.desc: EntriesToAccessControlProfile all failed.
1484 * @tc.type: FUNC
1485 * @tc.require:
1486 */
1487 HWTEST_F(ProfileUtilsTest, EntriesToAccessControlProfile001, TestSize.Level1)
1488 {
1489 ValuesBucket values;
1490 AccessControlProfile profile;
1491 ProfileUtils::EntriesToAccessControlProfile(values, profile);
1492 string strValue = "";
1493 bool ret = ProfileUtils::GetStringValue(values, SESSION_KEY, strValue);
1494 EXPECT_EQ(false, ret);
1495 }
1496
1497 /**
1498 * @tc.name: EntriesToAccesser001
1499 * @tc.desc: EntriesToAccesser all .
1500 * @tc.type: FUNC
1501 * @tc.require:
1502 */
1503 HWTEST_F(ProfileUtilsTest, EntriesToAccesser001, TestSize.Level1)
1504 {
1505 ValuesBucket values;
1506 ValueObject valueObject;
1507 Accesser profile;
1508 int32_t intValue = 1;
1509 int64_t longValue = 1;
1510 string strValue = "strValue";
1511
1512 values.PutLong(ACCESSER_ID, longValue);
1513 values.PutString(ACCESSER_DEVICE_ID, strValue);
1514 values.PutInt(ACCESSER_USER_ID, intValue);
1515 values.PutString(ACCESSER_ACCOUNT_ID, strValue);
1516 values.PutLong(ACCESSER_TOKEN_ID, longValue);
1517 values.PutString(ACCESSER_BUNDLE_NAME, strValue);
1518 values.PutInt(ACCESSER_BIND_LEVEL, intValue);
1519
1520 ProfileUtils::EntriesToAccesser(values, profile);
1521 EXPECT_EQ("strValue", profile.GetAccesserBundleName());
1522
1523 values.Clear();
1524 values.PutNull(ACCESSER_ID);
1525 values.PutNull(ACCESSER_DEVICE_ID);
1526 values.PutNull(ACCESSER_USER_ID);
1527 values.PutNull(ACCESSER_ACCOUNT_ID);
1528 values.PutNull(ACCESSER_TOKEN_ID);
1529 values.PutNull(ACCESSER_BUNDLE_NAME);
1530 values.PutNull(ACCESSER_BIND_LEVEL);
1531 EXPECT_EQ(true, values.GetObject(ACCESSER_ACCOUNT_ID, valueObject));
1532 EXPECT_EQ(false, valueObject.GetString(strValue) == E_OK);
1533 ProfileUtils::EntriesToAccesser(values, profile);
1534
1535 values.Clear();
1536 EXPECT_EQ(false, values.GetObject(ACCESSER_ACCOUNT_ID, valueObject));
1537 ProfileUtils::EntriesToAccesser(values, profile);
1538 }
1539
1540 /**
1541 * @tc.name: EntriesToAccessee001
1542 * @tc.desc: EntriesToAccessee all .
1543 * @tc.type: FUNC
1544 * @tc.require:
1545 */
1546 HWTEST_F(ProfileUtilsTest, EntriesToAccessee001, TestSize.Level1)
1547 {
1548 ValuesBucket values;
1549 ValueObject valueObject;
1550 Accessee profile;
1551 int32_t intValue = 1;
1552 int64_t longValue = 1;
1553 string strValue = "strValue";
1554
1555 values.PutLong(ACCESSEE_ID, longValue);
1556 values.PutString(ACCESSEE_DEVICE_ID, strValue);
1557 values.PutInt(ACCESSEE_USER_ID, intValue);
1558 values.PutString(ACCESSEE_ACCOUNT_ID, strValue);
1559 values.PutLong(ACCESSEE_TOKEN_ID, longValue);
1560 values.PutString(ACCESSEE_BUNDLE_NAME, strValue);
1561 values.PutInt(ACCESSEE_BIND_LEVEL, intValue);
1562
1563 ProfileUtils::EntriesToAccessee(values, profile);
1564 EXPECT_EQ("strValue", profile.GetAccesseeBundleName());
1565
1566 values.Clear();
1567 values.PutNull(ACCESSEE_ID);
1568 values.PutNull(ACCESSEE_DEVICE_ID);
1569 values.PutNull(ACCESSEE_USER_ID);
1570 values.PutNull(ACCESSEE_ACCOUNT_ID);
1571 values.PutNull(ACCESSEE_TOKEN_ID);
1572 values.PutNull(ACCESSEE_BUNDLE_NAME);
1573 values.PutNull(ACCESSEE_BIND_LEVEL);
1574 EXPECT_EQ(true, values.GetObject(ACCESSEE_ACCOUNT_ID, valueObject));
1575 EXPECT_EQ(false, valueObject.GetString(strValue) == E_OK);
1576 ProfileUtils::EntriesToAccessee(values, profile);
1577
1578 values.Clear();
1579 EXPECT_EQ(false, values.GetObject(ACCESSEE_ACCOUNT_ID, valueObject));
1580 ProfileUtils::EntriesToAccessee(values, profile);
1581 }
1582
1583 /**
1584 * @tc.name: EntriesToDeviceProfile002
1585 * @tc.desc: EntriesToDeviceProfile failed all.
1586 * @tc.type: FUNC
1587 * @tc.require:
1588 */
1589 HWTEST_F(ProfileUtilsTest, EntriesToDeviceProfile002, TestSize.Level1)
1590 {
1591 DeviceProfile profile;
1592 map<string, string> values;
1593 values["key"] = "value";
1594 auto propertiesMap = ProfileUtils::GetProfilePropertiesMap(values);
1595 bool ret1 = ProfileUtils::IsPropertyValid(propertiesMap, DEVICE_NAME, MAX_STRING_LEN);
1596 EXPECT_EQ(false, ret1);
1597 ProfileUtils::EntriesToDeviceProfile(values, profile);
1598
1599 for (int32_t i = 0; i < MAX_DB_RECORD_SIZE + 5; i++) {
1600 values[to_string(i)] = "value";
1601 }
1602 int32_t ret2 = ProfileUtils::EntriesToDeviceProfile(values, profile);
1603 EXPECT_EQ(DP_INVALID_PARAMS, ret2);
1604 }
1605
1606 /**
1607 * @tc.name: EntriesToServiceProfile002
1608 * @tc.desc: EntriesToServiceProfile all.
1609 * @tc.type: FUNC
1610 * @tc.require:
1611 */
1612 HWTEST_F(ProfileUtilsTest, EntriesToServiceProfile002, TestSize.Level1)
1613 {
1614 map<string, string> values;
1615 ServiceProfile profile;
1616 string strValye = "";
1617 values[SERVICE_NAME] = "serviceName";
1618 values[SERVICE_TYPE] = "serviceType";
1619
1620 int32_t ret1 = ProfileUtils::EntriesToServiceProfile(values, profile);
1621 EXPECT_EQ(DP_SUCCESS, ret1);
1622 EXPECT_EQ("", profile.GetServiceName());
1623
1624 for (int32_t i = 0; i < MAX_STRING_LEN + 5; i++) {
1625 strValye += "a";
1626 }
1627 values[SERVICE_NAME] = strValye;
1628 values[SERVICE_TYPE] = strValye;
1629 auto propertiesMap = ProfileUtils::GetProfilePropertiesMap(values);
1630 EXPECT_EQ(false, propertiesMap.count(SERVICE_TYPE) != 0);
1631 EXPECT_EQ(false, 0 < propertiesMap[SERVICE_TYPE].length());
1632 EXPECT_EQ(true, propertiesMap[SERVICE_TYPE].length() < MAX_STRING_LEN);
1633 ProfileUtils::EntriesToServiceProfile(values, profile);
1634
1635 values[SERVICE_NAME] = "";
1636 values[SERVICE_TYPE] = "";
1637 propertiesMap = ProfileUtils::GetProfilePropertiesMap(values);
1638 EXPECT_EQ(false, propertiesMap.count(SERVICE_TYPE) != 0);
1639 EXPECT_EQ(false, 0 < propertiesMap[SERVICE_TYPE].length());
1640 ProfileUtils::EntriesToServiceProfile(values, profile);
1641
1642 values.erase(SERVICE_NAME);
1643 values.erase(SERVICE_TYPE);
1644 propertiesMap = ProfileUtils::GetProfilePropertiesMap(values);
1645 EXPECT_EQ(false, propertiesMap.count(SERVICE_TYPE) != 0);
1646 ProfileUtils::EntriesToServiceProfile(values, profile);
1647
1648 for (int32_t i = 0; i < MAX_DB_RECORD_SIZE + 5; i++) {
1649 values[to_string(i)] = "value";
1650 }
1651 int32_t ret2 = ProfileUtils::EntriesToServiceProfile(values, profile);
1652 EXPECT_EQ(DP_INVALID_PARAMS, ret2);
1653 }
1654
1655 /**
1656 * @tc.name: EntriesToCharProfile002
1657 * @tc.desc: EntriesToCharProfile all.
1658 * @tc.type: FUNC
1659 * @tc.require:
1660 */
1661 HWTEST_F(ProfileUtilsTest, EntriesToCharProfile002, TestSize.Level1)
1662 {
1663 map<string, string> values;
1664 CharacteristicProfile profile;
1665 string strValye = "";
1666 values[CHARACTERISTIC_KEY] = "characteristicKey";
1667 values[CHARACTERISTIC_VALUE] = "characteristicValue";
1668
1669 int32_t ret1 = ProfileUtils::EntriesToCharProfile(values, profile);
1670 EXPECT_EQ(DP_SUCCESS, ret1);
1671 EXPECT_EQ("", profile.GetCharacteristicKey());
1672
1673 for (int32_t i = 0; i < MAX_STRING_LEN + 5; i++) {
1674 strValye += "a";
1675 }
1676 values[CHARACTERISTIC_KEY] = strValye;
1677 values[CHARACTERISTIC_VALUE] = strValye;
1678 EXPECT_EQ(true, values.count(CHARACTERISTIC_KEY) != 0);
1679 EXPECT_EQ(true, 0 < values[CHARACTERISTIC_KEY].length());
1680 EXPECT_EQ(false, values[CHARACTERISTIC_KEY].length() < MAX_STRING_LEN);
1681 ProfileUtils::EntriesToCharProfile(values, profile);
1682
1683 values[CHARACTERISTIC_KEY] = "";
1684 values[CHARACTERISTIC_VALUE] = "";
1685 EXPECT_EQ(true, values.count(CHARACTERISTIC_KEY) != 0);
1686 EXPECT_EQ(false, 0 < values[CHARACTERISTIC_KEY].length());
1687 ProfileUtils::EntriesToCharProfile(values, profile);
1688
1689 values.erase(CHARACTERISTIC_KEY);
1690 values.erase(CHARACTERISTIC_VALUE);
1691 EXPECT_EQ(false, values.count(CHARACTERISTIC_KEY) != 0);
1692 ProfileUtils::EntriesToCharProfile(values, profile);
1693
1694 for (int32_t i = 0; i < MAX_DB_RECORD_SIZE + 5; i++) {
1695 values[to_string(i)] = "value";
1696 }
1697 int32_t ret2 = ProfileUtils::EntriesToCharProfile(values, profile);
1698 EXPECT_EQ(DP_INVALID_PARAMS, ret2);
1699 }
1700
1701 /**
1702 * @tc.name: GetDbKeyByProfile001
1703 * @tc.desc:
1704 * @tc.type: FUNC
1705 * @tc.require:
1706 */
1707 HWTEST_F(ProfileUtilsTest, GetDbKeyByProfile001, TestSize.Level1)
1708 {
1709 CharacteristicProfile charProfile;
1710 charProfile.SetDeviceId("deviceId");
1711 charProfile.SetServiceName("serviceName");
1712 charProfile.SetCharacteristicKey("characteristicKey");
1713 charProfile.SetCharacteristicValue("characteristicValue");
1714 string dbKey = "";
1715 string succDbkey = "char#deviceId#serviceName#characteristicKey#characteristicValue";
1716 string failDbkey = "";
1717 dbKey = ProfileUtils::GetDbKeyByProfile(charProfile);
1718 EXPECT_EQ(dbKey, succDbkey);
1719
1720 charProfile.SetCharacteristicKey("");
1721 dbKey = ProfileUtils::GetDbKeyByProfile(charProfile);
1722 EXPECT_EQ(dbKey, failDbkey);
1723
1724 charProfile.SetServiceName("");
1725 dbKey = ProfileUtils::GetDbKeyByProfile(charProfile);
1726 EXPECT_EQ(dbKey, failDbkey);
1727
1728 charProfile.SetDeviceId("");
1729 dbKey = ProfileUtils::GetDbKeyByProfile(charProfile);
1730 EXPECT_EQ(dbKey, failDbkey);
1731 }
1732
1733 /**
1734 * @tc.name: IsNumStr001
1735 * @tc.desc:
1736 * @tc.type: FUNC
1737 * @tc.require:
1738 */
1739 HWTEST_F(ProfileUtilsTest, IsNumStr001, TestSize.Level1)
1740 {
1741 bool isNumStr = ProfileUtils::IsNumStr("");
1742 EXPECT_EQ(isNumStr, false);
1743 }
1744 } // namespace DistributedDeviceProfile
1745 } // namespace OHOS
1746