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 namespace OHOS {
DepacketizeHeaderTest(const uint8_t * data,size_t size)28 void DepacketizeHeaderTest(const uint8_t* data, size_t size)
29 {
30 if ((data == nullptr) || (size < Communication::SoftBus::MAX_STREAM_LEN - OVERHEAD_LEN)) {
31 return;
32 }
33 char tmp[Communication::SoftBus::MAX_STREAM_LEN - OVERHEAD_LEN + 1] = {0};
34 if (memcpy_s(tmp, sizeof(tmp) - 1, data, sizeof(tmp) - 1) != EOK) {
35 return;
36 }
37
38 Communication::SoftBus::StreamDepacketizer decode(1);
39 decode.DepacketizeHeader((const char *)tmp);
40 }
41
DepacketizeBufferTest(const uint8_t * data,size_t size)42 void DepacketizeBufferTest(const uint8_t* data, size_t size)
43 {
44 if ((data == nullptr) || (size < Communication::SoftBus::MAX_STREAM_LEN - OVERHEAD_LEN)) {
45 return;
46 }
47 char tmp[Communication::SoftBus::MAX_STREAM_LEN - OVERHEAD_LEN + 1] = {0};
48 if (memcpy_s(tmp, sizeof(tmp) - 1, data, sizeof(tmp) - 1) != EOK) {
49 return;
50 }
51
52 Communication::SoftBus::StreamDepacketizer decode(1);
53 decode.DepacketizeBuffer((char *)tmp);
54 }
55 } // namespace OHOS
56
57 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)58 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
59 {
60 /* Run your code on data */
61 OHOS::DepacketizeHeaderTest(data, size);
62 OHOS::DepacketizeBufferTest(data, size);
63 return 0;
64 }
65