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