]>
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
,
38 const wxString
& archiver
= wxEmptyString
,
39 const wxString
& unarchiver
= wxEmptyString
)
41 ArchiveTestCase
<wxZipClassFactory
>(name
, new wxZipClassFactory
,
42 options
, archiver
, unarchiver
),
47 void OnCreateArchive(wxZipOutputStream
& zip
);
49 void OnArchiveExtracted(wxZipInputStream
& zip
, int expectedTotal
);
51 void OnCreateEntry(wxZipOutputStream
& zip
,
55 void OnEntryExtracted(wxZipEntry
& entry
,
56 const TestEntry
& testEntry
,
57 wxZipInputStream
*arc
);
59 void OnSetNotifier(EntryT
& entry
);
65 void ZipTestCase::OnCreateArchive(wxZipOutputStream
& zip
)
67 m_comment
<< _T("Comment for test ") << m_id
;
68 zip
.SetComment(m_comment
);
71 void ZipTestCase::OnArchiveExtracted(wxZipInputStream
& zip
, int expectedTotal
)
73 CPPUNIT_ASSERT(zip
.GetComment() == m_comment
);
74 CPPUNIT_ASSERT(zip
.GetTotalEntries() == expectedTotal
);
77 void ZipTestCase::OnCreateEntry(wxZipOutputStream
& zip
,
81 zip
.SetLevel((m_id
+ m_count
) % 10);
84 switch ((m_id
+ m_count
) % 5) {
87 wxString comment
= _T("Comment for ") + entry
->GetName();
88 entry
->SetComment(comment
);
89 // lowercase the expected result, and the notifier should do
90 // the same for the zip entries when ModifyArchive() runs
91 testEntry
.SetComment(comment
.Lower());
95 entry
->SetMethod(wxZIP_METHOD_STORE
);
98 entry
->SetMethod(wxZIP_METHOD_DEFLATE
);
101 entry
->SetIsText(testEntry
.IsText());
107 void ZipTestCase::OnEntryExtracted(wxZipEntry
& entry
,
108 const TestEntry
& testEntry
,
109 wxZipInputStream
*arc
)
111 // provide some context for the error message so that we know which
112 // iteration of the loop we were on
113 wxString name
= _T(" '") + entry
.GetName() + _T("'");
114 string
error_entry(name
.mb_str());
115 string
error_context(" failed for entry" + error_entry
);
117 CPPUNIT_ASSERT_MESSAGE("GetComment" + error_context
,
118 entry
.GetComment() == testEntry
.GetComment());
120 // for seekable streams, GetNextEntry() doesn't read the local header so
121 // call OpenEntry() to do it
122 if (arc
&& (m_options
& PipeIn
) == 0 && entry
.IsDir())
123 arc
->OpenEntry(entry
);
125 CPPUNIT_ASSERT_MESSAGE("IsText" + error_context
,
126 entry
.IsText() == testEntry
.IsText());
128 CPPUNIT_ASSERT_MESSAGE("Extra/LocalExtra mismatch for entry" + error_entry
,
129 (entry
.GetExtraLen() != 0 && entry
.GetLocalExtraLen() != 0) ||
130 (entry
.GetExtraLen() == 0 && entry
.GetLocalExtraLen() == 0));
133 // check the notifier mechanism by using it to fold the entry comments to
136 class ZipNotifier
: public wxZipNotifier
139 void OnEntryUpdated(wxZipEntry
& entry
);
142 void ZipNotifier::OnEntryUpdated(wxZipEntry
& entry
)
144 entry
.SetComment(entry
.GetComment().Lower());
147 void ZipTestCase::OnSetNotifier(EntryT
& entry
)
149 static ZipNotifier notifier
;
150 entry
.SetNotifier(notifier
);
154 ///////////////////////////////////////////////////////////////////////////////
155 // 'zip - -' produces local headers without the size field set. This is a
156 // case not covered by all the other tests, so this class tests it as a
159 class ZipPipeTestCase
: public CppUnit::TestCase
162 ZipPipeTestCase(string name
, int options
) :
163 CppUnit::TestCase(TestId::MakeId() + name
),
165 m_id(TestId::GetId())
174 void ZipPipeTestCase::runTest()
176 TestOutputStream
out(m_options
);
178 wxString testdata
= _T("test data to pipe through zip");
179 wxString cmd
= _T("echo ") + testdata
+ _T(" | zip -q - -");
182 PFileInputStream
in(cmd
);
187 TestInputStream
in(out
, m_id
% ((m_options
& PipeIn
) ? 4 : 3));
188 wxZipInputStream
zip(in
);
190 auto_ptr
<wxZipEntry
> entry(zip
.GetNextEntry());
191 CPPUNIT_ASSERT(entry
.get() != NULL
);
193 if ((m_options
& PipeIn
) == 0)
194 CPPUNIT_ASSERT(entry
->GetSize() != wxInvalidOffset
);
197 size_t len
= zip
.Read(buf
, sizeof(buf
) - 1).LastRead();
199 while (len
> 0 && buf
[len
- 1] <= 32)
203 CPPUNIT_ASSERT(zip
.Eof());
204 CPPUNIT_ASSERT(wxString(buf
, *wxConvCurrent
) == testdata
);
208 ///////////////////////////////////////////////////////////////////////////////
211 class ziptest
: public ArchiveTestSuite
215 static CppUnit::Test
*suite() { return (new ziptest
)->makeSuite(); }
218 ArchiveTestSuite
*makeSuite();
220 CppUnit::Test
*makeTest(string descr
, int options
,
221 bool genericInterface
, const wxString
& archiver
,
222 const wxString
& unarchiver
);
226 : ArchiveTestSuite("zip")
228 AddArchiver(_T("zip -qr %s *"));
229 AddUnArchiver(_T("unzip -q %s"));
232 ArchiveTestSuite
*ziptest::makeSuite()
234 ArchiveTestSuite::makeSuite();
236 #if !defined WXARC_NO_POPEN && !defined __WXMSW__
237 // If have popen then can check the piped output of 'zip - -'.
238 // The gnuwin32 build of infozip does work for this, e.g.:
239 // C:\>echo test data to pipe through zip | zip -q > foo.zip
240 // doesn't produce a valid zip, so disabled for now.
241 if (IsInPath(_T("zip")))
242 for (int options
= 0; options
<= PipeIn
; options
+= PipeIn
) {
243 string name
= Description(_T("ZipPipeTestCase"), options
,
244 false, _T(""), _T("zip -q - -"));
245 addTest(new ZipPipeTestCase(name
, options
));
252 CppUnit::Test
*ziptest::makeTest(
255 bool genericInterface
,
256 const wxString
& archiver
,
257 const wxString
& unarchiver
)
259 // unzip doesn't support piping in the zip
260 if ((options
& PipeIn
) && !unarchiver
.empty())
263 if (genericInterface
)
264 return new ArchiveTestCase
<wxArchiveClassFactory
>(
265 descr
, new wxZipClassFactory
,
266 options
, archiver
, unarchiver
);
268 return new ZipTestCase(descr
, options
, archiver
, unarchiver
);
271 CPPUNIT_TEST_SUITE_REGISTRATION(ziptest
);
272 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ziptest
, "archive");
273 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ziptest
, "archive/zip");
275 #endif // wxUSE_STREAMS && wxUSE_ZIPSTREAM