xref: /llvm-project/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_empty/is_empty.pass.cpp (revision bce3b505931cee9dc79d1c56c021983b4a8fb819)
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 // UNSUPPORTED: c++03
10 // UNSUPPORTED: no-filesystem
11 // UNSUPPORTED: availability-filesystem-missing
12 
13 // Starting in Android N (API 24), SELinux policy prevents the shell user from
14 // 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_include.h"
23 #include <type_traits>
24 #include <cassert>
25 
26 #include "assert_macros.h"
27 #include "test_macros.h"
28 #include "filesystem_test_helper.h"
29 
30 using namespace fs;
31 
32 static 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 
40 static 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 
50 static 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 
57 static 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 
65 static 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 
72 static void test_is_empty_fails()
73 {
74     scoped_test_env env;
75 #ifdef _WIN32
76     // Windows doesn't support setting perms::none to trigger failures
77     // reading directories; test using a special inaccessible directory
78     // instead.
79     const path p = GetWindowsInaccessibleDir();
80     if (p.empty())
81         return;
82 #else
83     const path dir = env.create_dir("dir");
84     const path p = env.create_dir("dir/dir2");
85     permissions(dir, perms::none);
86 #endif
87 
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 
95 static void test_directory_access_denied()
96 {
97     scoped_test_env env;
98 #ifdef _WIN32
99     // Windows doesn't support setting perms::none to trigger failures
100     // reading directories; test using a special inaccessible directory
101     // instead.
102     const path dir = GetWindowsInaccessibleDir();
103     if (dir.empty())
104         return;
105 #else
106     const path dir = env.create_dir("dir");
107     const path file1 = env.create_file("dir/file", 42);
108     permissions(dir, perms::none);
109 #endif
110 
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 _WIN32
121 static 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 // _WIN32
134 
135 int 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 _WIN32
144     test_fifo_fails();
145 #endif
146 
147     return 0;
148 }
149