1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * SPDX-License-Identifier: GPL-2.0
4  *
5  * Unless required by applicable law or agreed to in writing, software
6  * distributed under the License is distributed on an "AS IS" BASIS,
7  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8  * See the License for the specific language governing permissions and
9  * limitations under the License.
10  */
11 
12 #include <cstddef>
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include <cstdint>
16 #include <cstdlib>
17 #include "hccommon.h"
18 
19 namespace OHOS {
HcFuzzTest(const uint8_t *data, const char *pathname, size_t size)20 bool HcFuzzTest(const uint8_t *data, const char *pathname, size_t size)
21 {
22     uint32_t value = 0;
23     uint32_t length = size > sizeof(uint32_t) ? sizeof(uint32_t) : size;
24     int ret = access("/mnt/f2fs_mount/", F_OK);
25     if (ret < 0) {
26         system("mkdir -p /mnt/f2fs_mount/");
27         system("mkfs.f2fs -d1 -t1 -O quota /data/image_f2fs");
28         system("losetup /dev/block/loop1 /data/image_f2fs");
29         system("mount -t f2fs /dev/block/loop1 /mnt/f2fs_mount/");
30     }
31 
32     int fd = open(pathname, O_RDWR);
33     if (fd < 0) {
34         return false;
35     }
36 
37     ret = read(fd, &value, sizeof(value));
38     if (ret < 0) {
39         close(fd);
40         return false;
41     }
42 
43     ret = write(fd, data, length);
44     if (ret < 0) {
45         close(fd);
46         return false;
47     }
48     close(fd);
49     return true;
50 }
51 }