• 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 "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 } // namespace
28 
29 #define MIN_(x, y) ((x) < (y)) ? (x) : (y)
30 
31 static int g_serverCount = -1;
IsServerCountOK()32 bool IsServerCountOK()
33 {
34     return g_serverCount >= 0 && g_serverCount < SERVER_COUNT_LIMIT;
35 }
36 
37 static bool g_sendMessFlag = false;
SendBytes(int sessionId,const void * data,unsigned int len)38 int SendBytes(int sessionId, const void *data, unsigned int len)
39 {
40     ACCESSTOKEN_LOG_DEBUG(LABEL, "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 
49 static std::string uuid = "";
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     uuid = id_string;
77     ACCESSTOKEN_LOG_DEBUG(LABEL, "id_string: %{public}s", id_string.c_str());
78     return;
79 }
80 
81 
CompressMock(const std::string & json,const unsigned char * compressedBytes,int & compressedLength)82 void CompressMock(const std::string &json, const unsigned char *compressedBytes, int &compressedLength)
83 {
84     uLong len = compressBound(json.size());
85     // length will not so that long
86     if (compressedLength > 0 && (int) len > compressedLength) {
87         ACCESSTOKEN_LOG_ERROR(LABEL,
88             "compress error. data length overflow, bound length: %{public}d, buffer length: %{public}d", (int) len,
89             compressedLength);
90         return ;
91     }
92 
93     int result = compress(const_cast<Byte *>(compressedBytes), &len,
94         reinterpret_cast<unsigned char *>(const_cast<char *>(json.c_str())), json.size() + 1);
95     if (result != Z_OK) {
96         ACCESSTOKEN_LOG_ERROR(LABEL, "compress failed! error code: %{public}d", result);
97         return;
98     }
99     ACCESSTOKEN_LOG_DEBUG(LABEL, "compress complete. compress %{public}d bytes to %{public}d", compressedLength,
100         (int) len);
101     compressedLength = len;
102     return ;
103 }
104 
GetUuidMock()105 std::string GetUuidMock()
106 {
107     ACCESSTOKEN_LOG_DEBUG(LABEL, "GetUuidMock called uuid: %{public}s", uuid.c_str());
108     return uuid;
109 }
110 
GetSendMessFlagMock()111 bool GetSendMessFlagMock()
112 {
113     return g_sendMessFlag;
114 }
115 
ResetSendMessFlagMock()116 void ResetSendMessFlagMock()
117 {
118     g_sendMessFlag = false;
119 }
120 
ResetUuidMock()121 void ResetUuidMock()
122 {
123     uuid = "";
124 }
125