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
16 #include "clienttransfile_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <cstring>
21 #include <securec.h>
22
23 #include "client_trans_file.h"
24 #include "softbus_utils.h"
25 #include "softbus_def.h"
26 #include "session.h"
27 #include "client_trans_file_listener.h"
28 #include "file_adapter.h"
29
30 namespace OHOS {
31
OnReceiveFileStarted(int sessionId,const char * files,int fileCnt)32 static int OnReceiveFileStarted(int sessionId, const char* files, int fileCnt)
33 {
34 return 0;
35 }
36
OnReceiveFileFinished(int sessionId,const char * files,int fileCnt)37 static void OnReceiveFileFinished(int sessionId, const char* files, int fileCnt)
38 {}
39
OnReceiveFileProcess(int sessionId,const char * firstFile,uint64_t bytesUpload,uint64_t bytesTotal)40 static int OnReceiveFileProcess(int sessionId, const char* firstFile, uint64_t bytesUpload, uint64_t bytesTotal)
41 {
42 return 0;
43 }
44
OnSendFileProcess(int sessionId,uint64_t bytesUpload,uint64_t bytesTotal)45 static int OnSendFileProcess(int sessionId, uint64_t bytesUpload, uint64_t bytesTotal)
46 {
47 return 0;
48 }
49
OnSendFileFinished(int sessionId,const char * firstFile)50 static int OnSendFileFinished(int sessionId, const char* firstFile)
51 {
52 return 0;
53 }
54
OnFileTransError(int sessionId)55 static void OnFileTransError(int sessionId)
56 {}
57
TransOnFileChannelOpenedTest(const uint8_t * data,size_t size)58 void TransOnFileChannelOpenedTest(const uint8_t* data, size_t size)
59 {
60 if ((data == nullptr) || (size == 0)) {
61 return;
62 }
63 const char* sessionName = reinterpret_cast<const char*>(data);
64 int32_t fileport = 0;
65 TransOnFileChannelOpened(sessionName, nullptr, &fileport);
66 }
67
TransSetFileReceiveListenerTest(const uint8_t * data,size_t size)68 void TransSetFileReceiveListenerTest(const uint8_t* data, size_t size)
69 {
70 if ((data == nullptr) || (size == 0)) {
71 return;
72 }
73 const IFileReceiveListener fileRecvListener = {
74 .OnReceiveFileStarted = OnReceiveFileStarted,
75 .OnReceiveFileProcess = OnReceiveFileProcess,
76 .OnReceiveFileFinished = OnReceiveFileFinished,
77 .OnFileTransError = OnFileTransError,
78 };
79 const char* sessionName = reinterpret_cast<const char*>(data);
80 const char* rootDir = "/data/recv/";
81 TransSetFileReceiveListener(sessionName, &fileRecvListener, rootDir);
82 }
83
TransSetFileSendListenerTest(const uint8_t * data,size_t size)84 void TransSetFileSendListenerTest(const uint8_t* data, size_t size)
85 {
86 if ((data == nullptr) || (size == 0)) {
87 return;
88 }
89
90 IFileSendListener sendListener = {
91 .OnSendFileProcess = OnSendFileProcess,
92 .OnSendFileFinished = OnSendFileFinished,
93 .OnFileTransError = OnFileTransError,
94 };
95 const char* sessionName = reinterpret_cast<const char*>(data);
96 TransSetFileSendListener(sessionName, &sendListener);
97 }
98
TransGetFileListenerTest(const uint8_t * data,size_t size)99 void TransGetFileListenerTest(const uint8_t* data, size_t size)
100 {
101 if ((data == nullptr) || (size == 0)) {
102 return;
103 }
104
105 FileListener fileListener;
106 const char* sessionName = reinterpret_cast<const char*>(data);
107 TransGetFileListener(sessionName, &fileListener);
108 }
109
StartNStackXDFileServerTest(const uint8_t * data,size_t size)110 void StartNStackXDFileServerTest(const uint8_t* data, size_t size)
111 {
112 if ((data == nullptr) || (size < sizeof(int32_t))) {
113 return;
114 }
115
116 #define DEFAULT_KEY_LENGTH 32
117 int32_t len = *(reinterpret_cast<const int32_t*>(data));
118 StartNStackXDFileServer(nullptr, data, DEFAULT_KEY_LENGTH, NULL, &len);
119 }
120
TransDeleteFileListenerTest(const uint8_t * data,size_t size)121 void TransDeleteFileListenerTest(const uint8_t* data, size_t size)
122 {
123 if ((data == nullptr) || (size < SESSION_NAME_SIZE_MAX)) {
124 return;
125 }
126 char tmp[SESSION_NAME_SIZE_MAX + 1] = {0};
127 if (memcpy_s(tmp, sizeof(tmp) - 1, data, sizeof(tmp) - 1) != EOK) {
128 return;
129 }
130 TransDeleteFileListener(tmp);
131 }
132 } // namespace OHOS
133
134 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)135 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
136 {
137 /* Run your code on data */
138 OHOS::TransOnFileChannelOpenedTest(data, size);
139 OHOS::TransSetFileReceiveListenerTest(data, size);
140 OHOS::TransSetFileSendListenerTest(data, size);
141 OHOS::TransGetFileListenerTest(data, size);
142 OHOS::StartNStackXDFileServerTest(data, size);
143 OHOS::TransDeleteFileListenerTest(data, size);
144 return 0;
145 }
146