ftp.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/uris/ftp.cpp
  3. // Purpose: wxFTP unit test
  4. // Author: Francesco Montorsi (extracted from console sample)
  5. // Created: 2010-05-23
  6. // Copyright: (c) 2010 wxWidgets team
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // ----------------------------------------------------------------------------
  9. // headers
  10. // ----------------------------------------------------------------------------
  11. #include "testprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #ifndef WX_PRECOMP
  16. #include "wx/wx.h"
  17. #endif // WX_PRECOMP
  18. #include <wx/protocol/ftp.h>
  19. #define FTP_ANONYMOUS
  20. #ifdef FTP_ANONYMOUS
  21. static const char *hostname = "ftp.wxwidgets.org";
  22. static const char *directory = "/pub/2.8.11";
  23. static const char *invalid_filename = "a_file_which_does_not_exist";
  24. static const char *valid_filename = "MD5SUM";
  25. // NOTE: choose a small file or otherwise the FTPTestCase::Download()
  26. // function will take (a lot of) time to complete!
  27. #else
  28. static const char *hostname = "localhost";
  29. static const char *user = "guest";
  30. static const char *password = "";
  31. static const char *directory = "/etc";
  32. static const char *invalid_filename = "issue";
  33. static const char *valid_filename = "hosts";
  34. #endif
  35. // ----------------------------------------------------------------------------
  36. // test class
  37. // ----------------------------------------------------------------------------
  38. class FTPTestCase : public CppUnit::TestCase
  39. {
  40. public:
  41. FTPTestCase() {}
  42. virtual void setUp();
  43. virtual void tearDown();
  44. private:
  45. CPPUNIT_TEST_SUITE( FTPTestCase );
  46. CPPUNIT_TEST( List );
  47. CPPUNIT_TEST( Download );
  48. CPPUNIT_TEST( FileSize );
  49. CPPUNIT_TEST( Misc );
  50. #ifndef FTP_ANONYMOUS
  51. CPPUNIT_TEST( Upload );
  52. #endif
  53. CPPUNIT_TEST_SUITE_END();
  54. void List();
  55. void Download();
  56. void FileSize();
  57. void Misc();
  58. void Upload();
  59. wxFTP *m_ftp;
  60. DECLARE_NO_COPY_CLASS(FTPTestCase)
  61. };
  62. // NOTE: we do not run FTPTestCase suite by default because buildslaves typically
  63. // do not have FTP connectivity enabled by default...
  64. //CPPUNIT_TEST_SUITE_REGISTRATION( FTPTestCase );
  65. // also include in its own registry so that these tests can be run alone
  66. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FTPTestCase, "FTPTestCase" );
  67. void FTPTestCase::setUp()
  68. {
  69. wxSocketBase::Initialize();
  70. // wxFTP cannot be a static variable as its ctor needs to access
  71. // wxWidgets internals after it has been initialized
  72. m_ftp = new wxFTP;
  73. #ifndef FTP_ANONYMOUS
  74. m_ftp->SetUser(user);
  75. m_ftp->SetPassword(password);
  76. #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
  77. }
  78. void FTPTestCase::tearDown()
  79. {
  80. delete m_ftp;
  81. wxSocketBase::Shutdown();
  82. }
  83. void FTPTestCase::List()
  84. {
  85. CPPUNIT_ASSERT( m_ftp->Connect(hostname) );
  86. // test CWD
  87. CPPUNIT_ASSERT( m_ftp->ChDir(directory) );
  88. // test NLIST and LIST
  89. wxArrayString files;
  90. CPPUNIT_ASSERT( m_ftp->GetFilesList(files) );
  91. CPPUNIT_ASSERT( m_ftp->GetDirList(files) );
  92. CPPUNIT_ASSERT( m_ftp->ChDir(wxT("..")) );
  93. }
  94. void FTPTestCase::Download()
  95. {
  96. CPPUNIT_ASSERT( m_ftp->Connect(hostname) );
  97. CPPUNIT_ASSERT( m_ftp->ChDir(directory) );
  98. // test RETR
  99. wxInputStream *in1 = m_ftp->GetInputStream(invalid_filename);
  100. CPPUNIT_ASSERT( in1 == NULL );
  101. delete in1;
  102. wxInputStream *in2 = m_ftp->GetInputStream(valid_filename);
  103. CPPUNIT_ASSERT( in2 != NULL );
  104. size_t size = in2->GetSize();
  105. wxChar *data = new wxChar[size];
  106. CPPUNIT_ASSERT( in2->Read(data, size).GetLastError() == wxSTREAM_NO_ERROR );
  107. delete [] data;
  108. delete in2;
  109. }
  110. void FTPTestCase::FileSize()
  111. {
  112. CPPUNIT_ASSERT( m_ftp->Connect(hostname) );
  113. CPPUNIT_ASSERT( m_ftp->ChDir(directory) );
  114. CPPUNIT_ASSERT( m_ftp->FileExists(valid_filename) );
  115. int size = m_ftp->GetFileSize(valid_filename);
  116. CPPUNIT_ASSERT( size != -1 );
  117. }
  118. void FTPTestCase::Misc()
  119. {
  120. CPPUNIT_ASSERT( m_ftp->Connect(hostname) );
  121. CPPUNIT_ASSERT( m_ftp->SendCommand(wxT("STAT")) == '2' );
  122. CPPUNIT_ASSERT( m_ftp->SendCommand(wxT("HELP SITE")) == '2' );
  123. }
  124. #ifndef FTP_ANONYMOUS
  125. void FTPTestCase::Upload()
  126. {
  127. CPPUNIT_ASSERT( m_ftp->Connect(hostname) );
  128. // upload a file
  129. static const wxChar *file1 = wxT("test1");
  130. wxOutputStream *out = m_ftp->GetOutputStream(file1);
  131. CPPUNIT_ASSERT( out != NULL );
  132. CPPUNIT_ASSERT( out->Write("First hello", 11).GetLastError() == wxSTREAM_NO_ERROR );
  133. delete out;
  134. // send a command to check the remote file
  135. CPPUNIT_ASSERT( m_ftp->SendCommand(wxString(wxT("STAT ")) + file1) == '2' );
  136. CPPUNIT_ASSERT( m_ftp->GetLastResult() == "11" );
  137. }
  138. #endif