Add ftruncate test for writeable fd but no write permissions.

PiperOrigin-RevId: 332907453
This commit is contained in:
Dean Deng
2020-09-21 12:30:53 -07:00
committed by gVisor bot
parent e09d78f016
commit d72022373f
+20
View File
@@ -196,6 +196,26 @@ TEST(TruncateTest, FtruncateNonWriteable) {
EXPECT_THAT(ftruncate(fd.get(), 0), SyscallFailsWithErrno(EINVAL));
}
// ftruncate(2) should succeed as long as the file descriptor is writeable,
// regardless of whether the file permissions allow writing.
TEST(TruncateTest, FtruncateWithoutWritePermission_NoRandomSave) {
// Drop capabilities that allow us to override file permissions.
ASSERT_NO_ERRNO(SetCapability(CAP_DAC_OVERRIDE, false));
// The only time we can open a file with flags forbidden by its permissions
// is when we are creating the file. We cannot re-open with the same flags,
// so we cannot restore an fd obtained from such an operation.
const DisableSave ds;
auto path = NewTempAbsPath();
const FileDescriptor fd =
ASSERT_NO_ERRNO_AND_VALUE(Open(path, O_RDWR | O_CREAT, 0444));
// In goferfs, ftruncate may be converted to a remote truncate operation that
// unavoidably requires write permission.
SKIP_IF(IsRunningOnGvisor() && !ASSERT_NO_ERRNO_AND_VALUE(IsTmpfs(path)));
ASSERT_THAT(ftruncate(fd.get(), 100), SyscallSucceeds());
}
TEST(TruncateTest, TruncateNonExist) {
EXPECT_THAT(truncate("/foo/bar", 0), SyscallFailsWithErrno(ENOENT));
}