19762338dSopenharmony_ci/*
29762338dSopenharmony_ci * Copyright (C) 2024 HiHope Open Source Organization.
39762338dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
49762338dSopenharmony_ci * you may not use this file except in compliance with the License.
59762338dSopenharmony_ci * You may obtain a copy of the License at
69762338dSopenharmony_ci *
79762338dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
89762338dSopenharmony_ci *
99762338dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
109762338dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
119762338dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
129762338dSopenharmony_ci * See the License for the specific language governing permissions and
139762338dSopenharmony_ci * limitations under the License.
149762338dSopenharmony_ci */
159762338dSopenharmony_ci
169762338dSopenharmony_ci#include <cerrno>
179762338dSopenharmony_ci#include <cstdio>
189762338dSopenharmony_ci#include <cstdlib>
199762338dSopenharmony_ci#include <string>
209762338dSopenharmony_ci#include <fcntl.h>
219762338dSopenharmony_ci#include <unistd.h>
229762338dSopenharmony_ci#include <vector>
239762338dSopenharmony_ci#include <arpa/inet.h>
249762338dSopenharmony_ci#include <gtest/gtest.h>
259762338dSopenharmony_ci#include <netinet/in.h>
269762338dSopenharmony_ci#include <sys/stat.h>
279762338dSopenharmony_ci#include <sys/socket.h>
289762338dSopenharmony_ci#include <sys/types.h>
299762338dSopenharmony_ci#include <sys/uio.h>
309762338dSopenharmony_ci#include "securec.h"
319762338dSopenharmony_ci
329762338dSopenharmony_ciusing namespace testing::ext;
339762338dSopenharmony_ci
349762338dSopenharmony_ciclass HatsWritevTest : public testing::Test {
359762338dSopenharmony_cipublic:
369762338dSopenharmony_cistatic void SetUpTestCase();
379762338dSopenharmony_cistatic void TearDownTestCase();
389762338dSopenharmony_civoid SetUp();
399762338dSopenharmony_civoid TearDown();
409762338dSopenharmony_ciprivate:
419762338dSopenharmony_ci};
429762338dSopenharmony_civoid HatsWritevTest::SetUp()
439762338dSopenharmony_ci{
449762338dSopenharmony_ci}
459762338dSopenharmony_civoid HatsWritevTest::TearDown()
469762338dSopenharmony_ci{
479762338dSopenharmony_ci}
489762338dSopenharmony_civoid HatsWritevTest::SetUpTestCase()
499762338dSopenharmony_ci{
509762338dSopenharmony_ci}
519762338dSopenharmony_civoid HatsWritevTest::TearDownTestCase()
529762338dSopenharmony_ci{
539762338dSopenharmony_ci}
549762338dSopenharmony_ci
559762338dSopenharmony_cistatic const char *WRITEV_TEST_FILE = "/data/local/tmp/tryWritev.txt";
569762338dSopenharmony_cistatic const int VEC_LEN = 2;
579762338dSopenharmony_cistatic const char *TEST_DATA = "Hello writev";
589762338dSopenharmony_cistatic const int TEST_DATA_LEN = strlen(TEST_DATA);
599762338dSopenharmony_cistatic const char *TEST_DATA_BUF1 = "Hello ";
609762338dSopenharmony_cistatic const int BUF1_LEN = strlen(TEST_DATA_BUF1);
619762338dSopenharmony_cistatic const char *TEST_DATA_BUF2 = "writev";
629762338dSopenharmony_cistatic const int BUF2_LEN = strlen(TEST_DATA_BUF2);
639762338dSopenharmony_ci
649762338dSopenharmony_ci/*
659762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_WRITEV_0100
669762338dSopenharmony_ci * @tc.name   : WritevWriteDataToFileSuccess_0001
679762338dSopenharmony_ci * @tc.desc   : writev writes data to file success.
689762338dSopenharmony_ci * @tc.size   : MediumTest
699762338dSopenharmony_ci * @tc.type   : Function
709762338dSopenharmony_ci * @tc.level  : Level 1
719762338dSopenharmony_ci */
729762338dSopenharmony_ciHWTEST_F(HatsWritevTest, WritevWriteDataToFileSuccess_0001, Function | MediumTest | Level1)
739762338dSopenharmony_ci{
749762338dSopenharmony_ci    int fd;
759762338dSopenharmony_ci    struct iovec vec[VEC_LEN];
769762338dSopenharmony_ci    int ret;
779762338dSopenharmony_ci    char *iovBuf1 = nullptr;
789762338dSopenharmony_ci    char *iovBuf2 = nullptr;
799762338dSopenharmony_ci    char *readBuf = nullptr;
809762338dSopenharmony_ci    iovBuf1 = new char[BUF1_LEN];
819762338dSopenharmony_ci    memcpy_s(iovBuf1, BUF1_LEN, TEST_DATA_BUF1, BUF1_LEN);
829762338dSopenharmony_ci    iovBuf2 = new char[BUF2_LEN];
839762338dSopenharmony_ci    memcpy_s(iovBuf2, BUF2_LEN, TEST_DATA_BUF2, BUF2_LEN);
849762338dSopenharmony_ci    vec[0].iov_base = iovBuf1;
859762338dSopenharmony_ci    vec[0].iov_len = BUF1_LEN;
869762338dSopenharmony_ci    vec[1].iov_base = iovBuf2;
879762338dSopenharmony_ci    vec[1].iov_len = BUF2_LEN;
889762338dSopenharmony_ci    readBuf = new char[TEST_DATA_LEN + 1];
899762338dSopenharmony_ci
909762338dSopenharmony_ci    fd = open(WRITEV_TEST_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0644);
919762338dSopenharmony_ci    EXPECT_TRUE(fd >= 3);
929762338dSopenharmony_ci
939762338dSopenharmony_ci    ret = writev(fd, vec, VEC_LEN);
949762338dSopenharmony_ci    EXPECT_TRUE(ret == TEST_DATA_LEN);
959762338dSopenharmony_ci    close(fd);
969762338dSopenharmony_ci
979762338dSopenharmony_ci    fd = open(WRITEV_TEST_FILE, O_RDONLY);
989762338dSopenharmony_ci    EXPECT_TRUE(fd >= 3);
999762338dSopenharmony_ci    ret = read(fd, readBuf, TEST_DATA_LEN);
1009762338dSopenharmony_ci    EXPECT_TRUE(ret == TEST_DATA_LEN);
1019762338dSopenharmony_ci    readBuf[TEST_DATA_LEN] = '\0';
1029762338dSopenharmony_ci    ret = strcmp(readBuf, TEST_DATA);
1039762338dSopenharmony_ci    EXPECT_TRUE(ret == 0);
1049762338dSopenharmony_ci    close(fd);
1059762338dSopenharmony_ci    delete[] iovBuf1;
1069762338dSopenharmony_ci    delete[] iovBuf2;
1079762338dSopenharmony_ci    delete[] readBuf;
1089762338dSopenharmony_ci}
1099762338dSopenharmony_ci
1109762338dSopenharmony_ci/*
1119762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_WRITEV_0200
1129762338dSopenharmony_ci * @tc.name   : WritevWriteToPipeSuccess_0002
1139762338dSopenharmony_ci * @tc.desc   : Writev write data to pipe success.
1149762338dSopenharmony_ci * @tc.size   : MediumTest
1159762338dSopenharmony_ci * @tc.type   : Function
1169762338dSopenharmony_ci * @tc.level  : Level 1
1179762338dSopenharmony_ci */
1189762338dSopenharmony_ciHWTEST_F(HatsWritevTest, WritevWriteToPipeSuccess_0002, Function | MediumTest | Level1)
1199762338dSopenharmony_ci{
1209762338dSopenharmony_ci    int pipefd[2];
1219762338dSopenharmony_ci    struct iovec vec[VEC_LEN];
1229762338dSopenharmony_ci    int ret;
1239762338dSopenharmony_ci    char *iovBuf1 = nullptr;
1249762338dSopenharmony_ci    char *iovBuf2 = nullptr;
1259762338dSopenharmony_ci    char *readBuf = nullptr;
1269762338dSopenharmony_ci    iovBuf1 = new char[BUF1_LEN];
1279762338dSopenharmony_ci    memcpy_s(iovBuf1, BUF1_LEN, TEST_DATA_BUF1, BUF1_LEN);
1289762338dSopenharmony_ci    iovBuf2 = new char[BUF2_LEN];
1299762338dSopenharmony_ci    memcpy_s(iovBuf2, BUF2_LEN, TEST_DATA_BUF2, BUF2_LEN);
1309762338dSopenharmony_ci    vec[0].iov_base = iovBuf1;
1319762338dSopenharmony_ci    vec[0].iov_len = BUF1_LEN;
1329762338dSopenharmony_ci    vec[1].iov_base = iovBuf2;
1339762338dSopenharmony_ci    vec[1].iov_len = BUF2_LEN;
1349762338dSopenharmony_ci    readBuf = new char[TEST_DATA_LEN + 1];
1359762338dSopenharmony_ci
1369762338dSopenharmony_ci    ret = pipe(pipefd);
1379762338dSopenharmony_ci    EXPECT_TRUE(ret == 0);
1389762338dSopenharmony_ci    ret = writev(pipefd[1], vec, VEC_LEN);
1399762338dSopenharmony_ci    EXPECT_TRUE(ret == TEST_DATA_LEN);
1409762338dSopenharmony_ci
1419762338dSopenharmony_ci    ret = read(pipefd[0], readBuf, TEST_DATA_LEN);
1429762338dSopenharmony_ci    EXPECT_TRUE(ret == TEST_DATA_LEN);
1439762338dSopenharmony_ci    readBuf[TEST_DATA_LEN] = '\0';
1449762338dSopenharmony_ci    ret = strcmp(readBuf, TEST_DATA);
1459762338dSopenharmony_ci    EXPECT_TRUE(ret == 0);
1469762338dSopenharmony_ci
1479762338dSopenharmony_ci    close(pipefd[0]);
1489762338dSopenharmony_ci    close(pipefd[1]);
1499762338dSopenharmony_ci    delete[] iovBuf1;
1509762338dSopenharmony_ci    delete[] iovBuf2;
1519762338dSopenharmony_ci    delete[] readBuf;
1529762338dSopenharmony_ci}
1539762338dSopenharmony_ci
1549762338dSopenharmony_ci
1559762338dSopenharmony_ci/*
1569762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_WRITEV_0300
1579762338dSopenharmony_ci * @tc.name   : WritevInvalidVecLenFail_0003
1589762338dSopenharmony_ci * @tc.desc   : Writv write with invalid iovec length fail.
1599762338dSopenharmony_ci * @tc.size   : MediumTest
1609762338dSopenharmony_ci * @tc.type   : Function
1619762338dSopenharmony_ci * @tc.level  : Level 2
1629762338dSopenharmony_ci */
1639762338dSopenharmony_ciHWTEST_F(HatsWritevTest, WritevInvalidVecLenFail_0003, Function | MediumTest | Level2)
1649762338dSopenharmony_ci{
1659762338dSopenharmony_ci    int fd;
1669762338dSopenharmony_ci    struct iovec vec[VEC_LEN];
1679762338dSopenharmony_ci    int ret;
1689762338dSopenharmony_ci    int newVecLen;
1699762338dSopenharmony_ci    char *iovBuf1 = nullptr;
1709762338dSopenharmony_ci    char *iovBuf2 = nullptr;
1719762338dSopenharmony_ci    iovBuf1 = new char[BUF1_LEN];
1729762338dSopenharmony_ci    ret = memcpy_s(iovBuf1, BUF1_LEN, TEST_DATA_BUF1, BUF1_LEN);
1739762338dSopenharmony_ci    iovBuf2 = new char[BUF2_LEN];
1749762338dSopenharmony_ci    ret = memcpy_s(iovBuf2, BUF2_LEN, TEST_DATA_BUF2, BUF2_LEN);
1759762338dSopenharmony_ci    vec[0].iov_base = iovBuf1;
1769762338dSopenharmony_ci    vec[0].iov_len = BUF1_LEN;
1779762338dSopenharmony_ci    vec[1].iov_base = iovBuf2;
1789762338dSopenharmony_ci    vec[1].iov_len = BUF2_LEN;
1799762338dSopenharmony_ci
1809762338dSopenharmony_ci    fd = open(WRITEV_TEST_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1819762338dSopenharmony_ci    EXPECT_TRUE(fd >= 3);
1829762338dSopenharmony_ci
1839762338dSopenharmony_ci    newVecLen = -1;
1849762338dSopenharmony_ci    errno = 0;
1859762338dSopenharmony_ci    ret = writev(fd, vec, newVecLen);
1869762338dSopenharmony_ci    EXPECT_TRUE(ret == -1);
1879762338dSopenharmony_ci    EXPECT_EQ(errno, EINVAL);
1889762338dSopenharmony_ci
1899762338dSopenharmony_ci    close(fd);
1909762338dSopenharmony_ci    delete[] iovBuf1;
1919762338dSopenharmony_ci    delete[] iovBuf2;
1929762338dSopenharmony_ci}
1939762338dSopenharmony_ci
1949762338dSopenharmony_ci/*
1959762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_WRITEV_0400
1969762338dSopenharmony_ci * @tc.name   : WritevInvalidFdFail_0004
1979762338dSopenharmony_ci * @tc.desc   : writev write data into invalid fd fail.
1989762338dSopenharmony_ci * @tc.size   : MediumTest
1999762338dSopenharmony_ci * @tc.type   : Function
2009762338dSopenharmony_ci * @tc.level  : Level 2
2019762338dSopenharmony_ci */
2029762338dSopenharmony_ciHWTEST_F(HatsWritevTest, WritevInvalidFdFail_0004, Function | MediumTest | Level2)
2039762338dSopenharmony_ci{
2049762338dSopenharmony_ci    int fd;
2059762338dSopenharmony_ci    struct iovec vec[VEC_LEN];
2069762338dSopenharmony_ci    int ret;
2079762338dSopenharmony_ci    char *iovBuf1 = nullptr;
2089762338dSopenharmony_ci    char *iovBuf2 = nullptr;
2099762338dSopenharmony_ci    iovBuf1 = new char[BUF1_LEN];
2109762338dSopenharmony_ci    ret = memcpy_s(iovBuf1, BUF1_LEN, TEST_DATA_BUF1, BUF1_LEN);
2119762338dSopenharmony_ci    iovBuf2 = new char[BUF2_LEN];
2129762338dSopenharmony_ci    ret = memcpy_s(iovBuf2, BUF2_LEN, TEST_DATA_BUF2, BUF2_LEN);
2139762338dSopenharmony_ci    vec[0].iov_base = iovBuf1;
2149762338dSopenharmony_ci    vec[0].iov_len = BUF1_LEN;
2159762338dSopenharmony_ci    vec[1].iov_base = iovBuf2;
2169762338dSopenharmony_ci    vec[1].iov_len = BUF2_LEN;
2179762338dSopenharmony_ci
2189762338dSopenharmony_ci    fd = -1;
2199762338dSopenharmony_ci    errno = 0;
2209762338dSopenharmony_ci    ret = writev(fd, vec, VEC_LEN);
2219762338dSopenharmony_ci    EXPECT_TRUE(ret == -1);
2229762338dSopenharmony_ci    EXPECT_EQ(errno, EBADF);
2239762338dSopenharmony_ci
2249762338dSopenharmony_ci    delete[] iovBuf1;
2259762338dSopenharmony_ci    delete[] iovBuf2;
2269762338dSopenharmony_ci}
2279762338dSopenharmony_ci
2289762338dSopenharmony_ci/*
2299762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_WRITEV_0500
2309762338dSopenharmony_ci * @tc.name   : WritevFdNotInWriteModeFail_0005
2319762338dSopenharmony_ci * @tc.desc   : writev fd is not in write mode fail.
2329762338dSopenharmony_ci * @tc.size   : MediumTest
2339762338dSopenharmony_ci * @tc.type   : Function
2349762338dSopenharmony_ci * @tc.level  : Level 2
2359762338dSopenharmony_ci */
2369762338dSopenharmony_ciHWTEST_F(HatsWritevTest, WritevFdNotInWriteModeFail_0005, Function | MediumTest | Level2)
2379762338dSopenharmony_ci{
2389762338dSopenharmony_ci    int fd;
2399762338dSopenharmony_ci    struct iovec vec[VEC_LEN];
2409762338dSopenharmony_ci    int ret;
2419762338dSopenharmony_ci    char *iovBuf1 = nullptr;
2429762338dSopenharmony_ci    char *iovBuf2 = nullptr;
2439762338dSopenharmony_ci    iovBuf1 = new char[BUF1_LEN];
2449762338dSopenharmony_ci    ret = memcpy_s(iovBuf1, BUF1_LEN, TEST_DATA_BUF1, BUF1_LEN);
2459762338dSopenharmony_ci    iovBuf2 = new char[BUF2_LEN];
2469762338dSopenharmony_ci    ret = memcpy_s(iovBuf2, BUF2_LEN, TEST_DATA_BUF2, BUF2_LEN);
2479762338dSopenharmony_ci    vec[0].iov_base = iovBuf1;
2489762338dSopenharmony_ci    vec[0].iov_len = BUF1_LEN;
2499762338dSopenharmony_ci    vec[1].iov_base = iovBuf2;
2509762338dSopenharmony_ci    vec[1].iov_len = BUF2_LEN;
2519762338dSopenharmony_ci
2529762338dSopenharmony_ci    fd = open(WRITEV_TEST_FILE, O_RDONLY | O_CREAT | O_TRUNC, 0644);
2539762338dSopenharmony_ci    EXPECT_TRUE(fd >= 3);
2549762338dSopenharmony_ci    errno = 0;
2559762338dSopenharmony_ci    ret = writev(fd, vec, VEC_LEN);
2569762338dSopenharmony_ci    EXPECT_TRUE(ret == -1);
2579762338dSopenharmony_ci    EXPECT_EQ(errno, EBADF);
2589762338dSopenharmony_ci    close(fd);
2599762338dSopenharmony_ci
2609762338dSopenharmony_ci    fd = open(WRITEV_TEST_FILE, O_APPEND | O_CREAT | O_TRUNC, 0644);
2619762338dSopenharmony_ci    EXPECT_TRUE(fd >= 3);
2629762338dSopenharmony_ci    errno = 0;
2639762338dSopenharmony_ci    ret = writev(fd, vec, VEC_LEN);
2649762338dSopenharmony_ci    EXPECT_TRUE(ret == -1);
2659762338dSopenharmony_ci    EXPECT_EQ(errno, EBADF);
2669762338dSopenharmony_ci    close(fd);
2679762338dSopenharmony_ci
2689762338dSopenharmony_ci    delete[] iovBuf1;
2699762338dSopenharmony_ci    delete[] iovBuf2;
2709762338dSopenharmony_ci}
271