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 // XFAIL: LIBCXX-WINDOWS-FIXME
12 
13 // <filesystem>
14 
15 // bool is_socket(file_status s) noexcept
16 // bool is_socket(path const& p);
17 // bool is_socket(path const& p, std::error_code& ec) noexcept;
18 
19 #include "filesystem_include.h"
20 #include <type_traits>
21 #include <cassert>
22 
23 #include "test_macros.h"
24 #include "rapid-cxx-test.h"
25 #include "filesystem_test_helper.h"
26 
27 using namespace fs;
28 
29 TEST_SUITE(is_socket_test_suite)
30 
31 TEST_CASE(signature_test)
32 {
33     file_status s; ((void)s);
34     const path p; ((void)p);
35     std::error_code ec; ((void)ec);
36     ASSERT_NOEXCEPT(is_socket(s));
37     ASSERT_NOEXCEPT(is_socket(p, ec));
38     ASSERT_NOT_NOEXCEPT(is_socket(p));
39 }
40 
41 TEST_CASE(is_socket_status_test)
42 {
43     struct TestCase {
44         file_type type;
45         bool expect;
46     };
47     const TestCase testCases[] = {
48         {file_type::none, false},
49         {file_type::not_found, false},
50         {file_type::regular, false},
51         {file_type::directory, false},
52         {file_type::symlink, false},
53         {file_type::block, false},
54         {file_type::character, false},
55         {file_type::fifo, false},
56         {file_type::socket, true},
57         {file_type::unknown, false}
58     };
59     for (auto& TC : testCases) {
60         file_status s(TC.type);
61         TEST_CHECK(is_socket(s) == TC.expect);
62     }
63 }
64 
65 TEST_CASE(test_exist_not_found)
66 {
67     static_test_env static_env;
68     const path p = static_env.DNE;
69     TEST_CHECK(is_socket(p) == false);
70 }
71 
72 TEST_CASE(test_is_socket_fails)
73 {
74     scoped_test_env env;
75     const path dir = env.create_dir("dir");
76     const path file = env.create_file("dir/file", 42);
77     permissions(dir, perms::none);
78 
79     std::error_code ec;
80     TEST_CHECK(is_socket(file, ec) == false);
81     TEST_CHECK(ec);
82 
83     TEST_CHECK_THROW(filesystem_error, is_socket(file));
84 }
85 
86 TEST_SUITE_END()
87