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 "lnn_connection_mock.h"
17 
18 #include "lnn_log.h"
19 #include "softbus_error_code.h"
20 
21 using namespace testing;
22 using namespace testing::ext;
23 
24 namespace OHOS {
25 void *g_connInterface;
26 static const int32_t TEST_DATA_LEN = 200;
LnnConnectInterfaceMock()27 LnnConnectInterfaceMock::LnnConnectInterfaceMock()
28 {
29     g_connInterface = reinterpret_cast<void *>(this);
30 }
31 
~LnnConnectInterfaceMock()32 LnnConnectInterfaceMock::~LnnConnectInterfaceMock()
33 {
34     g_connInterface = nullptr;
35 }
36 
GetConnInterface()37 static LnnConnectInterface *GetConnInterface()
38 {
39     return reinterpret_cast<LnnConnectInterfaceMock *>(g_connInterface);
40 }
41 
42 extern "C" {
ConnGetConnectionInfo(uint32_t connectionId,ConnectionInfo * info)43 int32_t ConnGetConnectionInfo(uint32_t connectionId, ConnectionInfo *info)
44 {
45     return GetConnInterface()->ConnGetConnectionInfo(connectionId, info);
46 }
47 
ConnSetConnectCallback(ConnModule moduleId,const ConnectCallback * callback)48 int32_t ConnSetConnectCallback(ConnModule moduleId, const ConnectCallback *callback)
49 {
50     return GetConnInterface()->ConnSetConnectCallback(moduleId, callback);
51 }
52 
ConnUnSetConnectCallback(ConnModule moduleId)53 void ConnUnSetConnectCallback(ConnModule moduleId)
54 {
55     GetConnInterface()->ConnUnSetConnectCallback(moduleId);
56 }
57 
ConnConnectDevice(const ConnectOption * option,uint32_t requestId,const ConnectResult * result)58 int32_t ConnConnectDevice(const ConnectOption *option, uint32_t requestId, const ConnectResult *result)
59 {
60     return GetConnInterface()->ConnConnectDevice(option, requestId, result);
61 }
62 
ConnDisconnectDevice(uint32_t connectionId)63 int32_t ConnDisconnectDevice(uint32_t connectionId)
64 {
65     return GetConnInterface()->ConnDisconnectDevice(connectionId);
66 }
67 
ConnGetHeadSize(void)68 uint32_t ConnGetHeadSize(void)
69 {
70     return GetConnInterface()->ConnGetHeadSize();
71 }
72 
ConnPostBytes(uint32_t connectionId,ConnPostData * data)73 int32_t ConnPostBytes(uint32_t connectionId, ConnPostData *data)
74 {
75     return GetConnInterface()->ConnPostBytes(connectionId, data);
76 }
77 
CheckActiveConnection(const ConnectOption * option,bool needOccupy)78 bool CheckActiveConnection(const ConnectOption *option, bool needOccupy)
79 {
80     return GetConnInterface()->CheckActiveConnection(option, needOccupy);
81 }
82 
ConnStartLocalListening(const LocalListenerInfo * info)83 int32_t ConnStartLocalListening(const LocalListenerInfo *info)
84 {
85     return GetConnInterface()->ConnStartLocalListening(info);
86 }
87 
ConnStopLocalListening(const LocalListenerInfo * info)88 int32_t ConnStopLocalListening(const LocalListenerInfo *info)
89 {
90     return GetConnInterface()->ConnStopLocalListening(info);
91 }
92 
ConnGetNewRequestId(ConnModule moduleId)93 uint32_t ConnGetNewRequestId(ConnModule moduleId)
94 {
95     return GetConnInterface()->ConnGetNewRequestId(moduleId);
96 }
DiscDeviceInfoChanged(InfoTypeChanged type)97 void DiscDeviceInfoChanged(InfoTypeChanged type)
98 {
99     return GetConnInterface()->DiscDeviceInfoChanged(type);
100 }
101 
ConnUpdateConnection(uint32_t connectionId,UpdateOption * option)102 int32_t ConnUpdateConnection(uint32_t connectionId, UpdateOption *option)
103 {
104     return GetConnInterface()->ConnUpdateConnection(connectionId, option);
105 }
106 }
107 
ActionofConnSetConnectCallback(ConnModule moduleId,const ConnectCallback * callback)108 int32_t LnnConnectInterfaceMock::ActionofConnSetConnectCallback(ConnModule moduleId, const ConnectCallback *callback)
109 {
110     (void)moduleId;
111     if (callback == nullptr) {
112         return SOFTBUS_INVALID_PARAM;
113     }
114     g_conncallback.OnDataReceived = callback->OnDataReceived;
115     g_conncallback.OnConnected = callback->OnConnected;
116     g_conncallback.OnDisconnected = callback->OnDisconnected;
117     return SOFTBUS_OK;
118 }
119 
ActionofOnConnectSuccessed(const ConnectOption * option,uint32_t requestId,const ConnectResult * result)120 int32_t LnnConnectInterfaceMock::ActionofOnConnectSuccessed(
121     const ConnectOption *option, uint32_t requestId, const ConnectResult *result)
122 {
123     (void)option;
124     uint32_t connectionId = 196619;
125     const ConnectionInfo info = {
126         .isAvailable = 1,
127         .isServer = 1,
128         .type = CONNECT_BR,
129         .brInfo.brMac = "11:22:33:44:55:66",
130     };
131     result->OnConnectSuccessed(requestId, connectionId, &info);
132     LNN_LOGI(LNN_TEST, "ActionofConnConnectDevice");
133     return SOFTBUS_OK;
134 }
135 
ActionofOnConnectFailed(const ConnectOption * option,uint32_t requestId,const ConnectResult * result)136 int32_t LnnConnectInterfaceMock::LnnConnectInterfaceMock::ActionofOnConnectFailed(
137     const ConnectOption *option, uint32_t requestId, const ConnectResult *result)
138 {
139     int32_t reason = 0;
140     result->OnConnectFailed(requestId, reason);
141     LNN_LOGI(LNN_TEST, "ActionofOnConnectFailed");
142     return SOFTBUS_OK;
143 }
144 
ActionofConnGetConnectionInfo(uint32_t connectionId,ConnectionInfo * info)145 int32_t LnnConnectInterfaceMock::ActionofConnGetConnectionInfo(uint32_t connectionId, ConnectionInfo *info)
146 {
147     (void)connectionId;
148     info->type = CONNECT_BLE;
149     info->isServer = SERVER_SIDE_FLAG;
150     info->isAvailable = 1;
151     strcpy_s(info->brInfo.brMac, sizeof(info->brInfo.brMac), "11:22:33:44:55:66");
152     return SOFTBUS_OK;
153 }
154 
ActionofConnUnSetConnectCallback(ConnModule moduleId)155 void LnnConnectInterfaceMock::ActionofConnUnSetConnectCallback(ConnModule moduleId)
156 {
157     (void)moduleId;
158 }
159 
ActionOfConnPostBytes(uint32_t connectionId,ConnPostData * data)160 int32_t LnnConnectInterfaceMock::ActionOfConnPostBytes(uint32_t connectionId, ConnPostData *data)
161 {
162     LNN_LOGI(LNN_TEST, "ActionOfConnPostBytes");
163     g_encryptData = data->buf;
164     if (strcpy_s(g_encryptData, TEST_DATA_LEN, data->buf) != SOFTBUS_OK) {
165         LNN_LOGE(LNN_TEST, "strcpy failed in conn post bytes");
166         return SOFTBUS_ERR;
167     }
168     return SOFTBUS_OK;
169 }
170 } // namespace OHOS