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 <errno.h>
17#include <fcntl.h>
18#include <stdio.h>
19#include <string.h>
20#include <sys/uio.h>
21
22#include "filepath_util.h"
23
24/**
25 * @tc.name      : readv_0100
26 * @tc.desc      : read data into multiple buffers
27 * @tc.level     : Level 0
28 */
29void readv_0100(void)
30{
31    char path[PATH_MAX] = {0};
32    FILE_ABSOLUTE_PATH(STR_FILE_TXT, path);
33    int fd = open(path, O_RDWR | O_CREAT, TEST_MODE);
34
35    char buf1[] = "hello";
36    char buf2[] = "world";
37    struct iovec ios[] = {{buf1, strlen(buf1)}, {buf2, strlen(buf2)}};
38
39    ssize_t result = writev(fd, ios, sizeof(ios) / sizeof(struct iovec));
40    if (result != strlen(buf1) + strlen(buf2)) {
41        t_error("%s failed: result = %ld\n", __func__, result);
42    }
43
44    lseek(fd, 0, SEEK_SET);
45
46    memset(buf1, 0, sizeof(buf1));
47    memset(buf2, 0, sizeof(buf2));
48
49    result = readv(fd, ios, sizeof(ios) / sizeof(struct iovec));
50    if (result != strlen(buf1) + strlen(buf2)) {
51        t_error("%s failed: result = %ld\n", __func__, result);
52    }
53
54    if (strcmp(buf1, "hello")) {
55        t_error("%s failed: buf1 = %s\n", __func__, buf1);
56    }
57
58    if (strcmp(buf2, "world")) {
59        t_error("%s failed: buf2 = %s\n", __func__, buf2);
60    }
61
62    remove(path);
63}
64
65/**
66 * @tc.name      : readv_0200
67 * @tc.desc      : read data into multiple buffers with invalid parameters
68 * @tc.level     : Level 2
69 */
70void readv_0200(void)
71{
72    errno = 0;
73    ssize_t result = readv(-1, NULL, -1);
74    if (result == 0) {
75        t_error("%s failed: result = %ld\n", __func__, result);
76    }
77
78    if (errno == 0) {
79        t_error("%s failed: errno = %d\n", __func__, errno);
80    }
81}
82
83int main(int argc, char *argv[])
84{
85    readv_0100();
86    readv_0200();
87
88    return t_status;
89}
90