]> git.saurik.com Git - wxWidgets.git/blob - tests/archive/ziptest.cpp
More German translations updates from Sebastian Walderich.
[wxWidgets.git] / 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 ///////////////////////////////////////////////////////////////////////////////
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
24 using std::string;
25 using 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
32 class ZipTestCase : public ArchiveTestCase<wxZipClassFactory>
33 {
34 public:
35 ZipTestCase(string name,
36 int options,
37 const wxString& archiver = wxEmptyString,
38 const wxString& unarchiver = wxEmptyString)
39 :
40 ArchiveTestCase<wxZipClassFactory>(name, new wxZipClassFactory,
41 options, archiver, unarchiver),
42 m_count(0)
43 { }
44
45 protected:
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
64 void ZipTestCase::OnCreateArchive(wxZipOutputStream& zip)
65 {
66 m_comment << wxT("Comment for test ") << m_id;
67 zip.SetComment(m_comment);
68 }
69
70 void ZipTestCase::OnArchiveExtracted(wxZipInputStream& zip, int expectedTotal)
71 {
72 CPPUNIT_ASSERT(zip.GetComment() == m_comment);
73 CPPUNIT_ASSERT(zip.GetTotalEntries() == expectedTotal);
74 }
75
76 void 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 {
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());
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
106 void 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
112 wxString name = wxT(" '") + entry.GetName() + wxT("'");
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 //
135 class ZipNotifier : public wxZipNotifier
136 {
137 public:
138 void OnEntryUpdated(wxZipEntry& entry);
139 };
140
141 void ZipNotifier::OnEntryUpdated(wxZipEntry& entry)
142 {
143 entry.SetComment(entry.GetComment().Lower());
144 }
145
146 void 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
158 class ZipPipeTestCase : public CppUnit::TestCase
159 {
160 public:
161 ZipPipeTestCase(string name, int options) :
162 CppUnit::TestCase(TestId::MakeId() + name),
163 m_options(options),
164 m_id(TestId::GetId())
165 { }
166
167 protected:
168 void runTest();
169 int m_options;
170 int m_id;
171 };
172
173 void ZipPipeTestCase::runTest()
174 {
175 TestOutputStream out(m_options);
176
177 wxString testdata = wxT("test data to pipe through zip");
178 wxString cmd = wxT("echo ") + testdata + wxT(" | zip -q - -");
179
180 {
181 PFileInputStream in(cmd);
182 if (in.IsOk())
183 out.Write(in);
184 }
185
186 TestInputStream in(out, m_id % ((m_options & PipeIn) ? 4 : 3));
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
210 class ziptest : public ArchiveTestSuite
211 {
212 public:
213 ziptest();
214 static CppUnit::Test *suite() { return (new ziptest)->makeSuite(); }
215
216 protected:
217 ArchiveTestSuite *makeSuite();
218
219 CppUnit::Test *makeTest(string descr, int options,
220 bool genericInterface, const wxString& archiver,
221 const wxString& unarchiver);
222 };
223
224 ziptest::ziptest()
225 : ArchiveTestSuite("zip")
226 {
227 AddArchiver(wxT("zip -qr %s *"));
228 AddUnArchiver(wxT("unzip -q %s"));
229 }
230
231 ArchiveTestSuite *ziptest::makeSuite()
232 {
233 ArchiveTestSuite::makeSuite();
234
235 #if 0
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));
242 }
243 #endif
244
245 return this;
246 }
247
248 CppUnit::Test *ziptest::makeTest(
249 string descr,
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)
260 {
261 return new ArchiveTestCase<wxArchiveClassFactory>(
262 descr, new wxZipClassFactory,
263 options, archiver, unarchiver);
264 }
265
266 return new ZipTestCase(descr, options, archiver, unarchiver);
267 }
268
269 CPPUNIT_TEST_SUITE_REGISTRATION(ziptest);
270 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ziptest, "archive");
271 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ziptest, "archive/zip");
272
273 #endif // wxUSE_STREAMS && wxUSE_ZIPSTREAM