source: trunk/test/fileutil_test.cc @ 1659

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

do not rely on errno=0 on success

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.1 KB
Line 
1// $Id: fileutil_test.cc 1633 2008-11-19 22:02:10Z 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 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  // setting errno just to check that we (on success) do not rely on
42  // the value of errno
43  errno = 1;
44
45  // checking permissions for current directory
46  try {
47    suite.err() << "FileUtil Test 1" << std::endl;
48    bool testval=true;
49    utility::FileUtil file(".");
50    if (testval=file.exists())
51      suite.err() << "\tfile exists: " << file.path() << std::endl;
52    suite.add(testval);
53    if (!(testval=file.permissions("rwx")))
54      suite.err() << "\trwx permissions on " << file.path() << std::endl;
55    suite.add(!testval);
56    if (!(testval=file.permissions("rx")))
57      suite.err() << "\trx permissions on " << file.path() << std::endl;
58    suite.add(!testval);
59    if (!(testval=file.permissions("d")))
60      suite.err() << "\td permissions on " << file.path() << std::endl;
61    suite.add(!testval);
62  }
63  catch (utility::IO_error e) {
64    suite.add(false);
65    suite.err() << e.what() << std::endl;
66  }
67
68  // checking permissions on non-existent file with ./ in path
69  try {
70    suite.err() << "FileUtil Test 2" << std::endl;
71    bool testval=true;
72    // filename below should be unique
73    utility::FileUtil file("./fileio_test.sdf34DSF");
74    suite.err() << "\tpath: " << std::endl;
75    if (!(testval=file.exists()))
76      suite.err() << "\tfile does not exist: " << file.path() << std::endl;
77    suite.add(!testval);
78    if (!(testval=file.permissions("r")))
79      suite.err() << "\tr permission on " << file.path() << std::endl;
80    suite.add(!testval);
81    if (!(testval=file.permissions("rwx")))
82      suite.err() << "\trwx permissions on " << file.path() << std::endl;
83    suite.add(!testval);
84    if (!(testval=file.permissions("w")))
85      suite.err() << "\tw permission on " << file.path() << std::endl;
86    suite.add(!testval); // 'w' on non-existent file ok if directory writeable
87    if (!(testval=file.permissions("d")))
88      suite.err() << "\td permission on " << file.path() << std::endl;
89    suite.add(!testval); 
90  }
91  catch (utility::IO_error e) {
92    suite.add(false);
93    suite.err() << e.what() << std::endl;
94  }
95
96  // checking permissions on non-existent file without ./ in path
97  try {
98    suite.err() << "FileUtil Test 3" << std::endl;
99    bool testval=true;
100    // filename below should be unique
101    utility::FileUtil file("fileio_test.sdf34DSF");
102    if (!(testval=file.exists()))
103      suite.err() << "\tfile does not exist: " << file.path() << std::endl;
104    suite.add(!testval);
105    if (!(testval=file.permissions("r")))
106      suite.err() << "\tr permission on " << file.path() << std::endl;
107    suite.add(!testval);
108    if (!(testval=file.permissions("rwx")))
109      suite.err() << "\trwx permissions on " << file.path() << std::endl;
110    suite.add(!testval);
111    if (!(testval=file.permissions("w")))
112      suite.err() << "\tw permission on " << file.path() << std::endl;
113    suite.add(!testval); // 'w' on non-existent file ok if directory writeable
114    if (!(testval=file.permissions("d")))
115      suite.err() << "\td permission on " << file.path() << std::endl;
116    suite.add(!testval); 
117  }
118  catch (utility::IO_error e) {
119    suite.add(false);
120    suite.err() << e.what() << std::endl;
121  }
122
123  // checking permissions on non-existent file with non-existent
124  // directories in path
125  try {
126    suite.err() << "FileUtil Test 4" << std::endl;
127    bool testval=true;
128     // intermediate dir must be unique!
129    utility::FileUtil file("./fileio_test.sdf34DSF/file");
130    if (!(testval=file.exists()))
131      suite.err() << "\tfile does not exist: " << file.path() << std::endl;
132    suite.add(!testval);
133    if ((testval=file.permissions("r")))
134      suite.err() << "\tr permission failed on " << file.path() << std::endl;
135    suite.add(testval);
136    if (testval=file.permissions("rwx"))
137      suite.err() << "\trwx permissions failed on " << file.path() << std::endl;
138    suite.add(testval);
139    if (testval=file.permissions("w"))
140      suite.err() << "\tw permission failed on " << file.path() << std::endl;
141    suite.add(testval);
142    if (testval=file.permissions("d"))
143      suite.err() << "\td permission failed on " << file.path() << std::endl;
144    suite.add(testval); 
145  }
146  catch (utility::IO_error e) {
147    suite.add(false);
148    suite.err() << e.what() << std::endl;
149  }
150
151  // checking permissions on existent file after non-existence file
152  // has been checked
153  try {
154    suite.err() << "FileUtil Test 5" << std::endl;
155    bool testval=true;
156     // intermediate dir must be unique!
157    utility::FileUtil file("./fileio_test.sdf34DSF/file");
158    if (!(testval=file.exists()))
159      suite.err() << "\tfile does not exist: " << file.path() << std::endl;
160    suite.add(!testval);
161    utility::FileUtil file2(test::filename("fileutil_test.cc").c_str());
162    if (!(testval=!file2.exists()))
163      suite.err() << "\tfile does exist: " << file2.path() << std::endl;
164    suite.add(!testval);
165  }
166  catch (utility::IO_error e) {
167    suite.add(false);
168    suite.err() << e.what() << std::endl;
169  }
170
171  try {
172    suite.err() << "FileUtil Test 6" << std::endl;
173    utility::FileUtil file("fileutil_test.cc");
174    file.permissions("rxwa"); // should throw
175    suite.add(false); 
176  }
177  catch (std::invalid_argument& e) {
178    suite.err() << "Expected exception thrown with what: " << e.what() 
179                << std::endl;
180  }
181
182  return suite.return_value();
183}
184
Note: See TracBrowser for help on using the repository browser.