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