]> git.saurik.com Git - wxWidgets.git/blame - tests/archive/archivetest.cpp
Implement hatched/stippled pens/brushes in wxGraphicsContext for Cairo.
[wxWidgets.git] / tests / archive / archivetest.cpp
CommitLineData
00375592
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/archive/archive.cpp
3// Purpose: Test the archive classes
4// Author: Mike Wetherell
5// RCS-ID: $Id$
6// Copyright: (c) 2004 Mike Wetherell
7// Licence: wxWindows licence
8///////////////////////////////////////////////////////////////////////////////
9
8899b155 10#include "testprec.h"
00375592
VZ
11
12#ifdef __BORLANDC__
13# pragma hdrstop
14#endif
15
16#ifndef WX_PRECOMP
17# include "wx/wx.h"
18#endif
19
9e8e867f 20#if wxUSE_STREAMS && wxUSE_ARCHIVE_STREAMS
00375592 21
f44eaed6
RN
22// VC++ 6 warns that the list iterator's '->' operator will not work whenever
23// std::list is used with a non-pointer, so switch it off.
24#if defined _MSC_VER && _MSC_VER < 1300
95662a83
RN
25#pragma warning (disable:4284)
26#endif
27
e6477b92 28#include "archivetest.h"
00375592 29#include "wx/dir.h"
00375592
VZ
30#include <string>
31#include <list>
95662a83 32#include <map>
00375592
VZ
33#include <sys/stat.h>
34
95662a83
RN
35using std::string;
36using std::auto_ptr;
37
38
00375592
VZ
39// Check whether member templates can be used
40//
41#if defined __GNUC__ && \
42 (__GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95))
43# define WXARC_MEMBER_TEMPLATES
44#endif
a901c3fd 45#if defined _MSC_VER && _MSC_VER >= 1310 && !defined __WIN64__
00375592
VZ
46# define WXARC_MEMBER_TEMPLATES
47#endif
48#if defined __BORLANDC__ && __BORLANDC__ >= 0x530
49# define WXARC_MEMBER_TEMPLATES
50#endif
51#if defined __DMC__ && __DMC__ >= 0x832
52# define WXARC_MEMBER_TEMPLATES
53#endif
00375592
VZ
54#if defined __HP_aCC && __HP_aCC > 33300
55# define WXARC_MEMBER_TEMPLATES
56#endif
57#if defined __SUNPRO_CC && __SUNPRO_CC > 0x500
58# define WXARC_MEMBER_TEMPLATES
59#endif
60
61
00375592
VZ
62///////////////////////////////////////////////////////////////////////////////
63// A class to hold a test entry
64
00375592
VZ
65TestEntry::TestEntry(const wxDateTime& dt, int len, const char *data)
66 : m_dt(dt),
67 m_len(len),
68 m_isText(len > 0)
69{
f44eaed6
RN
70 m_data = new char[len];
71 memcpy(m_data, data, len);
00375592
VZ
72
73 for (int i = 0; i < len && m_isText; i++)
74 m_isText = (signed char)m_data[i] > 0;
75}
76
77
78///////////////////////////////////////////////////////////////////////////////
79// TestOutputStream and TestInputStream are memory streams which can be
80// seekable or non-seekable.
81
e6477b92
MW
82const size_t STUB_SIZE = 2048;
83const size_t INITIAL_SIZE = 0x18000;
84const wxFileOffset SEEK_LIMIT = 0x100000;
00375592
VZ
85
86TestOutputStream::TestOutputStream(int options)
87 : m_options(options)
88{
89 Init();
90}
91
92void TestOutputStream::Init()
93{
94 m_data = NULL;
95 m_size = 0;
96 m_capacity = 0;
97 m_pos = 0;
98
99 if (m_options & Stub) {
100 wxCharBuffer buf(STUB_SIZE);
101 memset(buf.data(), 0, STUB_SIZE);
102 Write(buf, STUB_SIZE);
103 }
104}
105
106wxFileOffset TestOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
107{
108 if ((m_options & PipeOut) == 0) {
109 switch (mode) {
110 case wxFromStart: break;
111 case wxFromCurrent: pos += m_pos; break;
112 case wxFromEnd: pos += m_size; break;
113 }
114 if (pos < 0 || pos > SEEK_LIMIT)
115 return wxInvalidOffset;
95662a83 116 m_pos = (size_t)pos;
00375592
VZ
117 return m_pos;
118 }
119 return wxInvalidOffset;
120}
121
122wxFileOffset TestOutputStream::OnSysTell() const
123{
46263455 124 return (m_options & PipeOut) == 0 ? (wxFileOffset)m_pos : wxInvalidOffset;
00375592
VZ
125}
126
127size_t TestOutputStream::OnSysWrite(const void *buffer, size_t size)
128{
129 if (!IsOk() || !size)
130 return 0;
131 m_lasterror = wxSTREAM_WRITE_ERROR;
132
133 size_t newsize = m_pos + size;
134 wxCHECK(newsize > m_pos, 0);
135
136 if (m_capacity < newsize) {
e6477b92 137 size_t capacity = m_capacity ? m_capacity : INITIAL_SIZE;
00375592
VZ
138
139 while (capacity < newsize) {
140 capacity <<= 1;
141 wxCHECK(capacity > m_capacity, 0);
142 }
143
144 char *buf = new char[capacity];
145 if (m_data)
146 memcpy(buf, m_data, m_capacity);
147 delete [] m_data;
148 m_data = buf;
149 m_capacity = capacity;
150 }
151
152 memcpy(m_data + m_pos, buffer, size);
153 m_pos += size;
154 if (m_pos > m_size)
155 m_size = m_pos;
156 m_lasterror = wxSTREAM_NO_ERROR;
157
158 return size;
159}
160
f44eaed6 161void TestOutputStream::GetData(char*& data, size_t& size)
00375592
VZ
162{
163 data = m_data;
164 size = m_size;
165
166 if (m_options & Stub) {
167 char *d = m_data;
168 size += STUB_SIZE;
169
170 if (size > m_capacity) {
171 d = new char[size];
172 memcpy(d + STUB_SIZE, m_data, m_size);
173 delete [] m_data;
174 }
175 else {
176 memmove(d + STUB_SIZE, d, m_size);
177 }
178
179 memset(d, 0, STUB_SIZE);
180 data = d;
181 }
182
183 Init();
184 Reset();
185}
186
00375592 187
e6477b92
MW
188///////////////////////////////////////////////////////////////////////////////
189// TestOutputStream and TestInputStream are memory streams which can be
190// seekable or non-seekable.
00375592
VZ
191
192TestInputStream::TestInputStream(const TestInputStream& in)
a09cd189
WS
193 : wxInputStream(),
194 m_options(in.m_options),
00375592 195 m_pos(in.m_pos),
716e748b
MW
196 m_size(in.m_size),
197 m_eoftype(in.m_eoftype)
00375592 198{
f44eaed6
RN
199 m_data = new char[m_size];
200 memcpy(m_data, in.m_data, m_size);
00375592
VZ
201}
202
203void TestInputStream::Rewind()
204{
205 if ((m_options & Stub) && (m_options & PipeIn))
e6477b92 206 m_pos = STUB_SIZE * 2;
00375592
VZ
207 else
208 m_pos = 0;
209
210 if (m_wbacksize) {
211 free(m_wback);
212 m_wback = NULL;
213 m_wbacksize = 0;
214 m_wbackcur = 0;
215 }
98de4ac1
MW
216
217 Reset();
00375592
VZ
218}
219
220void TestInputStream::SetData(TestOutputStream& out)
221{
f44eaed6 222 delete [] m_data;
00375592
VZ
223 m_options = out.GetOptions();
224 out.GetData(m_data, m_size);
225 Rewind();
00375592
VZ
226}
227
228wxFileOffset TestInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
229{
230 if ((m_options & PipeIn) == 0) {
231 switch (mode) {
232 case wxFromStart: break;
233 case wxFromCurrent: pos += m_pos; break;
234 case wxFromEnd: pos += m_size; break;
235 }
e6477b92 236 if (pos < 0 || pos > SEEK_LIMIT)
00375592 237 return wxInvalidOffset;
95662a83 238 m_pos = (size_t)pos;
00375592
VZ
239 return m_pos;
240 }
241 return wxInvalidOffset;
242}
243
244wxFileOffset TestInputStream::OnSysTell() const
245{
46263455 246 return (m_options & PipeIn) == 0 ? (wxFileOffset)m_pos : wxInvalidOffset;
00375592
VZ
247}
248
249size_t TestInputStream::OnSysRead(void *buffer, size_t size)
250{
251 if (!IsOk() || !size)
252 return 0;
716e748b
MW
253
254 size_t count;
255
256 if (m_pos >= m_size)
257 count = 0;
258 else if (m_size - m_pos < size)
259 count = m_size - m_pos;
260 else
261 count = size;
262
263 if (count) {
264 memcpy(buffer, m_data + m_pos, count);
265 m_pos += count;
00375592
VZ
266 }
267
716e748b 268 if (((m_eoftype & AtLast) != 0 && m_pos >= m_size) || count < size)
fe26031e 269 {
716e748b
MW
270 if ((m_eoftype & WithError) != 0)
271 m_lasterror = wxSTREAM_READ_ERROR;
272 else
273 m_lasterror = wxSTREAM_EOF;
fe26031e 274 }
716e748b
MW
275
276 return count;
00375592
VZ
277}
278
279
280///////////////////////////////////////////////////////////////////////////////
281// minimal non-intrusive reference counting pointer for testing the iterators
282
283template <class T> class Ptr
284{
285public:
286 explicit Ptr(T* p = NULL) : m_p(p), m_count(new int) { *m_count = 1; }
287 Ptr(const Ptr& sp) : m_p(sp.m_p), m_count(sp.m_count) { ++*m_count; }
288 ~Ptr() { Free(); }
289
290 Ptr& operator =(const Ptr& sp) {
291 if (&sp != this) {
292 Free();
293 m_p = sp.m_p;
294 m_count = sp.m_count;
295 ++*m_count;
296 }
297 return *this;
298 }
299
300 T* get() const { return m_p; }
301 T* operator->() const { return m_p; }
302 T& operator*() const { return *m_p; }
303
304private:
305 void Free() {
306 if (--*m_count == 0) {
307 delete m_p;
308 delete m_count;
309 }
310 }
311
312 T *m_p;
313 int *m_count;
314};
315
316
317///////////////////////////////////////////////////////////////////////////////
318// Clean-up for temp directory
319
320class TempDir
321{
322public:
323 TempDir();
324 ~TempDir();
325 wxString GetName() const { return m_tmp; }
326
327private:
328 void RemoveDir(wxString& path);
329 wxString m_tmp;
330 wxString m_original;
331};
332
333TempDir::TempDir()
334{
9a83f860 335 wxString tmp = wxFileName::CreateTempFileName(wxT("arctest-"));
489f6cf7 336 if (!tmp.empty()) {
00375592
VZ
337 wxRemoveFile(tmp);
338 m_original = wxGetCwd();
339 CPPUNIT_ASSERT(wxMkdir(tmp, 0700));
340 m_tmp = tmp;
341 CPPUNIT_ASSERT(wxSetWorkingDirectory(tmp));
342 }
343}
344
345TempDir::~TempDir()
346{
489f6cf7 347 if (!m_tmp.empty()) {
00375592
VZ
348 wxSetWorkingDirectory(m_original);
349 RemoveDir(m_tmp);
350 }
351}
352
353void TempDir::RemoveDir(wxString& path)
354{
355 wxCHECK_RET(!m_tmp.empty() && path.substr(0, m_tmp.length()) == m_tmp,
9a83f860 356 wxT("remove '") + path + wxT("' fails safety check"));
00375592
VZ
357
358 const wxChar *files[] = {
9a83f860
VZ
359 wxT("text/empty"),
360 wxT("text/small"),
361 wxT("bin/bin1000"),
362 wxT("bin/bin4095"),
363 wxT("bin/bin4096"),
364 wxT("bin/bin4097"),
365 wxT("bin/bin16384"),
366 wxT("zero/zero5"),
367 wxT("zero/zero1024"),
368 wxT("zero/zero32768"),
369 wxT("zero/zero16385"),
370 wxT("zero/newname"),
371 wxT("newfile"),
00375592
VZ
372 };
373
374 const wxChar *dirs[] = {
9a83f860 375 wxT("text/"), wxT("bin/"), wxT("zero/"), wxT("empty/")
00375592
VZ
376 };
377
378 wxString tmp = m_tmp + wxFileName::GetPathSeparator();
379 size_t i;
380
381 for (i = 0; i < WXSIZEOF(files); i++)
382 wxRemoveFile(tmp + wxFileName(files[i], wxPATH_UNIX).GetFullPath());
383
384 for (i = 0; i < WXSIZEOF(dirs); i++)
385 wxRmdir(tmp + wxFileName(dirs[i], wxPATH_UNIX).GetFullPath());
386
387 if (!wxRmdir(m_tmp))
43b2d5e7 388 {
9a83f860 389 wxLogSysError(wxT("can't remove temporary dir '%s'"), m_tmp.c_str());
43b2d5e7 390 }
00375592
VZ
391}
392
393
394///////////////////////////////////////////////////////////////////////////////
395// wxFFile streams for piping to/from an external program
396
397#if defined __UNIX__ || defined __MINGW32__
398# define WXARC_popen popen
399# define WXARC_pclose pclose
400#elif defined _MSC_VER || defined __BORLANDC__
401# define WXARC_popen _popen
402# define WXARC_pclose _pclose
403#else
404# define WXARC_NO_POPEN
405# define WXARC_popen(cmd, type) NULL
406# define WXARC_pclose(fp)
407#endif
408
bb5a9514 409#ifdef __WINDOWS__
00375592
VZ
410# define WXARC_b "b"
411#else
412# define WXARC_b
413#endif
414
e6477b92
MW
415PFileInputStream::PFileInputStream(const wxString& cmd)
416 : wxFFileInputStream(WXARC_popen(cmd.mb_str(), "r" WXARC_b))
00375592 417{
e6477b92 418}
00375592 419
e6477b92 420PFileInputStream::~PFileInputStream()
00375592 421{
e6477b92
MW
422 WXARC_pclose(m_file->fp()); m_file->Detach();
423}
424
425PFileOutputStream::PFileOutputStream(const wxString& cmd)
426: wxFFileOutputStream(WXARC_popen(cmd.mb_str(), "w" WXARC_b))
427{
428}
429
430PFileOutputStream::~PFileOutputStream()
431{
432 WXARC_pclose(m_file->fp()); m_file->Detach();
433}
00375592
VZ
434
435
436///////////////////////////////////////////////////////////////////////////////
437// The test case
438
e6477b92
MW
439template <class ClassFactoryT>
440ArchiveTestCase<ClassFactoryT>::ArchiveTestCase(
95662a83 441 string name,
e6477b92 442 ClassFactoryT *factory,
95662a83
RN
443 int options,
444 const wxString& archiver,
445 const wxString& unarchiver)
446 :
716e748b 447 CppUnit::TestCase(TestId::MakeId() + name),
e6477b92 448 m_factory(factory),
95662a83 449 m_options(options),
e6477b92 450 m_timeStamp(1, wxDateTime::Mar, 2004, 12, 0),
716e748b 451 m_id(TestId::GetId()),
95662a83
RN
452 m_archiver(archiver),
453 m_unarchiver(unarchiver)
00375592 454{
95662a83 455 wxASSERT(m_factory.get() != NULL);
00375592 456}
a09cd189 457
e6477b92
MW
458template <class ClassFactoryT>
459ArchiveTestCase<ClassFactoryT>::~ArchiveTestCase()
00375592
VZ
460{
461 TestEntries::iterator it;
462 for (it = m_testEntries.begin(); it != m_testEntries.end(); ++it)
463 delete it->second;
464}
465
e6477b92
MW
466template <class ClassFactoryT>
467void ArchiveTestCase<ClassFactoryT>::runTest()
00375592
VZ
468{
469 TestOutputStream out(m_options);
470
471 CreateTestData();
472
473 if (m_archiver.empty())
474 CreateArchive(out);
475 else
476 CreateArchive(out, m_archiver);
477
478 // check archive could be created
46263455 479 CPPUNIT_ASSERT(out.GetLength() > 0);
00375592 480
716e748b 481 TestInputStream in(out, m_id % ((m_options & PipeIn) ? 4 : 3));
00375592
VZ
482
483 TestIterator(in);
484 in.Rewind();
485 TestPairIterator(in);
486 in.Rewind();
487 TestSmartIterator(in);
488 in.Rewind();
489 TestSmartPairIterator(in);
490 in.Rewind();
491
492 if ((m_options & PipeIn) == 0) {
493 ReadSimultaneous(in);
494 in.Rewind();
495 }
496
497 ModifyArchive(in, out);
498 in.SetData(out);
499
500 if (m_unarchiver.empty())
501 ExtractArchive(in);
502 else
503 ExtractArchive(in, m_unarchiver);
a09cd189 504
00375592
VZ
505 // check that all the test entries were found in the archive
506 CPPUNIT_ASSERT(m_testEntries.empty());
507}
508
e6477b92
MW
509template <class ClassFactoryT>
510void ArchiveTestCase<ClassFactoryT>::CreateTestData()
00375592
VZ
511{
512 Add("text/");
513 Add("text/empty", "");
514 Add("text/small", "Small text file for testing\n"
515 "archive streams in wxWidgets\n");
516
517 Add("bin/");
518 Add("bin/bin1000", 1000);
519 Add("bin/bin4095", 4095);
520 Add("bin/bin4096", 4096);
521 Add("bin/bin4097", 4097);
522 Add("bin/bin16384", 16384);
523
524 Add("zero/");
525 Add("zero/zero5", 5, 0);
526 Add("zero/zero1024", 1024, 109);
527 Add("zero/zero32768", 32768, 106);
528 Add("zero/zero16385", 16385, 119);
529
530 Add("empty/");
531}
532
e6477b92
MW
533template <class ClassFactoryT>
534TestEntry& ArchiveTestCase<ClassFactoryT>::Add(const char *name,
535 const char *data,
536 int len /*=-1*/)
00375592
VZ
537{
538 if (len == -1)
539 len = strlen(data);
540 TestEntry*& entry = m_testEntries[wxString(name, *wxConvCurrent)];
541 wxASSERT(entry == NULL);
542 entry = new TestEntry(m_timeStamp, len, data);
543 m_timeStamp += wxTimeSpan(0, 1, 30);
544 return *entry;
545}
546
e6477b92
MW
547template <class ClassFactoryT>
548TestEntry& ArchiveTestCase<ClassFactoryT>::Add(const char *name,
549 int len /*=0*/,
550 int value /*=EOF*/)
00375592
VZ
551{
552 wxCharBuffer buf(len);
553 for (int i = 0; i < len; i++)
f44eaed6 554 buf.data()[i] = (char)(value == EOF ? rand() : value);
00375592
VZ
555 return Add(name, buf, len);
556}
557
558// Create an archive using the wx archive classes, write it to 'out'
559//
e6477b92
MW
560template <class ClassFactoryT>
561void ArchiveTestCase<ClassFactoryT>::CreateArchive(wxOutputStream& out)
00375592 562{
95662a83 563 auto_ptr<OutputStreamT> arc(m_factory->NewStream(out));
00375592
VZ
564 TestEntries::iterator it;
565
566 OnCreateArchive(*arc);
567
568 // We want to try creating entries in various different ways, 'choices'
569 // is just a number used to select between all the various possibilities.
570 int choices = m_id;
571
572 for (it = m_testEntries.begin(); it != m_testEntries.end(); ++it) {
573 choices += 5;
574 TestEntry& testEntry = *it->second;
575 wxString name = it->first;
576
577 // It should be possible to create a directory entry just by supplying
578 // a name that looks like a directory, or alternatively any old name
579 // can be identified as a directory using SetIsDir or PutNextDirEntry
9a83f860 580 bool setIsDir = name.Last() == wxT('/') && (choices & 1);
00375592
VZ
581 if (setIsDir)
582 name.erase(name.length() - 1);
583
584 // provide some context for the error message so that we know which
585 // iteration of the loop we were on
9a83f860 586 string error_entry((wxT(" '") + name + wxT("'")).mb_str());
95662a83 587 string error_context(" failed for entry" + error_entry);
00375592
VZ
588
589 if ((choices & 2) || testEntry.IsText()) {
590 // try PutNextEntry(EntryT *pEntry)
95662a83 591 auto_ptr<EntryT> entry(m_factory->NewEntry());
00375592
VZ
592 entry->SetName(name, wxPATH_UNIX);
593 if (setIsDir)
594 entry->SetIsDir();
595 entry->SetDateTime(testEntry.GetDateTime());
596 entry->SetSize(testEntry.GetLength());
597 OnCreateEntry(*arc, testEntry, entry.get());
598 CPPUNIT_ASSERT_MESSAGE("PutNextEntry" + error_context,
599 arc->PutNextEntry(entry.release()));
600 }
601 else {
602 // try the convenience methods
603 OnCreateEntry(*arc, testEntry);
604 if (setIsDir)
605 CPPUNIT_ASSERT_MESSAGE("PutNextDirEntry" + error_context,
606 arc->PutNextDirEntry(name, testEntry.GetDateTime()));
607 else
608 CPPUNIT_ASSERT_MESSAGE("PutNextEntry" + error_context,
609 arc->PutNextEntry(name, testEntry.GetDateTime(),
610 testEntry.GetLength()));
611 }
612
9a83f860 613 if (it->first.Last() != wxT('/')) {
00375592
VZ
614 // for non-dirs write the data
615 arc->Write(testEntry.GetData(), testEntry.GetSize());
616 CPPUNIT_ASSERT_MESSAGE("LastWrite check" + error_context,
617 arc->LastWrite() == testEntry.GetSize());
618 // should work with or without explicit CloseEntry
619 if (choices & 3)
620 CPPUNIT_ASSERT_MESSAGE("CloseEntry" + error_context,
621 arc->CloseEntry());
622 }
623
624 CPPUNIT_ASSERT_MESSAGE("IsOk" + error_context, arc->IsOk());
625 }
626
627 // should work with or without explicit Close
628 if (m_id % 2)
629 CPPUNIT_ASSERT(arc->Close());
630}
631
632// Create an archive using an external archive program
633//
e6477b92
MW
634template <class ClassFactoryT>
635void ArchiveTestCase<ClassFactoryT>::CreateArchive(wxOutputStream& out,
636 const wxString& archiver)
00375592
VZ
637{
638 // for an external archiver the test data need to be written to
639 // temp files
640 TempDir tmpdir;
641
642 // write the files
643 TestEntries::iterator i;
644 for (i = m_testEntries.begin(); i != m_testEntries.end(); ++i) {
645 wxFileName fn(i->first, wxPATH_UNIX);
646 TestEntry& entry = *i->second;
647
648 if (fn.IsDir()) {
9f7191db 649 wxFileName::Mkdir(fn.GetPath(), 0777, wxPATH_MKDIR_FULL);
00375592
VZ
650 } else {
651 wxFileName::Mkdir(fn.GetPath(), 0777, wxPATH_MKDIR_FULL);
652 wxFFileOutputStream fileout(fn.GetFullPath());
653 fileout.Write(entry.GetData(), entry.GetSize());
654 }
655 }
656
657 for (i = m_testEntries.begin(); i != m_testEntries.end(); ++i) {
658 wxFileName fn(i->first, wxPATH_UNIX);
659 TestEntry& entry = *i->second;
660 wxDateTime dt = entry.GetDateTime();
bb5a9514 661#ifdef __WINDOWS__
00375592
VZ
662 if (fn.IsDir())
663 entry.SetDateTime(wxDateTime());
664 else
665#endif
666 fn.SetTimes(NULL, &dt, NULL);
667 }
668
669 if ((m_options & PipeOut) == 0) {
670 wxFileName fn(tmpdir.GetName());
9a83f860 671 fn.SetExt(wxT("arc"));
e6477b92 672 wxString tmparc = fn.GetPath(wxPATH_GET_SEPARATOR) + fn.GetFullName();
00375592
VZ
673
674 // call the archiver to create an archive file
675 system(wxString::Format(archiver, tmparc.c_str()).mb_str());
676
677 // then load the archive file
678 {
679 wxFFileInputStream in(tmparc);
a1b806b9 680 if (in.IsOk())
00375592
VZ
681 out.Write(in);
682 }
683
684 wxRemoveFile(tmparc);
685 }
686 else {
687 // for the non-seekable test, have the archiver output to "-"
688 // and read the archive via a pipe
9a83f860 689 PFileInputStream in(wxString::Format(archiver, wxT("-")));
a1b806b9 690 if (in.IsOk())
00375592
VZ
691 out.Write(in);
692 }
693}
694
695// Do a standard set of modification on an archive, delete an entry,
696// rename an entry and add an entry
697//
e6477b92
MW
698template <class ClassFactoryT>
699void ArchiveTestCase<ClassFactoryT>::ModifyArchive(wxInputStream& in,
700 wxOutputStream& out)
00375592 701{
95662a83
RN
702 auto_ptr<InputStreamT> arcIn(m_factory->NewStream(in));
703 auto_ptr<OutputStreamT> arcOut(m_factory->NewStream(out));
f44eaed6 704 EntryT *pEntry;
00375592 705
9a83f860
VZ
706 const wxString deleteName = wxT("bin/bin1000");
707 const wxString renameFrom = wxT("zero/zero1024");
708 const wxString renameTo = wxT("zero/newname");
709 const wxString newName = wxT("newfile");
00375592
VZ
710 const char *newData = "New file added as a test\n";
711
712 arcOut->CopyArchiveMetaData(*arcIn);
713
f44eaed6
RN
714 while ((pEntry = arcIn->GetNextEntry()) != NULL) {
715 auto_ptr<EntryT> entry(pEntry);
716 OnSetNotifier(*entry);
717 wxString name = entry->GetName(wxPATH_UNIX);
00375592
VZ
718
719 // provide some context for the error message so that we know which
720 // iteration of the loop we were on
9a83f860 721 string error_entry((wxT(" '") + name + wxT("'")).mb_str());
95662a83 722 string error_context(" failed for entry" + error_entry);
00375592
VZ
723
724 if (name == deleteName) {
725 TestEntries::iterator it = m_testEntries.find(name);
726 CPPUNIT_ASSERT_MESSAGE(
727 "deletion failed (already deleted?) for" + error_entry,
728 it != m_testEntries.end());
729 TestEntry *p = it->second;
730 m_testEntries.erase(it);
731 delete p;
732 }
733 else {
734 if (name == renameFrom) {
f44eaed6 735 entry->SetName(renameTo);
00375592
VZ
736 TestEntries::iterator it = m_testEntries.find(renameFrom);
737 CPPUNIT_ASSERT_MESSAGE(
738 "rename failed (already renamed?) for" + error_entry,
739 it != m_testEntries.end());
740 TestEntry *p = it->second;
741 m_testEntries.erase(it);
742 m_testEntries[renameTo] = p;
743 }
744
745 CPPUNIT_ASSERT_MESSAGE("CopyEntry" + error_context,
f44eaed6 746 arcOut->CopyEntry(entry.release(), *arcIn));
00375592
VZ
747 }
748 }
749
750 // check that the deletion and rename were done
751 CPPUNIT_ASSERT(m_testEntries.count(deleteName) == 0);
752 CPPUNIT_ASSERT(m_testEntries.count(renameFrom) == 0);
753 CPPUNIT_ASSERT(m_testEntries.count(renameTo) == 1);
754
755 // check that the end of the input archive was reached without error
756 CPPUNIT_ASSERT(arcIn->Eof());
757
758 // try adding a new entry
759 TestEntry& testEntry = Add(newName.mb_str(), newData);
95662a83
RN
760 auto_ptr<EntryT> newentry(m_factory->NewEntry());
761 newentry->SetName(newName);
762 newentry->SetDateTime(testEntry.GetDateTime());
763 newentry->SetSize(testEntry.GetLength());
764 OnCreateEntry(*arcOut, testEntry, newentry.get());
765 OnSetNotifier(*newentry);
766 CPPUNIT_ASSERT(arcOut->PutNextEntry(newentry.release()));
00375592
VZ
767 CPPUNIT_ASSERT(arcOut->Write(newData, strlen(newData)).IsOk());
768
769 // should work with or without explicit Close
770 if (m_id % 2)
771 CPPUNIT_ASSERT(arcOut->Close());
772}
773
774// Extract an archive using the wx archive classes
775//
e6477b92
MW
776template <class ClassFactoryT>
777void ArchiveTestCase<ClassFactoryT>::ExtractArchive(wxInputStream& in)
00375592
VZ
778{
779 typedef Ptr<EntryT> EntryPtr;
780 typedef std::list<EntryPtr> Entries;
781 typedef typename Entries::iterator EntryIter;
782
95662a83 783 auto_ptr<InputStreamT> arc(m_factory->NewStream(in));
00375592
VZ
784 int expectedTotal = m_testEntries.size();
785 EntryPtr entry;
786 Entries entries;
787
788 if ((m_options & PipeIn) == 0)
789 OnArchiveExtracted(*arc, expectedTotal);
790
791 while (entry = EntryPtr(arc->GetNextEntry()), entry.get() != NULL) {
792 wxString name = entry->GetName(wxPATH_UNIX);
793
794 // provide some context for the error message so that we know which
795 // iteration of the loop we were on
9a83f860 796 string error_entry((wxT(" '") + name + wxT("'")).mb_str());
95662a83 797 string error_context(" failed for entry" + error_entry);
00375592
VZ
798
799 TestEntries::iterator it = m_testEntries.find(name);
800 CPPUNIT_ASSERT_MESSAGE(
801 "archive contains an entry that shouldn't be there" + error_entry,
802 it != m_testEntries.end());
803
804 const TestEntry& testEntry = *it->second;
805
bb5a9514 806#ifndef __WINDOWS__
cc90c1cc
MW
807 // On Windows some archivers compensate for Windows DST handling, but
808 // other don't, so disable the test for now.
00375592
VZ
809 wxDateTime dt = testEntry.GetDateTime();
810 if (dt.IsValid())
811 CPPUNIT_ASSERT_MESSAGE("timestamp check" + error_context,
812 dt == entry->GetDateTime());
cc90c1cc 813#endif
00375592
VZ
814
815 // non-seekable entries are allowed to have GetSize == wxInvalidOffset
816 // until the end of the entry's data has been read past
817 CPPUNIT_ASSERT_MESSAGE("entry size check" + error_context,
818 testEntry.GetLength() == entry->GetSize() ||
819 ((m_options & PipeIn) != 0 && entry->GetSize() == wxInvalidOffset));
820 CPPUNIT_ASSERT_MESSAGE(
46263455
VZ
821 "arc->GetLength() == entry->GetSize()" + error_context,
822 arc->GetLength() == entry->GetSize());
00375592 823
9a83f860 824 if (name.Last() != wxT('/'))
00375592
VZ
825 {
826 CPPUNIT_ASSERT_MESSAGE("!IsDir" + error_context,
827 !entry->IsDir());
828 wxCharBuffer buf(testEntry.GetSize() + 1);
829 CPPUNIT_ASSERT_MESSAGE("Read until Eof" + error_context,
830 arc->Read(buf.data(), testEntry.GetSize() + 1).Eof());
831 CPPUNIT_ASSERT_MESSAGE("LastRead check" + error_context,
832 arc->LastRead() == testEntry.GetSize());
833 CPPUNIT_ASSERT_MESSAGE("data compare" + error_context,
834 !memcmp(buf.data(), testEntry.GetData(), testEntry.GetSize()));
835 } else {
836 CPPUNIT_ASSERT_MESSAGE("IsDir" + error_context, entry->IsDir());
837 }
838
839 // GetSize() must return the right result in all cases after all the
840 // data has been read
841 CPPUNIT_ASSERT_MESSAGE("entry size check" + error_context,
842 testEntry.GetLength() == entry->GetSize());
843 CPPUNIT_ASSERT_MESSAGE(
46263455
VZ
844 "arc->GetLength() == entry->GetSize()" + error_context,
845 arc->GetLength() == entry->GetSize());
00375592
VZ
846
847 if ((m_options & PipeIn) == 0) {
848 OnEntryExtracted(*entry, testEntry, arc.get());
849 delete it->second;
850 m_testEntries.erase(it);
851 } else {
852 entries.push_back(entry);
853 }
854 }
855
856 // check that the end of the input archive was reached without error
857 CPPUNIT_ASSERT(arc->Eof());
858
859 // for non-seekable streams these data are only guaranteed to be
860 // available once the end of the archive has been reached
861 if (m_options & PipeIn) {
862 for (EntryIter i = entries.begin(); i != entries.end(); ++i) {
863 wxString name = (*i)->GetName(wxPATH_UNIX);
864 TestEntries::iterator j = m_testEntries.find(name);
865 OnEntryExtracted(**i, *j->second);
866 delete j->second;
867 m_testEntries.erase(j);
868 }
869 OnArchiveExtracted(*arc, expectedTotal);
870 }
871}
872
873// Extract an archive using an external unarchive program
874//
e6477b92
MW
875template <class ClassFactoryT>
876void ArchiveTestCase<ClassFactoryT>::ExtractArchive(wxInputStream& in,
877 const wxString& unarchiver)
00375592
VZ
878{
879 // for an external unarchiver, unarchive to a tempdir
880 TempDir tmpdir;
881
882 if ((m_options & PipeIn) == 0) {
883 wxFileName fn(tmpdir.GetName());
9a83f860 884 fn.SetExt(wxT("arc"));
e6477b92 885 wxString tmparc = fn.GetPath(wxPATH_GET_SEPARATOR) + fn.GetFullName();
a09cd189 886
00375592 887 if (m_options & Stub)
e6477b92 888 in.SeekI(STUB_SIZE * 2);
00375592
VZ
889
890 // write the archive to a temporary file
891 {
892 wxFFileOutputStream out(tmparc);
a1b806b9 893 if (out.IsOk())
00375592
VZ
894 out.Write(in);
895 }
896
897 // call unarchiver
898 system(wxString::Format(unarchiver, tmparc.c_str()).mb_str());
899 wxRemoveFile(tmparc);
900 }
901 else {
902 // for the non-seekable test, have the archiver extract "-" and
903 // feed it the archive via a pipe
9a83f860 904 PFileOutputStream out(wxString::Format(unarchiver, wxT("-")));
a1b806b9 905 if (out.IsOk())
00375592
VZ
906 out.Write(in);
907 }
908
909 wxString dir = tmpdir.GetName();
910 VerifyDir(dir);
911}
912
913// Verifies the files produced by an external unarchiver are as expected
914//
e6477b92
MW
915template <class ClassFactoryT>
916void ArchiveTestCase<ClassFactoryT>::VerifyDir(wxString& path,
917 size_t rootlen /*=0*/)
00375592
VZ
918{
919 wxDir dir;
920 path += wxFileName::GetPathSeparator();
921 int pos = path.length();
922 wxString name;
923
924 if (!rootlen)
925 rootlen = pos;
926
927 if (dir.Open(path) && dir.GetFirst(&name)) {
928 do {
929 path.replace(pos, wxString::npos, name);
930 name = m_factory->GetInternalName(
931 path.substr(rootlen, wxString::npos));
932
da865fdd 933 bool isDir = wxDirExists(path);
00375592 934 if (isDir)
9a83f860 935 name += wxT("/");
00375592
VZ
936
937 // provide some context for the error message so that we know which
938 // iteration of the loop we were on
9a83f860 939 string error_entry((wxT(" '") + name + wxT("'")).mb_str());
95662a83 940 string error_context(" failed for entry" + error_entry);
00375592
VZ
941
942 TestEntries::iterator it = m_testEntries.find(name);
943 CPPUNIT_ASSERT_MESSAGE(
944 "archive contains an entry that shouldn't be there"
945 + error_entry,
946 it != m_testEntries.end());
947
948 const TestEntry& testEntry = *it->second;
00375592 949
bb5a9514 950#if 0 //ndef __WINDOWS__
00375592
VZ
951 CPPUNIT_ASSERT_MESSAGE("timestamp check" + error_context,
952 testEntry.GetDateTime() ==
953 wxFileName(path).GetModificationTime());
954#endif
955 if (!isDir) {
956 wxFFileInputStream in(path);
957 CPPUNIT_ASSERT_MESSAGE(
a1b806b9 958 "entry not found in archive" + error_entry, in.IsOk());
00375592 959
f44eaed6 960 size_t size = (size_t)in.GetLength();
00375592
VZ
961 wxCharBuffer buf(size);
962 CPPUNIT_ASSERT_MESSAGE("Read" + error_context,
963 in.Read(buf.data(), size).LastRead() == size);
964 CPPUNIT_ASSERT_MESSAGE("size check" + error_context,
965 testEntry.GetSize() == size);
966 CPPUNIT_ASSERT_MESSAGE("data compare" + error_context,
967 memcmp(buf.data(), testEntry.GetData(), size) == 0);
968 }
969 else {
970 VerifyDir(path, rootlen);
971 }
972
973 delete it->second;
974 m_testEntries.erase(it);
975 }
976 while (dir.GetNext(&name));
977 }
978}
979
980// test the simple iterators that give away ownership of an entry
981//
e6477b92
MW
982template <class ClassFactoryT>
983void ArchiveTestCase<ClassFactoryT>::TestIterator(wxInputStream& in)
00375592
VZ
984{
985 typedef std::list<EntryT*> ArchiveCatalog;
986 typedef typename ArchiveCatalog::iterator CatalogIter;
987
95662a83 988 auto_ptr<InputStreamT> arc(m_factory->NewStream(in));
00375592
VZ
989 size_t count = 0;
990
991#ifdef WXARC_MEMBER_TEMPLATES
992 ArchiveCatalog cat((IterT)*arc, IterT());
993#else
994 ArchiveCatalog cat;
995 for (IterT i(*arc); i != IterT(); ++i)
996 cat.push_back(*i);
997#endif
998
999 for (CatalogIter it = cat.begin(); it != cat.end(); ++it) {
95662a83 1000 auto_ptr<EntryT> entry(*it);
00375592
VZ
1001 count += m_testEntries.count(entry->GetName(wxPATH_UNIX));
1002 }
1003
1004 CPPUNIT_ASSERT(m_testEntries.size() == cat.size());
1005 CPPUNIT_ASSERT(count == cat.size());
1006}
1007
1008// test the pair iterators that can be used to load a std::map or wxHashMap
1009// these also give away ownership of entries
1010//
e6477b92
MW
1011template <class ClassFactoryT>
1012void ArchiveTestCase<ClassFactoryT>::TestPairIterator(wxInputStream& in)
00375592
VZ
1013{
1014 typedef std::map<wxString, EntryT*> ArchiveCatalog;
1015 typedef typename ArchiveCatalog::iterator CatalogIter;
1016
95662a83 1017 auto_ptr<InputStreamT> arc(m_factory->NewStream(in));
00375592
VZ
1018 size_t count = 0;
1019
1020#ifdef WXARC_MEMBER_TEMPLATES
1021 ArchiveCatalog cat((PairIterT)*arc, PairIterT());
1022#else
1023 ArchiveCatalog cat;
1024 for (PairIterT i(*arc); i != PairIterT(); ++i)
95662a83 1025 cat.insert(*i);
00375592
VZ
1026#endif
1027
1028 for (CatalogIter it = cat.begin(); it != cat.end(); ++it) {
95662a83 1029 auto_ptr<EntryT> entry(it->second);
00375592
VZ
1030 count += m_testEntries.count(entry->GetName(wxPATH_UNIX));
1031 }
1032
1033 CPPUNIT_ASSERT(m_testEntries.size() == cat.size());
1034 CPPUNIT_ASSERT(count == cat.size());
1035}
1036
1037// simple iterators using smart pointers, no need to worry about ownership
1038//
e6477b92
MW
1039template <class ClassFactoryT>
1040void ArchiveTestCase<ClassFactoryT>::TestSmartIterator(wxInputStream& in)
00375592
VZ
1041{
1042 typedef std::list<Ptr<EntryT> > ArchiveCatalog;
1043 typedef typename ArchiveCatalog::iterator CatalogIter;
1044 typedef wxArchiveIterator<InputStreamT, Ptr<EntryT> > Iter;
1045
95662a83 1046 auto_ptr<InputStreamT> arc(m_factory->NewStream(in));
00375592
VZ
1047
1048#ifdef WXARC_MEMBER_TEMPLATES
1049 ArchiveCatalog cat((Iter)*arc, Iter());
1050#else
1051 ArchiveCatalog cat;
1052 for (Iter i(*arc); i != Iter(); ++i)
1053 cat.push_back(*i);
1054#endif
1055
1056 CPPUNIT_ASSERT(m_testEntries.size() == cat.size());
1057
1058 for (CatalogIter it = cat.begin(); it != cat.end(); ++it)
1059 CPPUNIT_ASSERT(m_testEntries.count((*it)->GetName(wxPATH_UNIX)));
1060}
1061
1062// pair iterator using smart pointers
1063//
e6477b92
MW
1064template <class ClassFactoryT>
1065void ArchiveTestCase<ClassFactoryT>::TestSmartPairIterator(wxInputStream& in)
00375592 1066{
f44eaed6
RN
1067#if defined _MSC_VER && defined _MSC_VER < 1200
1068 // With VC++ 5.0 the '=' operator of std::pair breaks when the second
1069 // type is Ptr<EntryT>, so this iterator can't be made to work.
1070 (void)in;
1071#else
00375592
VZ
1072 typedef std::map<wxString, Ptr<EntryT> > ArchiveCatalog;
1073 typedef typename ArchiveCatalog::iterator CatalogIter;
1074 typedef wxArchiveIterator<InputStreamT,
1075 std::pair<wxString, Ptr<EntryT> > > PairIter;
1076
95662a83 1077 auto_ptr<InputStreamT> arc(m_factory->NewStream(in));
00375592
VZ
1078
1079#ifdef WXARC_MEMBER_TEMPLATES
1080 ArchiveCatalog cat((PairIter)*arc, PairIter());
1081#else
1082 ArchiveCatalog cat;
1083 for (PairIter i(*arc); i != PairIter(); ++i)
95662a83 1084 cat.insert(*i);
00375592
VZ
1085#endif
1086
1087 CPPUNIT_ASSERT(m_testEntries.size() == cat.size());
1088
1089 for (CatalogIter it = cat.begin(); it != cat.end(); ++it)
1090 CPPUNIT_ASSERT(m_testEntries.count(it->second->GetName(wxPATH_UNIX)));
f44eaed6 1091#endif
00375592
VZ
1092}
1093
1094// try reading two entries at the same time
1095//
e6477b92
MW
1096template <class ClassFactoryT>
1097void ArchiveTestCase<ClassFactoryT>::ReadSimultaneous(TestInputStream& in)
00375592
VZ
1098{
1099 typedef std::map<wxString, Ptr<EntryT> > ArchiveCatalog;
1100 typedef wxArchiveIterator<InputStreamT,
1101 std::pair<wxString, Ptr<EntryT> > > PairIter;
1102
1103 // create two archive input streams
1104 TestInputStream in2(in);
95662a83
RN
1105 auto_ptr<InputStreamT> arc(m_factory->NewStream(in));
1106 auto_ptr<InputStreamT> arc2(m_factory->NewStream(in2));
00375592
VZ
1107
1108 // load the catalog
1109#ifdef WXARC_MEMBER_TEMPLATES
1110 ArchiveCatalog cat((PairIter)*arc, PairIter());
1111#else
1112 ArchiveCatalog cat;
1113 for (PairIter i(*arc); i != PairIter(); ++i)
95662a83 1114 cat.insert(*i);
00375592
VZ
1115#endif
1116
1117 // the names of two entries to read
9a83f860
VZ
1118 const wxChar *name = wxT("text/small");
1119 const wxChar *name2 = wxT("bin/bin1000");
00375592
VZ
1120
1121 // open them
1122 typename ArchiveCatalog::iterator j;
1123 CPPUNIT_ASSERT((j = cat.find(name)) != cat.end());
1124 CPPUNIT_ASSERT(arc->OpenEntry(*j->second));
1125 CPPUNIT_ASSERT((j = cat.find(name2)) != cat.end());
1126 CPPUNIT_ASSERT(arc2->OpenEntry(*j->second));
1127
1128 // get pointers to the expected data
1129 TestEntries::iterator k;
1130 CPPUNIT_ASSERT((k = m_testEntries.find(name)) != m_testEntries.end());
1131 TestEntry *entry = k->second;
1132 CPPUNIT_ASSERT((k = m_testEntries.find(name2)) != m_testEntries.end());
1133 TestEntry *entry2 = k->second;
1134
1135 size_t count = 0, count2 = 0;
1136 size_t size = entry->GetSize(), size2 = entry2->GetSize();
1137 const char *data = entry->GetData(), *data2 = entry2->GetData();
1138
1139 // read and check the two entries in parallel, character by character
1140 while (arc->IsOk() || arc2->IsOk()) {
1141 char ch = arc->GetC();
1142 if (arc->LastRead() == 1) {
1143 CPPUNIT_ASSERT(count < size);
1144 CPPUNIT_ASSERT(ch == data[count++]);
1145 }
1146 char ch2 = arc2->GetC();
1147 if (arc2->LastRead() == 1) {
1148 CPPUNIT_ASSERT(count2 < size2);
1149 CPPUNIT_ASSERT(ch2 == data2[count2++]);
1150 }
1151 }
1152
1153 CPPUNIT_ASSERT(arc->Eof());
1154 CPPUNIT_ASSERT(arc2->Eof());
1155 CPPUNIT_ASSERT(count == size);
1156 CPPUNIT_ASSERT(count2 == size2);
1157}
1158
1159// Nothing useful can be done with a generic notifier yet, so just test one
1160// can be set
1161//
1162template <class NotifierT, class EntryT>
1163class ArchiveNotifier : public NotifierT
1164{
1165public:
1166 void OnEntryUpdated(EntryT& WXUNUSED(entry)) { }
1167};
1168
e6477b92
MW
1169template <class ClassFactoryT>
1170void ArchiveTestCase<ClassFactoryT>::OnSetNotifier(EntryT& entry)
00375592
VZ
1171{
1172 static ArchiveNotifier<NotifierT, EntryT> notifier;
1173 entry.SetNotifier(notifier);
1174}
1175
1176
98de4ac1
MW
1177///////////////////////////////////////////////////////////////////////////////
1178// An additional case to check that reading corrupt archives doesn't crash
1179
1180class CorruptionTestCase : public CppUnit::TestCase
1181{
1182public:
1183 CorruptionTestCase(std::string name,
1184 wxArchiveClassFactory *factory,
1185 int options)
1186 : CppUnit::TestCase(TestId::MakeId() + name),
1187 m_factory(factory),
1188 m_options(options)
1189 { }
1190
1191protected:
1192 // the entry point for the test
1193 void runTest();
1194
1195 void CreateArchive(wxOutputStream& out);
1196 void ExtractArchive(wxInputStream& in);
1197
1198 auto_ptr<wxArchiveClassFactory> m_factory; // factory to make classes
1199 int m_options; // test options
1200};
1201
1202void CorruptionTestCase::runTest()
1203{
1204 TestOutputStream out(m_options);
1205 CreateArchive(out);
1206 TestInputStream in(out, 0);
1207 wxFileOffset len = in.GetLength();
1208
1209 // try flipping one byte in the archive
5098c258
VZ
1210 int pos;
1211 for (pos = 0; pos < len; pos++) {
98de4ac1
MW
1212 char n = in[pos];
1213 in[pos] = ~n;
1214 ExtractArchive(in);
1215 in.Rewind();
1216 in[pos] = n;
1217 }
1218
1219 // try zeroing one byte in the archive
5098c258 1220 for (pos = 0; pos < len; pos++) {
98de4ac1
MW
1221 char n = in[pos];
1222 in[pos] = 0;
1223 ExtractArchive(in);
1224 in.Rewind();
1225 in[pos] = n;
1226 }
1227
1228 // try chopping the archive off
1229 for (int size = 1; size <= len; size++) {
1230 in.Chop(size);
1231 ExtractArchive(in);
1232 in.Rewind();
1233 }
1234}
1235
1236void CorruptionTestCase::CreateArchive(wxOutputStream& out)
1237{
1238 auto_ptr<wxArchiveOutputStream> arc(m_factory->NewStream(out));
1239
9a83f860
VZ
1240 arc->PutNextDirEntry(wxT("dir"));
1241 arc->PutNextEntry(wxT("file"));
1242 arc->Write(wxT("foo"), 3);
98de4ac1
MW
1243}
1244
1245void CorruptionTestCase::ExtractArchive(wxInputStream& in)
1246{
1247 auto_ptr<wxArchiveInputStream> arc(m_factory->NewStream(in));
1248 auto_ptr<wxArchiveEntry> entry(arc->GetNextEntry());
93a800a9 1249
98de4ac1 1250 while (entry.get() != NULL) {
98de4ac1 1251 char buf[1024];
93a800a9 1252
98de4ac1
MW
1253 while (arc->IsOk())
1254 arc->Read(buf, sizeof(buf));
1255
ed626c8b
VZ
1256 auto_ptr<wxArchiveEntry> next(arc->GetNextEntry());
1257 entry = next;
98de4ac1
MW
1258 }
1259}
1260
1261
716e748b
MW
1262///////////////////////////////////////////////////////////////////////////////
1263// Make the ids
1264
1265int TestId::m_seed = 6219;
1266
1267// static
1268string TestId::MakeId()
1269{
1270 m_seed = (m_seed * 171) % 30269;
9a83f860 1271 return string(wxString::Format(wxT("%-6d"), m_seed).mb_str());
716e748b
MW
1272}
1273
1274
00375592 1275///////////////////////////////////////////////////////////////////////////////
e6477b92 1276// Suite base
00375592 1277
e6477b92
MW
1278ArchiveTestSuite::ArchiveTestSuite(string name)
1279 : CppUnit::TestSuite("archive/" + name),
e6477b92 1280 m_name(name.c_str(), *wxConvCurrent)
00375592 1281{
9a83f860
VZ
1282 m_name = wxT("wx") + m_name.Left(1).Upper() + m_name.Mid(1).Lower();
1283 m_path.AddEnvList(wxT("PATH"));
1284 m_archivers.push_back(wxT(""));
1285 m_unarchivers.push_back(wxT(""));
00375592
VZ
1286}
1287
1288// add the command for an external archiver to the list, testing for it in
1289// the path first
1290//
1291void ArchiveTestSuite::AddCmd(wxArrayString& cmdlist, const wxString& cmd)
1292{
00375592
VZ
1293 if (IsInPath(cmd))
1294 cmdlist.push_back(cmd);
1295}
1296
1297bool ArchiveTestSuite::IsInPath(const wxString& cmd)
1298{
9a83f860 1299 wxString c = cmd.BeforeFirst(wxT(' '));
bb5a9514 1300#ifdef __WINDOWS__
9a83f860 1301 c += wxT(".exe");
00375592
VZ
1302#endif
1303 return !m_path.FindValidPath(c).empty();
1304}
1305
1306// make the test suite
1307//
1308ArchiveTestSuite *ArchiveTestSuite::makeSuite()
1309{
1310 typedef wxArrayString::iterator Iter;
00375592 1311
e6477b92
MW
1312 for (int generic = 0; generic < 2; generic++)
1313 for (Iter i = m_unarchivers.begin(); i != m_unarchivers.end(); ++i)
1314 for (Iter j = m_archivers.begin(); j != m_archivers.end(); ++j)
00375592
VZ
1315 for (int options = 0; options <= AllOptions; options++)
1316 {
e6477b92
MW
1317#ifdef WXARC_NO_POPEN
1318 // if no popen then can't pipe in/out of archiver
00375592
VZ
1319 if ((options & PipeIn) && !i->empty())
1320 continue;
00375592
VZ
1321 if ((options & PipeOut) && !j->empty())
1322 continue;
1323#endif
e6477b92
MW
1324 string descr = Description(m_name, options,
1325 generic != 0, *j, *i);
00375592 1326
716e748b 1327 CppUnit::Test *test = makeTest(descr, options,
e6477b92
MW
1328 generic != 0, *j, *i);
1329
716e748b 1330 if (test)
e6477b92 1331 addTest(test);
e6477b92 1332 }
00375592 1333
93a800a9 1334 for (int options = 0; options <= PipeIn; options += PipeIn)
98de4ac1 1335 {
9a83f860 1336 wxObject *pObj = wxCreateDynamicObject(m_name + wxT("ClassFactory"));
98de4ac1
MW
1337 wxArchiveClassFactory *factory;
1338 factory = wxDynamicCast(pObj, wxArchiveClassFactory);
1339
1340 if (factory) {
1341 string descr(m_name.mb_str());
1342 descr = "CorruptionTestCase (" + descr + ")";
1343
1344 if (options)
1345 descr += " (PipeIn)";
1346
1347 addTest(new CorruptionTestCase(descr, factory, options));
1348 }
1349 }
1350
00375592
VZ
1351 return this;
1352}
1353
e6477b92
MW
1354CppUnit::Test *ArchiveTestSuite::makeTest(
1355 string WXUNUSED(descr),
e6477b92
MW
1356 int WXUNUSED(options),
1357 bool WXUNUSED(genericInterface),
1358 const wxString& WXUNUSED(archiver),
1359 const wxString& WXUNUSED(unarchiver))
1360{
1361 return NULL;
1362}
1363
00375592
VZ
1364// make a display string for the option bits
1365//
95662a83
RN
1366string ArchiveTestSuite::Description(const wxString& type,
1367 int options,
1368 bool genericInterface,
1369 const wxString& archiver,
1370 const wxString& unarchiver)
00375592
VZ
1371{
1372 wxString descr;
a09cd189 1373
00375592 1374 if (genericInterface)
9a83f860 1375 descr << wxT("wxArchive (") << type << wxT(")");
00375592
VZ
1376 else
1377 descr << type;
1378
e6477b92 1379 if (!archiver.empty()) {
9a83f860 1380 const wxChar *fn = (options & PipeOut) != 0 ? wxT("-") : wxT("file");
72e7920f
VS
1381 const wxString cmd = archiver.Contains("%s")
1382 ? wxString::Format(archiver, fn)
1383 : archiver;
1384 descr << wxT(" (") << cmd << wxT(")");
e6477b92
MW
1385 }
1386 if (!unarchiver.empty()) {
9a83f860 1387 const wxChar *fn = (options & PipeIn) != 0 ? wxT("-") : wxT("file");
72e7920f
VS
1388 const wxString cmd = unarchiver.Contains("%s")
1389 ? wxString::Format(unarchiver, fn)
1390 : unarchiver;
1391 descr << wxT(" (") << cmd << wxT(")");
e6477b92 1392 }
a09cd189 1393
00375592
VZ
1394 wxString optstr;
1395
1396 if ((options & PipeIn) != 0)
9a83f860 1397 optstr += wxT("|PipeIn");
00375592 1398 if ((options & PipeOut) != 0)
9a83f860 1399 optstr += wxT("|PipeOut");
00375592 1400 if ((options & Stub) != 0)
9a83f860 1401 optstr += wxT("|Stub");
00375592 1402 if (!optstr.empty())
9a83f860 1403 optstr = wxT(" (") + optstr.substr(1) + wxT(")");
00375592
VZ
1404
1405 descr << optstr;
1406
98de4ac1 1407 return string(descr.mb_str());
00375592
VZ
1408}
1409
00375592 1410
e6477b92
MW
1411///////////////////////////////////////////////////////////////////////////////
1412// Instantiations
1413
1414template class ArchiveTestCase<wxArchiveClassFactory>;
1415
1416#if wxUSE_ZIPSTREAM
1417#include "wx/zipstrm.h"
1418template class ArchiveTestCase<wxZipClassFactory>;
1419#endif
00375592 1420
cdd7933f
MW
1421#if wxUSE_TARSTREAM
1422#include "wx/tarstrm.h"
1423template class ArchiveTestCase<wxTarClassFactory>;
1424#endif
1425
9e8e867f 1426#endif // wxUSE_STREAMS && wxUSE_ARCHIVE_STREAMS