]> git.saurik.com Git - wxWidgets.git/blobdiff - tests/archive/archivetest.cpp
added wxICON_NONE and implement support for it in wxGTK (closes #2897)
[wxWidgets.git] / tests / archive / archivetest.cpp
index 4550f7b9afe5b18139652df5f82d7819a342bf48..c9f6d69c0b618474dc093268f2b275d5d6adae9e 100644 (file)
@@ -216,6 +216,8 @@ void TestInputStream::Rewind()
         m_wbacksize = 0;
         m_wbackcur = 0;
     }
+
+    Reset();
 }
 
 void TestInputStream::SetData(TestOutputStream& out)
@@ -224,7 +226,6 @@ void TestInputStream::SetData(TestOutputStream& out)
     m_options = out.GetOptions();
     out.GetData(m_data, m_size);
     Rewind();
-    Reset();
 }
 
 wxFileOffset TestInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
@@ -268,10 +269,12 @@ size_t TestInputStream::OnSysRead(void *buffer, size_t size)
     }
 
     if (((m_eoftype & AtLast) != 0 && m_pos >= m_size) || count < size)
+    {
         if ((m_eoftype & WithError) != 0)
             m_lasterror = wxSTREAM_READ_ERROR;
         else
             m_lasterror = wxSTREAM_EOF;
+    }
 
     return count;
 }
@@ -644,7 +647,7 @@ void ArchiveTestCase<ClassFactoryT>::CreateArchive(wxOutputStream& out,
         TestEntry& entry = *i->second;
 
         if (fn.IsDir()) {
-            fn.Mkdir(0777, wxPATH_MKDIR_FULL);
+            wxFileName::Mkdir(fn.GetPath(), 0777, wxPATH_MKDIR_FULL);
         } else {
             wxFileName::Mkdir(fn.GetPath(), 0777, wxPATH_MKDIR_FULL);
             wxFFileOutputStream fileout(fn.GetFullPath());
@@ -801,10 +804,14 @@ void ArchiveTestCase<ClassFactoryT>::ExtractArchive(wxInputStream& in)
 
         const TestEntry& testEntry = *it->second;
 
+#ifndef __WXMSW__
+        // On Windows some archivers compensate for Windows DST handling, but
+        // other don't, so disable the test for now.
         wxDateTime dt = testEntry.GetDateTime();
         if (dt.IsValid())
             CPPUNIT_ASSERT_MESSAGE("timestamp check" + error_context,
                                    dt == entry->GetDateTime());
+#endif
 
         // non-seekable entries are allowed to have GetSize == wxInvalidOffset
         // until the end of the entry's data has been read past
@@ -1168,6 +1175,91 @@ void ArchiveTestCase<ClassFactoryT>::OnSetNotifier(EntryT& entry)
 }
 
 
+///////////////////////////////////////////////////////////////////////////////
+// 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
 
@@ -1177,7 +1269,7 @@ int TestId::m_seed = 6219;
 string TestId::MakeId()
 {
     m_seed = (m_seed * 171) % 30269;
-    return (const char *)wxString::Format(_T("%-6d"), m_seed).mb_str();
+    return string(wxString::Format(_T("%-6d"), m_seed).mb_str());
 }
 
 
@@ -1240,6 +1332,23 @@ ArchiveTestSuite *ArchiveTestSuite::makeSuite()
                         addTest(test);
                 }
 
+    for (int options = 0; options <= PipeIn; options += PipeIn)
+    {
+        wxObject *pObj = wxCreateDynamicObject(m_name + _T("ClassFactory"));
+        wxArchiveClassFactory *factory;
+        factory = wxDynamicCast(pObj, wxArchiveClassFactory);
+
+        if (factory) {
+            string descr(m_name.mb_str());
+            descr = "CorruptionTestCase (" + descr + ")";
+
+            if (options)
+                descr += " (PipeIn)";
+
+            addTest(new CorruptionTestCase(descr, factory, options));
+        }
+    }
+
     return this;
 }
 
@@ -1290,7 +1399,7 @@ string ArchiveTestSuite::Description(const wxString& type,
 
     descr << optstr;
 
-    return (const char*)descr.mb_str();
+    return string(descr.mb_str());
 }
 
 
@@ -1304,4 +1413,9 @@ template class ArchiveTestCase<wxArchiveClassFactory>;
 template class ArchiveTestCase<wxZipClassFactory>;
 #endif
 
+#if wxUSE_TARSTREAM
+#include "wx/tarstrm.h"
+template class ArchiveTestCase<wxTarClassFactory>;
+#endif
+
 #endif // wxUSE_STREAMS && wxUSE_ARCHIVE_STREAMS