github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/sync.cc (about) 1 // Copyright 2018 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include <fcntl.h> 16 #include <stdio.h> 17 #include <sys/syscall.h> 18 #include <unistd.h> 19 20 #include <string> 21 22 #include "gtest/gtest.h" 23 #include "test/util/temp_path.h" 24 #include "test/util/test_util.h" 25 26 namespace gvisor { 27 namespace testing { 28 29 namespace { 30 31 TEST(SyncTest, SyncEverything) { 32 ASSERT_THAT(syscall(SYS_sync), SyscallSucceeds()); 33 } 34 35 TEST(SyncTest, SyncFileSytem) { 36 int fd; 37 auto f = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile()); 38 ASSERT_THAT(fd = open(f.path().c_str(), O_RDONLY), SyscallSucceeds()); 39 EXPECT_THAT(syncfs(fd), SyscallSucceeds()); 40 EXPECT_THAT(close(fd), SyscallSucceeds()); 41 } 42 43 TEST(SyncTest, SyncFromPipe) { 44 int pipes[2]; 45 EXPECT_THAT(pipe(pipes), SyscallSucceeds()); 46 EXPECT_THAT(syncfs(pipes[0]), SyscallSucceeds()); 47 EXPECT_THAT(syncfs(pipes[1]), SyscallSucceeds()); 48 EXPECT_THAT(close(pipes[0]), SyscallSucceeds()); 49 EXPECT_THAT(close(pipes[1]), SyscallSucceeds()); 50 } 51 52 TEST(SyncTest, CannotSyncFileSystemAtBadFd) { 53 EXPECT_THAT(syncfs(-1), SyscallFailsWithErrno(EBADF)); 54 } 55 56 TEST(SyncTest, CannotSyncFileSystemAtOpathFD) { 57 SKIP_IF(IsRunningWithVFS1()); 58 59 const TempPath file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith( 60 GetAbsoluteTestTmpdir(), "", TempPath::kDefaultFileMode)); 61 const FileDescriptor fd = 62 ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_PATH)); 63 64 EXPECT_THAT(syncfs(fd.get()), SyscallFailsWithErrno(EBADF)); 65 } 66 } // namespace 67 68 } // namespace testing 69 } // namespace gvisor