13298bea7Sopenharmony_ci/*
23298bea7Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
33298bea7Sopenharmony_ci * SPDX-License-Identifier: GPL-2.0
43298bea7Sopenharmony_ci *
53298bea7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
63298bea7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
73298bea7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
83298bea7Sopenharmony_ci * See the License for the specific language governing permissions and
93298bea7Sopenharmony_ci * limitations under the License.
103298bea7Sopenharmony_ci */
113298bea7Sopenharmony_ci
123298bea7Sopenharmony_ci#include <cstddef>
133298bea7Sopenharmony_ci#include <fcntl.h>
143298bea7Sopenharmony_ci#include <unistd.h>
153298bea7Sopenharmony_ci#include <cstdint>
163298bea7Sopenharmony_ci#include <cstdlib>
173298bea7Sopenharmony_ci#include "hccommon.h"
183298bea7Sopenharmony_ci
193298bea7Sopenharmony_cinamespace OHOS {
203298bea7Sopenharmony_cibool HcFuzzTest(const uint8_t *data, const char *pathname, size_t size)
213298bea7Sopenharmony_ci{
223298bea7Sopenharmony_ci    uint32_t value = 0;
233298bea7Sopenharmony_ci    uint32_t length = size > sizeof(uint32_t) ? sizeof(uint32_t) : size;
243298bea7Sopenharmony_ci    int ret = access("/mnt/f2fs_mount/", F_OK);
253298bea7Sopenharmony_ci    if (ret < 0) {
263298bea7Sopenharmony_ci        system("mkdir -p /mnt/f2fs_mount/");
273298bea7Sopenharmony_ci        system("mkfs.f2fs -d1 -t1 -O quota /data/image_f2fs");
283298bea7Sopenharmony_ci        system("losetup /dev/block/loop1 /data/image_f2fs");
293298bea7Sopenharmony_ci        system("mount -t f2fs /dev/block/loop1 /mnt/f2fs_mount/");
303298bea7Sopenharmony_ci    }
313298bea7Sopenharmony_ci
323298bea7Sopenharmony_ci    int fd = open(pathname, O_RDWR);
333298bea7Sopenharmony_ci    if (fd < 0) {
343298bea7Sopenharmony_ci        return false;
353298bea7Sopenharmony_ci    }
363298bea7Sopenharmony_ci
373298bea7Sopenharmony_ci    ret = read(fd, &value, sizeof(value));
383298bea7Sopenharmony_ci    if (ret < 0) {
393298bea7Sopenharmony_ci        close(fd);
403298bea7Sopenharmony_ci        return false;
413298bea7Sopenharmony_ci    }
423298bea7Sopenharmony_ci
433298bea7Sopenharmony_ci    ret = write(fd, data, length);
443298bea7Sopenharmony_ci    if (ret < 0) {
453298bea7Sopenharmony_ci        close(fd);
463298bea7Sopenharmony_ci        return false;
473298bea7Sopenharmony_ci    }
483298bea7Sopenharmony_ci    close(fd);
493298bea7Sopenharmony_ci    return true;
503298bea7Sopenharmony_ci}
513298bea7Sopenharmony_ci}