1/*
2 * Copyright (c) 2023 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 "iam_fuzz_test.h"
17
18#include <cstdint>
19#include <string>
20#include <vector>
21
22#include "parcel.h"
23#include "securec.h"
24
25#include "iam_logger.h"
26
27#define LOG_TAG "FINGERPRINT_AUTH_TEST"
28
29namespace OHOS {
30namespace UserIam {
31namespace Common {
32namespace {
33constexpr int32_t MAX_DATA_LEN = 200;
34void FillFuzzBuffer(Parcel &parcel, void *p, uint32_t len)
35{
36    if (len == 0) {
37        return;
38    }
39
40    auto buffer = parcel.ReadBuffer(len);
41    if (buffer == nullptr) {
42        IAM_LOGE("ReadBuffer len %{public}u fail", len);
43        return;
44    }
45
46    if (memcpy_s(p, len, buffer, len) != EOK) {
47        IAM_LOGE("memcpy_s fail");
48        return;
49    }
50
51    return;
52}
53} // namespace
54
55void FillFuzzUint8Vector(Parcel &parcel, std::vector<uint8_t> &data)
56{
57    uint32_t len = parcel.ReadUint32() % MAX_DATA_LEN;
58    uint32_t memLen = len * sizeof(uint8_t);
59    data.resize(len);
60    FillFuzzBuffer(parcel, static_cast<void *>(&data[0]), memLen);
61    IAM_LOGI("fill vector len %{public}u ok", len);
62}
63
64void FillFuzzInt8Vector(Parcel &parcel, std::vector<int8_t> &data)
65{
66    uint32_t len = parcel.ReadUint32() % MAX_DATA_LEN;
67    uint32_t memLen = len * sizeof(int8_t);
68    data.resize(len);
69    FillFuzzBuffer(parcel, static_cast<void *>(&data[0]), memLen);
70    IAM_LOGI("fill vector len %{public}u ok", len);
71}
72
73void FillFuzzUint32Vector(Parcel &parcel, std::vector<uint32_t> &data)
74{
75    uint32_t len = parcel.ReadUint32() % MAX_DATA_LEN;
76    uint32_t memLen = len * sizeof(uint32_t);
77    data.resize(len);
78    FillFuzzBuffer(parcel, static_cast<void *>(&data[0]), memLen);
79    IAM_LOGI("fill vector len %{public}u ok", len);
80}
81
82void FillFuzzUint64Vector(Parcel &parcel, std::vector<uint64_t> &data)
83{
84    uint32_t len = parcel.ReadUint32() % MAX_DATA_LEN;
85    uint32_t memLen = len * sizeof(uint64_t);
86    data.resize(len);
87    FillFuzzBuffer(parcel, static_cast<void *>(&data[0]), memLen);
88    IAM_LOGI("fill vector len %{public}u ok", len);
89}
90
91void FillFuzzString(Parcel &parcel, std::string &str)
92{
93    uint32_t len = parcel.ReadUint32() % MAX_DATA_LEN + 1;
94    uint32_t memLen = len * sizeof(char);
95    std::vector<char> data(len, 0);
96    FillFuzzBuffer(parcel, static_cast<void *>(&data[0]), memLen - 1);
97    str = std::string(&data[0]);
98    IAM_LOGI("fill string len %{public}u ok", len - 1);
99}
100} // namespace Common
101} // namespace UserIam
102} // namespace OHOS
103