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 #include <cstdint>
17 #include <securec.h>
18
19 #include "lnn_local_ledger_deps_mock.h"
20 #include "softbus_common.h"
21 #include "softbus_error_code.h"
22
23 using namespace testing;
24 using namespace testing::ext;
25
26 namespace OHOS {
27 void *g_localLedgerDepsInterface;
28 constexpr char DEFAULT_DEVICE_NAME[] = "OpenHarmony";
29 constexpr char DEFAULT_DEVICE_UDID[] = "aaabbbcccdddeeefffggghhh";
30 constexpr char DEFAULT_DEVICE_TYPE[] = "default_type";
31 constexpr int32_t SOFTBUS_BUSCENTER_DUMP_LOCALDEVICEINFO_FD = -1;
32
LocalLedgerDepsInterfaceMock()33 LocalLedgerDepsInterfaceMock::LocalLedgerDepsInterfaceMock()
34 {
35 g_localLedgerDepsInterface = reinterpret_cast<void *>(this);
36 }
37
~LocalLedgerDepsInterfaceMock()38 LocalLedgerDepsInterfaceMock::~LocalLedgerDepsInterfaceMock()
39 {
40 g_localLedgerDepsInterface = nullptr;
41 }
42
GetLocalLedgerDepsInterface()43 static LocalLedgerDepsInterfaceMock *GetLocalLedgerDepsInterface()
44 {
45 return reinterpret_cast<LocalLedgerDepsInterfaceMock *>(g_localLedgerDepsInterface);
46 }
47
LedgerGetCommonDevInfo(const CommonDeviceKey key,char * value,uint32_t len)48 int32_t LocalLedgerDepsInterfaceMock::LedgerGetCommonDevInfo(const CommonDeviceKey key,
49 char *value, uint32_t len)
50 {
51 if (value == nullptr) {
52 return SOFTBUS_ERR;
53 }
54 switch (key) {
55 case COMM_DEVICE_KEY_DEVNAME:
56 if (strncpy_s(value, len, DEFAULT_DEVICE_NAME, strlen(DEFAULT_DEVICE_NAME)) != EOK) {
57 return SOFTBUS_ERR;
58 }
59 break;
60 case COMM_DEVICE_KEY_UDID:
61 if (strncpy_s(value, len, DEFAULT_DEVICE_UDID, UDID_BUF_LEN) != EOK) {
62 return SOFTBUS_ERR;
63 }
64 break;
65 case COMM_DEVICE_KEY_DEVTYPE:
66 if (strncpy_s(value, len, DEFAULT_DEVICE_TYPE, strlen(DEFAULT_DEVICE_TYPE)) != EOK) {
67 return SOFTBUS_ERR;
68 }
69 break;
70 default:
71 break;
72 }
73 return SOFTBUS_OK;
74 }
75
LedgerSoftBusRegBusCenterVarDump(char * dumpVar,SoftBusVarDumpCb cb)76 int32_t LocalLedgerDepsInterfaceMock::LedgerSoftBusRegBusCenterVarDump(char *dumpVar, SoftBusVarDumpCb cb)
77 {
78 int32_t ret = SOFTBUS_ERR;
79 if (cb != nullptr) {
80 ret = cb(SOFTBUS_BUSCENTER_DUMP_LOCALDEVICEINFO_FD);
81 }
82 return ret;
83 }
84
85 extern "C" {
LnnGetNetCapabilty(void)86 uint32_t LnnGetNetCapabilty(void)
87 {
88 return GetLocalLedgerDepsInterface()->LnnGetNetCapabilty();
89 }
90
SoftBusGenerateRandomArray(unsigned char * randStr,uint32_t len)91 int32_t SoftBusGenerateRandomArray(unsigned char *randStr, uint32_t len)
92 {
93 return GetLocalLedgerDepsInterface()->SoftBusGenerateRandomArray(randStr, len);
94 }
95
GetCommonDevInfo(const CommonDeviceKey key,char * value,uint32_t len)96 int32_t GetCommonDevInfo(const CommonDeviceKey key, char *value, uint32_t len)
97 {
98 return GetLocalLedgerDepsInterface()->GetCommonDevInfo(key, value, len);
99 }
100
LnnInitLocalP2pInfo(NodeInfo * info)101 int32_t LnnInitLocalP2pInfo(NodeInfo *info)
102 {
103 return GetLocalLedgerDepsInterface()->LnnInitLocalP2pInfo(info);
104 }
105
SoftBusRegBusCenterVarDump(char * dumpVar,SoftBusVarDumpCb cb)106 int32_t SoftBusRegBusCenterVarDump(char *dumpVar, SoftBusVarDumpCb cb)
107 {
108 return GetLocalLedgerDepsInterface()->SoftBusRegBusCenterVarDump(dumpVar, cb);
109 }
110
LnnInitOhosAccount(void)111 int32_t LnnInitOhosAccount(void)
112 {
113 return GetLocalLedgerDepsInterface()->LnnInitOhosAccount();
114 }
115
LnnGetFeatureCapabilty(void)116 uint64_t LnnGetFeatureCapabilty(void)
117 {
118 return GetLocalLedgerDepsInterface()->LnnGetFeatureCapabilty();
119 }
120
IsFeatureSupport(uint64_t feature,FeatureCapability capaBit)121 bool IsFeatureSupport(uint64_t feature, FeatureCapability capaBit)
122 {
123 return GetLocalLedgerDepsInterface()->IsFeatureSupport(feature, capaBit);
124 }
125
GetCommonOsType(int32_t * value)126 int32_t GetCommonOsType(int32_t *value)
127 {
128 return GetLocalLedgerDepsInterface()->GetCommonOsType(value);
129 }
130
GetCommonOsVersion(char * value,uint32_t len)131 int32_t GetCommonOsVersion(char *value, uint32_t len)
132 {
133 return GetLocalLedgerDepsInterface()->GetCommonOsVersion(value, len);
134 }
135
GetCommonDeviceVersion(char * value,uint32_t len)136 int32_t GetCommonDeviceVersion(char *value, uint32_t len)
137 {
138 return GetLocalLedgerDepsInterface()->GetCommonDeviceVersion(value, len);
139 }
140
GetDeviceSecurityLevel(int32_t * level)141 int32_t GetDeviceSecurityLevel(int32_t *level)
142 {
143 return GetLocalLedgerDepsInterface()->GetDeviceSecurityLevel(level);
144 }
145
SoftBusGetBtState(void)146 int SoftBusGetBtState(void)
147 {
148 return GetLocalLedgerDepsInterface()->SoftBusGetBtState();
149 }
150
SoftBusGetBtMacAddr(SoftBusBtAddr * mac)151 int SoftBusGetBtMacAddr(SoftBusBtAddr *mac)
152 {
153 return GetLocalLedgerDepsInterface()->SoftBusGetBtMacAddr(mac);
154 }
155 } // extern "C"
156 } // namespace OHOS
157