1/*
2 * Copyright (C) 2024 HiHope Open Source Organization.
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 <cerrno>
17#include <cstdio>
18#include <cstdlib>
19#include <csignal>
20#include <string>
21#include <vector>
22#include <fcntl.h>
23#include <unistd.h>
24#include <malloc.h>
25#include <arpa/inet.h>
26#include <gtest/gtest.h>
27#include <netinet/in.h>
28#include <sys/mman.h>
29#include <sys/stat.h>
30#include <sys/socket.h>
31#include <sys/uio.h>
32#include <sys/types.h>
33#include "securec.h"
34
35using namespace testing::ext;
36
37class HatsProcessvmTest : public testing::Test {
38public:
39    static void SetUpTestCase();
40    static void TearDownTestCase();
41    void SetUp();
42    void TearDown();
43private:
44};
45void HatsProcessvmTest::SetUp()
46{
47}
48
49void HatsProcessvmTest::TearDown()
50{
51}
52
53void HatsProcessvmTest::SetUpTestCase()
54{
55}
56
57void HatsProcessvmTest::TearDownTestCase()
58{
59}
60
61/*
62 * @tc.number : SUB_KERNEL_SYSCALL_PROCESSVM_0100
63 * @tc.name   : ProcessvmReadSuccess_0001
64 * @tc.desc   : process_vm_readv reads data successfully.
65 * @tc.size   : MediumTest
66 * @tc.type   : Function
67 * @tc.level  : Level 1
68 */
69HWTEST_F(HatsProcessvmTest, ProcessvmReadSuccess_0001, Function | MediumTest | Level1)
70{
71    pid_t pid = getpid();
72    struct iovec local[2];
73    struct iovec remote[1];
74
75    char buf1[1024];
76    char buf2[1024];
77    local[0].iov_base = buf1;
78    local[0].iov_len = sizeof(buf1);
79    local[1].iov_base = buf2;
80    local[1].iov_len = sizeof(buf2);
81
82    char remoteData[] = "Hello, World!";
83    remote[0].iov_base = remoteData;
84    remote[0].iov_len = sizeof(remoteData);
85
86    ssize_t nread = process_vm_readv(pid, local, 2, remote, 1, 0);
87    EXPECT_NE(nread, -1);
88    EXPECT_EQ(memcmp(buf1, remoteData, sizeof(remoteData)), 0);
89}
90
91/*
92 * @tc.number : SUB_KERNEL_SYSCALL_PROCESSVM_0200
93 * @tc.name   : ProcessvmWriteSuccess_0002
94 * @tc.desc   : process_vm_writev writes data successfully.
95 * @tc.size   : MediumTest
96 * @tc.type   : Function
97 * @tc.level  : Level 1
98 */
99HWTEST_F(HatsProcessvmTest, ProcessvmWriteSuccess_0002, Function | MediumTest | Level1)
100{
101    pid_t pid = getpid();
102    struct iovec local[2];
103    struct iovec remote[1];
104
105    char writeData1[] = "Hello, ";
106    char writeData2[] = "World!";
107    local[0].iov_base = writeData1;
108    local[0].iov_len = sizeof(writeData1) - 1;
109    local[1].iov_base = writeData2;
110    local[1].iov_len = sizeof(writeData2) - 1;
111
112    char remoteData[1024] = {0};
113    remote[0].iov_base = remoteData;
114    remote[0].iov_len = sizeof(remoteData);
115
116    ssize_t len = process_vm_writev(pid, local, 2, remote, 1, 0);
117    EXPECT_NE(len, -1);
118    EXPECT_EQ(strcmp(remoteData, "Hello, World!"), 0);
119}
120