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 
11 // <filesystem>
12 
13 // bool exists(file_status s) noexcept
14 // bool exists(path const& p);
15 // bool exists(path const& p, std::error_code& ec) noexcept;
16 
17 #include "filesystem_include.h"
18 #include <type_traits>
19 #include <cassert>
20 
21 #include "test_macros.h"
22 #include "rapid-cxx-test.h"
23 #include "filesystem_test_helper.h"
24 
25 using namespace fs;
26 
27 TEST_SUITE(exists_test_suite)
28 
TEST_CASE(signature_test)29 TEST_CASE(signature_test)
30 {
31     file_status s; ((void)s);
32     const path p; ((void)p);
33     std::error_code ec; ((void)ec);
34     ASSERT_NOEXCEPT(exists(s));
35     ASSERT_NOEXCEPT(exists(p, ec));
36     ASSERT_NOT_NOEXCEPT(exists(p));
37 }
38 
TEST_CASE(exists_status_test)39 TEST_CASE(exists_status_test)
40 {
41     struct TestCase {
42         file_type type;
43         bool expect;
44     };
45     const TestCase testCases[] = {
46         {file_type::none, false},
47         {file_type::not_found, false},
48         {file_type::regular, true},
49         {file_type::directory, true},
50         {file_type::symlink, true},
51         {file_type::block, true},
52         {file_type::character, true},
53         {file_type::fifo, true},
54         {file_type::socket, true},
55         {file_type::unknown, true}
56     };
57     for (auto& TC : testCases) {
58         file_status s(TC.type);
59         TEST_CHECK(exists(s) == TC.expect);
60     }
61 }
62 
TEST_CASE(test_exist_not_found)63 TEST_CASE(test_exist_not_found)
64 {
65     static_test_env static_env;
66     const path p = static_env.DNE;
67     TEST_CHECK(exists(p) == false);
68 
69     TEST_CHECK(exists(static_env.Dir) == true);
70     TEST_CHECK(exists(static_env.Dir / "dne") == false);
71     // Whether <dir>/dne/.. is considered to exist or not is not necessarily
72     // something we need to define, but the platform specific behaviour
73     // does affect a few other tests, so clarify the root cause here.
74 #ifdef _WIN32
75     TEST_CHECK(exists(static_env.Dir / "dne" / "..") == true);
76 #else
77     TEST_CHECK(exists(static_env.Dir / "dne" / "..") == false);
78 #endif
79 
80     std::error_code ec = GetTestEC();
81     TEST_CHECK(exists(p, ec) == false);
82     TEST_CHECK(!ec);
83 }
84 
TEST_CASE(test_exists_fails)85 TEST_CASE(test_exists_fails)
86 {
87 #ifdef _WIN32
88     // Windows doesn't support setting perms::none to trigger failures
89     // reading directories; test using a special inaccessible directory
90     // instead.
91     const path p = GetWindowsInaccessibleDir();
92     if (p.empty())
93         TEST_UNSUPPORTED();
94 #else
95     scoped_test_env env;
96     const path dir = env.create_dir("dir");
97     const path p = env.create_file("dir/file", 42);
98     permissions(dir, perms::none);
99 #endif
100 
101     std::error_code ec;
102     TEST_CHECK(exists(p, ec) == false);
103     TEST_CHECK(ec);
104 
105     TEST_CHECK_THROW(filesystem_error, exists(p));
106 }
107 
108 #ifndef _WIN32
109 // Checking for the existence of an invalid long path name doesn't
110 // trigger errors on windows.
TEST_CASE(test_name_too_long)111 TEST_CASE(test_name_too_long) {
112     std::string long_name(2500, 'a');
113     const path file(long_name);
114 
115     std::error_code ec;
116     TEST_CHECK(exists(file, ec) == false);
117     TEST_CHECK(ec);
118 }
119 #endif
120 
121 TEST_SUITE_END()
122