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