]> git.saurik.com Git - wxWidgets.git/blame - tests/archive/ziptest.cpp
Fix Doxygen warnings due to documenting overloaded functions together.
[wxWidgets.git] / tests / archive / ziptest.cpp
CommitLineData
e6477b92
MW
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/archive/ziptest.cpp
3// Purpose: Test the zip classes
4// Author: Mike Wetherell
e6477b92
MW
5// Copyright: (c) 2004 Mike Wetherell
6// Licence: wxWindows licence
7///////////////////////////////////////////////////////////////////////////////
8
9#include "testprec.h"
10
11#ifdef __BORLANDC__
12# pragma hdrstop
13#endif
14
15#ifndef WX_PRECOMP
16# include "wx/wx.h"
17#endif
18
19#if wxUSE_STREAMS && wxUSE_ZIPSTREAM
20
21#include "archivetest.h"
22#include "wx/zipstrm.h"
23
24using std::string;
25using std::auto_ptr;
26
27
28///////////////////////////////////////////////////////////////////////////////
29// ArchiveTestCase<wxZipClassFactory> could be used directly, but instead this
30// derived class is used so that zip specific features can be tested.
31
32class ZipTestCase : public ArchiveTestCase<wxZipClassFactory>
33{
34public:
35 ZipTestCase(string name,
e6477b92
MW
36 int options,
37 const wxString& archiver = wxEmptyString,
38 const wxString& unarchiver = wxEmptyString)
39 :
716e748b 40 ArchiveTestCase<wxZipClassFactory>(name, new wxZipClassFactory,
e6477b92
MW
41 options, archiver, unarchiver),
42 m_count(0)
43 { }
44
45protected:
46 void OnCreateArchive(wxZipOutputStream& zip);
47
48 void OnArchiveExtracted(wxZipInputStream& zip, int expectedTotal);
49
50 void OnCreateEntry(wxZipOutputStream& zip,
51 TestEntry& testEntry,
52 wxZipEntry *entry);
53
54 void OnEntryExtracted(wxZipEntry& entry,
55 const TestEntry& testEntry,
56 wxZipInputStream *arc);
57
58 void OnSetNotifier(EntryT& entry);
59
60 int m_count;
61 wxString m_comment;
62};
63
64void ZipTestCase::OnCreateArchive(wxZipOutputStream& zip)
65{
9a83f860 66 m_comment << wxT("Comment for test ") << m_id;
e6477b92
MW
67 zip.SetComment(m_comment);
68}
69
70void ZipTestCase::OnArchiveExtracted(wxZipInputStream& zip, int expectedTotal)
71{
72 CPPUNIT_ASSERT(zip.GetComment() == m_comment);
73 CPPUNIT_ASSERT(zip.GetTotalEntries() == expectedTotal);
74}
75
76void ZipTestCase::OnCreateEntry(wxZipOutputStream& zip,
77 TestEntry& testEntry,
78 wxZipEntry *entry)
79{
80 zip.SetLevel((m_id + m_count) % 10);
81
82 if (entry) {
83 switch ((m_id + m_count) % 5) {
84 case 0:
85 {
9a83f860 86 wxString comment = wxT("Comment for ") + entry->GetName();
e6477b92
MW
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());
91 break;
92 }
93 case 2:
94 entry->SetMethod(wxZIP_METHOD_STORE);
95 break;
96 case 4:
97 entry->SetMethod(wxZIP_METHOD_DEFLATE);
98 break;
99 }
100 entry->SetIsText(testEntry.IsText());
101 }
102
103 m_count++;
104}
105
106void ZipTestCase::OnEntryExtracted(wxZipEntry& entry,
107 const TestEntry& testEntry,
108 wxZipInputStream *arc)
109{
110 // provide some context for the error message so that we know which
111 // iteration of the loop we were on
9a83f860 112 wxString name = wxT(" '") + entry.GetName() + wxT("'");
e6477b92
MW
113 string error_entry(name.mb_str());
114 string error_context(" failed for entry" + error_entry);
115
116 CPPUNIT_ASSERT_MESSAGE("GetComment" + error_context,
117 entry.GetComment() == testEntry.GetComment());
118
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);
123
124 CPPUNIT_ASSERT_MESSAGE("IsText" + error_context,
125 entry.IsText() == testEntry.IsText());
126
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));
130}
131
132// check the notifier mechanism by using it to fold the entry comments to
133// lowercase
134//
135class ZipNotifier : public wxZipNotifier
136{
137public:
138 void OnEntryUpdated(wxZipEntry& entry);
139};
140
141void ZipNotifier::OnEntryUpdated(wxZipEntry& entry)
142{
143 entry.SetComment(entry.GetComment().Lower());
144}
145
146void ZipTestCase::OnSetNotifier(EntryT& entry)
147{
148 static ZipNotifier notifier;
149 entry.SetNotifier(notifier);
150}
151
152
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
156// special case
157
158class ZipPipeTestCase : public CppUnit::TestCase
159{
160public:
161 ZipPipeTestCase(string name, int options) :
716e748b
MW
162 CppUnit::TestCase(TestId::MakeId() + name),
163 m_options(options),
164 m_id(TestId::GetId())
165 { }
e6477b92
MW
166
167protected:
168 void runTest();
169 int m_options;
716e748b 170 int m_id;
e6477b92
MW
171};
172
173void ZipPipeTestCase::runTest()
174{
175 TestOutputStream out(m_options);
176
9a83f860
VZ
177 wxString testdata = wxT("test data to pipe through zip");
178 wxString cmd = wxT("echo ") + testdata + wxT(" | zip -q - -");
e6477b92
MW
179
180 {
181 PFileInputStream in(cmd);
a1b806b9 182 if (in.IsOk())
e6477b92
MW
183 out.Write(in);
184 }
185
716e748b 186 TestInputStream in(out, m_id % ((m_options & PipeIn) ? 4 : 3));
e6477b92
MW
187 wxZipInputStream zip(in);
188
189 auto_ptr<wxZipEntry> entry(zip.GetNextEntry());
190 CPPUNIT_ASSERT(entry.get() != NULL);
191
192 if ((m_options & PipeIn) == 0)
193 CPPUNIT_ASSERT(entry->GetSize() != wxInvalidOffset);
194
195 char buf[64];
196 size_t len = zip.Read(buf, sizeof(buf) - 1).LastRead();
197
198 while (len > 0 && buf[len - 1] <= 32)
199 --len;
200 buf[len] = 0;
201
202 CPPUNIT_ASSERT(zip.Eof());
203 CPPUNIT_ASSERT(wxString(buf, *wxConvCurrent) == testdata);
204}
205
206
207///////////////////////////////////////////////////////////////////////////////
208// Zip suite
209
210class ziptest : public ArchiveTestSuite
211{
212public:
213 ziptest();
214 static CppUnit::Test *suite() { return (new ziptest)->makeSuite(); }
215
216protected:
217 ArchiveTestSuite *makeSuite();
218
716e748b 219 CppUnit::Test *makeTest(string descr, int options,
e6477b92
MW
220 bool genericInterface, const wxString& archiver,
221 const wxString& unarchiver);
222};
223
224ziptest::ziptest()
225 : ArchiveTestSuite("zip")
226{
9a83f860
VZ
227 AddArchiver(wxT("zip -qr %s *"));
228 AddUnArchiver(wxT("unzip -q %s"));
e6477b92
MW
229}
230
231ArchiveTestSuite *ziptest::makeSuite()
232{
233 ArchiveTestSuite::makeSuite();
234
8bda419d
MW
235#if 0
236 // zip doesn't support this any more so disabled
9a83f860 237 if (IsInPath(wxT("zip")))
e6477b92 238 for (int options = 0; options <= PipeIn; options += PipeIn) {
9a83f860
VZ
239 string name = Description(wxT("ZipPipeTestCase"), options,
240 false, wxT(""), wxT("zip -q - -"));
e6477b92 241 addTest(new ZipPipeTestCase(name, options));
e6477b92
MW
242 }
243#endif
244
245 return this;
246}
247
248CppUnit::Test *ziptest::makeTest(
249 string descr,
e6477b92
MW
250 int options,
251 bool genericInterface,
252 const wxString& archiver,
253 const wxString& unarchiver)
254{
255 // unzip doesn't support piping in the zip
256 if ((options & PipeIn) && !unarchiver.empty())
257 return NULL;
258
259 if (genericInterface)
895cae46 260 {
e6477b92 261 return new ArchiveTestCase<wxArchiveClassFactory>(
716e748b 262 descr, new wxZipClassFactory,
e6477b92 263 options, archiver, unarchiver);
895cae46
VZ
264 }
265
266 return new ZipTestCase(descr, options, archiver, unarchiver);
e6477b92
MW
267}
268
269CPPUNIT_TEST_SUITE_REGISTRATION(ziptest);
270CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ziptest, "archive");
271CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ziptest, "archive/zip");
272
273#endif // wxUSE_STREAMS && wxUSE_ZIPSTREAM