garbage.cpp 5.7 KB

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