| 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_socket(file_status s) noexcept |
| 17 | // bool is_socket(path const& p); |
| 18 | // bool is_socket(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_socket(s: s)); |
| 36 | ASSERT_NOEXCEPT(is_socket(p: p, ec&: ec)); |
| 37 | ASSERT_NOT_NOEXCEPT(is_socket(p: p)); |
| 38 | } |
| 39 | |
| 40 | static void is_socket_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: false}, |
| 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: true}, |
| 56 | {.type: file_type::unknown, .expect: false} |
| 57 | }; |
| 58 | for (auto& TC : testCases) { |
| 59 | file_status s(TC.type); |
| 60 | assert(is_socket(s) == TC.expect); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | static void test_exist_not_found() |
| 65 | { |
| 66 | static_test_env static_env; |
| 67 | const path p = static_env.DNE; |
| 68 | assert(is_socket(p) == false); |
| 69 | } |
| 70 | |
| 71 | static void test_is_socket_fails() |
| 72 | { |
| 73 | scoped_test_env env; |
| 74 | #ifdef _WIN32 |
| 75 | // Windows doesn't support setting perms::none to trigger failures |
| 76 | // reading directories; test using a special inaccessible directory |
| 77 | // instead. |
| 78 | const path p = GetWindowsInaccessibleDir(); |
| 79 | if (p.empty()) |
| 80 | return; |
| 81 | #else |
| 82 | const path dir = env.create_dir("dir" ); |
| 83 | const path p = env.create_file("dir/file" , 42); |
| 84 | permissions(p: dir, prms: perms::none); |
| 85 | #endif |
| 86 | |
| 87 | std::error_code ec; |
| 88 | assert(is_socket(p, ec) == false); |
| 89 | assert(ec); |
| 90 | |
| 91 | TEST_THROWS_TYPE(filesystem_error, is_socket(p)); |
| 92 | } |
| 93 | |
| 94 | int main(int, char**) { |
| 95 | signature_test(); |
| 96 | is_socket_status_test(); |
| 97 | test_exist_not_found(); |
| 98 | test_is_socket_fails(); |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |