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 #include <securec.h>
16 #include "session.h"
17 #include "constant.h"
18 #include "soft_bus_manager.h"
19 #include "accesstoken_log.h"
20 #include "soft_bus_session_listener.h"
21 #include "soft_bus_channel.h"
22
23 using namespace OHOS::Security::AccessToken;
24 namespace {
25 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "SoftBusSenssionMock"};
26 static const int SESSION_COUNT_LIMIT = 20;
27 static const int SERVER_COUNT_LIMIT = 10;
28 } // namespace
29
30 #define MIN_(x, y) ((x) < (y)) ? (x) : (y)
31
32 static int serverCount_ = -1;
IsServerCountOK()33 bool IsServerCountOK()
34 {
35 return serverCount_ >= 0 && serverCount_ < SERVER_COUNT_LIMIT;
36 }
37
38 static ISessionListener *listener_ = nullptr;
CreateSessionServer(const char * pkgName,const char * sessionName,const ISessionListener * listener)39 int CreateSessionServer(const char *pkgName, const char *sessionName, const ISessionListener *listener)
40 {
41 ACCESSTOKEN_LOG_DEBUG(LABEL, "pkg name: %{public}s", pkgName);
42 ACCESSTOKEN_LOG_DEBUG(LABEL, "sessionName: %{public}s", sessionName);
43 serverCount_++;
44 if (IsServerCountOK()) {
45 listener_ = const_cast<ISessionListener *>(listener);
46 ACCESSTOKEN_LOG_DEBUG(LABEL, "success, server count: %{public}d", serverCount_);
47 return Constant::SUCCESS;
48 }
49
50 ACCESSTOKEN_LOG_DEBUG(LABEL, "failure, server count: %{public}d", serverCount_);
51 return Constant::FAILURE;
52 }
RemoveSessionServer(const char * pkgName,const char * sessionName)53 int RemoveSessionServer(const char *pkgName, const char *sessionName)
54 {
55 ACCESSTOKEN_LOG_DEBUG(LABEL, "pkg name: %{public}s", pkgName);
56 ACCESSTOKEN_LOG_DEBUG(LABEL, "sessionName: %{public}s", sessionName);
57 if (IsServerCountOK()) {
58 serverCount_--;
59 listener_ = nullptr;
60 ACCESSTOKEN_LOG_DEBUG(LABEL, "success, server count: %{public}d", serverCount_);
61 return Constant::SUCCESS;
62 }
63
64 if (serverCount_ >= 0) {
65 serverCount_--;
66 }
67 ACCESSTOKEN_LOG_DEBUG(LABEL, "failure, server count: %{public}d", serverCount_);
68 return Constant::FAILURE;
69 }
70
71 static int sessionCount_ = -1;
IsSessionCountOK()72 bool IsSessionCountOK()
73 {
74 ACCESSTOKEN_LOG_DEBUG(LABEL, "SESSION_COUNT_LIMIT: %{public}d", SESSION_COUNT_LIMIT);
75 return sessionCount_ >= 0 && sessionCount_ < SESSION_COUNT_LIMIT;
76 }
77
OpenSession(const char * mySessionName,const char * peerSessionName,const char * peerDeviceId,const char * groupId,const SessionAttribute * attr)78 int OpenSession(const char *mySessionName, const char *peerSessionName, const char *peerDeviceId, const char *groupId,
79 const SessionAttribute *attr)
80 {
81 ACCESSTOKEN_LOG_DEBUG(LABEL, "mySessionName: %{public}s", mySessionName);
82 ACCESSTOKEN_LOG_DEBUG(LABEL, "peerSessionName: %{public}s", peerSessionName);
83 ACCESSTOKEN_LOG_DEBUG(LABEL, "peerDeviceId: %{public}s", peerDeviceId);
84 ACCESSTOKEN_LOG_DEBUG(LABEL, "groupId: %{public}s", groupId);
85
86 sessionCount_++;
87 SoftBusSessionListener::OnSessionOpened(1, Constant::SUCCESS);
88 ACCESSTOKEN_LOG_DEBUG(LABEL, "success, session count: %{public}d", sessionCount_);
89 return 1;
90 }
CloseSession(int sessionId)91 void CloseSession(int sessionId)
92 {
93 ACCESSTOKEN_LOG_DEBUG(LABEL, "sessionId: %{public}d", sessionId);
94 if (sessionCount_ >= 0) {
95 sessionCount_--;
96 ACCESSTOKEN_LOG_DEBUG(LABEL, "success, session count: %{public}d", sessionCount_);
97 }
98 }
99
100 static bool g_sendMessFlag = false;
SendBytes(int sessionId,const void * data,unsigned int len)101 int SendBytes(int sessionId, const void *data, unsigned int len)
102 {
103 ACCESSTOKEN_LOG_DEBUG(LABEL, "len: %{public}d", len);
104 if (sessionId == Constant::INVALID_SESSION) {
105 return Constant::FAILURE;
106 }
107 DecompressMock(reinterpret_cast<const unsigned char *>(data), len);
108 g_sendMessFlag = true;
109 return Constant::SUCCESS;
110 }
111
GetPeerSessionName(int sessionId,char * sessionName,unsigned int len)112 int GetPeerSessionName(int sessionId, char *sessionName, unsigned int len)
113 {
114 if (sessionId == Constant::INVALID_SESSION) {
115 return Constant::FAILURE;
116 }
117 std::string x;
118 if (sessionId < SERVER_COUNT_LIMIT) {
119 x = SoftBusManager::SESSION_NAME;
120 } else {
121 x = "sessionid-" + std::to_string(sessionId);
122 }
123 if (len < x.length()) {
124 return Constant::FAILURE;
125 }
126 if (memcpy_s(sessionName, x.length(), x.c_str(), x.length()) != EOK) {
127 return Constant::FAILURE;
128 }
129 sessionName[x.length()] = '\0';
130 ACCESSTOKEN_LOG_DEBUG(LABEL, "success, session name: %{public}s", sessionName);
131
132 return 0;
133 }
134
GetPeerDeviceId(int sessionId,char * devId,unsigned int len)135 int GetPeerDeviceId(int sessionId, char *devId, unsigned int len)
136 {
137 if (sessionId == Constant::INVALID_SESSION) {
138 return Constant::FAILURE;
139 }
140
141 std::string x = "deviceid-" + std::to_string(sessionId);
142 if (len < x.length()) {
143 return Constant::FAILURE;
144 }
145
146 if (memcpy_s(devId, x.length(), x.c_str(), x.length()) != EOK) {
147 return Constant::FAILURE;
148 }
149 devId[x.length()] = '\0';
150 return 0;
151 }
152
153 static std::string uuid = "";
DecompressMock(const unsigned char * bytes,const int length)154 void DecompressMock(const unsigned char *bytes, const int length)
155 {
156 ACCESSTOKEN_LOG_DEBUG(LABEL, "input length: %{public}d", length);
157 uLong len = 1048576;
158 unsigned char *buf = static_cast<unsigned char *>(malloc(len + 1));
159 if (buf == nullptr) {
160 ACCESSTOKEN_LOG_ERROR(LABEL, "no enough memory!");
161 return;
162 }
163 (void)memset_s(buf, len + 1, 0, len + 1);
164 int result = uncompress(buf, &len, const_cast<unsigned char *>(bytes), length);
165 if (result != Z_OK) {
166 ACCESSTOKEN_LOG_ERROR(LABEL,
167 "uncompress failed, error code: %{public}d, bound length: %{public}d, buffer length: %{public}d", result,
168 static_cast<int>(len), length);
169 free(buf);
170 return;
171 }
172 buf[len] = '\0';
173 std::string str(reinterpret_cast<char *>(buf));
174 free(buf);
175 ACCESSTOKEN_LOG_DEBUG(LABEL, "done, output: %{public}s", str.c_str());
176
177 std::size_t id_post = str.find("\"id\":");
178
179 std::string id_string = str.substr(id_post + 6, 9);
180 uuid = id_string;
181 ACCESSTOKEN_LOG_DEBUG(LABEL, "id_string: %{public}s", id_string.c_str());
182 return;
183 }
184
185
CompressMock(const std::string & json,const unsigned char * compressedBytes,int & compressedLength)186 void CompressMock(const std::string &json, const unsigned char *compressedBytes, int &compressedLength)
187 {
188 uLong len = compressBound(json.size());
189 // length will not so that long
190 if (compressedLength > 0 && (int) len > compressedLength) {
191 ACCESSTOKEN_LOG_ERROR(LABEL,
192 "compress error. data length overflow, bound length: %{public}d, buffer length: %{public}d", (int) len,
193 compressedLength);
194 return ;
195 }
196
197 int result = compress(const_cast<Byte *>(compressedBytes), &len,
198 reinterpret_cast<unsigned char *>(const_cast<char *>(json.c_str())), json.size() + 1);
199 if (result != Z_OK) {
200 ACCESSTOKEN_LOG_ERROR(LABEL, "compress failed! error code: %{public}d", result);
201 return;
202 }
203 ACCESSTOKEN_LOG_DEBUG(LABEL, "compress complete. compress %{public}d bytes to %{public}d", compressedLength,
204 (int) len);
205 compressedLength = len;
206 return ;
207 }
208
GetUuidMock()209 std::string GetUuidMock()
210 {
211 ACCESSTOKEN_LOG_DEBUG(LABEL, "GetUuidMock called uuid: %{public}s", uuid.c_str());
212 return uuid;
213 }
214
215
GetSendMessFlagMock()216 bool GetSendMessFlagMock()
217 {
218 return g_sendMessFlag;
219 }
220
ResetSendMessFlagMock()221 void ResetSendMessFlagMock()
222 {
223 g_sendMessFlag = false;
224 }
225