]>
git.saurik.com Git - wxWidgets.git/blob - tests/archive/ziptest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/archive/ziptest.cpp
3 // Purpose: Test the zip classes
4 // Author: Mike Wetherell
6 // Copyright: (c) 2004 Mike Wetherell
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
20 #if wxUSE_STREAMS && wxUSE_ZIPSTREAM
22 #include "archivetest.h"
23 #include "wx/zipstrm.h"
29 ///////////////////////////////////////////////////////////////////////////////
30 // ArchiveTestCase<wxZipClassFactory> could be used directly, but instead this
31 // derived class is used so that zip specific features can be tested.
33 class ZipTestCase
: public ArchiveTestCase
<wxZipClassFactory
>
36 ZipTestCase(string name
,
39 const wxString
& archiver
= wxEmptyString
,
40 const wxString
& unarchiver
= wxEmptyString
)
42 ArchiveTestCase
<wxZipClassFactory
>(name
, id
, new wxZipClassFactory
,
43 options
, archiver
, unarchiver
),
48 void OnCreateArchive(wxZipOutputStream
& zip
);
50 void OnArchiveExtracted(wxZipInputStream
& zip
, int expectedTotal
);
52 void OnCreateEntry(wxZipOutputStream
& zip
,
56 void OnEntryExtracted(wxZipEntry
& entry
,
57 const TestEntry
& testEntry
,
58 wxZipInputStream
*arc
);
60 void OnSetNotifier(EntryT
& entry
);
66 void ZipTestCase::OnCreateArchive(wxZipOutputStream
& zip
)
68 m_comment
<< _T("Comment for test ") << m_id
;
69 zip
.SetComment(m_comment
);
72 void ZipTestCase::OnArchiveExtracted(wxZipInputStream
& zip
, int expectedTotal
)
74 CPPUNIT_ASSERT(zip
.GetComment() == m_comment
);
75 CPPUNIT_ASSERT(zip
.GetTotalEntries() == expectedTotal
);
78 void ZipTestCase::OnCreateEntry(wxZipOutputStream
& zip
,
82 zip
.SetLevel((m_id
+ m_count
) % 10);
85 switch ((m_id
+ m_count
) % 5) {
88 wxString comment
= _T("Comment for ") + entry
->GetName();
89 entry
->SetComment(comment
);
90 // lowercase the expected result, and the notifier should do
91 // the same for the zip entries when ModifyArchive() runs
92 testEntry
.SetComment(comment
.Lower());
96 entry
->SetMethod(wxZIP_METHOD_STORE
);
99 entry
->SetMethod(wxZIP_METHOD_DEFLATE
);
102 entry
->SetIsText(testEntry
.IsText());
108 void ZipTestCase::OnEntryExtracted(wxZipEntry
& entry
,
109 const TestEntry
& testEntry
,
110 wxZipInputStream
*arc
)
112 // provide some context for the error message so that we know which
113 // iteration of the loop we were on
114 wxString name
= _T(" '") + entry
.GetName() + _T("'");
115 string
error_entry(name
.mb_str());
116 string
error_context(" failed for entry" + error_entry
);
118 CPPUNIT_ASSERT_MESSAGE("GetComment" + error_context
,
119 entry
.GetComment() == testEntry
.GetComment());
121 // for seekable streams, GetNextEntry() doesn't read the local header so
122 // call OpenEntry() to do it
123 if (arc
&& (m_options
& PipeIn
) == 0 && entry
.IsDir())
124 arc
->OpenEntry(entry
);
126 CPPUNIT_ASSERT_MESSAGE("IsText" + error_context
,
127 entry
.IsText() == testEntry
.IsText());
129 CPPUNIT_ASSERT_MESSAGE("Extra/LocalExtra mismatch for entry" + error_entry
,
130 (entry
.GetExtraLen() != 0 && entry
.GetLocalExtraLen() != 0) ||
131 (entry
.GetExtraLen() == 0 && entry
.GetLocalExtraLen() == 0));
134 // check the notifier mechanism by using it to fold the entry comments to
137 class ZipNotifier
: public wxZipNotifier
140 void OnEntryUpdated(wxZipEntry
& entry
);
143 void ZipNotifier::OnEntryUpdated(wxZipEntry
& entry
)
145 entry
.SetComment(entry
.GetComment().Lower());
148 void ZipTestCase::OnSetNotifier(EntryT
& entry
)
150 static ZipNotifier notifier
;
151 entry
.SetNotifier(notifier
);
155 ///////////////////////////////////////////////////////////////////////////////
156 // 'zip - -' produces local headers without the size field set. This is a
157 // case not covered by all the other tests, so this class tests it as a
160 class ZipPipeTestCase
: public CppUnit::TestCase
163 ZipPipeTestCase(string name
, int options
) :
164 CppUnit::TestCase(name
), m_options(options
) { }
171 void ZipPipeTestCase::runTest()
173 TestOutputStream
out(m_options
);
175 wxString testdata
= _T("test data to pipe through zip");
176 wxString cmd
= _T("echo ") + testdata
+ _T(" | zip -q - -");
179 PFileInputStream
in(cmd
);
184 TestInputStream
in(out
);
185 wxZipInputStream
zip(in
);
187 auto_ptr
<wxZipEntry
> entry(zip
.GetNextEntry());
188 CPPUNIT_ASSERT(entry
.get() != NULL
);
190 if ((m_options
& PipeIn
) == 0)
191 CPPUNIT_ASSERT(entry
->GetSize() != wxInvalidOffset
);
194 size_t len
= zip
.Read(buf
, sizeof(buf
) - 1).LastRead();
196 while (len
> 0 && buf
[len
- 1] <= 32)
200 CPPUNIT_ASSERT(zip
.Eof());
201 CPPUNIT_ASSERT(wxString(buf
, *wxConvCurrent
) == testdata
);
205 ///////////////////////////////////////////////////////////////////////////////
208 class ziptest
: public ArchiveTestSuite
212 static CppUnit::Test
*suite() { return (new ziptest
)->makeSuite(); }
215 ArchiveTestSuite
*makeSuite();
217 CppUnit::Test
*makeTest(string descr
, int id
, int options
,
218 bool genericInterface
, const wxString
& archiver
,
219 const wxString
& unarchiver
);
223 : ArchiveTestSuite("zip")
225 AddArchiver(_T("zip -qr %s *"));
226 AddUnArchiver(_T("unzip -q %s"));
229 ArchiveTestSuite
*ziptest::makeSuite()
231 ArchiveTestSuite::makeSuite();
233 #ifndef WXARC_NO_POPEN
234 // if have popen then can check the piped output of 'zip - -'
235 if (IsInPath(_T("zip")))
236 for (int options
= 0; options
<= PipeIn
; options
+= PipeIn
) {
237 string name
= Description(_T("ZipPipeTestCase"), options
,
238 false, _T(""), _T("zip -q - -"));
239 addTest(new ZipPipeTestCase(name
, options
));
247 CppUnit::Test
*ziptest::makeTest(
251 bool genericInterface
,
252 const wxString
& archiver
,
253 const wxString
& unarchiver
)
255 // unzip doesn't support piping in the zip
256 if ((options
& PipeIn
) && !unarchiver
.empty())
259 if (genericInterface
)
260 return new ArchiveTestCase
<wxArchiveClassFactory
>(
261 descr
, id
, new wxZipClassFactory
,
262 options
, archiver
, unarchiver
);
264 return new ZipTestCase(descr
, id
, options
, archiver
, unarchiver
);
267 CPPUNIT_TEST_SUITE_REGISTRATION(ziptest
);
268 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ziptest
, "archive");
269 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ziptest
, "archive/zip");
271 #endif // wxUSE_STREAMS && wxUSE_ZIPSTREAM