blob: 97ba5d4dd9b601b5969790e78a1bf4f474c05e50 [file] [log] [blame]
Elliott Hughesbe57a402015-06-10 17:24:20 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
Elliott Hughescf59e192021-10-13 18:25:21 -070019#include <errno.h>
20#include <string.h>
21#include <sys/types.h>
Elliott Hughesbe57a402015-06-10 17:24:20 -070022#include <sys/uio.h>
Elliott Hughescf59e192021-10-13 18:25:21 -070023#include <unistd.h>
Elliott Hughesbe57a402015-06-10 17:24:20 -070024
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080025#include <android-base/file.h>
Elliott Hughes6f4594d2015-08-26 13:27:43 -070026
Elliott Hughes95646e62023-09-21 14:11:19 -070027#include "utils.h"
28
Elliott Hughes6f4594d2015-08-26 13:27:43 -070029TEST(sys_uio, readv_writev) {
30 TemporaryFile tf;
31
32 char buf1[] = "hello";
33 char buf2[] = "world";
34 iovec ios[] = { { buf1, 5 }, { buf2, 5 } };
35
36 ASSERT_EQ(10, writev(tf.fd, ios, 2));
37
38 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET));
39
40 memset(buf1, '1', sizeof(buf1));
41 memset(buf2, '2', sizeof(buf2));
42
43 ASSERT_EQ(10, readv(tf.fd, ios, 2));
44 buf1[5] = buf2[5] = '\0';
45 ASSERT_STREQ("hello", buf1);
46 ASSERT_STREQ("world", buf2);
47}
48
49template <typename ReadFn, typename WriteFn>
50void TestPreadVPwriteV(ReadFn read_fn, WriteFn write_fn) {
51 TemporaryFile tf;
52
53 char buf[] = "world";
54 iovec ios[] = { { buf, 5 } };
55
56 ASSERT_EQ(5, write_fn(tf.fd, ios, 1, 5));
57 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_CUR));
58
59 strcpy(buf, "hello");
60 ASSERT_EQ(5, write_fn(tf.fd, ios, 1, 0));
61 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_CUR));
62
63 ASSERT_EQ(5, read_fn(tf.fd, ios, 1, 5));
64 ASSERT_STREQ("world", buf);
65 ASSERT_EQ(5, read_fn(tf.fd, ios, 1, 0));
66 ASSERT_STREQ("hello", buf);
67}
68
69TEST(sys_uio, preadv_pwritev) {
70 TestPreadVPwriteV(preadv, pwritev);
71}
72
73TEST(sys_uio, preadv64_pwritev64) {
74 TestPreadVPwriteV(preadv64, pwritev64);
75}
76
Elliott Hughescf59e192021-10-13 18:25:21 -070077template <typename ReadFn, typename WriteFn>
78void TestPreadV2PwriteV2(ReadFn read_fn, WriteFn write_fn) {
79 TemporaryFile tf;
80
81 char buf[] = "world";
82 iovec ios[] = {{buf, 5}};
83
84 ASSERT_EQ(5, write_fn(tf.fd, ios, 1, 5, 0)) << strerror(errno);
85 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_CUR));
86
87 strcpy(buf, "hello");
88 ASSERT_EQ(5, write_fn(tf.fd, ios, 1, 0, 0)) << strerror(errno);
89 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_CUR));
90
91 ASSERT_EQ(5, read_fn(tf.fd, ios, 1, 5, 0)) << strerror(errno);
92 ASSERT_STREQ("world", buf);
93 ASSERT_EQ(5, read_fn(tf.fd, ios, 1, 0, 0)) << strerror(errno);
94 ASSERT_STREQ("hello", buf);
95}
96
97TEST(sys_uio, preadv2_pwritev2) {
98#if defined(__BIONIC__)
99 TestPreadV2PwriteV2(preadv2, pwritev2);
100#else
101 GTEST_SKIP() << "preadv2/pwritev2 not available";
102#endif
103}
104
105TEST(sys_uio, preadv64v2_pwritev64v2) {
106#if defined(__BIONIC__)
107 TestPreadV2PwriteV2(preadv64v2, pwritev64v2);
108#else
109 GTEST_SKIP() << "preadv2/pwritev2 not available";
110#endif
111}
112
Elliott Hughes6f4594d2015-08-26 13:27:43 -0700113TEST(sys_uio, process_vm_readv) {
Elliott Hughesbe57a402015-06-10 17:24:20 -0700114 ASSERT_EQ(0, process_vm_readv(0, nullptr, 0, nullptr, 0, 0));
Pavel Labathcc441002017-01-12 11:56:39 +0000115
116 // Test that we can read memory from our own process
117 char src[1024] = "This is the source buffer containing some data";
118 char dst[1024] = "";
119 iovec remote = { src, sizeof src };
120 iovec local = { dst, sizeof dst };
121 ASSERT_EQ(ssize_t(sizeof src), process_vm_readv(getpid(), &local, 1, &remote, 1, 0));
122 // Check whether data was copied (in the correct direction)
123 ASSERT_EQ('T', dst[0]);
124 ASSERT_EQ(0, memcmp(src, dst, sizeof src));
125
126 // Reading from non-allocated memory should return an error
127 remote = { nullptr, sizeof dst };
Elliott Hughes95646e62023-09-21 14:11:19 -0700128 errno = 0;
Pavel Labathcc441002017-01-12 11:56:39 +0000129 ASSERT_EQ(-1, process_vm_readv(getpid(), &local, 1, &remote, 1, 0));
Elliott Hughes95646e62023-09-21 14:11:19 -0700130 ASSERT_ERRNO(EFAULT);
Elliott Hughesbe57a402015-06-10 17:24:20 -0700131}
132
Elliott Hughes6f4594d2015-08-26 13:27:43 -0700133TEST(sys_uio, process_vm_writev) {
Elliott Hughesbe57a402015-06-10 17:24:20 -0700134 ASSERT_EQ(0, process_vm_writev(0, nullptr, 0, nullptr, 0, 0));
Pavel Labathcc441002017-01-12 11:56:39 +0000135
136 // Test that we can read memory from our own process
137 char src[1024] = "This is the source buffer containing some data";
138 char dst[1024] = "";
139 iovec remote = { dst, sizeof dst };
140 iovec local = { src, sizeof src };
141 ASSERT_EQ(ssize_t(sizeof src), process_vm_writev(getpid(), &local, 1, &remote, 1, 0));
142 // Check whether data was copied (in the correct direction)
143 ASSERT_EQ('T', dst[0]);
144 ASSERT_EQ(0, memcmp(src, dst, sizeof src));
145
146 // Writing to non-allocated memory should return an error
147 remote = { nullptr, sizeof dst };
Elliott Hughes95646e62023-09-21 14:11:19 -0700148 errno = 0;
Pavel Labathcc441002017-01-12 11:56:39 +0000149 ASSERT_EQ(-1, process_vm_writev(getpid(), &local, 1, &remote, 1, 0));
Elliott Hughes95646e62023-09-21 14:11:19 -0700150 ASSERT_ERRNO(EFAULT);
Elliott Hughesbe57a402015-06-10 17:24:20 -0700151}