]>
git.saurik.com Git - wxWidgets.git/blob - tests/archive/archivetest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/archive/archive.cpp
3 // Purpose: Test the archive classes
4 // Author: Mike Wetherell
6 // Copyright: (c) 2004 Mike Wetherell
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
20 #if wxUSE_STREAMS && wxUSE_ARCHIVE_STREAMS
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
25 #pragma warning (disable:4284)
28 #include "archivetest.h"
39 // Check whether member templates can be used
41 #if defined __GNUC__ && \
42 (__GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95))
43 # define WXARC_MEMBER_TEMPLATES
45 #if defined _MSC_VER && _MSC_VER >= 1310 && !defined __WIN64__
46 # define WXARC_MEMBER_TEMPLATES
48 #if defined __BORLANDC__ && __BORLANDC__ >= 0x530
49 # define WXARC_MEMBER_TEMPLATES
51 #if defined __DMC__ && __DMC__ >= 0x832
52 # define WXARC_MEMBER_TEMPLATES
54 #if defined __MWERKS__ && __MWERKS__ >= 0x2200
55 # define WXARC_MEMBER_TEMPLATES
57 #if defined __HP_aCC && __HP_aCC > 33300
58 # define WXARC_MEMBER_TEMPLATES
60 #if defined __SUNPRO_CC && __SUNPRO_CC > 0x500
61 # define WXARC_MEMBER_TEMPLATES
65 ///////////////////////////////////////////////////////////////////////////////
66 // A class to hold a test entry
68 TestEntry::TestEntry(const wxDateTime
& dt
, int len
, const char *data
)
73 m_data
= new char[len
];
74 memcpy(m_data
, data
, len
);
76 for (int i
= 0; i
< len
&& m_isText
; i
++)
77 m_isText
= (signed char)m_data
[i
] > 0;
81 ///////////////////////////////////////////////////////////////////////////////
82 // TestOutputStream and TestInputStream are memory streams which can be
83 // seekable or non-seekable.
85 const size_t STUB_SIZE
= 2048;
86 const size_t INITIAL_SIZE
= 0x18000;
87 const wxFileOffset SEEK_LIMIT
= 0x100000;
89 TestOutputStream::TestOutputStream(int options
)
95 void TestOutputStream::Init()
102 if (m_options
& Stub
) {
103 wxCharBuffer
buf(STUB_SIZE
);
104 memset(buf
.data(), 0, STUB_SIZE
);
105 Write(buf
, STUB_SIZE
);
109 wxFileOffset
TestOutputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
111 if ((m_options
& PipeOut
) == 0) {
113 case wxFromStart
: break;
114 case wxFromCurrent
: pos
+= m_pos
; break;
115 case wxFromEnd
: pos
+= m_size
; break;
117 if (pos
< 0 || pos
> SEEK_LIMIT
)
118 return wxInvalidOffset
;
122 return wxInvalidOffset
;
125 wxFileOffset
TestOutputStream::OnSysTell() const
127 return (m_options
& PipeOut
) == 0 ? (wxFileOffset
)m_pos
: wxInvalidOffset
;
130 size_t TestOutputStream::OnSysWrite(const void *buffer
, size_t size
)
132 if (!IsOk() || !size
)
134 m_lasterror
= wxSTREAM_WRITE_ERROR
;
136 size_t newsize
= m_pos
+ size
;
137 wxCHECK(newsize
> m_pos
, 0);
139 if (m_capacity
< newsize
) {
140 size_t capacity
= m_capacity
? m_capacity
: INITIAL_SIZE
;
142 while (capacity
< newsize
) {
144 wxCHECK(capacity
> m_capacity
, 0);
147 char *buf
= new char[capacity
];
149 memcpy(buf
, m_data
, m_capacity
);
152 m_capacity
= capacity
;
155 memcpy(m_data
+ m_pos
, buffer
, size
);
159 m_lasterror
= wxSTREAM_NO_ERROR
;
164 void TestOutputStream::GetData(char*& data
, size_t& size
)
169 if (m_options
& Stub
) {
173 if (size
> m_capacity
) {
175 memcpy(d
+ STUB_SIZE
, m_data
, m_size
);
179 memmove(d
+ STUB_SIZE
, d
, m_size
);
182 memset(d
, 0, STUB_SIZE
);
191 ///////////////////////////////////////////////////////////////////////////////
192 // TestOutputStream and TestInputStream are memory streams which can be
193 // seekable or non-seekable.
195 TestInputStream::TestInputStream(const TestInputStream
& in
)
197 m_options(in
.m_options
),
200 m_eoftype(in
.m_eoftype
)
202 m_data
= new char[m_size
];
203 memcpy(m_data
, in
.m_data
, m_size
);
206 void TestInputStream::Rewind()
208 if ((m_options
& Stub
) && (m_options
& PipeIn
))
209 m_pos
= STUB_SIZE
* 2;
221 void TestInputStream::SetData(TestOutputStream
& out
)
224 m_options
= out
.GetOptions();
225 out
.GetData(m_data
, m_size
);
230 wxFileOffset
TestInputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
232 if ((m_options
& PipeIn
) == 0) {
234 case wxFromStart
: break;
235 case wxFromCurrent
: pos
+= m_pos
; break;
236 case wxFromEnd
: pos
+= m_size
; break;
238 if (pos
< 0 || pos
> SEEK_LIMIT
)
239 return wxInvalidOffset
;
243 return wxInvalidOffset
;
246 wxFileOffset
TestInputStream::OnSysTell() const
248 return (m_options
& PipeIn
) == 0 ? (wxFileOffset
)m_pos
: wxInvalidOffset
;
251 size_t TestInputStream::OnSysRead(void *buffer
, size_t size
)
253 if (!IsOk() || !size
)
260 else if (m_size
- m_pos
< size
)
261 count
= m_size
- m_pos
;
266 memcpy(buffer
, m_data
+ m_pos
, count
);
270 if (((m_eoftype
& AtLast
) != 0 && m_pos
>= m_size
) || count
< size
)
271 if ((m_eoftype
& WithError
) != 0)
272 m_lasterror
= wxSTREAM_READ_ERROR
;
274 m_lasterror
= wxSTREAM_EOF
;
280 ///////////////////////////////////////////////////////////////////////////////
281 // minimal non-intrusive reference counting pointer for testing the iterators
283 template <class T
> class Ptr
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
; }
290 Ptr
& operator =(const Ptr
& sp
) {
294 m_count
= sp
.m_count
;
300 T
* get() const { return m_p
; }
301 T
* operator->() const { return m_p
; }
302 T
& operator*() const { return *m_p
; }
306 if (--*m_count
== 0) {
317 ///////////////////////////////////////////////////////////////////////////////
318 // Clean-up for temp directory
325 wxString
GetName() const { return m_tmp
; }
328 void RemoveDir(wxString
& path
);
335 wxString tmp
= wxFileName::CreateTempFileName(_T("arctest-"));
338 m_original
= wxGetCwd();
339 CPPUNIT_ASSERT(wxMkdir(tmp
, 0700));
341 CPPUNIT_ASSERT(wxSetWorkingDirectory(tmp
));
347 if (!m_tmp
.empty()) {
348 wxSetWorkingDirectory(m_original
);
353 void TempDir::RemoveDir(wxString
& path
)
355 wxCHECK_RET(!m_tmp
.empty() && path
.substr(0, m_tmp
.length()) == m_tmp
,
356 _T("remove '") + path
+ _T("' fails safety check"));
358 const wxChar
*files
[] = {
368 _T("zero/zero32768"),
369 _T("zero/zero16385"),
374 const wxChar
*dirs
[] = {
375 _T("text/"), _T("bin/"), _T("zero/"), _T("empty/")
378 wxString tmp
= m_tmp
+ wxFileName::GetPathSeparator();
381 for (i
= 0; i
< WXSIZEOF(files
); i
++)
382 wxRemoveFile(tmp
+ wxFileName(files
[i
], wxPATH_UNIX
).GetFullPath());
384 for (i
= 0; i
< WXSIZEOF(dirs
); i
++)
385 wxRmdir(tmp
+ wxFileName(dirs
[i
], wxPATH_UNIX
).GetFullPath());
388 wxLogSysError(_T("can't remove temporary dir '%s'"), m_tmp
.c_str());
392 ///////////////////////////////////////////////////////////////////////////////
393 // wxFFile streams for piping to/from an external program
395 #if defined __UNIX__ || defined __MINGW32__
396 # define WXARC_popen popen
397 # define WXARC_pclose pclose
398 #elif defined _MSC_VER || defined __BORLANDC__
399 # define WXARC_popen _popen
400 # define WXARC_pclose _pclose
402 # define WXARC_NO_POPEN
403 # define WXARC_popen(cmd, type) NULL
404 # define WXARC_pclose(fp)
413 PFileInputStream::PFileInputStream(const wxString
& cmd
)
414 : wxFFileInputStream(WXARC_popen(cmd
.mb_str(), "r" WXARC_b
))
418 PFileInputStream::~PFileInputStream()
420 WXARC_pclose(m_file
->fp()); m_file
->Detach();
423 PFileOutputStream::PFileOutputStream(const wxString
& cmd
)
424 : wxFFileOutputStream(WXARC_popen(cmd
.mb_str(), "w" WXARC_b
))
428 PFileOutputStream::~PFileOutputStream()
430 WXARC_pclose(m_file
->fp()); m_file
->Detach();
434 ///////////////////////////////////////////////////////////////////////////////
437 template <class ClassFactoryT
>
438 ArchiveTestCase
<ClassFactoryT
>::ArchiveTestCase(
440 ClassFactoryT
*factory
,
442 const wxString
& archiver
,
443 const wxString
& unarchiver
)
445 CppUnit::TestCase(TestId::MakeId() + name
),
448 m_timeStamp(1, wxDateTime::Mar
, 2004, 12, 0),
449 m_id(TestId::GetId()),
450 m_archiver(archiver
),
451 m_unarchiver(unarchiver
)
453 wxASSERT(m_factory
.get() != NULL
);
456 template <class ClassFactoryT
>
457 ArchiveTestCase
<ClassFactoryT
>::~ArchiveTestCase()
459 TestEntries::iterator it
;
460 for (it
= m_testEntries
.begin(); it
!= m_testEntries
.end(); ++it
)
464 template <class ClassFactoryT
>
465 void ArchiveTestCase
<ClassFactoryT
>::runTest()
467 TestOutputStream
out(m_options
);
471 if (m_archiver
.empty())
474 CreateArchive(out
, m_archiver
);
476 // check archive could be created
477 CPPUNIT_ASSERT(out
.GetLength() > 0);
479 TestInputStream
in(out
, m_id
% ((m_options
& PipeIn
) ? 4 : 3));
483 TestPairIterator(in
);
485 TestSmartIterator(in
);
487 TestSmartPairIterator(in
);
490 if ((m_options
& PipeIn
) == 0) {
491 ReadSimultaneous(in
);
495 ModifyArchive(in
, out
);
498 if (m_unarchiver
.empty())
501 ExtractArchive(in
, m_unarchiver
);
503 // check that all the test entries were found in the archive
504 CPPUNIT_ASSERT(m_testEntries
.empty());
507 template <class ClassFactoryT
>
508 void ArchiveTestCase
<ClassFactoryT
>::CreateTestData()
511 Add("text/empty", "");
512 Add("text/small", "Small text file for testing\n"
513 "archive streams in wxWidgets\n");
516 Add("bin/bin1000", 1000);
517 Add("bin/bin4095", 4095);
518 Add("bin/bin4096", 4096);
519 Add("bin/bin4097", 4097);
520 Add("bin/bin16384", 16384);
523 Add("zero/zero5", 5, 0);
524 Add("zero/zero1024", 1024, 109);
525 Add("zero/zero32768", 32768, 106);
526 Add("zero/zero16385", 16385, 119);
531 template <class ClassFactoryT
>
532 TestEntry
& ArchiveTestCase
<ClassFactoryT
>::Add(const char *name
,
538 TestEntry
*& entry
= m_testEntries
[wxString(name
, *wxConvCurrent
)];
539 wxASSERT(entry
== NULL
);
540 entry
= new TestEntry(m_timeStamp
, len
, data
);
541 m_timeStamp
+= wxTimeSpan(0, 1, 30);
545 template <class ClassFactoryT
>
546 TestEntry
& ArchiveTestCase
<ClassFactoryT
>::Add(const char *name
,
550 wxCharBuffer
buf(len
);
551 for (int i
= 0; i
< len
; i
++)
552 buf
.data()[i
] = (char)(value
== EOF
? rand() : value
);
553 return Add(name
, buf
, len
);
556 // Create an archive using the wx archive classes, write it to 'out'
558 template <class ClassFactoryT
>
559 void ArchiveTestCase
<ClassFactoryT
>::CreateArchive(wxOutputStream
& out
)
561 auto_ptr
<OutputStreamT
> arc(m_factory
->NewStream(out
));
562 TestEntries::iterator it
;
564 OnCreateArchive(*arc
);
566 // We want to try creating entries in various different ways, 'choices'
567 // is just a number used to select between all the various possibilities.
570 for (it
= m_testEntries
.begin(); it
!= m_testEntries
.end(); ++it
) {
572 TestEntry
& testEntry
= *it
->second
;
573 wxString name
= it
->first
;
575 // It should be possible to create a directory entry just by supplying
576 // a name that looks like a directory, or alternatively any old name
577 // can be identified as a directory using SetIsDir or PutNextDirEntry
578 bool setIsDir
= name
.Last() == _T('/') && (choices
& 1);
580 name
.erase(name
.length() - 1);
582 // provide some context for the error message so that we know which
583 // iteration of the loop we were on
584 string
error_entry((_T(" '") + name
+ _T("'")).mb_str());
585 string
error_context(" failed for entry" + error_entry
);
587 if ((choices
& 2) || testEntry
.IsText()) {
588 // try PutNextEntry(EntryT *pEntry)
589 auto_ptr
<EntryT
> entry(m_factory
->NewEntry());
590 entry
->SetName(name
, wxPATH_UNIX
);
593 entry
->SetDateTime(testEntry
.GetDateTime());
594 entry
->SetSize(testEntry
.GetLength());
595 OnCreateEntry(*arc
, testEntry
, entry
.get());
596 CPPUNIT_ASSERT_MESSAGE("PutNextEntry" + error_context
,
597 arc
->PutNextEntry(entry
.release()));
600 // try the convenience methods
601 OnCreateEntry(*arc
, testEntry
);
603 CPPUNIT_ASSERT_MESSAGE("PutNextDirEntry" + error_context
,
604 arc
->PutNextDirEntry(name
, testEntry
.GetDateTime()));
606 CPPUNIT_ASSERT_MESSAGE("PutNextEntry" + error_context
,
607 arc
->PutNextEntry(name
, testEntry
.GetDateTime(),
608 testEntry
.GetLength()));
611 if (it
->first
.Last() != _T('/')) {
612 // for non-dirs write the data
613 arc
->Write(testEntry
.GetData(), testEntry
.GetSize());
614 CPPUNIT_ASSERT_MESSAGE("LastWrite check" + error_context
,
615 arc
->LastWrite() == testEntry
.GetSize());
616 // should work with or without explicit CloseEntry
618 CPPUNIT_ASSERT_MESSAGE("CloseEntry" + error_context
,
622 CPPUNIT_ASSERT_MESSAGE("IsOk" + error_context
, arc
->IsOk());
625 // should work with or without explicit Close
627 CPPUNIT_ASSERT(arc
->Close());
630 // Create an archive using an external archive program
632 template <class ClassFactoryT
>
633 void ArchiveTestCase
<ClassFactoryT
>::CreateArchive(wxOutputStream
& out
,
634 const wxString
& archiver
)
636 // for an external archiver the test data need to be written to
641 TestEntries::iterator i
;
642 for (i
= m_testEntries
.begin(); i
!= m_testEntries
.end(); ++i
) {
643 wxFileName
fn(i
->first
, wxPATH_UNIX
);
644 TestEntry
& entry
= *i
->second
;
647 fn
.Mkdir(0777, wxPATH_MKDIR_FULL
);
649 wxFileName::Mkdir(fn
.GetPath(), 0777, wxPATH_MKDIR_FULL
);
650 wxFFileOutputStream
fileout(fn
.GetFullPath());
651 fileout
.Write(entry
.GetData(), entry
.GetSize());
655 for (i
= m_testEntries
.begin(); i
!= m_testEntries
.end(); ++i
) {
656 wxFileName
fn(i
->first
, wxPATH_UNIX
);
657 TestEntry
& entry
= *i
->second
;
658 wxDateTime dt
= entry
.GetDateTime();
661 entry
.SetDateTime(wxDateTime());
664 fn
.SetTimes(NULL
, &dt
, NULL
);
667 if ((m_options
& PipeOut
) == 0) {
668 wxFileName
fn(tmpdir
.GetName());
669 fn
.SetExt(_T("arc"));
670 wxString tmparc
= fn
.GetPath(wxPATH_GET_SEPARATOR
) + fn
.GetFullName();
672 // call the archiver to create an archive file
673 system(wxString::Format(archiver
, tmparc
.c_str()).mb_str());
675 // then load the archive file
677 wxFFileInputStream
in(tmparc
);
682 wxRemoveFile(tmparc
);
685 // for the non-seekable test, have the archiver output to "-"
686 // and read the archive via a pipe
687 PFileInputStream
in(wxString::Format(archiver
, _T("-")));
693 // Do a standard set of modification on an archive, delete an entry,
694 // rename an entry and add an entry
696 template <class ClassFactoryT
>
697 void ArchiveTestCase
<ClassFactoryT
>::ModifyArchive(wxInputStream
& in
,
700 auto_ptr
<InputStreamT
> arcIn(m_factory
->NewStream(in
));
701 auto_ptr
<OutputStreamT
> arcOut(m_factory
->NewStream(out
));
704 const wxString deleteName
= _T("bin/bin1000");
705 const wxString renameFrom
= _T("zero/zero1024");
706 const wxString renameTo
= _T("zero/newname");
707 const wxString newName
= _T("newfile");
708 const char *newData
= "New file added as a test\n";
710 arcOut
->CopyArchiveMetaData(*arcIn
);
712 while ((pEntry
= arcIn
->GetNextEntry()) != NULL
) {
713 auto_ptr
<EntryT
> entry(pEntry
);
714 OnSetNotifier(*entry
);
715 wxString name
= entry
->GetName(wxPATH_UNIX
);
717 // provide some context for the error message so that we know which
718 // iteration of the loop we were on
719 string
error_entry((_T(" '") + name
+ _T("'")).mb_str());
720 string
error_context(" failed for entry" + error_entry
);
722 if (name
== deleteName
) {
723 TestEntries::iterator it
= m_testEntries
.find(name
);
724 CPPUNIT_ASSERT_MESSAGE(
725 "deletion failed (already deleted?) for" + error_entry
,
726 it
!= m_testEntries
.end());
727 TestEntry
*p
= it
->second
;
728 m_testEntries
.erase(it
);
732 if (name
== renameFrom
) {
733 entry
->SetName(renameTo
);
734 TestEntries::iterator it
= m_testEntries
.find(renameFrom
);
735 CPPUNIT_ASSERT_MESSAGE(
736 "rename failed (already renamed?) for" + error_entry
,
737 it
!= m_testEntries
.end());
738 TestEntry
*p
= it
->second
;
739 m_testEntries
.erase(it
);
740 m_testEntries
[renameTo
] = p
;
743 CPPUNIT_ASSERT_MESSAGE("CopyEntry" + error_context
,
744 arcOut
->CopyEntry(entry
.release(), *arcIn
));
748 // check that the deletion and rename were done
749 CPPUNIT_ASSERT(m_testEntries
.count(deleteName
) == 0);
750 CPPUNIT_ASSERT(m_testEntries
.count(renameFrom
) == 0);
751 CPPUNIT_ASSERT(m_testEntries
.count(renameTo
) == 1);
753 // check that the end of the input archive was reached without error
754 CPPUNIT_ASSERT(arcIn
->Eof());
756 // try adding a new entry
757 TestEntry
& testEntry
= Add(newName
.mb_str(), newData
);
758 auto_ptr
<EntryT
> newentry(m_factory
->NewEntry());
759 newentry
->SetName(newName
);
760 newentry
->SetDateTime(testEntry
.GetDateTime());
761 newentry
->SetSize(testEntry
.GetLength());
762 OnCreateEntry(*arcOut
, testEntry
, newentry
.get());
763 OnSetNotifier(*newentry
);
764 CPPUNIT_ASSERT(arcOut
->PutNextEntry(newentry
.release()));
765 CPPUNIT_ASSERT(arcOut
->Write(newData
, strlen(newData
)).IsOk());
767 // should work with or without explicit Close
769 CPPUNIT_ASSERT(arcOut
->Close());
772 // Extract an archive using the wx archive classes
774 template <class ClassFactoryT
>
775 void ArchiveTestCase
<ClassFactoryT
>::ExtractArchive(wxInputStream
& in
)
777 typedef Ptr
<EntryT
> EntryPtr
;
778 typedef std::list
<EntryPtr
> Entries
;
779 typedef typename
Entries::iterator EntryIter
;
781 auto_ptr
<InputStreamT
> arc(m_factory
->NewStream(in
));
782 int expectedTotal
= m_testEntries
.size();
786 if ((m_options
& PipeIn
) == 0)
787 OnArchiveExtracted(*arc
, expectedTotal
);
789 while (entry
= EntryPtr(arc
->GetNextEntry()), entry
.get() != NULL
) {
790 wxString name
= entry
->GetName(wxPATH_UNIX
);
792 // provide some context for the error message so that we know which
793 // iteration of the loop we were on
794 string
error_entry((_T(" '") + name
+ _T("'")).mb_str());
795 string
error_context(" failed for entry" + error_entry
);
797 TestEntries::iterator it
= m_testEntries
.find(name
);
798 CPPUNIT_ASSERT_MESSAGE(
799 "archive contains an entry that shouldn't be there" + error_entry
,
800 it
!= m_testEntries
.end());
802 const TestEntry
& testEntry
= *it
->second
;
804 wxDateTime dt
= testEntry
.GetDateTime();
806 CPPUNIT_ASSERT_MESSAGE("timestamp check" + error_context
,
807 dt
== entry
->GetDateTime());
809 // non-seekable entries are allowed to have GetSize == wxInvalidOffset
810 // until the end of the entry's data has been read past
811 CPPUNIT_ASSERT_MESSAGE("entry size check" + error_context
,
812 testEntry
.GetLength() == entry
->GetSize() ||
813 ((m_options
& PipeIn
) != 0 && entry
->GetSize() == wxInvalidOffset
));
814 CPPUNIT_ASSERT_MESSAGE(
815 "arc->GetLength() == entry->GetSize()" + error_context
,
816 arc
->GetLength() == entry
->GetSize());
818 if (name
.Last() != _T('/'))
820 CPPUNIT_ASSERT_MESSAGE("!IsDir" + error_context
,
822 wxCharBuffer
buf(testEntry
.GetSize() + 1);
823 CPPUNIT_ASSERT_MESSAGE("Read until Eof" + error_context
,
824 arc
->Read(buf
.data(), testEntry
.GetSize() + 1).Eof());
825 CPPUNIT_ASSERT_MESSAGE("LastRead check" + error_context
,
826 arc
->LastRead() == testEntry
.GetSize());
827 CPPUNIT_ASSERT_MESSAGE("data compare" + error_context
,
828 !memcmp(buf
.data(), testEntry
.GetData(), testEntry
.GetSize()));
830 CPPUNIT_ASSERT_MESSAGE("IsDir" + error_context
, entry
->IsDir());
833 // GetSize() must return the right result in all cases after all the
834 // data has been read
835 CPPUNIT_ASSERT_MESSAGE("entry size check" + error_context
,
836 testEntry
.GetLength() == entry
->GetSize());
837 CPPUNIT_ASSERT_MESSAGE(
838 "arc->GetLength() == entry->GetSize()" + error_context
,
839 arc
->GetLength() == entry
->GetSize());
841 if ((m_options
& PipeIn
) == 0) {
842 OnEntryExtracted(*entry
, testEntry
, arc
.get());
844 m_testEntries
.erase(it
);
846 entries
.push_back(entry
);
850 // check that the end of the input archive was reached without error
851 CPPUNIT_ASSERT(arc
->Eof());
853 // for non-seekable streams these data are only guaranteed to be
854 // available once the end of the archive has been reached
855 if (m_options
& PipeIn
) {
856 for (EntryIter i
= entries
.begin(); i
!= entries
.end(); ++i
) {
857 wxString name
= (*i
)->GetName(wxPATH_UNIX
);
858 TestEntries::iterator j
= m_testEntries
.find(name
);
859 OnEntryExtracted(**i
, *j
->second
);
861 m_testEntries
.erase(j
);
863 OnArchiveExtracted(*arc
, expectedTotal
);
867 // Extract an archive using an external unarchive program
869 template <class ClassFactoryT
>
870 void ArchiveTestCase
<ClassFactoryT
>::ExtractArchive(wxInputStream
& in
,
871 const wxString
& unarchiver
)
873 // for an external unarchiver, unarchive to a tempdir
876 if ((m_options
& PipeIn
) == 0) {
877 wxFileName
fn(tmpdir
.GetName());
878 fn
.SetExt(_T("arc"));
879 wxString tmparc
= fn
.GetPath(wxPATH_GET_SEPARATOR
) + fn
.GetFullName();
881 if (m_options
& Stub
)
882 in
.SeekI(STUB_SIZE
* 2);
884 // write the archive to a temporary file
886 wxFFileOutputStream
out(tmparc
);
892 system(wxString::Format(unarchiver
, tmparc
.c_str()).mb_str());
893 wxRemoveFile(tmparc
);
896 // for the non-seekable test, have the archiver extract "-" and
897 // feed it the archive via a pipe
898 PFileOutputStream
out(wxString::Format(unarchiver
, _T("-")));
903 wxString dir
= tmpdir
.GetName();
907 // Verifies the files produced by an external unarchiver are as expected
909 template <class ClassFactoryT
>
910 void ArchiveTestCase
<ClassFactoryT
>::VerifyDir(wxString
& path
,
911 size_t rootlen
/*=0*/)
914 path
+= wxFileName::GetPathSeparator();
915 int pos
= path
.length();
921 if (dir
.Open(path
) && dir
.GetFirst(&name
)) {
923 path
.replace(pos
, wxString::npos
, name
);
924 name
= m_factory
->GetInternalName(
925 path
.substr(rootlen
, wxString::npos
));
927 bool isDir
= wxDirExists(path
);
931 // provide some context for the error message so that we know which
932 // iteration of the loop we were on
933 string
error_entry((_T(" '") + name
+ _T("'")).mb_str());
934 string
error_context(" failed for entry" + error_entry
);
936 TestEntries::iterator it
= m_testEntries
.find(name
);
937 CPPUNIT_ASSERT_MESSAGE(
938 "archive contains an entry that shouldn't be there"
940 it
!= m_testEntries
.end());
942 const TestEntry
& testEntry
= *it
->second
;
944 #if 0 //ndef __WXMSW__
945 CPPUNIT_ASSERT_MESSAGE("timestamp check" + error_context
,
946 testEntry
.GetDateTime() ==
947 wxFileName(path
).GetModificationTime());
950 wxFFileInputStream
in(path
);
951 CPPUNIT_ASSERT_MESSAGE(
952 "entry not found in archive" + error_entry
, in
.Ok());
954 size_t size
= (size_t)in
.GetLength();
955 wxCharBuffer
buf(size
);
956 CPPUNIT_ASSERT_MESSAGE("Read" + error_context
,
957 in
.Read(buf
.data(), size
).LastRead() == size
);
958 CPPUNIT_ASSERT_MESSAGE("size check" + error_context
,
959 testEntry
.GetSize() == size
);
960 CPPUNIT_ASSERT_MESSAGE("data compare" + error_context
,
961 memcmp(buf
.data(), testEntry
.GetData(), size
) == 0);
964 VerifyDir(path
, rootlen
);
968 m_testEntries
.erase(it
);
970 while (dir
.GetNext(&name
));
974 // test the simple iterators that give away ownership of an entry
976 template <class ClassFactoryT
>
977 void ArchiveTestCase
<ClassFactoryT
>::TestIterator(wxInputStream
& in
)
979 typedef std::list
<EntryT
*> ArchiveCatalog
;
980 typedef typename
ArchiveCatalog::iterator CatalogIter
;
982 auto_ptr
<InputStreamT
> arc(m_factory
->NewStream(in
));
985 #ifdef WXARC_MEMBER_TEMPLATES
986 ArchiveCatalog
cat((IterT
)*arc
, IterT());
989 for (IterT
i(*arc
); i
!= IterT(); ++i
)
993 for (CatalogIter it
= cat
.begin(); it
!= cat
.end(); ++it
) {
994 auto_ptr
<EntryT
> entry(*it
);
995 count
+= m_testEntries
.count(entry
->GetName(wxPATH_UNIX
));
998 CPPUNIT_ASSERT(m_testEntries
.size() == cat
.size());
999 CPPUNIT_ASSERT(count
== cat
.size());
1002 // test the pair iterators that can be used to load a std::map or wxHashMap
1003 // these also give away ownership of entries
1005 template <class ClassFactoryT
>
1006 void ArchiveTestCase
<ClassFactoryT
>::TestPairIterator(wxInputStream
& in
)
1008 typedef std::map
<wxString
, EntryT
*> ArchiveCatalog
;
1009 typedef typename
ArchiveCatalog::iterator CatalogIter
;
1011 auto_ptr
<InputStreamT
> arc(m_factory
->NewStream(in
));
1014 #ifdef WXARC_MEMBER_TEMPLATES
1015 ArchiveCatalog
cat((PairIterT
)*arc
, PairIterT());
1018 for (PairIterT
i(*arc
); i
!= PairIterT(); ++i
)
1022 for (CatalogIter it
= cat
.begin(); it
!= cat
.end(); ++it
) {
1023 auto_ptr
<EntryT
> entry(it
->second
);
1024 count
+= m_testEntries
.count(entry
->GetName(wxPATH_UNIX
));
1027 CPPUNIT_ASSERT(m_testEntries
.size() == cat
.size());
1028 CPPUNIT_ASSERT(count
== cat
.size());
1031 // simple iterators using smart pointers, no need to worry about ownership
1033 template <class ClassFactoryT
>
1034 void ArchiveTestCase
<ClassFactoryT
>::TestSmartIterator(wxInputStream
& in
)
1036 typedef std::list
<Ptr
<EntryT
> > ArchiveCatalog
;
1037 typedef typename
ArchiveCatalog::iterator CatalogIter
;
1038 typedef wxArchiveIterator
<InputStreamT
, Ptr
<EntryT
> > Iter
;
1040 auto_ptr
<InputStreamT
> arc(m_factory
->NewStream(in
));
1042 #ifdef WXARC_MEMBER_TEMPLATES
1043 ArchiveCatalog
cat((Iter
)*arc
, Iter());
1046 for (Iter
i(*arc
); i
!= Iter(); ++i
)
1050 CPPUNIT_ASSERT(m_testEntries
.size() == cat
.size());
1052 for (CatalogIter it
= cat
.begin(); it
!= cat
.end(); ++it
)
1053 CPPUNIT_ASSERT(m_testEntries
.count((*it
)->GetName(wxPATH_UNIX
)));
1056 // pair iterator using smart pointers
1058 template <class ClassFactoryT
>
1059 void ArchiveTestCase
<ClassFactoryT
>::TestSmartPairIterator(wxInputStream
& in
)
1061 #if defined _MSC_VER && defined _MSC_VER < 1200
1062 // With VC++ 5.0 the '=' operator of std::pair breaks when the second
1063 // type is Ptr<EntryT>, so this iterator can't be made to work.
1066 typedef std::map
<wxString
, Ptr
<EntryT
> > ArchiveCatalog
;
1067 typedef typename
ArchiveCatalog::iterator CatalogIter
;
1068 typedef wxArchiveIterator
<InputStreamT
,
1069 std::pair
<wxString
, Ptr
<EntryT
> > > PairIter
;
1071 auto_ptr
<InputStreamT
> arc(m_factory
->NewStream(in
));
1073 #ifdef WXARC_MEMBER_TEMPLATES
1074 ArchiveCatalog
cat((PairIter
)*arc
, PairIter());
1077 for (PairIter
i(*arc
); i
!= PairIter(); ++i
)
1081 CPPUNIT_ASSERT(m_testEntries
.size() == cat
.size());
1083 for (CatalogIter it
= cat
.begin(); it
!= cat
.end(); ++it
)
1084 CPPUNIT_ASSERT(m_testEntries
.count(it
->second
->GetName(wxPATH_UNIX
)));
1088 // try reading two entries at the same time
1090 template <class ClassFactoryT
>
1091 void ArchiveTestCase
<ClassFactoryT
>::ReadSimultaneous(TestInputStream
& in
)
1093 typedef std::map
<wxString
, Ptr
<EntryT
> > ArchiveCatalog
;
1094 typedef wxArchiveIterator
<InputStreamT
,
1095 std::pair
<wxString
, Ptr
<EntryT
> > > PairIter
;
1097 // create two archive input streams
1098 TestInputStream
in2(in
);
1099 auto_ptr
<InputStreamT
> arc(m_factory
->NewStream(in
));
1100 auto_ptr
<InputStreamT
> arc2(m_factory
->NewStream(in2
));
1103 #ifdef WXARC_MEMBER_TEMPLATES
1104 ArchiveCatalog
cat((PairIter
)*arc
, PairIter());
1107 for (PairIter
i(*arc
); i
!= PairIter(); ++i
)
1111 // the names of two entries to read
1112 const wxChar
*name
= _T("text/small");
1113 const wxChar
*name2
= _T("bin/bin1000");
1116 typename
ArchiveCatalog::iterator j
;
1117 CPPUNIT_ASSERT((j
= cat
.find(name
)) != cat
.end());
1118 CPPUNIT_ASSERT(arc
->OpenEntry(*j
->second
));
1119 CPPUNIT_ASSERT((j
= cat
.find(name2
)) != cat
.end());
1120 CPPUNIT_ASSERT(arc2
->OpenEntry(*j
->second
));
1122 // get pointers to the expected data
1123 TestEntries::iterator k
;
1124 CPPUNIT_ASSERT((k
= m_testEntries
.find(name
)) != m_testEntries
.end());
1125 TestEntry
*entry
= k
->second
;
1126 CPPUNIT_ASSERT((k
= m_testEntries
.find(name2
)) != m_testEntries
.end());
1127 TestEntry
*entry2
= k
->second
;
1129 size_t count
= 0, count2
= 0;
1130 size_t size
= entry
->GetSize(), size2
= entry2
->GetSize();
1131 const char *data
= entry
->GetData(), *data2
= entry2
->GetData();
1133 // read and check the two entries in parallel, character by character
1134 while (arc
->IsOk() || arc2
->IsOk()) {
1135 char ch
= arc
->GetC();
1136 if (arc
->LastRead() == 1) {
1137 CPPUNIT_ASSERT(count
< size
);
1138 CPPUNIT_ASSERT(ch
== data
[count
++]);
1140 char ch2
= arc2
->GetC();
1141 if (arc2
->LastRead() == 1) {
1142 CPPUNIT_ASSERT(count2
< size2
);
1143 CPPUNIT_ASSERT(ch2
== data2
[count2
++]);
1147 CPPUNIT_ASSERT(arc
->Eof());
1148 CPPUNIT_ASSERT(arc2
->Eof());
1149 CPPUNIT_ASSERT(count
== size
);
1150 CPPUNIT_ASSERT(count2
== size2
);
1153 // Nothing useful can be done with a generic notifier yet, so just test one
1156 template <class NotifierT
, class EntryT
>
1157 class ArchiveNotifier
: public NotifierT
1160 void OnEntryUpdated(EntryT
& WXUNUSED(entry
)) { }
1163 template <class ClassFactoryT
>
1164 void ArchiveTestCase
<ClassFactoryT
>::OnSetNotifier(EntryT
& entry
)
1166 static ArchiveNotifier
<NotifierT
, EntryT
> notifier
;
1167 entry
.SetNotifier(notifier
);
1171 ///////////////////////////////////////////////////////////////////////////////
1174 int TestId::m_seed
= 6219;
1177 string
TestId::MakeId()
1179 m_seed
= (m_seed
* 171) % 30269;
1180 return (const char *)wxString::Format(_T("%-6d"), m_seed
).mb_str();
1184 ///////////////////////////////////////////////////////////////////////////////
1187 ArchiveTestSuite::ArchiveTestSuite(string name
)
1188 : CppUnit::TestSuite("archive/" + name
),
1189 m_name(name
.c_str(), *wxConvCurrent
)
1191 m_name
= _T("wx") + m_name
.Left(1).Upper() + m_name
.Mid(1).Lower();
1192 m_path
.AddEnvList(_T("PATH"));
1193 m_archivers
.push_back(_T(""));
1194 m_unarchivers
.push_back(_T(""));
1197 // add the command for an external archiver to the list, testing for it in
1200 void ArchiveTestSuite::AddCmd(wxArrayString
& cmdlist
, const wxString
& cmd
)
1203 cmdlist
.push_back(cmd
);
1206 bool ArchiveTestSuite::IsInPath(const wxString
& cmd
)
1208 wxString c
= cmd
.BeforeFirst(_T(' '));
1212 return !m_path
.FindValidPath(c
).empty();
1215 // make the test suite
1217 ArchiveTestSuite
*ArchiveTestSuite::makeSuite()
1219 typedef wxArrayString::iterator Iter
;
1221 for (int generic
= 0; generic
< 2; generic
++)
1222 for (Iter i
= m_unarchivers
.begin(); i
!= m_unarchivers
.end(); ++i
)
1223 for (Iter j
= m_archivers
.begin(); j
!= m_archivers
.end(); ++j
)
1224 for (int options
= 0; options
<= AllOptions
; options
++)
1226 #ifdef WXARC_NO_POPEN
1227 // if no popen then can't pipe in/out of archiver
1228 if ((options
& PipeIn
) && !i
->empty())
1230 if ((options
& PipeOut
) && !j
->empty())
1233 string descr
= Description(m_name
, options
,
1234 generic
!= 0, *j
, *i
);
1236 CppUnit::Test
*test
= makeTest(descr
, options
,
1237 generic
!= 0, *j
, *i
);
1246 CppUnit::Test
*ArchiveTestSuite::makeTest(
1247 string
WXUNUSED(descr
),
1248 int WXUNUSED(options
),
1249 bool WXUNUSED(genericInterface
),
1250 const wxString
& WXUNUSED(archiver
),
1251 const wxString
& WXUNUSED(unarchiver
))
1256 // make a display string for the option bits
1258 string
ArchiveTestSuite::Description(const wxString
& type
,
1260 bool genericInterface
,
1261 const wxString
& archiver
,
1262 const wxString
& unarchiver
)
1266 if (genericInterface
)
1267 descr
<< _T("wxArchive (") << type
<< _T(")");
1271 if (!archiver
.empty()) {
1272 const wxChar
*fn
= (options
& PipeOut
) != 0 ? _T("-") : _T("file");
1273 descr
<< _T(" (") << wxString::Format(archiver
, fn
) << _T(")");
1275 if (!unarchiver
.empty()) {
1276 const wxChar
*fn
= (options
& PipeIn
) != 0 ? _T("-") : _T("file");
1277 descr
<< _T(" (") << wxString::Format(unarchiver
, fn
) << _T(")");
1282 if ((options
& PipeIn
) != 0)
1283 optstr
+= _T("|PipeIn");
1284 if ((options
& PipeOut
) != 0)
1285 optstr
+= _T("|PipeOut");
1286 if ((options
& Stub
) != 0)
1287 optstr
+= _T("|Stub");
1288 if (!optstr
.empty())
1289 optstr
= _T(" (") + optstr
.substr(1) + _T(")");
1293 return (const char*)descr
.mb_str();
1297 ///////////////////////////////////////////////////////////////////////////////
1300 template class ArchiveTestCase
<wxArchiveClassFactory
>;
1303 #include "wx/zipstrm.h"
1304 template class ArchiveTestCase
<wxZipClassFactory
>;
1307 #endif // wxUSE_STREAMS && wxUSE_ARCHIVE_STREAMS