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 "socket.h"
17 #include "constant.h"
18 #include "soft_bus_manager.h"
19 #include "accesstoken_common_log.h"
20 #include "soft_bus_socket_listener.h"
21 #include "soft_bus_channel.h"
22
23 using namespace OHOS::Security::AccessToken;
24 namespace {
25 static const int SERVER_COUNT_LIMIT = 10;
26 static int g_serverCount = -1;
27 static bool g_sendMessFlag = false;
28 static std::string g_uuid = "";
29 } // namespace
30
31 #define MIN_(x, y) ((x) < (y)) ? (x) : (y)
32
IsServerCountOK()33 bool IsServerCountOK()
34 {
35 return g_serverCount >= 0 && g_serverCount < SERVER_COUNT_LIMIT;
36 }
37
SendBytes(int sessionId,const void * data,unsigned int len)38 int SendBytes(int sessionId, const void *data, unsigned int len)
39 {
40 LOGD(ATM_DOMAIN, ATM_TAG, "len: %{public}d", len);
41 if (sessionId == Constant::INVALID_SESSION) {
42 return Constant::FAILURE;
43 }
44 DecompressMock(reinterpret_cast<const unsigned char *>(data), len);
45 g_sendMessFlag = true;
46 return Constant::SUCCESS;
47 }
48
DecompressMock(const unsigned char * bytes,const int length)49 void DecompressMock(const unsigned char *bytes, const int length)
50 {
51 LOGD(ATM_DOMAIN, ATM_TAG, "input length: %{public}d", length);
52 uLong len = 1048576;
53 unsigned char *buf = static_cast<unsigned char *>(malloc(len + 1));
54 if (buf == nullptr) {
55 LOGE(ATM_DOMAIN, ATM_TAG, "no enough memory!");
56 return;
57 }
58 (void)memset_s(buf, len + 1, 0, len + 1);
59 int result = uncompress(buf, &len, const_cast<unsigned char *>(bytes), length);
60 if (result != Z_OK) {
61 LOGE(ATM_DOMAIN, ATM_TAG,
62 "uncompress failed, error code: %{public}d, bound length: %{public}d, buffer length: %{public}d", result,
63 static_cast<int>(len), length);
64 free(buf);
65 return;
66 }
67 buf[len] = '\0';
68 std::string str(reinterpret_cast<char *>(buf));
69 free(buf);
70 LOGD(ATM_DOMAIN, ATM_TAG, "done, output: %{public}s", str.c_str());
71
72 std::size_t id_post = str.find("\"id\":");
73
74 std::string id_string = str.substr(id_post + 6, 9);
75 g_uuid = id_string;
76 LOGD(ATM_DOMAIN, ATM_TAG, "id_string: %{public}s", id_string.c_str());
77 return;
78 }
79
CompressMock(const std::string & json,const unsigned char * compressedBytes,int & compressedLength)80 void CompressMock(const std::string &json, const unsigned char *compressedBytes, int &compressedLength)
81 {
82 uLong len = compressBound(json.size());
83 // length will not so that long
84 if (compressedLength > 0 && (int) len > compressedLength) {
85 LOGE(ATM_DOMAIN, ATM_TAG,
86 "compress error. data length overflow, bound length: %{public}d, buffer length: %{public}d", (int) len,
87 compressedLength);
88 return ;
89 }
90
91 int result = compress(const_cast<Byte *>(compressedBytes), &len,
92 reinterpret_cast<unsigned char *>(const_cast<char *>(json.c_str())), json.size() + 1);
93 if (result != Z_OK) {
94 LOGE(ATM_DOMAIN, ATM_TAG, "compress failed! error code: %{public}d", result);
95 return;
96 }
97 LOGD(ATM_DOMAIN, ATM_TAG, "compress complete. compress %{public}d bytes to %{public}d", compressedLength,
98 (int) len);
99 compressedLength = len;
100 return ;
101 }
102
GetUuidMock()103 std::string GetUuidMock()
104 {
105 LOGD(ATM_DOMAIN, ATM_TAG, "GetUuidMock called uuid: %{public}s", g_uuid.c_str());
106 return g_uuid;
107 }
108
GetSendMessFlagMock()109 bool GetSendMessFlagMock()
110 {
111 return g_sendMessFlag;
112 }
113
ResetSendMessFlagMock()114 void ResetSendMessFlagMock()
115 {
116 g_sendMessFlag = false;
117 }
118
ResetUuidMock()119 void ResetUuidMock()
120 {
121 g_uuid = "";
122 }
123