source: trunk/test/fileutil_test.cc @ 1207

Last change on this file since 1207 was 1207, checked in by Peter, 14 years ago

fixes #282

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