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 "clienttransproxyfilecommon_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <arpa/inet.h>
23 #include <fcntl.h>
24 #include <string>
25 
26 #include "client_trans_proxy_file_common.h"
27 #include "securec.h"
28 #include "softbus_adapter_file.h"
29 #include "softbus_adapter_mem.h"
30 #include "softbus_adapter_timer.h"
31 #include "softbus_def.h"
32 #include "softbus_errcode.h"
33 #include "softbus_type_def.h"
34 
35 namespace OHOS {
ClientTransProxyFileCommonTest(const uint8_t * data,size_t size)36 void ClientTransProxyFileCommonTest(const uint8_t* data, size_t size)
37 {
38     if ((data == nullptr) || (size < sizeof(uint64_t))) {
39         return;
40     }
41 
42     char *filePath = nullptr;
43     char *absPath = nullptr;
44     char *destFile = nullptr;
45     uint64_t index = *(reinterpret_cast<const uint64_t*>(data));
46     uint64_t frameNumber = *(reinterpret_cast<const uint64_t*>(data));
47     uint32_t bufferSize = *(reinterpret_cast<const uint32_t*>(data));
48     int32_t fileCount = *(reinterpret_cast<const int32_t*>(data));
49     char *path = nullptr;
50     int32_t fd = *(reinterpret_cast<const int32_t*>(data));
51     int32_t type = *(reinterpret_cast<const int32_t*>(data));
52     bool isBlock = *(reinterpret_cast<const bool*>(data));
53 
54     IsPathValid(filePath);
55 
56     GetAndCheckRealPath(filePath, absPath);
57 
58     CheckDestFilePathValid(destFile);
59 
60     FrameIndexToType(index, frameNumber);
61 
62     BufferToFileList(nullptr, bufferSize, &fileCount);
63 
64     TransGetFileName(path);
65 
66     FileLock(fd, type, isBlock);
67 
68     FileUnLock(fd);
69 }
70 } // namespace OHOS
71 
72 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)73 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
74 {
75     /* Run your code on data */
76     OHOS::ClientTransProxyFileCommonTest(data, size);
77     return 0;
78 }
79