Path: blob/main/contrib/kyua/utils/fs/auto_cleaners_test.cpp
48081 views
// Copyright 2010 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728#include "utils/fs/auto_cleaners.hpp"2930extern "C" {31#include <unistd.h>32}3334#include <atf-c++.hpp>3536#include "utils/env.hpp"37#include "utils/fs/operations.hpp"38#include "utils/fs/path.hpp"3940namespace fs = utils::fs;414243ATF_TEST_CASE_WITHOUT_HEAD(auto_directory__automatic);44ATF_TEST_CASE_BODY(auto_directory__automatic)45{46const fs::path root("root");47fs::mkdir(root, 0755);4849{50fs::auto_directory dir(root);51ATF_REQUIRE_EQ(root, dir.directory());5253ATF_REQUIRE(::access("root", X_OK) == 0);5455{56fs::auto_directory dir_copy(dir);57}58// Should still exist after a copy is destructed.59ATF_REQUIRE(::access("root", X_OK) == 0);60}61ATF_REQUIRE(::access("root", X_OK) == -1);62}636465ATF_TEST_CASE_WITHOUT_HEAD(auto_directory__explicit);66ATF_TEST_CASE_BODY(auto_directory__explicit)67{68const fs::path root("root");69fs::mkdir(root, 0755);7071fs::auto_directory dir(root);72ATF_REQUIRE_EQ(root, dir.directory());7374ATF_REQUIRE(::access("root", X_OK) == 0);75dir.cleanup();76dir.cleanup();77ATF_REQUIRE(::access("root", X_OK) == -1);78}798081ATF_TEST_CASE_WITHOUT_HEAD(auto_directory__mkdtemp_public);82ATF_TEST_CASE_BODY(auto_directory__mkdtemp_public)83{84utils::setenv("TMPDIR", (fs::current_path() / "tmp").str());85fs::mkdir(fs::path("tmp"), 0755);8687const std::string path_template("test.XXXXXX");88{89fs::auto_directory auto_directory = fs::auto_directory::mkdtemp_public(90path_template);91ATF_REQUIRE(::access((fs::path("tmp") / path_template).c_str(),92X_OK) == -1);93ATF_REQUIRE(::rmdir("tmp") == -1);9495ATF_REQUIRE(::access(auto_directory.directory().c_str(), X_OK) == 0);96}97ATF_REQUIRE(::rmdir("tmp") != -1);98}99100101ATF_TEST_CASE_WITHOUT_HEAD(auto_file__automatic);102ATF_TEST_CASE_BODY(auto_file__automatic)103{104const fs::path file("foo");105atf::utils::create_file(file.str(), "");106{107fs::auto_file auto_file(file);108ATF_REQUIRE_EQ(file, auto_file.file());109110ATF_REQUIRE(::access(file.c_str(), R_OK) == 0);111112{113fs::auto_file auto_file_copy(auto_file);114}115// Should still exist after a copy is destructed.116ATF_REQUIRE(::access(file.c_str(), R_OK) == 0);117}118ATF_REQUIRE(::access(file.c_str(), R_OK) == -1);119}120121122ATF_TEST_CASE_WITHOUT_HEAD(auto_file__explicit);123ATF_TEST_CASE_BODY(auto_file__explicit)124{125const fs::path file("bar");126atf::utils::create_file(file.str(), "");127128fs::auto_file auto_file(file);129ATF_REQUIRE_EQ(file, auto_file.file());130131ATF_REQUIRE(::access(file.c_str(), R_OK) == 0);132auto_file.remove();133auto_file.remove();134ATF_REQUIRE(::access(file.c_str(), R_OK) == -1);135}136137138ATF_TEST_CASE_WITHOUT_HEAD(auto_file__mkstemp);139ATF_TEST_CASE_BODY(auto_file__mkstemp)140{141utils::setenv("TMPDIR", (fs::current_path() / "tmp").str());142fs::mkdir(fs::path("tmp"), 0755);143144const std::string path_template("test.XXXXXX");145{146fs::auto_file auto_file = fs::auto_file::mkstemp(path_template);147ATF_REQUIRE(::access((fs::path("tmp") / path_template).c_str(),148X_OK) == -1);149ATF_REQUIRE(::rmdir("tmp") == -1);150151ATF_REQUIRE(::access(auto_file.file().c_str(), R_OK) == 0);152}153ATF_REQUIRE(::rmdir("tmp") != -1);154}155156157ATF_INIT_TEST_CASES(tcs)158{159ATF_ADD_TEST_CASE(tcs, auto_directory__automatic);160ATF_ADD_TEST_CASE(tcs, auto_directory__explicit);161ATF_ADD_TEST_CASE(tcs, auto_directory__mkdtemp_public);162163ATF_ADD_TEST_CASE(tcs, auto_file__automatic);164ATF_ADD_TEST_CASE(tcs, auto_file__explicit);165ATF_ADD_TEST_CASE(tcs, auto_file__mkstemp);166}167168169