1 /*
2 * Copyright (c) 2022-2023 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 <iostream>
17 #include <cstring>
18 #include <thread>
19
20 #include "securec.h"
21 #include "session_mock.h"
22
23 constexpr int32_t DH_SUCCESS = 0;
24 const uint32_t AUTH_SESSION_SIDE_CLIENT = 1;
25 constexpr int32_t DH_ERROR = -1;
26 constexpr int32_t MOCK_SESSION_ID = 1;
27 static ISessionListener g_listener;
28 static char g_peerDeviceId[CHAR_ARRAY_SIZE + 1];
29 static char g_peerSessionName[CHAR_ARRAY_SIZE + 1];
30 static char g_mySessionName[CHAR_ARRAY_SIZE + 1];
CreateSessionServer(const char * pkgName,const char * sessionName,const ISessionListener * listener)31 int CreateSessionServer(const char *pkgName, const char *sessionName, const ISessionListener *listener)
32 {
33 (void)pkgName;
34 std::cout << "CreateSessionServer start sessionName:" << sessionName << std::endl;
35 if (strlen(sessionName) == 0) {
36 std::cout << "CreateSessionServer sessionName is empty." << std::endl;
37 return DH_ERROR;
38 }
39 if (listener == nullptr) {
40 std::cout << "CreateSessionServer listener is null." << std::endl;
41 return DH_ERROR;
42 }
43 if (strcpy_s(g_mySessionName, strlen(sessionName) + 1, sessionName) != EOK) {
44 std::cout << "strcpy_s failed" << std::endl;
45 return DH_ERROR;
46 }
47 g_listener.OnBytesReceived = listener->OnBytesReceived;
48 g_listener.OnMessageReceived = listener->OnMessageReceived;
49 g_listener.OnSessionClosed = listener->OnSessionClosed;
50 g_listener.OnSessionOpened = listener->OnSessionOpened;
51 g_listener.OnStreamReceived = listener->OnStreamReceived;
52 return DH_SUCCESS;
53 }
54
RemoveSessionServer(const char * pkgName,const char * sessionName)55 int RemoveSessionServer(const char *pkgName, const char *sessionName)
56 {
57 (void)pkgName;
58 (void)sessionName;
59 return DH_SUCCESS;
60 }
61
OpenSession(const char * mySessionName,const char * peerSessionName,const char * peerDeviceId,const char * groupId,const SessionAttribute * attr)62 int OpenSession(const char *mySessionName, const char *peerSessionName, const char *peerDeviceId, const char *groupId,
63 const SessionAttribute *attr)
64 {
65 (void)mySessionName;
66 (void)groupId;
67 (void)attr;
68 if (strlen(peerSessionName) == 0) {
69 return DH_ERROR;
70 }
71 if (strlen(peerDeviceId) == 0) {
72 return DH_ERROR;
73 }
74 if (strncpy_s(g_peerSessionName, CHAR_ARRAY_SIZE + 1, peerSessionName, CHAR_ARRAY_SIZE) != EOK) {
75 std::cout << "strncpy_s failed" << std::endl;
76 return DH_ERROR;
77 }
78 if (strncpy_s(g_peerDeviceId, CHAR_ARRAY_SIZE + 1, peerDeviceId, DEVICE_ID_SIZE_MAX) != EOK) {
79 std::cout << "strncpy_s failed" << std::endl;
80 return DH_ERROR;
81 }
82 std::thread thd(OpenSessionResult);
83 thd.detach();
84 return MOCK_SESSION_ID;
85 }
86
OpenSessionResult()87 void OpenSessionResult()
88 {
89 g_listener.OnSessionOpened(MOCK_SESSION_ID, DH_SUCCESS);
90 }
91
CloseSession(int sessionId)92 void CloseSession(int sessionId)
93 {
94 (void)sessionId;
95 }
96
SendBytes(int sessionId,const void * data,unsigned int len)97 int SendBytes(int sessionId, const void *data, unsigned int len)
98 {
99 (void)sessionId;
100 (void)data;
101 (void)len;
102 return DH_SUCCESS;
103 }
104
SendMessage(int sessionId,const void * data,unsigned int len)105 int SendMessage(int sessionId, const void *data, unsigned int len)
106 {
107 (void)sessionId;
108 (void)data;
109 (void)len;
110 return DH_SUCCESS;
111 }
112
SendStream(int sessionId,const StreamData * data,const StreamData * ext,const FrameInfo * param)113 int SendStream(int sessionId, const StreamData *data, const StreamData *ext, const FrameInfo *param)
114 {
115 (void)sessionId;
116 (void)data;
117 (void)ext;
118 (void)param;
119 return DH_SUCCESS;
120 }
121
GetMySessionName(int sessionId,char * sessionName,unsigned int len)122 int GetMySessionName(int sessionId, char *sessionName, unsigned int len)
123 {
124 (void)sessionId;
125 if (strncpy_s(sessionName, len + 1, g_mySessionName, CHAR_ARRAY_SIZE) != EOK) {
126 std::cout << "strncpy_s failed" << std::endl;
127 return DH_ERROR;
128 }
129 return DH_SUCCESS;
130 }
131
GetPeerSessionName(int sessionId,char * sessionName,unsigned int len)132 int GetPeerSessionName(int sessionId, char *sessionName, unsigned int len)
133 {
134 (void)sessionId;
135 if (strncpy_s(sessionName, len + 1, g_peerSessionName, CHAR_ARRAY_SIZE) != EOK) {
136 std::cout << "strncpy_s failed" << std::endl;
137 return DH_ERROR;
138 }
139 return DH_SUCCESS;
140 }
141
GetPeerDeviceId(int sessionId,char * devId,unsigned int len)142 int GetPeerDeviceId(int sessionId, char *devId, unsigned int len)
143 {
144 (void)sessionId;
145 if (strncpy_s(devId, len + 1, g_peerDeviceId, DEVICE_ID_SIZE_MAX) != EOK) {
146 std::cout << "strncpy_s failed" << std::endl;
147 return DH_ERROR;
148 }
149 return DH_SUCCESS;
150 }
151
GetSessionSide(int sessionId)152 int GetSessionSide(int sessionId)
153 {
154 (void)sessionId;
155 return AUTH_SESSION_SIDE_CLIENT;
156 }
157
SetFileReceiveListener(const char * pkgName,const char * sessionName,const IFileReceiveListener * recvListener,const char * rootDir)158 int SetFileReceiveListener(const char *pkgName, const char *sessionName, const IFileReceiveListener *recvListener,
159 const char *rootDir)
160 {
161 (void)pkgName;
162 (void)sessionName;
163 (void)recvListener;
164 (void)rootDir;
165 return DH_SUCCESS;
166 }
167
SetFileSendListener(const char * pkgName,const char * sessionName,const IFileSendListener * sendListener)168 int SetFileSendListener(const char *pkgName, const char *sessionName, const IFileSendListener *sendListener)
169 {
170 (void)pkgName;
171 (void)sessionName;
172 (void)sendListener;
173 return DH_SUCCESS;
174 }
175
SendFile(int sessionId,const char * sFileList[],const char * dFileList[],uint32_t fileCnt)176 int SendFile(int sessionId, const char *sFileList[], const char *dFileList[], uint32_t fileCnt)
177 {
178 (void)sessionId;
179 (void)sFileList;
180 (void)dFileList;
181 (void)fileCnt;
182 return DH_SUCCESS;
183 }