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