Remove building with NOCRYPTO option
[minix3.git] / external / bsd / kyua-cli / dist / utils / fs / operations_test.cpp
blob8043e2e8316a118f6ffaef7f4df2928118c8e841
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.
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.
29 #include "utils/fs/operations.hpp"
31 extern "C" {
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/wait.h>
36 #include <dirent.h>
37 #include <signal.h>
38 #include <unistd.h>
41 #include <cerrno>
42 #include <cstdlib>
43 #include <cstring>
44 #include <fstream>
45 #include <iostream>
46 #include <string>
47 #include <vector>
49 #include <atf-c++.hpp>
51 #include "utils/env.hpp"
52 #include "utils/format/macros.hpp"
53 #include "utils/fs/exceptions.hpp"
54 #include "utils/fs/path.hpp"
55 #include "utils/optional.ipp"
57 namespace fs = utils::fs;
59 using utils::optional;
62 namespace {
65 /// Checks if a directory entry exists and matches a specific type.
66 ///
67 /// \param dir The directory in which to look for the entry.
68 /// \param name The name of the entry to look up.
69 /// \param expected_type The expected type of the file as given by dir(5).
70 ///
71 /// \return True if the entry exists and matches the given type; false
72 /// otherwise.
73 static bool
74 lookup(const char* dir, const char* name, const int expected_type)
76 DIR* dirp = ::opendir(dir);
77 ATF_REQUIRE(dirp != NULL);
79 bool found = false;
80 struct dirent* dp;
81 while (!found && (dp = readdir(dirp)) != NULL) {
82 if (std::strcmp(dp->d_name, name) == 0 &&
83 dp->d_type == expected_type) {
84 found = true;
87 ::closedir(dirp);
88 return found;
92 } // anonymous namespace
95 ATF_TEST_CASE_WITHOUT_HEAD(current_path__ok);
96 ATF_TEST_CASE_BODY(current_path__ok)
98 const fs::path previous = fs::current_path();
99 fs::mkdir(fs::path("root"), 0755);
100 ATF_REQUIRE(::chdir("root") != -1);
101 const fs::path cwd = fs::current_path();
102 ATF_REQUIRE_EQ(cwd.str().length() - 5, cwd.str().find("/root"));
103 ATF_REQUIRE_EQ(previous / "root", cwd);
107 ATF_TEST_CASE_WITHOUT_HEAD(current_path__enoent);
108 ATF_TEST_CASE_BODY(current_path__enoent)
110 const fs::path previous = fs::current_path();
111 fs::mkdir(fs::path("root"), 0755);
112 ATF_REQUIRE(::chdir("root") != -1);
113 ATF_REQUIRE(::rmdir("../root") != -1);
114 try {
115 (void)fs::current_path();
116 fail("system_errpr not raised");
117 } catch (const fs::system_error& e) {
118 ATF_REQUIRE_EQ(ENOENT, e.original_errno());
123 ATF_TEST_CASE_WITHOUT_HEAD(exists);
124 ATF_TEST_CASE_BODY(exists)
126 const fs::path dir("dir");
127 ATF_REQUIRE(!fs::exists(dir));
128 fs::mkdir(dir, 0755);
129 ATF_REQUIRE(fs::exists(dir));
133 ATF_TEST_CASE_WITHOUT_HEAD(find_in_path__no_path);
134 ATF_TEST_CASE_BODY(find_in_path__no_path)
136 utils::unsetenv("PATH");
137 ATF_REQUIRE(!fs::find_in_path("ls"));
138 atf::utils::create_file("ls", "");
139 ATF_REQUIRE(!fs::find_in_path("ls"));
143 ATF_TEST_CASE_WITHOUT_HEAD(find_in_path__empty_path);
144 ATF_TEST_CASE_BODY(find_in_path__empty_path)
146 utils::setenv("PATH", "");
147 ATF_REQUIRE(!fs::find_in_path("ls"));
148 atf::utils::create_file("ls", "");
149 ATF_REQUIRE(!fs::find_in_path("ls"));
153 ATF_TEST_CASE_WITHOUT_HEAD(find_in_path__one_component);
154 ATF_TEST_CASE_BODY(find_in_path__one_component)
156 const fs::path dir = fs::current_path() / "bin";
157 fs::mkdir(dir, 0755);
158 utils::setenv("PATH", dir.str());
160 ATF_REQUIRE(!fs::find_in_path("ls"));
161 atf::utils::create_file((dir / "ls").str(), "");
162 ATF_REQUIRE_EQ(dir / "ls", fs::find_in_path("ls").get());
166 ATF_TEST_CASE_WITHOUT_HEAD(find_in_path__many_components);
167 ATF_TEST_CASE_BODY(find_in_path__many_components)
169 const fs::path dir1 = fs::current_path() / "dir1";
170 const fs::path dir2 = fs::current_path() / "dir2";
171 fs::mkdir(dir1, 0755);
172 fs::mkdir(dir2, 0755);
173 utils::setenv("PATH", dir1.str() + ":" + dir2.str());
175 ATF_REQUIRE(!fs::find_in_path("ls"));
176 atf::utils::create_file((dir2 / "ls").str(), "");
177 ATF_REQUIRE_EQ(dir2 / "ls", fs::find_in_path("ls").get());
178 atf::utils::create_file((dir1 / "ls").str(), "");
179 ATF_REQUIRE_EQ(dir1 / "ls", fs::find_in_path("ls").get());
183 ATF_TEST_CASE_WITHOUT_HEAD(find_in_path__current_directory);
184 ATF_TEST_CASE_BODY(find_in_path__current_directory)
186 utils::setenv("PATH", "bin:");
188 ATF_REQUIRE(!fs::find_in_path("foo-bar"));
189 atf::utils::create_file("foo-bar", "");
190 ATF_REQUIRE_EQ(fs::path("foo-bar").to_absolute(),
191 fs::find_in_path("foo-bar").get());
195 ATF_TEST_CASE_WITHOUT_HEAD(find_in_path__always_absolute);
196 ATF_TEST_CASE_BODY(find_in_path__always_absolute)
198 fs::mkdir(fs::path("my-bin"), 0755);
199 utils::setenv("PATH", "my-bin");
201 ATF_REQUIRE(!fs::find_in_path("abcd"));
202 atf::utils::create_file("my-bin/abcd", "");
203 ATF_REQUIRE_EQ(fs::path("my-bin/abcd").to_absolute(),
204 fs::find_in_path("abcd").get());
208 ATF_TEST_CASE_WITHOUT_HEAD(mkdir__ok);
209 ATF_TEST_CASE_BODY(mkdir__ok)
211 fs::mkdir(fs::path("dir"), 0755);
212 ATF_REQUIRE(lookup(".", "dir", DT_DIR));
216 ATF_TEST_CASE_WITHOUT_HEAD(mkdir__enoent);
217 ATF_TEST_CASE_BODY(mkdir__enoent)
219 try {
220 fs::mkdir(fs::path("dir1/dir2"), 0755);
221 fail("system_error not raised");
222 } catch (const fs::system_error& e) {
223 ATF_REQUIRE_EQ(ENOENT, e.original_errno());
225 ATF_REQUIRE(!lookup(".", "dir1", DT_DIR));
226 ATF_REQUIRE(!lookup(".", "dir2", DT_DIR));
230 ATF_TEST_CASE_WITHOUT_HEAD(mkdir_p__one_component);
231 ATF_TEST_CASE_BODY(mkdir_p__one_component)
233 ATF_REQUIRE(!lookup(".", "new-dir", DT_DIR));
234 fs::mkdir_p(fs::path("new-dir"), 0755);
235 ATF_REQUIRE(lookup(".", "new-dir", DT_DIR));
239 ATF_TEST_CASE_WITHOUT_HEAD(mkdir_p__many_components);
240 ATF_TEST_CASE_BODY(mkdir_p__many_components)
242 ATF_REQUIRE(!lookup(".", "a", DT_DIR));
243 fs::mkdir_p(fs::path("a/b/c"), 0755);
244 ATF_REQUIRE(lookup(".", "a", DT_DIR));
245 ATF_REQUIRE(lookup("a", "b", DT_DIR));
246 ATF_REQUIRE(lookup("a/b", "c", DT_DIR));
250 ATF_TEST_CASE_WITHOUT_HEAD(mkdir_p__already_exists);
251 ATF_TEST_CASE_BODY(mkdir_p__already_exists)
253 fs::mkdir(fs::path("a"), 0755);
254 fs::mkdir(fs::path("a/b"), 0755);
255 fs::mkdir_p(fs::path("a/b"), 0755);
259 ATF_TEST_CASE(mkdir_p__eacces)
260 ATF_TEST_CASE_HEAD(mkdir_p__eacces)
262 set_md_var("require.user", "unprivileged");
264 ATF_TEST_CASE_BODY(mkdir_p__eacces)
266 fs::mkdir(fs::path("a"), 0755);
267 fs::mkdir(fs::path("a/b"), 0755);
268 ATF_REQUIRE(::chmod("a/b", 0555) != -1);
269 try {
270 fs::mkdir_p(fs::path("a/b/c/d"), 0755);
271 fail("system_error not raised");
272 } catch (const fs::system_error& e) {
273 ATF_REQUIRE_EQ(EACCES, e.original_errno());
275 ATF_REQUIRE(lookup(".", "a", DT_DIR));
276 ATF_REQUIRE(lookup("a", "b", DT_DIR));
277 ATF_REQUIRE(!lookup(".", "c", DT_DIR));
278 ATF_REQUIRE(!lookup("a", "c", DT_DIR));
279 ATF_REQUIRE(!lookup("a/b", "c", DT_DIR));
283 ATF_TEST_CASE_WITHOUT_HEAD(mkdtemp)
284 ATF_TEST_CASE_BODY(mkdtemp)
286 const fs::path tmpdir = fs::current_path() / "tmp";
287 utils::setenv("TMPDIR", tmpdir.str());
288 fs::mkdir(tmpdir, 0755);
290 const std::string dir_template("tempdir.XXXXXX");
291 const fs::path tempdir = fs::mkdtemp(dir_template);
292 ATF_REQUIRE(!lookup("tmp", dir_template.c_str(), DT_DIR));
293 ATF_REQUIRE(lookup("tmp", tempdir.leaf_name().c_str(), DT_DIR));
297 ATF_TEST_CASE_WITHOUT_HEAD(mkstemp)
298 ATF_TEST_CASE_BODY(mkstemp)
300 const fs::path tmpdir = fs::current_path() / "tmp";
301 utils::setenv("TMPDIR", tmpdir.str());
302 fs::mkdir(tmpdir, 0755);
304 const std::string file_template("tempfile.XXXXXX");
305 const fs::path tempfile = fs::mkstemp(file_template);
306 ATF_REQUIRE(!lookup("tmp", file_template.c_str(), DT_REG));
307 ATF_REQUIRE(lookup("tmp", tempfile.leaf_name().c_str(), DT_REG));
311 ATF_TEST_CASE_WITHOUT_HEAD(rm_r__empty);
312 ATF_TEST_CASE_BODY(rm_r__empty)
314 fs::mkdir(fs::path("root"), 0755);
315 ATF_REQUIRE(lookup(".", "root", DT_DIR));
316 fs::rm_r(fs::path("root"));
317 ATF_REQUIRE(!lookup(".", "root", DT_DIR));
321 ATF_TEST_CASE_WITHOUT_HEAD(rm_r__files_and_directories);
322 ATF_TEST_CASE_BODY(rm_r__files_and_directories)
324 fs::mkdir(fs::path("root"), 0755);
325 atf::utils::create_file("root/.hidden_file", "");
326 fs::mkdir(fs::path("root/.hidden_dir"), 0755);
327 atf::utils::create_file("root/.hidden_dir/a", "");
328 atf::utils::create_file("root/file", "");
329 atf::utils::create_file("root/with spaces", "");
330 fs::mkdir(fs::path("root/dir1"), 0755);
331 fs::mkdir(fs::path("root/dir1/dir2"), 0755);
332 atf::utils::create_file("root/dir1/dir2/file", "");
333 fs::mkdir(fs::path("root/dir1/dir3"), 0755);
334 ATF_REQUIRE(lookup(".", "root", DT_DIR));
335 fs::rm_r(fs::path("root"));
336 ATF_REQUIRE(!lookup(".", "root", DT_DIR));
340 ATF_TEST_CASE_WITHOUT_HEAD(rmdir__ok)
341 ATF_TEST_CASE_BODY(rmdir__ok)
343 ATF_REQUIRE(::mkdir("foo", 0755) != -1);
344 ATF_REQUIRE(::access("foo", X_OK) == 0);
345 fs::rmdir(fs::path("foo"));
346 ATF_REQUIRE(::access("foo", X_OK) == -1);
350 ATF_TEST_CASE_WITHOUT_HEAD(rmdir__fail)
351 ATF_TEST_CASE_BODY(rmdir__fail)
353 ATF_REQUIRE_THROW_RE(fs::system_error, "Removal of foo failed",
354 fs::rmdir(fs::path("foo")));
358 ATF_TEST_CASE_WITHOUT_HEAD(unlink__ok)
359 ATF_TEST_CASE_BODY(unlink__ok)
361 atf::utils::create_file("foo", "");
362 ATF_REQUIRE(::access("foo", R_OK) == 0);
363 fs::unlink(fs::path("foo"));
364 ATF_REQUIRE(::access("foo", R_OK) == -1);
368 ATF_TEST_CASE_WITHOUT_HEAD(unlink__fail)
369 ATF_TEST_CASE_BODY(unlink__fail)
371 ATF_REQUIRE_THROW_RE(fs::system_error, "Removal of foo failed",
372 fs::unlink(fs::path("foo")));
376 ATF_INIT_TEST_CASES(tcs)
378 ATF_ADD_TEST_CASE(tcs, current_path__ok);
379 ATF_ADD_TEST_CASE(tcs, current_path__enoent);
381 ATF_ADD_TEST_CASE(tcs, exists);
383 ATF_ADD_TEST_CASE(tcs, find_in_path__no_path);
384 ATF_ADD_TEST_CASE(tcs, find_in_path__empty_path);
385 ATF_ADD_TEST_CASE(tcs, find_in_path__one_component);
386 ATF_ADD_TEST_CASE(tcs, find_in_path__many_components);
387 ATF_ADD_TEST_CASE(tcs, find_in_path__current_directory);
388 ATF_ADD_TEST_CASE(tcs, find_in_path__always_absolute);
390 ATF_ADD_TEST_CASE(tcs, mkdir__ok);
391 ATF_ADD_TEST_CASE(tcs, mkdir__enoent);
393 ATF_ADD_TEST_CASE(tcs, mkdir_p__one_component);
394 ATF_ADD_TEST_CASE(tcs, mkdir_p__many_components);
395 ATF_ADD_TEST_CASE(tcs, mkdir_p__already_exists);
396 ATF_ADD_TEST_CASE(tcs, mkdir_p__eacces);
398 ATF_ADD_TEST_CASE(tcs, mkdtemp);
400 ATF_ADD_TEST_CASE(tcs, mkstemp);
402 ATF_ADD_TEST_CASE(tcs, rm_r__empty);
403 ATF_ADD_TEST_CASE(tcs, rm_r__files_and_directories);
405 ATF_ADD_TEST_CASE(tcs, rmdir__ok);
406 ATF_ADD_TEST_CASE(tcs, rmdir__fail);
408 ATF_ADD_TEST_CASE(tcs, unlink__ok);
409 ATF_ADD_TEST_CASE(tcs, unlink__fail);