source: trunk/test/fileutil_test.cc @ 1001

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

reset errno to zero when it is an expected error

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.1 KB
Line 
1// $Id: fileutil_test.cc 1001 2008-01-01 22:19:57Z 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  }
63  catch (utility::IO_error e) {
64    ok=false;
65    *error << e.what() << std::endl;
66  }
67
68  // checking permissions on non-existent file with ./ in path
69  try {
70    *error << "FileUtil Test 2" << std::endl;
71    bool testval=true;
72    // filename below should be unique
73    utility::FileUtil file("./fileio_test.sdf34DSF");
74    *error << "\tpath: " << std::endl;
75    if (!(testval=file.exists()))
76      *error << "\tfile does not exist: " << file.path() << std::endl;
77    ok&=!testval;
78    if (!(testval=file.permissions("r")))
79      *error << "\tr permission on " << file.path() << std::endl;
80    ok&=!testval;
81    if (!(testval=file.permissions("rwx")))
82      *error << "\trwx permissions on " << file.path() << std::endl;
83    ok&=!testval;
84    if (!(testval=file.permissions("w")))
85      *error << "\tw permission on " << file.path() << std::endl;
86    ok&=!testval; // 'w' on non-existent file ok if directory writeable
87  }
88  catch (utility::IO_error e) {
89    ok=false;
90    *error << e.what() << std::endl;
91  }
92
93  // checking permissions on non-existent file without ./ in path
94  try {
95    *error << "FileUtil Test 3" << std::endl;
96    bool testval=true;
97    // filename below should be unique
98    utility::FileUtil file("fileio_test.sdf34DSF");
99    if (!(testval=file.exists()))
100      *error << "\tfile does not exist: " << file.path() << std::endl;
101    ok&=!testval;
102    if (!(testval=file.permissions("r")))
103      *error << "\tr permission on " << file.path() << std::endl;
104    ok&=!testval;
105    if (!(testval=file.permissions("rwx")))
106      *error << "\trwx permissions on " << file.path() << std::endl;
107    ok&=!testval;
108    if (!(testval=file.permissions("w")))
109      *error << "\tw permission on " << file.path() << std::endl;
110    ok&=!testval; // 'w' on non-existent file ok if directory writeable
111  }
112  catch (utility::IO_error e) {
113    ok=false;
114    *error << e.what() << std::endl;
115  }
116
117  // checking permissions on non-existent file with non-existent
118  // directories in path
119  try {
120    *error << "FileUtil Test 4" << std::endl;
121    bool testval=true;
122     // intermediate dir must be unique!
123    utility::FileUtil file("./fileio_test.sdf34DSF/file");
124    if (!(testval=file.exists()))
125      *error << "\tfile does not exist: " << file.path() << std::endl;
126    ok&=!testval;
127    if ((testval=file.permissions("r")))
128      *error << "\tr permission failed on " << file.path() << std::endl;
129    ok&=testval;
130    if (testval=file.permissions("rwx"))
131      *error << "\trwx permissions failed on " << file.path() << std::endl;
132    ok&=testval;
133    if (testval=file.permissions("w"))
134      *error << "\tw permission failed on " << file.path() << std::endl;
135    ok&=testval;
136  }
137  catch (utility::IO_error e) {
138    ok=false;
139    *error << e.what() << std::endl;
140  }
141
142  // checking permissions on existent file after non-existence file
143  // has been checked
144  try {
145    *error << "FileUtil Test 5" << std::endl;
146    bool testval=true;
147     // intermediate dir must be unique!
148    utility::FileUtil file("./fileio_test.sdf34DSF/file");
149    if (!(testval=file.exists()))
150      *error << "\tfile does not exist: " << file.path() << std::endl;
151    ok&=!testval;
152    utility::FileUtil file2("fileutil_test.cc");
153    if (!(testval=!file2.exists()))
154      *error << "\tfile does exist: " << file2.path() << std::endl;
155    ok&=!testval;
156  }
157  catch (utility::IO_error e) {
158    ok=false;
159    *error << e.what() << std::endl;
160  }
161
162 
163
164  *error << (ok ? "OK" : "Failed") << std::endl;
165  if (error!=&std::cerr)
166    delete error;
167
168  return (ok ? 0 : -1);
169}
Note: See TracBrowser for help on using the repository browser.