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/operations.hpp"
30 
31 extern "C" {
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/wait.h>
35 
36 #include <dirent.h>
37 #include <signal.h>
38 #include <unistd.h>
39 }
40 
41 #include <cerrno>
42 #include <cstdlib>
43 #include <cstring>
44 #include <fstream>
45 #include <iostream>
46 #include <string>
47 #include <vector>
48 
49 #include <atf-c++.hpp>
50 
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"
56 
57 namespace fs = utils::fs;
58 
59 using utils::optional;
60 
61 
62 namespace {
63 
64 
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
lookup(const char * dir,const char * name,const int expected_type)74 lookup(const char* dir, const char* name, const int expected_type)
75 {
76     DIR* dirp = ::opendir(dir);
77     ATF_REQUIRE(dirp != NULL);
78 
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;
85         }
86     }
87     ::closedir(dirp);
88     return found;
89 }
90 
91 
92 }  // anonymous namespace
93 
94 
95 ATF_TEST_CASE_WITHOUT_HEAD(current_path__ok);
ATF_TEST_CASE_BODY(current_path__ok)96 ATF_TEST_CASE_BODY(current_path__ok)
97 {
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);
104 }
105 
106 
107 ATF_TEST_CASE_WITHOUT_HEAD(current_path__enoent);
ATF_TEST_CASE_BODY(current_path__enoent)108 ATF_TEST_CASE_BODY(current_path__enoent)
109 {
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());
119     }
120 }
121 
122 
123 ATF_TEST_CASE_WITHOUT_HEAD(exists);
ATF_TEST_CASE_BODY(exists)124 ATF_TEST_CASE_BODY(exists)
125 {
126     const fs::path dir("dir");
127     ATF_REQUIRE(!fs::exists(dir));
128     fs::mkdir(dir, 0755);
129     ATF_REQUIRE(fs::exists(dir));
130 }
131 
132 
133 ATF_TEST_CASE_WITHOUT_HEAD(find_in_path__no_path);
ATF_TEST_CASE_BODY(find_in_path__no_path)134 ATF_TEST_CASE_BODY(find_in_path__no_path)
135 {
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"));
140 }
141 
142 
143 ATF_TEST_CASE_WITHOUT_HEAD(find_in_path__empty_path);
ATF_TEST_CASE_BODY(find_in_path__empty_path)144 ATF_TEST_CASE_BODY(find_in_path__empty_path)
145 {
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"));
150 }
151 
152 
153 ATF_TEST_CASE_WITHOUT_HEAD(find_in_path__one_component);
ATF_TEST_CASE_BODY(find_in_path__one_component)154 ATF_TEST_CASE_BODY(find_in_path__one_component)
155 {
156     const fs::path dir = fs::current_path() / "bin";
157     fs::mkdir(dir, 0755);
158     utils::setenv("PATH", dir.str());
159 
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());
163 }
164 
165 
166 ATF_TEST_CASE_WITHOUT_HEAD(find_in_path__many_components);
ATF_TEST_CASE_BODY(find_in_path__many_components)167 ATF_TEST_CASE_BODY(find_in_path__many_components)
168 {
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());
174 
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());
180 }
181 
182 
183 ATF_TEST_CASE_WITHOUT_HEAD(find_in_path__current_directory);
ATF_TEST_CASE_BODY(find_in_path__current_directory)184 ATF_TEST_CASE_BODY(find_in_path__current_directory)
185 {
186     utils::setenv("PATH", "bin:");
187 
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());
192 }
193 
194 
195 ATF_TEST_CASE_WITHOUT_HEAD(find_in_path__always_absolute);
ATF_TEST_CASE_BODY(find_in_path__always_absolute)196 ATF_TEST_CASE_BODY(find_in_path__always_absolute)
197 {
198     fs::mkdir(fs::path("my-bin"), 0755);
199     utils::setenv("PATH", "my-bin");
200 
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());
205 }
206 
207 
208 ATF_TEST_CASE_WITHOUT_HEAD(mkdir__ok);
ATF_TEST_CASE_BODY(mkdir__ok)209 ATF_TEST_CASE_BODY(mkdir__ok)
210 {
211     fs::mkdir(fs::path("dir"), 0755);
212     ATF_REQUIRE(lookup(".", "dir", DT_DIR));
213 }
214 
215 
216 ATF_TEST_CASE_WITHOUT_HEAD(mkdir__enoent);
ATF_TEST_CASE_BODY(mkdir__enoent)217 ATF_TEST_CASE_BODY(mkdir__enoent)
218 {
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());
224     }
225     ATF_REQUIRE(!lookup(".", "dir1", DT_DIR));
226     ATF_REQUIRE(!lookup(".", "dir2", DT_DIR));
227 }
228 
229 
230 ATF_TEST_CASE_WITHOUT_HEAD(mkdir_p__one_component);
ATF_TEST_CASE_BODY(mkdir_p__one_component)231 ATF_TEST_CASE_BODY(mkdir_p__one_component)
232 {
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));
236 }
237 
238 
239 ATF_TEST_CASE_WITHOUT_HEAD(mkdir_p__many_components);
ATF_TEST_CASE_BODY(mkdir_p__many_components)240 ATF_TEST_CASE_BODY(mkdir_p__many_components)
241 {
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));
247 }
248 
249 
250 ATF_TEST_CASE_WITHOUT_HEAD(mkdir_p__already_exists);
ATF_TEST_CASE_BODY(mkdir_p__already_exists)251 ATF_TEST_CASE_BODY(mkdir_p__already_exists)
252 {
253     fs::mkdir(fs::path("a"), 0755);
254     fs::mkdir(fs::path("a/b"), 0755);
255     fs::mkdir_p(fs::path("a/b"), 0755);
256 }
257 
258 
259 ATF_TEST_CASE(mkdir_p__eacces)
ATF_TEST_CASE_HEAD(mkdir_p__eacces)260 ATF_TEST_CASE_HEAD(mkdir_p__eacces)
261 {
262     set_md_var("require.user", "unprivileged");
263 }
ATF_TEST_CASE_BODY(mkdir_p__eacces)264 ATF_TEST_CASE_BODY(mkdir_p__eacces)
265 {
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());
274     }
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));
280 }
281 
282 
283 ATF_TEST_CASE_WITHOUT_HEAD(mkdtemp)
ATF_TEST_CASE_BODY(mkdtemp)284 ATF_TEST_CASE_BODY(mkdtemp)
285 {
286     const fs::path tmpdir = fs::current_path() / "tmp";
287     utils::setenv("TMPDIR", tmpdir.str());
288     fs::mkdir(tmpdir, 0755);
289 
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));
294 }
295 
296 
297 ATF_TEST_CASE_WITHOUT_HEAD(mkstemp)
ATF_TEST_CASE_BODY(mkstemp)298 ATF_TEST_CASE_BODY(mkstemp)
299 {
300     const fs::path tmpdir = fs::current_path() / "tmp";
301     utils::setenv("TMPDIR", tmpdir.str());
302     fs::mkdir(tmpdir, 0755);
303 
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));
308 }
309 
310 
311 ATF_TEST_CASE_WITHOUT_HEAD(rm_r__empty);
ATF_TEST_CASE_BODY(rm_r__empty)312 ATF_TEST_CASE_BODY(rm_r__empty)
313 {
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));
318 }
319 
320 
321 ATF_TEST_CASE_WITHOUT_HEAD(rm_r__files_and_directories);
ATF_TEST_CASE_BODY(rm_r__files_and_directories)322 ATF_TEST_CASE_BODY(rm_r__files_and_directories)
323 {
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));
337 }
338 
339 
340 ATF_TEST_CASE_WITHOUT_HEAD(rmdir__ok)
ATF_TEST_CASE_BODY(rmdir__ok)341 ATF_TEST_CASE_BODY(rmdir__ok)
342 {
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);
347 }
348 
349 
350 ATF_TEST_CASE_WITHOUT_HEAD(rmdir__fail)
ATF_TEST_CASE_BODY(rmdir__fail)351 ATF_TEST_CASE_BODY(rmdir__fail)
352 {
353     ATF_REQUIRE_THROW_RE(fs::system_error, "Removal of foo failed",
354                          fs::rmdir(fs::path("foo")));
355 }
356 
357 
358 ATF_TEST_CASE_WITHOUT_HEAD(unlink__ok)
ATF_TEST_CASE_BODY(unlink__ok)359 ATF_TEST_CASE_BODY(unlink__ok)
360 {
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);
365 }
366 
367 
368 ATF_TEST_CASE_WITHOUT_HEAD(unlink__fail)
ATF_TEST_CASE_BODY(unlink__fail)369 ATF_TEST_CASE_BODY(unlink__fail)
370 {
371     ATF_REQUIRE_THROW_RE(fs::system_error, "Removal of foo failed",
372                          fs::unlink(fs::path("foo")));
373 }
374 
375 
ATF_INIT_TEST_CASES(tcs)376 ATF_INIT_TEST_CASES(tcs)
377 {
378     ATF_ADD_TEST_CASE(tcs, current_path__ok);
379     ATF_ADD_TEST_CASE(tcs, current_path__enoent);
380 
381     ATF_ADD_TEST_CASE(tcs, exists);
382 
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);
389 
390     ATF_ADD_TEST_CASE(tcs, mkdir__ok);
391     ATF_ADD_TEST_CASE(tcs, mkdir__enoent);
392 
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);
397 
398     ATF_ADD_TEST_CASE(tcs, mkdtemp);
399 
400     ATF_ADD_TEST_CASE(tcs, mkstemp);
401 
402     ATF_ADD_TEST_CASE(tcs, rm_r__empty);
403     ATF_ADD_TEST_CASE(tcs, rm_r__files_and_directories);
404 
405     ATF_ADD_TEST_CASE(tcs, rmdir__ok);
406     ATF_ADD_TEST_CASE(tcs, rmdir__fail);
407 
408     ATF_ADD_TEST_CASE(tcs, unlink__ok);
409     ATF_ADD_TEST_CASE(tcs, unlink__fail);
410 }
411