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