]>
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(wxT("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 wxT("remove '") + path
+ wxT("' fails safety check"));
361 const wxChar
*files
[] = {
370 wxT("zero/zero1024"),
371 wxT("zero/zero32768"),
372 wxT("zero/zero16385"),
377 const wxChar
*dirs
[] = {
378 wxT("text/"), wxT("bin/"), wxT("zero/"), wxT("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());
392 wxLogSysError(wxT("can't remove temporary dir '%s'"), m_tmp
.c_str());
397 ///////////////////////////////////////////////////////////////////////////////
398 // wxFFile streams for piping to/from an external program
400 #if defined __UNIX__ || defined __MINGW32__
401 # define WXARC_popen popen
402 # define WXARC_pclose pclose
403 #elif defined _MSC_VER || defined __BORLANDC__
404 # define WXARC_popen _popen
405 # define WXARC_pclose _pclose
407 # define WXARC_NO_POPEN
408 # define WXARC_popen(cmd, type) NULL
409 # define WXARC_pclose(fp)
418 PFileInputStream::PFileInputStream(const wxString
& cmd
)
419 : wxFFileInputStream(WXARC_popen(cmd
.mb_str(), "r" WXARC_b
))
423 PFileInputStream::~PFileInputStream()
425 WXARC_pclose(m_file
->fp()); m_file
->Detach();
428 PFileOutputStream::PFileOutputStream(const wxString
& cmd
)
429 : wxFFileOutputStream(WXARC_popen(cmd
.mb_str(), "w" WXARC_b
))
433 PFileOutputStream::~PFileOutputStream()
435 WXARC_pclose(m_file
->fp()); m_file
->Detach();
439 ///////////////////////////////////////////////////////////////////////////////
442 template <class ClassFactoryT
>
443 ArchiveTestCase
<ClassFactoryT
>::ArchiveTestCase(
445 ClassFactoryT
*factory
,
447 const wxString
& archiver
,
448 const wxString
& unarchiver
)
450 CppUnit::TestCase(TestId::MakeId() + name
),
453 m_timeStamp(1, wxDateTime::Mar
, 2004, 12, 0),
454 m_id(TestId::GetId()),
455 m_archiver(archiver
),
456 m_unarchiver(unarchiver
)
458 wxASSERT(m_factory
.get() != NULL
);
461 template <class ClassFactoryT
>
462 ArchiveTestCase
<ClassFactoryT
>::~ArchiveTestCase()
464 TestEntries::iterator it
;
465 for (it
= m_testEntries
.begin(); it
!= m_testEntries
.end(); ++it
)
469 template <class ClassFactoryT
>
470 void ArchiveTestCase
<ClassFactoryT
>::runTest()
472 TestOutputStream
out(m_options
);
476 if (m_archiver
.empty())
479 CreateArchive(out
, m_archiver
);
481 // check archive could be created
482 CPPUNIT_ASSERT(out
.GetLength() > 0);
484 TestInputStream
in(out
, m_id
% ((m_options
& PipeIn
) ? 4 : 3));
488 TestPairIterator(in
);
490 TestSmartIterator(in
);
492 TestSmartPairIterator(in
);
495 if ((m_options
& PipeIn
) == 0) {
496 ReadSimultaneous(in
);
500 ModifyArchive(in
, out
);
503 if (m_unarchiver
.empty())
506 ExtractArchive(in
, m_unarchiver
);
508 // check that all the test entries were found in the archive
509 CPPUNIT_ASSERT(m_testEntries
.empty());
512 template <class ClassFactoryT
>
513 void ArchiveTestCase
<ClassFactoryT
>::CreateTestData()
516 Add("text/empty", "");
517 Add("text/small", "Small text file for testing\n"
518 "archive streams in wxWidgets\n");
521 Add("bin/bin1000", 1000);
522 Add("bin/bin4095", 4095);
523 Add("bin/bin4096", 4096);
524 Add("bin/bin4097", 4097);
525 Add("bin/bin16384", 16384);
528 Add("zero/zero5", 5, 0);
529 Add("zero/zero1024", 1024, 109);
530 Add("zero/zero32768", 32768, 106);
531 Add("zero/zero16385", 16385, 119);
536 template <class ClassFactoryT
>
537 TestEntry
& ArchiveTestCase
<ClassFactoryT
>::Add(const char *name
,
543 TestEntry
*& entry
= m_testEntries
[wxString(name
, *wxConvCurrent
)];
544 wxASSERT(entry
== NULL
);
545 entry
= new TestEntry(m_timeStamp
, len
, data
);
546 m_timeStamp
+= wxTimeSpan(0, 1, 30);
550 template <class ClassFactoryT
>
551 TestEntry
& ArchiveTestCase
<ClassFactoryT
>::Add(const char *name
,
555 wxCharBuffer
buf(len
);
556 for (int i
= 0; i
< len
; i
++)
557 buf
.data()[i
] = (char)(value
== EOF
? rand() : value
);
558 return Add(name
, buf
, len
);
561 // Create an archive using the wx archive classes, write it to 'out'
563 template <class ClassFactoryT
>
564 void ArchiveTestCase
<ClassFactoryT
>::CreateArchive(wxOutputStream
& out
)
566 auto_ptr
<OutputStreamT
> arc(m_factory
->NewStream(out
));
567 TestEntries::iterator it
;
569 OnCreateArchive(*arc
);
571 // We want to try creating entries in various different ways, 'choices'
572 // is just a number used to select between all the various possibilities.
575 for (it
= m_testEntries
.begin(); it
!= m_testEntries
.end(); ++it
) {
577 TestEntry
& testEntry
= *it
->second
;
578 wxString name
= it
->first
;
580 // It should be possible to create a directory entry just by supplying
581 // a name that looks like a directory, or alternatively any old name
582 // can be identified as a directory using SetIsDir or PutNextDirEntry
583 bool setIsDir
= name
.Last() == wxT('/') && (choices
& 1);
585 name
.erase(name
.length() - 1);
587 // provide some context for the error message so that we know which
588 // iteration of the loop we were on
589 string
error_entry((wxT(" '") + name
+ wxT("'")).mb_str());
590 string
error_context(" failed for entry" + error_entry
);
592 if ((choices
& 2) || testEntry
.IsText()) {
593 // try PutNextEntry(EntryT *pEntry)
594 auto_ptr
<EntryT
> entry(m_factory
->NewEntry());
595 entry
->SetName(name
, wxPATH_UNIX
);
598 entry
->SetDateTime(testEntry
.GetDateTime());
599 entry
->SetSize(testEntry
.GetLength());
600 OnCreateEntry(*arc
, testEntry
, entry
.get());
601 CPPUNIT_ASSERT_MESSAGE("PutNextEntry" + error_context
,
602 arc
->PutNextEntry(entry
.release()));
605 // try the convenience methods
606 OnCreateEntry(*arc
, testEntry
);
608 CPPUNIT_ASSERT_MESSAGE("PutNextDirEntry" + error_context
,
609 arc
->PutNextDirEntry(name
, testEntry
.GetDateTime()));
611 CPPUNIT_ASSERT_MESSAGE("PutNextEntry" + error_context
,
612 arc
->PutNextEntry(name
, testEntry
.GetDateTime(),
613 testEntry
.GetLength()));
616 if (it
->first
.Last() != wxT('/')) {
617 // for non-dirs write the data
618 arc
->Write(testEntry
.GetData(), testEntry
.GetSize());
619 CPPUNIT_ASSERT_MESSAGE("LastWrite check" + error_context
,
620 arc
->LastWrite() == testEntry
.GetSize());
621 // should work with or without explicit CloseEntry
623 CPPUNIT_ASSERT_MESSAGE("CloseEntry" + error_context
,
627 CPPUNIT_ASSERT_MESSAGE("IsOk" + error_context
, arc
->IsOk());
630 // should work with or without explicit Close
632 CPPUNIT_ASSERT(arc
->Close());
635 // Create an archive using an external archive program
637 template <class ClassFactoryT
>
638 void ArchiveTestCase
<ClassFactoryT
>::CreateArchive(wxOutputStream
& out
,
639 const wxString
& archiver
)
641 // for an external archiver the test data need to be written to
646 TestEntries::iterator i
;
647 for (i
= m_testEntries
.begin(); i
!= m_testEntries
.end(); ++i
) {
648 wxFileName
fn(i
->first
, wxPATH_UNIX
);
649 TestEntry
& entry
= *i
->second
;
652 wxFileName::Mkdir(fn
.GetPath(), 0777, wxPATH_MKDIR_FULL
);
654 wxFileName::Mkdir(fn
.GetPath(), 0777, wxPATH_MKDIR_FULL
);
655 wxFFileOutputStream
fileout(fn
.GetFullPath());
656 fileout
.Write(entry
.GetData(), entry
.GetSize());
660 for (i
= m_testEntries
.begin(); i
!= m_testEntries
.end(); ++i
) {
661 wxFileName
fn(i
->first
, wxPATH_UNIX
);
662 TestEntry
& entry
= *i
->second
;
663 wxDateTime dt
= entry
.GetDateTime();
666 entry
.SetDateTime(wxDateTime());
669 fn
.SetTimes(NULL
, &dt
, NULL
);
672 if ((m_options
& PipeOut
) == 0) {
673 wxFileName
fn(tmpdir
.GetName());
674 fn
.SetExt(wxT("arc"));
675 wxString tmparc
= fn
.GetPath(wxPATH_GET_SEPARATOR
) + fn
.GetFullName();
677 // call the archiver to create an archive file
678 system(wxString::Format(archiver
, tmparc
.c_str()).mb_str());
680 // then load the archive file
682 wxFFileInputStream
in(tmparc
);
687 wxRemoveFile(tmparc
);
690 // for the non-seekable test, have the archiver output to "-"
691 // and read the archive via a pipe
692 PFileInputStream
in(wxString::Format(archiver
, wxT("-")));
698 // Do a standard set of modification on an archive, delete an entry,
699 // rename an entry and add an entry
701 template <class ClassFactoryT
>
702 void ArchiveTestCase
<ClassFactoryT
>::ModifyArchive(wxInputStream
& in
,
705 auto_ptr
<InputStreamT
> arcIn(m_factory
->NewStream(in
));
706 auto_ptr
<OutputStreamT
> arcOut(m_factory
->NewStream(out
));
709 const wxString deleteName
= wxT("bin/bin1000");
710 const wxString renameFrom
= wxT("zero/zero1024");
711 const wxString renameTo
= wxT("zero/newname");
712 const wxString newName
= wxT("newfile");
713 const char *newData
= "New file added as a test\n";
715 arcOut
->CopyArchiveMetaData(*arcIn
);
717 while ((pEntry
= arcIn
->GetNextEntry()) != NULL
) {
718 auto_ptr
<EntryT
> entry(pEntry
);
719 OnSetNotifier(*entry
);
720 wxString name
= entry
->GetName(wxPATH_UNIX
);
722 // provide some context for the error message so that we know which
723 // iteration of the loop we were on
724 string
error_entry((wxT(" '") + name
+ wxT("'")).mb_str());
725 string
error_context(" failed for entry" + error_entry
);
727 if (name
== deleteName
) {
728 TestEntries::iterator it
= m_testEntries
.find(name
);
729 CPPUNIT_ASSERT_MESSAGE(
730 "deletion failed (already deleted?) for" + error_entry
,
731 it
!= m_testEntries
.end());
732 TestEntry
*p
= it
->second
;
733 m_testEntries
.erase(it
);
737 if (name
== renameFrom
) {
738 entry
->SetName(renameTo
);
739 TestEntries::iterator it
= m_testEntries
.find(renameFrom
);
740 CPPUNIT_ASSERT_MESSAGE(
741 "rename failed (already renamed?) for" + error_entry
,
742 it
!= m_testEntries
.end());
743 TestEntry
*p
= it
->second
;
744 m_testEntries
.erase(it
);
745 m_testEntries
[renameTo
] = p
;
748 CPPUNIT_ASSERT_MESSAGE("CopyEntry" + error_context
,
749 arcOut
->CopyEntry(entry
.release(), *arcIn
));
753 // check that the deletion and rename were done
754 CPPUNIT_ASSERT(m_testEntries
.count(deleteName
) == 0);
755 CPPUNIT_ASSERT(m_testEntries
.count(renameFrom
) == 0);
756 CPPUNIT_ASSERT(m_testEntries
.count(renameTo
) == 1);
758 // check that the end of the input archive was reached without error
759 CPPUNIT_ASSERT(arcIn
->Eof());
761 // try adding a new entry
762 TestEntry
& testEntry
= Add(newName
.mb_str(), newData
);
763 auto_ptr
<EntryT
> newentry(m_factory
->NewEntry());
764 newentry
->SetName(newName
);
765 newentry
->SetDateTime(testEntry
.GetDateTime());
766 newentry
->SetSize(testEntry
.GetLength());
767 OnCreateEntry(*arcOut
, testEntry
, newentry
.get());
768 OnSetNotifier(*newentry
);
769 CPPUNIT_ASSERT(arcOut
->PutNextEntry(newentry
.release()));
770 CPPUNIT_ASSERT(arcOut
->Write(newData
, strlen(newData
)).IsOk());
772 // should work with or without explicit Close
774 CPPUNIT_ASSERT(arcOut
->Close());
777 // Extract an archive using the wx archive classes
779 template <class ClassFactoryT
>
780 void ArchiveTestCase
<ClassFactoryT
>::ExtractArchive(wxInputStream
& in
)
782 typedef Ptr
<EntryT
> EntryPtr
;
783 typedef std::list
<EntryPtr
> Entries
;
784 typedef typename
Entries::iterator EntryIter
;
786 auto_ptr
<InputStreamT
> arc(m_factory
->NewStream(in
));
787 int expectedTotal
= m_testEntries
.size();
791 if ((m_options
& PipeIn
) == 0)
792 OnArchiveExtracted(*arc
, expectedTotal
);
794 while (entry
= EntryPtr(arc
->GetNextEntry()), entry
.get() != NULL
) {
795 wxString name
= entry
->GetName(wxPATH_UNIX
);
797 // provide some context for the error message so that we know which
798 // iteration of the loop we were on
799 string
error_entry((wxT(" '") + name
+ wxT("'")).mb_str());
800 string
error_context(" failed for entry" + error_entry
);
802 TestEntries::iterator it
= m_testEntries
.find(name
);
803 CPPUNIT_ASSERT_MESSAGE(
804 "archive contains an entry that shouldn't be there" + error_entry
,
805 it
!= m_testEntries
.end());
807 const TestEntry
& testEntry
= *it
->second
;
810 // On Windows some archivers compensate for Windows DST handling, but
811 // other don't, so disable the test for now.
812 wxDateTime dt
= testEntry
.GetDateTime();
814 CPPUNIT_ASSERT_MESSAGE("timestamp check" + error_context
,
815 dt
== entry
->GetDateTime());
818 // non-seekable entries are allowed to have GetSize == wxInvalidOffset
819 // until the end of the entry's data has been read past
820 CPPUNIT_ASSERT_MESSAGE("entry size check" + error_context
,
821 testEntry
.GetLength() == entry
->GetSize() ||
822 ((m_options
& PipeIn
) != 0 && entry
->GetSize() == wxInvalidOffset
));
823 CPPUNIT_ASSERT_MESSAGE(
824 "arc->GetLength() == entry->GetSize()" + error_context
,
825 arc
->GetLength() == entry
->GetSize());
827 if (name
.Last() != wxT('/'))
829 CPPUNIT_ASSERT_MESSAGE("!IsDir" + error_context
,
831 wxCharBuffer
buf(testEntry
.GetSize() + 1);
832 CPPUNIT_ASSERT_MESSAGE("Read until Eof" + error_context
,
833 arc
->Read(buf
.data(), testEntry
.GetSize() + 1).Eof());
834 CPPUNIT_ASSERT_MESSAGE("LastRead check" + error_context
,
835 arc
->LastRead() == testEntry
.GetSize());
836 CPPUNIT_ASSERT_MESSAGE("data compare" + error_context
,
837 !memcmp(buf
.data(), testEntry
.GetData(), testEntry
.GetSize()));
839 CPPUNIT_ASSERT_MESSAGE("IsDir" + error_context
, entry
->IsDir());
842 // GetSize() must return the right result in all cases after all the
843 // data has been read
844 CPPUNIT_ASSERT_MESSAGE("entry size check" + error_context
,
845 testEntry
.GetLength() == entry
->GetSize());
846 CPPUNIT_ASSERT_MESSAGE(
847 "arc->GetLength() == entry->GetSize()" + error_context
,
848 arc
->GetLength() == entry
->GetSize());
850 if ((m_options
& PipeIn
) == 0) {
851 OnEntryExtracted(*entry
, testEntry
, arc
.get());
853 m_testEntries
.erase(it
);
855 entries
.push_back(entry
);
859 // check that the end of the input archive was reached without error
860 CPPUNIT_ASSERT(arc
->Eof());
862 // for non-seekable streams these data are only guaranteed to be
863 // available once the end of the archive has been reached
864 if (m_options
& PipeIn
) {
865 for (EntryIter i
= entries
.begin(); i
!= entries
.end(); ++i
) {
866 wxString name
= (*i
)->GetName(wxPATH_UNIX
);
867 TestEntries::iterator j
= m_testEntries
.find(name
);
868 OnEntryExtracted(**i
, *j
->second
);
870 m_testEntries
.erase(j
);
872 OnArchiveExtracted(*arc
, expectedTotal
);
876 // Extract an archive using an external unarchive program
878 template <class ClassFactoryT
>
879 void ArchiveTestCase
<ClassFactoryT
>::ExtractArchive(wxInputStream
& in
,
880 const wxString
& unarchiver
)
882 // for an external unarchiver, unarchive to a tempdir
885 if ((m_options
& PipeIn
) == 0) {
886 wxFileName
fn(tmpdir
.GetName());
887 fn
.SetExt(wxT("arc"));
888 wxString tmparc
= fn
.GetPath(wxPATH_GET_SEPARATOR
) + fn
.GetFullName();
890 if (m_options
& Stub
)
891 in
.SeekI(STUB_SIZE
* 2);
893 // write the archive to a temporary file
895 wxFFileOutputStream
out(tmparc
);
901 system(wxString::Format(unarchiver
, tmparc
.c_str()).mb_str());
902 wxRemoveFile(tmparc
);
905 // for the non-seekable test, have the archiver extract "-" and
906 // feed it the archive via a pipe
907 PFileOutputStream
out(wxString::Format(unarchiver
, wxT("-")));
912 wxString dir
= tmpdir
.GetName();
916 // Verifies the files produced by an external unarchiver are as expected
918 template <class ClassFactoryT
>
919 void ArchiveTestCase
<ClassFactoryT
>::VerifyDir(wxString
& path
,
920 size_t rootlen
/*=0*/)
923 path
+= wxFileName::GetPathSeparator();
924 int pos
= path
.length();
930 if (dir
.Open(path
) && dir
.GetFirst(&name
)) {
932 path
.replace(pos
, wxString::npos
, name
);
933 name
= m_factory
->GetInternalName(
934 path
.substr(rootlen
, wxString::npos
));
936 bool isDir
= wxDirExists(path
);
940 // provide some context for the error message so that we know which
941 // iteration of the loop we were on
942 string
error_entry((wxT(" '") + name
+ wxT("'")).mb_str());
943 string
error_context(" failed for entry" + error_entry
);
945 TestEntries::iterator it
= m_testEntries
.find(name
);
946 CPPUNIT_ASSERT_MESSAGE(
947 "archive contains an entry that shouldn't be there"
949 it
!= m_testEntries
.end());
951 const TestEntry
& testEntry
= *it
->second
;
953 #if 0 //ndef __WXMSW__
954 CPPUNIT_ASSERT_MESSAGE("timestamp check" + error_context
,
955 testEntry
.GetDateTime() ==
956 wxFileName(path
).GetModificationTime());
959 wxFFileInputStream
in(path
);
960 CPPUNIT_ASSERT_MESSAGE(
961 "entry not found in archive" + error_entry
, in
.Ok());
963 size_t size
= (size_t)in
.GetLength();
964 wxCharBuffer
buf(size
);
965 CPPUNIT_ASSERT_MESSAGE("Read" + error_context
,
966 in
.Read(buf
.data(), size
).LastRead() == size
);
967 CPPUNIT_ASSERT_MESSAGE("size check" + error_context
,
968 testEntry
.GetSize() == size
);
969 CPPUNIT_ASSERT_MESSAGE("data compare" + error_context
,
970 memcmp(buf
.data(), testEntry
.GetData(), size
) == 0);
973 VerifyDir(path
, rootlen
);
977 m_testEntries
.erase(it
);
979 while (dir
.GetNext(&name
));
983 // test the simple iterators that give away ownership of an entry
985 template <class ClassFactoryT
>
986 void ArchiveTestCase
<ClassFactoryT
>::TestIterator(wxInputStream
& in
)
988 typedef std::list
<EntryT
*> ArchiveCatalog
;
989 typedef typename
ArchiveCatalog::iterator CatalogIter
;
991 auto_ptr
<InputStreamT
> arc(m_factory
->NewStream(in
));
994 #ifdef WXARC_MEMBER_TEMPLATES
995 ArchiveCatalog
cat((IterT
)*arc
, IterT());
998 for (IterT
i(*arc
); i
!= IterT(); ++i
)
1002 for (CatalogIter it
= cat
.begin(); it
!= cat
.end(); ++it
) {
1003 auto_ptr
<EntryT
> entry(*it
);
1004 count
+= m_testEntries
.count(entry
->GetName(wxPATH_UNIX
));
1007 CPPUNIT_ASSERT(m_testEntries
.size() == cat
.size());
1008 CPPUNIT_ASSERT(count
== cat
.size());
1011 // test the pair iterators that can be used to load a std::map or wxHashMap
1012 // these also give away ownership of entries
1014 template <class ClassFactoryT
>
1015 void ArchiveTestCase
<ClassFactoryT
>::TestPairIterator(wxInputStream
& in
)
1017 typedef std::map
<wxString
, EntryT
*> ArchiveCatalog
;
1018 typedef typename
ArchiveCatalog::iterator CatalogIter
;
1020 auto_ptr
<InputStreamT
> arc(m_factory
->NewStream(in
));
1023 #ifdef WXARC_MEMBER_TEMPLATES
1024 ArchiveCatalog
cat((PairIterT
)*arc
, PairIterT());
1027 for (PairIterT
i(*arc
); i
!= PairIterT(); ++i
)
1031 for (CatalogIter it
= cat
.begin(); it
!= cat
.end(); ++it
) {
1032 auto_ptr
<EntryT
> entry(it
->second
);
1033 count
+= m_testEntries
.count(entry
->GetName(wxPATH_UNIX
));
1036 CPPUNIT_ASSERT(m_testEntries
.size() == cat
.size());
1037 CPPUNIT_ASSERT(count
== cat
.size());
1040 // simple iterators using smart pointers, no need to worry about ownership
1042 template <class ClassFactoryT
>
1043 void ArchiveTestCase
<ClassFactoryT
>::TestSmartIterator(wxInputStream
& in
)
1045 typedef std::list
<Ptr
<EntryT
> > ArchiveCatalog
;
1046 typedef typename
ArchiveCatalog::iterator CatalogIter
;
1047 typedef wxArchiveIterator
<InputStreamT
, Ptr
<EntryT
> > Iter
;
1049 auto_ptr
<InputStreamT
> arc(m_factory
->NewStream(in
));
1051 #ifdef WXARC_MEMBER_TEMPLATES
1052 ArchiveCatalog
cat((Iter
)*arc
, Iter());
1055 for (Iter
i(*arc
); i
!= Iter(); ++i
)
1059 CPPUNIT_ASSERT(m_testEntries
.size() == cat
.size());
1061 for (CatalogIter it
= cat
.begin(); it
!= cat
.end(); ++it
)
1062 CPPUNIT_ASSERT(m_testEntries
.count((*it
)->GetName(wxPATH_UNIX
)));
1065 // pair iterator using smart pointers
1067 template <class ClassFactoryT
>
1068 void ArchiveTestCase
<ClassFactoryT
>::TestSmartPairIterator(wxInputStream
& in
)
1070 #if defined _MSC_VER && defined _MSC_VER < 1200
1071 // With VC++ 5.0 the '=' operator of std::pair breaks when the second
1072 // type is Ptr<EntryT>, so this iterator can't be made to work.
1075 typedef std::map
<wxString
, Ptr
<EntryT
> > ArchiveCatalog
;
1076 typedef typename
ArchiveCatalog::iterator CatalogIter
;
1077 typedef wxArchiveIterator
<InputStreamT
,
1078 std::pair
<wxString
, Ptr
<EntryT
> > > PairIter
;
1080 auto_ptr
<InputStreamT
> arc(m_factory
->NewStream(in
));
1082 #ifdef WXARC_MEMBER_TEMPLATES
1083 ArchiveCatalog
cat((PairIter
)*arc
, PairIter());
1086 for (PairIter
i(*arc
); i
!= PairIter(); ++i
)
1090 CPPUNIT_ASSERT(m_testEntries
.size() == cat
.size());
1092 for (CatalogIter it
= cat
.begin(); it
!= cat
.end(); ++it
)
1093 CPPUNIT_ASSERT(m_testEntries
.count(it
->second
->GetName(wxPATH_UNIX
)));
1097 // try reading two entries at the same time
1099 template <class ClassFactoryT
>
1100 void ArchiveTestCase
<ClassFactoryT
>::ReadSimultaneous(TestInputStream
& in
)
1102 typedef std::map
<wxString
, Ptr
<EntryT
> > ArchiveCatalog
;
1103 typedef wxArchiveIterator
<InputStreamT
,
1104 std::pair
<wxString
, Ptr
<EntryT
> > > PairIter
;
1106 // create two archive input streams
1107 TestInputStream
in2(in
);
1108 auto_ptr
<InputStreamT
> arc(m_factory
->NewStream(in
));
1109 auto_ptr
<InputStreamT
> arc2(m_factory
->NewStream(in2
));
1112 #ifdef WXARC_MEMBER_TEMPLATES
1113 ArchiveCatalog
cat((PairIter
)*arc
, PairIter());
1116 for (PairIter
i(*arc
); i
!= PairIter(); ++i
)
1120 // the names of two entries to read
1121 const wxChar
*name
= wxT("text/small");
1122 const wxChar
*name2
= wxT("bin/bin1000");
1125 typename
ArchiveCatalog::iterator j
;
1126 CPPUNIT_ASSERT((j
= cat
.find(name
)) != cat
.end());
1127 CPPUNIT_ASSERT(arc
->OpenEntry(*j
->second
));
1128 CPPUNIT_ASSERT((j
= cat
.find(name2
)) != cat
.end());
1129 CPPUNIT_ASSERT(arc2
->OpenEntry(*j
->second
));
1131 // get pointers to the expected data
1132 TestEntries::iterator k
;
1133 CPPUNIT_ASSERT((k
= m_testEntries
.find(name
)) != m_testEntries
.end());
1134 TestEntry
*entry
= k
->second
;
1135 CPPUNIT_ASSERT((k
= m_testEntries
.find(name2
)) != m_testEntries
.end());
1136 TestEntry
*entry2
= k
->second
;
1138 size_t count
= 0, count2
= 0;
1139 size_t size
= entry
->GetSize(), size2
= entry2
->GetSize();
1140 const char *data
= entry
->GetData(), *data2
= entry2
->GetData();
1142 // read and check the two entries in parallel, character by character
1143 while (arc
->IsOk() || arc2
->IsOk()) {
1144 char ch
= arc
->GetC();
1145 if (arc
->LastRead() == 1) {
1146 CPPUNIT_ASSERT(count
< size
);
1147 CPPUNIT_ASSERT(ch
== data
[count
++]);
1149 char ch2
= arc2
->GetC();
1150 if (arc2
->LastRead() == 1) {
1151 CPPUNIT_ASSERT(count2
< size2
);
1152 CPPUNIT_ASSERT(ch2
== data2
[count2
++]);
1156 CPPUNIT_ASSERT(arc
->Eof());
1157 CPPUNIT_ASSERT(arc2
->Eof());
1158 CPPUNIT_ASSERT(count
== size
);
1159 CPPUNIT_ASSERT(count2
== size2
);
1162 // Nothing useful can be done with a generic notifier yet, so just test one
1165 template <class NotifierT
, class EntryT
>
1166 class ArchiveNotifier
: public NotifierT
1169 void OnEntryUpdated(EntryT
& WXUNUSED(entry
)) { }
1172 template <class ClassFactoryT
>
1173 void ArchiveTestCase
<ClassFactoryT
>::OnSetNotifier(EntryT
& entry
)
1175 static ArchiveNotifier
<NotifierT
, EntryT
> notifier
;
1176 entry
.SetNotifier(notifier
);
1180 ///////////////////////////////////////////////////////////////////////////////
1181 // An additional case to check that reading corrupt archives doesn't crash
1183 class CorruptionTestCase
: public CppUnit::TestCase
1186 CorruptionTestCase(std::string name
,
1187 wxArchiveClassFactory
*factory
,
1189 : CppUnit::TestCase(TestId::MakeId() + name
),
1195 // the entry point for the test
1198 void CreateArchive(wxOutputStream
& out
);
1199 void ExtractArchive(wxInputStream
& in
);
1201 auto_ptr
<wxArchiveClassFactory
> m_factory
; // factory to make classes
1202 int m_options
; // test options
1205 void CorruptionTestCase::runTest()
1207 TestOutputStream
out(m_options
);
1209 TestInputStream
in(out
, 0);
1210 wxFileOffset len
= in
.GetLength();
1212 // try flipping one byte in the archive
1214 for (pos
= 0; pos
< len
; pos
++) {
1222 // try zeroing one byte in the archive
1223 for (pos
= 0; pos
< len
; pos
++) {
1231 // try chopping the archive off
1232 for (int size
= 1; size
<= len
; size
++) {
1239 void CorruptionTestCase::CreateArchive(wxOutputStream
& out
)
1241 auto_ptr
<wxArchiveOutputStream
> arc(m_factory
->NewStream(out
));
1243 arc
->PutNextDirEntry(wxT("dir"));
1244 arc
->PutNextEntry(wxT("file"));
1245 arc
->Write(wxT("foo"), 3);
1248 void CorruptionTestCase::ExtractArchive(wxInputStream
& in
)
1250 auto_ptr
<wxArchiveInputStream
> arc(m_factory
->NewStream(in
));
1251 auto_ptr
<wxArchiveEntry
> entry(arc
->GetNextEntry());
1253 while (entry
.get() != NULL
) {
1257 arc
->Read(buf
, sizeof(buf
));
1259 auto_ptr
<wxArchiveEntry
> next(arc
->GetNextEntry());
1265 ///////////////////////////////////////////////////////////////////////////////
1268 int TestId::m_seed
= 6219;
1271 string
TestId::MakeId()
1273 m_seed
= (m_seed
* 171) % 30269;
1274 return string(wxString::Format(wxT("%-6d"), m_seed
).mb_str());
1278 ///////////////////////////////////////////////////////////////////////////////
1281 ArchiveTestSuite::ArchiveTestSuite(string name
)
1282 : CppUnit::TestSuite("archive/" + name
),
1283 m_name(name
.c_str(), *wxConvCurrent
)
1285 m_name
= wxT("wx") + m_name
.Left(1).Upper() + m_name
.Mid(1).Lower();
1286 m_path
.AddEnvList(wxT("PATH"));
1287 m_archivers
.push_back(wxT(""));
1288 m_unarchivers
.push_back(wxT(""));
1291 // add the command for an external archiver to the list, testing for it in
1294 void ArchiveTestSuite::AddCmd(wxArrayString
& cmdlist
, const wxString
& cmd
)
1297 cmdlist
.push_back(cmd
);
1300 bool ArchiveTestSuite::IsInPath(const wxString
& cmd
)
1302 wxString c
= cmd
.BeforeFirst(wxT(' '));
1306 return !m_path
.FindValidPath(c
).empty();
1309 // make the test suite
1311 ArchiveTestSuite
*ArchiveTestSuite::makeSuite()
1313 typedef wxArrayString::iterator Iter
;
1315 for (int generic
= 0; generic
< 2; generic
++)
1316 for (Iter i
= m_unarchivers
.begin(); i
!= m_unarchivers
.end(); ++i
)
1317 for (Iter j
= m_archivers
.begin(); j
!= m_archivers
.end(); ++j
)
1318 for (int options
= 0; options
<= AllOptions
; options
++)
1320 #ifdef WXARC_NO_POPEN
1321 // if no popen then can't pipe in/out of archiver
1322 if ((options
& PipeIn
) && !i
->empty())
1324 if ((options
& PipeOut
) && !j
->empty())
1327 string descr
= Description(m_name
, options
,
1328 generic
!= 0, *j
, *i
);
1330 CppUnit::Test
*test
= makeTest(descr
, options
,
1331 generic
!= 0, *j
, *i
);
1337 for (int options
= 0; options
<= PipeIn
; options
+= PipeIn
)
1339 wxObject
*pObj
= wxCreateDynamicObject(m_name
+ wxT("ClassFactory"));
1340 wxArchiveClassFactory
*factory
;
1341 factory
= wxDynamicCast(pObj
, wxArchiveClassFactory
);
1344 string
descr(m_name
.mb_str());
1345 descr
= "CorruptionTestCase (" + descr
+ ")";
1348 descr
+= " (PipeIn)";
1350 addTest(new CorruptionTestCase(descr
, factory
, options
));
1357 CppUnit::Test
*ArchiveTestSuite::makeTest(
1358 string
WXUNUSED(descr
),
1359 int WXUNUSED(options
),
1360 bool WXUNUSED(genericInterface
),
1361 const wxString
& WXUNUSED(archiver
),
1362 const wxString
& WXUNUSED(unarchiver
))
1367 // make a display string for the option bits
1369 string
ArchiveTestSuite::Description(const wxString
& type
,
1371 bool genericInterface
,
1372 const wxString
& archiver
,
1373 const wxString
& unarchiver
)
1377 if (genericInterface
)
1378 descr
<< wxT("wxArchive (") << type
<< wxT(")");
1382 if (!archiver
.empty()) {
1383 const wxChar
*fn
= (options
& PipeOut
) != 0 ? wxT("-") : wxT("file");
1384 descr
<< wxT(" (") << wxString::Format(archiver
, fn
) << wxT(")");
1386 if (!unarchiver
.empty()) {
1387 const wxChar
*fn
= (options
& PipeIn
) != 0 ? wxT("-") : wxT("file");
1388 descr
<< wxT(" (") << wxString::Format(unarchiver
, fn
) << wxT(")");
1393 if ((options
& PipeIn
) != 0)
1394 optstr
+= wxT("|PipeIn");
1395 if ((options
& PipeOut
) != 0)
1396 optstr
+= wxT("|PipeOut");
1397 if ((options
& Stub
) != 0)
1398 optstr
+= wxT("|Stub");
1399 if (!optstr
.empty())
1400 optstr
= wxT(" (") + optstr
.substr(1) + wxT(")");
1404 return string(descr
.mb_str());
1408 ///////////////////////////////////////////////////////////////////////////////
1411 template class ArchiveTestCase
<wxArchiveClassFactory
>;
1414 #include "wx/zipstrm.h"
1415 template class ArchiveTestCase
<wxZipClassFactory
>;
1419 #include "wx/tarstrm.h"
1420 template class ArchiveTestCase
<wxTarClassFactory
>;
1423 #endif // wxUSE_STREAMS && wxUSE_ARCHIVE_STREAMS