]>
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
5 // Copyright: (c) 2004 Mike Wetherell
6 // Licence: wxWindows licence
7 ///////////////////////////////////////////////////////////////////////////////
19 #if wxUSE_STREAMS && wxUSE_ZIPSTREAM
21 #include "archivetest.h"
22 #include "wx/zipstrm.h"
28 ///////////////////////////////////////////////////////////////////////////////
29 // ArchiveTestCase<wxZipClassFactory> could be used directly, but instead this
30 // derived class is used so that zip specific features can be tested.
32 class ZipTestCase
: public ArchiveTestCase
<wxZipClassFactory
>
35 ZipTestCase(string name
,
37 const wxString
& archiver
= wxEmptyString
,
38 const wxString
& unarchiver
= wxEmptyString
)
40 ArchiveTestCase
<wxZipClassFactory
>(name
, new wxZipClassFactory
,
41 options
, archiver
, unarchiver
),
46 void OnCreateArchive(wxZipOutputStream
& zip
);
48 void OnArchiveExtracted(wxZipInputStream
& zip
, int expectedTotal
);
50 void OnCreateEntry(wxZipOutputStream
& zip
,
54 void OnEntryExtracted(wxZipEntry
& entry
,
55 const TestEntry
& testEntry
,
56 wxZipInputStream
*arc
);
58 void OnSetNotifier(EntryT
& entry
);
64 void ZipTestCase::OnCreateArchive(wxZipOutputStream
& zip
)
66 m_comment
<< wxT("Comment for test ") << m_id
;
67 zip
.SetComment(m_comment
);
70 void ZipTestCase::OnArchiveExtracted(wxZipInputStream
& zip
, int expectedTotal
)
72 CPPUNIT_ASSERT(zip
.GetComment() == m_comment
);
73 CPPUNIT_ASSERT(zip
.GetTotalEntries() == expectedTotal
);
76 void ZipTestCase::OnCreateEntry(wxZipOutputStream
& zip
,
80 zip
.SetLevel((m_id
+ m_count
) % 10);
83 switch ((m_id
+ m_count
) % 5) {
86 wxString comment
= wxT("Comment for ") + entry
->GetName();
87 entry
->SetComment(comment
);
88 // lowercase the expected result, and the notifier should do
89 // the same for the zip entries when ModifyArchive() runs
90 testEntry
.SetComment(comment
.Lower());
94 entry
->SetMethod(wxZIP_METHOD_STORE
);
97 entry
->SetMethod(wxZIP_METHOD_DEFLATE
);
100 entry
->SetIsText(testEntry
.IsText());
106 void ZipTestCase::OnEntryExtracted(wxZipEntry
& entry
,
107 const TestEntry
& testEntry
,
108 wxZipInputStream
*arc
)
110 // provide some context for the error message so that we know which
111 // iteration of the loop we were on
112 wxString name
= wxT(" '") + entry
.GetName() + wxT("'");
113 string
error_entry(name
.mb_str());
114 string
error_context(" failed for entry" + error_entry
);
116 CPPUNIT_ASSERT_MESSAGE("GetComment" + error_context
,
117 entry
.GetComment() == testEntry
.GetComment());
119 // for seekable streams, GetNextEntry() doesn't read the local header so
120 // call OpenEntry() to do it
121 if (arc
&& (m_options
& PipeIn
) == 0 && entry
.IsDir())
122 arc
->OpenEntry(entry
);
124 CPPUNIT_ASSERT_MESSAGE("IsText" + error_context
,
125 entry
.IsText() == testEntry
.IsText());
127 CPPUNIT_ASSERT_MESSAGE("Extra/LocalExtra mismatch for entry" + error_entry
,
128 (entry
.GetExtraLen() != 0 && entry
.GetLocalExtraLen() != 0) ||
129 (entry
.GetExtraLen() == 0 && entry
.GetLocalExtraLen() == 0));
132 // check the notifier mechanism by using it to fold the entry comments to
135 class ZipNotifier
: public wxZipNotifier
138 void OnEntryUpdated(wxZipEntry
& entry
);
141 void ZipNotifier::OnEntryUpdated(wxZipEntry
& entry
)
143 entry
.SetComment(entry
.GetComment().Lower());
146 void ZipTestCase::OnSetNotifier(EntryT
& entry
)
148 static ZipNotifier notifier
;
149 entry
.SetNotifier(notifier
);
153 ///////////////////////////////////////////////////////////////////////////////
154 // 'zip - -' produces local headers without the size field set. This is a
155 // case not covered by all the other tests, so this class tests it as a
158 class ZipPipeTestCase
: public CppUnit::TestCase
161 ZipPipeTestCase(string name
, int options
) :
162 CppUnit::TestCase(TestId::MakeId() + name
),
164 m_id(TestId::GetId())
173 void ZipPipeTestCase::runTest()
175 TestOutputStream
out(m_options
);
177 wxString testdata
= wxT("test data to pipe through zip");
178 wxString cmd
= wxT("echo ") + testdata
+ wxT(" | zip -q - -");
181 PFileInputStream
in(cmd
);
186 TestInputStream
in(out
, m_id
% ((m_options
& PipeIn
) ? 4 : 3));
187 wxZipInputStream
zip(in
);
189 auto_ptr
<wxZipEntry
> entry(zip
.GetNextEntry());
190 CPPUNIT_ASSERT(entry
.get() != NULL
);
192 if ((m_options
& PipeIn
) == 0)
193 CPPUNIT_ASSERT(entry
->GetSize() != wxInvalidOffset
);
196 size_t len
= zip
.Read(buf
, sizeof(buf
) - 1).LastRead();
198 while (len
> 0 && buf
[len
- 1] <= 32)
202 CPPUNIT_ASSERT(zip
.Eof());
203 CPPUNIT_ASSERT(wxString(buf
, *wxConvCurrent
) == testdata
);
207 ///////////////////////////////////////////////////////////////////////////////
210 class ziptest
: public ArchiveTestSuite
214 static CppUnit::Test
*suite() { return (new ziptest
)->makeSuite(); }
217 ArchiveTestSuite
*makeSuite();
219 CppUnit::Test
*makeTest(string descr
, int options
,
220 bool genericInterface
, const wxString
& archiver
,
221 const wxString
& unarchiver
);
225 : ArchiveTestSuite("zip")
227 AddArchiver(wxT("zip -qr %s *"));
228 AddUnArchiver(wxT("unzip -q %s"));
231 ArchiveTestSuite
*ziptest::makeSuite()
233 ArchiveTestSuite::makeSuite();
236 // zip doesn't support this any more so disabled
237 if (IsInPath(wxT("zip")))
238 for (int options
= 0; options
<= PipeIn
; options
+= PipeIn
) {
239 string name
= Description(wxT("ZipPipeTestCase"), options
,
240 false, wxT(""), wxT("zip -q - -"));
241 addTest(new ZipPipeTestCase(name
, options
));
248 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
)
261 return new ArchiveTestCase
<wxArchiveClassFactory
>(
262 descr
, new wxZipClassFactory
,
263 options
, archiver
, unarchiver
);
266 return new ZipTestCase(descr
, options
, archiver
, unarchiver
);
269 CPPUNIT_TEST_SUITE_REGISTRATION(ziptest
);
270 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ziptest
, "archive");
271 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ziptest
, "archive/zip");
273 #endif // wxUSE_STREAMS && wxUSE_ZIPSTREAM