1 /* 2 * Copyright (c) 2024-2025 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 AUTH_SESSION_JOSN_DEPS_MOCK_H 17 #define AUTH_SESSION_JOSN_DEPS_MOCK_H 18 19 #include <gmock/gmock.h> 20 #include <mutex> 21 22 #include "auth_common.h" 23 #include "auth_connection.h" 24 #include "auth_hichain_adapter.h" 25 #include "auth_interface.h" 26 #include "auth_manager.h" 27 #include "auth_meta_manager.h" 28 #include "auth_session_json.h" 29 #include "auth_session_key.h" 30 #include "auth_pre_link.h" 31 #include "auth_device_common_key_struct.h" 32 #include "bus_center_manager.h" 33 #include "lnn_common_utils.h" 34 #include "lnn_device_info.h" 35 #include "lnn_cipherkey_manager_struct.h" 36 #include "lnn_extdata_config.h" 37 #include "lnn_feature_capability.h" 38 #include "lnn_local_net_ledger.h" 39 #include "lnn_node_info.h" 40 #include "lnn_p2p_info.h" 41 #include "lnn_settingdata_event_monitor.h" 42 #include "softbus_adapter_bt_common.h" 43 #include "softbus_adapter_crypto.h" 44 #include "softbus_adapter_json.h" 45 #include "softbus_adapter_socket.h" 46 #include "softbus_adapter_timer.h" 47 #include "softbus_feature_config.h" 48 #include "softbus_utils.h" 49 50 namespace OHOS { 51 class AuthSessionJsonDepsInterface { 52 public: AuthSessionJsonDepsInterface()53 AuthSessionJsonDepsInterface() {}; ~AuthSessionJsonDepsInterface()54 virtual ~AuthSessionJsonDepsInterface() {}; 55 virtual bool JSON_GetStringFromObject(const JsonObj *obj, const char *key, char *value, uint32_t size) = 0; 56 virtual bool JSON_GetInt32FromOject(const JsonObj *obj, const char *key, int32_t *value) = 0; 57 virtual bool JSON_GetInt64FromOject(const JsonObj *obj, const char *key, int64_t *value) = 0; 58 virtual bool JSON_GetBoolFromOject(const JsonObj *obj, const char *key, bool *value) = 0; 59 virtual int32_t LnnEncryptAesGcm(AesGcmInputParam *in, int32_t keyIndex, 60 uint8_t **out, uint32_t *outLen) = 0; 61 virtual int32_t LnnDecryptAesGcm(AesGcmInputParam *in, uint8_t **out, uint32_t *outLen) = 0; 62 virtual bool JSON_AddStringToObject(JsonObj *obj, const char *key, const char *value) = 0; 63 virtual int32_t SoftBusGenerateStrHash(const unsigned char *str, uint32_t len, unsigned char *hash) = 0; 64 virtual int32_t ConvertBytesToHexString(char *outBuf, uint32_t outBufLen, 65 const unsigned char *inBuf, uint32_t inLen) = 0; 66 virtual int32_t LnnGetUdidByBrMac(const char *brMac, char *udid, uint32_t udidLen) = 0; 67 virtual int32_t AuthFindLatestNormalizeKey(const char *udidHash, 68 AuthDeviceKeyInfo *deviceKey, bool clearOldKey) = 0; 69 virtual int32_t AuthFindDeviceKey(const char *udidHash, int32_t keyType, AuthDeviceKeyInfo *deviceKey) = 0; 70 virtual void AuthGetLatestIdByUuid(const char *uuid, 71 AuthLinkType type, bool isMeta, AuthHandle *authHandle) = 0; 72 virtual AuthManager *GetAuthManagerByAuthId(int64_t authId) = 0; 73 virtual int32_t GetLatestSessionKey(const SessionKeyList *list, 74 AuthLinkType type, int32_t *index, SessionKey *key) = 0; 75 virtual void DelDupAuthManager(AuthManager *auth) = 0; 76 virtual bool IsPotentialTrustedDevice(TrustedRelationIdType idType, 77 const char *deviceId, bool isPrecise, bool isPointToPoint) = 0; 78 virtual bool IsPotentialTrustedDeviceDp(const char *deviceIdHash, bool isPointToPoint) = 0; 79 virtual int32_t ConvertBytesToUpperCaseHexString(char *outBuf, uint32_t outBufLen, 80 const unsigned char *inBuf, uint32_t inLen) = 0; 81 virtual int32_t ConvertHexStringToBytes(unsigned char *outBuf, uint32_t outBufLen, 82 const char *inBuf, uint32_t inLen) = 0; 83 virtual int32_t AuthFindNormalizeKeyByServerSide(const char *udidHash, bool isServer, 84 AuthDeviceKeyInfo *deviceKey) = 0; 85 virtual void AuthUpdateCreateTime(const char *udidHash, int32_t keyType, bool isServer) = 0; 86 virtual bool IsFeatureSupport(uint64_t feature, FeatureCapability capaBit) = 0; 87 virtual int32_t LnnGetLocalStrInfo(InfoKey key, char *info, uint32_t len) = 0; 88 virtual int32_t LnnGetLocalStrInfoByIfnameIdx(InfoKey key, char *info, uint32_t len, int32_t ifIdx) = 0; 89 virtual bool IsSupportUDIDAbatement(void) = 0; 90 virtual bool JSON_AddBoolToObject(JsonObj *obj, const char *key, bool value) = 0; 91 virtual bool IsNeedUDIDAbatement(const AuthSessionInfo *info) = 0; 92 virtual bool JSON_AddInt32ToObject(JsonObj *obj, const char *key, int32_t value) = 0; 93 virtual bool IsSupportFeatureByCapaBit(uint32_t feature, AuthCapability capaBit) = 0; 94 virtual JsonObj *JSON_CreateObject() = 0; 95 virtual void JSON_Delete(JsonObj *obj) = 0; 96 virtual JsonObj *JSON_Parse(const char *str, uint32_t len) = 0; 97 virtual const NodeInfo *LnnGetLocalNodeInfo(void) = 0; 98 virtual char *JSON_PrintUnformatted(const JsonObj *obj) = 0; 99 virtual int32_t GetFd(uint64_t connId) = 0; 100 virtual int32_t SoftBusSocketGetPeerName(int32_t socketFd, SoftBusSockAddr *addr) = 0; 101 virtual const char *SoftBusInetNtoP(int32_t af, const void *src, char *dst, int32_t size) = 0; 102 virtual int32_t GetPeerUdidByNetworkId(const char *networkId, char *udid, uint32_t len) = 0; 103 virtual int32_t GetIsExchangeUdidByNetworkId(const char *networkId, bool *isExchangeUdid) = 0; 104 virtual int32_t LnnGetUnifiedDeviceName(char *unifiedName, uint32_t len) = 0; 105 virtual int32_t LnnSetLocalStrInfo(InfoKey key, const char *info) = 0; 106 virtual int32_t GetExtData(char *value, uint32_t len) = 0; 107 virtual int32_t AuthMetaGetConnIdByInfo(const AuthConnInfo *connInfo, uint32_t *connectionId) = 0; 108 virtual int32_t LnnGetMetaPtk(uint32_t connId, char *metaPtk, uint32_t len) = 0; 109 virtual int32_t LnnGetLocalPtkByUuid(const char *uuid, char *localPtk, uint32_t len) = 0; 110 virtual void LnnDumpRemotePtk(const char *oldPtk, const char *newPtk, const char *log) = 0; 111 virtual int32_t SoftBusBase64Encode(unsigned char *dst, size_t dlen, size_t *olen, 112 const unsigned char *src, size_t slen) = 0; 113 virtual int32_t SoftBusBase64Decode(unsigned char *dst, size_t dlen, size_t *olen, 114 const unsigned char *src, size_t slen) = 0; 115 virtual int32_t LnnUpdateLocalBroadcastCipherKey(BroadcastCipherKey *broadcastKey) = 0; 116 virtual void ProcessCipherKeySyncInfo(const void *json, const char *networkId) = 0; 117 virtual bool LnnHasDiscoveryType(const NodeInfo *info, DiscoveryType type) = 0; 118 virtual int32_t LnnGetNetworkIdByUuid(const char *uuid, char *buf, uint32_t len) = 0; 119 virtual int32_t LnnGetRemoteNumInfo(const char *netWorkId, InfoKey key, int32_t *info) = 0; 120 virtual int32_t SoftbusGetConfig(ConfigType type, unsigned char *val, uint32_t len); 121 virtual int32_t GenerateCertificate(SoftbusCertChain *softbusCertChain, const AuthSessionInfo *info) = 0; 122 virtual void FreeSoftbusChain(SoftbusCertChain *softbusCertChain) = 0; 123 virtual int32_t InitSoftbusChain(SoftbusCertChain *softbusCertChain) = 0; 124 virtual int32_t VerifyCertificate(SoftbusCertChain *softbusCertChain, 125 const NodeInfo *nodeInfo, const AuthSessionInfo *info) = 0; 126 virtual int32_t LnnGetLocalNodeInfoSafe(NodeInfo *info) = 0; 127 virtual const char *LnnGetBtMac(const NodeInfo *info) = 0; 128 virtual int32_t SoftBusGetBtState(void) = 0; 129 virtual int32_t SoftBusGetBtMacAddr(SoftBusBtAddr *mac) = 0; 130 virtual int32_t ConvertBtMacToStr(char *strMac, uint32_t strMacLen, 131 const uint8_t *binMac, uint32_t binMacLen) = 0; 132 virtual int32_t LnnGetDeviceDisplayName(const char *nickName, 133 const char *defaultName, char *deviceName, uint32_t len) = 0; 134 virtual uint64_t SoftBusGetSysTimeMs(void) = 0; 135 virtual uint64_t LnnGetSupportedProtocols(const NodeInfo *info) = 0; 136 virtual int32_t StringToUpperCase(const char *str, char *buf, int32_t size) = 0; 137 virtual int32_t LnnGetAuthPort(const NodeInfo *info, int32_t ifnameIdx) = 0; 138 virtual int32_t LnnGetSessionPort(const NodeInfo *info, int32_t ifnameIdx) = 0; 139 virtual int32_t LnnGetProxyPort(const NodeInfo *info, int32_t ifnameIdx) = 0; 140 virtual bool JSON_AddBytesToObject(JsonObj *obj, const char *key, uint8_t *value, uint32_t size) = 0; 141 virtual bool JSON_GetBytesFromObject(const JsonObj *obj, const char *key, uint8_t *value, 142 uint32_t bufLen, uint32_t *size) = 0; 143 virtual bool JSON_AddInt16ToObject(JsonObj *obj, const char *key, int16_t value) = 0; 144 virtual bool JSON_AddInt64ToObject(JsonObj *obj, const char *key, int64_t value) = 0; 145 virtual const char *LnnGetP2pMac(const NodeInfo *info) = 0; 146 virtual const char *LnnGetDeviceName(const DeviceBasicInfo *info) = 0; 147 virtual char *LnnConvertIdToDeviceType(uint16_t typeId) = 0; 148 virtual const char *LnnGetDeviceUdid(const NodeInfo *info) = 0; 149 virtual bool PackCipherKeySyncMsg(void *json) = 0; 150 virtual int32_t LnnGetP2pRole(const NodeInfo *info) = 0; 151 virtual int32_t LnnGetStaFrequency(const NodeInfo *info) = 0; 152 virtual int32_t FindAuthPreLinkNodeById(uint32_t requestId, AuthPreLinkNode *reuseNode) = 0; 153 virtual bool GetSessionKeyProfile(int32_t sessionKeyId, uint8_t *sessionKey, uint32_t *length) = 0; 154 virtual void DelSessionKeyProfile(int32_t sessionKeyId) = 0; 155 virtual int32_t LnnGetLocalByteInfo(InfoKey key, uint8_t *info, uint32_t len) = 0; 156 virtual bool AuthSessionGetIsSameAccount(int64_t authSeq) = 0; 157 virtual int32_t AuthSessionGetUserId(int64_t authSeq) = 0; 158 virtual int32_t GetActiveOsAccountIds(void) = 0; 159 virtual bool IsTrustedDeviceFromAccess(const char *peerAccountHash, const char *peerUdid, int32_t peerUserId) = 0; 160 }; 161 class AuthSessionJsonDepsInterfaceMock : public AuthSessionJsonDepsInterface { 162 public: 163 AuthSessionJsonDepsInterfaceMock(); 164 ~AuthSessionJsonDepsInterfaceMock() override; 165 MOCK_METHOD4(JSON_GetStringFromObject, bool (const JsonObj *, const char *, char *, uint32_t)); 166 MOCK_METHOD3(JSON_GetInt32FromOject, bool (const JsonObj *, const char *, int32_t *)); 167 MOCK_METHOD3(JSON_GetInt64FromOject, bool (const JsonObj *, const char *, int64_t *)); 168 MOCK_METHOD3(JSON_GetBoolFromOject, bool (const JsonObj *, const char *, bool *)); 169 MOCK_METHOD4(LnnEncryptAesGcm, int32_t (AesGcmInputParam *, int32_t, uint8_t **, uint32_t *)); 170 MOCK_METHOD3(JSON_AddStringToObject, bool (JsonObj *, const char *, const char *)); 171 MOCK_METHOD3(SoftBusGenerateStrHash, int32_t (const unsigned char *, uint32_t, unsigned char *)); 172 MOCK_METHOD4(ConvertBytesToHexString, int32_t (char *, uint32_t, const unsigned char *, uint32_t)); 173 MOCK_METHOD3(LnnGetUdidByBrMac, int32_t (const char *, char *, uint32_t)); 174 MOCK_METHOD3(AuthFindLatestNormalizeKey, int32_t (const char *, AuthDeviceKeyInfo *, bool)); 175 MOCK_METHOD3(AuthFindDeviceKey, int32_t (const char *, int32_t, AuthDeviceKeyInfo *)); 176 MOCK_METHOD4(AuthGetLatestIdByUuid, void (const char *, AuthLinkType, bool, AuthHandle *)); 177 MOCK_METHOD1(GetAuthManagerByAuthId, AuthManager *(int64_t authId)); 178 MOCK_METHOD4(GetLatestSessionKey, int32_t (const SessionKeyList *, AuthLinkType, int32_t *, SessionKey *)); 179 MOCK_METHOD1(DelDupAuthManager, void (AuthManager *)); 180 MOCK_METHOD4(IsPotentialTrustedDevice, bool (TrustedRelationIdType, const char *, bool, bool)); 181 MOCK_METHOD2(IsPotentialTrustedDeviceDp, bool (const char *, bool)); 182 MOCK_METHOD4(ConvertBytesToUpperCaseHexString, int32_t (char *, uint32_t, const unsigned char *, uint32_t)); 183 MOCK_METHOD4(ConvertHexStringToBytes, int32_t (unsigned char *, uint32_t, const char *, uint32_t)); 184 MOCK_METHOD3(LnnDecryptAesGcm, int32_t (AesGcmInputParam *, uint8_t **, uint32_t *)); 185 MOCK_METHOD3(AuthFindNormalizeKeyByServerSide, int32_t (const char *, bool, AuthDeviceKeyInfo *)); 186 MOCK_METHOD3(AuthUpdateCreateTime, void (const char *, int32_t, bool)); 187 MOCK_METHOD2(IsFeatureSupport, bool (uint64_t feature, FeatureCapability capaBit)); 188 MOCK_METHOD3(LnnGetLocalStrInfo, int32_t (InfoKey, char *, uint32_t)); 189 MOCK_METHOD0(IsSupportUDIDAbatement, bool ()); 190 MOCK_METHOD3(JSON_AddBoolToObject, bool (JsonObj *, const char *, bool)); 191 MOCK_METHOD1(IsNeedUDIDAbatement, bool (const AuthSessionInfo *)); 192 MOCK_METHOD3(JSON_AddInt32ToObject, bool (JsonObj *, const char *, int32_t)); 193 MOCK_METHOD2(IsSupportFeatureByCapaBit, bool (uint32_t, AuthCapability)); 194 MOCK_METHOD0(JSON_CreateObject, JsonObj *()); 195 MOCK_METHOD1(JSON_Delete, void (JsonObj *obj)); 196 MOCK_METHOD2(JSON_Parse, JsonObj *(const char *str, uint32_t len)); 197 MOCK_METHOD0(LnnGetLocalNodeInfo, const NodeInfo *()); 198 MOCK_METHOD1(JSON_PrintUnformatted, char *(const JsonObj *obj)); 199 MOCK_METHOD1(GetFd, int32_t (uint64_t)); 200 MOCK_METHOD2(SoftBusSocketGetPeerName, int32_t (int32_t, SoftBusSockAddr *)); 201 MOCK_METHOD4(SoftBusInetNtoP, const char *(int32_t, const void *, char *, int32_t)); 202 MOCK_METHOD3(GetPeerUdidByNetworkId, int32_t (const char *, char *, uint32_t)); 203 MOCK_METHOD3(GetSessionKeyProfile, bool (int32_t, uint8_t *, uint32_t *)); 204 MOCK_METHOD2(GetIsExchangeUdidByNetworkId, int32_t (const char *, bool *)); 205 MOCK_METHOD2(LnnGetUnifiedDeviceName, int32_t (char *, uint32_t)); 206 MOCK_METHOD2(LnnSetLocalStrInfo, int32_t (InfoKey, const char *)); 207 MOCK_METHOD2(GetExtData, int32_t (char *, uint32_t)); 208 MOCK_METHOD2(AuthMetaGetConnIdByInfo, int32_t (const AuthConnInfo *, uint32_t *)); 209 MOCK_METHOD3(LnnGetMetaPtk, int32_t (uint32_t, char *, uint32_t)); 210 MOCK_METHOD3(LnnGetLocalPtkByUuid, int32_t (const char *, char *, uint32_t)); 211 MOCK_METHOD3(LnnDumpRemotePtk, void (const char *, const char *, const char *)); 212 MOCK_METHOD5(SoftBusBase64Encode, int32_t (unsigned char *, size_t, size_t *, const unsigned char *, size_t)); 213 MOCK_METHOD5(SoftBusBase64Decode, int32_t (unsigned char *, size_t, size_t *, const unsigned char *, size_t)); 214 MOCK_METHOD1(LnnUpdateLocalBroadcastCipherKey, int32_t (BroadcastCipherKey *)); 215 MOCK_METHOD2(ProcessCipherKeySyncInfo, void (const void *, const char *)); 216 MOCK_METHOD2(LnnHasDiscoveryType, bool (const NodeInfo *, DiscoveryType)); 217 MOCK_METHOD3(LnnGetNetworkIdByUuid, int32_t (const char *, char *, uint32_t)); 218 MOCK_METHOD3(LnnGetRemoteNumInfo, int32_t(const char *, InfoKey, int32_t *)); 219 MOCK_METHOD3(SoftbusGetConfig, int32_t (ConfigType, unsigned char *, uint32_t)); 220 MOCK_METHOD2(GenerateCertificate, int32_t (SoftbusCertChain *, const AuthSessionInfo *)); 221 MOCK_METHOD1(FreeSoftbusChain, void (SoftbusCertChain *)); 222 MOCK_METHOD2(FindAuthPreLinkNodeById, int32_t (uint32_t, AuthPreLinkNode *)); 223 MOCK_METHOD1(InitSoftbusChain, int32_t (SoftbusCertChain *)); 224 MOCK_METHOD3(VerifyCertificate, int32_t (SoftbusCertChain *, const NodeInfo *, const AuthSessionInfo *)); 225 MOCK_METHOD1(LnnGetLocalNodeInfoSafe, int32_t (NodeInfo *)); 226 MOCK_METHOD1(LnnGetBtMac, const char *(const NodeInfo *)); 227 MOCK_METHOD0(SoftBusGetBtState, int32_t (void)); 228 MOCK_METHOD1(SoftBusGetBtMacAddr, int32_t (SoftBusBtAddr *)); 229 MOCK_METHOD4(ConvertBtMacToStr, int32_t (char *, uint32_t, const uint8_t *, uint32_t)); 230 MOCK_METHOD4(LnnGetDeviceDisplayName, int32_t (const char *, const char *, char *, uint32_t)); 231 MOCK_METHOD0(SoftBusGetSysTimeMs, uint64_t (void)); 232 MOCK_METHOD1(LnnGetSupportedProtocols, uint64_t (const NodeInfo *)); 233 MOCK_METHOD3(StringToUpperCase, int32_t (const char *, char *, int32_t)); 234 MOCK_METHOD2(LnnGetAuthPort, int32_t (const NodeInfo *, int32_t)); 235 MOCK_METHOD2(LnnGetSessionPort, int32_t (const NodeInfo *, int32_t)); 236 MOCK_METHOD2(LnnGetProxyPort, int32_t (const NodeInfo *, int32_t)); 237 MOCK_METHOD4(JSON_AddBytesToObject, bool (JsonObj *, const char *, uint8_t *, uint32_t)); 238 MOCK_METHOD5(JSON_GetBytesFromObject, bool (const JsonObj *, const char *, uint8_t *, uint32_t, uint32_t *)); 239 MOCK_METHOD3(JSON_AddInt16ToObject, bool (JsonObj *, const char *, int16_t)); 240 MOCK_METHOD3(JSON_AddInt64ToObject, bool (JsonObj *, const char *, int64_t)); 241 MOCK_METHOD1(LnnGetP2pMac, const char * (const NodeInfo *)); 242 MOCK_METHOD1(LnnGetDeviceName, const char * (const DeviceBasicInfo *)); 243 MOCK_METHOD1(LnnConvertIdToDeviceType, char * (uint16_t)); 244 MOCK_METHOD1(LnnGetDeviceUdid, const char * (const NodeInfo *)); 245 MOCK_METHOD1(PackCipherKeySyncMsg, bool (void *)); 246 MOCK_METHOD1(LnnGetP2pRole, int32_t (const NodeInfo *)); 247 MOCK_METHOD1(LnnGetStaFrequency, int32_t (const NodeInfo *)); 248 MOCK_METHOD1(DelSessionKeyProfile, void(int32_t)); 249 MOCK_METHOD3(LnnGetLocalByteInfo, int32_t (InfoKey key, uint8_t *info, uint32_t len)); 250 MOCK_METHOD1(AuthSessionGetIsSameAccount, bool (int64_t authSeq)); 251 MOCK_METHOD1(AuthSessionGetUserId, int32_t (int64_t authSeq)); 252 MOCK_METHOD0(GetActiveOsAccountIds, int32_t(void)); 253 MOCK_METHOD4(LnnGetLocalStrInfoByIfnameIdx, int32_t(InfoKey, char *, uint32_t, int32_t)); 254 MOCK_METHOD3(IsTrustedDeviceFromAccess, bool (const char *, const char *, int32_t)); 255 }; 256 } // namespace OHOS 257 #endif // AUTH_TCP_CONNECTION_MOCK_H