blob: aac08e764e95cde75b178785d7c4bb972c45dd77 [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
27TEST(sys_uio, readv_writev) {
28 TemporaryFile tf;
29
30 char buf1[] = "hello";
31 char buf2[] = "world";
32 iovec ios[] = { { buf1, 5 }, { buf2, 5 } };
33
34 ASSERT_EQ(10, writev(tf.fd, ios, 2));
35
36 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET));
37
38 memset(buf1, '1', sizeof(buf1));
39 memset(buf2, '2', sizeof(buf2));
40
41 ASSERT_EQ(10, readv(tf.fd, ios, 2));
42 buf1[5] = buf2[5] = '\0';
43 ASSERT_STREQ("hello", buf1);
44 ASSERT_STREQ("world", buf2);
45}
46
47template <typename ReadFn, typename WriteFn>
48void TestPreadVPwriteV(ReadFn read_fn, WriteFn write_fn) {
49 TemporaryFile tf;
50
51 char buf[] = "world";
52 iovec ios[] = { { buf, 5 } };
53
54 ASSERT_EQ(5, write_fn(tf.fd, ios, 1, 5));
55 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_CUR));
56
57 strcpy(buf, "hello");
58 ASSERT_EQ(5, write_fn(tf.fd, ios, 1, 0));
59 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_CUR));
60
61 ASSERT_EQ(5, read_fn(tf.fd, ios, 1, 5));
62 ASSERT_STREQ("world", buf);
63 ASSERT_EQ(5, read_fn(tf.fd, ios, 1, 0));
64 ASSERT_STREQ("hello", buf);
65}
66
67TEST(sys_uio, preadv_pwritev) {
68 TestPreadVPwriteV(preadv, pwritev);
69}
70
71TEST(sys_uio, preadv64_pwritev64) {
72 TestPreadVPwriteV(preadv64, pwritev64);
73}
74
Elliott Hughescf59e192021-10-13 18:25:21 -070075template <typename ReadFn, typename WriteFn>
76void TestPreadV2PwriteV2(ReadFn read_fn, WriteFn write_fn) {
77 TemporaryFile tf;
78
79 char buf[] = "world";
80 iovec ios[] = {{buf, 5}};
81
82 ASSERT_EQ(5, write_fn(tf.fd, ios, 1, 5, 0)) << strerror(errno);
83 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_CUR));
84
85 strcpy(buf, "hello");
86 ASSERT_EQ(5, write_fn(tf.fd, ios, 1, 0, 0)) << strerror(errno);
87 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_CUR));
88
89 ASSERT_EQ(5, read_fn(tf.fd, ios, 1, 5, 0)) << strerror(errno);
90 ASSERT_STREQ("world", buf);
91 ASSERT_EQ(5, read_fn(tf.fd, ios, 1, 0, 0)) << strerror(errno);
92 ASSERT_STREQ("hello", buf);
93}
94
95TEST(sys_uio, preadv2_pwritev2) {
96#if defined(__BIONIC__)
97 TestPreadV2PwriteV2(preadv2, pwritev2);
98#else
99 GTEST_SKIP() << "preadv2/pwritev2 not available";
100#endif
101}
102
103TEST(sys_uio, preadv64v2_pwritev64v2) {
104#if defined(__BIONIC__)
105 TestPreadV2PwriteV2(preadv64v2, pwritev64v2);
106#else
107 GTEST_SKIP() << "preadv2/pwritev2 not available";
108#endif
109}
110
Elliott Hughes6f4594d2015-08-26 13:27:43 -0700111TEST(sys_uio, process_vm_readv) {
Elliott Hughesbe57a402015-06-10 17:24:20 -0700112 ASSERT_EQ(0, process_vm_readv(0, nullptr, 0, nullptr, 0, 0));
Pavel Labathcc441002017-01-12 11:56:39 +0000113
114 // Test that we can read memory from our own process
115 char src[1024] = "This is the source buffer containing some data";
116 char dst[1024] = "";
117 iovec remote = { src, sizeof src };
118 iovec local = { dst, sizeof dst };
119 ASSERT_EQ(ssize_t(sizeof src), process_vm_readv(getpid(), &local, 1, &remote, 1, 0));
120 // Check whether data was copied (in the correct direction)
121 ASSERT_EQ('T', dst[0]);
122 ASSERT_EQ(0, memcmp(src, dst, sizeof src));
123
124 // Reading from non-allocated memory should return an error
125 remote = { nullptr, sizeof dst };
126 ASSERT_EQ(-1, process_vm_readv(getpid(), &local, 1, &remote, 1, 0));
127 ASSERT_EQ(EFAULT, errno);
Elliott Hughesbe57a402015-06-10 17:24:20 -0700128}
129
Elliott Hughes6f4594d2015-08-26 13:27:43 -0700130TEST(sys_uio, process_vm_writev) {
Elliott Hughesbe57a402015-06-10 17:24:20 -0700131 ASSERT_EQ(0, process_vm_writev(0, nullptr, 0, nullptr, 0, 0));
Pavel Labathcc441002017-01-12 11:56:39 +0000132
133 // Test that we can read memory from our own process
134 char src[1024] = "This is the source buffer containing some data";
135 char dst[1024] = "";
136 iovec remote = { dst, sizeof dst };
137 iovec local = { src, sizeof src };
138 ASSERT_EQ(ssize_t(sizeof src), process_vm_writev(getpid(), &local, 1, &remote, 1, 0));
139 // Check whether data was copied (in the correct direction)
140 ASSERT_EQ('T', dst[0]);
141 ASSERT_EQ(0, memcmp(src, dst, sizeof src));
142
143 // Writing to non-allocated memory should return an error
144 remote = { nullptr, sizeof dst };
145 ASSERT_EQ(-1, process_vm_writev(getpid(), &local, 1, &remote, 1, 0));
146 ASSERT_EQ(EFAULT, errno);
Elliott Hughesbe57a402015-06-10 17:24:20 -0700147}