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