source: trunk/test/fileutil_test.cc @ 1457

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

fixes #426 - skipping test of LFS when configure option --disable-largefile is used.

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