]>
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;
223 void TestInputStream::SetData(TestOutputStream
& out
)
226 m_options
= out
.GetOptions();
227 out
.GetData(m_data
, m_size
);
231 wxFileOffset
TestInputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
233 if ((m_options
& PipeIn
) == 0) {
235 case wxFromStart
: break;
236 case wxFromCurrent
: pos
+= m_pos
; break;
237 case wxFromEnd
: pos
+= m_size
; break;
239 if (pos
< 0 || pos
> SEEK_LIMIT
)
240 return wxInvalidOffset
;
244 return wxInvalidOffset
;
247 wxFileOffset
TestInputStream::OnSysTell() const
249 return (m_options
& PipeIn
) == 0 ? (wxFileOffset
)m_pos
: wxInvalidOffset
;
252 size_t TestInputStream::OnSysRead(void *buffer
, size_t size
)
254 if (!IsOk() || !size
)
261 else if (m_size
- m_pos
< size
)
262 count
= m_size
- m_pos
;
267 memcpy(buffer
, m_data
+ m_pos
, count
);
271 if (((m_eoftype
& AtLast
) != 0 && m_pos
>= m_size
) || count
< size
)
272 if ((m_eoftype
& WithError
) != 0)
273 m_lasterror
= wxSTREAM_READ_ERROR
;
275 m_lasterror
= wxSTREAM_EOF
;
281 ///////////////////////////////////////////////////////////////////////////////
282 // minimal non-intrusive reference counting pointer for testing the iterators
284 template <class T
> class Ptr
287 explicit Ptr(T
* p
= NULL
) : m_p(p
), m_count(new int) { *m_count
= 1; }
288 Ptr(const Ptr
& sp
) : m_p(sp
.m_p
), m_count(sp
.m_count
) { ++*m_count
; }
291 Ptr
& operator =(const Ptr
& sp
) {
295 m_count
= sp
.m_count
;
301 T
* get() const { return m_p
; }
302 T
* operator->() const { return m_p
; }
303 T
& operator*() const { return *m_p
; }
307 if (--*m_count
== 0) {
318 ///////////////////////////////////////////////////////////////////////////////
319 // Clean-up for temp directory
326 wxString
GetName() const { return m_tmp
; }
329 void RemoveDir(wxString
& path
);
336 wxString tmp
= wxFileName::CreateTempFileName(_T("arctest-"));
339 m_original
= wxGetCwd();
340 CPPUNIT_ASSERT(wxMkdir(tmp
, 0700));
342 CPPUNIT_ASSERT(wxSetWorkingDirectory(tmp
));
348 if (!m_tmp
.empty()) {
349 wxSetWorkingDirectory(m_original
);
354 void TempDir::RemoveDir(wxString
& path
)
356 wxCHECK_RET(!m_tmp
.empty() && path
.substr(0, m_tmp
.length()) == m_tmp
,
357 _T("remove '") + path
+ _T("' fails safety check"));
359 const wxChar
*files
[] = {
369 _T("zero/zero32768"),
370 _T("zero/zero16385"),
375 const wxChar
*dirs
[] = {
376 _T("text/"), _T("bin/"), _T("zero/"), _T("empty/")
379 wxString tmp
= m_tmp
+ wxFileName::GetPathSeparator();
382 for (i
= 0; i
< WXSIZEOF(files
); i
++)
383 wxRemoveFile(tmp
+ wxFileName(files
[i
], wxPATH_UNIX
).GetFullPath());
385 for (i
= 0; i
< WXSIZEOF(dirs
); i
++)
386 wxRmdir(tmp
+ wxFileName(dirs
[i
], wxPATH_UNIX
).GetFullPath());
389 wxLogSysError(_T("can't remove temporary dir '%s'"), m_tmp
.c_str());
393 ///////////////////////////////////////////////////////////////////////////////
394 // wxFFile streams for piping to/from an external program
396 #if defined __UNIX__ || defined __MINGW32__
397 # define WXARC_popen popen
398 # define WXARC_pclose pclose
399 #elif defined _MSC_VER || defined __BORLANDC__
400 # define WXARC_popen _popen
401 # define WXARC_pclose _pclose
403 # define WXARC_NO_POPEN
404 # define WXARC_popen(cmd, type) NULL
405 # define WXARC_pclose(fp)
414 PFileInputStream::PFileInputStream(const wxString
& cmd
)
415 : wxFFileInputStream(WXARC_popen(cmd
.mb_str(), "r" WXARC_b
))
419 PFileInputStream::~PFileInputStream()
421 WXARC_pclose(m_file
->fp()); m_file
->Detach();
424 PFileOutputStream::PFileOutputStream(const wxString
& cmd
)
425 : wxFFileOutputStream(WXARC_popen(cmd
.mb_str(), "w" WXARC_b
))
429 PFileOutputStream::~PFileOutputStream()
431 WXARC_pclose(m_file
->fp()); m_file
->Detach();
435 ///////////////////////////////////////////////////////////////////////////////
438 template <class ClassFactoryT
>
439 ArchiveTestCase
<ClassFactoryT
>::ArchiveTestCase(
441 ClassFactoryT
*factory
,
443 const wxString
& archiver
,
444 const wxString
& unarchiver
)
446 CppUnit::TestCase(TestId::MakeId() + name
),
449 m_timeStamp(1, wxDateTime::Mar
, 2004, 12, 0),
450 m_id(TestId::GetId()),
451 m_archiver(archiver
),
452 m_unarchiver(unarchiver
)
454 wxASSERT(m_factory
.get() != NULL
);
457 template <class ClassFactoryT
>
458 ArchiveTestCase
<ClassFactoryT
>::~ArchiveTestCase()
460 TestEntries::iterator it
;
461 for (it
= m_testEntries
.begin(); it
!= m_testEntries
.end(); ++it
)
465 template <class ClassFactoryT
>
466 void ArchiveTestCase
<ClassFactoryT
>::runTest()
468 TestOutputStream
out(m_options
);
472 if (m_archiver
.empty())
475 CreateArchive(out
, m_archiver
);
477 // check archive could be created
478 CPPUNIT_ASSERT(out
.GetLength() > 0);
480 TestInputStream
in(out
, m_id
% ((m_options
& PipeIn
) ? 4 : 3));
484 TestPairIterator(in
);
486 TestSmartIterator(in
);
488 TestSmartPairIterator(in
);
491 if ((m_options
& PipeIn
) == 0) {
492 ReadSimultaneous(in
);
496 ModifyArchive(in
, out
);
499 if (m_unarchiver
.empty())
502 ExtractArchive(in
, m_unarchiver
);
504 // check that all the test entries were found in the archive
505 CPPUNIT_ASSERT(m_testEntries
.empty());
508 template <class ClassFactoryT
>
509 void ArchiveTestCase
<ClassFactoryT
>::CreateTestData()
512 Add("text/empty", "");
513 Add("text/small", "Small text file for testing\n"
514 "archive streams in wxWidgets\n");
517 Add("bin/bin1000", 1000);
518 Add("bin/bin4095", 4095);
519 Add("bin/bin4096", 4096);
520 Add("bin/bin4097", 4097);
521 Add("bin/bin16384", 16384);
524 Add("zero/zero5", 5, 0);
525 Add("zero/zero1024", 1024, 109);
526 Add("zero/zero32768", 32768, 106);
527 Add("zero/zero16385", 16385, 119);
532 template <class ClassFactoryT
>
533 TestEntry
& ArchiveTestCase
<ClassFactoryT
>::Add(const char *name
,
539 TestEntry
*& entry
= m_testEntries
[wxString(name
, *wxConvCurrent
)];
540 wxASSERT(entry
== NULL
);
541 entry
= new TestEntry(m_timeStamp
, len
, data
);
542 m_timeStamp
+= wxTimeSpan(0, 1, 30);
546 template <class ClassFactoryT
>
547 TestEntry
& ArchiveTestCase
<ClassFactoryT
>::Add(const char *name
,
551 wxCharBuffer
buf(len
);
552 for (int i
= 0; i
< len
; i
++)
553 buf
.data()[i
] = (char)(value
== EOF
? rand() : value
);
554 return Add(name
, buf
, len
);
557 // Create an archive using the wx archive classes, write it to 'out'
559 template <class ClassFactoryT
>
560 void ArchiveTestCase
<ClassFactoryT
>::CreateArchive(wxOutputStream
& out
)
562 auto_ptr
<OutputStreamT
> arc(m_factory
->NewStream(out
));
563 TestEntries::iterator it
;
565 OnCreateArchive(*arc
);
567 // We want to try creating entries in various different ways, 'choices'
568 // is just a number used to select between all the various possibilities.
571 for (it
= m_testEntries
.begin(); it
!= m_testEntries
.end(); ++it
) {
573 TestEntry
& testEntry
= *it
->second
;
574 wxString name
= it
->first
;
576 // It should be possible to create a directory entry just by supplying
577 // a name that looks like a directory, or alternatively any old name
578 // can be identified as a directory using SetIsDir or PutNextDirEntry
579 bool setIsDir
= name
.Last() == _T('/') && (choices
& 1);
581 name
.erase(name
.length() - 1);
583 // provide some context for the error message so that we know which
584 // iteration of the loop we were on
585 string
error_entry((_T(" '") + name
+ _T("'")).mb_str());
586 string
error_context(" failed for entry" + error_entry
);
588 if ((choices
& 2) || testEntry
.IsText()) {
589 // try PutNextEntry(EntryT *pEntry)
590 auto_ptr
<EntryT
> entry(m_factory
->NewEntry());
591 entry
->SetName(name
, wxPATH_UNIX
);
594 entry
->SetDateTime(testEntry
.GetDateTime());
595 entry
->SetSize(testEntry
.GetLength());
596 OnCreateEntry(*arc
, testEntry
, entry
.get());
597 CPPUNIT_ASSERT_MESSAGE("PutNextEntry" + error_context
,
598 arc
->PutNextEntry(entry
.release()));
601 // try the convenience methods
602 OnCreateEntry(*arc
, testEntry
);
604 CPPUNIT_ASSERT_MESSAGE("PutNextDirEntry" + error_context
,
605 arc
->PutNextDirEntry(name
, testEntry
.GetDateTime()));
607 CPPUNIT_ASSERT_MESSAGE("PutNextEntry" + error_context
,
608 arc
->PutNextEntry(name
, testEntry
.GetDateTime(),
609 testEntry
.GetLength()));
612 if (it
->first
.Last() != _T('/')) {
613 // for non-dirs write the data
614 arc
->Write(testEntry
.GetData(), testEntry
.GetSize());
615 CPPUNIT_ASSERT_MESSAGE("LastWrite check" + error_context
,
616 arc
->LastWrite() == testEntry
.GetSize());
617 // should work with or without explicit CloseEntry
619 CPPUNIT_ASSERT_MESSAGE("CloseEntry" + error_context
,
623 CPPUNIT_ASSERT_MESSAGE("IsOk" + error_context
, arc
->IsOk());
626 // should work with or without explicit Close
628 CPPUNIT_ASSERT(arc
->Close());
631 // Create an archive using an external archive program
633 template <class ClassFactoryT
>
634 void ArchiveTestCase
<ClassFactoryT
>::CreateArchive(wxOutputStream
& out
,
635 const wxString
& archiver
)
637 // for an external archiver the test data need to be written to
642 TestEntries::iterator i
;
643 for (i
= m_testEntries
.begin(); i
!= m_testEntries
.end(); ++i
) {
644 wxFileName
fn(i
->first
, wxPATH_UNIX
);
645 TestEntry
& entry
= *i
->second
;
648 fn
.Mkdir(0777, wxPATH_MKDIR_FULL
);
650 wxFileName::Mkdir(fn
.GetPath(), 0777, wxPATH_MKDIR_FULL
);
651 wxFFileOutputStream
fileout(fn
.GetFullPath());
652 fileout
.Write(entry
.GetData(), entry
.GetSize());
656 for (i
= m_testEntries
.begin(); i
!= m_testEntries
.end(); ++i
) {
657 wxFileName
fn(i
->first
, wxPATH_UNIX
);
658 TestEntry
& entry
= *i
->second
;
659 wxDateTime dt
= entry
.GetDateTime();
662 entry
.SetDateTime(wxDateTime());
665 fn
.SetTimes(NULL
, &dt
, NULL
);
668 if ((m_options
& PipeOut
) == 0) {
669 wxFileName
fn(tmpdir
.GetName());
670 fn
.SetExt(_T("arc"));
671 wxString tmparc
= fn
.GetPath(wxPATH_GET_SEPARATOR
) + fn
.GetFullName();
673 // call the archiver to create an archive file
674 system(wxString::Format(archiver
, tmparc
.c_str()).mb_str());
676 // then load the archive file
678 wxFFileInputStream
in(tmparc
);
683 wxRemoveFile(tmparc
);
686 // for the non-seekable test, have the archiver output to "-"
687 // and read the archive via a pipe
688 PFileInputStream
in(wxString::Format(archiver
, _T("-")));
694 // Do a standard set of modification on an archive, delete an entry,
695 // rename an entry and add an entry
697 template <class ClassFactoryT
>
698 void ArchiveTestCase
<ClassFactoryT
>::ModifyArchive(wxInputStream
& in
,
701 auto_ptr
<InputStreamT
> arcIn(m_factory
->NewStream(in
));
702 auto_ptr
<OutputStreamT
> arcOut(m_factory
->NewStream(out
));
705 const wxString deleteName
= _T("bin/bin1000");
706 const wxString renameFrom
= _T("zero/zero1024");
707 const wxString renameTo
= _T("zero/newname");
708 const wxString newName
= _T("newfile");
709 const char *newData
= "New file added as a test\n";
711 arcOut
->CopyArchiveMetaData(*arcIn
);
713 while ((pEntry
= arcIn
->GetNextEntry()) != NULL
) {
714 auto_ptr
<EntryT
> entry(pEntry
);
715 OnSetNotifier(*entry
);
716 wxString name
= entry
->GetName(wxPATH_UNIX
);
718 // provide some context for the error message so that we know which
719 // iteration of the loop we were on
720 string
error_entry((_T(" '") + name
+ _T("'")).mb_str());
721 string
error_context(" failed for entry" + error_entry
);
723 if (name
== deleteName
) {
724 TestEntries::iterator it
= m_testEntries
.find(name
);
725 CPPUNIT_ASSERT_MESSAGE(
726 "deletion failed (already deleted?) for" + error_entry
,
727 it
!= m_testEntries
.end());
728 TestEntry
*p
= it
->second
;
729 m_testEntries
.erase(it
);
733 if (name
== renameFrom
) {
734 entry
->SetName(renameTo
);
735 TestEntries::iterator it
= m_testEntries
.find(renameFrom
);
736 CPPUNIT_ASSERT_MESSAGE(
737 "rename failed (already renamed?) for" + error_entry
,
738 it
!= m_testEntries
.end());
739 TestEntry
*p
= it
->second
;
740 m_testEntries
.erase(it
);
741 m_testEntries
[renameTo
] = p
;
744 CPPUNIT_ASSERT_MESSAGE("CopyEntry" + error_context
,
745 arcOut
->CopyEntry(entry
.release(), *arcIn
));
749 // check that the deletion and rename were done
750 CPPUNIT_ASSERT(m_testEntries
.count(deleteName
) == 0);
751 CPPUNIT_ASSERT(m_testEntries
.count(renameFrom
) == 0);
752 CPPUNIT_ASSERT(m_testEntries
.count(renameTo
) == 1);
754 // check that the end of the input archive was reached without error
755 CPPUNIT_ASSERT(arcIn
->Eof());
757 // try adding a new entry
758 TestEntry
& testEntry
= Add(newName
.mb_str(), newData
);
759 auto_ptr
<EntryT
> newentry(m_factory
->NewEntry());
760 newentry
->SetName(newName
);
761 newentry
->SetDateTime(testEntry
.GetDateTime());
762 newentry
->SetSize(testEntry
.GetLength());
763 OnCreateEntry(*arcOut
, testEntry
, newentry
.get());
764 OnSetNotifier(*newentry
);
765 CPPUNIT_ASSERT(arcOut
->PutNextEntry(newentry
.release()));
766 CPPUNIT_ASSERT(arcOut
->Write(newData
, strlen(newData
)).IsOk());
768 // should work with or without explicit Close
770 CPPUNIT_ASSERT(arcOut
->Close());
773 // Extract an archive using the wx archive classes
775 template <class ClassFactoryT
>
776 void ArchiveTestCase
<ClassFactoryT
>::ExtractArchive(wxInputStream
& in
)
778 typedef Ptr
<EntryT
> EntryPtr
;
779 typedef std::list
<EntryPtr
> Entries
;
780 typedef typename
Entries::iterator EntryIter
;
782 auto_ptr
<InputStreamT
> arc(m_factory
->NewStream(in
));
783 int expectedTotal
= m_testEntries
.size();
787 if ((m_options
& PipeIn
) == 0)
788 OnArchiveExtracted(*arc
, expectedTotal
);
790 while (entry
= EntryPtr(arc
->GetNextEntry()), entry
.get() != NULL
) {
791 wxString name
= entry
->GetName(wxPATH_UNIX
);
793 // provide some context for the error message so that we know which
794 // iteration of the loop we were on
795 string
error_entry((_T(" '") + name
+ _T("'")).mb_str());
796 string
error_context(" failed for entry" + error_entry
);
798 TestEntries::iterator it
= m_testEntries
.find(name
);
799 CPPUNIT_ASSERT_MESSAGE(
800 "archive contains an entry that shouldn't be there" + error_entry
,
801 it
!= m_testEntries
.end());
803 const TestEntry
& testEntry
= *it
->second
;
805 wxDateTime dt
= testEntry
.GetDateTime();
807 CPPUNIT_ASSERT_MESSAGE("timestamp check" + error_context
,
808 dt
== entry
->GetDateTime());
810 // non-seekable entries are allowed to have GetSize == wxInvalidOffset
811 // until the end of the entry's data has been read past
812 CPPUNIT_ASSERT_MESSAGE("entry size check" + error_context
,
813 testEntry
.GetLength() == entry
->GetSize() ||
814 ((m_options
& PipeIn
) != 0 && entry
->GetSize() == wxInvalidOffset
));
815 CPPUNIT_ASSERT_MESSAGE(
816 "arc->GetLength() == entry->GetSize()" + error_context
,
817 arc
->GetLength() == entry
->GetSize());
819 if (name
.Last() != _T('/'))
821 CPPUNIT_ASSERT_MESSAGE("!IsDir" + error_context
,
823 wxCharBuffer
buf(testEntry
.GetSize() + 1);
824 CPPUNIT_ASSERT_MESSAGE("Read until Eof" + error_context
,
825 arc
->Read(buf
.data(), testEntry
.GetSize() + 1).Eof());
826 CPPUNIT_ASSERT_MESSAGE("LastRead check" + error_context
,
827 arc
->LastRead() == testEntry
.GetSize());
828 CPPUNIT_ASSERT_MESSAGE("data compare" + error_context
,
829 !memcmp(buf
.data(), testEntry
.GetData(), testEntry
.GetSize()));
831 CPPUNIT_ASSERT_MESSAGE("IsDir" + error_context
, entry
->IsDir());
834 // GetSize() must return the right result in all cases after all the
835 // data has been read
836 CPPUNIT_ASSERT_MESSAGE("entry size check" + error_context
,
837 testEntry
.GetLength() == entry
->GetSize());
838 CPPUNIT_ASSERT_MESSAGE(
839 "arc->GetLength() == entry->GetSize()" + error_context
,
840 arc
->GetLength() == entry
->GetSize());
842 if ((m_options
& PipeIn
) == 0) {
843 OnEntryExtracted(*entry
, testEntry
, arc
.get());
845 m_testEntries
.erase(it
);
847 entries
.push_back(entry
);
851 // check that the end of the input archive was reached without error
852 CPPUNIT_ASSERT(arc
->Eof());
854 // for non-seekable streams these data are only guaranteed to be
855 // available once the end of the archive has been reached
856 if (m_options
& PipeIn
) {
857 for (EntryIter i
= entries
.begin(); i
!= entries
.end(); ++i
) {
858 wxString name
= (*i
)->GetName(wxPATH_UNIX
);
859 TestEntries::iterator j
= m_testEntries
.find(name
);
860 OnEntryExtracted(**i
, *j
->second
);
862 m_testEntries
.erase(j
);
864 OnArchiveExtracted(*arc
, expectedTotal
);
868 // Extract an archive using an external unarchive program
870 template <class ClassFactoryT
>
871 void ArchiveTestCase
<ClassFactoryT
>::ExtractArchive(wxInputStream
& in
,
872 const wxString
& unarchiver
)
874 // for an external unarchiver, unarchive to a tempdir
877 if ((m_options
& PipeIn
) == 0) {
878 wxFileName
fn(tmpdir
.GetName());
879 fn
.SetExt(_T("arc"));
880 wxString tmparc
= fn
.GetPath(wxPATH_GET_SEPARATOR
) + fn
.GetFullName();
882 if (m_options
& Stub
)
883 in
.SeekI(STUB_SIZE
* 2);
885 // write the archive to a temporary file
887 wxFFileOutputStream
out(tmparc
);
893 system(wxString::Format(unarchiver
, tmparc
.c_str()).mb_str());
894 wxRemoveFile(tmparc
);
897 // for the non-seekable test, have the archiver extract "-" and
898 // feed it the archive via a pipe
899 PFileOutputStream
out(wxString::Format(unarchiver
, _T("-")));
904 wxString dir
= tmpdir
.GetName();
908 // Verifies the files produced by an external unarchiver are as expected
910 template <class ClassFactoryT
>
911 void ArchiveTestCase
<ClassFactoryT
>::VerifyDir(wxString
& path
,
912 size_t rootlen
/*=0*/)
915 path
+= wxFileName::GetPathSeparator();
916 int pos
= path
.length();
922 if (dir
.Open(path
) && dir
.GetFirst(&name
)) {
924 path
.replace(pos
, wxString::npos
, name
);
925 name
= m_factory
->GetInternalName(
926 path
.substr(rootlen
, wxString::npos
));
928 bool isDir
= wxDirExists(path
);
932 // provide some context for the error message so that we know which
933 // iteration of the loop we were on
934 string
error_entry((_T(" '") + name
+ _T("'")).mb_str());
935 string
error_context(" failed for entry" + error_entry
);
937 TestEntries::iterator it
= m_testEntries
.find(name
);
938 CPPUNIT_ASSERT_MESSAGE(
939 "archive contains an entry that shouldn't be there"
941 it
!= m_testEntries
.end());
943 const TestEntry
& testEntry
= *it
->second
;
945 #if 0 //ndef __WXMSW__
946 CPPUNIT_ASSERT_MESSAGE("timestamp check" + error_context
,
947 testEntry
.GetDateTime() ==
948 wxFileName(path
).GetModificationTime());
951 wxFFileInputStream
in(path
);
952 CPPUNIT_ASSERT_MESSAGE(
953 "entry not found in archive" + error_entry
, in
.Ok());
955 size_t size
= (size_t)in
.GetLength();
956 wxCharBuffer
buf(size
);
957 CPPUNIT_ASSERT_MESSAGE("Read" + error_context
,
958 in
.Read(buf
.data(), size
).LastRead() == size
);
959 CPPUNIT_ASSERT_MESSAGE("size check" + error_context
,
960 testEntry
.GetSize() == size
);
961 CPPUNIT_ASSERT_MESSAGE("data compare" + error_context
,
962 memcmp(buf
.data(), testEntry
.GetData(), size
) == 0);
965 VerifyDir(path
, rootlen
);
969 m_testEntries
.erase(it
);
971 while (dir
.GetNext(&name
));
975 // test the simple iterators that give away ownership of an entry
977 template <class ClassFactoryT
>
978 void ArchiveTestCase
<ClassFactoryT
>::TestIterator(wxInputStream
& in
)
980 typedef std::list
<EntryT
*> ArchiveCatalog
;
981 typedef typename
ArchiveCatalog::iterator CatalogIter
;
983 auto_ptr
<InputStreamT
> arc(m_factory
->NewStream(in
));
986 #ifdef WXARC_MEMBER_TEMPLATES
987 ArchiveCatalog
cat((IterT
)*arc
, IterT());
990 for (IterT
i(*arc
); i
!= IterT(); ++i
)
994 for (CatalogIter it
= cat
.begin(); it
!= cat
.end(); ++it
) {
995 auto_ptr
<EntryT
> entry(*it
);
996 count
+= m_testEntries
.count(entry
->GetName(wxPATH_UNIX
));
999 CPPUNIT_ASSERT(m_testEntries
.size() == cat
.size());
1000 CPPUNIT_ASSERT(count
== cat
.size());
1003 // test the pair iterators that can be used to load a std::map or wxHashMap
1004 // these also give away ownership of entries
1006 template <class ClassFactoryT
>
1007 void ArchiveTestCase
<ClassFactoryT
>::TestPairIterator(wxInputStream
& in
)
1009 typedef std::map
<wxString
, EntryT
*> ArchiveCatalog
;
1010 typedef typename
ArchiveCatalog::iterator CatalogIter
;
1012 auto_ptr
<InputStreamT
> arc(m_factory
->NewStream(in
));
1015 #ifdef WXARC_MEMBER_TEMPLATES
1016 ArchiveCatalog
cat((PairIterT
)*arc
, PairIterT());
1019 for (PairIterT
i(*arc
); i
!= PairIterT(); ++i
)
1023 for (CatalogIter it
= cat
.begin(); it
!= cat
.end(); ++it
) {
1024 auto_ptr
<EntryT
> entry(it
->second
);
1025 count
+= m_testEntries
.count(entry
->GetName(wxPATH_UNIX
));
1028 CPPUNIT_ASSERT(m_testEntries
.size() == cat
.size());
1029 CPPUNIT_ASSERT(count
== cat
.size());
1032 // simple iterators using smart pointers, no need to worry about ownership
1034 template <class ClassFactoryT
>
1035 void ArchiveTestCase
<ClassFactoryT
>::TestSmartIterator(wxInputStream
& in
)
1037 typedef std::list
<Ptr
<EntryT
> > ArchiveCatalog
;
1038 typedef typename
ArchiveCatalog::iterator CatalogIter
;
1039 typedef wxArchiveIterator
<InputStreamT
, Ptr
<EntryT
> > Iter
;
1041 auto_ptr
<InputStreamT
> arc(m_factory
->NewStream(in
));
1043 #ifdef WXARC_MEMBER_TEMPLATES
1044 ArchiveCatalog
cat((Iter
)*arc
, Iter());
1047 for (Iter
i(*arc
); i
!= Iter(); ++i
)
1051 CPPUNIT_ASSERT(m_testEntries
.size() == cat
.size());
1053 for (CatalogIter it
= cat
.begin(); it
!= cat
.end(); ++it
)
1054 CPPUNIT_ASSERT(m_testEntries
.count((*it
)->GetName(wxPATH_UNIX
)));
1057 // pair iterator using smart pointers
1059 template <class ClassFactoryT
>
1060 void ArchiveTestCase
<ClassFactoryT
>::TestSmartPairIterator(wxInputStream
& in
)
1062 #if defined _MSC_VER && defined _MSC_VER < 1200
1063 // With VC++ 5.0 the '=' operator of std::pair breaks when the second
1064 // type is Ptr<EntryT>, so this iterator can't be made to work.
1067 typedef std::map
<wxString
, Ptr
<EntryT
> > ArchiveCatalog
;
1068 typedef typename
ArchiveCatalog::iterator CatalogIter
;
1069 typedef wxArchiveIterator
<InputStreamT
,
1070 std::pair
<wxString
, Ptr
<EntryT
> > > PairIter
;
1072 auto_ptr
<InputStreamT
> arc(m_factory
->NewStream(in
));
1074 #ifdef WXARC_MEMBER_TEMPLATES
1075 ArchiveCatalog
cat((PairIter
)*arc
, PairIter());
1078 for (PairIter
i(*arc
); i
!= PairIter(); ++i
)
1082 CPPUNIT_ASSERT(m_testEntries
.size() == cat
.size());
1084 for (CatalogIter it
= cat
.begin(); it
!= cat
.end(); ++it
)
1085 CPPUNIT_ASSERT(m_testEntries
.count(it
->second
->GetName(wxPATH_UNIX
)));
1089 // try reading two entries at the same time
1091 template <class ClassFactoryT
>
1092 void ArchiveTestCase
<ClassFactoryT
>::ReadSimultaneous(TestInputStream
& in
)
1094 typedef std::map
<wxString
, Ptr
<EntryT
> > ArchiveCatalog
;
1095 typedef wxArchiveIterator
<InputStreamT
,
1096 std::pair
<wxString
, Ptr
<EntryT
> > > PairIter
;
1098 // create two archive input streams
1099 TestInputStream
in2(in
);
1100 auto_ptr
<InputStreamT
> arc(m_factory
->NewStream(in
));
1101 auto_ptr
<InputStreamT
> arc2(m_factory
->NewStream(in2
));
1104 #ifdef WXARC_MEMBER_TEMPLATES
1105 ArchiveCatalog
cat((PairIter
)*arc
, PairIter());
1108 for (PairIter
i(*arc
); i
!= PairIter(); ++i
)
1112 // the names of two entries to read
1113 const wxChar
*name
= _T("text/small");
1114 const wxChar
*name2
= _T("bin/bin1000");
1117 typename
ArchiveCatalog::iterator j
;
1118 CPPUNIT_ASSERT((j
= cat
.find(name
)) != cat
.end());
1119 CPPUNIT_ASSERT(arc
->OpenEntry(*j
->second
));
1120 CPPUNIT_ASSERT((j
= cat
.find(name2
)) != cat
.end());
1121 CPPUNIT_ASSERT(arc2
->OpenEntry(*j
->second
));
1123 // get pointers to the expected data
1124 TestEntries::iterator k
;
1125 CPPUNIT_ASSERT((k
= m_testEntries
.find(name
)) != m_testEntries
.end());
1126 TestEntry
*entry
= k
->second
;
1127 CPPUNIT_ASSERT((k
= m_testEntries
.find(name2
)) != m_testEntries
.end());
1128 TestEntry
*entry2
= k
->second
;
1130 size_t count
= 0, count2
= 0;
1131 size_t size
= entry
->GetSize(), size2
= entry2
->GetSize();
1132 const char *data
= entry
->GetData(), *data2
= entry2
->GetData();
1134 // read and check the two entries in parallel, character by character
1135 while (arc
->IsOk() || arc2
->IsOk()) {
1136 char ch
= arc
->GetC();
1137 if (arc
->LastRead() == 1) {
1138 CPPUNIT_ASSERT(count
< size
);
1139 CPPUNIT_ASSERT(ch
== data
[count
++]);
1141 char ch2
= arc2
->GetC();
1142 if (arc2
->LastRead() == 1) {
1143 CPPUNIT_ASSERT(count2
< size2
);
1144 CPPUNIT_ASSERT(ch2
== data2
[count2
++]);
1148 CPPUNIT_ASSERT(arc
->Eof());
1149 CPPUNIT_ASSERT(arc2
->Eof());
1150 CPPUNIT_ASSERT(count
== size
);
1151 CPPUNIT_ASSERT(count2
== size2
);
1154 // Nothing useful can be done with a generic notifier yet, so just test one
1157 template <class NotifierT
, class EntryT
>
1158 class ArchiveNotifier
: public NotifierT
1161 void OnEntryUpdated(EntryT
& WXUNUSED(entry
)) { }
1164 template <class ClassFactoryT
>
1165 void ArchiveTestCase
<ClassFactoryT
>::OnSetNotifier(EntryT
& entry
)
1167 static ArchiveNotifier
<NotifierT
, EntryT
> notifier
;
1168 entry
.SetNotifier(notifier
);
1172 ///////////////////////////////////////////////////////////////////////////////
1173 // An additional case to check that reading corrupt archives doesn't crash
1175 class CorruptionTestCase
: public CppUnit::TestCase
1178 CorruptionTestCase(std::string name
,
1179 wxArchiveClassFactory
*factory
,
1181 : CppUnit::TestCase(TestId::MakeId() + name
),
1187 // the entry point for the test
1190 void CreateArchive(wxOutputStream
& out
);
1191 void ExtractArchive(wxInputStream
& in
);
1193 auto_ptr
<wxArchiveClassFactory
> m_factory
; // factory to make classes
1194 int m_options
; // test options
1197 void CorruptionTestCase::runTest()
1199 TestOutputStream
out(m_options
);
1201 TestInputStream
in(out
, 0);
1202 wxFileOffset len
= in
.GetLength();
1204 // try flipping one byte in the archive
1205 for (int pos
= 0; pos
< len
; pos
++) {
1213 // try zeroing one byte in the archive
1214 for (int pos
= 0; pos
< len
; pos
++) {
1222 // try chopping the archive off
1223 for (int size
= 1; size
<= len
; size
++) {
1230 void CorruptionTestCase::CreateArchive(wxOutputStream
& out
)
1232 auto_ptr
<wxArchiveOutputStream
> arc(m_factory
->NewStream(out
));
1234 arc
->PutNextDirEntry(_T("dir"));
1235 arc
->PutNextEntry(_T("file"));
1236 arc
->Write(_T("foo"), 3);
1239 void CorruptionTestCase::ExtractArchive(wxInputStream
& in
)
1241 auto_ptr
<wxArchiveInputStream
> arc(m_factory
->NewStream(in
));
1242 auto_ptr
<wxArchiveEntry
> entry(arc
->GetNextEntry());
1244 while (entry
.get() != NULL
) {
1245 wxString name
= entry
->GetName();
1249 arc
->Read(buf
, sizeof(buf
));
1251 auto_ptr
<wxArchiveEntry
> next(arc
->GetNextEntry());
1257 ///////////////////////////////////////////////////////////////////////////////
1260 int TestId::m_seed
= 6219;
1263 string
TestId::MakeId()
1265 m_seed
= (m_seed
* 171) % 30269;
1266 return string(wxString::Format(_T("%-6d"), m_seed
).mb_str());
1270 ///////////////////////////////////////////////////////////////////////////////
1273 ArchiveTestSuite::ArchiveTestSuite(string name
)
1274 : CppUnit::TestSuite("archive/" + name
),
1275 m_name(name
.c_str(), *wxConvCurrent
)
1277 m_name
= _T("wx") + m_name
.Left(1).Upper() + m_name
.Mid(1).Lower();
1278 m_path
.AddEnvList(_T("PATH"));
1279 m_archivers
.push_back(_T(""));
1280 m_unarchivers
.push_back(_T(""));
1283 // add the command for an external archiver to the list, testing for it in
1286 void ArchiveTestSuite::AddCmd(wxArrayString
& cmdlist
, const wxString
& cmd
)
1289 cmdlist
.push_back(cmd
);
1292 bool ArchiveTestSuite::IsInPath(const wxString
& cmd
)
1294 wxString c
= cmd
.BeforeFirst(_T(' '));
1298 return !m_path
.FindValidPath(c
).empty();
1301 // make the test suite
1303 ArchiveTestSuite
*ArchiveTestSuite::makeSuite()
1305 typedef wxArrayString::iterator Iter
;
1307 for (int generic
= 0; generic
< 2; generic
++)
1308 for (Iter i
= m_unarchivers
.begin(); i
!= m_unarchivers
.end(); ++i
)
1309 for (Iter j
= m_archivers
.begin(); j
!= m_archivers
.end(); ++j
)
1310 for (int options
= 0; options
<= AllOptions
; options
++)
1312 #ifdef WXARC_NO_POPEN
1313 // if no popen then can't pipe in/out of archiver
1314 if ((options
& PipeIn
) && !i
->empty())
1316 if ((options
& PipeOut
) && !j
->empty())
1319 string descr
= Description(m_name
, options
,
1320 generic
!= 0, *j
, *i
);
1322 CppUnit::Test
*test
= makeTest(descr
, options
,
1323 generic
!= 0, *j
, *i
);
1329 for (int options
= 0; options
<= PipeIn
; options
+= PipeIn
)
1331 wxObject
*pObj
= wxCreateDynamicObject(m_name
+ _T("ClassFactory"));
1332 wxArchiveClassFactory
*factory
;
1333 factory
= wxDynamicCast(pObj
, wxArchiveClassFactory
);
1336 string
descr(m_name
.mb_str());
1337 descr
= "CorruptionTestCase (" + descr
+ ")";
1340 descr
+= " (PipeIn)";
1342 addTest(new CorruptionTestCase(descr
, factory
, options
));
1349 CppUnit::Test
*ArchiveTestSuite::makeTest(
1350 string
WXUNUSED(descr
),
1351 int WXUNUSED(options
),
1352 bool WXUNUSED(genericInterface
),
1353 const wxString
& WXUNUSED(archiver
),
1354 const wxString
& WXUNUSED(unarchiver
))
1359 // make a display string for the option bits
1361 string
ArchiveTestSuite::Description(const wxString
& type
,
1363 bool genericInterface
,
1364 const wxString
& archiver
,
1365 const wxString
& unarchiver
)
1369 if (genericInterface
)
1370 descr
<< _T("wxArchive (") << type
<< _T(")");
1374 if (!archiver
.empty()) {
1375 const wxChar
*fn
= (options
& PipeOut
) != 0 ? _T("-") : _T("file");
1376 descr
<< _T(" (") << wxString::Format(archiver
, fn
) << _T(")");
1378 if (!unarchiver
.empty()) {
1379 const wxChar
*fn
= (options
& PipeIn
) != 0 ? _T("-") : _T("file");
1380 descr
<< _T(" (") << wxString::Format(unarchiver
, fn
) << _T(")");
1385 if ((options
& PipeIn
) != 0)
1386 optstr
+= _T("|PipeIn");
1387 if ((options
& PipeOut
) != 0)
1388 optstr
+= _T("|PipeOut");
1389 if ((options
& Stub
) != 0)
1390 optstr
+= _T("|Stub");
1391 if (!optstr
.empty())
1392 optstr
= _T(" (") + optstr
.substr(1) + _T(")");
1396 return string(descr
.mb_str());
1400 ///////////////////////////////////////////////////////////////////////////////
1403 template class ArchiveTestCase
<wxArchiveClassFactory
>;
1406 #include "wx/zipstrm.h"
1407 template class ArchiveTestCase
<wxZipClassFactory
>;
1410 #endif // wxUSE_STREAMS && wxUSE_ARCHIVE_STREAMS