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