149 lines · cpp
1//===----------------------------------------------------------------------===//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// REQUIRES: can-create-symlinks10// UNSUPPORTED: c++03, c++11, c++1411// UNSUPPORTED: no-filesystem12 13// Starting in Android N (API 24), SELinux policy prevents the shell user from14// creating a FIFO file.15// XFAIL: LIBCXX-ANDROID-FIXME && !android-device-api={{21|22|23}}16 17// <filesystem>18 19// bool is_empty(path const& p);20// bool is_empty(path const& p, std::error_code& ec);21 22#include <filesystem>23#include <type_traits>24#include <cassert>25 26#include "assert_macros.h"27#include "test_macros.h"28#include "filesystem_test_helper.h"29namespace fs = std::filesystem;30using namespace fs;31 32static void signature_test()33{34 const path p; ((void)p);35 std::error_code ec; ((void)ec);36 ASSERT_NOT_NOEXCEPT(is_empty(p, ec));37 ASSERT_NOT_NOEXCEPT(is_empty(p));38}39 40static void test_exist_not_found()41{42 static_test_env static_env;43 const path p = static_env.DNE;44 std::error_code ec;45 assert(is_empty(p, ec) == false);46 assert(ec);47 TEST_THROWS_TYPE(filesystem_error, is_empty(p));48}49 50static void test_is_empty_directory()51{52 static_test_env static_env;53 assert(!is_empty(static_env.Dir));54 assert(!is_empty(static_env.SymlinkToDir));55}56 57static void test_is_empty_directory_dynamic()58{59 scoped_test_env env;60 assert(is_empty(env.test_root));61 env.create_file("foo", 42);62 assert(!is_empty(env.test_root));63}64 65static void test_is_empty_file()66{67 static_test_env static_env;68 assert(is_empty(static_env.EmptyFile));69 assert(!is_empty(static_env.NonEmptyFile));70}71 72static void test_is_empty_fails()73{74 scoped_test_env env;75#ifdef _WIN3276 // Windows doesn't support setting perms::none to trigger failures77 // reading directories; test using a special inaccessible directory78 // instead.79 const path p = GetWindowsInaccessibleDir();80 if (p.empty())81 return;82#else83 const path dir = env.create_dir("dir");84 const path p = env.create_dir("dir/dir2");85 permissions(dir, perms::none);86#endif87 88 std::error_code ec;89 assert(is_empty(p, ec) == false);90 assert(ec);91 92 TEST_THROWS_TYPE(filesystem_error, is_empty(p));93}94 95static void test_directory_access_denied()96{97 scoped_test_env env;98#ifdef _WIN3299 // Windows doesn't support setting perms::none to trigger failures100 // reading directories; test using a special inaccessible directory101 // instead.102 const path dir = GetWindowsInaccessibleDir();103 if (dir.empty())104 return;105#else106 const path dir = env.create_dir("dir");107 const path file1 = env.create_file("dir/file", 42);108 permissions(dir, perms::none);109#endif110 111 std::error_code ec = GetTestEC();112 assert(is_empty(dir, ec) == false);113 assert(ec);114 assert(ec != GetTestEC());115 116 TEST_THROWS_TYPE(filesystem_error, is_empty(dir));117}118 119 120#ifndef _WIN32121static void test_fifo_fails()122{123 scoped_test_env env;124 const path fifo = env.create_fifo("fifo");125 126 std::error_code ec = GetTestEC();127 assert(is_empty(fifo, ec) == false);128 assert(ec);129 assert(ec != GetTestEC());130 131 TEST_THROWS_TYPE(filesystem_error, is_empty(fifo));132}133#endif // _WIN32134 135int main(int, char**) {136 signature_test();137 test_exist_not_found();138 test_is_empty_directory();139 test_is_empty_directory_dynamic();140 test_is_empty_file();141 test_is_empty_fails();142 test_directory_access_denied();143#ifndef _WIN32144 test_fifo_fails();145#endif146 147 return 0;148}149