Home | History | Annotate | Download | only in tests
      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 <sys/uio.h>
     20 
     21 #include "TemporaryFile.h"
     22 
     23 TEST(sys_uio, readv_writev) {
     24   TemporaryFile tf;
     25 
     26   char buf1[] = "hello";
     27   char buf2[] = "world";
     28   iovec ios[] = { { buf1, 5 }, { buf2, 5 } };
     29 
     30   ASSERT_EQ(10, writev(tf.fd, ios, 2));
     31 
     32   ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET));
     33 
     34   memset(buf1, '1', sizeof(buf1));
     35   memset(buf2, '2', sizeof(buf2));
     36 
     37   ASSERT_EQ(10, readv(tf.fd, ios, 2));
     38   buf1[5] = buf2[5] = '\0';
     39   ASSERT_STREQ("hello", buf1);
     40   ASSERT_STREQ("world", buf2);
     41 }
     42 
     43 template <typename ReadFn, typename WriteFn>
     44 void TestPreadVPwriteV(ReadFn read_fn, WriteFn write_fn) {
     45   TemporaryFile tf;
     46 
     47   char buf[] = "world";
     48   iovec ios[] = { { buf, 5 } };
     49 
     50   ASSERT_EQ(5, write_fn(tf.fd, ios, 1, 5));
     51   ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_CUR));
     52 
     53   strcpy(buf, "hello");
     54   ASSERT_EQ(5, write_fn(tf.fd, ios, 1, 0));
     55   ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_CUR));
     56 
     57   ASSERT_EQ(5, read_fn(tf.fd, ios, 1, 5));
     58   ASSERT_STREQ("world", buf);
     59   ASSERT_EQ(5, read_fn(tf.fd, ios, 1, 0));
     60   ASSERT_STREQ("hello", buf);
     61 }
     62 
     63 TEST(sys_uio, preadv_pwritev) {
     64   TestPreadVPwriteV(preadv, pwritev);
     65 }
     66 
     67 TEST(sys_uio, preadv64_pwritev64) {
     68   TestPreadVPwriteV(preadv64, pwritev64);
     69 }
     70 
     71 TEST(sys_uio, process_vm_readv) {
     72   ASSERT_EQ(0, process_vm_readv(0, nullptr, 0, nullptr, 0, 0));
     73 }
     74 
     75 TEST(sys_uio, process_vm_writev) {
     76   ASSERT_EQ(0, process_vm_writev(0, nullptr, 0, nullptr, 0, 0));
     77 }
     78