Separate out zip specific parts of the archive test suite
[wxWidgets.git] / tests / archive / archivetest.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/archive/archivetest.h
3 // Purpose: Test the archive classes
4 // Author: Mike Wetherell
5 // RCS-ID: $Id$
6 // Copyright: (c) 2004 Mike Wetherell
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #define WX_TEST_ARCHIVE_ITERATOR
11
12 #include "wx/archive.h"
13 #include "wx/wfstream.h"
14
15
16 ///////////////////////////////////////////////////////////////////////////////
17 // Bit flags for options for the tests
18
19 enum Options
20 {
21 PipeIn = 0x01, // input streams are non-seekable
22 PipeOut = 0x02, // output streams are non-seekable
23 Stub = 0x04, // the archive should be appended to a stub
24 AllOptions = 0x07
25 };
26
27
28 ///////////////////////////////////////////////////////////////////////////////
29 // TestOutputStream and TestInputStream are memory streams which can be
30 // seekable or non-seekable.
31
32 class TestOutputStream : public wxOutputStream
33 {
34 public:
35 TestOutputStream(int options);
36
37 ~TestOutputStream() { delete [] m_data; }
38
39 int GetOptions() const { return m_options; }
40 wxFileOffset GetLength() const { return m_size; }
41
42 // gives away the data, this stream is then empty, and can be reused
43 void GetData(char*& data, size_t& size);
44
45 private:
46 void Init();
47
48 wxFileOffset OnSysSeek(wxFileOffset pos, wxSeekMode mode);
49 wxFileOffset OnSysTell() const;
50 size_t OnSysWrite(const void *buffer, size_t size);
51
52 int m_options;
53 size_t m_pos;
54 size_t m_capacity;
55 size_t m_size;
56 char *m_data;
57 };
58
59 class TestInputStream : public wxInputStream
60 {
61 public:
62 // ctor takes the data from the output stream, which is then empty
63 TestInputStream(TestOutputStream& out) : m_data(NULL) { SetData(out); }
64 // this ctor 'dups'
65 TestInputStream(const TestInputStream& in);
66 ~TestInputStream() { delete [] m_data; }
67
68 void Rewind();
69 wxFileOffset GetLength() const { return m_size; }
70 void SetData(TestOutputStream& out);
71
72 private:
73 wxFileOffset OnSysSeek(wxFileOffset pos, wxSeekMode mode);
74 wxFileOffset OnSysTell() const;
75 size_t OnSysRead(void *buffer, size_t size);
76
77 int m_options;
78 size_t m_pos;
79 size_t m_size;
80 char *m_data;
81 };
82
83
84 ///////////////////////////////////////////////////////////////////////////////
85 // wxFFile streams for piping to/from an external program
86
87 class PFileInputStream : public wxFFileInputStream
88 {
89 public:
90 PFileInputStream(const wxString& cmd);
91 ~PFileInputStream();
92 };
93
94 class PFileOutputStream : public wxFFileOutputStream
95 {
96 public:
97 PFileOutputStream(const wxString& cmd);
98 ~PFileOutputStream();
99 };
100
101
102 ///////////////////////////////////////////////////////////////////////////////
103 // A class to hold a test entry
104
105 class TestEntry
106 {
107 public:
108 TestEntry(const wxDateTime& dt, int len, const char *data);
109 ~TestEntry() { delete [] m_data; }
110
111 wxDateTime GetDateTime() const { return m_dt; }
112 wxFileOffset GetLength() const { return m_len; }
113 size_t GetSize() const { return m_len; }
114 const char *GetData() const { return m_data; }
115 wxString GetComment() const { return m_comment; }
116 bool IsText() const { return m_isText; }
117
118 void SetComment(const wxString& comment) { m_comment = comment; }
119 void SetDateTime(const wxDateTime& dt) { m_dt = dt; }
120
121 private:
122 wxDateTime m_dt;
123 size_t m_len;
124 char *m_data;
125 wxString m_comment;
126 bool m_isText;
127 };
128
129
130 ///////////////////////////////////////////////////////////////////////////////
131 // The test case
132
133 template <class ClassFactoryT>
134 class ArchiveTestCase : public CppUnit::TestCase
135 {
136 public:
137 ArchiveTestCase(std::string name,
138 int id,
139 ClassFactoryT *factory,
140 int options,
141 const wxString& archiver = wxEmptyString,
142 const wxString& unarchiver = wxEmptyString);
143
144 ~ArchiveTestCase();
145
146 protected:
147 // the classes to test
148 typedef typename ClassFactoryT::entry_type EntryT;
149 typedef typename ClassFactoryT::instream_type InputStreamT;
150 typedef typename ClassFactoryT::outstream_type OutputStreamT;
151 typedef typename ClassFactoryT::notifier_type NotifierT;
152 typedef typename ClassFactoryT::iter_type IterT;
153 typedef typename ClassFactoryT::pairiter_type PairIterT;
154
155 // the entry point for the test
156 void runTest();
157
158 // create the test data
159 void CreateTestData();
160 TestEntry& Add(const char *name, const char *data, int len = -1);
161 TestEntry& Add(const char *name, int len = 0, int value = EOF);
162
163 // 'archive up' the test data
164 void CreateArchive(wxOutputStream& out);
165 void CreateArchive(wxOutputStream& out, const wxString& archiver);
166
167 // perform various modifications on the archive
168 void ModifyArchive(wxInputStream& in, wxOutputStream& out);
169
170 // extract the archive and verify its contents
171 void ExtractArchive(wxInputStream& in);
172 void ExtractArchive(wxInputStream& in, const wxString& unarchiver);
173 void VerifyDir(wxString& path, size_t rootlen = 0);
174
175 // tests for the iterators
176 void TestIterator(wxInputStream& in);
177 void TestPairIterator(wxInputStream& in);
178 void TestSmartIterator(wxInputStream& in);
179 void TestSmartPairIterator(wxInputStream& in);
180
181 // try reading two entries at the same time
182 void ReadSimultaneous(TestInputStream& in);
183
184 // overridables
185 virtual void OnCreateArchive(OutputStreamT& WXUNUSED(arc)) { }
186 virtual void OnSetNotifier(EntryT& entry);
187
188 virtual void OnArchiveExtracted(InputStreamT& WXUNUSED(arc),
189 int WXUNUSED(expectedTotal)) { }
190
191 virtual void OnCreateEntry( OutputStreamT& WXUNUSED(arc),
192 TestEntry& WXUNUSED(testEntry),
193 EntryT *entry = NULL) { (void)entry; }
194
195 virtual void OnEntryExtracted( EntryT& WXUNUSED(entry),
196 const TestEntry& WXUNUSED(testEntry),
197 InputStreamT *arc = NULL) { (void)arc; }
198
199 typedef std::map<wxString, TestEntry*> TestEntries;
200 TestEntries m_testEntries; // test data
201 std::auto_ptr<ClassFactoryT> m_factory; // factory to make classes
202 int m_options; // test options
203 wxDateTime m_timeStamp; // timestamp to give test entries
204 int m_id; // select between the possibilites
205 wxString m_archiver; // external archiver
206 wxString m_unarchiver; // external unarchiver
207 };
208
209
210 ///////////////////////////////////////////////////////////////////////////////
211 // Base class for the archive test suites
212
213 class ArchiveTestSuite : public CppUnit::TestSuite
214 {
215 public:
216 ArchiveTestSuite(std::string name);
217
218 protected:
219 int m_id;
220
221 virtual ArchiveTestSuite *makeSuite();
222
223 virtual CppUnit::Test *makeTest(std::string descr,
224 int id,
225 int options,
226 bool genericInterface,
227 const wxString& archiver,
228 const wxString& unarchiver);
229
230 void AddArchiver(const wxString& cmd) { AddCmd(m_archivers, cmd); }
231 void AddUnArchiver(const wxString &cmd) { AddCmd(m_unarchivers, cmd); }
232 bool IsInPath(const wxString& cmd);
233
234 std::string Description(const wxString& type,
235 int options,
236 bool genericInterface = false,
237 const wxString& archiver = wxEmptyString,
238 const wxString& unarchiver = wxEmptyString);
239
240 private:
241 wxString m_name;
242 wxPathList m_path;
243 wxArrayString m_archivers;
244 wxArrayString m_unarchivers;
245
246 void AddCmd(wxArrayString& cmdlist, const wxString& cmd);
247 };