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 <csignal>
209762338dSopenharmony_ci#include <string>
219762338dSopenharmony_ci#include <vector>
229762338dSopenharmony_ci#include <fcntl.h>
239762338dSopenharmony_ci#include <unistd.h>
249762338dSopenharmony_ci#include <malloc.h>
259762338dSopenharmony_ci#include <arpa/inet.h>
269762338dSopenharmony_ci#include <gtest/gtest.h>
279762338dSopenharmony_ci#include <netinet/in.h>
289762338dSopenharmony_ci#include <sys/mman.h>
299762338dSopenharmony_ci#include <sys/stat.h>
309762338dSopenharmony_ci#include <sys/socket.h>
319762338dSopenharmony_ci#include <sys/uio.h>
329762338dSopenharmony_ci#include <sys/types.h>
339762338dSopenharmony_ci#include "securec.h"
349762338dSopenharmony_ci
359762338dSopenharmony_ciusing namespace testing::ext;
369762338dSopenharmony_ci
379762338dSopenharmony_ciclass HatsProcessvmTest : public testing::Test {
389762338dSopenharmony_cipublic:
399762338dSopenharmony_ci    static void SetUpTestCase();
409762338dSopenharmony_ci    static void TearDownTestCase();
419762338dSopenharmony_ci    void SetUp();
429762338dSopenharmony_ci    void TearDown();
439762338dSopenharmony_ciprivate:
449762338dSopenharmony_ci};
459762338dSopenharmony_civoid HatsProcessvmTest::SetUp()
469762338dSopenharmony_ci{
479762338dSopenharmony_ci}
489762338dSopenharmony_ci
499762338dSopenharmony_civoid HatsProcessvmTest::TearDown()
509762338dSopenharmony_ci{
519762338dSopenharmony_ci}
529762338dSopenharmony_ci
539762338dSopenharmony_civoid HatsProcessvmTest::SetUpTestCase()
549762338dSopenharmony_ci{
559762338dSopenharmony_ci}
569762338dSopenharmony_ci
579762338dSopenharmony_civoid HatsProcessvmTest::TearDownTestCase()
589762338dSopenharmony_ci{
599762338dSopenharmony_ci}
609762338dSopenharmony_ci
619762338dSopenharmony_ci/*
629762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_PROCESSVM_0100
639762338dSopenharmony_ci * @tc.name   : ProcessvmReadSuccess_0001
649762338dSopenharmony_ci * @tc.desc   : process_vm_readv reads data successfully.
659762338dSopenharmony_ci * @tc.size   : MediumTest
669762338dSopenharmony_ci * @tc.type   : Function
679762338dSopenharmony_ci * @tc.level  : Level 1
689762338dSopenharmony_ci */
699762338dSopenharmony_ciHWTEST_F(HatsProcessvmTest, ProcessvmReadSuccess_0001, Function | MediumTest | Level1)
709762338dSopenharmony_ci{
719762338dSopenharmony_ci    pid_t pid = getpid();
729762338dSopenharmony_ci    struct iovec local[2];
739762338dSopenharmony_ci    struct iovec remote[1];
749762338dSopenharmony_ci
759762338dSopenharmony_ci    char buf1[1024];
769762338dSopenharmony_ci    char buf2[1024];
779762338dSopenharmony_ci    local[0].iov_base = buf1;
789762338dSopenharmony_ci    local[0].iov_len = sizeof(buf1);
799762338dSopenharmony_ci    local[1].iov_base = buf2;
809762338dSopenharmony_ci    local[1].iov_len = sizeof(buf2);
819762338dSopenharmony_ci
829762338dSopenharmony_ci    char remoteData[] = "Hello, World!";
839762338dSopenharmony_ci    remote[0].iov_base = remoteData;
849762338dSopenharmony_ci    remote[0].iov_len = sizeof(remoteData);
859762338dSopenharmony_ci
869762338dSopenharmony_ci    ssize_t nread = process_vm_readv(pid, local, 2, remote, 1, 0);
879762338dSopenharmony_ci    EXPECT_NE(nread, -1);
889762338dSopenharmony_ci    EXPECT_EQ(memcmp(buf1, remoteData, sizeof(remoteData)), 0);
899762338dSopenharmony_ci}
909762338dSopenharmony_ci
919762338dSopenharmony_ci/*
929762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_PROCESSVM_0200
939762338dSopenharmony_ci * @tc.name   : ProcessvmWriteSuccess_0002
949762338dSopenharmony_ci * @tc.desc   : process_vm_writev writes data successfully.
959762338dSopenharmony_ci * @tc.size   : MediumTest
969762338dSopenharmony_ci * @tc.type   : Function
979762338dSopenharmony_ci * @tc.level  : Level 1
989762338dSopenharmony_ci */
999762338dSopenharmony_ciHWTEST_F(HatsProcessvmTest, ProcessvmWriteSuccess_0002, Function | MediumTest | Level1)
1009762338dSopenharmony_ci{
1019762338dSopenharmony_ci    pid_t pid = getpid();
1029762338dSopenharmony_ci    struct iovec local[2];
1039762338dSopenharmony_ci    struct iovec remote[1];
1049762338dSopenharmony_ci
1059762338dSopenharmony_ci    char writeData1[] = "Hello, ";
1069762338dSopenharmony_ci    char writeData2[] = "World!";
1079762338dSopenharmony_ci    local[0].iov_base = writeData1;
1089762338dSopenharmony_ci    local[0].iov_len = sizeof(writeData1) - 1;
1099762338dSopenharmony_ci    local[1].iov_base = writeData2;
1109762338dSopenharmony_ci    local[1].iov_len = sizeof(writeData2) - 1;
1119762338dSopenharmony_ci
1129762338dSopenharmony_ci    char remoteData[1024] = {0};
1139762338dSopenharmony_ci    remote[0].iov_base = remoteData;
1149762338dSopenharmony_ci    remote[0].iov_len = sizeof(remoteData);
1159762338dSopenharmony_ci
1169762338dSopenharmony_ci    ssize_t len = process_vm_writev(pid, local, 2, remote, 1, 0);
1179762338dSopenharmony_ci    EXPECT_NE(len, -1);
1189762338dSopenharmony_ci    EXPECT_EQ(strcmp(remoteData, "Hello, World!"), 0);
1199762338dSopenharmony_ci}
120