1 /*
2 * Copyright (c) 2021-2022 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
18 #define private public
19 #define protected public
20 #include "bool_wrapper.h"
21 #include "int_wrapper.h"
22 #include "long_wrapper.h"
23 #include "refbase.h"
24 #include "skills.h"
25 #include "string_wrapper.h"
26 #undef private
27 #undef protected
28
29 using namespace testing::ext;
30 using namespace OHOS::AAFwk;
31 using OHOS::Parcel;
32
33 namespace OHOS {
34 namespace AAFwk {
35 static const int LARGE_STR_LEN = 65534;
36 static const int SET_COUNT = 20;
37 static const int DISMATCH_DATA = -102;
38 class SkillsBaseTest : public testing::Test {
39 public:
SkillsBaseTest()40 SkillsBaseTest()
41 {}
~SkillsBaseTest()42 ~SkillsBaseTest()
43 {}
44 static void SetUpTestCase(void);
45 static void TearDownTestCase(void);
46 void SetUp();
47 void TearDown();
48
49 std::shared_ptr<Skills> base_ = nullptr;
50 void CompareSkills(const std::shared_ptr<Skills> &skills1, const std::shared_ptr<Skills> &skills2) const;
51 };
52
SetUpTestCase(void)53 void SkillsBaseTest::SetUpTestCase(void)
54 {}
55
TearDownTestCase(void)56 void SkillsBaseTest::TearDownTestCase(void)
57 {}
58
SetUp(void)59 void SkillsBaseTest::SetUp(void)
60 {
61 base_ = std::make_shared<Skills>();
62 }
63
TearDown(void)64 void SkillsBaseTest::TearDown(void)
65 {}
66
67 /**
68 * @tc.number: AaFwk_Skills_Parcelable_0100
69 * @tc.name: Marshalling/Unmarshalling
70 * @tc.desc: marshalling Skills, and then check result.
71 */
72 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Parcelable_0100, Function | MediumTest | Level1)
73 {
74 std::shared_ptr<Skills> SkillsIn_ = std::make_shared<Skills>();
75 if (SkillsIn_ == nullptr) {
76 return;
77 }
78 SkillsIn_->AddEntity("12345");
79 SkillsIn_->AddAction("12345");
80 SkillsIn_->AddAuthority("12345");
81 SkillsIn_->AddScheme("12345");
82 SkillsIn_->AddPath("12345");
83 SkillsIn_->AddSchemeSpecificPart("12345");
84 SkillsIn_->AddType("12345");
85 WantParams wantParams;
86 std::string keyStr = "12345667";
87 bool valueBool = true;
88 wantParams.SetParam(keyStr, Boolean::Box(valueBool));
89 SkillsIn_->SetWantParams(wantParams);
90
91 Parcel in;
92 std::shared_ptr<Skills> SkillsOut_(Skills::Unmarshalling(in));
93 SkillsIn_->Marshalling(in);
94 if (SkillsOut_ != nullptr) {
95 CompareSkills(SkillsIn_, SkillsOut_);
96 EXPECT_EQ(valueBool, Boolean::Unbox(IBoolean::Query(SkillsOut_->GetWantParams().GetParam(keyStr))));
97 }
98 }
99
100 /**
101 * @tc.number: AaFwk_Skills_Parcelable_0200
102 * @tc.name: Marshalling/Unmarshalling
103 * @tc.desc: marshalling Skills, and then check result.
104 */
105 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Parcelable_0200, Function | MediumTest | Level1)
106 {
107 std::shared_ptr<Skills> SkillsIn_ = std::make_shared<Skills>();
108 if (SkillsIn_ == nullptr) {
109 return;
110 }
111
112 SkillsIn_->AddEntity("@#¥#3243adsafdf_中文");
113 SkillsIn_->AddAction("@#¥#3243adsafdf_中文");
114 SkillsIn_->AddAuthority("@#¥#3243adsafdf_中文");
115 SkillsIn_->AddScheme("@#¥#3243adsafdf_中文");
116 SkillsIn_->AddPath("@#¥#3243adsafdf_中文");
117 SkillsIn_->AddSchemeSpecificPart("@#¥#3243adsafdf_中文");
118 SkillsIn_->AddType("@#¥#3243adsafdf_中文");
119 WantParams wantParams;
120 std::string keyStr = "@#¥#3243adsafdf_中文";
121 long valueLong = 12345L;
122 wantParams.SetParam(keyStr, Long::Box(valueLong));
123 EXPECT_EQ(valueLong, Long::Unbox(ILong::Query(wantParams.GetParam(keyStr))));
124
125 SkillsIn_->SetWantParams(wantParams);
126
127 Parcel in;
128 SkillsIn_->Marshalling(in);
129 std::shared_ptr<Skills> SkillsOut_(Skills::Unmarshalling(in));
130
131 if (SkillsOut_ != nullptr) {
132 CompareSkills(SkillsIn_, SkillsOut_);
133 std::string result = String::Unbox(IString::Query(SkillsOut_->GetWantParams().GetParam(keyStr)));
134 EXPECT_STREQ(std::to_string(valueLong).c_str(), result.c_str());
135 }
136 }
137
138 /**
139 * @tc.number: AaFwk_Skills_Parcelable_0300
140 * @tc.name: Marshalling/Unmarshalling
141 * @tc.desc: marshalling Skills, and then check result.
142 */
143 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Parcelable_0300, Function | MediumTest | Level1)
144 {
145 std::shared_ptr<Skills> SkillsIn_ = std::make_shared<Skills>();
146 if (SkillsIn_ == nullptr) {
147 return;
148 }
149
150 SkillsIn_->AddEntity("");
151 SkillsIn_->AddAction("");
152 SkillsIn_->AddAuthority("");
153 SkillsIn_->AddScheme("");
154 SkillsIn_->AddPath("");
155 SkillsIn_->AddSchemeSpecificPart("");
156 SkillsIn_->AddType("");
157 WantParams wantParams;
158 std::string keyStr = "";
159 int valueInt = 123;
160 wantParams.SetParam(keyStr, Integer::Box(valueInt));
161
162 SkillsIn_->SetWantParams(wantParams);
163 EXPECT_EQ(valueInt, Integer::Unbox(IInteger::Query(wantParams.GetParam(keyStr))));
164
165 Parcel in;
166 SkillsIn_->Marshalling(in);
167 std::shared_ptr<Skills> SkillsOut_(Skills::Unmarshalling(in));
168
169 if (SkillsOut_ != nullptr) {
170 CompareSkills(SkillsIn_, SkillsOut_);
171 EXPECT_EQ(valueInt, Integer::Unbox(IInteger::Query(SkillsOut_->GetWantParams().GetParam(keyStr))));
172 }
173 }
174
175 /**
176 * @tc.number: AaFwk_Skills_Parcelable_0400
177 * @tc.name: Marshalling/Unmarshalling
178 * @tc.desc: marshalling Skills, and then check result.
179 */
180 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Parcelable_0400, Function | MediumTest | Level1)
181 {
182 std::shared_ptr<Skills> SkillsIn_ = std::make_shared<Skills>();
183 if (SkillsIn_ == nullptr) {
184 return;
185 }
186 SkillsIn_->AddEntity("12345");
187 SkillsIn_->AddAction("12345");
188 SkillsIn_->AddAuthority("12345");
189 SkillsIn_->AddScheme("12345");
190 SkillsIn_->AddPath("12345");
191 SkillsIn_->AddSchemeSpecificPart("12345");
192 SkillsIn_->AddType("12345");
193 SkillsIn_->AddEntity("@#¥#3243adsafdf_中文");
194 SkillsIn_->AddAction("@#¥#3243adsafdf_中文");
195 SkillsIn_->AddAuthority("@#¥#3243adsafdf_中文");
196 SkillsIn_->AddScheme("@#¥#3243adsafdf_中文");
197 SkillsIn_->AddPath("@#¥#3243adsafdf_中文");
198 SkillsIn_->AddSchemeSpecificPart("@#¥#3243adsafdf_中文");
199 SkillsIn_->AddType("@#¥#3243adsafdf_中文");
200 SkillsIn_->AddEntity("");
201 SkillsIn_->AddAction("");
202 SkillsIn_->AddAuthority("");
203 SkillsIn_->AddScheme("");
204 SkillsIn_->AddPath("");
205 SkillsIn_->AddSchemeSpecificPart("");
206 SkillsIn_->AddType("");
207 WantParams wantParams;
208 std::string keyStr = "12345667";
209 std::string valueString = "123";
210 wantParams.SetParam(keyStr, String::Box(valueString));
211 SkillsIn_->SetWantParams(wantParams);
212
213 Parcel in;
214 SkillsIn_->Marshalling(in);
215 std::shared_ptr<Skills> SkillsOut_(Skills::Unmarshalling(in));
216
217 if (SkillsOut_ != nullptr) {
218 CompareSkills(SkillsIn_, SkillsOut_);
219 EXPECT_EQ(valueString, String::Unbox(IString::Query(SkillsOut_->GetWantParams().GetParam(keyStr))));
220 }
221 }
222
CompareSkills(const std::shared_ptr<Skills> & skills1,const std::shared_ptr<Skills> & skills2) const223 void SkillsBaseTest::CompareSkills(const std::shared_ptr<Skills> &skills1, const std::shared_ptr<Skills> &skills2) const
224 {
225 EXPECT_EQ(skills1->CountEntities(), skills2->CountEntities());
226 EXPECT_EQ(skills1->CountActions(), skills2->CountActions());
227 EXPECT_EQ(skills1->CountAuthorities(), skills2->CountAuthorities());
228 EXPECT_EQ(skills1->CountSchemes(), skills2->CountSchemes());
229 EXPECT_EQ(skills1->CountPaths(), skills2->CountPaths());
230 EXPECT_EQ(skills1->CountSchemeSpecificParts(), skills2->CountSchemeSpecificParts());
231 EXPECT_EQ(skills1->CountTypes(), skills2->CountTypes());
232
233 int count = skills1->CountEntities();
234 for (int i = 0; i < count; i++) {
235 EXPECT_EQ(skills1->GetEntity(i), skills1->GetEntity(i));
236 }
237 count = skills1->CountActions();
238 for (int i = 0; i < count; i++) {
239 EXPECT_EQ(skills1->GetAction(i), skills1->GetAction(i));
240 }
241 count = skills1->CountAuthorities();
242 for (int i = 0; i < count; i++) {
243 EXPECT_EQ(skills1->GetAuthority(i), skills1->GetAuthority(i));
244 }
245 count = skills1->CountSchemes();
246 for (int i = 0; i < count; i++) {
247 EXPECT_EQ(skills1->GetScheme(i), skills1->GetScheme(i));
248 }
249 count = skills1->CountPaths();
250 for (int i = 0; i < count; i++) {
251 EXPECT_EQ(skills1->GetPath(i), skills1->GetPath(i));
252 }
253 count = skills1->CountSchemeSpecificParts();
254 for (int i = 0; i < count; i++) {
255 EXPECT_EQ(skills1->GetSchemeSpecificPart(i), skills1->GetSchemeSpecificPart(i));
256 }
257 count = skills1->CountTypes();
258 for (int i = 0; i < count; i++) {
259 EXPECT_EQ(skills1->GetType(i), skills1->GetType(i));
260 }
261
262 std::set<std::string> key1;
263 std::set<std::string> key2;
264 key1 = skills1->GetWantParams().KeySet();
265 key2 = skills2->GetWantParams().KeySet();
266 EXPECT_EQ(key1.size(), key2.size());
267
268 if (key1.size() > 0 && key2.size() > 0) {
269 std::set<std::string>::iterator iter1 = key1.begin();
270 std::set<std::string>::iterator iter2 = key2.begin();
271 for (; (iter1 != key1.end() && iter2 != key2.end()); iter1++, iter2++) {
272 EXPECT_EQ(*iter1, *iter2);
273 }
274 }
275 }
276
277 /**
278 * @tc.number: AaFwk_Skills_Entities_0100
279 * @tc.name: CountEntitie/HasEntity/GetEntity
280 * @tc.desc: Verify the function when the input string contains special characters.
281 */
282 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Entities_0100, Function | MediumTest | Level1)
283 {
284 std::string empty;
285 std::string entities = "entities.system.test";
286 EXPECT_EQ(0, base_->CountEntities());
287 EXPECT_EQ(false, base_->HasEntity(entities));
288 EXPECT_EQ(empty, base_->GetEntity(0));
289
290 base_->RemoveEntity(entities);
291 EXPECT_EQ(0, base_->CountEntities());
292 EXPECT_EQ(false, base_->HasEntity(entities));
293 }
294 /**
295 * @tc.number: AaFwk_Skills_GetEntities_0100
296 * @tc.name: AddEntity and GetEntities
297 * @tc.desc: Verify AddEntity and GetEntities.
298 */
299 HWTEST_F(SkillsBaseTest, AaFwk_Skills_GetEntities_0100, Function | MediumTest | Level1)
300 {
301 std::string entity = "12345667";
302 base_->AddEntity(entity);
303
304 size_t length = base_->GetEntities().size();
305
306 EXPECT_EQ((size_t)1, length);
307 EXPECT_EQ(entity, base_->GetEntities().at(0));
308 }
309
310 /**
311 * @tc.number: AaFwk_Skills_Authorities_0100
312 * @tc.name: CountEntitie/HasEntity/GetEntity
313 * @tc.desc: Verify the function when the input string has a long size.
314 */
315 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Authorities_0100, Function | MediumTest | Level1)
316 {
317 std::string empty;
318 std::string authorities = "authorities.system.test";
319 EXPECT_EQ(0, base_->CountAuthorities());
320 EXPECT_EQ(false, base_->HasAuthority(authorities));
321 EXPECT_EQ(empty, base_->GetAuthority(0));
322
323 base_->RemoveAuthority(authorities);
324 EXPECT_EQ(0, base_->CountAuthorities());
325 EXPECT_EQ(false, base_->HasAuthority(authorities));
326 }
327
328 /**
329 * @tc.number: AaFwk_Skills_Path_0300
330 * @tc.name: CountPaths/HasPath/GetPath
331 * @tc.desc: Verify the function when the input string is overrided.
332 */
333 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Path_0300, Function | MediumTest | Level1)
334 {
335 std::string empty;
336 std::string path = "paths.system.test";
337 PatternsMatcher pm(path, MatchType::DEFAULT);
338 base_->AddPath(pm);
339
340 EXPECT_EQ(1, base_->CountPaths());
341 EXPECT_EQ(true, base_->HasPath(path));
342 EXPECT_EQ(path, base_->GetPath(0));
343
344 base_->RemovePath(pm);
345 EXPECT_EQ(0, base_->CountPaths());
346 EXPECT_EQ(false, base_->HasPath(path));
347 }
348
349 /**
350 * @tc.number: AaFwk_Skills_Action_0100
351 * @tc.name: AddAction/CountActions/HasAction/GetAction
352 * @tc.desc: Verify the function when the input string is set 20 times.
353 */
354 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Action_0100, Function | MediumTest | Level1)
355 {
356 std::string empty;
357 std::string action = "action.system.test";
358 int actionCount = 1;
359
360 for (int i = 0; i < SET_COUNT; i++) {
361 base_->AddAction(action);
362 }
363
364 EXPECT_EQ(actionCount, base_->CountActions());
365 EXPECT_EQ(true, base_->HasAction(action));
366 EXPECT_EQ(action, base_->GetAction(0));
367
368 base_->RemoveAction(action);
369 EXPECT_EQ(0, base_->CountActions());
370 EXPECT_EQ(false, base_->HasAction(action));
371 }
372
373 /**
374 * @tc.number: AaFwk_Skills_ActionsIterator_0100
375 * @tc.name: ActionsIterator
376 * @tc.desc: Test the function of ActionsIterator.
377 */
378 HWTEST_F(SkillsBaseTest, AaFwk_Skills_ActionsIterator_0100, Function | MediumTest | Level1)
379 {
380 base_->actions_.push_back("a");
381 base_->actions_.push_back("b");
382 auto iter = base_->ActionsIterator();
383
384 EXPECT_EQ(*iter, "a");
385 }
386
387 /**
388 * @tc.number: AaFwk_Skills_Entity_0100
389 * @tc.name: CountEntities/HasEntity/CountEntities/GetEntity
390 * @tc.desc: Verify the function when the input string is default.
391 */
392 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Entity_0100, Function | MediumTest | Level1)
393 {
394 std::string empty;
395 std::string entity = "entity.system.test";
396 int entityCount = 1;
397
398 for (int i = 0; i < SET_COUNT; i++) {
399 base_->AddEntity(entity);
400 }
401
402 EXPECT_EQ(entityCount, base_->CountEntities());
403 EXPECT_EQ(true, base_->HasEntity(entity));
404 EXPECT_EQ(entity, base_->GetEntity(0));
405
406 base_->RemoveEntity(entity);
407 EXPECT_EQ(0, base_->CountEntities());
408 EXPECT_EQ(false, base_->HasEntity(entity));
409 }
410
411 /**
412 * @tc.number: AaFwk_Skills_Authority_0100
413 * @tc.name: CountAuthorities/HasAuthority/GetAuthority
414 * @tc.desc: Verify the function when the input string contains special characters.
415 */
416 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Authority_0100, Function | MediumTest | Level1)
417 {
418 std::string empty;
419 std::string authority = "Authority.system.test";
420 int authorityCount = 1;
421
422 for (int i = 0; i < SET_COUNT; i++) {
423 base_->AddAuthority(authority);
424 }
425
426 EXPECT_EQ(authorityCount, base_->CountAuthorities());
427 EXPECT_EQ(true, base_->HasAuthority(authority));
428 EXPECT_EQ(authority, base_->GetAuthority(0));
429
430 base_->RemoveAuthority(authority);
431 EXPECT_EQ(0, base_->CountAuthorities());
432 EXPECT_EQ(false, base_->HasAuthority(authority));
433 }
434
435 /**
436 * @tc.number: AaFwk_Skills_Path_0100
437 * @tc.name: CountPaths/HasPath/GetPath
438 * @tc.desc: Verify the function when the input string contains special characters.
439 */
440 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Path_0100, Function | MediumTest | Level1)
441 {
442 std::string empty;
443 std::string path = "Path.system.test";
444 int pathCount = 1;
445
446 for (int i = 0; i < SET_COUNT; i++) {
447 base_->AddPath(path);
448 }
449
450 EXPECT_EQ(pathCount, base_->CountPaths());
451 EXPECT_EQ(true, base_->HasPath(path));
452 EXPECT_EQ(path, base_->GetPath(0));
453
454 base_->RemovePath(path);
455 EXPECT_EQ(0, base_->CountPaths());
456 EXPECT_EQ(false, base_->HasPath(path));
457 }
458
459 /**
460 * @tc.number: AaFwk_Skills_Scheme_0100
461 * @tc.name: CountSchemes/HasScheme/GetScheme
462 * @tc.desc: Verify the function when the input string contains special characters.
463 */
464 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Scheme_0100, Function | MediumTest | Level1)
465 {
466 std::string empty;
467 std::string scheme = "scheme.system.test";
468 int schemeCount = 1;
469
470 for (int i = 0; i < SET_COUNT; i++) {
471 base_->AddScheme(scheme);
472 }
473
474 EXPECT_EQ(schemeCount, base_->CountSchemes());
475 EXPECT_EQ(true, base_->HasScheme(scheme));
476 EXPECT_EQ(scheme, base_->GetScheme(0));
477
478 base_->RemoveScheme(scheme);
479 EXPECT_EQ(0, base_->CountSchemes());
480 EXPECT_EQ(false, base_->HasScheme(scheme));
481 }
482
483 /**
484 * @tc.number: AaFwk_Skills_SchemeSpecificPart_0100
485 * @tc.name: CountSchemeSpecificParts/HasSchemeSpecificPart/GetSchemeSpecificPart
486 * @tc.desc: Verify the function when the input string contains special characters.
487 */
488 HWTEST_F(SkillsBaseTest, AaFwk_Skills_SchemeSpecificPart_0100, Function | MediumTest | Level1)
489 {
490 std::string empty;
491 std::string schemespecificpart = "schemespecificpart.system.test";
492 int schemespecificpartCount = 1;
493
494 for (int i = 0; i < SET_COUNT; i++) {
495 base_->AddSchemeSpecificPart(schemespecificpart);
496 }
497
498 EXPECT_EQ(schemespecificpartCount, base_->CountSchemeSpecificParts());
499 EXPECT_EQ(true, base_->HasSchemeSpecificPart(schemespecificpart));
500 EXPECT_EQ(schemespecificpart, base_->GetSchemeSpecificPart(0));
501
502 base_->RemoveSchemeSpecificPart(schemespecificpart);
503 EXPECT_EQ(0, base_->CountSchemeSpecificParts());
504 EXPECT_EQ(false, base_->HasSchemeSpecificPart(schemespecificpart));
505 }
506
507 /**
508 * @tc.number: AaFwk_Skills_Type_0100
509 * @tc.name: CountTypes/HasType/GetType
510 * @tc.desc: Verify the function when the input string contains special characters.
511 */
512 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Type_0100, Function | MediumTest | Level1)
513 {
514 std::string empty;
515 std::string type = "type/system.test";
516 int typeCount = 1;
517
518 for (int i = 0; i < SET_COUNT; i++) {
519 base_->AddType(type);
520 }
521
522 EXPECT_EQ(typeCount, base_->CountTypes());
523 EXPECT_EQ(true, base_->HasType(type));
524 EXPECT_EQ(type, base_->GetType(0));
525
526 base_->RemoveType(type);
527 EXPECT_EQ(0, base_->CountTypes());
528 EXPECT_EQ(false, base_->HasType(type));
529 }
530
531 /**
532 * @tc.number: AaFwk_Skills_Actions_0100
533 * @tc.name: CountActions/HasAuthority/GetAuthority
534 * @tc.desc: Verify the function when the input string contains special characters.
535 */
536 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Actions_0100, Function | MediumTest | Level1)
537 {
538 std::string empty;
539 std::string actions = "actions.system.test";
540 EXPECT_EQ(0, base_->CountActions());
541 EXPECT_EQ(false, base_->HasAuthority(actions));
542 EXPECT_EQ(empty, base_->GetAuthority(0));
543
544 base_->RemoveAuthority(actions);
545 EXPECT_EQ(0, base_->CountActions());
546 EXPECT_EQ(false, base_->HasAuthority(actions));
547 }
548
549 /**
550 * @tc.number: AaFwk_Skills_Schemes_0100
551 * @tc.name: CountSchemes/HasAuthority/GetAuthority
552 * @tc.desc: Verify the function when the input string contains special characters.
553 */
554 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Schemes_0100, Function | MediumTest | Level1)
555 {
556 std::string empty;
557 std::string schemes = "schemes.system.test";
558 EXPECT_EQ(0, base_->CountSchemes());
559 EXPECT_EQ(false, base_->HasAuthority(schemes));
560 EXPECT_EQ(empty, base_->GetAuthority(0));
561
562 base_->RemoveAuthority(schemes);
563 EXPECT_EQ(0, base_->CountSchemes());
564 EXPECT_EQ(false, base_->HasAuthority(schemes));
565 }
566
567 /**
568 * @tc.number: AaFwk_Skills_SchemeSpecificParts_0100
569 * @tc.name: CountSchemeSpecificParts/HasAuthority/GetAuthority
570 * @tc.desc: Verify the function when the input string contains special characters.
571 */
572 HWTEST_F(SkillsBaseTest, AaFwk_Skills_SchemeSpecificParts_0100, Function | MediumTest | Level1)
573 {
574 std::string empty;
575 std::string schemespecificparts = "schemespecificparts.system.test";
576 EXPECT_EQ(0, base_->CountSchemeSpecificParts());
577 EXPECT_EQ(false, base_->HasAuthority(schemespecificparts));
578 EXPECT_EQ(empty, base_->GetAuthority(0));
579
580 base_->RemoveAuthority(schemespecificparts);
581 EXPECT_EQ(0, base_->CountSchemeSpecificParts());
582 EXPECT_EQ(false, base_->HasAuthority(schemespecificparts));
583 }
584
585 /**
586 * @tc.number: AaFwk_Skills_Types_0100
587 * @tc.name: CountTypes/HasAuthority/GetAuthority
588 * @tc.desc: Verify the function when the input string contains special characters.
589 */
590 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Types_0100, Function | MediumTest | Level1)
591 {
592 std::string empty;
593 std::string types = "types.system.test";
594 GTEST_LOG_(INFO) << "---------------a ";
595 EXPECT_EQ(0, base_->CountTypes());
596 GTEST_LOG_(INFO) << "---------------b ";
597 EXPECT_EQ(false, base_->HasAuthority(types));
598 GTEST_LOG_(INFO) << "---------------1 ";
599 EXPECT_EQ(empty, base_->GetAuthority(0));
600 GTEST_LOG_(INFO) << "---------------2 ";
601
602 base_->RemoveAuthority(types);
603 EXPECT_EQ(0, base_->CountTypes());
604 EXPECT_EQ(false, base_->HasAuthority(types));
605 }
606
607 /**
608 * @tc.number: AaFwk_Skills_Action_0200
609 * @tc.name: CountActions/HasAction/GetAction
610 * @tc.desc: Verify the function when action is not exist.
611 */
612 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Action_0200, Function | MediumTest | Level1)
613 {
614 std::string empty;
615 std::string action = "action.system.test";
616 EXPECT_EQ(0, base_->CountActions());
617 EXPECT_EQ(false, base_->HasAction(action));
618 EXPECT_EQ(empty, base_->GetAction(0));
619
620 base_->RemoveAction(action);
621 EXPECT_EQ(0, base_->CountActions());
622 EXPECT_EQ(false, base_->HasAction(action));
623 }
624
625 /**
626 * @tc.number: AaFwk_Skills_Authority_0200
627 * @tc.name: CountAuthorities/HasAuthority/GetAuthority
628 * @tc.desc: Verify the function when action is not exist.
629 */
630 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Authority_0200, Function | MediumTest | Level1)
631 {
632 std::string empty;
633 std::string authority = "authority.system.test";
634 EXPECT_EQ(0, base_->CountAuthorities());
635 EXPECT_EQ(false, base_->HasAuthority(authority));
636 EXPECT_EQ(empty, base_->GetAuthority(0));
637
638 base_->RemoveAuthority(authority);
639 EXPECT_EQ(0, base_->CountAuthorities());
640 EXPECT_EQ(false, base_->HasAuthority(authority));
641 }
642
643 /**
644 * @tc.number: AaFwk_Skills_Path_0200
645 * @tc.name: CountPaths/HasPath/GetPath
646 * @tc.desc: Verify the function when action is not exist.
647 */
648 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Path_0200, Function | MediumTest | Level1)
649 {
650 std::string empty;
651 std::string path = "path.system.test";
652 base_->AddPath(path, MatchType::DEFAULT);
653
654 EXPECT_EQ(1, base_->CountPaths());
655 EXPECT_EQ(true, base_->HasPath(path));
656 EXPECT_EQ(path, base_->GetPath(0));
657
658 base_->RemovePath(path, MatchType::DEFAULT);
659 EXPECT_EQ(0, base_->CountPaths());
660 EXPECT_EQ(false, base_->HasPath(path));
661 }
662
663 /**
664 * @tc.number: AaFwk_Skills_Scheme_0200
665 * @tc.name: CountSchemes/HasScheme/GetScheme
666 * @tc.desc: Verify the function when action is not exist.
667 */
668 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Scheme_0200, Function | MediumTest | Level1)
669 {
670 std::string empty;
671 std::string scheme = "scheme.system.test";
672 EXPECT_EQ(0, base_->CountSchemes());
673 EXPECT_EQ(false, base_->HasScheme(scheme));
674 EXPECT_EQ(empty, base_->GetScheme(0));
675
676 base_->RemoveScheme(scheme);
677 EXPECT_EQ(0, base_->CountSchemes());
678 EXPECT_EQ(false, base_->HasScheme(scheme));
679 }
680
681 /**
682 * @tc.number: AaFwk_Skills_SchemeSpecificPart_0200
683 * @tc.name: CountSchemeSpecificParts/HasSchemeSpecificPart/GetSchemeSpecificPart
684 * @tc.desc: Verify the function when action is not exist.
685 */
686 HWTEST_F(SkillsBaseTest, AaFwk_Skills_SchemeSpecificPart_0200, Function | MediumTest | Level1)
687 {
688 std::string empty;
689 std::string schemespecificpart = "schemespecificpart.system.test";
690 EXPECT_EQ(0, base_->CountSchemeSpecificParts());
691 EXPECT_EQ(false, base_->HasSchemeSpecificPart(schemespecificpart));
692 EXPECT_EQ(empty, base_->GetSchemeSpecificPart(0));
693
694 base_->RemoveSchemeSpecificPart(schemespecificpart);
695 EXPECT_EQ(0, base_->CountSchemeSpecificParts());
696 EXPECT_EQ(false, base_->HasSchemeSpecificPart(schemespecificpart));
697 }
698
699 /**
700 * @tc.number: AaFwk_Skills_Type_0300
701 * @tc.name: CountTypes/HasType/GetType
702 * @tc.desc: Verify the function when action is not exist.
703 */
704 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Type_0300, Function | MediumTest | Level1)
705 {
706 std::string empty;
707 std::string type = "type.system.test";
708 EXPECT_EQ(0, base_->CountTypes());
709
710 EXPECT_EQ(false, base_->HasType(type));
711 EXPECT_EQ(empty, base_->GetType(0));
712
713 base_->RemoveType(type);
714 EXPECT_EQ(0, base_->CountTypes());
715 EXPECT_EQ(false, base_->HasType(type));
716 }
717
718 using SkillsMatchType = std::tuple<std::string, std::string, bool>;
719 class SkillsMatchTest : public testing::TestWithParam<SkillsMatchType> {
720 public:
SkillsMatchTest()721 SkillsMatchTest()
722 {}
~SkillsMatchTest()723 ~SkillsMatchTest()
724 {}
725 static void SetUpTestCase(void);
726 static void TearDownTestCase(void);
727 void SetUp();
728 void TearDown();
729 std::shared_ptr<Skills> skills_ = nullptr;
730 };
731
SetUpTestCase(void)732 void SkillsMatchTest::SetUpTestCase(void)
733 {}
734
TearDownTestCase(void)735 void SkillsMatchTest::TearDownTestCase(void)
736 {}
737
SetUp(void)738 void SkillsMatchTest::SetUp(void)
739 {
740 skills_ = std::make_shared<Skills>();
741 }
742
TearDown(void)743 void SkillsMatchTest::TearDown(void)
744 {}
745
746 /**
747 * @tc.number: AaFwk_Skills_match_0100
748 * @tc.name: CountTypes/HasType/GetType
749 * @tc.desc: Verify whether parameter change.
750 */
751 HWTEST_P(SkillsMatchTest, AaFwk_Skills_match_0100, Function | MediumTest | Level1)
752 {
753 std::string filterEntity = "entity.system.entity1";
754 std::string filterAction1 = "action.system.action1";
755 std::string filterAction2 = "action.system.action2";
756 std::string wantEntity = std::get<0>(GetParam());
757 std::string wantAction = std::get<1>(GetParam());
758 bool result = std::get<2>(GetParam());
759
760 skills_->AddEntity(filterEntity);
761 skills_->AddAction(filterAction1);
762 skills_->AddAction(filterAction2);
763
764 Want want;
765 want.AddEntity(wantEntity);
766 want.SetAction(wantAction);
767
768 EXPECT_EQ(result, skills_->Match(want));
769 }
770
771 INSTANTIATE_TEST_SUITE_P(SkillsMatchTestP, SkillsMatchTest,
772 testing::Values(SkillsMatchType("entity.system.entityA", "action.system.actionA", false),
773 SkillsMatchType("entity.system.entity1", "action.system.actionA", false),
774 SkillsMatchType("entity.system.entityA", "action.system.action2", false),
775 SkillsMatchType("entity.system.entity1", "action.system.action1", true)));
776
777 /**
778 * @tc.name: AaFwk_Skills_match_0200
779 * @tc.desc: Verify Matching rules action segment
780 * @tc.type: FUNC
781 * @tc.require: I5PZK2
782 */
783 HWTEST_F(SkillsBaseTest, AaFwk_Skills_match_0200, Function | MediumTest | Level1)
784 {
785 Skills skills;
786 std::string filterEntity = "entity.system.entity1";
787 skills.AddEntity(filterEntity);
788 Want want;
789 want.AddEntity(filterEntity);
790 // Both actions are empty.
791 EXPECT_EQ(true, skills.Match(want));
792 }
793
794 /**
795 * @tc.name: AaFwk_Skills_match_0300
796 * @tc.desc: Verify Matching rules action segment
797 * @tc.type: FUNC
798 * @tc.require: I5PZK2
799 */
800 HWTEST_F(SkillsBaseTest, AaFwk_Skills_match_0300, Function | MediumTest | Level1)
801 {
802 Skills skills;
803 std::string filterEntity = "entity.system.entity1";
804 skills.AddEntity(filterEntity);
805 Want want;
806 want.AddEntity(filterEntity);
807 want.SetAction("action.system.action1");
808 // empty actions in skill vs non-empty actions in want
809 EXPECT_EQ(false, skills.Match(want));
810 }
811
812 /**
813 * @tc.name: AaFwk_Skills_match_0400
814 * @tc.desc: Verify Matching rules action segment
815 * @tc.type: FUNC
816 * @tc.require: I5PZK2
817 */
818 HWTEST_F(SkillsBaseTest, AaFwk_Skills_match_0400, Function | MediumTest | Level1)
819 {
820 Skills skills;
821 std::string filterEntity = "entity.system.entity1";
822 std::string filterAction1 = "action.system.action1";
823 std::string filterAction2 = "action.system.action2";
824 skills.AddEntity(filterEntity);
825 skills.AddAction(filterAction1);
826 skills.AddAction(filterAction2);
827 Want want;
828 want.AddEntity(filterEntity);
829 want.SetAction(filterAction1);
830 // actions that in skill contains non-empty actions in want
831 EXPECT_EQ(true, skills.Match(want));
832 }
833
834 /**
835 * @tc.name: AaFwk_Skills_match_0500
836 * @tc.desc: Verify Matching rules action segment
837 * @tc.type: FUNC
838 * @tc.require: I5PZK2
839 */
840 HWTEST_F(SkillsBaseTest, AaFwk_Skills_match_0600, Function | MediumTest | Level1)
841 {
842 Skills skills;
843 std::string filterEntity = "entity.system.entity1";
844 std::string filterAction1 = "action.system.action1";
845 std::string filterAction2 = "action.system.action2";
846 skills.AddEntity(filterEntity);
847 skills.AddAction(filterAction2);
848 Want want;
849 want.AddEntity(filterEntity);
850 // non-empty actions that in skill vs empty actions in want
851 EXPECT_EQ(false, skills.Match(want));
852 }
853
854 /**
855 * @tc.name: AaFwk_Skills_match_0600
856 * @tc.desc: Verify Matching rules action segment
857 * @tc.type: FUNC
858 * @tc.require: I5PZK2
859 */
860 HWTEST_F(SkillsBaseTest, AaFwk_Skills_match_0500, Function | MediumTest | Level1)
861 {
862 Skills skills;
863 std::string filterEntity = "entity.system.entity1";
864 std::string filterAction1 = "action.system.action1";
865 std::string filterAction2 = "action.system.action2";
866 skills.AddEntity(filterEntity);
867 skills.AddAction(filterAction2);
868 Want want;
869 want.AddEntity(filterEntity);
870 want.SetAction(filterAction1);
871 // actions that in skill doesn't contain non-empty actions in want
872 EXPECT_EQ(false, skills.Match(want));
873 }
874
875 /**
876 * @tc.number: AaFwk_Skills_Skills_0100
877 * @tc.name: Skills() and Skills(Skills)
878 * @tc.desc: Verify Skills().
879 */
880 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Skills_0100, Function | MediumTest | Level1)
881 {
882 Skills skills;
883
884 EXPECT_EQ(0, skills.CountEntities());
885 EXPECT_EQ(0, skills.CountActions());
886 EXPECT_EQ(0, skills.CountAuthorities());
887 EXPECT_EQ(0, skills.CountSchemes());
888
889 EXPECT_EQ(0, skills.CountPaths());
890 EXPECT_EQ(0, skills.CountSchemeSpecificParts());
891 EXPECT_EQ(0, skills.CountTypes());
892 EXPECT_EQ(0, skills.GetWantParams().Size());
893 }
894
895 /**
896 * @tc.number: AaFwk_Skills_Skills_0200
897 * @tc.name: Skills() and Skills(Skills)
898 * @tc.desc: Verify Skills().
899 */
900 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Skills_0200, Function | MediumTest | Level1)
901 {
902 Skills skillsBase;
903 std::string entityString = "entity";
904 skillsBase.AddEntity(entityString);
905 std::string actionString = "action";
906 skillsBase.AddAction(actionString);
907 std::string authorityString = "authority";
908 skillsBase.AddAuthority(authorityString);
909 std::string schemeString = "scheme";
910 skillsBase.AddScheme(schemeString);
911 std::string pathString = "path";
912 skillsBase.AddPath(pathString);
913 std::string schemeSpecificPartsString = "schemeSpecificParts";
914 skillsBase.AddSchemeSpecificPart(schemeSpecificPartsString);
915 std::string typeString = "/type";
916 skillsBase.AddType(typeString);
917 Skills skills(skillsBase);
918
919 EXPECT_EQ(entityString, skills.GetEntity(0));
920 EXPECT_EQ(actionString, skills.GetAction(0));
921 EXPECT_EQ(authorityString, skills.GetAuthority(0));
922 EXPECT_EQ(schemeString, skills.GetScheme(0));
923
924 EXPECT_EQ(pathString, skills.GetPath(0));
925 EXPECT_EQ(schemeSpecificPartsString, skills.GetSchemeSpecificPart(0));
926 EXPECT_EQ(typeString, skills.GetType(0));
927 }
928
929 /**
930 * @tc.number: AaFwk_Skills_addremoveType_0100
931 * @tc.name: addType(PatternsMatcher)/ removeType(PatternsMatcher)
932 * @tc.desc: Verify addType/removeType result.
933 */
934 HWTEST_F(SkillsBaseTest, AaFwk_Skills_addremoveType_0100, Function | MediumTest | Level1)
935 {
936 std::string patternStr = std::string("systems/*t");
937
938 PatternsMatcher pattern(patternStr, MatchType::DEFAULT);
939
940 base_->AddType(pattern);
941 std::string type1 = base_->GetType(0);
942 EXPECT_EQ(patternStr, type1);
943
944 base_->RemoveType(patternStr);
945
946 EXPECT_EQ(0, base_->CountEntities());
947
948 base_->AddType(pattern);
949
950 std::string patternStr2 = std::string("systems/*test");
951 PatternsMatcher pattern2(patternStr2, MatchType::PREFIX);
952 base_->AddType(pattern2);
953 std::string type2 = base_->GetType(1);
954 EXPECT_EQ(patternStr2, type2);
955
956 base_->RemoveType(pattern2);
957 EXPECT_EQ(0, base_->CountEntities());
958
959 std::string patternStr3 = std::string("systems/*test3");
960 base_->AddType(patternStr3, MatchType::GLOBAL);
961
962 std::string type3 = base_->GetType(1);
963 EXPECT_EQ(patternStr3, type3);
964
965 base_->RemoveType(patternStr3, MatchType::GLOBAL);
966
967 EXPECT_EQ(0, base_->CountEntities());
968 }
969
970 /**
971 * @tc.number: AaFwk_Skills_MatchData_0100
972 * @tc.name: MatchData
973 * @tc.desc: Test MatchData.
974 * @tc.require: issueI648W6
975 */
976 HWTEST_F(SkillsBaseTest, AaFwk_Skills_MatchData_0100, Function | MediumTest | Level1)
977 {
978 std::string type = "this is type";
979 std::string scheme = "this is scheme";
980 std::string value = "this is value";
981 OHOS::Uri data(value);
982 int result = base_->MatchData(type, scheme, data);
983 EXPECT_EQ(result, DISMATCH_DATA);
984 }
985
986 /**
987 * @tc.number: AaFwk_Skills_FindMimeType_0100
988 * @tc.name: FindMimeType
989 * @tc.desc: Test FindMimeType.
990 * @tc.require: issueI653GZ
991 */
992 HWTEST_F(SkillsBaseTest, AaFwk_Skills_FindMimeType_0100, Function | MediumTest | Level1)
993 {
994 std::string type = "";
995 bool result = base_->FindMimeType(type);
996 EXPECT_EQ(result, false);
997
998 std::string type1 = "this is type";
999 bool result1 = base_->FindMimeType(type1);
1000 EXPECT_EQ(result1, false);
1001 }
1002
1003 /**
1004 * @tc.number: AaFwk_Skills_RegionMatches_0100
1005 * @tc.name: RegionMatches
1006 * @tc.desc: Test RegionMatches.
1007 * @tc.require: issueI653GZ
1008 */
1009 HWTEST_F(SkillsBaseTest, AaFwk_Skills_RegionMatches_0100, Function | MediumTest | Level1)
1010 {
1011 std::string type = "this is type";
1012 int toffset = -1;
1013 std::string other = "this is other";
1014 int ooffset = -2;
1015 int len = 1;
1016 bool result = base_->RegionMatches(type, toffset, other, ooffset, len);
1017 EXPECT_EQ(result, false);
1018 }
1019
1020 /**
1021 * @tc.number: AaFwk_Skills_RegionMatches_0200
1022 * @tc.name: RegionMatches
1023 * @tc.desc: Test RegionMatches.
1024 * @tc.require: issueI653GZ
1025 */
1026 HWTEST_F(SkillsBaseTest, AaFwk_Skills_RegionMatches_0200, Function | MediumTest | Level1)
1027 {
1028 std::string type = "this is type";
1029 int toffset = 1;
1030 std::string other = "this is other";
1031 int ooffset = 2;
1032 int len = 2;
1033 bool result = base_->RegionMatches(type, toffset, other, ooffset, len);
1034 EXPECT_EQ(result, false);
1035
1036 int len1 = 0;
1037 bool result1 = base_->RegionMatches(type, toffset, other, ooffset, len1);
1038 EXPECT_EQ(result1, true);
1039 }
1040
1041 using testParamsType = std::tuple<std::string, std::string>;
1042 class SkillsParamsTest : public testing::TestWithParam<testParamsType> {
1043 public:
SkillsParamsTest()1044 SkillsParamsTest()
1045 {}
~SkillsParamsTest()1046 ~SkillsParamsTest()
1047 {}
1048 static void SetUpTestCase(void);
1049 static void TearDownTestCase(void);
1050 void SetUp();
1051 void TearDown();
1052 std::shared_ptr<Skills> skills_ = nullptr;
1053 };
1054
SetUpTestCase(void)1055 void SkillsParamsTest::SetUpTestCase(void)
1056 {}
1057
TearDownTestCase(void)1058 void SkillsParamsTest::TearDownTestCase(void)
1059 {}
1060
SetUp(void)1061 void SkillsParamsTest::SetUp(void)
1062 {
1063 skills_ = std::make_shared<Skills>();
1064 }
1065
TearDown(void)1066 void SkillsParamsTest::TearDown(void)
1067 {}
1068
1069 /**
1070 * @tc.number: AaFwk_Skills_Params_0100
1071 * @tc.name: SetWantParams/GetWantParams
1072 * @tc.desc: Verify addType/removeType result.
1073 */
1074 HWTEST_P(SkillsParamsTest, AaFwk_Skills_Params_0100, Function | MediumTest | Level1)
1075 {
1076 std::string keyStr = std::get<0>(GetParam());
1077 std::string valueStr = std::get<1>(GetParam());
1078 WantParams wantParams;
1079 wantParams.SetParam(keyStr, String::Box(valueStr));
1080 skills_->SetWantParams(wantParams);
1081 EXPECT_EQ(valueStr, String::Unbox(IString::Query(skills_->GetWantParams().GetParam(keyStr))));
1082 }
1083
1084 INSTANTIATE_TEST_SUITE_P(SkillsParamsTestCaseP, SkillsParamsTest,
1085 testing::Values(testParamsType("", "asdsdsdasa"), testParamsType(std::string(LARGE_STR_LEN + 1, 's'), "sadsdsdads"),
1086 testParamsType("#$%^&*(!@\":<>{}", "asdsdsdasa"), testParamsType("3456677", ""),
1087 testParamsType("1234", std::string(LARGE_STR_LEN + 1, 's')),
1088 testParamsType("2323sdasdZ", "#$%^&*(!@\":<>{}sadsdasdsaf"), testParamsType("12345667", "sdasdfdsffdgfdg"),
1089 testParamsType("", ""),
1090 testParamsType(std::string(LARGE_STR_LEN + 1, 'k'), std::string(LARGE_STR_LEN + 1, 'k')),
1091 testParamsType("#$%^&*(!@\":<>{},/", "#$%^&*(!@\":<>{},/")));
1092 } // namespace AAFwk
1093 } // namespace OHOS