Path: blob/main/contrib/atf/atf-c++/detail/fs_test.cpp
39563 views
// Copyright (c) 2007 The NetBSD Foundation, Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions5// are met:6// 1. Redistributions of source code must retain the above copyright7// notice, this list of conditions and the following disclaimer.8// 2. Redistributions in binary form must reproduce the above copyright9// notice, this list of conditions and the following disclaimer in the10// documentation and/or other materials provided with the distribution.11//12// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND13// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,14// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF15// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE19// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS20// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER21// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR22// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN23// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2425#include "atf-c++/detail/fs.hpp"2627extern "C" {28#include <sys/types.h>29#include <sys/stat.h>30}3132#include <fstream>33#include <cerrno>34#include <cstdio>3536#include <atf-c++.hpp>3738#include "atf-c++/detail/exceptions.hpp"3940// ------------------------------------------------------------------------41// Auxiliary functions.42// ------------------------------------------------------------------------4344static45void46create_files(void)47{48::mkdir("files", 0755);49::mkdir("files/dir", 0755);5051std::ofstream os("files/reg");52os.close();5354// TODO: Should create all other file types (blk, chr, fifo, lnk, sock)55// and test for them... but the underlying file system may not support56// most of these. Specially as we are working on /tmp, which can be57// mounted with flags such as "nodev". See how to deal with this58// situation.59}6061// ------------------------------------------------------------------------62// Test cases for the "path" class.63// ------------------------------------------------------------------------6465ATF_TEST_CASE(path_normalize);66ATF_TEST_CASE_HEAD(path_normalize)67{68set_md_var("descr", "Tests the path's normalization");69}70ATF_TEST_CASE_BODY(path_normalize)71{72using atf::fs::path;7374ATF_REQUIRE_EQ(path(".").str(), ".");75ATF_REQUIRE_EQ(path("..").str(), "..");7677ATF_REQUIRE_EQ(path("foo").str(), "foo");78ATF_REQUIRE_EQ(path("foo/bar").str(), "foo/bar");79ATF_REQUIRE_EQ(path("foo/bar/").str(), "foo/bar");8081ATF_REQUIRE_EQ(path("/foo").str(), "/foo");82ATF_REQUIRE_EQ(path("/foo/bar").str(), "/foo/bar");83ATF_REQUIRE_EQ(path("/foo/bar/").str(), "/foo/bar");8485ATF_REQUIRE_EQ(path("///foo").str(), "/foo");86ATF_REQUIRE_EQ(path("///foo///bar").str(), "/foo/bar");87ATF_REQUIRE_EQ(path("///foo///bar///").str(), "/foo/bar");88}8990ATF_TEST_CASE(path_is_absolute);91ATF_TEST_CASE_HEAD(path_is_absolute)92{93set_md_var("descr", "Tests the path::is_absolute function");94}95ATF_TEST_CASE_BODY(path_is_absolute)96{97using atf::fs::path;9899ATF_REQUIRE( path("/").is_absolute());100ATF_REQUIRE( path("////").is_absolute());101ATF_REQUIRE( path("////a").is_absolute());102ATF_REQUIRE( path("//a//").is_absolute());103ATF_REQUIRE(!path("a////").is_absolute());104ATF_REQUIRE(!path("../foo").is_absolute());105}106107ATF_TEST_CASE(path_is_root);108ATF_TEST_CASE_HEAD(path_is_root)109{110set_md_var("descr", "Tests the path::is_root function");111}112ATF_TEST_CASE_BODY(path_is_root)113{114using atf::fs::path;115116ATF_REQUIRE( path("/").is_root());117ATF_REQUIRE( path("////").is_root());118ATF_REQUIRE(!path("////a").is_root());119ATF_REQUIRE(!path("//a//").is_root());120ATF_REQUIRE(!path("a////").is_root());121ATF_REQUIRE(!path("../foo").is_root());122}123124ATF_TEST_CASE(path_branch_path);125ATF_TEST_CASE_HEAD(path_branch_path)126{127set_md_var("descr", "Tests the path::branch_path function");128}129ATF_TEST_CASE_BODY(path_branch_path)130{131using atf::fs::path;132133ATF_REQUIRE_EQ(path(".").branch_path().str(), ".");134ATF_REQUIRE_EQ(path("foo").branch_path().str(), ".");135ATF_REQUIRE_EQ(path("foo/bar").branch_path().str(), "foo");136ATF_REQUIRE_EQ(path("/foo").branch_path().str(), "/");137ATF_REQUIRE_EQ(path("/foo/bar").branch_path().str(), "/foo");138}139140ATF_TEST_CASE(path_leaf_name);141ATF_TEST_CASE_HEAD(path_leaf_name)142{143set_md_var("descr", "Tests the path::leaf_name function");144}145ATF_TEST_CASE_BODY(path_leaf_name)146{147using atf::fs::path;148149ATF_REQUIRE_EQ(path(".").leaf_name(), ".");150ATF_REQUIRE_EQ(path("foo").leaf_name(), "foo");151ATF_REQUIRE_EQ(path("foo/bar").leaf_name(), "bar");152ATF_REQUIRE_EQ(path("/foo").leaf_name(), "foo");153ATF_REQUIRE_EQ(path("/foo/bar").leaf_name(), "bar");154}155156ATF_TEST_CASE(path_compare_equal);157ATF_TEST_CASE_HEAD(path_compare_equal)158{159set_md_var("descr", "Tests the comparison for equality between paths");160}161ATF_TEST_CASE_BODY(path_compare_equal)162{163using atf::fs::path;164165ATF_REQUIRE(path("/") == path("///"));166ATF_REQUIRE(path("/a") == path("///a"));167ATF_REQUIRE(path("/a") == path("///a///"));168169ATF_REQUIRE(path("a/b/c") == path("a//b//c"));170ATF_REQUIRE(path("a/b/c") == path("a//b//c///"));171}172173ATF_TEST_CASE(path_compare_different);174ATF_TEST_CASE_HEAD(path_compare_different)175{176set_md_var("descr", "Tests the comparison for difference between paths");177}178ATF_TEST_CASE_BODY(path_compare_different)179{180using atf::fs::path;181182ATF_REQUIRE(path("/") != path("//a/"));183ATF_REQUIRE(path("/a") != path("a///"));184185ATF_REQUIRE(path("a/b/c") != path("a/b"));186ATF_REQUIRE(path("a/b/c") != path("a//b"));187ATF_REQUIRE(path("a/b/c") != path("/a/b/c"));188ATF_REQUIRE(path("a/b/c") != path("/a//b//c"));189}190191ATF_TEST_CASE(path_concat);192ATF_TEST_CASE_HEAD(path_concat)193{194set_md_var("descr", "Tests the concatenation of multiple paths");195}196ATF_TEST_CASE_BODY(path_concat)197{198using atf::fs::path;199200ATF_REQUIRE_EQ((path("foo") / "bar").str(), "foo/bar");201ATF_REQUIRE_EQ((path("foo/") / "/bar").str(), "foo/bar");202ATF_REQUIRE_EQ((path("foo/") / "/bar/baz").str(), "foo/bar/baz");203ATF_REQUIRE_EQ((path("foo/") / "///bar///baz").str(), "foo/bar/baz");204}205206ATF_TEST_CASE(path_to_absolute);207ATF_TEST_CASE_HEAD(path_to_absolute)208{209set_md_var("descr", "Tests the conversion of a relative path to an "210"absolute one");211}212ATF_TEST_CASE_BODY(path_to_absolute)213{214using atf::fs::file_info;215using atf::fs::path;216217create_files();218219{220const path p(".");221path pa = p.to_absolute();222ATF_REQUIRE(pa.is_absolute());223224file_info fi(p);225file_info fia(pa);226ATF_REQUIRE_EQ(fi.get_device(), fia.get_device());227ATF_REQUIRE_EQ(fi.get_inode(), fia.get_inode());228}229230{231const path p("files/reg");232path pa = p.to_absolute();233ATF_REQUIRE(pa.is_absolute());234235file_info fi(p);236file_info fia(pa);237ATF_REQUIRE_EQ(fi.get_device(), fia.get_device());238ATF_REQUIRE_EQ(fi.get_inode(), fia.get_inode());239}240}241242ATF_TEST_CASE(path_op_less);243ATF_TEST_CASE_HEAD(path_op_less)244{245set_md_var("descr", "Tests that the path's less-than operator works");246}247ATF_TEST_CASE_BODY(path_op_less)248{249using atf::fs::path;250251create_files();252253ATF_REQUIRE(!(path("aaa") < path("aaa")));254255ATF_REQUIRE( path("aab") < path("abc"));256ATF_REQUIRE(!(path("abc") < path("aab")));257}258259// ------------------------------------------------------------------------260// Test cases for the "directory" class.261// ------------------------------------------------------------------------262263ATF_TEST_CASE(directory_read);264ATF_TEST_CASE_HEAD(directory_read)265{266set_md_var("descr", "Tests the directory class creation, which reads "267"the contents of a directory");268}269ATF_TEST_CASE_BODY(directory_read)270{271using atf::fs::directory;272using atf::fs::path;273274create_files();275276directory d(path("files"));277ATF_REQUIRE_EQ(d.size(), 4);278ATF_REQUIRE(d.find(".") != d.end());279ATF_REQUIRE(d.find("..") != d.end());280ATF_REQUIRE(d.find("dir") != d.end());281ATF_REQUIRE(d.find("reg") != d.end());282}283284ATF_TEST_CASE(directory_file_info);285ATF_TEST_CASE_HEAD(directory_file_info)286{287set_md_var("descr", "Tests that the file_info objects attached to the "288"directory are valid");289}290ATF_TEST_CASE_BODY(directory_file_info)291{292using atf::fs::directory;293using atf::fs::file_info;294using atf::fs::path;295296create_files();297298directory d(path("files"));299300{301directory::const_iterator iter = d.find("dir");302ATF_REQUIRE(iter != d.end());303const file_info& fi = (*iter).second;304ATF_REQUIRE(fi.get_type() == file_info::dir_type);305}306307{308directory::const_iterator iter = d.find("reg");309ATF_REQUIRE(iter != d.end());310const file_info& fi = (*iter).second;311ATF_REQUIRE(fi.get_type() == file_info::reg_type);312}313}314315ATF_TEST_CASE(directory_names);316ATF_TEST_CASE_HEAD(directory_names)317{318set_md_var("descr", "Tests the directory's names method");319}320ATF_TEST_CASE_BODY(directory_names)321{322using atf::fs::directory;323using atf::fs::path;324325create_files();326327directory d(path("files"));328std::set< std::string > ns = d.names();329ATF_REQUIRE_EQ(ns.size(), 4);330ATF_REQUIRE(ns.find(".") != ns.end());331ATF_REQUIRE(ns.find("..") != ns.end());332ATF_REQUIRE(ns.find("dir") != ns.end());333ATF_REQUIRE(ns.find("reg") != ns.end());334}335336// ------------------------------------------------------------------------337// Test cases for the "file_info" class.338// ------------------------------------------------------------------------339340ATF_TEST_CASE(file_info_stat);341ATF_TEST_CASE_HEAD(file_info_stat)342{343set_md_var("descr", "Tests the file_info creation and its basic contents");344}345ATF_TEST_CASE_BODY(file_info_stat)346{347using atf::fs::file_info;348using atf::fs::path;349350create_files();351352{353path p("files/dir");354file_info fi(p);355ATF_REQUIRE(fi.get_type() == file_info::dir_type);356}357358{359path p("files/reg");360file_info fi(p);361ATF_REQUIRE(fi.get_type() == file_info::reg_type);362}363}364365ATF_TEST_CASE(file_info_perms);366ATF_TEST_CASE_HEAD(file_info_perms)367{368set_md_var("descr", "Tests the file_info methods to get the file's "369"permissions");370}371ATF_TEST_CASE_BODY(file_info_perms)372{373using atf::fs::file_info;374using atf::fs::path;375376path p("file");377378std::ofstream os(p.c_str());379os.close();380381#define perms(ur, uw, ux, gr, gw, gx, othr, othw, othx) \382{ \383file_info fi(p); \384ATF_REQUIRE(fi.is_owner_readable() == ur); \385ATF_REQUIRE(fi.is_owner_writable() == uw); \386ATF_REQUIRE(fi.is_owner_executable() == ux); \387ATF_REQUIRE(fi.is_group_readable() == gr); \388ATF_REQUIRE(fi.is_group_writable() == gw); \389ATF_REQUIRE(fi.is_group_executable() == gx); \390ATF_REQUIRE(fi.is_other_readable() == othr); \391ATF_REQUIRE(fi.is_other_writable() == othw); \392ATF_REQUIRE(fi.is_other_executable() == othx); \393}394395::chmod(p.c_str(), 0000);396perms(false, false, false, false, false, false, false, false, false);397398::chmod(p.c_str(), 0001);399perms(false, false, false, false, false, false, false, false, true);400401::chmod(p.c_str(), 0010);402perms(false, false, false, false, false, true, false, false, false);403404::chmod(p.c_str(), 0100);405perms(false, false, true, false, false, false, false, false, false);406407::chmod(p.c_str(), 0002);408perms(false, false, false, false, false, false, false, true, false);409410::chmod(p.c_str(), 0020);411perms(false, false, false, false, true, false, false, false, false);412413::chmod(p.c_str(), 0200);414perms(false, true, false, false, false, false, false, false, false);415416::chmod(p.c_str(), 0004);417perms(false, false, false, false, false, false, true, false, false);418419::chmod(p.c_str(), 0040);420perms(false, false, false, true, false, false, false, false, false);421422::chmod(p.c_str(), 0400);423perms(true, false, false, false, false, false, false, false, false);424425::chmod(p.c_str(), 0644);426perms(true, true, false, true, false, false, true, false, false);427428::chmod(p.c_str(), 0755);429perms(true, true, true, true, false, true, true, false, true);430431::chmod(p.c_str(), 0777);432perms(true, true, true, true, true, true, true, true, true);433434#undef perms435}436437// ------------------------------------------------------------------------438// Test cases for the free functions.439// ------------------------------------------------------------------------440441ATF_TEST_CASE(exists);442ATF_TEST_CASE_HEAD(exists)443{444set_md_var("descr", "Tests the exists function");445}446ATF_TEST_CASE_BODY(exists)447{448using atf::fs::exists;449using atf::fs::path;450451create_files();452453ATF_REQUIRE( exists(path("files")));454ATF_REQUIRE(!exists(path("file")));455ATF_REQUIRE(!exists(path("files2")));456457ATF_REQUIRE( exists(path("files/.")));458ATF_REQUIRE( exists(path("files/..")));459ATF_REQUIRE( exists(path("files/dir")));460ATF_REQUIRE( exists(path("files/reg")));461ATF_REQUIRE(!exists(path("files/foo")));462}463464ATF_TEST_CASE(is_executable);465ATF_TEST_CASE_HEAD(is_executable)466{467set_md_var("descr", "Tests the is_executable function");468}469ATF_TEST_CASE_BODY(is_executable)470{471using atf::fs::is_executable;472using atf::fs::path;473474create_files();475476ATF_REQUIRE( is_executable(path("files")));477ATF_REQUIRE( is_executable(path("files/.")));478ATF_REQUIRE( is_executable(path("files/..")));479ATF_REQUIRE( is_executable(path("files/dir")));480481ATF_REQUIRE(!is_executable(path("non-existent")));482483ATF_REQUIRE(!is_executable(path("files/reg")));484ATF_REQUIRE(::chmod("files/reg", 0755) != -1);485ATF_REQUIRE( is_executable(path("files/reg")));486}487488ATF_TEST_CASE(remove);489ATF_TEST_CASE_HEAD(remove)490{491set_md_var("descr", "Tests the remove function");492}493ATF_TEST_CASE_BODY(remove)494{495using atf::fs::exists;496using atf::fs::path;497using atf::fs::remove;498499create_files();500501ATF_REQUIRE( exists(path("files/reg")));502remove(path("files/reg"));503ATF_REQUIRE(!exists(path("files/reg")));504505ATF_REQUIRE( exists(path("files/dir")));506ATF_REQUIRE_THROW(atf::system_error, remove(path("files/dir")));507ATF_REQUIRE( exists(path("files/dir")));508}509510// ------------------------------------------------------------------------511// Main.512// ------------------------------------------------------------------------513514ATF_INIT_TEST_CASES(tcs)515{516// Add the tests for the "path" class.517ATF_ADD_TEST_CASE(tcs, path_normalize);518ATF_ADD_TEST_CASE(tcs, path_is_absolute);519ATF_ADD_TEST_CASE(tcs, path_is_root);520ATF_ADD_TEST_CASE(tcs, path_branch_path);521ATF_ADD_TEST_CASE(tcs, path_leaf_name);522ATF_ADD_TEST_CASE(tcs, path_compare_equal);523ATF_ADD_TEST_CASE(tcs, path_compare_different);524ATF_ADD_TEST_CASE(tcs, path_concat);525ATF_ADD_TEST_CASE(tcs, path_to_absolute);526ATF_ADD_TEST_CASE(tcs, path_op_less);527528// Add the tests for the "file_info" class.529ATF_ADD_TEST_CASE(tcs, file_info_stat);530ATF_ADD_TEST_CASE(tcs, file_info_perms);531532// Add the tests for the "directory" class.533ATF_ADD_TEST_CASE(tcs, directory_read);534ATF_ADD_TEST_CASE(tcs, directory_names);535ATF_ADD_TEST_CASE(tcs, directory_file_info);536537// Add the tests for the free functions.538ATF_ADD_TEST_CASE(tcs, exists);539ATF_ADD_TEST_CASE(tcs, is_executable);540ATF_ADD_TEST_CASE(tcs, remove);541}542543544