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