xref: /minix3/external/bsd/atf/dist/atf-c++/detail/fs_test.cpp (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc //
2*11be35a1SLionel Sambuc // Automated Testing Framework (atf)
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Copyright (c) 2007 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc // All rights reserved.
6*11be35a1SLionel Sambuc //
7*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc // are met:
10*11be35a1SLionel Sambuc // 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc //    notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc // 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc //    notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc //    documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc //
16*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*11be35a1SLionel Sambuc // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*11be35a1SLionel Sambuc // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*11be35a1SLionel Sambuc // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*11be35a1SLionel Sambuc // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*11be35a1SLionel Sambuc // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*11be35a1SLionel Sambuc // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*11be35a1SLionel Sambuc // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*11be35a1SLionel Sambuc // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*11be35a1SLionel Sambuc // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*11be35a1SLionel Sambuc // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*11be35a1SLionel Sambuc // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc //
29*11be35a1SLionel Sambuc 
30*11be35a1SLionel Sambuc extern "C" {
31*11be35a1SLionel Sambuc #include <sys/types.h>
32*11be35a1SLionel Sambuc #include <sys/stat.h>
33*11be35a1SLionel Sambuc }
34*11be35a1SLionel Sambuc 
35*11be35a1SLionel Sambuc #include <fstream>
36*11be35a1SLionel Sambuc #include <cerrno>
37*11be35a1SLionel Sambuc #include <cstdio>
38*11be35a1SLionel Sambuc 
39*11be35a1SLionel Sambuc #include "../macros.hpp"
40*11be35a1SLionel Sambuc 
41*11be35a1SLionel Sambuc #include "exceptions.hpp"
42*11be35a1SLionel Sambuc #include "fs.hpp"
43*11be35a1SLionel Sambuc 
44*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
45*11be35a1SLionel Sambuc // Auxiliary functions.
46*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
47*11be35a1SLionel Sambuc 
48*11be35a1SLionel Sambuc static
49*11be35a1SLionel Sambuc void
create_files(void)50*11be35a1SLionel Sambuc create_files(void)
51*11be35a1SLionel Sambuc {
52*11be35a1SLionel Sambuc     ::mkdir("files", 0755);
53*11be35a1SLionel Sambuc     ::mkdir("files/dir", 0755);
54*11be35a1SLionel Sambuc 
55*11be35a1SLionel Sambuc     std::ofstream os("files/reg");
56*11be35a1SLionel Sambuc     os.close();
57*11be35a1SLionel Sambuc 
58*11be35a1SLionel Sambuc     // TODO: Should create all other file types (blk, chr, fifo, lnk, sock)
59*11be35a1SLionel Sambuc     // and test for them... but the underlying file system may not support
60*11be35a1SLionel Sambuc     // most of these.  Specially as we are working on /tmp, which can be
61*11be35a1SLionel Sambuc     // mounted with flags such as "nodev".  See how to deal with this
62*11be35a1SLionel Sambuc     // situation.
63*11be35a1SLionel Sambuc }
64*11be35a1SLionel Sambuc 
65*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
66*11be35a1SLionel Sambuc // Test cases for the "path" class.
67*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
68*11be35a1SLionel Sambuc 
69*11be35a1SLionel Sambuc ATF_TEST_CASE(path_normalize);
ATF_TEST_CASE_HEAD(path_normalize)70*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(path_normalize)
71*11be35a1SLionel Sambuc {
72*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the path's normalization");
73*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(path_normalize)74*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(path_normalize)
75*11be35a1SLionel Sambuc {
76*11be35a1SLionel Sambuc     using atf::fs::path;
77*11be35a1SLionel Sambuc 
78*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(path(".").str(), ".");
79*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(path("..").str(), "..");
80*11be35a1SLionel Sambuc 
81*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(path("foo").str(), "foo");
82*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(path("foo/bar").str(), "foo/bar");
83*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(path("foo/bar/").str(), "foo/bar");
84*11be35a1SLionel Sambuc 
85*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(path("/foo").str(), "/foo");
86*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(path("/foo/bar").str(), "/foo/bar");
87*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(path("/foo/bar/").str(), "/foo/bar");
88*11be35a1SLionel Sambuc 
89*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(path("///foo").str(), "/foo");
90*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(path("///foo///bar").str(), "/foo/bar");
91*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(path("///foo///bar///").str(), "/foo/bar");
92*11be35a1SLionel Sambuc }
93*11be35a1SLionel Sambuc 
94*11be35a1SLionel Sambuc ATF_TEST_CASE(path_is_absolute);
ATF_TEST_CASE_HEAD(path_is_absolute)95*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(path_is_absolute)
96*11be35a1SLionel Sambuc {
97*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the path::is_absolute function");
98*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(path_is_absolute)99*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(path_is_absolute)
100*11be35a1SLionel Sambuc {
101*11be35a1SLionel Sambuc     using atf::fs::path;
102*11be35a1SLionel Sambuc 
103*11be35a1SLionel Sambuc     ATF_REQUIRE( path("/").is_absolute());
104*11be35a1SLionel Sambuc     ATF_REQUIRE( path("////").is_absolute());
105*11be35a1SLionel Sambuc     ATF_REQUIRE( path("////a").is_absolute());
106*11be35a1SLionel Sambuc     ATF_REQUIRE( path("//a//").is_absolute());
107*11be35a1SLionel Sambuc     ATF_REQUIRE(!path("a////").is_absolute());
108*11be35a1SLionel Sambuc     ATF_REQUIRE(!path("../foo").is_absolute());
109*11be35a1SLionel Sambuc }
110*11be35a1SLionel Sambuc 
111*11be35a1SLionel Sambuc ATF_TEST_CASE(path_is_root);
ATF_TEST_CASE_HEAD(path_is_root)112*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(path_is_root)
113*11be35a1SLionel Sambuc {
114*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the path::is_root function");
115*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(path_is_root)116*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(path_is_root)
117*11be35a1SLionel Sambuc {
118*11be35a1SLionel Sambuc     using atf::fs::path;
119*11be35a1SLionel Sambuc 
120*11be35a1SLionel Sambuc     ATF_REQUIRE( path("/").is_root());
121*11be35a1SLionel Sambuc     ATF_REQUIRE( path("////").is_root());
122*11be35a1SLionel Sambuc     ATF_REQUIRE(!path("////a").is_root());
123*11be35a1SLionel Sambuc     ATF_REQUIRE(!path("//a//").is_root());
124*11be35a1SLionel Sambuc     ATF_REQUIRE(!path("a////").is_root());
125*11be35a1SLionel Sambuc     ATF_REQUIRE(!path("../foo").is_root());
126*11be35a1SLionel Sambuc }
127*11be35a1SLionel Sambuc 
128*11be35a1SLionel Sambuc ATF_TEST_CASE(path_branch_path);
ATF_TEST_CASE_HEAD(path_branch_path)129*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(path_branch_path)
130*11be35a1SLionel Sambuc {
131*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the path::branch_path function");
132*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(path_branch_path)133*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(path_branch_path)
134*11be35a1SLionel Sambuc {
135*11be35a1SLionel Sambuc     using atf::fs::path;
136*11be35a1SLionel Sambuc 
137*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(path(".").branch_path().str(), ".");
138*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(path("foo").branch_path().str(), ".");
139*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(path("foo/bar").branch_path().str(), "foo");
140*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(path("/foo").branch_path().str(), "/");
141*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(path("/foo/bar").branch_path().str(), "/foo");
142*11be35a1SLionel Sambuc }
143*11be35a1SLionel Sambuc 
144*11be35a1SLionel Sambuc ATF_TEST_CASE(path_leaf_name);
ATF_TEST_CASE_HEAD(path_leaf_name)145*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(path_leaf_name)
146*11be35a1SLionel Sambuc {
147*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the path::leaf_name function");
148*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(path_leaf_name)149*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(path_leaf_name)
150*11be35a1SLionel Sambuc {
151*11be35a1SLionel Sambuc     using atf::fs::path;
152*11be35a1SLionel Sambuc 
153*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(path(".").leaf_name(), ".");
154*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(path("foo").leaf_name(), "foo");
155*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(path("foo/bar").leaf_name(), "bar");
156*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(path("/foo").leaf_name(), "foo");
157*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(path("/foo/bar").leaf_name(), "bar");
158*11be35a1SLionel Sambuc }
159*11be35a1SLionel Sambuc 
160*11be35a1SLionel Sambuc ATF_TEST_CASE(path_compare_equal);
ATF_TEST_CASE_HEAD(path_compare_equal)161*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(path_compare_equal)
162*11be35a1SLionel Sambuc {
163*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the comparison for equality between paths");
164*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(path_compare_equal)165*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(path_compare_equal)
166*11be35a1SLionel Sambuc {
167*11be35a1SLionel Sambuc     using atf::fs::path;
168*11be35a1SLionel Sambuc 
169*11be35a1SLionel Sambuc     ATF_REQUIRE(path("/") == path("///"));
170*11be35a1SLionel Sambuc     ATF_REQUIRE(path("/a") == path("///a"));
171*11be35a1SLionel Sambuc     ATF_REQUIRE(path("/a") == path("///a///"));
172*11be35a1SLionel Sambuc 
173*11be35a1SLionel Sambuc     ATF_REQUIRE(path("a/b/c") == path("a//b//c"));
174*11be35a1SLionel Sambuc     ATF_REQUIRE(path("a/b/c") == path("a//b//c///"));
175*11be35a1SLionel Sambuc }
176*11be35a1SLionel Sambuc 
177*11be35a1SLionel Sambuc ATF_TEST_CASE(path_compare_different);
ATF_TEST_CASE_HEAD(path_compare_different)178*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(path_compare_different)
179*11be35a1SLionel Sambuc {
180*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the comparison for difference between paths");
181*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(path_compare_different)182*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(path_compare_different)
183*11be35a1SLionel Sambuc {
184*11be35a1SLionel Sambuc     using atf::fs::path;
185*11be35a1SLionel Sambuc 
186*11be35a1SLionel Sambuc     ATF_REQUIRE(path("/") != path("//a/"));
187*11be35a1SLionel Sambuc     ATF_REQUIRE(path("/a") != path("a///"));
188*11be35a1SLionel Sambuc 
189*11be35a1SLionel Sambuc     ATF_REQUIRE(path("a/b/c") != path("a/b"));
190*11be35a1SLionel Sambuc     ATF_REQUIRE(path("a/b/c") != path("a//b"));
191*11be35a1SLionel Sambuc     ATF_REQUIRE(path("a/b/c") != path("/a/b/c"));
192*11be35a1SLionel Sambuc     ATF_REQUIRE(path("a/b/c") != path("/a//b//c"));
193*11be35a1SLionel Sambuc }
194*11be35a1SLionel Sambuc 
195*11be35a1SLionel Sambuc ATF_TEST_CASE(path_concat);
ATF_TEST_CASE_HEAD(path_concat)196*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(path_concat)
197*11be35a1SLionel Sambuc {
198*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the concatenation of multiple paths");
199*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(path_concat)200*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(path_concat)
201*11be35a1SLionel Sambuc {
202*11be35a1SLionel Sambuc     using atf::fs::path;
203*11be35a1SLionel Sambuc 
204*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ((path("foo") / "bar").str(), "foo/bar");
205*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ((path("foo/") / "/bar").str(), "foo/bar");
206*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ((path("foo/") / "/bar/baz").str(), "foo/bar/baz");
207*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ((path("foo/") / "///bar///baz").str(), "foo/bar/baz");
208*11be35a1SLionel Sambuc }
209*11be35a1SLionel Sambuc 
210*11be35a1SLionel Sambuc ATF_TEST_CASE(path_to_absolute);
ATF_TEST_CASE_HEAD(path_to_absolute)211*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(path_to_absolute)
212*11be35a1SLionel Sambuc {
213*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the conversion of a relative path to an "
214*11be35a1SLionel Sambuc                "absolute one");
215*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(path_to_absolute)216*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(path_to_absolute)
217*11be35a1SLionel Sambuc {
218*11be35a1SLionel Sambuc     using atf::fs::file_info;
219*11be35a1SLionel Sambuc     using atf::fs::path;
220*11be35a1SLionel Sambuc 
221*11be35a1SLionel Sambuc     create_files();
222*11be35a1SLionel Sambuc 
223*11be35a1SLionel Sambuc     {
224*11be35a1SLionel Sambuc         const path p(".");
225*11be35a1SLionel Sambuc         path pa = p.to_absolute();
226*11be35a1SLionel Sambuc         ATF_REQUIRE(pa.is_absolute());
227*11be35a1SLionel Sambuc 
228*11be35a1SLionel Sambuc         file_info fi(p);
229*11be35a1SLionel Sambuc         file_info fia(pa);
230*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(fi.get_device(), fia.get_device());
231*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(fi.get_inode(), fia.get_inode());
232*11be35a1SLionel Sambuc     }
233*11be35a1SLionel Sambuc 
234*11be35a1SLionel Sambuc     {
235*11be35a1SLionel Sambuc         const path p("files/reg");
236*11be35a1SLionel Sambuc         path pa = p.to_absolute();
237*11be35a1SLionel Sambuc         ATF_REQUIRE(pa.is_absolute());
238*11be35a1SLionel Sambuc 
239*11be35a1SLionel Sambuc         file_info fi(p);
240*11be35a1SLionel Sambuc         file_info fia(pa);
241*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(fi.get_device(), fia.get_device());
242*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(fi.get_inode(), fia.get_inode());
243*11be35a1SLionel Sambuc     }
244*11be35a1SLionel Sambuc }
245*11be35a1SLionel Sambuc 
246*11be35a1SLionel Sambuc ATF_TEST_CASE(path_op_less);
ATF_TEST_CASE_HEAD(path_op_less)247*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(path_op_less)
248*11be35a1SLionel Sambuc {
249*11be35a1SLionel Sambuc     set_md_var("descr", "Tests that the path's less-than operator works");
250*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(path_op_less)251*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(path_op_less)
252*11be35a1SLionel Sambuc {
253*11be35a1SLionel Sambuc     using atf::fs::path;
254*11be35a1SLionel Sambuc 
255*11be35a1SLionel Sambuc     create_files();
256*11be35a1SLionel Sambuc 
257*11be35a1SLionel Sambuc     ATF_REQUIRE(!(path("aaa") < path("aaa")));
258*11be35a1SLionel Sambuc 
259*11be35a1SLionel Sambuc     ATF_REQUIRE(  path("aab") < path("abc"));
260*11be35a1SLionel Sambuc     ATF_REQUIRE(!(path("abc") < path("aab")));
261*11be35a1SLionel Sambuc }
262*11be35a1SLionel Sambuc 
263*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
264*11be35a1SLionel Sambuc // Test cases for the "directory" class.
265*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
266*11be35a1SLionel Sambuc 
267*11be35a1SLionel Sambuc ATF_TEST_CASE(directory_read);
ATF_TEST_CASE_HEAD(directory_read)268*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(directory_read)
269*11be35a1SLionel Sambuc {
270*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the directory class creation, which reads "
271*11be35a1SLionel Sambuc                "the contents of a directory");
272*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(directory_read)273*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(directory_read)
274*11be35a1SLionel Sambuc {
275*11be35a1SLionel Sambuc     using atf::fs::directory;
276*11be35a1SLionel Sambuc     using atf::fs::path;
277*11be35a1SLionel Sambuc 
278*11be35a1SLionel Sambuc     create_files();
279*11be35a1SLionel Sambuc 
280*11be35a1SLionel Sambuc     directory d(path("files"));
281*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(d.size(), 4);
282*11be35a1SLionel Sambuc     ATF_REQUIRE(d.find(".") != d.end());
283*11be35a1SLionel Sambuc     ATF_REQUIRE(d.find("..") != d.end());
284*11be35a1SLionel Sambuc     ATF_REQUIRE(d.find("dir") != d.end());
285*11be35a1SLionel Sambuc     ATF_REQUIRE(d.find("reg") != d.end());
286*11be35a1SLionel Sambuc }
287*11be35a1SLionel Sambuc 
288*11be35a1SLionel Sambuc ATF_TEST_CASE(directory_file_info);
ATF_TEST_CASE_HEAD(directory_file_info)289*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(directory_file_info)
290*11be35a1SLionel Sambuc {
291*11be35a1SLionel Sambuc     set_md_var("descr", "Tests that the file_info objects attached to the "
292*11be35a1SLionel Sambuc                "directory are valid");
293*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(directory_file_info)294*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(directory_file_info)
295*11be35a1SLionel Sambuc {
296*11be35a1SLionel Sambuc     using atf::fs::directory;
297*11be35a1SLionel Sambuc     using atf::fs::file_info;
298*11be35a1SLionel Sambuc     using atf::fs::path;
299*11be35a1SLionel Sambuc 
300*11be35a1SLionel Sambuc     create_files();
301*11be35a1SLionel Sambuc 
302*11be35a1SLionel Sambuc     directory d(path("files"));
303*11be35a1SLionel Sambuc 
304*11be35a1SLionel Sambuc     {
305*11be35a1SLionel Sambuc         directory::const_iterator iter = d.find("dir");
306*11be35a1SLionel Sambuc         ATF_REQUIRE(iter != d.end());
307*11be35a1SLionel Sambuc         const file_info& fi = (*iter).second;
308*11be35a1SLionel Sambuc         ATF_REQUIRE(fi.get_type() == file_info::dir_type);
309*11be35a1SLionel Sambuc     }
310*11be35a1SLionel Sambuc 
311*11be35a1SLionel Sambuc     {
312*11be35a1SLionel Sambuc         directory::const_iterator iter = d.find("reg");
313*11be35a1SLionel Sambuc         ATF_REQUIRE(iter != d.end());
314*11be35a1SLionel Sambuc         const file_info& fi = (*iter).second;
315*11be35a1SLionel Sambuc         ATF_REQUIRE(fi.get_type() == file_info::reg_type);
316*11be35a1SLionel Sambuc     }
317*11be35a1SLionel Sambuc }
318*11be35a1SLionel Sambuc 
319*11be35a1SLionel Sambuc ATF_TEST_CASE(directory_names);
ATF_TEST_CASE_HEAD(directory_names)320*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(directory_names)
321*11be35a1SLionel Sambuc {
322*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the directory's names method");
323*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(directory_names)324*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(directory_names)
325*11be35a1SLionel Sambuc {
326*11be35a1SLionel Sambuc     using atf::fs::directory;
327*11be35a1SLionel Sambuc     using atf::fs::path;
328*11be35a1SLionel Sambuc 
329*11be35a1SLionel Sambuc     create_files();
330*11be35a1SLionel Sambuc 
331*11be35a1SLionel Sambuc     directory d(path("files"));
332*11be35a1SLionel Sambuc     std::set< std::string > ns = d.names();
333*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(ns.size(), 4);
334*11be35a1SLionel Sambuc     ATF_REQUIRE(ns.find(".") != ns.end());
335*11be35a1SLionel Sambuc     ATF_REQUIRE(ns.find("..") != ns.end());
336*11be35a1SLionel Sambuc     ATF_REQUIRE(ns.find("dir") != ns.end());
337*11be35a1SLionel Sambuc     ATF_REQUIRE(ns.find("reg") != ns.end());
338*11be35a1SLionel Sambuc }
339*11be35a1SLionel Sambuc 
340*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
341*11be35a1SLionel Sambuc // Test cases for the "file_info" class.
342*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
343*11be35a1SLionel Sambuc 
344*11be35a1SLionel Sambuc ATF_TEST_CASE(file_info_stat);
ATF_TEST_CASE_HEAD(file_info_stat)345*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(file_info_stat)
346*11be35a1SLionel Sambuc {
347*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the file_info creation and its basic contents");
348*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(file_info_stat)349*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(file_info_stat)
350*11be35a1SLionel Sambuc {
351*11be35a1SLionel Sambuc     using atf::fs::file_info;
352*11be35a1SLionel Sambuc     using atf::fs::path;
353*11be35a1SLionel Sambuc 
354*11be35a1SLionel Sambuc     create_files();
355*11be35a1SLionel Sambuc 
356*11be35a1SLionel Sambuc     {
357*11be35a1SLionel Sambuc         path p("files/dir");
358*11be35a1SLionel Sambuc         file_info fi(p);
359*11be35a1SLionel Sambuc         ATF_REQUIRE(fi.get_type() == file_info::dir_type);
360*11be35a1SLionel Sambuc     }
361*11be35a1SLionel Sambuc 
362*11be35a1SLionel Sambuc     {
363*11be35a1SLionel Sambuc         path p("files/reg");
364*11be35a1SLionel Sambuc         file_info fi(p);
365*11be35a1SLionel Sambuc         ATF_REQUIRE(fi.get_type() == file_info::reg_type);
366*11be35a1SLionel Sambuc     }
367*11be35a1SLionel Sambuc }
368*11be35a1SLionel Sambuc 
369*11be35a1SLionel Sambuc ATF_TEST_CASE(file_info_perms);
ATF_TEST_CASE_HEAD(file_info_perms)370*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(file_info_perms)
371*11be35a1SLionel Sambuc {
372*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the file_info methods to get the file's "
373*11be35a1SLionel Sambuc                "permissions");
374*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(file_info_perms)375*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(file_info_perms)
376*11be35a1SLionel Sambuc {
377*11be35a1SLionel Sambuc     using atf::fs::file_info;
378*11be35a1SLionel Sambuc     using atf::fs::path;
379*11be35a1SLionel Sambuc 
380*11be35a1SLionel Sambuc     path p("file");
381*11be35a1SLionel Sambuc 
382*11be35a1SLionel Sambuc     std::ofstream os(p.c_str());
383*11be35a1SLionel Sambuc     os.close();
384*11be35a1SLionel Sambuc 
385*11be35a1SLionel Sambuc #define perms(ur, uw, ux, gr, gw, gx, othr, othw, othx) \
386*11be35a1SLionel Sambuc     { \
387*11be35a1SLionel Sambuc         file_info fi(p); \
388*11be35a1SLionel Sambuc         ATF_REQUIRE(fi.is_owner_readable() == ur); \
389*11be35a1SLionel Sambuc         ATF_REQUIRE(fi.is_owner_writable() == uw); \
390*11be35a1SLionel Sambuc         ATF_REQUIRE(fi.is_owner_executable() == ux); \
391*11be35a1SLionel Sambuc         ATF_REQUIRE(fi.is_group_readable() == gr); \
392*11be35a1SLionel Sambuc         ATF_REQUIRE(fi.is_group_writable() == gw); \
393*11be35a1SLionel Sambuc         ATF_REQUIRE(fi.is_group_executable() == gx); \
394*11be35a1SLionel Sambuc         ATF_REQUIRE(fi.is_other_readable() == othr); \
395*11be35a1SLionel Sambuc         ATF_REQUIRE(fi.is_other_writable() == othw); \
396*11be35a1SLionel Sambuc         ATF_REQUIRE(fi.is_other_executable() == othx); \
397*11be35a1SLionel Sambuc     }
398*11be35a1SLionel Sambuc 
399*11be35a1SLionel Sambuc     ::chmod(p.c_str(), 0000);
400*11be35a1SLionel Sambuc     perms(false, false, false, false, false, false, false, false, false);
401*11be35a1SLionel Sambuc 
402*11be35a1SLionel Sambuc     ::chmod(p.c_str(), 0001);
403*11be35a1SLionel Sambuc     perms(false, false, false, false, false, false, false, false, true);
404*11be35a1SLionel Sambuc 
405*11be35a1SLionel Sambuc     ::chmod(p.c_str(), 0010);
406*11be35a1SLionel Sambuc     perms(false, false, false, false, false, true, false, false, false);
407*11be35a1SLionel Sambuc 
408*11be35a1SLionel Sambuc     ::chmod(p.c_str(), 0100);
409*11be35a1SLionel Sambuc     perms(false, false, true, false, false, false, false, false, false);
410*11be35a1SLionel Sambuc 
411*11be35a1SLionel Sambuc     ::chmod(p.c_str(), 0002);
412*11be35a1SLionel Sambuc     perms(false, false, false, false, false, false, false, true, false);
413*11be35a1SLionel Sambuc 
414*11be35a1SLionel Sambuc     ::chmod(p.c_str(), 0020);
415*11be35a1SLionel Sambuc     perms(false, false, false, false, true, false, false, false, false);
416*11be35a1SLionel Sambuc 
417*11be35a1SLionel Sambuc     ::chmod(p.c_str(), 0200);
418*11be35a1SLionel Sambuc     perms(false, true, false, false, false, false, false, false, false);
419*11be35a1SLionel Sambuc 
420*11be35a1SLionel Sambuc     ::chmod(p.c_str(), 0004);
421*11be35a1SLionel Sambuc     perms(false, false, false, false, false, false, true, false, false);
422*11be35a1SLionel Sambuc 
423*11be35a1SLionel Sambuc     ::chmod(p.c_str(), 0040);
424*11be35a1SLionel Sambuc     perms(false, false, false, true, false, false, false, false, false);
425*11be35a1SLionel Sambuc 
426*11be35a1SLionel Sambuc     ::chmod(p.c_str(), 0400);
427*11be35a1SLionel Sambuc     perms(true, false, false, false, false, false, false, false, false);
428*11be35a1SLionel Sambuc 
429*11be35a1SLionel Sambuc     ::chmod(p.c_str(), 0644);
430*11be35a1SLionel Sambuc     perms(true, true, false, true, false, false, true, false, false);
431*11be35a1SLionel Sambuc 
432*11be35a1SLionel Sambuc     ::chmod(p.c_str(), 0755);
433*11be35a1SLionel Sambuc     perms(true, true, true, true, false, true, true, false, true);
434*11be35a1SLionel Sambuc 
435*11be35a1SLionel Sambuc     ::chmod(p.c_str(), 0777);
436*11be35a1SLionel Sambuc     perms(true, true, true, true, true, true, true, true, true);
437*11be35a1SLionel Sambuc 
438*11be35a1SLionel Sambuc #undef perms
439*11be35a1SLionel Sambuc }
440*11be35a1SLionel Sambuc 
441*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
442*11be35a1SLionel Sambuc // Test cases for the free functions.
443*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
444*11be35a1SLionel Sambuc 
445*11be35a1SLionel Sambuc ATF_TEST_CASE(exists);
ATF_TEST_CASE_HEAD(exists)446*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(exists)
447*11be35a1SLionel Sambuc {
448*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the exists function");
449*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(exists)450*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(exists)
451*11be35a1SLionel Sambuc {
452*11be35a1SLionel Sambuc     using atf::fs::exists;
453*11be35a1SLionel Sambuc     using atf::fs::path;
454*11be35a1SLionel Sambuc 
455*11be35a1SLionel Sambuc     create_files();
456*11be35a1SLionel Sambuc 
457*11be35a1SLionel Sambuc     ATF_REQUIRE( exists(path("files")));
458*11be35a1SLionel Sambuc     ATF_REQUIRE(!exists(path("file")));
459*11be35a1SLionel Sambuc     ATF_REQUIRE(!exists(path("files2")));
460*11be35a1SLionel Sambuc 
461*11be35a1SLionel Sambuc     ATF_REQUIRE( exists(path("files/.")));
462*11be35a1SLionel Sambuc     ATF_REQUIRE( exists(path("files/..")));
463*11be35a1SLionel Sambuc     ATF_REQUIRE( exists(path("files/dir")));
464*11be35a1SLionel Sambuc     ATF_REQUIRE( exists(path("files/reg")));
465*11be35a1SLionel Sambuc     ATF_REQUIRE(!exists(path("files/foo")));
466*11be35a1SLionel Sambuc }
467*11be35a1SLionel Sambuc 
468*11be35a1SLionel Sambuc ATF_TEST_CASE(is_executable);
ATF_TEST_CASE_HEAD(is_executable)469*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(is_executable)
470*11be35a1SLionel Sambuc {
471*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the is_executable function");
472*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(is_executable)473*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_executable)
474*11be35a1SLionel Sambuc {
475*11be35a1SLionel Sambuc     using atf::fs::is_executable;
476*11be35a1SLionel Sambuc     using atf::fs::path;
477*11be35a1SLionel Sambuc 
478*11be35a1SLionel Sambuc     create_files();
479*11be35a1SLionel Sambuc 
480*11be35a1SLionel Sambuc     ATF_REQUIRE( is_executable(path("files")));
481*11be35a1SLionel Sambuc     ATF_REQUIRE( is_executable(path("files/.")));
482*11be35a1SLionel Sambuc     ATF_REQUIRE( is_executable(path("files/..")));
483*11be35a1SLionel Sambuc     ATF_REQUIRE( is_executable(path("files/dir")));
484*11be35a1SLionel Sambuc 
485*11be35a1SLionel Sambuc     ATF_REQUIRE(!is_executable(path("non-existent")));
486*11be35a1SLionel Sambuc 
487*11be35a1SLionel Sambuc     ATF_REQUIRE(!is_executable(path("files/reg")));
488*11be35a1SLionel Sambuc     ATF_REQUIRE(::chmod("files/reg", 0755) != -1);
489*11be35a1SLionel Sambuc     ATF_REQUIRE( is_executable(path("files/reg")));
490*11be35a1SLionel Sambuc }
491*11be35a1SLionel Sambuc 
492*11be35a1SLionel Sambuc ATF_TEST_CASE(remove);
ATF_TEST_CASE_HEAD(remove)493*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(remove)
494*11be35a1SLionel Sambuc {
495*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the remove function");
496*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(remove)497*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(remove)
498*11be35a1SLionel Sambuc {
499*11be35a1SLionel Sambuc     using atf::fs::exists;
500*11be35a1SLionel Sambuc     using atf::fs::path;
501*11be35a1SLionel Sambuc     using atf::fs::remove;
502*11be35a1SLionel Sambuc 
503*11be35a1SLionel Sambuc     create_files();
504*11be35a1SLionel Sambuc 
505*11be35a1SLionel Sambuc     ATF_REQUIRE( exists(path("files/reg")));
506*11be35a1SLionel Sambuc     remove(path("files/reg"));
507*11be35a1SLionel Sambuc     ATF_REQUIRE(!exists(path("files/reg")));
508*11be35a1SLionel Sambuc 
509*11be35a1SLionel Sambuc     ATF_REQUIRE( exists(path("files/dir")));
510*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(atf::system_error, remove(path("files/dir")));
511*11be35a1SLionel Sambuc     ATF_REQUIRE( exists(path("files/dir")));
512*11be35a1SLionel Sambuc }
513*11be35a1SLionel Sambuc 
514*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
515*11be35a1SLionel Sambuc // Main.
516*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
517*11be35a1SLionel Sambuc 
ATF_INIT_TEST_CASES(tcs)518*11be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
519*11be35a1SLionel Sambuc {
520*11be35a1SLionel Sambuc     // Add the tests for the "path" class.
521*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, path_normalize);
522*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, path_is_absolute);
523*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, path_is_root);
524*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, path_branch_path);
525*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, path_leaf_name);
526*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, path_compare_equal);
527*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, path_compare_different);
528*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, path_concat);
529*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, path_to_absolute);
530*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, path_op_less);
531*11be35a1SLionel Sambuc 
532*11be35a1SLionel Sambuc     // Add the tests for the "file_info" class.
533*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, file_info_stat);
534*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, file_info_perms);
535*11be35a1SLionel Sambuc 
536*11be35a1SLionel Sambuc     // Add the tests for the "directory" class.
537*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, directory_read);
538*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, directory_names);
539*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, directory_file_info);
540*11be35a1SLionel Sambuc 
541*11be35a1SLionel Sambuc     // Add the tests for the free functions.
542*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, exists);
543*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_executable);
544*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, remove);
545*11be35a1SLionel Sambuc }
546