1 //===-- Unittests for rename ----------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "include/llvm-libc-macros/linux/sys-stat-macros.h"
10 #include "include/llvm-libc-macros/linux/unistd-macros.h"
11 #include "src/errno/libc_errno.h"
12 #include "src/fcntl/open.h"
13 #include "src/stdio/rename.h"
14 #include "src/unistd/access.h"
15 #include "src/unistd/close.h"
16 #include "test/UnitTest/ErrnoSetterMatcher.h"
17 #include "test/UnitTest/Test.h"
19 TEST(LlvmLibcRenameTest
, CreateAndRenameFile
) {
20 // The test strategy is to create a file and rename it, and also verify that
22 LIBC_NAMESPACE::libc_errno
= 0;
23 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails
;
24 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds
;
26 constexpr const char *FILENAME0
= "rename.test.file0";
27 auto TEST_FILEPATH0
= libc_make_test_file_path(FILENAME0
);
29 int fd
= LIBC_NAMESPACE::open(TEST_FILEPATH0
, O_WRONLY
| O_CREAT
, S_IRWXU
);
30 ASSERT_ERRNO_SUCCESS();
32 ASSERT_THAT(LIBC_NAMESPACE::close(fd
), Succeeds(0));
33 ASSERT_THAT(LIBC_NAMESPACE::access(TEST_FILEPATH0
, F_OK
), Succeeds(0));
35 constexpr const char *FILENAME1
= "rename.test.file1";
36 auto TEST_FILEPATH1
= libc_make_test_file_path(FILENAME1
);
37 ASSERT_THAT(LIBC_NAMESPACE::rename(TEST_FILEPATH0
, TEST_FILEPATH1
),
39 ASSERT_THAT(LIBC_NAMESPACE::access(TEST_FILEPATH1
, F_OK
), Succeeds(0));
40 ASSERT_THAT(LIBC_NAMESPACE::access(TEST_FILEPATH0
, F_OK
), Fails(ENOENT
));
43 TEST(LlvmLibcRenameTest
, RenameNonExistent
) {
44 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails
;
46 constexpr const char *FILENAME1
= "rename.test.file1";
47 auto TEST_FILEPATH1
= libc_make_test_file_path(FILENAME1
);
49 ASSERT_THAT(LIBC_NAMESPACE::rename("non-existent", TEST_FILEPATH1
),