xref: /minix3/external/bsd/atf/dist/tools/fs_test.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc //
2*0a6a1f1dSLionel Sambuc // Automated Testing Framework (atf)
3*0a6a1f1dSLionel Sambuc //
4*0a6a1f1dSLionel Sambuc // Copyright (c) 2007 The NetBSD Foundation, Inc.
5*0a6a1f1dSLionel Sambuc // All rights reserved.
6*0a6a1f1dSLionel Sambuc //
7*0a6a1f1dSLionel Sambuc // Redistribution and use in source and binary forms, with or without
8*0a6a1f1dSLionel Sambuc // modification, are permitted provided that the following conditions
9*0a6a1f1dSLionel Sambuc // are met:
10*0a6a1f1dSLionel Sambuc // 1. Redistributions of source code must retain the above copyright
11*0a6a1f1dSLionel Sambuc //    notice, this list of conditions and the following disclaimer.
12*0a6a1f1dSLionel Sambuc // 2. Redistributions in binary form must reproduce the above copyright
13*0a6a1f1dSLionel Sambuc //    notice, this list of conditions and the following disclaimer in the
14*0a6a1f1dSLionel Sambuc //    documentation and/or other materials provided with the distribution.
15*0a6a1f1dSLionel Sambuc //
16*0a6a1f1dSLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*0a6a1f1dSLionel Sambuc // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*0a6a1f1dSLionel Sambuc // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*0a6a1f1dSLionel Sambuc // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*0a6a1f1dSLionel Sambuc // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*0a6a1f1dSLionel Sambuc // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*0a6a1f1dSLionel Sambuc // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*0a6a1f1dSLionel Sambuc // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*0a6a1f1dSLionel Sambuc // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*0a6a1f1dSLionel Sambuc // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*0a6a1f1dSLionel Sambuc // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*0a6a1f1dSLionel Sambuc // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*0a6a1f1dSLionel Sambuc //
29*0a6a1f1dSLionel Sambuc 
30*0a6a1f1dSLionel Sambuc extern "C" {
31*0a6a1f1dSLionel Sambuc #include <sys/types.h>
32*0a6a1f1dSLionel Sambuc #include <sys/stat.h>
33*0a6a1f1dSLionel Sambuc }
34*0a6a1f1dSLionel Sambuc 
35*0a6a1f1dSLionel Sambuc #include <cerrno>
36*0a6a1f1dSLionel Sambuc #include <cstdio>
37*0a6a1f1dSLionel Sambuc #include <fstream>
38*0a6a1f1dSLionel Sambuc 
39*0a6a1f1dSLionel Sambuc #include <atf-c++.hpp>
40*0a6a1f1dSLionel Sambuc 
41*0a6a1f1dSLionel Sambuc #include "exceptions.hpp"
42*0a6a1f1dSLionel Sambuc #include "fs.hpp"
43*0a6a1f1dSLionel Sambuc #include "user.hpp"
44*0a6a1f1dSLionel Sambuc 
45*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
46*0a6a1f1dSLionel Sambuc // Auxiliary functions.
47*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
48*0a6a1f1dSLionel Sambuc 
49*0a6a1f1dSLionel Sambuc static
50*0a6a1f1dSLionel Sambuc void
create_file(const char * name)51*0a6a1f1dSLionel Sambuc create_file(const char *name)
52*0a6a1f1dSLionel Sambuc {
53*0a6a1f1dSLionel Sambuc     std::ofstream os(name);
54*0a6a1f1dSLionel Sambuc     os.close();
55*0a6a1f1dSLionel Sambuc }
56*0a6a1f1dSLionel Sambuc 
57*0a6a1f1dSLionel Sambuc static
58*0a6a1f1dSLionel Sambuc void
create_files(void)59*0a6a1f1dSLionel Sambuc create_files(void)
60*0a6a1f1dSLionel Sambuc {
61*0a6a1f1dSLionel Sambuc     ::mkdir("files", 0755);
62*0a6a1f1dSLionel Sambuc     ::mkdir("files/dir", 0755);
63*0a6a1f1dSLionel Sambuc 
64*0a6a1f1dSLionel Sambuc     std::ofstream os("files/reg");
65*0a6a1f1dSLionel Sambuc     os.close();
66*0a6a1f1dSLionel Sambuc 
67*0a6a1f1dSLionel Sambuc     // TODO: Should create all other file types (blk, chr, fifo, lnk, sock)
68*0a6a1f1dSLionel Sambuc     // and test for them... but the underlying file system may not support
69*0a6a1f1dSLionel Sambuc     // most of these.  Specially as we are working on /tmp, which can be
70*0a6a1f1dSLionel Sambuc     // mounted with flags such as "nodev".  See how to deal with this
71*0a6a1f1dSLionel Sambuc     // situation.
72*0a6a1f1dSLionel Sambuc }
73*0a6a1f1dSLionel Sambuc 
74*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
75*0a6a1f1dSLionel Sambuc // Test cases for the "path" class.
76*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
77*0a6a1f1dSLionel Sambuc 
78*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(path_normalize);
ATF_TEST_CASE_HEAD(path_normalize)79*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(path_normalize)
80*0a6a1f1dSLionel Sambuc {
81*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests the path's normalization");
82*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(path_normalize)83*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(path_normalize)
84*0a6a1f1dSLionel Sambuc {
85*0a6a1f1dSLionel Sambuc     using tools::fs::path;
86*0a6a1f1dSLionel Sambuc 
87*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ(path(".").str(), ".");
88*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ(path("..").str(), "..");
89*0a6a1f1dSLionel Sambuc 
90*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ(path("foo").str(), "foo");
91*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ(path("foo/bar").str(), "foo/bar");
92*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ(path("foo/bar/").str(), "foo/bar");
93*0a6a1f1dSLionel Sambuc 
94*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ(path("/foo").str(), "/foo");
95*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ(path("/foo/bar").str(), "/foo/bar");
96*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ(path("/foo/bar/").str(), "/foo/bar");
97*0a6a1f1dSLionel Sambuc 
98*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ(path("///foo").str(), "/foo");
99*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ(path("///foo///bar").str(), "/foo/bar");
100*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ(path("///foo///bar///").str(), "/foo/bar");
101*0a6a1f1dSLionel Sambuc }
102*0a6a1f1dSLionel Sambuc 
103*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(path_is_absolute);
ATF_TEST_CASE_HEAD(path_is_absolute)104*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(path_is_absolute)
105*0a6a1f1dSLionel Sambuc {
106*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests the path::is_absolute function");
107*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(path_is_absolute)108*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(path_is_absolute)
109*0a6a1f1dSLionel Sambuc {
110*0a6a1f1dSLionel Sambuc     using tools::fs::path;
111*0a6a1f1dSLionel Sambuc 
112*0a6a1f1dSLionel Sambuc     ATF_REQUIRE( path("/").is_absolute());
113*0a6a1f1dSLionel Sambuc     ATF_REQUIRE( path("////").is_absolute());
114*0a6a1f1dSLionel Sambuc     ATF_REQUIRE( path("////a").is_absolute());
115*0a6a1f1dSLionel Sambuc     ATF_REQUIRE( path("//a//").is_absolute());
116*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(!path("a////").is_absolute());
117*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(!path("../foo").is_absolute());
118*0a6a1f1dSLionel Sambuc }
119*0a6a1f1dSLionel Sambuc 
120*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(path_is_root);
ATF_TEST_CASE_HEAD(path_is_root)121*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(path_is_root)
122*0a6a1f1dSLionel Sambuc {
123*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests the path::is_root function");
124*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(path_is_root)125*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(path_is_root)
126*0a6a1f1dSLionel Sambuc {
127*0a6a1f1dSLionel Sambuc     using tools::fs::path;
128*0a6a1f1dSLionel Sambuc 
129*0a6a1f1dSLionel Sambuc     ATF_REQUIRE( path("/").is_root());
130*0a6a1f1dSLionel Sambuc     ATF_REQUIRE( path("////").is_root());
131*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(!path("////a").is_root());
132*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(!path("//a//").is_root());
133*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(!path("a////").is_root());
134*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(!path("../foo").is_root());
135*0a6a1f1dSLionel Sambuc }
136*0a6a1f1dSLionel Sambuc 
137*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(path_branch_path);
ATF_TEST_CASE_HEAD(path_branch_path)138*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(path_branch_path)
139*0a6a1f1dSLionel Sambuc {
140*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests the path::branch_path function");
141*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(path_branch_path)142*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(path_branch_path)
143*0a6a1f1dSLionel Sambuc {
144*0a6a1f1dSLionel Sambuc     using tools::fs::path;
145*0a6a1f1dSLionel Sambuc 
146*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ(path(".").branch_path().str(), ".");
147*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ(path("foo").branch_path().str(), ".");
148*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ(path("foo/bar").branch_path().str(), "foo");
149*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ(path("/foo").branch_path().str(), "/");
150*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ(path("/foo/bar").branch_path().str(), "/foo");
151*0a6a1f1dSLionel Sambuc }
152*0a6a1f1dSLionel Sambuc 
153*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(path_leaf_name);
ATF_TEST_CASE_HEAD(path_leaf_name)154*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(path_leaf_name)
155*0a6a1f1dSLionel Sambuc {
156*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests the path::leaf_name function");
157*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(path_leaf_name)158*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(path_leaf_name)
159*0a6a1f1dSLionel Sambuc {
160*0a6a1f1dSLionel Sambuc     using tools::fs::path;
161*0a6a1f1dSLionel Sambuc 
162*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ(path(".").leaf_name(), ".");
163*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ(path("foo").leaf_name(), "foo");
164*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ(path("foo/bar").leaf_name(), "bar");
165*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ(path("/foo").leaf_name(), "foo");
166*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ(path("/foo/bar").leaf_name(), "bar");
167*0a6a1f1dSLionel Sambuc }
168*0a6a1f1dSLionel Sambuc 
169*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(path_compare_equal);
ATF_TEST_CASE_HEAD(path_compare_equal)170*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(path_compare_equal)
171*0a6a1f1dSLionel Sambuc {
172*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests the comparison for equality between paths");
173*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(path_compare_equal)174*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(path_compare_equal)
175*0a6a1f1dSLionel Sambuc {
176*0a6a1f1dSLionel Sambuc     using tools::fs::path;
177*0a6a1f1dSLionel Sambuc 
178*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(path("/") == path("///"));
179*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(path("/a") == path("///a"));
180*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(path("/a") == path("///a///"));
181*0a6a1f1dSLionel Sambuc 
182*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(path("a/b/c") == path("a//b//c"));
183*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(path("a/b/c") == path("a//b//c///"));
184*0a6a1f1dSLionel Sambuc }
185*0a6a1f1dSLionel Sambuc 
186*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(path_compare_different);
ATF_TEST_CASE_HEAD(path_compare_different)187*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(path_compare_different)
188*0a6a1f1dSLionel Sambuc {
189*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests the comparison for difference between paths");
190*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(path_compare_different)191*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(path_compare_different)
192*0a6a1f1dSLionel Sambuc {
193*0a6a1f1dSLionel Sambuc     using tools::fs::path;
194*0a6a1f1dSLionel Sambuc 
195*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(path("/") != path("//a/"));
196*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(path("/a") != path("a///"));
197*0a6a1f1dSLionel Sambuc 
198*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(path("a/b/c") != path("a/b"));
199*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(path("a/b/c") != path("a//b"));
200*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(path("a/b/c") != path("/a/b/c"));
201*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(path("a/b/c") != path("/a//b//c"));
202*0a6a1f1dSLionel Sambuc }
203*0a6a1f1dSLionel Sambuc 
204*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(path_concat);
ATF_TEST_CASE_HEAD(path_concat)205*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(path_concat)
206*0a6a1f1dSLionel Sambuc {
207*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests the concatenation of multiple paths");
208*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(path_concat)209*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(path_concat)
210*0a6a1f1dSLionel Sambuc {
211*0a6a1f1dSLionel Sambuc     using tools::fs::path;
212*0a6a1f1dSLionel Sambuc 
213*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ((path("foo") / "bar").str(), "foo/bar");
214*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ((path("foo/") / "/bar").str(), "foo/bar");
215*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ((path("foo/") / "/bar/baz").str(), "foo/bar/baz");
216*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ((path("foo/") / "///bar///baz").str(), "foo/bar/baz");
217*0a6a1f1dSLionel Sambuc }
218*0a6a1f1dSLionel Sambuc 
219*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(path_to_absolute);
ATF_TEST_CASE_HEAD(path_to_absolute)220*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(path_to_absolute)
221*0a6a1f1dSLionel Sambuc {
222*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests the conversion of a relative path to an "
223*0a6a1f1dSLionel Sambuc                "absolute one");
224*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(path_to_absolute)225*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(path_to_absolute)
226*0a6a1f1dSLionel Sambuc {
227*0a6a1f1dSLionel Sambuc     using tools::fs::file_info;
228*0a6a1f1dSLionel Sambuc     using tools::fs::path;
229*0a6a1f1dSLionel Sambuc 
230*0a6a1f1dSLionel Sambuc     create_files();
231*0a6a1f1dSLionel Sambuc 
232*0a6a1f1dSLionel Sambuc     {
233*0a6a1f1dSLionel Sambuc         const path p(".");
234*0a6a1f1dSLionel Sambuc         path pa = p.to_absolute();
235*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(pa.is_absolute());
236*0a6a1f1dSLionel Sambuc 
237*0a6a1f1dSLionel Sambuc         file_info fi(p);
238*0a6a1f1dSLionel Sambuc         file_info fia(pa);
239*0a6a1f1dSLionel Sambuc         ATF_REQUIRE_EQ(fi.get_device(), fia.get_device());
240*0a6a1f1dSLionel Sambuc         ATF_REQUIRE_EQ(fi.get_inode(), fia.get_inode());
241*0a6a1f1dSLionel Sambuc     }
242*0a6a1f1dSLionel Sambuc 
243*0a6a1f1dSLionel Sambuc     {
244*0a6a1f1dSLionel Sambuc         const path p("files/reg");
245*0a6a1f1dSLionel Sambuc         path pa = p.to_absolute();
246*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(pa.is_absolute());
247*0a6a1f1dSLionel Sambuc 
248*0a6a1f1dSLionel Sambuc         file_info fi(p);
249*0a6a1f1dSLionel Sambuc         file_info fia(pa);
250*0a6a1f1dSLionel Sambuc         ATF_REQUIRE_EQ(fi.get_device(), fia.get_device());
251*0a6a1f1dSLionel Sambuc         ATF_REQUIRE_EQ(fi.get_inode(), fia.get_inode());
252*0a6a1f1dSLionel Sambuc     }
253*0a6a1f1dSLionel Sambuc }
254*0a6a1f1dSLionel Sambuc 
255*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(path_op_less);
ATF_TEST_CASE_HEAD(path_op_less)256*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(path_op_less)
257*0a6a1f1dSLionel Sambuc {
258*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests that the path's less-than operator works");
259*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(path_op_less)260*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(path_op_less)
261*0a6a1f1dSLionel Sambuc {
262*0a6a1f1dSLionel Sambuc     using tools::fs::path;
263*0a6a1f1dSLionel Sambuc 
264*0a6a1f1dSLionel Sambuc     create_files();
265*0a6a1f1dSLionel Sambuc 
266*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(!(path("aaa") < path("aaa")));
267*0a6a1f1dSLionel Sambuc 
268*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(  path("aab") < path("abc"));
269*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(!(path("abc") < path("aab")));
270*0a6a1f1dSLionel Sambuc }
271*0a6a1f1dSLionel Sambuc 
272*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
273*0a6a1f1dSLionel Sambuc // Test cases for the "directory" class.
274*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
275*0a6a1f1dSLionel Sambuc 
276*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(directory_read);
ATF_TEST_CASE_HEAD(directory_read)277*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(directory_read)
278*0a6a1f1dSLionel Sambuc {
279*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests the directory class creation, which reads "
280*0a6a1f1dSLionel Sambuc                "the contents of a directory");
281*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(directory_read)282*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(directory_read)
283*0a6a1f1dSLionel Sambuc {
284*0a6a1f1dSLionel Sambuc     using tools::fs::directory;
285*0a6a1f1dSLionel Sambuc     using tools::fs::path;
286*0a6a1f1dSLionel Sambuc 
287*0a6a1f1dSLionel Sambuc     create_files();
288*0a6a1f1dSLionel Sambuc 
289*0a6a1f1dSLionel Sambuc     directory d(path("files"));
290*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ(d.size(), 4);
291*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(d.find(".") != d.end());
292*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(d.find("..") != d.end());
293*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(d.find("dir") != d.end());
294*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(d.find("reg") != d.end());
295*0a6a1f1dSLionel Sambuc }
296*0a6a1f1dSLionel Sambuc 
297*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(directory_file_info);
ATF_TEST_CASE_HEAD(directory_file_info)298*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(directory_file_info)
299*0a6a1f1dSLionel Sambuc {
300*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests that the file_info objects attached to the "
301*0a6a1f1dSLionel Sambuc                "directory are valid");
302*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(directory_file_info)303*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(directory_file_info)
304*0a6a1f1dSLionel Sambuc {
305*0a6a1f1dSLionel Sambuc     using tools::fs::directory;
306*0a6a1f1dSLionel Sambuc     using tools::fs::file_info;
307*0a6a1f1dSLionel Sambuc     using tools::fs::path;
308*0a6a1f1dSLionel Sambuc 
309*0a6a1f1dSLionel Sambuc     create_files();
310*0a6a1f1dSLionel Sambuc 
311*0a6a1f1dSLionel Sambuc     directory d(path("files"));
312*0a6a1f1dSLionel Sambuc 
313*0a6a1f1dSLionel Sambuc     {
314*0a6a1f1dSLionel Sambuc         directory::const_iterator iter = d.find("dir");
315*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(iter != d.end());
316*0a6a1f1dSLionel Sambuc         const file_info& fi = (*iter).second;
317*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(fi.get_type() == file_info::dir_type);
318*0a6a1f1dSLionel Sambuc     }
319*0a6a1f1dSLionel Sambuc 
320*0a6a1f1dSLionel Sambuc     {
321*0a6a1f1dSLionel Sambuc         directory::const_iterator iter = d.find("reg");
322*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(iter != d.end());
323*0a6a1f1dSLionel Sambuc         const file_info& fi = (*iter).second;
324*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(fi.get_type() == file_info::reg_type);
325*0a6a1f1dSLionel Sambuc     }
326*0a6a1f1dSLionel Sambuc }
327*0a6a1f1dSLionel Sambuc 
328*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(directory_names);
ATF_TEST_CASE_HEAD(directory_names)329*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(directory_names)
330*0a6a1f1dSLionel Sambuc {
331*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests the directory's names method");
332*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(directory_names)333*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(directory_names)
334*0a6a1f1dSLionel Sambuc {
335*0a6a1f1dSLionel Sambuc     using tools::fs::directory;
336*0a6a1f1dSLionel Sambuc     using tools::fs::path;
337*0a6a1f1dSLionel Sambuc 
338*0a6a1f1dSLionel Sambuc     create_files();
339*0a6a1f1dSLionel Sambuc 
340*0a6a1f1dSLionel Sambuc     directory d(path("files"));
341*0a6a1f1dSLionel Sambuc     std::set< std::string > ns = d.names();
342*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ(ns.size(), 4);
343*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(ns.find(".") != ns.end());
344*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(ns.find("..") != ns.end());
345*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(ns.find("dir") != ns.end());
346*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(ns.find("reg") != ns.end());
347*0a6a1f1dSLionel Sambuc }
348*0a6a1f1dSLionel Sambuc 
349*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
350*0a6a1f1dSLionel Sambuc // Test cases for the "file_info" class.
351*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
352*0a6a1f1dSLionel Sambuc 
353*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(file_info_stat);
ATF_TEST_CASE_HEAD(file_info_stat)354*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(file_info_stat)
355*0a6a1f1dSLionel Sambuc {
356*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests the file_info creation and its basic contents");
357*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(file_info_stat)358*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(file_info_stat)
359*0a6a1f1dSLionel Sambuc {
360*0a6a1f1dSLionel Sambuc     using tools::fs::file_info;
361*0a6a1f1dSLionel Sambuc     using tools::fs::path;
362*0a6a1f1dSLionel Sambuc 
363*0a6a1f1dSLionel Sambuc     create_files();
364*0a6a1f1dSLionel Sambuc 
365*0a6a1f1dSLionel Sambuc     {
366*0a6a1f1dSLionel Sambuc         path p("files/dir");
367*0a6a1f1dSLionel Sambuc         file_info fi(p);
368*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(fi.get_type() == file_info::dir_type);
369*0a6a1f1dSLionel Sambuc     }
370*0a6a1f1dSLionel Sambuc 
371*0a6a1f1dSLionel Sambuc     {
372*0a6a1f1dSLionel Sambuc         path p("files/reg");
373*0a6a1f1dSLionel Sambuc         file_info fi(p);
374*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(fi.get_type() == file_info::reg_type);
375*0a6a1f1dSLionel Sambuc     }
376*0a6a1f1dSLionel Sambuc }
377*0a6a1f1dSLionel Sambuc 
378*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(file_info_perms);
ATF_TEST_CASE_HEAD(file_info_perms)379*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(file_info_perms)
380*0a6a1f1dSLionel Sambuc {
381*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests the file_info methods to get the file's "
382*0a6a1f1dSLionel Sambuc                "permissions");
383*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(file_info_perms)384*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(file_info_perms)
385*0a6a1f1dSLionel Sambuc {
386*0a6a1f1dSLionel Sambuc     using tools::fs::file_info;
387*0a6a1f1dSLionel Sambuc     using tools::fs::path;
388*0a6a1f1dSLionel Sambuc 
389*0a6a1f1dSLionel Sambuc     path p("file");
390*0a6a1f1dSLionel Sambuc 
391*0a6a1f1dSLionel Sambuc     std::ofstream os(p.c_str());
392*0a6a1f1dSLionel Sambuc     os.close();
393*0a6a1f1dSLionel Sambuc 
394*0a6a1f1dSLionel Sambuc #define perms(ur, uw, ux, gr, gw, gx, othr, othw, othx) \
395*0a6a1f1dSLionel Sambuc     { \
396*0a6a1f1dSLionel Sambuc         file_info fi(p); \
397*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(fi.is_owner_readable() == ur); \
398*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(fi.is_owner_writable() == uw); \
399*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(fi.is_owner_executable() == ux); \
400*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(fi.is_group_readable() == gr); \
401*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(fi.is_group_writable() == gw); \
402*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(fi.is_group_executable() == gx); \
403*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(fi.is_other_readable() == othr); \
404*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(fi.is_other_writable() == othw); \
405*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(fi.is_other_executable() == othx); \
406*0a6a1f1dSLionel Sambuc     }
407*0a6a1f1dSLionel Sambuc 
408*0a6a1f1dSLionel Sambuc     ::chmod(p.c_str(), 0000);
409*0a6a1f1dSLionel Sambuc     perms(false, false, false, false, false, false, false, false, false);
410*0a6a1f1dSLionel Sambuc 
411*0a6a1f1dSLionel Sambuc     ::chmod(p.c_str(), 0001);
412*0a6a1f1dSLionel Sambuc     perms(false, false, false, false, false, false, false, false, true);
413*0a6a1f1dSLionel Sambuc 
414*0a6a1f1dSLionel Sambuc     ::chmod(p.c_str(), 0010);
415*0a6a1f1dSLionel Sambuc     perms(false, false, false, false, false, true, false, false, false);
416*0a6a1f1dSLionel Sambuc 
417*0a6a1f1dSLionel Sambuc     ::chmod(p.c_str(), 0100);
418*0a6a1f1dSLionel Sambuc     perms(false, false, true, false, false, false, false, false, false);
419*0a6a1f1dSLionel Sambuc 
420*0a6a1f1dSLionel Sambuc     ::chmod(p.c_str(), 0002);
421*0a6a1f1dSLionel Sambuc     perms(false, false, false, false, false, false, false, true, false);
422*0a6a1f1dSLionel Sambuc 
423*0a6a1f1dSLionel Sambuc     ::chmod(p.c_str(), 0020);
424*0a6a1f1dSLionel Sambuc     perms(false, false, false, false, true, false, false, false, false);
425*0a6a1f1dSLionel Sambuc 
426*0a6a1f1dSLionel Sambuc     ::chmod(p.c_str(), 0200);
427*0a6a1f1dSLionel Sambuc     perms(false, true, false, false, false, false, false, false, false);
428*0a6a1f1dSLionel Sambuc 
429*0a6a1f1dSLionel Sambuc     ::chmod(p.c_str(), 0004);
430*0a6a1f1dSLionel Sambuc     perms(false, false, false, false, false, false, true, false, false);
431*0a6a1f1dSLionel Sambuc 
432*0a6a1f1dSLionel Sambuc     ::chmod(p.c_str(), 0040);
433*0a6a1f1dSLionel Sambuc     perms(false, false, false, true, false, false, false, false, false);
434*0a6a1f1dSLionel Sambuc 
435*0a6a1f1dSLionel Sambuc     ::chmod(p.c_str(), 0400);
436*0a6a1f1dSLionel Sambuc     perms(true, false, false, false, false, false, false, false, false);
437*0a6a1f1dSLionel Sambuc 
438*0a6a1f1dSLionel Sambuc     ::chmod(p.c_str(), 0644);
439*0a6a1f1dSLionel Sambuc     perms(true, true, false, true, false, false, true, false, false);
440*0a6a1f1dSLionel Sambuc 
441*0a6a1f1dSLionel Sambuc     ::chmod(p.c_str(), 0755);
442*0a6a1f1dSLionel Sambuc     perms(true, true, true, true, false, true, true, false, true);
443*0a6a1f1dSLionel Sambuc 
444*0a6a1f1dSLionel Sambuc     ::chmod(p.c_str(), 0777);
445*0a6a1f1dSLionel Sambuc     perms(true, true, true, true, true, true, true, true, true);
446*0a6a1f1dSLionel Sambuc 
447*0a6a1f1dSLionel Sambuc #undef perms
448*0a6a1f1dSLionel Sambuc }
449*0a6a1f1dSLionel Sambuc 
450*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
451*0a6a1f1dSLionel Sambuc // Test cases for the "temp_dir" class.
452*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
453*0a6a1f1dSLionel Sambuc 
454*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(temp_dir_raii);
ATF_TEST_CASE_HEAD(temp_dir_raii)455*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(temp_dir_raii)
456*0a6a1f1dSLionel Sambuc {
457*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests the RAII behavior of the temp_dir class");
458*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(temp_dir_raii)459*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(temp_dir_raii)
460*0a6a1f1dSLionel Sambuc {
461*0a6a1f1dSLionel Sambuc     tools::fs::path t1("non-existent");
462*0a6a1f1dSLionel Sambuc     tools::fs::path t2("non-existent");
463*0a6a1f1dSLionel Sambuc 
464*0a6a1f1dSLionel Sambuc     {
465*0a6a1f1dSLionel Sambuc         tools::fs::path tmpl("testdir.XXXXXX");
466*0a6a1f1dSLionel Sambuc         tools::fs::temp_dir td1(tmpl);
467*0a6a1f1dSLionel Sambuc         tools::fs::temp_dir td2(tmpl);
468*0a6a1f1dSLionel Sambuc         t1 = td1.get_path();
469*0a6a1f1dSLionel Sambuc         t2 = td2.get_path();
470*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(t1.str().find("XXXXXX") == std::string::npos);
471*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(t2.str().find("XXXXXX") == std::string::npos);
472*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(t1 != t2);
473*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(!tools::fs::exists(tmpl));
474*0a6a1f1dSLionel Sambuc         ATF_REQUIRE( tools::fs::exists(t1));
475*0a6a1f1dSLionel Sambuc         ATF_REQUIRE( tools::fs::exists(t2));
476*0a6a1f1dSLionel Sambuc 
477*0a6a1f1dSLionel Sambuc         tools::fs::file_info fi1(t1);
478*0a6a1f1dSLionel Sambuc         ATF_REQUIRE( fi1.is_owner_readable());
479*0a6a1f1dSLionel Sambuc         ATF_REQUIRE( fi1.is_owner_writable());
480*0a6a1f1dSLionel Sambuc         ATF_REQUIRE( fi1.is_owner_executable());
481*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(!fi1.is_group_readable());
482*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(!fi1.is_group_writable());
483*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(!fi1.is_group_executable());
484*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(!fi1.is_other_readable());
485*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(!fi1.is_other_writable());
486*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(!fi1.is_other_executable());
487*0a6a1f1dSLionel Sambuc 
488*0a6a1f1dSLionel Sambuc         tools::fs::file_info fi2(t2);
489*0a6a1f1dSLionel Sambuc         ATF_REQUIRE( fi2.is_owner_readable());
490*0a6a1f1dSLionel Sambuc         ATF_REQUIRE( fi2.is_owner_writable());
491*0a6a1f1dSLionel Sambuc         ATF_REQUIRE( fi2.is_owner_executable());
492*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(!fi2.is_group_readable());
493*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(!fi2.is_group_writable());
494*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(!fi2.is_group_executable());
495*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(!fi2.is_other_readable());
496*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(!fi2.is_other_writable());
497*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(!fi2.is_other_executable());
498*0a6a1f1dSLionel Sambuc     }
499*0a6a1f1dSLionel Sambuc 
500*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(t1.str() != "non-existent");
501*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(!tools::fs::exists(t1));
502*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(t2.str() != "non-existent");
503*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(!tools::fs::exists(t2));
504*0a6a1f1dSLionel Sambuc }
505*0a6a1f1dSLionel Sambuc 
506*0a6a1f1dSLionel Sambuc 
507*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
508*0a6a1f1dSLionel Sambuc // Test cases for the free functions.
509*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
510*0a6a1f1dSLionel Sambuc 
511*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(exists);
ATF_TEST_CASE_HEAD(exists)512*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(exists)
513*0a6a1f1dSLionel Sambuc {
514*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests the exists function");
515*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(exists)516*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(exists)
517*0a6a1f1dSLionel Sambuc {
518*0a6a1f1dSLionel Sambuc     using tools::fs::exists;
519*0a6a1f1dSLionel Sambuc     using tools::fs::path;
520*0a6a1f1dSLionel Sambuc 
521*0a6a1f1dSLionel Sambuc     create_files();
522*0a6a1f1dSLionel Sambuc 
523*0a6a1f1dSLionel Sambuc     ATF_REQUIRE( exists(path("files")));
524*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(!exists(path("file")));
525*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(!exists(path("files2")));
526*0a6a1f1dSLionel Sambuc 
527*0a6a1f1dSLionel Sambuc     ATF_REQUIRE( exists(path("files/.")));
528*0a6a1f1dSLionel Sambuc     ATF_REQUIRE( exists(path("files/..")));
529*0a6a1f1dSLionel Sambuc     ATF_REQUIRE( exists(path("files/dir")));
530*0a6a1f1dSLionel Sambuc     ATF_REQUIRE( exists(path("files/reg")));
531*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(!exists(path("files/foo")));
532*0a6a1f1dSLionel Sambuc }
533*0a6a1f1dSLionel Sambuc 
534*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(is_executable);
ATF_TEST_CASE_HEAD(is_executable)535*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(is_executable)
536*0a6a1f1dSLionel Sambuc {
537*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests the is_executable function");
538*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(is_executable)539*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(is_executable)
540*0a6a1f1dSLionel Sambuc {
541*0a6a1f1dSLionel Sambuc     using tools::fs::is_executable;
542*0a6a1f1dSLionel Sambuc     using tools::fs::path;
543*0a6a1f1dSLionel Sambuc 
544*0a6a1f1dSLionel Sambuc     create_files();
545*0a6a1f1dSLionel Sambuc 
546*0a6a1f1dSLionel Sambuc     ATF_REQUIRE( is_executable(path("files")));
547*0a6a1f1dSLionel Sambuc     ATF_REQUIRE( is_executable(path("files/.")));
548*0a6a1f1dSLionel Sambuc     ATF_REQUIRE( is_executable(path("files/..")));
549*0a6a1f1dSLionel Sambuc     ATF_REQUIRE( is_executable(path("files/dir")));
550*0a6a1f1dSLionel Sambuc 
551*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(!is_executable(path("non-existent")));
552*0a6a1f1dSLionel Sambuc 
553*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(!is_executable(path("files/reg")));
554*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(::chmod("files/reg", 0755) != -1);
555*0a6a1f1dSLionel Sambuc     ATF_REQUIRE( is_executable(path("files/reg")));
556*0a6a1f1dSLionel Sambuc }
557*0a6a1f1dSLionel Sambuc 
558*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(remove);
ATF_TEST_CASE_HEAD(remove)559*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(remove)
560*0a6a1f1dSLionel Sambuc {
561*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests the remove function");
562*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(remove)563*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(remove)
564*0a6a1f1dSLionel Sambuc {
565*0a6a1f1dSLionel Sambuc     using tools::fs::exists;
566*0a6a1f1dSLionel Sambuc     using tools::fs::path;
567*0a6a1f1dSLionel Sambuc     using tools::fs::remove;
568*0a6a1f1dSLionel Sambuc 
569*0a6a1f1dSLionel Sambuc     create_files();
570*0a6a1f1dSLionel Sambuc 
571*0a6a1f1dSLionel Sambuc     ATF_REQUIRE( exists(path("files/reg")));
572*0a6a1f1dSLionel Sambuc     remove(path("files/reg"));
573*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(!exists(path("files/reg")));
574*0a6a1f1dSLionel Sambuc 
575*0a6a1f1dSLionel Sambuc     ATF_REQUIRE( exists(path("files/dir")));
576*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_THROW(tools::system_error, remove(path("files/dir")));
577*0a6a1f1dSLionel Sambuc     ATF_REQUIRE( exists(path("files/dir")));
578*0a6a1f1dSLionel Sambuc }
579*0a6a1f1dSLionel Sambuc 
580*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(cleanup);
ATF_TEST_CASE_HEAD(cleanup)581*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(cleanup)
582*0a6a1f1dSLionel Sambuc {
583*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests the cleanup function");
584*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(cleanup)585*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(cleanup)
586*0a6a1f1dSLionel Sambuc {
587*0a6a1f1dSLionel Sambuc     using tools::fs::cleanup;
588*0a6a1f1dSLionel Sambuc 
589*0a6a1f1dSLionel Sambuc     ::mkdir("root", 0755);
590*0a6a1f1dSLionel Sambuc     ::mkdir("root/dir", 0755);
591*0a6a1f1dSLionel Sambuc     ::mkdir("root/dir/1", 0100);
592*0a6a1f1dSLionel Sambuc     ::mkdir("root/dir/2", 0644);
593*0a6a1f1dSLionel Sambuc     create_file("root/reg");
594*0a6a1f1dSLionel Sambuc 
595*0a6a1f1dSLionel Sambuc     tools::fs::path p("root");
596*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(tools::fs::exists(p));
597*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(tools::fs::exists(p / "dir"));
598*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(tools::fs::exists(p / "dir/1"));
599*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(tools::fs::exists(p / "dir/2"));
600*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(tools::fs::exists(p / "reg"));
601*0a6a1f1dSLionel Sambuc     cleanup(p);
602*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(!tools::fs::exists(p));
603*0a6a1f1dSLionel Sambuc }
604*0a6a1f1dSLionel Sambuc 
605*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(cleanup_eacces_on_root);
ATF_TEST_CASE_HEAD(cleanup_eacces_on_root)606*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(cleanup_eacces_on_root)
607*0a6a1f1dSLionel Sambuc {
608*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests the cleanup function");
609*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(cleanup_eacces_on_root)610*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(cleanup_eacces_on_root)
611*0a6a1f1dSLionel Sambuc {
612*0a6a1f1dSLionel Sambuc     using tools::fs::cleanup;
613*0a6a1f1dSLionel Sambuc 
614*0a6a1f1dSLionel Sambuc     ::mkdir("aux", 0755);
615*0a6a1f1dSLionel Sambuc     ::mkdir("aux/root", 0755);
616*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(::chmod("aux", 0555) != -1);
617*0a6a1f1dSLionel Sambuc 
618*0a6a1f1dSLionel Sambuc     try {
619*0a6a1f1dSLionel Sambuc         cleanup(tools::fs::path("aux/root"));
620*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(tools::user::is_root());
621*0a6a1f1dSLionel Sambuc     } catch (const tools::system_error& e) {
622*0a6a1f1dSLionel Sambuc         ATF_REQUIRE(!tools::user::is_root());
623*0a6a1f1dSLionel Sambuc         ATF_REQUIRE_EQ(EACCES, e.code());
624*0a6a1f1dSLionel Sambuc     }
625*0a6a1f1dSLionel Sambuc }
626*0a6a1f1dSLionel Sambuc 
627*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(cleanup_eacces_on_subdir);
ATF_TEST_CASE_HEAD(cleanup_eacces_on_subdir)628*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(cleanup_eacces_on_subdir)
629*0a6a1f1dSLionel Sambuc {
630*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests the cleanup function");
631*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(cleanup_eacces_on_subdir)632*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(cleanup_eacces_on_subdir)
633*0a6a1f1dSLionel Sambuc {
634*0a6a1f1dSLionel Sambuc     using tools::fs::cleanup;
635*0a6a1f1dSLionel Sambuc 
636*0a6a1f1dSLionel Sambuc     ::mkdir("root", 0755);
637*0a6a1f1dSLionel Sambuc     ::mkdir("root/1", 0755);
638*0a6a1f1dSLionel Sambuc     ::mkdir("root/1/2", 0755);
639*0a6a1f1dSLionel Sambuc     ::mkdir("root/1/2/3", 0755);
640*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(::chmod("root/1/2", 0555) != -1);
641*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(::chmod("root/1", 0555) != -1);
642*0a6a1f1dSLionel Sambuc 
643*0a6a1f1dSLionel Sambuc     const tools::fs::path p("root");
644*0a6a1f1dSLionel Sambuc     cleanup(p);
645*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(!tools::fs::exists(p));
646*0a6a1f1dSLionel Sambuc }
647*0a6a1f1dSLionel Sambuc 
648*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(change_directory);
ATF_TEST_CASE_HEAD(change_directory)649*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(change_directory)
650*0a6a1f1dSLionel Sambuc {
651*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests the change_directory function");
652*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(change_directory)653*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(change_directory)
654*0a6a1f1dSLionel Sambuc {
655*0a6a1f1dSLionel Sambuc     using tools::fs::change_directory;
656*0a6a1f1dSLionel Sambuc     using tools::fs::get_current_dir;
657*0a6a1f1dSLionel Sambuc 
658*0a6a1f1dSLionel Sambuc     ::mkdir("files", 0755);
659*0a6a1f1dSLionel Sambuc     ::mkdir("files/dir", 0755);
660*0a6a1f1dSLionel Sambuc     create_file("files/reg");
661*0a6a1f1dSLionel Sambuc 
662*0a6a1f1dSLionel Sambuc     const tools::fs::path old = get_current_dir();
663*0a6a1f1dSLionel Sambuc 
664*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_THROW(tools::system_error,
665*0a6a1f1dSLionel Sambuc                     change_directory(tools::fs::path("files/reg")));
666*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(get_current_dir() == old);
667*0a6a1f1dSLionel Sambuc 
668*0a6a1f1dSLionel Sambuc     tools::fs::path old2 = change_directory(tools::fs::path("files"));
669*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(old2 == old);
670*0a6a1f1dSLionel Sambuc     tools::fs::path old3 = change_directory(tools::fs::path("dir"));
671*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(old3 == old2 / "files");
672*0a6a1f1dSLionel Sambuc     tools::fs::path old4 = change_directory(tools::fs::path("../.."));
673*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(old4 == old3 / "dir");
674*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(get_current_dir() == old);
675*0a6a1f1dSLionel Sambuc }
676*0a6a1f1dSLionel Sambuc 
677*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(get_current_dir);
ATF_TEST_CASE_HEAD(get_current_dir)678*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(get_current_dir)
679*0a6a1f1dSLionel Sambuc {
680*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Tests the get_current_dir function");
681*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(get_current_dir)682*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(get_current_dir)
683*0a6a1f1dSLionel Sambuc {
684*0a6a1f1dSLionel Sambuc     using tools::fs::change_directory;
685*0a6a1f1dSLionel Sambuc     using tools::fs::get_current_dir;
686*0a6a1f1dSLionel Sambuc 
687*0a6a1f1dSLionel Sambuc     ::mkdir("files", 0755);
688*0a6a1f1dSLionel Sambuc     ::mkdir("files/dir", 0755);
689*0a6a1f1dSLionel Sambuc     create_file("files/reg");
690*0a6a1f1dSLionel Sambuc 
691*0a6a1f1dSLionel Sambuc     tools::fs::path curdir = get_current_dir();
692*0a6a1f1dSLionel Sambuc     change_directory(tools::fs::path("."));
693*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(get_current_dir() == curdir);
694*0a6a1f1dSLionel Sambuc     change_directory(tools::fs::path("files"));
695*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(get_current_dir() == curdir / "files");
696*0a6a1f1dSLionel Sambuc     change_directory(tools::fs::path("dir"));
697*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(get_current_dir() == curdir / "files/dir");
698*0a6a1f1dSLionel Sambuc     change_directory(tools::fs::path(".."));
699*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(get_current_dir() == curdir / "files");
700*0a6a1f1dSLionel Sambuc     change_directory(tools::fs::path(".."));
701*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(get_current_dir() == curdir);
702*0a6a1f1dSLionel Sambuc }
703*0a6a1f1dSLionel Sambuc 
704*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
705*0a6a1f1dSLionel Sambuc // Main.
706*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
707*0a6a1f1dSLionel Sambuc 
ATF_INIT_TEST_CASES(tcs)708*0a6a1f1dSLionel Sambuc ATF_INIT_TEST_CASES(tcs)
709*0a6a1f1dSLionel Sambuc {
710*0a6a1f1dSLionel Sambuc     // Add the tests for the "path" class.
711*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, path_normalize);
712*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, path_is_absolute);
713*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, path_is_root);
714*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, path_branch_path);
715*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, path_leaf_name);
716*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, path_compare_equal);
717*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, path_compare_different);
718*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, path_concat);
719*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, path_to_absolute);
720*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, path_op_less);
721*0a6a1f1dSLionel Sambuc 
722*0a6a1f1dSLionel Sambuc     // Add the tests for the "file_info" class.
723*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, file_info_stat);
724*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, file_info_perms);
725*0a6a1f1dSLionel Sambuc 
726*0a6a1f1dSLionel Sambuc     // Add the tests for the "directory" class.
727*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, directory_read);
728*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, directory_names);
729*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, directory_file_info);
730*0a6a1f1dSLionel Sambuc 
731*0a6a1f1dSLionel Sambuc     // Add the tests for the "temp_dir" class.
732*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, temp_dir_raii);
733*0a6a1f1dSLionel Sambuc 
734*0a6a1f1dSLionel Sambuc     // Add the tests for the free functions.
735*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, exists);
736*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_executable);
737*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, remove);
738*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, cleanup);
739*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, cleanup_eacces_on_root);
740*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, cleanup_eacces_on_subdir);
741*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, change_directory);
742*0a6a1f1dSLionel Sambuc     ATF_ADD_TEST_CASE(tcs, get_current_dir);
743*0a6a1f1dSLionel Sambuc }
744