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 #ifndef INPUTMETHOD_IMF_TEST_UNITTEST_COMMON_SCOPE_UTILS_H 17 #define INPUTMETHOD_IMF_TEST_UNITTEST_COMMON_SCOPE_UTILS_H 18 19 #include "global.h" 20 #include "tdd_util.h" 21 namespace OHOS { 22 namespace MiscServices { 23 constexpr int32_t ROOT_UID = 0; 24 class AccessScope { 25 public: AccessScope(uint64_t tokenId,int32_t uid)26 AccessScope(uint64_t tokenId, int32_t uid) 27 { 28 IMSA_HILOGI("enter"); 29 if (tokenId > 0) { 30 originalTokenId_ = TddUtil::GetCurrentTokenID(); 31 TddUtil::SetTestTokenID(tokenId); 32 } 33 if (uid > 0) { 34 TddUtil::SetSelfUid(uid); 35 } 36 } ~AccessScope()37 ~AccessScope() 38 { 39 if (originalTokenId_ > 0) { 40 TddUtil::SetTestTokenID(originalTokenId_); 41 } 42 TddUtil::SetSelfUid(ROOT_UID); 43 IMSA_HILOGI("exit"); 44 } 45 46 private: 47 uint64_t originalTokenId_{ 0 }; 48 }; 49 50 class TokenScope { 51 public: TokenScope(uint64_t tokenId)52 explicit TokenScope(uint64_t tokenId) 53 { 54 IMSA_HILOGI("enter"); 55 originalTokenId_ = TddUtil::GetCurrentTokenID(); 56 TddUtil::SetTestTokenID(tokenId); 57 } ~TokenScope()58 ~TokenScope() 59 { 60 TddUtil::SetTestTokenID(originalTokenId_); 61 IMSA_HILOGI("exit"); 62 } 63 64 private: 65 uint64_t originalTokenId_{ 0 }; 66 }; 67 68 class UidScope { 69 public: UidScope(int32_t uid)70 explicit UidScope(int32_t uid) 71 { 72 IMSA_HILOGI("enter, uid: %{public}d", uid); 73 TddUtil::SetSelfUid(uid); 74 } ~UidScope()75 ~UidScope() 76 { 77 TddUtil::SetSelfUid(ROOT_UID); 78 IMSA_HILOGI("exit"); 79 } 80 }; 81 } // namespace MiscServices 82 } // namespace OHOS 83 #endif // INPUTMETHOD_IMF_TEST_UNITTEST_COMMON_SCOPE_UTILS_H 84