Debian Codegraph @

libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.is_symlink/is_symlink.pass.cpp

raw
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
13 // <filesystem>
14
15 // bool is_symlink(file_status s) noexcept
16 // bool is_symlink(path const& p);
17 // bool is_symlink(path const& p, std::error_code& ec) noexcept;
18
19 #include <filesystem>
20 #include <type_traits>
21 #include <cassert>
22
23 #include "assert_macros.h"
24 #include "test_macros.h"
25 #include "filesystem_test_helper.h"
26 namespace fs = std::filesystem;
27 using namespace fs;
28
29 static void 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(is_symlink(s));
35 ASSERT_NOEXCEPT(is_symlink(p, ec));
36 ASSERT_NOT_NOEXCEPT(is_symlink(p));
37 }
38
39 static void is_symlink_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, false},
49 {file_type::directory, false},
50 {file_type::symlink, true},
51 {file_type::block, false},
52 {file_type::character, false},
53 {file_type::fifo, false},
54 {file_type::socket, false},
55 {file_type::unknown, false}
56 };
57 for (auto& TC : testCases) {
58 file_status s(TC.type);
59 assert(is_symlink(s) == TC.expect);
60 }
61 }
62
63 static void static_env_test()
64 {
65 static_test_env static_env;
66 struct TestCase {
67 path p;
68 bool expect;
69 };
70 const TestCase testCases[] = {
71 {static_env.File, false},
72 {static_env.Dir, false},
73 {static_env.SymlinkToFile, true},
74 {static_env.SymlinkToDir, true},
75 {static_env.BadSymlink, true}
76 };
77 for (auto& TC : testCases) {
78 assert(is_symlink(TC.p) == TC.expect);
79 }
80 }
81
82 static void test_exist_not_found()
83 {
84 static_test_env static_env;
85 const path p = static_env.DNE;
86 assert(is_symlink(p) == false);
87 std::error_code ec;
88 assert(is_symlink(p, ec) == false);
89 assert(ec);
90 }
91
92 static void test_is_symlink_fails()
93 {
94 scoped_test_env env;
95 #ifdef _WIN32
96 // Windows doesn't support setting perms::none to trigger failures
97 // reading directories; test using a special inaccessible directory
98 // instead.
99 const path p = GetWindowsInaccessibleDir();
100 if (p.empty())
101 return;
102 #else
103 const path dir = env.create_dir("dir");
104 const path p = env.create_file("dir/file", 42);
105 permissions(dir, perms::none);
106 #endif
107
108 std::error_code ec;
109 assert(is_symlink(p, ec) == false);
110 assert(ec);
111
112 TEST_THROWS_TYPE(filesystem_error, is_symlink(p));
113 }
114
115 int main(int, char**) {
116 signature_test();
117 is_symlink_status_test();
118 static_env_test();
119 test_exist_not_found();
120 test_is_symlink_fails();
121
122 return 0;
123 }