xref: /netbsd-src/external/bsd/kyua-cli/dist/utils/fs/auto_cleaners_test.cpp (revision 6b3a42af15b5e090c339512c790dd68f3d11a9d8)
1 // Copyright 2010 Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 //   notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 //   notice, this list of conditions and the following disclaimer in the
12 //   documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 //   may be used to endorse or promote products derived from this software
15 //   without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #include "utils/fs/auto_cleaners.hpp"
30 
31 extern "C" {
32 #include <unistd.h>
33 }
34 
35 #include <atf-c++.hpp>
36 
37 #include "utils/env.hpp"
38 #include "utils/fs/operations.hpp"
39 
40 namespace fs = utils::fs;
41 
42 
43 ATF_TEST_CASE_WITHOUT_HEAD(auto_directory__automatic);
ATF_TEST_CASE_BODY(auto_directory__automatic)44 ATF_TEST_CASE_BODY(auto_directory__automatic)
45 {
46     const fs::path root("root");
47     fs::mkdir(root, 0755);
48 
49     {
50         fs::auto_directory dir(root);
51         ATF_REQUIRE_EQ(root, dir.directory());
52 
53         ATF_REQUIRE(::access("root", X_OK) == 0);
54 
55         {
56             fs::auto_directory dir_copy(dir);
57         }
58         // Should still exist after a copy is destructed.
59         ATF_REQUIRE(::access("root", X_OK) == 0);
60     }
61     ATF_REQUIRE(::access("root", X_OK) == -1);
62 }
63 
64 
65 ATF_TEST_CASE_WITHOUT_HEAD(auto_directory__explicit);
ATF_TEST_CASE_BODY(auto_directory__explicit)66 ATF_TEST_CASE_BODY(auto_directory__explicit)
67 {
68     const fs::path root("root");
69     fs::mkdir(root, 0755);
70 
71     fs::auto_directory dir(root);
72     ATF_REQUIRE_EQ(root, dir.directory());
73 
74     ATF_REQUIRE(::access("root", X_OK) == 0);
75     dir.cleanup();
76     dir.cleanup();
77     ATF_REQUIRE(::access("root", X_OK) == -1);
78 }
79 
80 
81 ATF_TEST_CASE_WITHOUT_HEAD(auto_directory__mkdtemp);
ATF_TEST_CASE_BODY(auto_directory__mkdtemp)82 ATF_TEST_CASE_BODY(auto_directory__mkdtemp)
83 {
84     utils::setenv("TMPDIR", (fs::current_path() / "tmp").str());
85     fs::mkdir(fs::path("tmp"), 0755);
86 
87     const std::string path_template("test.XXXXXX");
88     {
89         fs::auto_directory auto_directory = fs::auto_directory::mkdtemp(
90             path_template);
91         ATF_REQUIRE(::access((fs::path("tmp") / path_template).c_str(),
92                              X_OK) == -1);
93         ATF_REQUIRE(::rmdir("tmp") == -1);
94 
95         ATF_REQUIRE(::access(auto_directory.directory().c_str(), X_OK) == 0);
96     }
97     ATF_REQUIRE(::rmdir("tmp") != -1);
98 }
99 
100 
101 ATF_TEST_CASE_WITHOUT_HEAD(auto_file__automatic);
ATF_TEST_CASE_BODY(auto_file__automatic)102 ATF_TEST_CASE_BODY(auto_file__automatic)
103 {
104     const fs::path file("foo");
105     atf::utils::create_file(file.str(), "");
106     {
107         fs::auto_file auto_file(file);
108         ATF_REQUIRE_EQ(file, auto_file.file());
109 
110         ATF_REQUIRE(::access(file.c_str(), R_OK) == 0);
111 
112         {
113             fs::auto_file auto_file_copy(auto_file);
114         }
115         // Should still exist after a copy is destructed.
116         ATF_REQUIRE(::access(file.c_str(), R_OK) == 0);
117     }
118     ATF_REQUIRE(::access(file.c_str(), R_OK) == -1);
119 }
120 
121 
122 ATF_TEST_CASE_WITHOUT_HEAD(auto_file__explicit);
ATF_TEST_CASE_BODY(auto_file__explicit)123 ATF_TEST_CASE_BODY(auto_file__explicit)
124 {
125     const fs::path file("bar");
126     atf::utils::create_file(file.str(), "");
127 
128     fs::auto_file auto_file(file);
129     ATF_REQUIRE_EQ(file, auto_file.file());
130 
131     ATF_REQUIRE(::access(file.c_str(), R_OK) == 0);
132     auto_file.remove();
133     auto_file.remove();
134     ATF_REQUIRE(::access(file.c_str(), R_OK) == -1);
135 }
136 
137 
138 ATF_TEST_CASE_WITHOUT_HEAD(auto_file__mkstemp);
ATF_TEST_CASE_BODY(auto_file__mkstemp)139 ATF_TEST_CASE_BODY(auto_file__mkstemp)
140 {
141     utils::setenv("TMPDIR", (fs::current_path() / "tmp").str());
142     fs::mkdir(fs::path("tmp"), 0755);
143 
144     const std::string path_template("test.XXXXXX");
145     {
146         fs::auto_file auto_file = fs::auto_file::mkstemp(path_template);
147         ATF_REQUIRE(::access((fs::path("tmp") / path_template).c_str(),
148                              X_OK) == -1);
149         ATF_REQUIRE(::rmdir("tmp") == -1);
150 
151         ATF_REQUIRE(::access(auto_file.file().c_str(), R_OK) == 0);
152     }
153     ATF_REQUIRE(::rmdir("tmp") != -1);
154 }
155 
156 
ATF_INIT_TEST_CASES(tcs)157 ATF_INIT_TEST_CASES(tcs)
158 {
159     ATF_ADD_TEST_CASE(tcs, auto_directory__automatic);
160     ATF_ADD_TEST_CASE(tcs, auto_directory__explicit);
161     ATF_ADD_TEST_CASE(tcs, auto_directory__mkdtemp);
162 
163     ATF_ADD_TEST_CASE(tcs, auto_file__automatic);
164     ATF_ADD_TEST_CASE(tcs, auto_file__explicit);
165     ATF_ADD_TEST_CASE(tcs, auto_file__mkstemp);
166 }
167