]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/27_io/filesystem/operations/temp_directory_path.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / filesystem / operations / temp_directory_path.cc
CommitLineData
7adcbafe 1// Copyright (C) 2015-2022 Free Software Foundation, Inc.
641cb5a6
JW
2//
3// This file is part of the GNU ISO C++ Library. This library is free
4// software; you can redistribute it and/or modify it under the
5// terms of the GNU General Public License as published by the
6// Free Software Foundation; either version 3, or (at your option)
7// any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License along
15// with this library; see the file COPYING3. If not see
16// <http://www.gnu.org/licenses/>.
17
641cb5a6
JW
18// { dg-do run { target c++17 } }
19// { dg-require-filesystem-ts "" }
20
21#include <filesystem>
22#include <stdlib.h>
3dbd4d94 23#include <stdio.h>
641cb5a6
JW
24#include <testsuite_hooks.h>
25#include <testsuite_fs.h>
26
27void
28clean_env()
29{
9534a5e6
JW
30#if defined(__MINGW32__) || defined(__MINGW64__)
31 ::_putenv("TMP=");
32 ::_putenv("TEMP=");
33#else
641cb5a6
JW
34 ::unsetenv("TMPDIR");
35 ::unsetenv("TMP");
36 ::unsetenv("TEMPDIR");
37 ::unsetenv("TEMP");
9534a5e6
JW
38#endif
39}
40
41bool
42set_env(const char* name, std::string value)
43{
44#if defined(__MINGW32__) || defined(__MINGW64__)
45 std::string s = name;
46 s += '=';
47 s += value;
48 return !::_putenv(s.c_str());
49#else
50 return !::setenv(name, value.c_str(), 1);
51#endif
641cb5a6
JW
52}
53
54namespace fs = std::filesystem;
55
56void
57test01()
58{
59 clean_env();
60
61 if (!fs::exists("/tmp"))
3dbd4d94
JW
62 {
63 puts("/tmp doesn't exist, not testing it for temp_directory_path");
641cb5a6 64 return; // just give up
3dbd4d94 65 }
641cb5a6
JW
66
67 std::error_code ec = make_error_code(std::errc::invalid_argument);
68 fs::path p1 = fs::temp_directory_path(ec);
69 VERIFY( !ec );
70 VERIFY( exists(p1) );
71
72 fs::path p2 = fs::temp_directory_path();
73 VERIFY( p1 == p2 );
74}
75
76void
77test02()
78{
79 clean_env();
80
dd0f7ba2 81 if (!set_env("TMP", __gnu_test::nonexistent_path().string()))
3dbd4d94
JW
82 {
83 puts("Cannot set environment variables, not testing temp_directory_path");
641cb5a6 84 return; // just give up
3dbd4d94 85 }
641cb5a6
JW
86
87 std::error_code ec;
88 fs::path p = fs::temp_directory_path(ec);
89 VERIFY( ec );
90 VERIFY( p == fs::path() );
91
92 std::error_code ec2;
93 try {
94 p = fs::temp_directory_path();
95 } catch (const fs::filesystem_error& e) {
96 ec2 = e.code();
97 }
98 VERIFY( ec2 == ec );
99}
100
101void
102test03()
103{
29b2fd37
JW
104 if (!__gnu_test::permissions_are_testable())
105 return;
dd0f7ba2
JW
106
107 clean_env();
108
641cb5a6
JW
109 auto p = __gnu_test::nonexistent_path();
110 create_directories(p/"tmp");
111 permissions(p, fs::perms::none);
9534a5e6 112 set_env("TMPDIR", (p/"tmp").string());
641cb5a6
JW
113 std::error_code ec;
114 auto r = fs::temp_directory_path(ec); // libstdc++/PR71337
115 VERIFY( ec == std::make_error_code(std::errc::permission_denied) );
116 VERIFY( r == fs::path() );
117
118 std::error_code ec2;
119 try {
120 fs::temp_directory_path();
121 } catch (const fs::filesystem_error& e) {
122 ec2 = e.code();
123 }
124 VERIFY( ec2 == ec );
125
126 permissions(p, fs::perms::owner_all, ec);
127 remove_all(p, ec);
128}
129
130void
131test04()
132{
dd0f7ba2
JW
133 clean_env();
134
641cb5a6 135 __gnu_test::scoped_file f;
dd0f7ba2 136 set_env("TMP", f.path.string());
641cb5a6
JW
137 std::error_code ec;
138 auto r = fs::temp_directory_path(ec);
139 VERIFY( ec == std::make_error_code(std::errc::not_a_directory) );
140 VERIFY( r == fs::path() );
141
142 std::error_code ec2;
143 try {
144 fs::temp_directory_path();
145 } catch (const fs::filesystem_error& e) {
146 ec2 = e.code();
147 }
148 VERIFY( ec2 == ec );
149}
150
151int
152main()
153{
154 test01();
155 test02();
156 test03();
157 test04();
158}