1 /* 2 * Copyright (c) 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 #ifndef ACCESSTOKEN_KIT_H 17 #define ACCESSTOKEN_KIT_H 18 19 #include <string> 20 21 #include "gmock/gmock.h" 22 #include "parcel.h" 23 24 namespace OHOS::Security::AccessToken { 25 typedef unsigned int AccessTokenID; 26 27 typedef enum TypeATokenTypeEnum { 28 TOKEN_INVALID = -1, 29 TOKEN_HAP = 0, 30 TOKEN_NATIVE, 31 TOKEN_SHELL, 32 TOKEN_TYPE_BUTT, 33 } ATokenTypeEnum; 34 35 typedef enum TypePermissionState { 36 PERMISSION_DENIED = -1, 37 PERMISSION_GRANTED = 0, 38 } PermissionState; 39 40 struct NativeTokenInfoParcel final : public Parcelable { 41 NativeTokenInfoParcel() = default; 42 43 ~NativeTokenInfoParcel() override = default; 44 Marshallingfinal45 bool Marshalling(Parcel &out) const override { return true; }; 46 Unmarshallingfinal47 static NativeTokenInfoParcel *Unmarshalling(Parcel &in) { return {}; }; 48 }; 49 50 struct HapTokenInfoParcel final : public Parcelable { 51 HapTokenInfoParcel() = default; 52 53 ~HapTokenInfoParcel() override = default; 54 Marshallingfinal55 bool Marshalling(Parcel &out) const override { return true; }; 56 Unmarshallingfinal57 static HapTokenInfoParcel *Unmarshalling(Parcel &in) { return {}; }; 58 }; 59 60 class HapTokenInfo final { 61 public: 62 std::string bundleName; 63 }; 64 65 class NativeTokenInfo final { 66 public: 67 std::string processName; 68 }; 69 70 class TokenIdKitInterface { 71 public: 72 virtual ~TokenIdKitInterface() = default; 73 virtual bool IsSystemAppByFullTokenID(uint64_t tokenId) = 0; 74 }; 75 76 class MockTokenIdKitInterface : public TokenIdKitInterface { 77 public: 78 MockTokenIdKitInterface() = default; 79 ~MockTokenIdKitInterface() override = default; 80 MOCK_METHOD1(IsSystemAppByFullTokenID, bool(uint64_t tokenId)); 81 }; 82 83 class TokenIdKit { 84 public: IsSystemAppByFullTokenID(uint64_t tokenId)85 static bool IsSystemAppByFullTokenID(uint64_t tokenId) 86 { 87 if (instance_ == nullptr) { 88 return false; 89 } 90 return instance_->IsSystemAppByFullTokenID(tokenId); 91 } 92 GetInterface()93 static std::shared_ptr<MockTokenIdKitInterface> GetInterface() 94 { 95 if (instance_ == nullptr) { 96 std::lock_guard<std::mutex> lock(mutex_); 97 if (instance_ == nullptr) { 98 instance_ = std::make_shared<MockTokenIdKitInterface>(); 99 } 100 } 101 return instance_; 102 }; 103 DelInterface()104 static void DelInterface() 105 { 106 if (instance_ != nullptr) { 107 instance_.reset(); 108 } 109 }; 110 111 private: 112 static std::shared_ptr<MockTokenIdKitInterface> instance_; 113 static std::mutex mutex_; 114 }; 115 116 class AccessTokenKitInterface { 117 public: 118 virtual ~AccessTokenKitInterface() = default; 119 virtual int32_t VerifyAccessToken(AccessToken::AccessTokenID callerToken, const std::string &permission) = 0; 120 virtual ATokenTypeEnum GetTokenType(AccessTokenID tokenID) = 0; 121 virtual int GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo& hapTokenInfoRes) = 0; 122 virtual int GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfo& nativeTokenInfoRes) = 0; 123 }; 124 125 class MockAccessTokenKitInterface : public AccessTokenKitInterface { 126 public: 127 MockAccessTokenKitInterface() = default; 128 ~MockAccessTokenKitInterface() override = default; 129 MOCK_METHOD2(VerifyAccessToken, int32_t(AccessToken::AccessTokenID callerToken, const std::string &permission)); 130 MOCK_METHOD1(GetTokenType, ATokenTypeEnum(AccessTokenID tokenID)); 131 MOCK_METHOD2(GetHapTokenInfo, int(AccessTokenID tokenID, HapTokenInfo& hapTokenInfoRes)); 132 MOCK_METHOD2(GetNativeTokenInfo, int(AccessTokenID tokenID, NativeTokenInfo& nativeTokenInfoRes)); 133 }; 134 135 class AccessTokenKit { 136 public: VerifyAccessToken(AccessToken::AccessTokenID callerToken,const std::string & permission)137 static int32_t VerifyAccessToken(AccessToken::AccessTokenID callerToken, const std::string &permission) 138 { 139 if (instance_ == nullptr) { 140 return -1; 141 } 142 return instance_->VerifyAccessToken(callerToken, permission); 143 } 144 GetTokenType(AccessTokenID tokenID)145 static ATokenTypeEnum GetTokenType(AccessTokenID tokenID) 146 { 147 if (instance_ == nullptr) { 148 return TOKEN_INVALID; 149 } 150 return instance_->GetTokenType(tokenID); 151 } 152 GetHapTokenInfo(AccessTokenID tokenID,HapTokenInfo & hapTokenInfoRes)153 static int GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo& hapTokenInfoRes) 154 { 155 if (instance_ == nullptr) { 156 return -1; 157 } 158 return instance_->GetHapTokenInfo(tokenID, hapTokenInfoRes); 159 } 160 GetNativeTokenInfo(AccessTokenID tokenID,NativeTokenInfo & nativeTokenInfoRes)161 static int GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfo& nativeTokenInfoRes) 162 { 163 if (instance_ == nullptr) { 164 return -1; 165 } 166 return instance_->GetNativeTokenInfo(tokenID, nativeTokenInfoRes); 167 } 168 GetInterface()169 static std::shared_ptr<MockAccessTokenKitInterface> GetInterface() 170 { 171 if (instance_ == nullptr) { 172 std::lock_guard<std::mutex> lock(mutex_); 173 if (instance_ == nullptr) { 174 instance_ = std::make_shared<MockAccessTokenKitInterface>(); 175 } 176 } 177 return instance_; 178 }; 179 DelInterface()180 static void DelInterface() 181 { 182 if (instance_ != nullptr) { 183 instance_.reset(); 184 } 185 }; 186 187 private: 188 static std::shared_ptr<MockAccessTokenKitInterface> instance_; 189 static std::mutex mutex_; 190 }; 191 } // OHOS::Security::AccessToken 192 193 #endif // ACCESSTOKEN_KIT_H