591fef32e52aa4be28d9d791b0a86842793c2def
[wxWidgets.git] / tests / misc / garbage.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/misc/garbage.cpp
3 // Purpose: test if loading garbage fails
4 // Author: Francesco Montorsi
5 // Created: 2009-01-10
6 // RCS-ID: $Id$
7 // Copyright: (c) 2009 Francesco Montorsi
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #include "wx/filename.h"
21 #include "wx/image.h"
22 #include "wx/animate.h"
23 #include "wx/mstream.h"
24 #include "wx/dynlib.h"
25 #include "wx/mediactrl.h"
26 #include "wx/html/htmlwin.h"
27 #include "wx/xrc/xmlres.h"
28
29 #define GARBAGE_DATA_SIZE 1000000 // in bytes; ~ 1MB
30
31 // ----------------------------------------------------------------------------
32 // test class
33 // ----------------------------------------------------------------------------
34
35 class GarbageTestCase : public CppUnit::TestCase
36 {
37 public:
38 GarbageTestCase() { }
39
40 private:
41 CPPUNIT_TEST_SUITE( GarbageTestCase );
42 CPPUNIT_TEST( LoadGarbage );
43 CPPUNIT_TEST_SUITE_END();
44
45 void LoadGarbage();
46 void DoLoadFile(const wxString& fullname);
47 void DoLoadStream(wxInputStream& stream);
48
49 DECLARE_NO_COPY_CLASS(GarbageTestCase)
50 };
51
52 // register in the unnamed registry so that these tests are run by default
53 CPPUNIT_TEST_SUITE_REGISTRATION( GarbageTestCase );
54
55 // also include in it's own registry so that these tests can be run alone
56 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( GarbageTestCase, "GarbageTestCase" );
57
58
59 void GarbageTestCase::LoadGarbage()
60 {
61 srand(1234);
62
63 wxInitAllImageHandlers();
64
65 for (size_t size = 1; size < GARBAGE_DATA_SIZE; size *= size+1)
66 {
67 // first, generate some garbage data
68 unsigned char *data = new unsigned char[size];
69 for (size_t i = 0; i < size; i++)
70 data[i] = rand();
71
72 // write it to a file
73 wxString garbagename = wxFileName::CreateTempFileName("garbage");
74 CPPUNIT_ASSERT( !garbagename.empty() );
75
76 wxFile garbage(garbagename, wxFile::write);
77 CPPUNIT_ASSERT( garbage.IsOpened() );
78
79 CPPUNIT_ASSERT( garbage.Write(data, size) == size );
80 garbage.Close();
81
82 // try to load it by name
83 DoLoadFile(garbagename);
84
85 // try to load it from a wxInputStream
86 wxMemoryInputStream stream(data, size);
87 DoLoadStream(stream);
88
89 wxDELETEA(data);
90 }
91 }
92
93 void GarbageTestCase::DoLoadFile(const wxString& fullname)
94 {
95 // test wxImage
96 wxImage img;
97 CPPUNIT_ASSERT( img.LoadFile(fullname) == false );
98 // test with the default wxBITMAP_TYPE_ANY
99
100 for (int type = wxBITMAP_TYPE_BMP; type < wxBITMAP_TYPE_ANY; type++)
101 CPPUNIT_ASSERT( img.LoadFile(fullname, (wxBitmapType)type) == false );
102 // test with all other possible wxBITMAP_TYPE_* flags
103
104 // test wxBitmap
105 wxBitmap bmp;
106 CPPUNIT_ASSERT( bmp.LoadFile(fullname) == false );
107 // test with the default wxBITMAP_TYPE_ANY
108
109 for (int type = wxBITMAP_TYPE_BMP; type < wxBITMAP_TYPE_ANY; type++)
110 CPPUNIT_ASSERT( bmp.LoadFile(fullname, (wxBitmapType)type) == false );
111 // test with all other possible wxBITMAP_TYPE_* flags
112
113 // test wxIcon
114 wxIcon icon;
115 CPPUNIT_ASSERT( icon.LoadFile(fullname) == false );
116 // test with the default wxICON_DEFAULT_TYPE
117
118 for (int type = wxBITMAP_TYPE_BMP; type < wxBITMAP_TYPE_ANY; type++)
119 CPPUNIT_ASSERT( icon.LoadFile(fullname, (wxBitmapType)type) == false );
120 // test with all other possible wxBITMAP_TYPE_* flags
121
122
123 // test wxAnimation
124 wxAnimation anim;
125 CPPUNIT_ASSERT( anim.LoadFile(fullname) == false );
126 // test with the default wxANIMATION_TYPE_ANY
127
128 for (int type = wxANIMATION_TYPE_INVALID+1; type < wxANIMATION_TYPE_ANY; type++)
129 CPPUNIT_ASSERT( anim.LoadFile(fullname, (wxAnimationType)type) == false );
130 // test with all other possible wxANIMATION_TYPE_* flags
131
132
133 // test wxDynamicLibrary
134 wxDynamicLibrary lib;
135 CPPUNIT_ASSERT( lib.Load(fullname) == false );
136 // test with the default wxANIMATION_TYPE_ANY
137 /*
138 #if wxUSE_MEDIACTRL
139 // test wxMediaCtrl
140 wxMediaCtrl *media = new wxMediaCtrl(wxTheApp->GetTopWindow());
141 CPPUNIT_ASSERT( media->Load(fullname) == false );
142 #endif
143
144 // test wxHtmlWindow
145 wxHtmlWindow *htmlwin = new wxHtmlWindow(wxTheApp->GetTopWindow());
146 CPPUNIT_ASSERT( htmlwin->LoadFile(fullname) == false );
147 delete htmlwin;
148 */
149 // test wxXmlResource
150 CPPUNIT_ASSERT( wxXmlResource::Get()->Load(fullname) == false );
151 }
152
153 void GarbageTestCase::DoLoadStream(wxInputStream& stream)
154 {
155 // NOTE: not all classes tested by DoLoadFile() supports loading
156 // from an input stream!
157
158 // test wxImage
159 wxImage img;
160 CPPUNIT_ASSERT( img.LoadFile(stream) == false );
161 // test with the default wxBITMAP_TYPE_ANY
162
163 for (int type = wxBITMAP_TYPE_INVALID+1; type < wxBITMAP_TYPE_ANY; type++)
164 CPPUNIT_ASSERT( img.LoadFile(stream, (wxBitmapType)type) == false );
165 // test with all other possible wxBITMAP_TYPE_* flags
166
167
168 // test wxAnimation
169 wxAnimation anim;
170 CPPUNIT_ASSERT( anim.Load(stream) == false );
171 // test with the default wxANIMATION_TYPE_ANY
172
173 for (int type = wxANIMATION_TYPE_INVALID+1; type < wxANIMATION_TYPE_ANY; type++)
174 CPPUNIT_ASSERT( anim.Load(stream, (wxAnimationType)type) == false );
175 // test with all other possible wxANIMATION_TYPE_* flags
176 /*
177 // test wxHtmlWindow
178 wxHtmlWindow *htmlwin = new wxHtmlWindow(wxTheApp->GetTopWindow());
179 CPPUNIT_ASSERT( htmlwin->LoadFile(fullname) == false );
180 delete htmlwin;
181 */
182 }
183