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 #include "fuzzer/FuzzedDataProvider.h"
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 FuzzedDataProvider fuzzer(data, size);
63 size_t blobSize = fuzzer.ConsumeIntegralInRange<size_t>(1, 50);
64 std::vector<uint8_t> fuzzVec = fuzzer.ConsumeBytes<uint8_t>(blobSize);
65
66 std::string str1 = fuzzer.ConsumeRandomLengthString();
67 Blob blob1(str1.c_str());
68 std::string str2 = fuzzer.ConsumeRandomLengthString();
69 blob1 = str2.c_str();
70 Blob blob2(fuzzer.ConsumeRandomLengthString());
71 blob2 = fuzzer.ConsumeRandomLengthString();
72 Blob blob3(fuzzVec);
73 std::string str3 = fuzzer.ConsumeRandomLengthString();
74 Blob blob4(str3.c_str(), str3.length() + 1);
75 Blob blob5(blob4);
76 Blob blob6(std::move(blob5));
77 Blob blob7 = blob6;
78 blob7 = Blob(blob6);
79 int count = 10;
80 auto buffer = std::make_unique<uint8_t[]>(count);
81 uint8_t *writePtr = buffer.get();
82 Blob blob8(fuzzer.ConsumeRandomLengthString());
83 blob8.WriteToBuffer(writePtr, count);
84 OHOS::BlobOption(blob8);
85
86 return 0;
87 }
88