1 /* 2 * Copyright (c) 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 "multi_thread_test.h" 17 18 #include "access_token.h" 19 #include "access_token_error.h" 20 #include "accesstoken_common_log.h" 21 #define private public 22 #include "accesstoken_id_manager.h" 23 #undef private 24 #include "permission_validator.h" 25 #include "string_ex.h" 26 #include "token_setproc.h" 27 28 29 using namespace testing::ext; 30 using namespace testing::mt; 31 namespace OHOS { 32 namespace Security { 33 namespace AccessToken { 34 namespace { 35 bool g_register = false; 36 static std::set<AccessTokenID> g_tokenIdSet; 37 static constexpr int32_t TEST_TOKEN_ID_1 = 537800000; 38 static constexpr int32_t TEST_TOKEN_ID_2 = 537900000; 39 static constexpr int32_t MULTI_CYCLE_TIMES = 1000; 40 } 41 SetUpTestCase()42void AccessTokenMultiThreadTest::SetUpTestCase() 43 { 44 } 45 TearDownTestCase()46void AccessTokenMultiThreadTest::TearDownTestCase() 47 { 48 } 49 SetUp()50void AccessTokenMultiThreadTest::SetUp() 51 { 52 LOGI(ATM_DOMAIN, ATM_TAG, "SetUp ok."); 53 g_tokenIdSet = AccessTokenIDManager::GetInstance().tokenIdSet_; 54 AccessTokenIDManager::GetInstance().tokenIdSet_.clear(); 55 } 56 TearDown()57void AccessTokenMultiThreadTest::TearDown() 58 { 59 AccessTokenIDManager::GetInstance().tokenIdSet_ = g_tokenIdSet; // recovery 60 } 61 TestRegisterTokenId()62void TestRegisterTokenId() 63 { 64 AccessTokenID tokenId = TEST_TOKEN_ID_2; 65 int32_t i = MULTI_CYCLE_TIMES + 1; 66 while (i--) { 67 tokenId += i; 68 AccessTokenIDManager::GetInstance().RegisterTokenId(tokenId, TOKEN_HAP); 69 } 70 } 71 TestReleaseTokenId()72void TestReleaseTokenId() 73 { 74 AccessTokenID releaseId = TEST_TOKEN_ID_2; 75 AccessTokenID tokenId = TEST_TOKEN_ID_1; 76 g_register = !g_register; 77 int32_t i = MULTI_CYCLE_TIMES + 1; 78 if (!g_register) { 79 while (i--) { 80 releaseId += i; 81 AccessTokenIDManager::GetInstance().ReleaseTokenId(tokenId); 82 } 83 } else { 84 while (i--) { 85 tokenId += i; 86 AccessTokenIDManager::GetInstance().RegisterTokenId(tokenId, TOKEN_HAP); 87 } 88 } 89 } 90 } // namespace AccessToken 91 } // namespace Security 92 } // namespace OHOS 93