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