1 /*
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 
19 #include <errno.h>
20 #include <string.h>
21 #include <sys/types.h>
22 #include <sys/uio.h>
23 #include <unistd.h>
24 
25 #include <android-base/file.h>
26 
27 #include "utils.h"
28 
TEST(sys_uio,readv_writev)29 TEST(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 
49 template <typename ReadFn, typename WriteFn>
TestPreadVPwriteV(ReadFn read_fn,WriteFn write_fn)50 void 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 
TEST(sys_uio,preadv_pwritev)69 TEST(sys_uio, preadv_pwritev) {
70   TestPreadVPwriteV(preadv, pwritev);
71 }
72 
TEST(sys_uio,preadv64_pwritev64)73 TEST(sys_uio, preadv64_pwritev64) {
74   TestPreadVPwriteV(preadv64, pwritev64);
75 }
76 
77 template <typename ReadFn, typename WriteFn>
TestPreadV2PwriteV2(ReadFn read_fn,WriteFn write_fn)78 void 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 
TEST(sys_uio,preadv2_pwritev2)97 TEST(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 
TEST(sys_uio,preadv64v2_pwritev64v2)105 TEST(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 
TEST(sys_uio,process_vm_readv)113 TEST(sys_uio, process_vm_readv) {
114   ASSERT_EQ(0, process_vm_readv(0, nullptr, 0, nullptr, 0, 0));
115 
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 };
128   errno = 0;
129   ASSERT_EQ(-1, process_vm_readv(getpid(), &local, 1, &remote, 1, 0));
130   ASSERT_ERRNO(EFAULT);
131 }
132 
TEST(sys_uio,process_vm_writev)133 TEST(sys_uio, process_vm_writev) {
134   ASSERT_EQ(0, process_vm_writev(0, nullptr, 0, nullptr, 0, 0));
135 
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 };
148   errno = 0;
149   ASSERT_EQ(-1, process_vm_writev(getpid(), &local, 1, &remote, 1, 0));
150   ASSERT_ERRNO(EFAULT);
151 }
152