53 lines · cpp
1//===-- Unittests for rename ----------------------------------------------===//2//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "include/llvm-libc-macros/linux/sys-stat-macros.h"10#include "include/llvm-libc-macros/linux/unistd-macros.h"11#include "src/fcntl/open.h"12#include "src/stdio/rename.h"13#include "src/unistd/access.h"14#include "src/unistd/close.h"15#include "test/UnitTest/ErrnoCheckingTest.h"16#include "test/UnitTest/ErrnoSetterMatcher.h"17#include "test/UnitTest/Test.h"18 19using LlvmLibcRenameTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;20 21TEST_F(LlvmLibcRenameTest, CreateAndRenameFile) {22 // The test strategy is to create a file and rename it, and also verify that23 // it was renamed.24 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;25 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;26 27 constexpr const char *FILENAME0 = APPEND_LIBC_TEST("rename.test.file0");28 auto TEST_FILEPATH0 = libc_make_test_file_path(FILENAME0);29 30 int fd = LIBC_NAMESPACE::open(TEST_FILEPATH0, O_WRONLY | O_CREAT, S_IRWXU);31 ASSERT_ERRNO_SUCCESS();32 ASSERT_GT(fd, 0);33 ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));34 ASSERT_THAT(LIBC_NAMESPACE::access(TEST_FILEPATH0, F_OK), Succeeds(0));35 36 constexpr const char *FILENAME1 = APPEND_LIBC_TEST("rename.test.file1");37 auto TEST_FILEPATH1 = libc_make_test_file_path(FILENAME1);38 ASSERT_THAT(LIBC_NAMESPACE::rename(TEST_FILEPATH0, TEST_FILEPATH1),39 Succeeds(0));40 ASSERT_THAT(LIBC_NAMESPACE::access(TEST_FILEPATH1, F_OK), Succeeds(0));41 ASSERT_THAT(LIBC_NAMESPACE::access(TEST_FILEPATH0, F_OK), Fails(ENOENT));42}43 44TEST_F(LlvmLibcRenameTest, RenameNonExistent) {45 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;46 47 constexpr const char *FILENAME1 = APPEND_LIBC_TEST("rename.test.file1");48 auto TEST_FILEPATH1 = libc_make_test_file_path(FILENAME1);49 50 ASSERT_THAT(LIBC_NAMESPACE::rename("non-existent", TEST_FILEPATH1),51 Fails(ENOENT));52}53