source: trunk/test/fileutil_test.cc @ 1619

Last change on this file since 1619 was 1487, checked in by Jari Häkkinen, 13 years ago

Addresses #436. GPL license copy reference should also be updated.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.0 KB
Line 
1// $Id: fileutil_test.cc 1487 2008-09-10 08:41:36Z jari $
2
3/*
4  Copyright (C) 2006 Jari Häkkinen
5  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2008 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 <fstream>
30#include <iostream>
31#include <string>
32
33using namespace theplu::yat;
34void test_large_file_support(test::Suite&);
35
36int main(int argc,char* argv[])
37{ 
38  test::Suite suite(argc, argv);
39  suite.err() << "testing FileUtil ... " << std::endl;
40
41  // checking permissions for current directory
42  try {
43    suite.err() << "FileUtil Test 1" << std::endl;
44    bool testval=true;
45    utility::FileUtil file(".");
46    if (testval=file.exists())
47      suite.err() << "\tfile exists: " << file.path() << std::endl;
48    suite.add(testval);
49    if (!(testval=file.permissions("rwx")))
50      suite.err() << "\trwx permissions on " << file.path() << std::endl;
51    suite.add(!testval);
52    if (!(testval=file.permissions("rx")))
53      suite.err() << "\trx permissions on " << file.path() << std::endl;
54    suite.add(!testval);
55    if (!(testval=file.permissions("d")))
56      suite.err() << "\td permissions on " << file.path() << std::endl;
57    suite.add(!testval);
58  }
59  catch (utility::IO_error e) {
60    suite.add(false);
61    suite.err() << e.what() << std::endl;
62  }
63
64  // checking permissions on non-existent file with ./ in path
65  try {
66    suite.err() << "FileUtil Test 2" << std::endl;
67    bool testval=true;
68    // filename below should be unique
69    utility::FileUtil file("./fileio_test.sdf34DSF");
70    suite.err() << "\tpath: " << std::endl;
71    if (!(testval=file.exists()))
72      suite.err() << "\tfile does not exist: " << file.path() << std::endl;
73    suite.add(!testval);
74    if (!(testval=file.permissions("r")))
75      suite.err() << "\tr permission on " << file.path() << std::endl;
76    suite.add(!testval);
77    if (!(testval=file.permissions("rwx")))
78      suite.err() << "\trwx permissions on " << file.path() << std::endl;
79    suite.add(!testval);
80    if (!(testval=file.permissions("w")))
81      suite.err() << "\tw permission on " << file.path() << std::endl;
82    suite.add(!testval); // 'w' on non-existent file ok if directory writeable
83    if (!(testval=file.permissions("d")))
84      suite.err() << "\td permission on " << file.path() << std::endl;
85    suite.add(!testval); 
86  }
87  catch (utility::IO_error e) {
88    suite.add(false);
89    suite.err() << e.what() << std::endl;
90  }
91
92  // checking permissions on non-existent file without ./ in path
93  try {
94    suite.err() << "FileUtil Test 3" << std::endl;
95    bool testval=true;
96    // filename below should be unique
97    utility::FileUtil file("fileio_test.sdf34DSF");
98    if (!(testval=file.exists()))
99      suite.err() << "\tfile does not exist: " << file.path() << std::endl;
100    suite.add(!testval);
101    if (!(testval=file.permissions("r")))
102      suite.err() << "\tr permission on " << file.path() << std::endl;
103    suite.add(!testval);
104    if (!(testval=file.permissions("rwx")))
105      suite.err() << "\trwx permissions on " << file.path() << std::endl;
106    suite.add(!testval);
107    if (!(testval=file.permissions("w")))
108      suite.err() << "\tw permission on " << file.path() << std::endl;
109    suite.add(!testval); // 'w' on non-existent file ok if directory writeable
110    if (!(testval=file.permissions("d")))
111      suite.err() << "\td permission on " << file.path() << std::endl;
112    suite.add(!testval); 
113  }
114  catch (utility::IO_error e) {
115    suite.add(false);
116    suite.err() << e.what() << std::endl;
117  }
118
119  // checking permissions on non-existent file with non-existent
120  // directories in path
121  try {
122    suite.err() << "FileUtil Test 4" << std::endl;
123    bool testval=true;
124     // intermediate dir must be unique!
125    utility::FileUtil file("./fileio_test.sdf34DSF/file");
126    if (!(testval=file.exists()))
127      suite.err() << "\tfile does not exist: " << file.path() << std::endl;
128    suite.add(!testval);
129    if ((testval=file.permissions("r")))
130      suite.err() << "\tr permission failed on " << file.path() << std::endl;
131    suite.add(testval);
132    if (testval=file.permissions("rwx"))
133      suite.err() << "\trwx permissions failed on " << file.path() << std::endl;
134    suite.add(testval);
135    if (testval=file.permissions("w"))
136      suite.err() << "\tw permission failed on " << file.path() << std::endl;
137    suite.add(testval);
138    if (testval=file.permissions("d"))
139      suite.err() << "\td permission failed on " << file.path() << std::endl;
140    suite.add(testval); 
141  }
142  catch (utility::IO_error e) {
143    suite.add(false);
144    suite.err() << e.what() << std::endl;
145  }
146
147  // checking permissions on existent file after non-existence file
148  // has been checked
149  try {
150    suite.err() << "FileUtil Test 5" << std::endl;
151    bool testval=true;
152     // intermediate dir must be unique!
153    utility::FileUtil file("./fileio_test.sdf34DSF/file");
154    if (!(testval=file.exists()))
155      suite.err() << "\tfile does not exist: " << file.path() << std::endl;
156    suite.add(!testval);
157    utility::FileUtil file2(test::filename("fileutil_test.cc").c_str());
158    if (!(testval=!file2.exists()))
159      suite.err() << "\tfile does exist: " << file2.path() << std::endl;
160    suite.add(!testval);
161  }
162  catch (utility::IO_error e) {
163    suite.add(false);
164    suite.err() << e.what() << std::endl;
165  }
166
167  try {
168    suite.err() << "FileUtil Test 6" << std::endl;
169    utility::FileUtil file("fileutil_test.cc");
170    file.permissions("rxwa"); // should throw
171    suite.add(false); 
172  }
173  catch (std::invalid_argument& e) {
174    suite.err() << "Expected exception thrown with what: " << e.what() 
175                << std::endl;
176  }
177
178  return suite.return_value();
179}
180
Note: See TracBrowser for help on using the repository browser.