1 /*
2  * Copyright (c) 2021-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 "native_token_receptor_test.h"
17 
18 #include <fcntl.h>
19 #include <memory>
20 #include <string>
21 #include <sys/socket.h>
22 #include <sys/stat.h>
23 #include <sys/un.h>
24 #include <thread>
25 #include <unistd.h>
26 #include <vector>
27 
28 #include "accesstoken_info_manager.h"
29 #include "accesstoken_kit.h"
30 #include "access_token_db.h"
31 #include "access_token_error.h"
32 #include "permission_definition_cache.h"
33 #include "permission_manager.h"
34 #include "permission_state_full.h"
35 #include "token_field_const.h"
36 #define private public
37 #include "nativetoken_kit.h"
38 #include "native_token_receptor.h"
39 #undef private
40 #include "securec.h"
41 
42 using namespace testing::ext;
43 using namespace OHOS::Security::AccessToken;
44 
45 namespace {
46 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "NativeTokenReceptorTest"};
47 }
48 
SetUpTestCase()49 void NativeTokenReceptorTest::SetUpTestCase()
50 {
51     // delete all test 0x28100000 - 0x28100007
52     for (unsigned int i = 0x28100000; i <= 0x28100007; i++) {
53         AccessTokenInfoManager::GetInstance().RemoveNativeTokenInfo(i);
54     }
55     PermissionDef infoManagerPermDef = {
56         .permissionName = "ohos.permission.DISCOVER_BLUETOOTH",
57         .bundleName = "accesstoken_test",
58         .grantMode = USER_GRANT,
59         .availableLevel = APL_NORMAL,
60         .provisionEnable = false,
61         .distributedSceneEnable = false,
62         .label = "label",
63         .labelId = 1,
64         .description = "CAMERA",
65         .descriptionId = 1
66     };
67     PermissionDefinitionCache::GetInstance().Insert(infoManagerPermDef, 1);
68     infoManagerPermDef.permissionName = "ohos.permission.MANAGE_USER_IDM";
69     PermissionDefinitionCache::GetInstance().Insert(infoManagerPermDef, 1);
70     infoManagerPermDef.permissionName = "ohos.permission.ACCELEROMETER";
71     PermissionDefinitionCache::GetInstance().Insert(infoManagerPermDef, 1);
72 }
73 
TearDownTestCase()74 void NativeTokenReceptorTest::TearDownTestCase()
75 {
76     sleep(3); // delay 3 minutes
77 }
78 
SetUp()79 void NativeTokenReceptorTest::SetUp()
80 {}
81 
TearDown()82 void NativeTokenReceptorTest::TearDown()
83 {
84     ACCESSTOKEN_LOG_INFO(LABEL, "test down!");
85 }
86 
87 /**
88  * @tc.name: ParserNativeRawData001
89  * @tc.desc: Verify processing right native token json.
90  * @tc.type: FUNC
91  * @tc.require: Issue Number
92  */
93 HWTEST_F(NativeTokenReceptorTest, ParserNativeRawData001, TestSize.Level1)
94 {
95     ACCESSTOKEN_LOG_INFO(LABEL, "test ParserNativeRawData001!");
96     std::string testStr = R"([)"\
97         R"({"processName":"process6","APL":3,"version":1,"tokenId":685266937,"tokenAttr":0,)"\
98         R"("dcaps":["AT_CAP","ST_CAP"], "permissions":[], "nativeAcls":[]},)"\
99         R"({"processName":"process5","APL":3,"version":1,"tokenId":678065606,"tokenAttr":0,)"\
100         R"("dcaps":["AT_CAP","ST_CAP"], "permissions":[], "nativeAcls":[]}])";
101 
102     NativeTokenReceptor& receptor = NativeTokenReceptor::GetInstance();
103     std::vector<std::shared_ptr<NativeTokenInfoInner>> tokenInfos;
104     receptor.ParserNativeRawData(testStr, tokenInfos);
105     ASSERT_EQ(static_cast<uint32_t>(2), tokenInfos.size());
106     ASSERT_NE(nullptr, tokenInfos[0]);
107     ASSERT_NE(nullptr, tokenInfos[1]);
108 
109     ASSERT_EQ("process6", tokenInfos[0]->GetProcessName());
110     ASSERT_EQ(static_cast<AccessTokenID>(685266937), tokenInfos[0]->GetTokenID());
111     ASSERT_EQ(static_cast<uint32_t>(2), tokenInfos[0]->GetDcap().size());
112     ASSERT_EQ("AT_CAP", (tokenInfos[0]->GetDcap())[0]);
113     ASSERT_EQ("ST_CAP", (tokenInfos[0]->GetDcap())[1]);
114 
115     ASSERT_EQ("process5", tokenInfos[1]->GetProcessName());
116     ASSERT_EQ(static_cast<AccessTokenID>(678065606), tokenInfos[1]->GetTokenID());
117     ASSERT_EQ(static_cast<uint32_t>(2), tokenInfos[1]->GetDcap().size());
118     ASSERT_EQ("AT_CAP", (tokenInfos[1]->GetDcap())[0]);
119     ASSERT_EQ("ST_CAP", (tokenInfos[1]->GetDcap())[1]);
120 }
121 
122 /**
123  * @tc.name: ParserNativeRawData002
124  * @tc.desc: Verify processing wrong native token json.
125  * @tc.type: FUNC
126  * @tc.require: Issue Number
127  */
128 HWTEST_F(NativeTokenReceptorTest, ParserNativeRawData002, TestSize.Level1)
129 {
130     ACCESSTOKEN_LOG_INFO(LABEL, "test ParserNativeRawData002!");
131     std::string testStr = R"([{"processName":""}])";
132     std::vector<std::shared_ptr<NativeTokenInfoInner>> tokenInfos;
133 
134     NativeTokenReceptor& receptor = NativeTokenReceptor::GetInstance();
135 
136     receptor.ParserNativeRawData(testStr, tokenInfos);
137     ASSERT_EQ(static_cast<uint32_t>(0), tokenInfos.size());
138 
139     std::string testStr1 = R"([{"processName":"", }])";
140     receptor.ParserNativeRawData(testStr1, tokenInfos);
141     ASSERT_EQ(static_cast<uint32_t>(0), tokenInfos.size());
142 
143     std::string testStr2 = R"([{"processName":"process6"}, {}])";
144     receptor.ParserNativeRawData(testStr2, tokenInfos);
145     ASSERT_EQ(static_cast<uint32_t>(0), tokenInfos.size());
146 
147     std::string testStr3 = R"([{"processName":""}, {"":"", ""}])";
148     receptor.ParserNativeRawData(testStr3, tokenInfos);
149     ASSERT_EQ(static_cast<uint32_t>(0), tokenInfos.size());
150 
151     std::string testStr4 = R"([{"processName":"process6", "tokenId":685266937, "APL":3, "version":new}])";
152     receptor.ParserNativeRawData(testStr4, tokenInfos);
153     ASSERT_EQ(static_cast<uint32_t>(0), tokenInfos.size());
154 
155     std::string testStr5 = R"([{"processName":"process6", "tokenId":685266937, "APL":7, "version":1}])";
156     receptor.ParserNativeRawData(testStr5, tokenInfos);
157     ASSERT_EQ(static_cast<uint32_t>(0), tokenInfos.size());
158 
159     std::string testStr6 =
160         R"({"NativeToken":[{"processName":"process6", "tokenId":685266937, "APL":7, "version":1}]})";
161     receptor.ParserNativeRawData(testStr6, tokenInfos);
162     ASSERT_EQ(static_cast<uint32_t>(0), tokenInfos.size());
163 
164     std::string testStr7 = R"({"NativeToken":[{"processName":"process6", "tokenId":685266937, "APL":7, "version":1}])";
165     receptor.ParserNativeRawData(testStr7, tokenInfos);
166     ASSERT_EQ(static_cast<uint32_t>(0), tokenInfos.size());
167 
168     std::string testStr8 = R"(["NativeToken":])";
169     receptor.ParserNativeRawData(testStr8, tokenInfos);
170     ASSERT_EQ(static_cast<uint32_t>(0), tokenInfos.size());
171 
172     std::string testStr9 = R"([)";
173     receptor.ParserNativeRawData(testStr9, tokenInfos);
174     ASSERT_EQ(static_cast<uint32_t>(0), tokenInfos.size());
175 }
176 
177 namespace OHOS {
178 namespace Security {
179 namespace AccessToken {
180     extern void from_json(const nlohmann::json& j, std::shared_ptr<NativeTokenInfoInner>& p);
181 }
182 }
183 }
184 
185 /**
186  * @tc.name: from_json001
187  * @tc.desc: Verify from json right case.
188  * @tc.type: FUNC
189  * @tc.require: Issue Number
190  */
191 HWTEST_F(NativeTokenReceptorTest, from_json001, TestSize.Level1)
192 {
193     ACCESSTOKEN_LOG_INFO(LABEL, "test from_json001!");
194     nlohmann::json j = nlohmann::json{
195         {"processName", "process6"},
196         {"APL", APL_SYSTEM_CORE},
197         {"version", 1},
198         {"tokenId", 685266937},
199         {"tokenAttr", 0},
200         {"dcaps", {"AT_CAP", "ST_CAP"}},
201         {"permissions", {"ohos.permission.PLACE_CALL"}},
202         {"nativeAcls", {"ohos.permission.PLACE_CALL"}}};
203     std::shared_ptr<NativeTokenInfoInner> p;
204     from_json(j, p);
205     ASSERT_NE((p == nullptr), true);
206 }
207 
208 /**
209  * @tc.name: from_json002
210  * @tc.desc: Verify from json wrong case.
211  * @tc.type: FUNC
212  * @tc.require: Issue Number
213  */
214 HWTEST_F(NativeTokenReceptorTest, from_json002, TestSize.Level1)
215 {
216     ACCESSTOKEN_LOG_INFO(LABEL, "test from_json002!");
217     // version wrong
218     nlohmann::json j = nlohmann::json{
219         {"processName", "process6"}, {"APL", APL_SYSTEM_CORE},
220         {"version", 2}, {"tokenId", 685266937},
221         {"tokenAttr", 0},
222         {"dcaps", {"AT_CAP", "ST_CAP"}}};
223     std::shared_ptr<NativeTokenInfoInner> p;
224     from_json(j, p);
225     ASSERT_EQ((p == nullptr), true);
226 
227     // APL wrong
228     j = nlohmann::json{
229         {"processName", "process6"},
230         {"APL", -1}, {"version", 1},
231         {"tokenId", 685266937}, {"tokenAttr", 0},
232         {"dcaps", {"AT_CAP", "ST_CAP"}}};
233     from_json(j, p);
234     ASSERT_EQ((p == nullptr), true);
235 
236     // tokenId wrong
237     j = nlohmann::json{
238         {"processName", "process6"},
239         {"APL", APL_SYSTEM_BASIC}, {"version", 1},
240         {"tokenId", 0}, {"tokenAttr", 0},
241         {"dcaps", {"AT_CAP", "ST_CAP"}}};
242     from_json(j, p);
243     ASSERT_EQ((p == nullptr), true);
244 
245     // process name empty
246     j = nlohmann::json{
247         {"processName", ""},
248         {"APL", APL_SYSTEM_BASIC}, {"version", 1},
249         {"tokenId", 685266937}, {"tokenAttr", 0},
250         {"dcaps", {"AT_CAP", "ST_CAP"}}};
251     from_json(j, p);
252     ASSERT_EQ((p == nullptr), true);
253 
254     // process name too long
255     std::string name(512, 'c');
256     j = nlohmann::json{
257         {"processName", name},
258         {"APL", APL_SYSTEM_BASIC}, {"version", 1},
259         {"tokenId", 685266937}, {"tokenAttr", 0},
260         {"dcaps", {"AT_CAP", "ST_CAP"}}};
261     from_json(j, p);
262     ASSERT_EQ((p == nullptr), true);
263 
264     // lose process name
265     j = nlohmann::json{
266         {"APL", APL_SYSTEM_BASIC},
267         {"version", 1}, {"tokenId", 685266937},
268         {"tokenAttr", 0}, {"dcaps", {"AT_CAP", "ST_CAP"}}};
269     from_json(j, p);
270     ASSERT_EQ((p == nullptr), true);
271 }
272 
273 /**
274  * @tc.name: ProcessNativeTokenInfos001
275  * @tc.desc: test add one native token
276  * @tc.type: FUNC
277  * @tc.require: Issue Number
278  */
279 HWTEST_F(NativeTokenReceptorTest, ProcessNativeTokenInfos001, TestSize.Level1)
280 {
281     ACCESSTOKEN_LOG_INFO(LABEL, "test ProcessNativeTokenInfos001!");
282     std::vector<std::shared_ptr<NativeTokenInfoInner>> tokenInfos;
283 
284     // test process one
285     NativeTokenInfo info = {
286         .apl = APL_NORMAL,
287         .ver = 1,
288         .processName = "ProcessNativeTokenInfos001",
289         .dcap =  {"AT_CAP", "ST_CAP"},
290         .tokenID = 0x28100000,
291         .tokenAttr = 0
292     };
293 
294     std::vector<PermissionStateFull> permStateList = {};
295     std::shared_ptr<NativeTokenInfoInner> nativeToken = std::make_shared<NativeTokenInfoInner>(info, permStateList);
296     tokenInfos.emplace_back(nativeToken);
297     AccessTokenInfoManager::GetInstance().ProcessNativeTokenInfos(tokenInfos);
298     NativeTokenInfo findInfo;
299     int ret = AccessTokenInfoManager::GetInstance().GetNativeTokenInfo(info.tokenID, findInfo);
300     ASSERT_EQ(ret, RET_SUCCESS);
301     ASSERT_EQ(findInfo.apl, info.apl);
302     ASSERT_EQ(findInfo.ver, info.ver);
303     ASSERT_EQ(findInfo.processName, info.processName);
304     ASSERT_EQ(findInfo.tokenID, info.tokenID);
305     ASSERT_EQ(findInfo.tokenAttr, info.tokenAttr);
306     ASSERT_EQ(findInfo.dcap, info.dcap);
307 
308     // wait fresh tokens to sql.
309     sleep(3);
310 
311     // get sql data
312     GenericValues conditionValue;
313     std::vector<GenericValues> nativeTokenResults;
314     AccessTokenDb::GetInstance().Find(AtmDataType::ACCESSTOKEN_NATIVE_INFO, conditionValue, nativeTokenResults);
315     std::vector<GenericValues> permStateRes;
316     AccessTokenDb::GetInstance().Find(AtmDataType::ACCESSTOKEN_PERMISSION_STATE, conditionValue, permStateRes);
317     for (GenericValues nativeTokenValue : nativeTokenResults) {
318         AccessTokenID tokenId = (AccessTokenID)nativeTokenValue.GetInt(TokenFiledConst::FIELD_TOKEN_ID);
319         if (tokenId != info.tokenID) {
320             continue;
321         }
322         GTEST_LOG_(INFO) <<"apl " << nativeTokenValue.GetInt(TokenFiledConst::FIELD_APL);
323         std::shared_ptr<NativeTokenInfoInner> native = std::make_shared<NativeTokenInfoInner>();
324         ASSERT_NE(native, nullptr);
325         ret = native->RestoreNativeTokenInfo(tokenId, nativeTokenValue, permStateRes);
326         ASSERT_EQ(ret, RET_SUCCESS);
327         ASSERT_EQ(native->GetTokenID(), info.tokenID);
328         ASSERT_EQ(native->GetProcessName(), info.processName);
329         ASSERT_EQ(native->GetDcap(), info.dcap);
330     }
331 
332     ret = AccessTokenInfoManager::GetInstance().RemoveNativeTokenInfo(info.tokenID);
333     ASSERT_EQ(ret, RET_SUCCESS);
334 }
335 
336 /**
337  * @tc.name: CheckNativeDCap001
338  * @tc.desc: Verify CheckNativeDCap normal and abnormal branch
339  * @tc.type: FUNC
340  * @tc.require: Issue Number
341  */
342 HWTEST_F(NativeTokenReceptorTest, CheckNativeDCap001, TestSize.Level1)
343 {
344     ACCESSTOKEN_LOG_INFO(LABEL, "test CheckNativeDCap001!");
345 
346     // test tokenInfo = nullptr
347     std::vector<std::shared_ptr<NativeTokenInfoInner>> tokenInfos;
348     tokenInfos.emplace_back(nullptr);
349     AccessTokenInfoManager::GetInstance().ProcessNativeTokenInfos(tokenInfos);
350 
351     // test process one
352     NativeTokenInfo info = {.apl = APL_NORMAL,
353         .ver = 1,
354         .processName = "CheckNativeDCap001",
355         .dcap = {"AT_CAP", "ST_CAP"},
356         .tokenID = 0x28100000,
357         .tokenAttr = 0};
358 
359     std::vector<PermissionStateFull> permStateList = {};
360     std::shared_ptr<NativeTokenInfoInner> nativeToken = std::make_shared<NativeTokenInfoInner>(info, permStateList);
361     tokenInfos.emplace_back(nativeToken);
362     AccessTokenInfoManager::GetInstance().ProcessNativeTokenInfos(tokenInfos);
363     NativeTokenInfo findInfo;
364     int ret = AccessTokenInfoManager::GetInstance().GetNativeTokenInfo(info.tokenID, findInfo);
365     ASSERT_EQ(ret, RET_SUCCESS);
366     ASSERT_EQ(findInfo.apl, info.apl);
367     ASSERT_EQ(findInfo.ver, info.ver);
368     ASSERT_EQ(findInfo.processName, info.processName);
369     ASSERT_EQ(findInfo.tokenID, info.tokenID);
370     ASSERT_EQ(findInfo.tokenAttr, info.tokenAttr);
371     ASSERT_EQ(findInfo.dcap, info.dcap);
372 
373     std::string dcap = "AT_CAP";
374     ASSERT_EQ(AccessTokenInfoManager::GetInstance().CheckNativeDCap(findInfo.tokenID, dcap), RET_SUCCESS);
375     std::string ndcap = "AT";
376     ASSERT_NE(AccessTokenInfoManager::GetInstance().CheckNativeDCap(findInfo.tokenID, ndcap), RET_SUCCESS);
377     AccessTokenID testId = 1;
378     ASSERT_NE(AccessTokenInfoManager::GetInstance().CheckNativeDCap(testId, dcap), RET_SUCCESS);
379     ret = AccessTokenInfoManager::GetInstance().RemoveNativeTokenInfo(info.tokenID);
380     ASSERT_EQ(ret, RET_SUCCESS);
381 }
382 
PermStateListSet(std::vector<PermissionStateFull> & permStateList)383 static void PermStateListSet(std::vector<PermissionStateFull> &permStateList)
384 {
385     PermissionStateFull infoManagerTestState1 = {
386         .permissionName = "ohos.permission.ACCELEROMETER",
387         .isGeneral = true,
388         .resDeviceID = {"local"},
389         .grantStatus = {0},
390         .grantFlags = {0}
391     };
392 
393     PermissionStateFull infoManagerTestState2 = {
394         .permissionName = "ohos.permission.MANAGE_USER_IDM",
395         .isGeneral = true,
396         .resDeviceID = {"device 1", "device 2"},
397         .grantStatus = {0, 0},
398         .grantFlags = {0, 2}
399     };
400 
401     PermissionStateFull infoManagerTestState3 = {
402         .permissionName = "ohos.permission.USER_TEAT",
403         .isGeneral = true,
404         .resDeviceID = {"device 1", "device 2"},
405         .grantStatus = {0, 0},
406         .grantFlags = {0, 2}
407     };
408     permStateList.emplace_back(infoManagerTestState1);
409     permStateList.emplace_back(infoManagerTestState2);
410     permStateList.emplace_back(infoManagerTestState3);
411 }
412 
CompareGoalTokenInfo(NativeTokenInfo & info)413 static void CompareGoalTokenInfo(NativeTokenInfo &info)
414 {
415     NativeTokenInfo findInfo;
416     int ret = AccessTokenInfoManager::GetInstance().GetNativeTokenInfo(info.tokenID, findInfo);
417     ASSERT_EQ(ret, RET_SUCCESS);
418     ASSERT_EQ(findInfo.apl, info.apl);
419     ASSERT_EQ(findInfo.ver, info.ver);
420     ASSERT_EQ(findInfo.processName, info.processName);
421     ASSERT_EQ(findInfo.tokenID, info.tokenID);
422     ASSERT_EQ(findInfo.tokenAttr, info.tokenAttr);
423     ASSERT_EQ(findInfo.dcap, info.dcap);
424 }
425 
426 /**
427  * @tc.name: ProcessNativeTokenInfos002
428  * @tc.desc: test add two native tokens.
429  * @tc.type: FUNC
430  * @tc.require: Issue Number
431  */
432 HWTEST_F(NativeTokenReceptorTest, ProcessNativeTokenInfos002, TestSize.Level1)
433 {
434     ACCESSTOKEN_LOG_INFO(LABEL, "test ProcessNativeTokenInfos002!");
435     std::vector<std::shared_ptr<NativeTokenInfoInner>> tokenInfos;
436     NativeTokenInfo info1;
437     info1.apl = APL_NORMAL;
438     info1.ver = 1;
439     info1.processName = "native_token_test1";
440     info1.dcap =  {"AT_CAP", "ST_CAP"};
441     info1.tokenID = 0x28100001;
442     info1.tokenAttr = 0;
443 
444     NativeTokenInfo info2;
445     info2.apl = APL_SYSTEM_BASIC;
446     info2.ver = 1;
447     info2.processName = "native_token_test2";
448     info2.dcap =  {"AT_CAP", "ST_CAP"};
449     info2.tokenID = 0x28100002;
450     info2.tokenAttr = 0;
451 
452     std::vector<PermissionStateFull> permStateList;
453     PermStateListSet(permStateList);
454     std::shared_ptr<NativeTokenInfoInner> nativeToken1 = std::make_shared<NativeTokenInfoInner>(info1, permStateList);
455 
456     std::shared_ptr<PermissionPolicySet> permPolicySet =
457         nativeToken1->GetNativeInfoPermissionPolicySet();
458     GTEST_LOG_(INFO) <<"permPolicySet: " << permPolicySet;
459 
460     std::vector<PermissionStateFull> permList;
461     permPolicySet->GetPermissionStateFulls(permList);
462     for (const auto& perm : permList) {
463         GTEST_LOG_(INFO) <<"perm.permissionName: " << perm.permissionName;
464     }
465 
466     tokenInfos.emplace_back(nativeToken1);
467 
468     std::shared_ptr<NativeTokenInfoInner> nativeToken2 = std::make_shared<NativeTokenInfoInner>(info2, permStateList);
469     tokenInfos.emplace_back(nativeToken2);
470 
471     AccessTokenInfoManager::GetInstance().ProcessNativeTokenInfos(tokenInfos);
472 
473     CompareGoalTokenInfo(info1);
474 
475     int ret = PermissionManager::GetInstance().VerifyAccessToken(info1.tokenID, "ohos.permission.MANAGE_USER_IDM");
476     ASSERT_EQ(ret, PERMISSION_GRANTED);
477     ret = PermissionManager::GetInstance().VerifyAccessToken(info1.tokenID, "ohos.permission.ACCELEROMETER");
478     ASSERT_EQ(ret, PERMISSION_GRANTED);
479     ret = PermissionManager::GetInstance().VerifyAccessToken(info1.tokenID, "ohos.permission.DISCOVER_BLUETOOTH");
480     ASSERT_EQ(ret, PERMISSION_DENIED);
481 
482     CompareGoalTokenInfo(info2);
483 
484     ret = AccessTokenInfoManager::GetInstance().RemoveNativeTokenInfo(info1.tokenID);
485     ASSERT_EQ(ret, RET_SUCCESS);
486 
487     ret = PermissionManager::GetInstance().VerifyAccessToken(info2.tokenID, "ohos.permission.MANAGE_USER_IDM");
488     ASSERT_EQ(ret, PERMISSION_GRANTED);
489     ret = PermissionManager::GetInstance().VerifyAccessToken(info2.tokenID, "ohos.permission.ACCELEROMETER");
490     ASSERT_EQ(ret, PERMISSION_GRANTED);
491 
492     ret = AccessTokenInfoManager::GetInstance().RemoveNativeTokenInfo(info2.tokenID);
493     ASSERT_EQ(ret, RET_SUCCESS);
494 }
495 
496 /**
497  * @tc.name: ProcessNativeTokenInfos003
498  * @tc.desc: test add nullptr tokenInfo.
499  * @tc.type: FUNC
500  * @tc.require: Issue Number
501  */
502 HWTEST_F(NativeTokenReceptorTest, ProcessNativeTokenInfos003, TestSize.Level1)
503 {
504     ACCESSTOKEN_LOG_INFO(LABEL, "test ProcessNativeTokenInfos003!");
505     std::vector<std::shared_ptr<NativeTokenInfoInner>> tokenInfos;
506 
507     std::shared_ptr<NativeTokenInfoInner> nativeToken1 = std::make_shared<NativeTokenInfoInner>();
508     tokenInfos.emplace_back(nativeToken1);
509     AccessTokenInfoManager::GetInstance().ProcessNativeTokenInfos(tokenInfos);
510     ASSERT_EQ(RET_SUCCESS, RET_SUCCESS);
511 }
512 
513 /**
514  * @tc.name: ProcessNativeTokenInfos004
515  * @tc.desc: test add repeat id, but process doesn't
516  * @tc.type: FUNC
517  * @tc.require: Issue Number
518  */
519 HWTEST_F(NativeTokenReceptorTest, ProcessNativeTokenInfos004, TestSize.Level1)
520 {
521     ACCESSTOKEN_LOG_INFO(LABEL, "test ProcessNativeTokenInfos004!");
522     std::vector<std::shared_ptr<NativeTokenInfoInner>> tokenInfos;
523 
524     NativeTokenInfo info3 = {
525         .apl = APL_NORMAL,
526         .ver = 1,
527         .processName = "native_token_test3",
528         .dcap =  {"AT_CAP", "ST_CAP"},
529         .tokenID = 0x28100003,
530         .tokenAttr = 0
531     };
532 
533     NativeTokenInfo info4 = {
534         .apl = APL_NORMAL,
535         .ver = 1,
536         .processName = "native_token_test4",
537         .dcap =  {"AT_CAP", "ST_CAP"},
538         .tokenID = 0x28100003,
539         .tokenAttr = 0
540     };
541     std::vector<PermissionStateFull> permStateList = {};
542     std::shared_ptr<NativeTokenInfoInner> nativeToken3 = std::make_shared<NativeTokenInfoInner>(info3, permStateList);
543     tokenInfos.emplace_back(nativeToken3);
544 
545     std::shared_ptr<NativeTokenInfoInner> nativeToken4 = std::make_shared<NativeTokenInfoInner>(info4, permStateList);
546     tokenInfos.emplace_back(nativeToken4);
547 
548     AccessTokenInfoManager::GetInstance().ProcessNativeTokenInfos(tokenInfos);
549 
550     NativeTokenInfo findInfo;
551     int ret = AccessTokenInfoManager::GetInstance().GetNativeTokenInfo(info3.tokenID, findInfo);
552     ASSERT_EQ(ret, RET_SUCCESS);
553     ASSERT_EQ(findInfo.apl, info3.apl);
554     ASSERT_EQ(findInfo.ver, info3.ver);
555     ASSERT_EQ(findInfo.processName, info3.processName);
556     ASSERT_EQ(findInfo.tokenID, info3.tokenID);
557     ASSERT_EQ(findInfo.tokenAttr, info3.tokenAttr);
558     ASSERT_EQ(findInfo.dcap, info3.dcap);
559 
560     ret = AccessTokenInfoManager::GetInstance().RemoveNativeTokenInfo(info3.tokenID);
561     ASSERT_EQ(ret, RET_SUCCESS);
562 }
563 
564 /**
565  * @tc.name: ProcessNativeTokenInfos005
566  * @tc.desc: test add repeat process, but id doesn't
567  * @tc.type: FUNC
568  * @tc.require: Issue Number
569  */
570 HWTEST_F(NativeTokenReceptorTest, ProcessNativeTokenInfos005, TestSize.Level1)
571 {
572     ACCESSTOKEN_LOG_INFO(LABEL, "test ProcessNativeTokenInfos005!");
573     std::vector<std::shared_ptr<NativeTokenInfoInner>> tokenInfos;
574 
575     NativeTokenInfo info5 = {
576         .apl = APL_NORMAL,
577         .ver = 1,
578         .processName = "native_token_test5",
579         .dcap =  {"AT_CAP", "ST_CAP"},
580         .tokenID = 0x28100005,
581         .tokenAttr = 0
582     };
583 
584     NativeTokenInfo info6 = {
585         .apl = APL_NORMAL,
586         .ver = 1,
587         .processName = "native_token_test5",
588         .dcap =  {"AT_CAP", "ST_CAP"},
589         .tokenID = 0x28100006,
590         .tokenAttr = 0
591     };
592     std::vector<PermissionStateFull> permStateList = {};
593     std::shared_ptr<NativeTokenInfoInner> nativeToken5 = std::make_shared<NativeTokenInfoInner>(info5, permStateList);
594     tokenInfos.emplace_back(nativeToken5);
595 
596     std::shared_ptr<NativeTokenInfoInner> nativeToken6 = std::make_shared<NativeTokenInfoInner>(info6, permStateList);
597     tokenInfos.emplace_back(nativeToken6);
598 
599     AccessTokenInfoManager::GetInstance().ProcessNativeTokenInfos(tokenInfos);
600 
601     NativeTokenInfo findInfo;
602     int ret = AccessTokenInfoManager::GetInstance().GetNativeTokenInfo(info5.tokenID, findInfo);
603     ASSERT_EQ(ret, ERR_TOKENID_NOT_EXIST);
604 
605     ret = AccessTokenInfoManager::GetInstance().GetNativeTokenInfo(info6.tokenID, findInfo);
606     ASSERT_EQ(ret, RET_SUCCESS);
607     ASSERT_EQ(findInfo.apl, info6.apl);
608     ASSERT_EQ(findInfo.ver, info6.ver);
609     ASSERT_EQ(findInfo.processName, info6.processName);
610     ASSERT_EQ(findInfo.tokenID, info6.tokenID);
611     ASSERT_EQ(findInfo.tokenAttr, info6.tokenAttr);
612     ASSERT_EQ(findInfo.dcap, info6.dcap);
613 
614     ret = AccessTokenInfoManager::GetInstance().RemoveNativeTokenInfo(info6.tokenID);
615     ASSERT_EQ(ret, RET_SUCCESS);
616 }
617 
618 /**
619  * @tc.name: ProcessNativeTokenInfos006
620  * @tc.desc: test add repeat process and id
621  * @tc.type: FUNC
622  * @tc.require: Issue Number
623  */
624 HWTEST_F(NativeTokenReceptorTest, ProcessNativeTokenInfos006, TestSize.Level1)
625 {
626     ACCESSTOKEN_LOG_INFO(LABEL, "test ProcessNativeTokenInfos006!");
627     std::vector<std::shared_ptr<NativeTokenInfoInner>> tokenInfos;
628 
629     NativeTokenInfo info7 = {
630         .apl = APL_NORMAL,
631         .ver = 1,
632         .processName = "native_token_test7",
633         .dcap =  {"AT_CAP", "ST_CAP"},
634         .tokenID = 0x28100007,
635         .tokenAttr = 0
636     };
637 
638     NativeTokenInfo info8 = {
639         .apl = APL_SYSTEM_BASIC,
640         .ver = 1,
641         .processName = "native_token_test7",
642         .dcap =  {"AT_CAP"},
643         .tokenID = 0x28100007,
644         .tokenAttr = 0
645     };
646     std::vector<PermissionStateFull> permStateList = {};
647     std::shared_ptr<NativeTokenInfoInner> nativeToken7 = std::make_shared<NativeTokenInfoInner>(info7, permStateList);
648     tokenInfos.emplace_back(nativeToken7);
649 
650     std::shared_ptr<NativeTokenInfoInner> nativeToken8 = std::make_shared<NativeTokenInfoInner>(info8, permStateList);
651     tokenInfos.emplace_back(nativeToken8);
652 
653     AccessTokenInfoManager::GetInstance().ProcessNativeTokenInfos(tokenInfos);
654 
655     NativeTokenInfo findInfo;
656     int ret = AccessTokenInfoManager::GetInstance().GetNativeTokenInfo(info7.tokenID, findInfo);
657     ASSERT_EQ(ret, RET_SUCCESS);
658     ASSERT_EQ(findInfo.apl, info8.apl);
659     ASSERT_EQ(findInfo.ver, info8.ver);
660     ASSERT_EQ(findInfo.processName, info8.processName);
661     ASSERT_EQ(findInfo.tokenID, info8.tokenID);
662     ASSERT_EQ(findInfo.tokenAttr, info8.tokenAttr);
663     ASSERT_EQ(findInfo.dcap, info8.dcap);
664 
665     ret = AccessTokenInfoManager::GetInstance().RemoveNativeTokenInfo(info8.tokenID);
666     ASSERT_EQ(ret, RET_SUCCESS);
667 }
668 
669 /**
670  * @tc.name: init001
671  * @tc.desc: test get native cfg
672  * @tc.type: FUNC
673  * @tc.require: Issue Number
674  */
675 HWTEST_F(NativeTokenReceptorTest, init001, TestSize.Level1)
676 {
677     ACCESSTOKEN_LOG_INFO(LABEL, "test init001!");
678 
679     const char *dcaps[1];
680     dcaps[0] = "AT_CAP_01";
681     int dcapNum = 1;
682     const char *perms[2];
683     perms[0] = "ohos.permission.test1";
684     perms[1] = "ohos.permission.test2";
685     NativeTokenInfoParams infoInstance = {
686         .dcapsNum = dcapNum,
687         .permsNum = 2,
688         .aclsNum = 0,
689         .dcaps = dcaps,
690         .perms = perms,
691         .acls = nullptr,
692         .processName = "native_token_test7",
693         .aplStr = "system_core",
694     };
695     uint64_t tokenId = ::GetAccessTokenId(&infoInstance);
696     ASSERT_NE(tokenId, INVALID_TOKENID);
697 
698     NativeTokenReceptor::GetInstance().Init();
699     NativeTokenInfo findInfo;
700     int ret = AccessTokenInfoManager::GetInstance().GetNativeTokenInfo(tokenId, findInfo);
701     ASSERT_EQ(ret, RET_SUCCESS);
702     ASSERT_EQ(findInfo.processName, infoInstance.processName);
703 
704     ret = AccessTokenInfoManager::GetInstance().RemoveNativeTokenInfo(tokenId);
705     ASSERT_EQ(ret, RET_SUCCESS);
706 }