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