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