source: trunk/test/fileutil_test.cc @ 1458

Last change on this file since 1458 was 1458, checked in by Peter, 13 years ago

moving LFS test to separate file so skipping the test (when using --disable-largefile) becomes more visible.

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