1 // Copyright 2015 The Kyua Authors.
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/directory.hpp"
30 
31 #include <sstream>
32 
33 #include <atf-c++.hpp>
34 
35 #include "utils/format/containers.ipp"
36 #include "utils/fs/exceptions.hpp"
37 #include "utils/fs/operations.hpp"
38 #include "utils/fs/path.hpp"
39 
40 namespace fs = utils::fs;
41 
42 
43 ATF_TEST_CASE_WITHOUT_HEAD(directory_entry__public_fields);
44 ATF_TEST_CASE_BODY(directory_entry__public_fields)
45 {
46     const fs::directory_entry entry("name");
47     ATF_REQUIRE_EQ("name", entry.name);
48 }
49 
50 
51 ATF_TEST_CASE_WITHOUT_HEAD(directory_entry__equality);
52 ATF_TEST_CASE_BODY(directory_entry__equality)
53 {
54     const fs::directory_entry entry1("name");
55     const fs::directory_entry entry2("other-name");
56 
57     ATF_REQUIRE(  entry1 == entry1);
58     ATF_REQUIRE(!(entry1 != entry1));
59 
60     ATF_REQUIRE(!(entry1 == entry2));
61     ATF_REQUIRE(  entry1 != entry2);
62 }
63 
64 
65 ATF_TEST_CASE_WITHOUT_HEAD(directory_entry__sorting);
66 ATF_TEST_CASE_BODY(directory_entry__sorting)
67 {
68     const fs::directory_entry entry1("name");
69     const fs::directory_entry entry2("other-name");
70 
71     ATF_REQUIRE(!(entry1 < entry1));
72     ATF_REQUIRE(!(entry2 < entry2));
73     ATF_REQUIRE(  entry1 < entry2);
74     ATF_REQUIRE(!(entry2 < entry1));
75 }
76 
77 
78 ATF_TEST_CASE_WITHOUT_HEAD(directory_entry__format);
79 ATF_TEST_CASE_BODY(directory_entry__format)
80 {
81     const fs::directory_entry entry("this is the name");
82     std::ostringstream output;
83     output << entry;
84     ATF_REQUIRE_EQ("directory_entry{name='this is the name'}", output.str());
85 }
86 
87 
88 ATF_TEST_CASE_WITHOUT_HEAD(integration__empty);
89 ATF_TEST_CASE_BODY(integration__empty)
90 {
91     fs::mkdir(fs::path("empty"), 0755);
92 
93     std::set< fs::directory_entry > contents;
94     const fs::directory dir(fs::path("empty"));
95     for (fs::directory::const_iterator iter = dir.begin(); iter != dir.end();
96          ++iter) {
97         contents.insert(*iter);
98         // While we are here, make sure both * and -> represent the same.
99         ATF_REQUIRE((*iter).name == iter->name);
100     }
101 
102     std::set< fs::directory_entry > exp_contents;
103     exp_contents.insert(fs::directory_entry("."));
104     exp_contents.insert(fs::directory_entry(".."));
105 
106     ATF_REQUIRE_EQ(exp_contents, contents);
107 }
108 
109 
110 ATF_TEST_CASE_WITHOUT_HEAD(integration__some_contents);
111 ATF_TEST_CASE_BODY(integration__some_contents)
112 {
113     fs::mkdir(fs::path("full"), 0755);
114     atf::utils::create_file("full/a file", "");
115     atf::utils::create_file("full/something-else", "");
116     atf::utils::create_file("full/.hidden", "");
117     fs::mkdir(fs::path("full/subdir"), 0755);
118     atf::utils::create_file("full/subdir/not-listed", "");
119 
120     std::set< fs::directory_entry > contents;
121     const fs::directory dir(fs::path("full"));
122     for (fs::directory::const_iterator iter = dir.begin(); iter != dir.end();
123          ++iter) {
124         contents.insert(*iter);
125         // While we are here, make sure both * and -> represent the same.
126         ATF_REQUIRE((*iter).name == iter->name);
127     }
128 
129     std::set< fs::directory_entry > exp_contents;
130     exp_contents.insert(fs::directory_entry("."));
131     exp_contents.insert(fs::directory_entry(".."));
132     exp_contents.insert(fs::directory_entry(".hidden"));
133     exp_contents.insert(fs::directory_entry("a file"));
134     exp_contents.insert(fs::directory_entry("something-else"));
135     exp_contents.insert(fs::directory_entry("subdir"));
136 
137     ATF_REQUIRE_EQ(exp_contents, contents);
138 }
139 
140 
141 ATF_TEST_CASE_WITHOUT_HEAD(integration__open_failure);
142 ATF_TEST_CASE_BODY(integration__open_failure)
143 {
144     const fs::directory directory(fs::path("non-existent"));
145     ATF_REQUIRE_THROW_RE(fs::system_error, "opendir(.*non-existent.*) failed",
146                          directory.begin());
147 }
148 
149 
150 ATF_TEST_CASE_WITHOUT_HEAD(integration__iterators_equality);
151 ATF_TEST_CASE_BODY(integration__iterators_equality)
152 {
153     const fs::directory directory(fs::path("."));
154 
155     fs::directory::const_iterator iter_ok1 = directory.begin();
156     fs::directory::const_iterator iter_ok2 = directory.begin();
157     fs::directory::const_iterator iter_end = directory.end();
158 
159     ATF_REQUIRE(  iter_ok1 == iter_ok1);
160     ATF_REQUIRE(!(iter_ok1 != iter_ok1));
161 
162     ATF_REQUIRE(  iter_ok2 == iter_ok2);
163     ATF_REQUIRE(!(iter_ok2 != iter_ok2));
164 
165     ATF_REQUIRE(!(iter_ok1 == iter_ok2));
166     ATF_REQUIRE(  iter_ok1 != iter_ok2);
167 
168     ATF_REQUIRE(!(iter_ok1 == iter_end));
169     ATF_REQUIRE(  iter_ok1 != iter_end);
170 
171     ATF_REQUIRE(!(iter_ok2 == iter_end));
172     ATF_REQUIRE(  iter_ok2 != iter_end);
173 
174     ATF_REQUIRE(  iter_end == iter_end);
175     ATF_REQUIRE(!(iter_end != iter_end));
176 }
177 
178 
179 ATF_INIT_TEST_CASES(tcs)
180 {
181     ATF_ADD_TEST_CASE(tcs, directory_entry__public_fields);
182     ATF_ADD_TEST_CASE(tcs, directory_entry__equality);
183     ATF_ADD_TEST_CASE(tcs, directory_entry__sorting);
184     ATF_ADD_TEST_CASE(tcs, directory_entry__format);
185 
186     ATF_ADD_TEST_CASE(tcs, integration__empty);
187     ATF_ADD_TEST_CASE(tcs, integration__some_contents);
188     ATF_ADD_TEST_CASE(tcs, integration__open_failure);
189     ATF_ADD_TEST_CASE(tcs, integration__iterators_equality);
190 }
191