From ec0e0939137473cdd18aab3713b9aa91e25bba9c Mon Sep 17 00:00:00 2001 From: Francesco Montorsi Date: Sat, 29 May 2010 07:56:08 +0000 Subject: [PATCH] move all non-interactive FTP tests from the console sample to a new CppUnit FTPTestCase test. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@64422 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- samples/console/console.cpp | 216 ++++-------------------------------- tests/Makefile.in | 4 + tests/makefile.bcc | 4 + tests/makefile.gcc | 4 + tests/makefile.vc | 4 + tests/makefile.wat | 4 + tests/test.bkl | 1 + tests/test_test.dsp | 4 + tests/test_vc7_test.vcproj | 3 + tests/test_vc8_test.vcproj | 4 + tests/test_vc9_test.vcproj | 46 ++++---- tests/uris/ftp.cpp | 180 ++++++++++++++++++++++++++++++ 12 files changed, 255 insertions(+), 219 deletions(-) create mode 100644 tests/uris/ftp.cpp diff --git a/samples/console/console.cpp b/samples/console/console.cpp index 26f7ce0f19..71acd820f8 100644 --- a/samples/console/console.cpp +++ b/samples/console/console.cpp @@ -114,7 +114,6 @@ #define TEST_FILECONF #define TEST_FILENAME #define TEST_FILETIME - #define TEST_FTP #define TEST_INFO_FUNCTIONS #define TEST_LOCALE #define TEST_LOG @@ -128,11 +127,12 @@ #define TEST_SCOPEGUARD #define TEST_SNGLINST // #define TEST_SOCKETS --FIXME! (RN) - #define TEST_STACKWALKER - #define TEST_STDPATHS #else // #if TEST_ALL #define TEST_DATETIME #define TEST_VOLUME + #define TEST_STDPATHS + #define TEST_STACKWALKER + #define TEST_FTP #endif // some tests are interactive, define this to run them @@ -2367,9 +2367,11 @@ static void TestSocketClient() static wxFTP *ftp; #ifdef FTP_ANONYMOUS + static const wxChar *hostname = wxT("ftp.wxwidgets.org"); static const wxChar *directory = wxT("/pub"); static const wxChar *filename = wxT("welcome.msg"); #else + static const wxChar *hostname = "localhost"; static const wxChar *directory = wxT("/etc"); static const wxChar *filename = wxT("issue"); #endif @@ -2379,12 +2381,8 @@ static bool TestFtpConnect() wxPuts(wxT("*** Testing FTP connect ***")); #ifdef FTP_ANONYMOUS - static const wxChar *hostname = wxT("ftp.wxwidgets.org"); - wxPrintf(wxT("--- Attempting to connect to %s:21 anonymously...\n"), hostname); #else // !FTP_ANONYMOUS - static const wxChar *hostname = "localhost"; - wxChar user[256]; wxFgets(user, WXSIZEOF(user), stdin); user[wxStrlen(user) - 1] = '\0'; // chop off '\n' @@ -2415,140 +2413,6 @@ static bool TestFtpConnect() return true; } -static void TestFtpList() -{ - wxPuts(wxT("*** Testing wxFTP file listing ***\n")); - - // test CWD - if ( !ftp->ChDir(directory) ) - { - wxPrintf(wxT("ERROR: failed to cd to %s\n"), directory); - } - - wxPrintf(wxT("Current directory is '%s'\n"), ftp->Pwd().c_str()); - - // test NLIST and LIST - wxArrayString files; - if ( !ftp->GetFilesList(files) ) - { - wxPuts(wxT("ERROR: failed to get NLIST of files")); - } - else - { - wxPrintf(wxT("Brief list of files under '%s':\n"), ftp->Pwd().c_str()); - size_t count = files.GetCount(); - for ( size_t n = 0; n < count; n++ ) - { - wxPrintf(wxT("\t%s\n"), files[n].c_str()); - } - wxPuts(wxT("End of the file list")); - } - - if ( !ftp->GetDirList(files) ) - { - wxPuts(wxT("ERROR: failed to get LIST of files")); - } - else - { - wxPrintf(wxT("Detailed list of files under '%s':\n"), ftp->Pwd().c_str()); - size_t count = files.GetCount(); - for ( size_t n = 0; n < count; n++ ) - { - wxPrintf(wxT("\t%s\n"), files[n].c_str()); - } - wxPuts(wxT("End of the file list")); - } - - if ( !ftp->ChDir(wxT("..")) ) - { - wxPuts(wxT("ERROR: failed to cd to ..")); - } - - wxPrintf(wxT("Current directory is '%s'\n"), ftp->Pwd().c_str()); -} - -static void TestFtpDownload() -{ - wxPuts(wxT("*** Testing wxFTP download ***\n")); - - // test RETR - wxInputStream *in = ftp->GetInputStream(filename); - if ( !in ) - { - wxPrintf(wxT("ERROR: couldn't get input stream for %s\n"), filename); - } - else - { - size_t size = in->GetSize(); - wxPrintf(wxT("Reading file %s (%u bytes)..."), filename, size); - fflush(stdout); - - wxChar *data = new wxChar[size]; - if ( !in->Read(data, size) ) - { - wxPuts(wxT("ERROR: read error")); - } - else - { - wxPrintf(wxT("\nContents of %s:\n%s\n"), filename, data); - } - - delete [] data; - delete in; - } -} - -static void TestFtpFileSize() -{ - wxPuts(wxT("*** Testing FTP SIZE command ***")); - - if ( !ftp->ChDir(directory) ) - { - wxPrintf(wxT("ERROR: failed to cd to %s\n"), directory); - } - - wxPrintf(wxT("Current directory is '%s'\n"), ftp->Pwd().c_str()); - - if ( ftp->FileExists(filename) ) - { - int size = ftp->GetFileSize(filename); - if ( size == -1 ) - wxPrintf(wxT("ERROR: couldn't get size of '%s'\n"), filename); - else - wxPrintf(wxT("Size of '%s' is %d bytes.\n"), filename, size); - } - else - { - wxPrintf(wxT("ERROR: '%s' doesn't exist\n"), filename); - } -} - -static void TestFtpMisc() -{ - wxPuts(wxT("*** Testing miscellaneous wxFTP functions ***")); - - if ( ftp->SendCommand(wxT("STAT")) != '2' ) - { - wxPuts(wxT("ERROR: STAT failed")); - } - else - { - wxPrintf(wxT("STAT returned:\n\n%s\n"), ftp->GetLastResult().c_str()); - } - - if ( ftp->SendCommand(wxT("HELP SITE")) != '2' ) - { - wxPuts(wxT("ERROR: HELP SITE failed")); - } - else - { - wxPrintf(wxT("The list of site-specific commands:\n\n%s\n"), - ftp->GetLastResult().c_str()); - } -} - -#if TEST_INTERACTIVE - static void TestFtpInteractive() { wxPuts(wxT("\n*** Interactive wxFTP test ***")); @@ -2557,7 +2421,7 @@ static void TestFtpInteractive() for ( ;; ) { - wxPrintf(wxT("Enter FTP command: ")); + wxPrintf(wxT("Enter FTP command (or 'quit' to escape): ")); if ( !wxFgets(buf, WXSIZEOF(buf), stdin) ) break; @@ -2590,6 +2454,10 @@ static void TestFtpInteractive() wxPuts(wxT("--- End of the file list")); } } + else if ( start == wxT("QUIT") ) + { + break; // get out of here! + } else // !list { wxChar ch = ftp->SendCommand(buf); @@ -2606,43 +2474,6 @@ static void TestFtpInteractive() wxPuts(wxT("\n*** done ***")); } -#endif // TEST_INTERACTIVE - -static void TestFtpUpload() -{ - wxPuts(wxT("*** Testing wxFTP uploading ***\n")); - - // upload a file - static const wxChar *file1 = wxT("test1"); - static const wxChar *file2 = wxT("test2"); - wxOutputStream *out = ftp->GetOutputStream(file1); - if ( out ) - { - wxPrintf(wxT("--- Uploading to %s ---\n"), file1); - out->Write("First hello", 11); - delete out; - } - - // send a command to check the remote file - if ( ftp->SendCommand(wxString(wxT("STAT ")) + file1) != '2' ) - { - wxPrintf(wxT("ERROR: STAT %s failed\n"), file1); - } - else - { - wxPrintf(wxT("STAT %s returned:\n\n%s\n"), - file1, ftp->GetLastResult().c_str()); - } - - out = ftp->GetOutputStream(file2); - if ( out ) - { - wxPrintf(wxT("--- Uploading to %s ---\n"), file1); - out->Write("Second hello", 12); - delete out; - } -} - #endif // TEST_FTP // ---------------------------------------------------------------------------- @@ -2710,6 +2541,8 @@ static void TestStackWalk(const char *argv0) StackDump dump(argv0); dump.Walk(); + + wxPuts("\n"); } #endif // wxUSE_STACKWALKER @@ -2750,6 +2583,8 @@ static void TestStandardPaths() wxT("fr"), wxStandardPaths::ResourceCat_Messages ).c_str()); + + wxPuts("\n"); } #endif // TEST_STDPATHS @@ -2809,6 +2644,8 @@ static void TestFSVolume() vol.GetFlags() & wxFS_VOL_REMOVABLE ? wxT("removable") : wxT("fixed")); } + + wxPuts("\n"); } #endif // TEST_VOLUME @@ -2832,12 +2669,15 @@ static void TestDateTimeInteractive() for ( ;; ) { - wxPrintf(wxT("Enter a date: ")); + wxPrintf(wxT("Enter a date (or 'quit' to escape): ")); if ( !wxFgets(buf, WXSIZEOF(buf), stdin) ) break; // kill the last '\n' buf[wxStrlen(buf) - 1] = 0; + + if ( wxString(buf).CmpNoCase("quit") == 0 ) + break; wxDateTime dt; const wxChar *p = dt.ParseDate(buf); @@ -2859,8 +2699,8 @@ static void TestDateTimeInteractive() dt.GetWeekOfMonth(wxDateTime::Sunday_First), dt.GetWeekOfYear(wxDateTime::Monday_First)); } - - wxPuts(wxT("\n*** done ***")); + + wxPuts("\n"); } #endif // TEST_INTERACTIVE @@ -3056,17 +2896,7 @@ int main(int argc, char **argv) if ( TestFtpConnect() ) { - #if TEST_ALL - TestFtpList(); - TestFtpDownload(); - TestFtpMisc(); - TestFtpFileSize(); - TestFtpUpload(); - #endif // TEST_ALL - - #if TEST_INTERACTIVE - //TestFtpInteractive(); - #endif + TestFtpInteractive(); } //else: connecting to the FTP server failed diff --git a/tests/Makefile.in b/tests/Makefile.in index e5fd2ee4e6..3fc0cd9d42 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -117,6 +117,7 @@ TEST_OBJECTS = \ test_misc.o \ test_queue.o \ test_tls.o \ + test_ftp.o \ test_uris.o \ test_url.o \ test_vectors.o \ @@ -546,6 +547,9 @@ test_queue.o: $(srcdir)/thread/queue.cpp $(TEST_ODEP) test_tls.o: $(srcdir)/thread/tls.cpp $(TEST_ODEP) $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/thread/tls.cpp +test_ftp.o: $(srcdir)/uris/ftp.cpp $(TEST_ODEP) + $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/uris/ftp.cpp + test_uris.o: $(srcdir)/uris/uris.cpp $(TEST_ODEP) $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/uris/uris.cpp diff --git a/tests/makefile.bcc b/tests/makefile.bcc index a079fb9059..fe93077865 100644 --- a/tests/makefile.bcc +++ b/tests/makefile.bcc @@ -101,6 +101,7 @@ TEST_OBJECTS = \ $(OBJS)\test_misc.obj \ $(OBJS)\test_queue.obj \ $(OBJS)\test_tls.obj \ + $(OBJS)\test_ftp.obj \ $(OBJS)\test_uris.obj \ $(OBJS)\test_url.obj \ $(OBJS)\test_vectors.obj \ @@ -588,6 +589,9 @@ $(OBJS)\test_queue.obj: .\thread\queue.cpp $(OBJS)\test_tls.obj: .\thread\tls.cpp $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\thread\tls.cpp +$(OBJS)\test_ftp.obj: .\uris\ftp.cpp + $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\uris\ftp.cpp + $(OBJS)\test_uris.obj: .\uris\uris.cpp $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\uris\uris.cpp diff --git a/tests/makefile.gcc b/tests/makefile.gcc index 42d881e88e..f93cc1b112 100644 --- a/tests/makefile.gcc +++ b/tests/makefile.gcc @@ -93,6 +93,7 @@ TEST_OBJECTS = \ $(OBJS)\test_misc.o \ $(OBJS)\test_queue.o \ $(OBJS)\test_tls.o \ + $(OBJS)\test_ftp.o \ $(OBJS)\test_uris.o \ $(OBJS)\test_url.o \ $(OBJS)\test_vectors.o \ @@ -569,6 +570,9 @@ $(OBJS)\test_queue.o: ./thread/queue.cpp $(OBJS)\test_tls.o: ./thread/tls.cpp $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< +$(OBJS)\test_ftp.o: ./uris/ftp.cpp + $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< + $(OBJS)\test_uris.o: ./uris/uris.cpp $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< diff --git a/tests/makefile.vc b/tests/makefile.vc index cc32e91070..0b787b683a 100644 --- a/tests/makefile.vc +++ b/tests/makefile.vc @@ -95,6 +95,7 @@ TEST_OBJECTS = \ $(OBJS)\test_misc.obj \ $(OBJS)\test_queue.obj \ $(OBJS)\test_tls.obj \ + $(OBJS)\test_ftp.obj \ $(OBJS)\test_uris.obj \ $(OBJS)\test_url.obj \ $(OBJS)\test_vectors.obj \ @@ -714,6 +715,9 @@ $(OBJS)\test_queue.obj: .\thread\queue.cpp $(OBJS)\test_tls.obj: .\thread\tls.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\thread\tls.cpp +$(OBJS)\test_ftp.obj: .\uris\ftp.cpp + $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\uris\ftp.cpp + $(OBJS)\test_uris.obj: .\uris\uris.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\uris\uris.cpp diff --git a/tests/makefile.wat b/tests/makefile.wat index 67b82aead3..90a8887763 100644 --- a/tests/makefile.wat +++ b/tests/makefile.wat @@ -331,6 +331,7 @@ TEST_OBJECTS = & $(OBJS)\test_misc.obj & $(OBJS)\test_queue.obj & $(OBJS)\test_tls.obj & + $(OBJS)\test_ftp.obj & $(OBJS)\test_uris.obj & $(OBJS)\test_url.obj & $(OBJS)\test_vectors.obj & @@ -626,6 +627,9 @@ $(OBJS)\test_queue.obj : .AUTODEPEND .\thread\queue.cpp $(OBJS)\test_tls.obj : .AUTODEPEND .\thread\tls.cpp $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $< +$(OBJS)\test_ftp.obj : .AUTODEPEND .\uris\ftp.cpp + $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $< + $(OBJS)\test_uris.obj : .AUTODEPEND .\uris\uris.cpp $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $< diff --git a/tests/test.bkl b/tests/test.bkl index 3ce77af3af..34e0e723c1 100644 --- a/tests/test.bkl +++ b/tests/test.bkl @@ -92,6 +92,7 @@ thread/misc.cpp thread/queue.cpp thread/tls.cpp + uris/ftp.cpp uris/uris.cpp uris/url.cpp vectors/vectors.cpp diff --git a/tests/test_test.dsp b/tests/test_test.dsp index 981800bbbc..b79c58606a 100644 --- a/tests/test_test.dsp +++ b/tests/test_test.dsp @@ -345,6 +345,10 @@ SOURCE=.\fswatcher\fswatchertest.cpp # End Source File # Begin Source File +SOURCE=.\uris\ftp.cpp +# End Source File +# Begin Source File + SOURCE=.\hashes\hashes.cpp # End Source File # Begin Source File diff --git a/tests/test_vc7_test.vcproj b/tests/test_vc7_test.vcproj index 284639384d..6237a140f1 100644 --- a/tests/test_vc7_test.vcproj +++ b/tests/test_vc7_test.vcproj @@ -694,6 +694,9 @@ + + diff --git a/tests/test_vc8_test.vcproj b/tests/test_vc8_test.vcproj index 8215eacc9f..c3b9d9b5e2 100644 --- a/tests/test_vc8_test.vcproj +++ b/tests/test_vc8_test.vcproj @@ -999,6 +999,10 @@ RelativePath=".\fswatcher\fswatchertest.cpp" > + + diff --git a/tests/test_vc9_test.vcproj b/tests/test_vc9_test.vcproj index 2b48c4b5d2..7b1b284d9d 100644 --- a/tests/test_vc9_test.vcproj +++ b/tests/test_vc9_test.vcproj @@ -1,16 +1,10 @@ - - - + + @@ -1154,7 +1150,5 @@ - - diff --git a/tests/uris/ftp.cpp b/tests/uris/ftp.cpp new file mode 100644 index 0000000000..671b606b8b --- /dev/null +++ b/tests/uris/ftp.cpp @@ -0,0 +1,180 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: tests/uris/ftp.cpp +// Purpose: wxFTP unit test +// Author: Francesco Montorsi (extracted from console sample) +// Created: 2010-05-23 +// RCS-ID: $Id$ +// Copyright: (c) 2010 wxWidgets team +/////////////////////////////////////////////////////////////////////////////// + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + +#include "testprec.h" + +#ifdef __BORLANDC__ + #pragma hdrstop +#endif + +#ifndef WX_PRECOMP + #include "wx/wx.h" +#endif // WX_PRECOMP + +#include + +#define FTP_ANONYMOUS + +#ifdef FTP_ANONYMOUS + static const char *hostname = "ftp.wxwidgets.org"; + static const char *directory = "/pub/2.8.11"; + static const char *invalid_filename = "a_file_which_does_not_exist"; + static const char *valid_filename = "MD5SUM"; + // NOTE: choose a small file or otherwise the FTPTestCase::Download() + // function will take (a lot of) time to complete! +#else + static const char *hostname = "localhost"; + static const char *user = "guest"; + static const char *password = ""; + static const char *directory = "/etc"; + static const char *invalid_filename = "issue"; + static const char *valid_filename = "hosts"; +#endif + +// ---------------------------------------------------------------------------- +// test class +// ---------------------------------------------------------------------------- + +class FTPTestCase : public CppUnit::TestCase +{ +public: + FTPTestCase() {} + + virtual void setUp(); + virtual void tearDown(); + +private: + CPPUNIT_TEST_SUITE( FTPTestCase ); + CPPUNIT_TEST( List ); + CPPUNIT_TEST( Download ); + CPPUNIT_TEST( FileSize ); + CPPUNIT_TEST( Misc ); +#ifndef FTP_ANONYMOUS + CPPUNIT_TEST( Upload ); +#endif + CPPUNIT_TEST_SUITE_END(); + + void List(); + void Download(); + void FileSize(); + void Misc(); + void Upload(); + + wxFTP *m_ftp; + + DECLARE_NO_COPY_CLASS(FTPTestCase) +}; + +// register in the unnamed registry so that these tests are run by default +CPPUNIT_TEST_SUITE_REGISTRATION( FTPTestCase ); + +// also include in it's own registry so that these tests can be run alone +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FTPTestCase, "FTPTestCase" ); + + +void FTPTestCase::setUp() +{ + wxSocketBase::Initialize(); + + // wxFTP cannot be a static variable as its ctor needs to access + // wxWidgets internals after it has been initialized + m_ftp = new wxFTP; + +#ifndef FTP_ANONYMOUS + m_ftp->SetUser(user); + m_ftp->SetPassword(password); +#endif // FTP_ANONYMOUS/!FTP_ANONYMOUS +} + +void FTPTestCase::tearDown() +{ + delete m_ftp; + + wxSocketBase::Shutdown(); +} + +void FTPTestCase::List() +{ + CPPUNIT_ASSERT( m_ftp->Connect(hostname) ); + + // test CWD + CPPUNIT_ASSERT( m_ftp->ChDir(directory) ); + + // test NLIST and LIST + wxArrayString files; + CPPUNIT_ASSERT( m_ftp->GetFilesList(files) ); + CPPUNIT_ASSERT( m_ftp->GetDirList(files) ); + + CPPUNIT_ASSERT( m_ftp->ChDir(wxT("..")) ); +} + +void FTPTestCase::Download() +{ + CPPUNIT_ASSERT( m_ftp->Connect(hostname) ); + CPPUNIT_ASSERT( m_ftp->ChDir(directory) ); + + // test RETR + wxInputStream *in1 = m_ftp->GetInputStream(invalid_filename); + CPPUNIT_ASSERT( in1 == NULL ); + delete in1; + + wxInputStream *in2 = m_ftp->GetInputStream(valid_filename); + CPPUNIT_ASSERT( in2 != NULL ); + + size_t size = in2->GetSize(); + wxChar *data = new wxChar[size]; + CPPUNIT_ASSERT( in2->Read(data, size).GetLastError() == wxSTREAM_NO_ERROR ); + + delete [] data; + delete in2; +} + +void FTPTestCase::FileSize() +{ + CPPUNIT_ASSERT( m_ftp->Connect(hostname) ); + + CPPUNIT_ASSERT( m_ftp->ChDir(directory) ); + + CPPUNIT_ASSERT( m_ftp->FileExists(valid_filename) ); + + int size = m_ftp->GetFileSize(valid_filename); + CPPUNIT_ASSERT( size != -1 ); +} + +void FTPTestCase::Misc() +{ + CPPUNIT_ASSERT( m_ftp->Connect(hostname) ); + + CPPUNIT_ASSERT( m_ftp->SendCommand(wxT("STAT")) == '2' ); + CPPUNIT_ASSERT( m_ftp->SendCommand(wxT("HELP SITE")) == '2' ); +} + +#ifndef FTP_ANONYMOUS +void FTPTestCase::Upload() +{ + CPPUNIT_ASSERT( m_ftp->Connect(hostname) ); + + // upload a file + static const wxChar *file1 = wxT("test1"); + wxOutputStream *out = m_ftp->GetOutputStream(file1); + CPPUNIT_ASSERT( out != NULL ); + CPPUNIT_ASSERT( out->Write("First hello", 11).GetLastError() == wxSTREAM_NO_ERROR ); + delete out; + + // send a command to check the remote file + CPPUNIT_ASSERT( m_ftp->SendCommand(wxString(wxT("STAT ")) + file1) == '2' ); + CPPUNIT_ASSERT( m_ftp->GetLastResult() == "11" ); +} +#endif + + -- 2.45.2