]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/experimental/filesystem/operations/temp_directory_path.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / experimental / filesystem / operations / temp_directory_path.cc
CommitLineData
cbe34bb5 1// Copyright (C) 2015-2017 Free Software Foundation, Inc.
9caf7b27
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
52066eae 18// { dg-options "-lstdc++fs" }
8152d6ef 19// { dg-do run { target c++11 } }
9caf7b27
JW
20// { dg-require-filesystem-ts "" }
21
22#include <experimental/filesystem>
23#include <stdlib.h>
24#include <testsuite_hooks.h>
25#include <testsuite_fs.h>
26
27void
28clean_env()
29{
30 ::unsetenv("TMPDIR");
31 ::unsetenv("TMP");
32 ::unsetenv("TEMPDIR");
33 ::unsetenv("TEMP");
34}
35
36namespace fs = std::experimental::filesystem;
37
38void
39test01()
40{
41 clean_env();
42
43 if (!fs::exists("/tmp"))
44 return; // just give up
45
46 std::error_code ec;
47 fs::path p1 = fs::temp_directory_path(ec);
6daff2d9 48 VERIFY( !ec );
9caf7b27
JW
49 VERIFY( exists(p1) );
50
51 fs::path p2 = fs::temp_directory_path();
52 VERIFY( p1 == p2 );
53}
54
55void
56test02()
57{
58 clean_env();
59
60 if (::setenv("TMPDIR", __gnu_test::nonexistent_path().string().c_str(), 1))
61 return; // just give up
62
63 std::error_code ec;
64 fs::path p = fs::temp_directory_path(ec);
65 VERIFY( ec );
6daff2d9 66 VERIFY( p == fs::path() );
9caf7b27
JW
67
68 std::error_code ec2;
69 try {
70 p = fs::temp_directory_path();
71 } catch (const fs::filesystem_error& e) {
72 ec2 = e.code();
73 }
74 VERIFY( ec2 == ec );
75}
76
6daff2d9
JW
77void
78test03()
79{
80 auto p = __gnu_test::nonexistent_path();
81 create_directories(p/"tmp");
82 permissions(p, fs::perms::none);
83 setenv("TMPDIR", (p/"tmp").c_str(), 1);
84 std::error_code ec;
85 auto r = fs::temp_directory_path(ec); // libstdc++/PR71337
86 VERIFY( ec == std::make_error_code(std::errc::permission_denied) );
87 VERIFY( r == fs::path() );
88
89 std::error_code ec2;
90 try {
91 fs::temp_directory_path();
92 } catch (const fs::filesystem_error& e) {
93 ec2 = e.code();
94 }
95 VERIFY( ec2 == ec );
96
97 permissions(p, fs::perms::owner_all, ec);
98 remove_all(p, ec);
99}
100
101void
102test04()
103{
104 __gnu_test::scoped_file f;
105 setenv("TMPDIR", f.path.c_str(), 1);
106 std::error_code ec;
107 auto r = fs::temp_directory_path(ec);
108 VERIFY( ec == std::make_error_code(std::errc::not_a_directory) );
109 VERIFY( r == fs::path() );
110
111 std::error_code ec2;
112 try {
113 fs::temp_directory_path();
114 } catch (const fs::filesystem_error& e) {
115 ec2 = e.code();
116 }
117 VERIFY( ec2 == ec );
118}
9caf7b27
JW
119
120int
121main()
122{
123 test01();
124 test02();
6daff2d9
JW
125 test03();
126 test04();
9caf7b27 127}