disable a VC6 warning occurring inside a standard header
[wxWidgets.git] / tests / streams / largefile.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/streams/largefile.cpp
3 // Purpose: Tests for large file support
4 // Author: Mike Wetherell
5 // RCS-ID: $Id$
6 // Copyright: (c) 2004 Mike Wetherell
7 // Licence: wxWidgets licence
8 ///////////////////////////////////////////////////////////////////////////////
9
10 //
11 // We're interested in what happens around offsets 0x7fffffff and 0xffffffff
12 // so the test writes a small chunk of test data just before and just after
13 // these offsets, then reads them back.
14 //
15 // The tests can be run with:
16 //
17 // test --verbose largeFile
18 //
19 // On systems supporting sparse files they may also be registered in the
20 // Streams subsuite.
21 //
22
23 // For compilers that support precompilation, includes "wx/wx.h".
24 #include "testprec.h"
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif
29
30 // for all others, include the necessary headers
31 #ifndef WX_PRECOMP
32 #include "wx/wx.h"
33 #endif
34
35 #include "wx/filename.h"
36 #include "wx/wfstream.h"
37
38 #ifdef __WXMSW__
39 #include "wx/msw/wrapwin.h"
40 #ifdef __VISUALC__
41 // 'nonstandard extension used : nameless struct/union' occurs inside
42 // winioctl.h
43 #pragma warning(disable:4201)
44 #endif
45 #include <winioctl.h>
46 #ifdef __VISUALC__
47 #pragma warning(default:4201)
48 #endif
49 #endif
50
51 #ifdef __VISUALC__
52 #define fileno _fileno
53 #endif
54
55 using std::auto_ptr;
56
57
58 ///////////////////////////////////////////////////////////////////////////////
59 // Helpers
60
61 bool IsFAT(const wxString& path);
62 void MakeSparse(const wxString& path, int fd);
63
64
65 ///////////////////////////////////////////////////////////////////////////////
66 // Base class for the test cases - common code
67
68 class LargeFileTest : public CppUnit::TestCase
69 {
70 public:
71 LargeFileTest(std::string name) : CppUnit::TestCase(name) { }
72 virtual ~LargeFileTest() { }
73
74 protected:
75 void runTest();
76
77 virtual wxInputStream *MakeInStream(const wxString& name) const = 0;
78 virtual wxOutputStream *MakeOutStream(const wxString& name) const = 0;
79 virtual bool HasLFS() const = 0;
80 };
81
82 void LargeFileTest::runTest()
83 {
84 // self deleting temp file
85 struct TmpFile {
86 TmpFile() : m_name(wxFileName::CreateTempFileName(_T("wxlfs-"))) { }
87 ~TmpFile() { if (!m_name.empty()) wxRemoveFile(m_name); }
88 wxString m_name;
89 } tmpfile;
90
91 CPPUNIT_ASSERT(!tmpfile.m_name.empty());
92
93 bool haveLFS = true;
94 bool fourGigLimit = false;
95
96 if (!HasLFS()) {
97 haveLFS = false;
98 wxString n(getName().c_str(), *wxConvCurrent);
99 wxLogInfo(n + _T(": No large file support, testing up to 2GB only"));
100 }
101 else if (IsFAT(tmpfile.m_name)) {
102 fourGigLimit = true;
103 wxString n(getName().c_str(), *wxConvCurrent);
104 wxLogInfo(n + _T(": FAT volumes are limited to 4GB files"));
105 }
106
107 // size of the test blocks
108 const size_t size = 0x40;
109
110 // the test blocks
111 char upto2Gig[size];
112 char past2Gig[size];
113 char upto4Gig[size];
114 char past4Gig[size];
115 memset(upto2Gig, 'A', size);
116 memset(past2Gig, 'B', size);
117 memset(upto4Gig, 'X', size);
118 memset(past4Gig, 'Y', size);
119
120 wxFileOffset pos;
121
122 // write a large file
123 {
124 auto_ptr<wxOutputStream> out(MakeOutStream(tmpfile.m_name));
125
126 // write 'A's at [ 0x7fffffbf, 0x7fffffff [
127 pos = 0x7fffffff - size;
128 CPPUNIT_ASSERT(out->SeekO(pos) == pos);
129 CPPUNIT_ASSERT(out->Write(upto2Gig, size).LastWrite() == size);
130 pos += size;
131
132 if (haveLFS) {
133 // write 'B's at [ 0x7fffffff, 0x8000003f [
134 CPPUNIT_ASSERT(out->Write(past2Gig, size).LastWrite() == size);
135 pos += size;
136 CPPUNIT_ASSERT(out->TellO() == pos);
137
138 // write 'X's at [ 0xffffffbf, 0xffffffff [
139 pos = 0xffffffff - size;
140 CPPUNIT_ASSERT(out->SeekO(pos) == pos);
141 CPPUNIT_ASSERT(out->Write(upto4Gig, size).LastWrite() == size);
142 pos += size;
143
144 if (!fourGigLimit) {
145 // write 'Y's at [ 0xffffffff, 0x10000003f [
146 CPPUNIT_ASSERT(out->Write(past4Gig, size).LastWrite() == size);
147 pos += size;
148 }
149 }
150
151 // check the file seems to be the right length
152 CPPUNIT_ASSERT(out->TellO() == pos);
153 CPPUNIT_ASSERT(out->GetLength() == pos);
154 }
155
156 // read the large file back
157 {
158 auto_ptr<wxInputStream> in(MakeInStream(tmpfile.m_name));
159 char buf[size];
160
161 if (haveLFS) {
162 CPPUNIT_ASSERT(in->GetLength() == pos);
163 pos = 0xffffffff;
164
165 if (!fourGigLimit) {
166 CPPUNIT_ASSERT(in->GetLength() > pos);
167
168 // read back the 'Y's at [ 0xffffffff, 0x10000003f [
169 CPPUNIT_ASSERT(in->SeekI(pos) == pos);
170 CPPUNIT_ASSERT(in->Read(buf, size).LastRead() == size);
171 CPPUNIT_ASSERT(memcmp(buf, past4Gig, size) == 0);
172
173 CPPUNIT_ASSERT(in->TellI() == in->GetLength());
174 }
175
176 // read back the 'X's at [ 0xffffffbf, 0xffffffff [
177 pos -= size;
178 CPPUNIT_ASSERT(in->SeekI(pos) == pos);
179 CPPUNIT_ASSERT(in->Read(buf, size).LastRead() == size);
180 CPPUNIT_ASSERT(memcmp(buf, upto4Gig, size) == 0);
181 pos += size;
182 CPPUNIT_ASSERT(in->TellI() == pos);
183
184 // read back the 'B's at [ 0x7fffffff, 0x8000003f [
185 pos = 0x7fffffff;
186 CPPUNIT_ASSERT(in->SeekI(pos) == pos);
187 CPPUNIT_ASSERT(in->Read(buf, size).LastRead() == size);
188 CPPUNIT_ASSERT(memcmp(buf, past2Gig, size) == 0);
189 }
190 else {
191 CPPUNIT_ASSERT(in->GetLength() == 0x7fffffff);
192 pos = 0x7fffffff;
193 }
194
195 // read back the 'A's at [ 0x7fffffbf, 0x7fffffff [
196 pos -= size;
197 CPPUNIT_ASSERT(in->SeekI(pos) == pos);
198 CPPUNIT_ASSERT(in->Read(buf, size).LastRead() == size);
199 CPPUNIT_ASSERT(memcmp(buf, upto2Gig, size) == 0);
200 pos += size;
201 CPPUNIT_ASSERT(in->TellI() == pos);
202 }
203 }
204
205
206 ///////////////////////////////////////////////////////////////////////////////
207 // wxFile test case
208
209 class LargeFileTest_wxFile : public LargeFileTest
210 {
211 public:
212 LargeFileTest_wxFile() : LargeFileTest("wxFile streams") { }
213
214 protected:
215 wxInputStream *MakeInStream(const wxString& name) const;
216 wxOutputStream *MakeOutStream(const wxString& name) const;
217 bool HasLFS() const { return (wxFileOffset)0xffffffff > 0; }
218 };
219
220 wxInputStream *LargeFileTest_wxFile::MakeInStream(const wxString& name) const
221 {
222 auto_ptr<wxFileInputStream> in(new wxFileInputStream(name));
223 CPPUNIT_ASSERT(in->Ok());
224 return in.release();
225 }
226
227 wxOutputStream *LargeFileTest_wxFile::MakeOutStream(const wxString& name) const
228 {
229 wxFile file(name, wxFile::write);
230 CPPUNIT_ASSERT(file.IsOpened());
231 int fd = file.fd();
232 file.Detach();
233 MakeSparse(name, fd);
234 return new wxFileOutputStream(fd);
235 }
236
237
238 ///////////////////////////////////////////////////////////////////////////////
239 // wxFFile test case
240
241 class LargeFileTest_wxFFile : public LargeFileTest
242 {
243 public:
244 LargeFileTest_wxFFile() : LargeFileTest("wxFFile streams") { }
245
246 protected:
247 wxInputStream *MakeInStream(const wxString& name) const;
248 wxOutputStream *MakeOutStream(const wxString& name) const;
249 bool HasLFS() const;
250 };
251
252 wxInputStream *LargeFileTest_wxFFile::MakeInStream(const wxString& name) const
253 {
254 auto_ptr<wxFFileInputStream> in(new wxFFileInputStream(name));
255 CPPUNIT_ASSERT(in->Ok());
256 return in.release();
257 }
258
259 wxOutputStream *LargeFileTest_wxFFile::MakeOutStream(const wxString& name) const
260 {
261 wxFFile file(name, _T("w"));
262 CPPUNIT_ASSERT(file.IsOpened());
263 FILE *fp = file.fp();
264 file.Detach();
265 MakeSparse(name, fileno(fp));
266 return new wxFFileOutputStream(fp);
267 }
268
269 bool LargeFileTest_wxFFile::HasLFS() const
270 {
271 #ifdef HAVE_FSEEKO
272 return (wxFileOffset)0xffffffff > 0;
273 #else
274 return false;
275 #endif
276 }
277
278
279 ///////////////////////////////////////////////////////////////////////////////
280 // The suite
281
282 class largeFile : public CppUnit::TestSuite
283 {
284 public:
285 largeFile() : CppUnit::TestSuite("largeFile") { }
286
287 static CppUnit::Test *suite();
288 };
289
290 CppUnit::Test *largeFile::suite()
291 {
292 largeFile *suite = new largeFile;
293
294 suite->addTest(new LargeFileTest_wxFile);
295 suite->addTest(new LargeFileTest_wxFFile);
296
297 return suite;
298 }
299
300
301 ///////////////////////////////////////////////////////////////////////////////
302 // Implement the helpers
303 //
304 // Ideally these tests will be part of the default suite so that regressions
305 // are picked up. However this is only possible when sparse files are
306 // supported otherwise the tests require too much disk space.
307 //
308 // On unix, most filesystems support sparse files, though not all. So for now
309 // I'm not assuming sparse file support on unix. On Windows it's possible to
310 // test, and sparse files should be available on Win 5+ with NTFS.
311
312 #ifdef __WXMSW__
313
314 #ifndef FILE_SUPPORTS_SPARSE_FILES
315 #define FILE_SUPPORTS_SPARSE_FILES 0x00000040
316 #endif
317
318 #ifndef FSCTL_SET_SPARSE
319
320 # ifndef FILE_SPECIAL_ACCESS
321 # define FILE_SPECIAL_ACCESS FILE_ANY_ACCESS
322 # endif
323 # define FSCTL_SET_SPARSE CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 49, \
324 METHOD_BUFFERED, FILE_SPECIAL_ACCESS)
325 #endif
326
327 static DWORD volumeFlags;
328 static wxChar volumeType[64];
329 static bool volumeInfoInit;
330
331 void GetVolumeInfo(const wxString& path)
332 {
333 // extract the volume 'C:\' or '\\tooter\share\' from the path
334 wxString vol;
335
336 if (path.substr(1, 2) == _T(":\\")) {
337 vol = path.substr(0, 3);
338 } else {
339 if (path.substr(0, 2) == _T("\\\\")) {
340 size_t i = path.find(_T('\\'), 2);
341
342 if (i != wxString::npos && i > 2) {
343 size_t j = path.find(_T('\\'), ++i);
344
345 if (j != i)
346 vol = path.substr(0, j) + _T("\\");
347 }
348 }
349 }
350
351 // NULL means the current volume
352 const wxChar *pVol = vol.empty() ? (const wxChar *)NULL
353 : vol.c_str();
354
355 if (!::GetVolumeInformation(pVol, NULL, 0, NULL, NULL,
356 &volumeFlags,
357 volumeType,
358 WXSIZEOF(volumeType)))
359 wxLogSysError(_T("GetVolumeInformation() failed"));
360
361 volumeInfoInit = true;
362 }
363
364 bool IsFAT(const wxString& path)
365 {
366 if (!volumeInfoInit)
367 GetVolumeInfo(path);
368 return wxString(volumeType).Upper().find(_T("FAT")) != wxString::npos;
369 }
370
371 void MakeSparse(const wxString& path, int fd)
372 {
373 DWORD cb;
374
375 if (!volumeInfoInit)
376 GetVolumeInfo(path);
377
378 if ((volumeFlags & FILE_SUPPORTS_SPARSE_FILES) != 0)
379 if (!::DeviceIoControl((HANDLE)_get_osfhandle(fd),
380 FSCTL_SET_SPARSE,
381 NULL, 0, NULL, 0, &cb, NULL))
382 volumeFlags &= ~FILE_SUPPORTS_SPARSE_FILES;
383 }
384
385 CppUnit::Test* GetlargeFileSuite()
386 {
387 if (!volumeInfoInit) {
388 wxFile file;
389 wxString path = wxFileName::CreateTempFileName(_T("wxlfs-"), &file);
390 MakeSparse(path, file.fd());
391 wxRemoveFile(path);
392 }
393
394 if ((volumeFlags & FILE_SUPPORTS_SPARSE_FILES) != 0)
395 return largeFile::suite();
396 else
397 return NULL;
398 }
399
400 #else // __WXMSW__
401
402 bool IsFAT(const wxString& WXUNUSED(path)) { return false; }
403 void MakeSparse(const wxString& WXUNUSED(path), int WXUNUSED(fd)) { }
404
405 CppUnit::Test* GetlargeFileSuite() { return NULL; }
406
407 #endif // __WXMSW__
408
409 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(largeFile, "largeFile");
410 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(largeFile, "Streams.largeFile");