• 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 "ispassseqcheck_fuzzer.h"
17 
18 #include <cstddef>
19 
20 #include "fuzz_data_generator.h"
21 #include "softbus_sequence_verification.h"
22 
23 using namespace std;
24 
25 namespace OHOS {
26     constexpr size_t THRESHOLD = 10;
27     constexpr uint32_t TEST_NUM = 4;
28     constexpr int32_t MINSEQ = 2;
29     constexpr int32_t BURDEN_MINSEQ = -2;
30     constexpr int32_t MAXSEQ = 4;
31     constexpr int32_t BURDEN_MAXSEQ = -1;
32     SeqVerifyInfo seqInfo;
33     enum  CmdId {
34         CMD_SOFTBUS_ONE,
35         CMD_SOFTBUS_TWO,
36         CMD_SOFTBUS_THREE,
37         CMD_SOFTBUS_FOUR,
38     };
39 
IsPassSeqCheckSwitch()40     static void IsPassSeqCheckSwitch()
41     {
42         uint64_t bit = 0;
43         GenerateUint64(bit);
44         bit = bit % THRESHOLD;
45         uint32_t cmd = 0;
46         GenerateUint32(cmd);
47         cmd = cmd % TEST_NUM;
48         switch (cmd) {
49             case CMD_SOFTBUS_ONE: {
50                 seqInfo.minSeq = MINSEQ;
51                 seqInfo.maxSeq = MAXSEQ;
52                 seqInfo.recvBitmap = bit;
53                 IsPassSeqCheck(&seqInfo, MINSEQ);
54                 break;
55             }
56             case CMD_SOFTBUS_TWO: {
57                 seqInfo.minSeq = MINSEQ;
58                 seqInfo.maxSeq = BURDEN_MAXSEQ;
59                 seqInfo.recvBitmap = bit;
60                 IsPassSeqCheck(&seqInfo, BURDEN_MINSEQ);
61                 break;
62             }
63             case CMD_SOFTBUS_THREE: {
64                 seqInfo.minSeq = BURDEN_MINSEQ;
65                 seqInfo.maxSeq = MAXSEQ;
66                 seqInfo.recvBitmap = bit;
67                 IsPassSeqCheck(&seqInfo, BURDEN_MINSEQ);
68                 break;
69             }
70             case CMD_SOFTBUS_FOUR: {
71                 seqInfo.minSeq = MINSEQ;
72                 seqInfo.maxSeq = MINSEQ;
73                 seqInfo.recvBitmap = bit;
74                 IsPassSeqCheck(&seqInfo, MINSEQ);
75                 break;
76             }
77             default:
78                 break;
79         }
80     }
81 } // namespace OHOS
82 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)83 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
84 {
85     if (size < OHOS::THRESHOLD) {
86         return 0;
87     }
88     DataGenerator::Write(data, size);
89 
90     /* Run your code on data */
91     OHOS::IsPassSeqCheckSwitch();
92 
93     DataGenerator::Clear();
94     return 0;
95 }