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