]>
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
)
273 if ((m_eoftype
& WithError
) != 0)
274 m_lasterror
= wxSTREAM_READ_ERROR
;
276 m_lasterror
= wxSTREAM_EOF
;
283 ///////////////////////////////////////////////////////////////////////////////
284 // minimal non-intrusive reference counting pointer for testing the iterators
286 template <class T
> class Ptr
289 explicit Ptr(T
* p
= NULL
) : m_p(p
), m_count(new int) { *m_count
= 1; }
290 Ptr(const Ptr
& sp
) : m_p(sp
.m_p
), m_count(sp
.m_count
) { ++*m_count
; }
293 Ptr
& operator =(const Ptr
& sp
) {
297 m_count
= sp
.m_count
;
303 T
* get() const { return m_p
; }
304 T
* operator->() const { return m_p
; }
305 T
& operator*() const { return *m_p
; }
309 if (--*m_count
== 0) {
320 ///////////////////////////////////////////////////////////////////////////////
321 // Clean-up for temp directory
328 wxString
GetName() const { return m_tmp
; }
331 void RemoveDir(wxString
& path
);
338 wxString tmp
= wxFileName::CreateTempFileName(_T("arctest-"));
341 m_original
= wxGetCwd();
342 CPPUNIT_ASSERT(wxMkdir(tmp
, 0700));
344 CPPUNIT_ASSERT(wxSetWorkingDirectory(tmp
));
350 if (!m_tmp
.empty()) {
351 wxSetWorkingDirectory(m_original
);
356 void TempDir::RemoveDir(wxString
& path
)
358 wxCHECK_RET(!m_tmp
.empty() && path
.substr(0, m_tmp
.length()) == m_tmp
,
359 _T("remove '") + path
+ _T("' fails safety check"));
361 const wxChar
*files
[] = {
371 _T("zero/zero32768"),
372 _T("zero/zero16385"),
377 const wxChar
*dirs
[] = {
378 _T("text/"), _T("bin/"), _T("zero/"), _T("empty/")
381 wxString tmp
= m_tmp
+ wxFileName::GetPathSeparator();
384 for (i
= 0; i
< WXSIZEOF(files
); i
++)
385 wxRemoveFile(tmp
+ wxFileName(files
[i
], wxPATH_UNIX
).GetFullPath());
387 for (i
= 0; i
< WXSIZEOF(dirs
); i
++)
388 wxRmdir(tmp
+ wxFileName(dirs
[i
], wxPATH_UNIX
).GetFullPath());
391 wxLogSysError(_T("can't remove temporary dir '%s'"), m_tmp
.c_str());
395 ///////////////////////////////////////////////////////////////////////////////
396 // wxFFile streams for piping to/from an external program
398 #if defined __UNIX__ || defined __MINGW32__
399 # define WXARC_popen popen
400 # define WXARC_pclose pclose
401 #elif defined _MSC_VER || defined __BORLANDC__
402 # define WXARC_popen _popen
403 # define WXARC_pclose _pclose
405 # define WXARC_NO_POPEN
406 # define WXARC_popen(cmd, type) NULL
407 # define WXARC_pclose(fp)
416 PFileInputStream::PFileInputStream(const wxString
& cmd
)
417 : wxFFileInputStream(WXARC_popen(cmd
.mb_str(), "r" WXARC_b
))
421 PFileInputStream::~PFileInputStream()
423 WXARC_pclose(m_file
->fp()); m_file
->Detach();
426 PFileOutputStream::PFileOutputStream(const wxString
& cmd
)
427 : wxFFileOutputStream(WXARC_popen(cmd
.mb_str(), "w" WXARC_b
))
431 PFileOutputStream::~PFileOutputStream()
433 WXARC_pclose(m_file
->fp()); m_file
->Detach();
437 ///////////////////////////////////////////////////////////////////////////////
440 template <class ClassFactoryT
>
441 ArchiveTestCase
<ClassFactoryT
>::ArchiveTestCase(
443 ClassFactoryT
*factory
,
445 const wxString
& archiver
,
446 const wxString
& unarchiver
)
448 CppUnit::TestCase(TestId::MakeId() + name
),
451 m_timeStamp(1, wxDateTime::Mar
, 2004, 12, 0),
452 m_id(TestId::GetId()),
453 m_archiver(archiver
),
454 m_unarchiver(unarchiver
)
456 wxASSERT(m_factory
.get() != NULL
);
459 template <class ClassFactoryT
>
460 ArchiveTestCase
<ClassFactoryT
>::~ArchiveTestCase()
462 TestEntries::iterator it
;
463 for (it
= m_testEntries
.begin(); it
!= m_testEntries
.end(); ++it
)
467 template <class ClassFactoryT
>
468 void ArchiveTestCase
<ClassFactoryT
>::runTest()
470 TestOutputStream
out(m_options
);
474 if (m_archiver
.empty())
477 CreateArchive(out
, m_archiver
);
479 // check archive could be created
480 CPPUNIT_ASSERT(out
.GetLength() > 0);
482 TestInputStream
in(out
, m_id
% ((m_options
& PipeIn
) ? 4 : 3));
486 TestPairIterator(in
);
488 TestSmartIterator(in
);
490 TestSmartPairIterator(in
);
493 if ((m_options
& PipeIn
) == 0) {
494 ReadSimultaneous(in
);
498 ModifyArchive(in
, out
);
501 if (m_unarchiver
.empty())
504 ExtractArchive(in
, m_unarchiver
);
506 // check that all the test entries were found in the archive
507 CPPUNIT_ASSERT(m_testEntries
.empty());
510 template <class ClassFactoryT
>
511 void ArchiveTestCase
<ClassFactoryT
>::CreateTestData()
514 Add("text/empty", "");
515 Add("text/small", "Small text file for testing\n"
516 "archive streams in wxWidgets\n");
519 Add("bin/bin1000", 1000);
520 Add("bin/bin4095", 4095);
521 Add("bin/bin4096", 4096);
522 Add("bin/bin4097", 4097);
523 Add("bin/bin16384", 16384);
526 Add("zero/zero5", 5, 0);
527 Add("zero/zero1024", 1024, 109);
528 Add("zero/zero32768", 32768, 106);
529 Add("zero/zero16385", 16385, 119);
534 template <class ClassFactoryT
>
535 TestEntry
& ArchiveTestCase
<ClassFactoryT
>::Add(const char *name
,
541 TestEntry
*& entry
= m_testEntries
[wxString(name
, *wxConvCurrent
)];
542 wxASSERT(entry
== NULL
);
543 entry
= new TestEntry(m_timeStamp
, len
, data
);
544 m_timeStamp
+= wxTimeSpan(0, 1, 30);
548 template <class ClassFactoryT
>
549 TestEntry
& ArchiveTestCase
<ClassFactoryT
>::Add(const char *name
,
553 wxCharBuffer
buf(len
);
554 for (int i
= 0; i
< len
; i
++)
555 buf
.data()[i
] = (char)(value
== EOF
? rand() : value
);
556 return Add(name
, buf
, len
);
559 // Create an archive using the wx archive classes, write it to 'out'
561 template <class ClassFactoryT
>
562 void ArchiveTestCase
<ClassFactoryT
>::CreateArchive(wxOutputStream
& out
)
564 auto_ptr
<OutputStreamT
> arc(m_factory
->NewStream(out
));
565 TestEntries::iterator it
;
567 OnCreateArchive(*arc
);
569 // We want to try creating entries in various different ways, 'choices'
570 // is just a number used to select between all the various possibilities.
573 for (it
= m_testEntries
.begin(); it
!= m_testEntries
.end(); ++it
) {
575 TestEntry
& testEntry
= *it
->second
;
576 wxString name
= it
->first
;
578 // It should be possible to create a directory entry just by supplying
579 // a name that looks like a directory, or alternatively any old name
580 // can be identified as a directory using SetIsDir or PutNextDirEntry
581 bool setIsDir
= name
.Last() == _T('/') && (choices
& 1);
583 name
.erase(name
.length() - 1);
585 // provide some context for the error message so that we know which
586 // iteration of the loop we were on
587 string
error_entry((_T(" '") + name
+ _T("'")).mb_str());
588 string
error_context(" failed for entry" + error_entry
);
590 if ((choices
& 2) || testEntry
.IsText()) {
591 // try PutNextEntry(EntryT *pEntry)
592 auto_ptr
<EntryT
> entry(m_factory
->NewEntry());
593 entry
->SetName(name
, wxPATH_UNIX
);
596 entry
->SetDateTime(testEntry
.GetDateTime());
597 entry
->SetSize(testEntry
.GetLength());
598 OnCreateEntry(*arc
, testEntry
, entry
.get());
599 CPPUNIT_ASSERT_MESSAGE("PutNextEntry" + error_context
,
600 arc
->PutNextEntry(entry
.release()));
603 // try the convenience methods
604 OnCreateEntry(*arc
, testEntry
);
606 CPPUNIT_ASSERT_MESSAGE("PutNextDirEntry" + error_context
,
607 arc
->PutNextDirEntry(name
, testEntry
.GetDateTime()));
609 CPPUNIT_ASSERT_MESSAGE("PutNextEntry" + error_context
,
610 arc
->PutNextEntry(name
, testEntry
.GetDateTime(),
611 testEntry
.GetLength()));
614 if (it
->first
.Last() != _T('/')) {
615 // for non-dirs write the data
616 arc
->Write(testEntry
.GetData(), testEntry
.GetSize());
617 CPPUNIT_ASSERT_MESSAGE("LastWrite check" + error_context
,
618 arc
->LastWrite() == testEntry
.GetSize());
619 // should work with or without explicit CloseEntry
621 CPPUNIT_ASSERT_MESSAGE("CloseEntry" + error_context
,
625 CPPUNIT_ASSERT_MESSAGE("IsOk" + error_context
, arc
->IsOk());
628 // should work with or without explicit Close
630 CPPUNIT_ASSERT(arc
->Close());
633 // Create an archive using an external archive program
635 template <class ClassFactoryT
>
636 void ArchiveTestCase
<ClassFactoryT
>::CreateArchive(wxOutputStream
& out
,
637 const wxString
& archiver
)
639 // for an external archiver the test data need to be written to
644 TestEntries::iterator i
;
645 for (i
= m_testEntries
.begin(); i
!= m_testEntries
.end(); ++i
) {
646 wxFileName
fn(i
->first
, wxPATH_UNIX
);
647 TestEntry
& entry
= *i
->second
;
650 wxFileName::Mkdir(fn
.GetPath(), 0777, wxPATH_MKDIR_FULL
);
652 wxFileName::Mkdir(fn
.GetPath(), 0777, wxPATH_MKDIR_FULL
);
653 wxFFileOutputStream
fileout(fn
.GetFullPath());
654 fileout
.Write(entry
.GetData(), entry
.GetSize());
658 for (i
= m_testEntries
.begin(); i
!= m_testEntries
.end(); ++i
) {
659 wxFileName
fn(i
->first
, wxPATH_UNIX
);
660 TestEntry
& entry
= *i
->second
;
661 wxDateTime dt
= entry
.GetDateTime();
664 entry
.SetDateTime(wxDateTime());
667 fn
.SetTimes(NULL
, &dt
, NULL
);
670 if ((m_options
& PipeOut
) == 0) {
671 wxFileName
fn(tmpdir
.GetName());
672 fn
.SetExt(_T("arc"));
673 wxString tmparc
= fn
.GetPath(wxPATH_GET_SEPARATOR
) + fn
.GetFullName();
675 // call the archiver to create an archive file
676 system(wxString::Format(archiver
, tmparc
.c_str()).mb_str());
678 // then load the archive file
680 wxFFileInputStream
in(tmparc
);
685 wxRemoveFile(tmparc
);
688 // for the non-seekable test, have the archiver output to "-"
689 // and read the archive via a pipe
690 PFileInputStream
in(wxString::Format(archiver
, _T("-")));
696 // Do a standard set of modification on an archive, delete an entry,
697 // rename an entry and add an entry
699 template <class ClassFactoryT
>
700 void ArchiveTestCase
<ClassFactoryT
>::ModifyArchive(wxInputStream
& in
,
703 auto_ptr
<InputStreamT
> arcIn(m_factory
->NewStream(in
));
704 auto_ptr
<OutputStreamT
> arcOut(m_factory
->NewStream(out
));
707 const wxString deleteName
= _T("bin/bin1000");
708 const wxString renameFrom
= _T("zero/zero1024");
709 const wxString renameTo
= _T("zero/newname");
710 const wxString newName
= _T("newfile");
711 const char *newData
= "New file added as a test\n";
713 arcOut
->CopyArchiveMetaData(*arcIn
);
715 while ((pEntry
= arcIn
->GetNextEntry()) != NULL
) {
716 auto_ptr
<EntryT
> entry(pEntry
);
717 OnSetNotifier(*entry
);
718 wxString name
= entry
->GetName(wxPATH_UNIX
);
720 // provide some context for the error message so that we know which
721 // iteration of the loop we were on
722 string
error_entry((_T(" '") + name
+ _T("'")).mb_str());
723 string
error_context(" failed for entry" + error_entry
);
725 if (name
== deleteName
) {
726 TestEntries::iterator it
= m_testEntries
.find(name
);
727 CPPUNIT_ASSERT_MESSAGE(
728 "deletion failed (already deleted?) for" + error_entry
,
729 it
!= m_testEntries
.end());
730 TestEntry
*p
= it
->second
;
731 m_testEntries
.erase(it
);
735 if (name
== renameFrom
) {
736 entry
->SetName(renameTo
);
737 TestEntries::iterator it
= m_testEntries
.find(renameFrom
);
738 CPPUNIT_ASSERT_MESSAGE(
739 "rename failed (already renamed?) for" + error_entry
,
740 it
!= m_testEntries
.end());
741 TestEntry
*p
= it
->second
;
742 m_testEntries
.erase(it
);
743 m_testEntries
[renameTo
] = p
;
746 CPPUNIT_ASSERT_MESSAGE("CopyEntry" + error_context
,
747 arcOut
->CopyEntry(entry
.release(), *arcIn
));
751 // check that the deletion and rename were done
752 CPPUNIT_ASSERT(m_testEntries
.count(deleteName
) == 0);
753 CPPUNIT_ASSERT(m_testEntries
.count(renameFrom
) == 0);
754 CPPUNIT_ASSERT(m_testEntries
.count(renameTo
) == 1);
756 // check that the end of the input archive was reached without error
757 CPPUNIT_ASSERT(arcIn
->Eof());
759 // try adding a new entry
760 TestEntry
& testEntry
= Add(newName
.mb_str(), newData
);
761 auto_ptr
<EntryT
> newentry(m_factory
->NewEntry());
762 newentry
->SetName(newName
);
763 newentry
->SetDateTime(testEntry
.GetDateTime());
764 newentry
->SetSize(testEntry
.GetLength());
765 OnCreateEntry(*arcOut
, testEntry
, newentry
.get());
766 OnSetNotifier(*newentry
);
767 CPPUNIT_ASSERT(arcOut
->PutNextEntry(newentry
.release()));
768 CPPUNIT_ASSERT(arcOut
->Write(newData
, strlen(newData
)).IsOk());
770 // should work with or without explicit Close
772 CPPUNIT_ASSERT(arcOut
->Close());
775 // Extract an archive using the wx archive classes
777 template <class ClassFactoryT
>
778 void ArchiveTestCase
<ClassFactoryT
>::ExtractArchive(wxInputStream
& in
)
780 typedef Ptr
<EntryT
> EntryPtr
;
781 typedef std::list
<EntryPtr
> Entries
;
782 typedef typename
Entries::iterator EntryIter
;
784 auto_ptr
<InputStreamT
> arc(m_factory
->NewStream(in
));
785 int expectedTotal
= m_testEntries
.size();
789 if ((m_options
& PipeIn
) == 0)
790 OnArchiveExtracted(*arc
, expectedTotal
);
792 while (entry
= EntryPtr(arc
->GetNextEntry()), entry
.get() != NULL
) {
793 wxString name
= entry
->GetName(wxPATH_UNIX
);
795 // provide some context for the error message so that we know which
796 // iteration of the loop we were on
797 string
error_entry((_T(" '") + name
+ _T("'")).mb_str());
798 string
error_context(" failed for entry" + error_entry
);
800 TestEntries::iterator it
= m_testEntries
.find(name
);
801 CPPUNIT_ASSERT_MESSAGE(
802 "archive contains an entry that shouldn't be there" + error_entry
,
803 it
!= m_testEntries
.end());
805 const TestEntry
& testEntry
= *it
->second
;
808 // On Windows some archivers compensate for Windows DST handling, but
809 // other don't, so disable the test for now.
810 wxDateTime dt
= testEntry
.GetDateTime();
812 CPPUNIT_ASSERT_MESSAGE("timestamp check" + error_context
,
813 dt
== entry
->GetDateTime());
816 // non-seekable entries are allowed to have GetSize == wxInvalidOffset
817 // until the end of the entry's data has been read past
818 CPPUNIT_ASSERT_MESSAGE("entry size check" + error_context
,
819 testEntry
.GetLength() == entry
->GetSize() ||
820 ((m_options
& PipeIn
) != 0 && entry
->GetSize() == wxInvalidOffset
));
821 CPPUNIT_ASSERT_MESSAGE(
822 "arc->GetLength() == entry->GetSize()" + error_context
,
823 arc
->GetLength() == entry
->GetSize());
825 if (name
.Last() != _T('/'))
827 CPPUNIT_ASSERT_MESSAGE("!IsDir" + error_context
,
829 wxCharBuffer
buf(testEntry
.GetSize() + 1);
830 CPPUNIT_ASSERT_MESSAGE("Read until Eof" + error_context
,
831 arc
->Read(buf
.data(), testEntry
.GetSize() + 1).Eof());
832 CPPUNIT_ASSERT_MESSAGE("LastRead check" + error_context
,
833 arc
->LastRead() == testEntry
.GetSize());
834 CPPUNIT_ASSERT_MESSAGE("data compare" + error_context
,
835 !memcmp(buf
.data(), testEntry
.GetData(), testEntry
.GetSize()));
837 CPPUNIT_ASSERT_MESSAGE("IsDir" + error_context
, entry
->IsDir());
840 // GetSize() must return the right result in all cases after all the
841 // data has been read
842 CPPUNIT_ASSERT_MESSAGE("entry size check" + error_context
,
843 testEntry
.GetLength() == entry
->GetSize());
844 CPPUNIT_ASSERT_MESSAGE(
845 "arc->GetLength() == entry->GetSize()" + error_context
,
846 arc
->GetLength() == entry
->GetSize());
848 if ((m_options
& PipeIn
) == 0) {
849 OnEntryExtracted(*entry
, testEntry
, arc
.get());
851 m_testEntries
.erase(it
);
853 entries
.push_back(entry
);
857 // check that the end of the input archive was reached without error
858 CPPUNIT_ASSERT(arc
->Eof());
860 // for non-seekable streams these data are only guaranteed to be
861 // available once the end of the archive has been reached
862 if (m_options
& PipeIn
) {
863 for (EntryIter i
= entries
.begin(); i
!= entries
.end(); ++i
) {
864 wxString name
= (*i
)->GetName(wxPATH_UNIX
);
865 TestEntries::iterator j
= m_testEntries
.find(name
);
866 OnEntryExtracted(**i
, *j
->second
);
868 m_testEntries
.erase(j
);
870 OnArchiveExtracted(*arc
, expectedTotal
);
874 // Extract an archive using an external unarchive program
876 template <class ClassFactoryT
>
877 void ArchiveTestCase
<ClassFactoryT
>::ExtractArchive(wxInputStream
& in
,
878 const wxString
& unarchiver
)
880 // for an external unarchiver, unarchive to a tempdir
883 if ((m_options
& PipeIn
) == 0) {
884 wxFileName
fn(tmpdir
.GetName());
885 fn
.SetExt(_T("arc"));
886 wxString tmparc
= fn
.GetPath(wxPATH_GET_SEPARATOR
) + fn
.GetFullName();
888 if (m_options
& Stub
)
889 in
.SeekI(STUB_SIZE
* 2);
891 // write the archive to a temporary file
893 wxFFileOutputStream
out(tmparc
);
899 system(wxString::Format(unarchiver
, tmparc
.c_str()).mb_str());
900 wxRemoveFile(tmparc
);
903 // for the non-seekable test, have the archiver extract "-" and
904 // feed it the archive via a pipe
905 PFileOutputStream
out(wxString::Format(unarchiver
, _T("-")));
910 wxString dir
= tmpdir
.GetName();
914 // Verifies the files produced by an external unarchiver are as expected
916 template <class ClassFactoryT
>
917 void ArchiveTestCase
<ClassFactoryT
>::VerifyDir(wxString
& path
,
918 size_t rootlen
/*=0*/)
921 path
+= wxFileName::GetPathSeparator();
922 int pos
= path
.length();
928 if (dir
.Open(path
) && dir
.GetFirst(&name
)) {
930 path
.replace(pos
, wxString::npos
, name
);
931 name
= m_factory
->GetInternalName(
932 path
.substr(rootlen
, wxString::npos
));
934 bool isDir
= wxDirExists(path
);
938 // provide some context for the error message so that we know which
939 // iteration of the loop we were on
940 string
error_entry((_T(" '") + name
+ _T("'")).mb_str());
941 string
error_context(" failed for entry" + error_entry
);
943 TestEntries::iterator it
= m_testEntries
.find(name
);
944 CPPUNIT_ASSERT_MESSAGE(
945 "archive contains an entry that shouldn't be there"
947 it
!= m_testEntries
.end());
949 const TestEntry
& testEntry
= *it
->second
;
951 #if 0 //ndef __WXMSW__
952 CPPUNIT_ASSERT_MESSAGE("timestamp check" + error_context
,
953 testEntry
.GetDateTime() ==
954 wxFileName(path
).GetModificationTime());
957 wxFFileInputStream
in(path
);
958 CPPUNIT_ASSERT_MESSAGE(
959 "entry not found in archive" + error_entry
, in
.Ok());
961 size_t size
= (size_t)in
.GetLength();
962 wxCharBuffer
buf(size
);
963 CPPUNIT_ASSERT_MESSAGE("Read" + error_context
,
964 in
.Read(buf
.data(), size
).LastRead() == size
);
965 CPPUNIT_ASSERT_MESSAGE("size check" + error_context
,
966 testEntry
.GetSize() == size
);
967 CPPUNIT_ASSERT_MESSAGE("data compare" + error_context
,
968 memcmp(buf
.data(), testEntry
.GetData(), size
) == 0);
971 VerifyDir(path
, rootlen
);
975 m_testEntries
.erase(it
);
977 while (dir
.GetNext(&name
));
981 // test the simple iterators that give away ownership of an entry
983 template <class ClassFactoryT
>
984 void ArchiveTestCase
<ClassFactoryT
>::TestIterator(wxInputStream
& in
)
986 typedef std::list
<EntryT
*> ArchiveCatalog
;
987 typedef typename
ArchiveCatalog::iterator CatalogIter
;
989 auto_ptr
<InputStreamT
> arc(m_factory
->NewStream(in
));
992 #ifdef WXARC_MEMBER_TEMPLATES
993 ArchiveCatalog
cat((IterT
)*arc
, IterT());
996 for (IterT
i(*arc
); i
!= IterT(); ++i
)
1000 for (CatalogIter it
= cat
.begin(); it
!= cat
.end(); ++it
) {
1001 auto_ptr
<EntryT
> entry(*it
);
1002 count
+= m_testEntries
.count(entry
->GetName(wxPATH_UNIX
));
1005 CPPUNIT_ASSERT(m_testEntries
.size() == cat
.size());
1006 CPPUNIT_ASSERT(count
== cat
.size());
1009 // test the pair iterators that can be used to load a std::map or wxHashMap
1010 // these also give away ownership of entries
1012 template <class ClassFactoryT
>
1013 void ArchiveTestCase
<ClassFactoryT
>::TestPairIterator(wxInputStream
& in
)
1015 typedef std::map
<wxString
, EntryT
*> ArchiveCatalog
;
1016 typedef typename
ArchiveCatalog::iterator CatalogIter
;
1018 auto_ptr
<InputStreamT
> arc(m_factory
->NewStream(in
));
1021 #ifdef WXARC_MEMBER_TEMPLATES
1022 ArchiveCatalog
cat((PairIterT
)*arc
, PairIterT());
1025 for (PairIterT
i(*arc
); i
!= PairIterT(); ++i
)
1029 for (CatalogIter it
= cat
.begin(); it
!= cat
.end(); ++it
) {
1030 auto_ptr
<EntryT
> entry(it
->second
);
1031 count
+= m_testEntries
.count(entry
->GetName(wxPATH_UNIX
));
1034 CPPUNIT_ASSERT(m_testEntries
.size() == cat
.size());
1035 CPPUNIT_ASSERT(count
== cat
.size());
1038 // simple iterators using smart pointers, no need to worry about ownership
1040 template <class ClassFactoryT
>
1041 void ArchiveTestCase
<ClassFactoryT
>::TestSmartIterator(wxInputStream
& in
)
1043 typedef std::list
<Ptr
<EntryT
> > ArchiveCatalog
;
1044 typedef typename
ArchiveCatalog::iterator CatalogIter
;
1045 typedef wxArchiveIterator
<InputStreamT
, Ptr
<EntryT
> > Iter
;
1047 auto_ptr
<InputStreamT
> arc(m_factory
->NewStream(in
));
1049 #ifdef WXARC_MEMBER_TEMPLATES
1050 ArchiveCatalog
cat((Iter
)*arc
, Iter());
1053 for (Iter
i(*arc
); i
!= Iter(); ++i
)
1057 CPPUNIT_ASSERT(m_testEntries
.size() == cat
.size());
1059 for (CatalogIter it
= cat
.begin(); it
!= cat
.end(); ++it
)
1060 CPPUNIT_ASSERT(m_testEntries
.count((*it
)->GetName(wxPATH_UNIX
)));
1063 // pair iterator using smart pointers
1065 template <class ClassFactoryT
>
1066 void ArchiveTestCase
<ClassFactoryT
>::TestSmartPairIterator(wxInputStream
& in
)
1068 #if defined _MSC_VER && defined _MSC_VER < 1200
1069 // With VC++ 5.0 the '=' operator of std::pair breaks when the second
1070 // type is Ptr<EntryT>, so this iterator can't be made to work.
1073 typedef std::map
<wxString
, Ptr
<EntryT
> > ArchiveCatalog
;
1074 typedef typename
ArchiveCatalog::iterator CatalogIter
;
1075 typedef wxArchiveIterator
<InputStreamT
,
1076 std::pair
<wxString
, Ptr
<EntryT
> > > PairIter
;
1078 auto_ptr
<InputStreamT
> arc(m_factory
->NewStream(in
));
1080 #ifdef WXARC_MEMBER_TEMPLATES
1081 ArchiveCatalog
cat((PairIter
)*arc
, PairIter());
1084 for (PairIter
i(*arc
); i
!= PairIter(); ++i
)
1088 CPPUNIT_ASSERT(m_testEntries
.size() == cat
.size());
1090 for (CatalogIter it
= cat
.begin(); it
!= cat
.end(); ++it
)
1091 CPPUNIT_ASSERT(m_testEntries
.count(it
->second
->GetName(wxPATH_UNIX
)));
1095 // try reading two entries at the same time
1097 template <class ClassFactoryT
>
1098 void ArchiveTestCase
<ClassFactoryT
>::ReadSimultaneous(TestInputStream
& in
)
1100 typedef std::map
<wxString
, Ptr
<EntryT
> > ArchiveCatalog
;
1101 typedef wxArchiveIterator
<InputStreamT
,
1102 std::pair
<wxString
, Ptr
<EntryT
> > > PairIter
;
1104 // create two archive input streams
1105 TestInputStream
in2(in
);
1106 auto_ptr
<InputStreamT
> arc(m_factory
->NewStream(in
));
1107 auto_ptr
<InputStreamT
> arc2(m_factory
->NewStream(in2
));
1110 #ifdef WXARC_MEMBER_TEMPLATES
1111 ArchiveCatalog
cat((PairIter
)*arc
, PairIter());
1114 for (PairIter
i(*arc
); i
!= PairIter(); ++i
)
1118 // the names of two entries to read
1119 const wxChar
*name
= _T("text/small");
1120 const wxChar
*name2
= _T("bin/bin1000");
1123 typename
ArchiveCatalog::iterator j
;
1124 CPPUNIT_ASSERT((j
= cat
.find(name
)) != cat
.end());
1125 CPPUNIT_ASSERT(arc
->OpenEntry(*j
->second
));
1126 CPPUNIT_ASSERT((j
= cat
.find(name2
)) != cat
.end());
1127 CPPUNIT_ASSERT(arc2
->OpenEntry(*j
->second
));
1129 // get pointers to the expected data
1130 TestEntries::iterator k
;
1131 CPPUNIT_ASSERT((k
= m_testEntries
.find(name
)) != m_testEntries
.end());
1132 TestEntry
*entry
= k
->second
;
1133 CPPUNIT_ASSERT((k
= m_testEntries
.find(name2
)) != m_testEntries
.end());
1134 TestEntry
*entry2
= k
->second
;
1136 size_t count
= 0, count2
= 0;
1137 size_t size
= entry
->GetSize(), size2
= entry2
->GetSize();
1138 const char *data
= entry
->GetData(), *data2
= entry2
->GetData();
1140 // read and check the two entries in parallel, character by character
1141 while (arc
->IsOk() || arc2
->IsOk()) {
1142 char ch
= arc
->GetC();
1143 if (arc
->LastRead() == 1) {
1144 CPPUNIT_ASSERT(count
< size
);
1145 CPPUNIT_ASSERT(ch
== data
[count
++]);
1147 char ch2
= arc2
->GetC();
1148 if (arc2
->LastRead() == 1) {
1149 CPPUNIT_ASSERT(count2
< size2
);
1150 CPPUNIT_ASSERT(ch2
== data2
[count2
++]);
1154 CPPUNIT_ASSERT(arc
->Eof());
1155 CPPUNIT_ASSERT(arc2
->Eof());
1156 CPPUNIT_ASSERT(count
== size
);
1157 CPPUNIT_ASSERT(count2
== size2
);
1160 // Nothing useful can be done with a generic notifier yet, so just test one
1163 template <class NotifierT
, class EntryT
>
1164 class ArchiveNotifier
: public NotifierT
1167 void OnEntryUpdated(EntryT
& WXUNUSED(entry
)) { }
1170 template <class ClassFactoryT
>
1171 void ArchiveTestCase
<ClassFactoryT
>::OnSetNotifier(EntryT
& entry
)
1173 static ArchiveNotifier
<NotifierT
, EntryT
> notifier
;
1174 entry
.SetNotifier(notifier
);
1178 ///////////////////////////////////////////////////////////////////////////////
1179 // An additional case to check that reading corrupt archives doesn't crash
1181 class CorruptionTestCase
: public CppUnit::TestCase
1184 CorruptionTestCase(std::string name
,
1185 wxArchiveClassFactory
*factory
,
1187 : CppUnit::TestCase(TestId::MakeId() + name
),
1193 // the entry point for the test
1196 void CreateArchive(wxOutputStream
& out
);
1197 void ExtractArchive(wxInputStream
& in
);
1199 auto_ptr
<wxArchiveClassFactory
> m_factory
; // factory to make classes
1200 int m_options
; // test options
1203 void CorruptionTestCase::runTest()
1205 TestOutputStream
out(m_options
);
1207 TestInputStream
in(out
, 0);
1208 wxFileOffset len
= in
.GetLength();
1210 // try flipping one byte in the archive
1212 for (pos
= 0; pos
< len
; pos
++) {
1220 // try zeroing one byte in the archive
1221 for (pos
= 0; pos
< len
; pos
++) {
1229 // try chopping the archive off
1230 for (int size
= 1; size
<= len
; size
++) {
1237 void CorruptionTestCase::CreateArchive(wxOutputStream
& out
)
1239 auto_ptr
<wxArchiveOutputStream
> arc(m_factory
->NewStream(out
));
1241 arc
->PutNextDirEntry(_T("dir"));
1242 arc
->PutNextEntry(_T("file"));
1243 arc
->Write(_T("foo"), 3);
1246 void CorruptionTestCase::ExtractArchive(wxInputStream
& in
)
1248 auto_ptr
<wxArchiveInputStream
> arc(m_factory
->NewStream(in
));
1249 auto_ptr
<wxArchiveEntry
> entry(arc
->GetNextEntry());
1251 while (entry
.get() != NULL
) {
1255 arc
->Read(buf
, sizeof(buf
));
1257 auto_ptr
<wxArchiveEntry
> next(arc
->GetNextEntry());
1263 ///////////////////////////////////////////////////////////////////////////////
1266 int TestId::m_seed
= 6219;
1269 string
TestId::MakeId()
1271 m_seed
= (m_seed
* 171) % 30269;
1272 return string(wxString::Format(_T("%-6d"), m_seed
).mb_str());
1276 ///////////////////////////////////////////////////////////////////////////////
1279 ArchiveTestSuite::ArchiveTestSuite(string name
)
1280 : CppUnit::TestSuite("archive/" + name
),
1281 m_name(name
.c_str(), *wxConvCurrent
)
1283 m_name
= _T("wx") + m_name
.Left(1).Upper() + m_name
.Mid(1).Lower();
1284 m_path
.AddEnvList(_T("PATH"));
1285 m_archivers
.push_back(_T(""));
1286 m_unarchivers
.push_back(_T(""));
1289 // add the command for an external archiver to the list, testing for it in
1292 void ArchiveTestSuite::AddCmd(wxArrayString
& cmdlist
, const wxString
& cmd
)
1295 cmdlist
.push_back(cmd
);
1298 bool ArchiveTestSuite::IsInPath(const wxString
& cmd
)
1300 wxString c
= cmd
.BeforeFirst(_T(' '));
1304 return !m_path
.FindValidPath(c
).empty();
1307 // make the test suite
1309 ArchiveTestSuite
*ArchiveTestSuite::makeSuite()
1311 typedef wxArrayString::iterator Iter
;
1313 for (int generic
= 0; generic
< 2; generic
++)
1314 for (Iter i
= m_unarchivers
.begin(); i
!= m_unarchivers
.end(); ++i
)
1315 for (Iter j
= m_archivers
.begin(); j
!= m_archivers
.end(); ++j
)
1316 for (int options
= 0; options
<= AllOptions
; options
++)
1318 #ifdef WXARC_NO_POPEN
1319 // if no popen then can't pipe in/out of archiver
1320 if ((options
& PipeIn
) && !i
->empty())
1322 if ((options
& PipeOut
) && !j
->empty())
1325 string descr
= Description(m_name
, options
,
1326 generic
!= 0, *j
, *i
);
1328 CppUnit::Test
*test
= makeTest(descr
, options
,
1329 generic
!= 0, *j
, *i
);
1335 for (int options
= 0; options
<= PipeIn
; options
+= PipeIn
)
1337 wxObject
*pObj
= wxCreateDynamicObject(m_name
+ _T("ClassFactory"));
1338 wxArchiveClassFactory
*factory
;
1339 factory
= wxDynamicCast(pObj
, wxArchiveClassFactory
);
1342 string
descr(m_name
.mb_str());
1343 descr
= "CorruptionTestCase (" + descr
+ ")";
1346 descr
+= " (PipeIn)";
1348 addTest(new CorruptionTestCase(descr
, factory
, options
));
1355 CppUnit::Test
*ArchiveTestSuite::makeTest(
1356 string
WXUNUSED(descr
),
1357 int WXUNUSED(options
),
1358 bool WXUNUSED(genericInterface
),
1359 const wxString
& WXUNUSED(archiver
),
1360 const wxString
& WXUNUSED(unarchiver
))
1365 // make a display string for the option bits
1367 string
ArchiveTestSuite::Description(const wxString
& type
,
1369 bool genericInterface
,
1370 const wxString
& archiver
,
1371 const wxString
& unarchiver
)
1375 if (genericInterface
)
1376 descr
<< _T("wxArchive (") << type
<< _T(")");
1380 if (!archiver
.empty()) {
1381 const wxChar
*fn
= (options
& PipeOut
) != 0 ? _T("-") : _T("file");
1382 descr
<< _T(" (") << wxString::Format(archiver
, fn
) << _T(")");
1384 if (!unarchiver
.empty()) {
1385 const wxChar
*fn
= (options
& PipeIn
) != 0 ? _T("-") : _T("file");
1386 descr
<< _T(" (") << wxString::Format(unarchiver
, fn
) << _T(")");
1391 if ((options
& PipeIn
) != 0)
1392 optstr
+= _T("|PipeIn");
1393 if ((options
& PipeOut
) != 0)
1394 optstr
+= _T("|PipeOut");
1395 if ((options
& Stub
) != 0)
1396 optstr
+= _T("|Stub");
1397 if (!optstr
.empty())
1398 optstr
= _T(" (") + optstr
.substr(1) + _T(")");
1402 return string(descr
.mb_str());
1406 ///////////////////////////////////////////////////////////////////////////////
1409 template class ArchiveTestCase
<wxArchiveClassFactory
>;
1412 #include "wx/zipstrm.h"
1413 template class ArchiveTestCase
<wxZipClassFactory
>;
1417 #include "wx/tarstrm.h"
1418 template class ArchiveTestCase
<wxTarClassFactory
>;
1421 #endif // wxUSE_STREAMS && wxUSE_ARCHIVE_STREAMS