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 "blob_fuzzer.h"
17 
18 #include <cstdint>
19 #include <vector>
20 
21 #include "securec.h"
22 #include "types.h"
23 #include "blob.h"
24 
25 using namespace OHOS::DistributedKv;
26 namespace OHOS {
BlobSelfOption(const Blob & blob)27 void BlobSelfOption(const Blob &blob)
28 {
29     blob.Empty();
30     blob.Size();
31     blob.Data();
32     blob.ToString();
33     blob.RawSize();
34 }
35 
BlobEachOtherOption(const Blob & blob1,const Blob & blob2)36 void BlobEachOtherOption(const Blob &blob1, const Blob &blob2)
37 {
38     blob1.Compare(blob2);
39     Blob blobOut;
40     blob1.Compare(blobOut);
41     blob1.StartsWith(blob2);
42 }
43 
BlobOption(const Blob & blob)44 void BlobOption(const Blob &blob)
45 {
46     BlobSelfOption(blob);
47     Blob blobTmp(blob);
48     BlobEachOtherOption(blob, blobTmp);
49 
50     Blob blobPrefix = { "fuzz" };
51     blobTmp = blobPrefix.ToString() + blob.ToString();
52     if (blobPrefix[0] == blobTmp[0] && (!(blobPrefix == blobTmp))) {
53         BlobEachOtherOption(blobTmp, blobPrefix);
54     }
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     std::string fuzzStr(reinterpret_cast<const char *>(data), size);
63     std::vector<uint8_t> fuzzVec(fuzzStr.begin(), fuzzStr.end());
64 
65     int count = 10;
66     char str[count + 1];
67     auto ret = memcpy_s(str, count + 1, data, std::min(static_cast<size_t>(count + 1), size));
68     if (ret != EOK) {
69         return 0;
70     }
71     str[count] = '\0';
72     Blob blob1(str);
73     blob1 = str;
74     Blob blob2(fuzzStr);
75     blob2 = fuzzStr;
76     Blob blob3(fuzzVec);
77     Blob blob4(str, count + 1);
78     Blob blob5(blob4);
79     Blob blob6(std::move(blob5));
80     Blob blob7 = blob6;
81     blob7 = Blob(blob6);
82     auto buffer = std::make_unique<uint8_t[]>(count);
83     uint8_t *writePtr = buffer.get();
84     Blob blob8(fuzzStr);
85     blob8.WriteToBuffer(writePtr, count);
86     OHOS::BlobOption(blob8);
87 
88     return 0;
89 }
90