101 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// REQUIRES: can-create-symlinks10// UNSUPPORTED: c++03, c++11, c++1411// UNSUPPORTED: no-filesystem12 13// <filesystem>14 15// bool is_socket(file_status s) noexcept16// bool is_socket(path const& p);17// bool is_socket(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"26namespace fs = std::filesystem;27using namespace fs;28 29static 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_socket(s));35 ASSERT_NOEXCEPT(is_socket(p, ec));36 ASSERT_NOT_NOEXCEPT(is_socket(p));37}38 39static void is_socket_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, false},51 {file_type::block, false},52 {file_type::character, false},53 {file_type::fifo, false},54 {file_type::socket, true},55 {file_type::unknown, false}56 };57 for (auto& TC : testCases) {58 file_status s(TC.type);59 assert(is_socket(s) == TC.expect);60 }61}62 63static void test_exist_not_found()64{65 static_test_env static_env;66 const path p = static_env.DNE;67 assert(is_socket(p) == false);68}69 70static void test_is_socket_fails()71{72 scoped_test_env env;73#ifdef _WIN3274 // Windows doesn't support setting perms::none to trigger failures75 // reading directories; test using a special inaccessible directory76 // instead.77 const path p = GetWindowsInaccessibleDir();78 if (p.empty())79 return;80#else81 const path dir = env.create_dir("dir");82 const path p = env.create_file("dir/file", 42);83 permissions(dir, perms::none);84#endif85 86 std::error_code ec;87 assert(is_socket(p, ec) == false);88 assert(ec);89 90 TEST_THROWS_TYPE(filesystem_error, is_socket(p));91}92 93int main(int, char**) {94 signature_test();95 is_socket_status_test();96 test_exist_not_found();97 test_is_socket_fails();98 99 return 0;100}101