summaryrefslogtreecommitdiffhomepage
path: root/test/syscalls/linux/preadv2.cc
diff options
context:
space:
mode:
authorZach Koopmans <zkoopmans@google.com>2018-12-19 13:14:53 -0800
committerShentubot <shentubot@google.com>2018-12-19 13:16:06 -0800
commitff7178a4d10f9f1fb34e54fed5ef27cfbff5d6f9 (patch)
treeb49fb1a2b4cbb1291f41502a7494a1d56a395a87 /test/syscalls/linux/preadv2.cc
parent898838e34d1b0c76405f3e7f7f5fa7f1a444da0e (diff)
Implement pwritev2.
Implement pwritev2 and associated unit tests. Clean up preadv2 unit tests. Tag RWF_ flags in both preadv2 and pwritev2 with associated bug tickets. PiperOrigin-RevId: 226222119 Change-Id: Ieb22672418812894ba114bbc88e67f1dd50de620
Diffstat (limited to 'test/syscalls/linux/preadv2.cc')
-rw-r--r--test/syscalls/linux/preadv2.cc156
1 files changed, 109 insertions, 47 deletions
diff --git a/test/syscalls/linux/preadv2.cc b/test/syscalls/linux/preadv2.cc
index 642eed624..58a4f9224 100644
--- a/test/syscalls/linux/preadv2.cc
+++ b/test/syscalls/linux/preadv2.cc
@@ -13,23 +13,18 @@
// limitations under the License.
#include <fcntl.h>
-#include <stdlib.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/uio.h>
-#include <sys/wait.h>
-#include <unistd.h>
-#include <atomic>
#include <string>
#include <vector>
#include "gtest/gtest.h"
#include "gtest/gtest.h"
+#include "absl/memory/memory.h"
#include "test/syscalls/linux/file_base.h"
-#include "test/syscalls/linux/readv_common.h"
#include "test/util/file_descriptor.h"
-#include "test/util/memory_util.h"
#include "test/util/temp_path.h"
#include "test/util/test_util.h"
@@ -60,11 +55,17 @@ std::string SetContent() {
return content;
}
+ssize_t preadv2(unsigned long fd, const struct iovec* iov, unsigned long iovcnt,
+ off_t offset, unsigned long flags) {
+ // syscall on preadv2 does some weird things (see man syscall and search
+ // preadv2), so we insert a 0 to word align the flags argument on native.
+ return syscall(SYS_preadv2, fd, iov, iovcnt, offset, 0, flags);
+}
+
// This test is the base case where we call preadv (no offset, no flags).
TEST(Preadv2Test, TestBaseCall) {
- if (!IsRunningOnGvisor()) {
- SKIP_BEFORE_KERNEL(/*major_version=*/4, /*minor_version=*/6);
- }
+ SKIP_IF(preadv2(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS);
+
std::string content = SetContent();
const TempPath file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
@@ -73,12 +74,13 @@ TEST(Preadv2Test, TestBaseCall) {
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDONLY));
std::vector<char> buf(kBufSize);
- struct iovec iov;
- iov.iov_base = buf.data();
- iov.iov_len = buf.size();
+ struct iovec iov[2];
+ iov[0].iov_base = buf.data();
+ iov[0].iov_len = buf.size() / 2;
+ iov[1].iov_base = static_cast<char*>(iov[0].iov_base) + (content.size() / 2);
+ iov[1].iov_len = content.size() / 2;
- EXPECT_THAT(syscall(SYS_preadv2, fd.get(), &iov, /*iov_cnt*/ 1,
- /*offset=*/0, /*flags=*/0),
+ EXPECT_THAT(preadv2(fd.get(), iov, /*iovcnt*/ 2, /*offset=*/0, /*flags=*/0),
SyscallSucceedsWithValue(kBufSize));
EXPECT_EQ(content, std::string(buf.data(), buf.size()));
@@ -86,9 +88,8 @@ TEST(Preadv2Test, TestBaseCall) {
// This test is where we call preadv with an offset and no flags.
TEST(Preadv2Test, TestValidPositiveOffset) {
- if (!IsRunningOnGvisor()) {
- SKIP_BEFORE_KERNEL(/*major_version=*/4, /*minor_version=*/6);
- }
+ SKIP_IF(preadv2(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS);
+
std::string content = SetContent();
const std::string prefix = "0";
@@ -102,10 +103,12 @@ TEST(Preadv2Test, TestValidPositiveOffset) {
iov.iov_base = buf.data();
iov.iov_len = buf.size();
- EXPECT_THAT(syscall(SYS_preadv2, fd.get(), &iov, /*iov_cnt=*/1,
- /*offset=*/prefix.size(), /*flags=*/0),
+ EXPECT_THAT(preadv2(fd.get(), &iov, /*iovcnt=*/1, /*offset=*/prefix.size(),
+ /*flags=*/0),
SyscallSucceedsWithValue(kBufSize));
+ EXPECT_THAT(lseek(fd.get(), 0, SEEK_CUR), SyscallSucceedsWithValue(0));
+
EXPECT_EQ(content, std::string(buf.data(), buf.size()));
}
@@ -113,9 +116,8 @@ TEST(Preadv2Test, TestValidPositiveOffset) {
// read should use the file offset, so the test increments it by one prior to
// calling preadv2.
TEST(Preadv2Test, TestNegativeOneOffset) {
- if (!IsRunningOnGvisor()) {
- SKIP_BEFORE_KERNEL(/*major_version=*/4, /*minor_version=*/6);
- }
+ SKIP_IF(preadv2(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS);
+
std::string content = SetContent();
const std::string prefix = "231";
@@ -123,6 +125,7 @@ TEST(Preadv2Test, TestNegativeOneOffset) {
GetAbsoluteTestTmpdir(), prefix + content, TempPath::kDefaultFileMode));
const FileDescriptor fd =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDONLY));
+
ASSERT_THAT(lseek(fd.get(), prefix.size(), SEEK_SET),
SyscallSucceedsWithValue(prefix.size()));
@@ -131,79 +134,111 @@ TEST(Preadv2Test, TestNegativeOneOffset) {
iov.iov_base = buf.data();
iov.iov_len = buf.size();
- EXPECT_THAT(syscall(SYS_preadv2, fd.get(), &iov, /*iov_cnt=*/1,
- /*offset=*/static_cast<off_t>(-1), /*flags=*/0),
+ EXPECT_THAT(preadv2(fd.get(), &iov, /*iovcnt=*/1, /*offset=*/-1, /*flags=*/0),
SyscallSucceedsWithValue(kBufSize));
+ EXPECT_THAT(lseek(fd.get(), 0, SEEK_CUR),
+ SyscallSucceedsWithValue(prefix.size() + buf.size()));
+
EXPECT_EQ(content, std::string(buf.data(), buf.size()));
}
+// preadv2 requires if the RWF_HIPRI flag is passed, the fd must be opened with
+// O_DIRECT. This test implements a correct call with the RWF_HIPRI flag.
+TEST(Preadv2Test, TestCallWithRWF_HIPRI) {
+ SKIP_IF(preadv2(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS);
+
+ std::string content = SetContent();
+
+ const TempPath file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
+ GetAbsoluteTestTmpdir(), content, TempPath::kDefaultFileMode));
+ const FileDescriptor fd =
+ ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDONLY));
+
+ EXPECT_THAT(fsync(fd.get()), SyscallSucceeds());
+
+ std::vector<char> buf(kBufSize, '0');
+ struct iovec iov;
+ iov.iov_base = buf.data();
+ iov.iov_len = buf.size();
+
+ EXPECT_THAT(
+ preadv2(fd.get(), &iov, /*iovcnt=*/1, /*offset=*/0, /*flags=*/RWF_HIPRI),
+ SyscallSucceedsWithValue(kBufSize));
+
+ EXPECT_THAT(lseek(fd.get(), 0, SEEK_CUR), SyscallSucceedsWithValue(0));
+
+ EXPECT_EQ(content, std::string(buf.data(), buf.size()));
+}
// This test calls preadv2 with an invalid flag.
TEST(Preadv2Test, TestInvalidFlag) {
- if (!IsRunningOnGvisor()) {
- SKIP_BEFORE_KERNEL(/*major_version=*/4, /*minor_version=*/6);
- }
+ SKIP_IF(preadv2(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS);
const TempPath file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
GetAbsoluteTestTmpdir(), "", TempPath::kDefaultFileMode));
const FileDescriptor fd =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDONLY | O_DIRECT));
+ std::vector<char> buf(kBufSize, '0');
struct iovec iov;
+ iov.iov_base = buf.data();
+ iov.iov_len = buf.size();
- EXPECT_THAT(syscall(SYS_preadv2, fd.get(), &iov, /*iov_cnt=*/1,
- /*offset=*/0, /*flags=*/RWF_HIPRI << 1),
- SyscallFailsWithErrno(EINVAL));
+ EXPECT_THAT(preadv2(fd.get(), &iov, /*iovcnt=*/1,
+ /*offset=*/0, /*flags=*/0xF0),
+ SyscallFailsWithErrno(EOPNOTSUPP));
}
// This test calls preadv2 with an invalid offset.
TEST(Preadv2Test, TestInvalidOffset) {
- if (!IsRunningOnGvisor()) {
- SKIP_BEFORE_KERNEL(/*major_version=*/4, /*minor_version=*/6);
- }
+ SKIP_IF(preadv2(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS);
const TempPath file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
GetAbsoluteTestTmpdir(), "", TempPath::kDefaultFileMode));
const FileDescriptor fd =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDONLY | O_DIRECT));
- struct iovec iov;
- EXPECT_THAT(syscall(SYS_preadv2, fd.get(), &iov, /*iov_cnt=*/1,
- /*offset=*/static_cast<off_t>(-8), /*flags=*/RWF_HIPRI),
+ auto iov = absl::make_unique<struct iovec[]>(1);
+ iov[0].iov_base = nullptr;
+ iov[0].iov_len = 0;
+
+ EXPECT_THAT(preadv2(fd.get(), iov.get(), /*iovcnt=*/1, /*offset=*/-8,
+ /*flags=*/RWF_HIPRI),
SyscallFailsWithErrno(EINVAL));
}
// This test calls preadv with a file set O_WRONLY.
TEST(Preadv2Test, TestUnreadableFile) {
- if (!IsRunningOnGvisor()) {
- SKIP_BEFORE_KERNEL(/*major_version=*/4, /*minor_version=*/6);
- }
+ SKIP_IF(preadv2(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS);
const TempPath file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
GetAbsoluteTestTmpdir(), "", TempPath::kDefaultFileMode));
const FileDescriptor fd =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_WRONLY));
- struct iovec iov;
- EXPECT_THAT(syscall(SYS_preadv2, fd.get(), &iov, /*iov_cnt=*/1,
+ auto iov = absl::make_unique<struct iovec[]>(1);
+ iov[0].iov_base = nullptr;
+ iov[0].iov_len = 0;
+
+ EXPECT_THAT(preadv2(fd.get(), iov.get(), /*iovcnt=*/1,
/*offset=*/0, /*flags=*/0),
SyscallFailsWithErrno(EBADF));
}
// Calling preadv2 with a non-negative offset calls preadv. Calling preadv with
// an unseekable file is not allowed. A pipe is used for an unseekable file.
-TEST(Preadv2Test, TestUnseekableFile) {
- if (!IsRunningOnGvisor()) {
- SKIP_BEFORE_KERNEL(/*major_version=*/4, /*minor_version=*/6);
- }
+TEST(Preadv2Test, TestUnseekableFileInvalid) {
+ SKIP_IF(preadv2(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS);
int pipe_fds[2];
ASSERT_THAT(pipe(pipe_fds), SyscallSucceeds());
- struct iovec iov;
+ auto iov = absl::make_unique<struct iovec[]>(1);
+ iov[0].iov_base = nullptr;
+ iov[0].iov_len = 0;
- EXPECT_THAT(syscall(SYS_preadv2, pipe_fds[0], &iov, /*iov_cnt=*/1,
+ EXPECT_THAT(preadv2(pipe_fds[0], iov.get(), /*iovcnt=*/1,
/*offset=*/2, /*flags=*/0),
SyscallFailsWithErrno(ESPIPE));
@@ -211,6 +246,33 @@ TEST(Preadv2Test, TestUnseekableFile) {
EXPECT_THAT(close(pipe_fds[1]), SyscallSucceeds());
}
+TEST(Preadv2Test, TestUnseekableFileValid) {
+ SKIP_IF(preadv2(-1, nullptr, 0, 0, 0) < 0 && errno == ENOSYS);
+
+ int pipe_fds[2];
+
+ ASSERT_THAT(pipe(pipe_fds), SyscallSucceeds());
+
+ std::vector<char> content(32, 'X');
+
+ EXPECT_THAT(write(pipe_fds[1], content.data(), content.size()),
+ SyscallSucceedsWithValue(content.size()));
+
+ std::vector<char> buf(content.size());
+ auto iov = absl::make_unique<struct iovec[]>(1);
+ iov[0].iov_base = buf.data();
+ iov[0].iov_len = buf.size();
+
+ EXPECT_THAT(preadv2(pipe_fds[0], iov.get(), /*iovcnt=*/1,
+ /*offset=*/static_cast<off_t>(-1), /*flags=*/0),
+ SyscallSucceedsWithValue(buf.size()));
+
+ EXPECT_EQ(content, buf);
+
+ EXPECT_THAT(close(pipe_fds[0]), SyscallSucceeds());
+ EXPECT_THAT(close(pipe_fds[1]), SyscallSucceeds());
+}
+
} // namespace
} // namespace testing