+///////////////////////////////////////////////////////////////////////////////
+// An additional case to check that reading corrupt archives doesn't crash
+
+class CorruptionTestCase : public CppUnit::TestCase
+{
+public:
+ CorruptionTestCase(std::string name,
+ wxArchiveClassFactory *factory,
+ int options)
+ : CppUnit::TestCase(TestId::MakeId() + name),
+ m_factory(factory),
+ m_options(options)
+ { }
+
+protected:
+ // the entry point for the test
+ void runTest();
+
+ void CreateArchive(wxOutputStream& out);
+ void ExtractArchive(wxInputStream& in);
+
+ auto_ptr<wxArchiveClassFactory> m_factory; // factory to make classes
+ int m_options; // test options
+};
+
+void CorruptionTestCase::runTest()
+{
+ TestOutputStream out(m_options);
+ CreateArchive(out);
+ TestInputStream in(out, 0);
+ wxFileOffset len = in.GetLength();
+
+ // try flipping one byte in the archive
+ int pos;
+ for (pos = 0; pos < len; pos++) {
+ char n = in[pos];
+ in[pos] = ~n;
+ ExtractArchive(in);
+ in.Rewind();
+ in[pos] = n;
+ }
+
+ // try zeroing one byte in the archive
+ for (pos = 0; pos < len; pos++) {
+ char n = in[pos];
+ in[pos] = 0;
+ ExtractArchive(in);
+ in.Rewind();
+ in[pos] = n;
+ }
+
+ // try chopping the archive off
+ for (int size = 1; size <= len; size++) {
+ in.Chop(size);
+ ExtractArchive(in);
+ in.Rewind();
+ }
+}
+
+void CorruptionTestCase::CreateArchive(wxOutputStream& out)
+{
+ auto_ptr<wxArchiveOutputStream> arc(m_factory->NewStream(out));
+
+ arc->PutNextDirEntry(_T("dir"));
+ arc->PutNextEntry(_T("file"));
+ arc->Write(_T("foo"), 3);
+}
+
+void CorruptionTestCase::ExtractArchive(wxInputStream& in)
+{
+ auto_ptr<wxArchiveInputStream> arc(m_factory->NewStream(in));
+ auto_ptr<wxArchiveEntry> entry(arc->GetNextEntry());
+
+ while (entry.get() != NULL) {
+ char buf[1024];
+
+ while (arc->IsOk())
+ arc->Read(buf, sizeof(buf));
+
+ auto_ptr<wxArchiveEntry> next(arc->GetNextEntry());
+ entry = next;
+ }
+}
+
+
+///////////////////////////////////////////////////////////////////////////////
+// Make the ids
+
+int TestId::m_seed = 6219;
+
+// static
+string TestId::MakeId()
+{
+ m_seed = (m_seed * 171) % 30269;
+ return string(wxString::Format(_T("%-6d"), m_seed).mb_str());
+}
+
+