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