xref: /freebsd/contrib/capsicum-test/rename.cc (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 #include <fcntl.h>
2 #include <sys/stat.h>
3 
4 #include "./capsicum-test.h"
5 
6 // There was a Capsicum-related regression in FreeBSD renameat,
7 // which affects certain cases independent of Capsicum or capability mode
8 //
9 // added to test the renameat syscall for the case that
10 //    - the "to" file already exists
11 //    - the "to" file is specified by an absolute path
12 //    - the "to" file descriptor is used
13 //          (this descriptor should be ignored if absolute path is provided)
14 //
15 // details at: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=222258
16 
17 
18 const char * create_tmp_src(const char* filename) {
19     const char *src_path = TmpFile(filename);
20     int src_fd = open(src_path, O_CREAT|O_RDWR, 0644);
21     close(src_fd);
22     return src_path;
23 }
24 
25 TEST(Rename, AbsDesignationSame) {
26     const char *src_path = create_tmp_src("rename_test");
27     EXPECT_OK(rename(src_path, src_path));
28     unlink(src_path);
29 }
30 
31 TEST(RenameAt, AbsDesignationSame) {
32     const char *src_path = create_tmp_src("renameat_test");
33     const char *dir_path = TmpFile("renameat_test_dir");
34 
35     EXPECT_OK(mkdir(dir_path, 0755));
36     // random temporary directory descriptor
37     int dfd = open(dir_path, O_DIRECTORY);
38 
39     // Various rename from/to the same absolute path; in each case the source
40     // and dest directory FDs should be irrelevant.
41     EXPECT_OK(renameat(AT_FDCWD, src_path, AT_FDCWD, src_path));
42     EXPECT_OK(renameat(AT_FDCWD, src_path, dfd, src_path));
43     EXPECT_OK(renameat(dfd, src_path, AT_FDCWD, src_path));
44     EXPECT_OK(renameat(dfd, src_path, dfd, src_path));
45 
46     close(dfd);
47     rmdir(dir_path);
48     unlink(src_path);
49 }
50