712 |
21 Dec 06 |
jari |
// $Id$ |
712 |
21 Dec 06 |
jari |
2 |
|
712 |
21 Dec 06 |
jari |
3 |
/* |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2006 Jari Häkkinen |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2007 Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2008 Jari Häkkinen, Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2010, 2012, 2020 Peter Johansson |
712 |
21 Dec 06 |
jari |
8 |
|
1437 |
25 Aug 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
712 |
21 Dec 06 |
jari |
10 |
|
712 |
21 Dec 06 |
jari |
The yat library is free software; you can redistribute it and/or |
712 |
21 Dec 06 |
jari |
modify it under the terms of the GNU General Public License as |
1486 |
09 Sep 08 |
jari |
published by the Free Software Foundation; either version 3 of the |
712 |
21 Dec 06 |
jari |
License, or (at your option) any later version. |
712 |
21 Dec 06 |
jari |
15 |
|
712 |
21 Dec 06 |
jari |
The yat library is distributed in the hope that it will be useful, |
712 |
21 Dec 06 |
jari |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
712 |
21 Dec 06 |
jari |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
712 |
21 Dec 06 |
jari |
General Public License for more details. |
712 |
21 Dec 06 |
jari |
20 |
|
712 |
21 Dec 06 |
jari |
You should have received a copy of the GNU General Public License |
1487 |
10 Sep 08 |
jari |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
712 |
21 Dec 06 |
jari |
23 |
*/ |
712 |
21 Dec 06 |
jari |
24 |
|
2881 |
18 Nov 12 |
peter |
25 |
#include <config.h> |
2881 |
18 Nov 12 |
peter |
26 |
|
1248 |
19 Mar 08 |
peter |
27 |
#include "Suite.h" |
1248 |
19 Mar 08 |
peter |
28 |
|
712 |
21 Dec 06 |
jari |
29 |
#include "yat/utility/FileUtil.h" |
712 |
21 Dec 06 |
jari |
30 |
#include "yat/utility/Exception.h" |
712 |
21 Dec 06 |
jari |
31 |
|
2209 |
05 Mar 10 |
peter |
32 |
#include <cerrno> |
712 |
21 Dec 06 |
jari |
33 |
#include <fstream> |
712 |
21 Dec 06 |
jari |
34 |
#include <iostream> |
712 |
21 Dec 06 |
jari |
35 |
#include <string> |
712 |
21 Dec 06 |
jari |
36 |
|
1456 |
30 Aug 08 |
peter |
37 |
using namespace theplu::yat; |
1456 |
30 Aug 08 |
peter |
38 |
void test_large_file_support(test::Suite&); |
1456 |
30 Aug 08 |
peter |
39 |
|
1248 |
19 Mar 08 |
peter |
40 |
int main(int argc,char* argv[]) |
4200 |
19 Aug 22 |
peter |
41 |
{ |
1248 |
19 Mar 08 |
peter |
42 |
test::Suite suite(argc, argv); |
1248 |
19 Mar 08 |
peter |
43 |
suite.err() << "testing FileUtil ... " << std::endl; |
712 |
21 Dec 06 |
jari |
44 |
|
1633 |
19 Nov 08 |
peter |
// setting errno just to check that we (on success) do not rely on |
1633 |
19 Nov 08 |
peter |
// the value of errno |
1633 |
19 Nov 08 |
peter |
47 |
errno = 1; |
1633 |
19 Nov 08 |
peter |
48 |
|
712 |
21 Dec 06 |
jari |
// checking permissions for current directory |
712 |
21 Dec 06 |
jari |
50 |
try { |
1248 |
19 Mar 08 |
peter |
51 |
suite.err() << "FileUtil Test 1" << std::endl; |
712 |
21 Dec 06 |
jari |
52 |
bool testval=true; |
712 |
21 Dec 06 |
jari |
53 |
utility::FileUtil file("."); |
2055 |
08 Sep 09 |
peter |
54 |
if ((testval=file.exists())) |
1248 |
19 Mar 08 |
peter |
55 |
suite.err() << "\tfile exists: " << file.path() << std::endl; |
1248 |
19 Mar 08 |
peter |
56 |
suite.add(testval); |
712 |
21 Dec 06 |
jari |
57 |
if (!(testval=file.permissions("rwx"))) |
1248 |
19 Mar 08 |
peter |
58 |
suite.err() << "\trwx permissions on " << file.path() << std::endl; |
1248 |
19 Mar 08 |
peter |
59 |
suite.add(!testval); |
712 |
21 Dec 06 |
jari |
60 |
if (!(testval=file.permissions("rx"))) |
1248 |
19 Mar 08 |
peter |
61 |
suite.err() << "\trx permissions on " << file.path() << std::endl; |
1248 |
19 Mar 08 |
peter |
62 |
suite.add(!testval); |
1207 |
05 Mar 08 |
peter |
63 |
if (!(testval=file.permissions("d"))) |
1248 |
19 Mar 08 |
peter |
64 |
suite.err() << "\td permissions on " << file.path() << std::endl; |
1248 |
19 Mar 08 |
peter |
65 |
suite.add(!testval); |
712 |
21 Dec 06 |
jari |
66 |
} |
3936 |
13 Jul 20 |
peter |
67 |
catch (utility::IO_error& e) { |
1248 |
19 Mar 08 |
peter |
68 |
suite.add(false); |
1248 |
19 Mar 08 |
peter |
69 |
suite.err() << e.what() << std::endl; |
712 |
21 Dec 06 |
jari |
70 |
} |
712 |
21 Dec 06 |
jari |
71 |
|
712 |
21 Dec 06 |
jari |
// checking permissions on non-existent file with ./ in path |
712 |
21 Dec 06 |
jari |
73 |
try { |
1248 |
19 Mar 08 |
peter |
74 |
suite.err() << "FileUtil Test 2" << std::endl; |
712 |
21 Dec 06 |
jari |
75 |
bool testval=true; |
712 |
21 Dec 06 |
jari |
// filename below should be unique |
712 |
21 Dec 06 |
jari |
77 |
utility::FileUtil file("./fileio_test.sdf34DSF"); |
1248 |
19 Mar 08 |
peter |
78 |
suite.err() << "\tpath: " << std::endl; |
712 |
21 Dec 06 |
jari |
79 |
if (!(testval=file.exists())) |
1248 |
19 Mar 08 |
peter |
80 |
suite.err() << "\tfile does not exist: " << file.path() << std::endl; |
1248 |
19 Mar 08 |
peter |
81 |
suite.add(!testval); |
712 |
21 Dec 06 |
jari |
82 |
if (!(testval=file.permissions("r"))) |
1248 |
19 Mar 08 |
peter |
83 |
suite.err() << "\tr permission on " << file.path() << std::endl; |
1248 |
19 Mar 08 |
peter |
84 |
suite.add(!testval); |
712 |
21 Dec 06 |
jari |
85 |
if (!(testval=file.permissions("rwx"))) |
1248 |
19 Mar 08 |
peter |
86 |
suite.err() << "\trwx permissions on " << file.path() << std::endl; |
1248 |
19 Mar 08 |
peter |
87 |
suite.add(!testval); |
712 |
21 Dec 06 |
jari |
88 |
if (!(testval=file.permissions("w"))) |
1248 |
19 Mar 08 |
peter |
89 |
suite.err() << "\tw permission on " << file.path() << std::endl; |
1248 |
19 Mar 08 |
peter |
90 |
suite.add(!testval); // 'w' on non-existent file ok if directory writeable |
1207 |
05 Mar 08 |
peter |
91 |
if (!(testval=file.permissions("d"))) |
1248 |
19 Mar 08 |
peter |
92 |
suite.err() << "\td permission on " << file.path() << std::endl; |
4200 |
19 Aug 22 |
peter |
93 |
suite.add(!testval); |
712 |
21 Dec 06 |
jari |
94 |
} |
3936 |
13 Jul 20 |
peter |
95 |
catch (utility::IO_error& e) { |
1248 |
19 Mar 08 |
peter |
96 |
suite.add(false); |
1248 |
19 Mar 08 |
peter |
97 |
suite.err() << e.what() << std::endl; |
712 |
21 Dec 06 |
jari |
98 |
} |
712 |
21 Dec 06 |
jari |
99 |
|
712 |
21 Dec 06 |
jari |
// checking permissions on non-existent file without ./ in path |
712 |
21 Dec 06 |
jari |
101 |
try { |
1248 |
19 Mar 08 |
peter |
102 |
suite.err() << "FileUtil Test 3" << std::endl; |
712 |
21 Dec 06 |
jari |
103 |
bool testval=true; |
712 |
21 Dec 06 |
jari |
// filename below should be unique |
712 |
21 Dec 06 |
jari |
105 |
utility::FileUtil file("fileio_test.sdf34DSF"); |
712 |
21 Dec 06 |
jari |
106 |
if (!(testval=file.exists())) |
1248 |
19 Mar 08 |
peter |
107 |
suite.err() << "\tfile does not exist: " << file.path() << std::endl; |
1248 |
19 Mar 08 |
peter |
108 |
suite.add(!testval); |
712 |
21 Dec 06 |
jari |
109 |
if (!(testval=file.permissions("r"))) |
1248 |
19 Mar 08 |
peter |
110 |
suite.err() << "\tr permission on " << file.path() << std::endl; |
1248 |
19 Mar 08 |
peter |
111 |
suite.add(!testval); |
712 |
21 Dec 06 |
jari |
112 |
if (!(testval=file.permissions("rwx"))) |
1248 |
19 Mar 08 |
peter |
113 |
suite.err() << "\trwx permissions on " << file.path() << std::endl; |
1248 |
19 Mar 08 |
peter |
114 |
suite.add(!testval); |
712 |
21 Dec 06 |
jari |
115 |
if (!(testval=file.permissions("w"))) |
1248 |
19 Mar 08 |
peter |
116 |
suite.err() << "\tw permission on " << file.path() << std::endl; |
1248 |
19 Mar 08 |
peter |
117 |
suite.add(!testval); // 'w' on non-existent file ok if directory writeable |
1207 |
05 Mar 08 |
peter |
118 |
if (!(testval=file.permissions("d"))) |
1248 |
19 Mar 08 |
peter |
119 |
suite.err() << "\td permission on " << file.path() << std::endl; |
4200 |
19 Aug 22 |
peter |
120 |
suite.add(!testval); |
712 |
21 Dec 06 |
jari |
121 |
} |
3936 |
13 Jul 20 |
peter |
122 |
catch (utility::IO_error& e) { |
1248 |
19 Mar 08 |
peter |
123 |
suite.add(false); |
1248 |
19 Mar 08 |
peter |
124 |
suite.err() << e.what() << std::endl; |
712 |
21 Dec 06 |
jari |
125 |
} |
712 |
21 Dec 06 |
jari |
126 |
|
712 |
21 Dec 06 |
jari |
// checking permissions on non-existent file with non-existent |
712 |
21 Dec 06 |
jari |
// directories in path |
712 |
21 Dec 06 |
jari |
129 |
try { |
1248 |
19 Mar 08 |
peter |
130 |
suite.err() << "FileUtil Test 4" << std::endl; |
712 |
21 Dec 06 |
jari |
131 |
bool testval=true; |
712 |
21 Dec 06 |
jari |
// intermediate dir must be unique! |
712 |
21 Dec 06 |
jari |
133 |
utility::FileUtil file("./fileio_test.sdf34DSF/file"); |
712 |
21 Dec 06 |
jari |
134 |
if (!(testval=file.exists())) |
1248 |
19 Mar 08 |
peter |
135 |
suite.err() << "\tfile does not exist: " << file.path() << std::endl; |
1248 |
19 Mar 08 |
peter |
136 |
suite.add(!testval); |
712 |
21 Dec 06 |
jari |
137 |
if ((testval=file.permissions("r"))) |
1248 |
19 Mar 08 |
peter |
138 |
suite.err() << "\tr permission failed on " << file.path() << std::endl; |
1248 |
19 Mar 08 |
peter |
139 |
suite.add(testval); |
2055 |
08 Sep 09 |
peter |
140 |
if ((testval=file.permissions("rwx"))) |
1248 |
19 Mar 08 |
peter |
141 |
suite.err() << "\trwx permissions failed on " << file.path() << std::endl; |
1248 |
19 Mar 08 |
peter |
142 |
suite.add(testval); |
2055 |
08 Sep 09 |
peter |
143 |
if ((testval=file.permissions("w"))) |
1248 |
19 Mar 08 |
peter |
144 |
suite.err() << "\tw permission failed on " << file.path() << std::endl; |
1248 |
19 Mar 08 |
peter |
145 |
suite.add(testval); |
2055 |
08 Sep 09 |
peter |
146 |
if ((testval=file.permissions("d"))) |
1248 |
19 Mar 08 |
peter |
147 |
suite.err() << "\td permission failed on " << file.path() << std::endl; |
4200 |
19 Aug 22 |
peter |
148 |
suite.add(testval); |
712 |
21 Dec 06 |
jari |
149 |
} |
3936 |
13 Jul 20 |
peter |
150 |
catch (utility::IO_error& e) { |
1248 |
19 Mar 08 |
peter |
151 |
suite.add(false); |
1248 |
19 Mar 08 |
peter |
152 |
suite.err() << e.what() << std::endl; |
712 |
21 Dec 06 |
jari |
153 |
} |
712 |
21 Dec 06 |
jari |
154 |
|
1001 |
01 Jan 08 |
peter |
// checking permissions on existent file after non-existence file |
1001 |
01 Jan 08 |
peter |
// has been checked |
1001 |
01 Jan 08 |
peter |
157 |
try { |
1248 |
19 Mar 08 |
peter |
158 |
suite.err() << "FileUtil Test 5" << std::endl; |
1001 |
01 Jan 08 |
peter |
159 |
bool testval=true; |
1001 |
01 Jan 08 |
peter |
// intermediate dir must be unique! |
1001 |
01 Jan 08 |
peter |
161 |
utility::FileUtil file("./fileio_test.sdf34DSF/file"); |
1001 |
01 Jan 08 |
peter |
162 |
if (!(testval=file.exists())) |
1248 |
19 Mar 08 |
peter |
163 |
suite.err() << "\tfile does not exist: " << file.path() << std::endl; |
1248 |
19 Mar 08 |
peter |
164 |
suite.add(!testval); |
2370 |
11 Dec 10 |
peter |
165 |
utility::FileUtil file2(test::filename("fileutil.cc").c_str()); |
1001 |
01 Jan 08 |
peter |
166 |
if (!(testval=!file2.exists())) |
1248 |
19 Mar 08 |
peter |
167 |
suite.err() << "\tfile does exist: " << file2.path() << std::endl; |
1248 |
19 Mar 08 |
peter |
168 |
suite.add(!testval); |
1001 |
01 Jan 08 |
peter |
169 |
} |
3936 |
13 Jul 20 |
peter |
170 |
catch (utility::IO_error& e) { |
1248 |
19 Mar 08 |
peter |
171 |
suite.add(false); |
1248 |
19 Mar 08 |
peter |
172 |
suite.err() << e.what() << std::endl; |
1001 |
01 Jan 08 |
peter |
173 |
} |
1001 |
01 Jan 08 |
peter |
174 |
|
1207 |
05 Mar 08 |
peter |
175 |
try { |
1248 |
19 Mar 08 |
peter |
176 |
suite.err() << "FileUtil Test 6" << std::endl; |
1207 |
05 Mar 08 |
peter |
177 |
utility::FileUtil file("fileutil_test.cc"); |
1207 |
05 Mar 08 |
peter |
178 |
file.permissions("rxwa"); // should throw |
4200 |
19 Aug 22 |
peter |
179 |
suite.add(false); |
1207 |
05 Mar 08 |
peter |
180 |
} |
1207 |
05 Mar 08 |
peter |
181 |
catch (std::invalid_argument& e) { |
4200 |
19 Aug 22 |
peter |
182 |
suite.err() << "Expected exception thrown with what: " << e.what() |
1457 |
30 Aug 08 |
peter |
183 |
<< std::endl; |
1207 |
05 Mar 08 |
peter |
184 |
} |
1207 |
05 Mar 08 |
peter |
185 |
|
2217 |
14 Mar 10 |
peter |
// checking permissions on non-existence file in `/' |
2217 |
14 Mar 10 |
peter |
187 |
try { |
2217 |
14 Mar 10 |
peter |
188 |
suite.err() << "FileUtil Test 7" << std::endl; |
2217 |
14 Mar 10 |
peter |
// intermediate dir must be unique! |
2217 |
14 Mar 10 |
peter |
190 |
utility::FileUtil file("/theplu_yat_fileutil_test.sdf34DSF"); |
2217 |
14 Mar 10 |
peter |
191 |
if(!suite.add(file.permissions("r") == 0)) { |
2217 |
14 Mar 10 |
peter |
192 |
suite.err() << " r permission failed on " << file.path() << std::endl; |
2217 |
14 Mar 10 |
peter |
193 |
} |
2217 |
14 Mar 10 |
peter |
194 |
} |
3936 |
13 Jul 20 |
peter |
195 |
catch (utility::IO_error& e) { |
2217 |
14 Mar 10 |
peter |
196 |
suite.add(false); |
2217 |
14 Mar 10 |
peter |
197 |
suite.err() << e.what() << std::endl; |
2217 |
14 Mar 10 |
peter |
198 |
} |
2217 |
14 Mar 10 |
peter |
199 |
|
1248 |
19 Mar 08 |
peter |
200 |
return suite.return_value(); |
712 |
21 Dec 06 |
jari |
201 |
} |