summaryrefslogtreecommitdiffhomepage
path: root/test/syscalls/linux
diff options
context:
space:
mode:
authorNicolas Lacasse <nlacasse@google.com>2019-04-18 12:38:29 -0700
committerShentubot <shentubot@google.com>2019-04-18 12:39:35 -0700
commitce64d9ebf03b1efac3e8c2b9ddafb02aafed9f33 (patch)
treed553e38cf4bdf137a296a59cb29be33d7cd84f16 /test/syscalls/linux
parent133700007a8495c7d8df53801b1d34345d6c5cf8 (diff)
Keep symlink target open while in test that compares inode ids.
Inode ids are only guaranteed to be stable across save/restore if the file is held open. This CL fixes a simple stat test to allow it to compare symlink and target by inode id, as long as the link target is held open. PiperOrigin-RevId: 244238343 Change-Id: I74c5115915b1cc032a4c16515a056a480f218f00
Diffstat (limited to 'test/syscalls/linux')
-rw-r--r--test/syscalls/linux/symlink.cc38
1 files changed, 16 insertions, 22 deletions
diff --git a/test/syscalls/linux/symlink.cc b/test/syscalls/linux/symlink.cc
index ea6baf76a..318917f4b 100644
--- a/test/syscalls/linux/symlink.cc
+++ b/test/syscalls/linux/symlink.cc
@@ -151,33 +151,27 @@ TEST(SymlinkTest, NewnameCannotExist) {
}
TEST(SymlinkTest, CanEvaluateLink) {
- const std::string oldname = NewTempAbsPath();
- const std::string newname = NewTempAbsPath();
+ const auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
- int fd;
- ASSERT_THAT(fd = open(oldname.c_str(), O_CREAT | O_RDWR, 0666),
- SyscallSucceeds());
- struct stat old;
- EXPECT_THAT(fstat(fd, &old), SyscallSucceeds());
- EXPECT_THAT(close(fd), SyscallSucceeds());
+ // We are going to assert that the symlink inode id is the same as the linked
+ // file's inode id. In order for the inode id to be stable across
+ // save/restore, it must be kept open. The FileDescriptor type will do that
+ // for us automatically.
+ auto fd = ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDWR));
+ struct stat file_st;
+ EXPECT_THAT(fstat(fd.get(), &file_st), SyscallSucceeds());
- EXPECT_THAT(symlink(oldname.c_str(), newname.c_str()), SyscallSucceeds());
- EXPECT_EQ(FilePermission(newname), 0777);
+ const std::string link = NewTempAbsPath();
+ EXPECT_THAT(symlink(file.path().c_str(), link.c_str()), SyscallSucceeds());
+ EXPECT_EQ(FilePermission(link), 0777);
- EXPECT_THAT(fd = open(newname.c_str(), O_RDWR, 0666), SyscallSucceeds());
- struct stat old_linked;
- EXPECT_THAT(fstat(fd, &old_linked), SyscallSucceeds());
- EXPECT_THAT(close(fd), SyscallSucceeds());
+ auto linkfd = ASSERT_NO_ERRNO_AND_VALUE(Open(link.c_str(), O_RDWR));
+ struct stat link_st;
+ EXPECT_THAT(fstat(linkfd.get(), &link_st), SyscallSucceeds());
// Check that in fact newname points to the file we expect.
- // FIXME: use only inodes here once they are consistent,
- // but this is better than nothing.
- EXPECT_EQ(old.st_dev, old_linked.st_dev);
- EXPECT_EQ(old.st_mode, old_linked.st_mode);
- EXPECT_EQ(old.st_size, old_linked.st_size);
-
- EXPECT_THAT(unlink(newname.c_str()), SyscallSucceeds());
- EXPECT_THAT(unlink(oldname.c_str()), SyscallSucceeds());
+ EXPECT_EQ(file_st.st_dev, link_st.st_dev);
+ EXPECT_EQ(file_st.st_ino, link_st.st_ino);
}
TEST(SymlinkTest, TargetIsNotMapped) {