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 "streamdepacketizer_fuzzer.h"
17 #include "stream_depacketizer.h"
18 #include "common_inner.h"
19 #include "i_stream.h"
20 #include "stream_common.h"
21 #include "softbus_adapter_crypto.h"
22 #include <securec.h>
23 #include <cstddef>
24 #include <cstdint>
25 
26 using namespace std;
27 
28 namespace OHOS {
DepacketizeHeaderTest(const uint8_t * data,size_t size)29     void DepacketizeHeaderTest(const uint8_t* data, size_t size)
30     {
31         if ((data == nullptr) || (size < Communication::SoftBus::MAX_STREAM_LEN - OVERHEAD_LEN)) {
32             return;
33         }
34         char tmp[Communication::SoftBus::MAX_STREAM_LEN - OVERHEAD_LEN + 1] = {0};
35         if (memcpy_s(tmp, sizeof(tmp) - 1, data, sizeof(tmp) - 1) != EOK) {
36             return;
37         }
38 
39         Communication::SoftBus::StreamDepacketizer decode(1);
40         decode.DepacketizeHeader((const char *)tmp);
41     }
42 
DepacketizeBufferTest(const uint8_t * data,size_t size)43     void DepacketizeBufferTest(const uint8_t* data, size_t size)
44     {
45         if ((data == nullptr) || (size < Communication::SoftBus::MAX_STREAM_LEN - OVERHEAD_LEN)) {
46             return;
47         }
48         char tmp[Communication::SoftBus::MAX_STREAM_LEN - OVERHEAD_LEN + 1] = {0};
49         if (memcpy_s(tmp, sizeof(tmp) - 1, data, sizeof(tmp) - 1) != EOK) {
50             return;
51         }
52 
53         Communication::SoftBus::StreamDepacketizer decode(1);
54         decode.DepacketizeBuffer((char *)tmp, sizeof(tmp));
55     }
56 } // namespace OHOS
57 
58 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)59 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
60 {
61     /* Run your code on data */
62     OHOS::DepacketizeHeaderTest(data, size);
63     OHOS::DepacketizeBufferTest(data, size);
64     return 0;
65 }
66