1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
///////////////////////////////////////////////////////////////////////////////
// Name: tests/uris/ftp.cpp
// Purpose: wxFTP unit test
// Author: Francesco Montorsi (extracted from console sample)
// Created: 2010-05-23
// Copyright: (c) 2010 wxWidgets team
///////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#include "testprec.h"
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif // WX_PRECOMP
#include <wx/protocol/ftp.h>
// For this to run, the following environment variables need to be defined:
//
// - WX_FTP_TEST_HOST: the host to use for testing (e.g. ftp.example.com)
// - WX_FTP_TEST_DIR: the directory in which to perform most of the tests
// - WX_FTP_TEST_FILE: name of an existing file in this directory
//
// Optionally, WX_FTP_TEST_USER and WX_FTP_TEST_PASS may also be defined,
// otherwise anonymous FTP is used.
TEST_CASE("FTP", "[net][.]")
{
wxString hostname,
directory,
valid_filename;
if ( !wxGetEnv("WX_FTP_TEST_HOST", &hostname) ||
!wxGetEnv("WX_FTP_TEST_DIR", &directory) ||
!wxGetEnv("WX_FTP_TEST_FILE", &valid_filename) )
{
WARN("Skipping FTPTestCase because required WX_FTP_TEST_XXX "
"environment variables are not defined.");
return;
}
const wxString user = wxGetenv("WX_FTP_TEST_USER");
const wxString password = wxGetenv("WX_FTP_TEST_PASS");
class SocketInit
{
public:
SocketInit() { wxSocketBase::Initialize(); }
~SocketInit() { wxSocketBase::Shutdown(); }
} socketInit;
// wxFTP cannot be a static variable as its ctor needs to access
// wxWidgets internals after it has been initialized
wxFTP ftp;
if ( !user.empty() )
{
ftp.SetUser(user);
ftp.SetPassword(password);
}
REQUIRE( ftp.Connect(hostname) );
SECTION("List")
{
// test CWD
CPPUNIT_ASSERT( ftp.ChDir(directory) );
// test NLIST and LIST
wxArrayString files;
CPPUNIT_ASSERT( ftp.GetFilesList(files) );
CPPUNIT_ASSERT( ftp.GetDirList(files) );
CPPUNIT_ASSERT( ftp.ChDir(wxT("..")) );
}
SECTION("Download")
{
CPPUNIT_ASSERT( ftp.ChDir(directory) );
// test RETR
wxInputStream *in1 = ftp.GetInputStream("bloordyblop");
CPPUNIT_ASSERT( in1 == NULL );
delete in1;
wxInputStream *in2 = 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;
}
SECTION("FileSize")
{
CPPUNIT_ASSERT( ftp.ChDir(directory) );
CPPUNIT_ASSERT( ftp.FileExists(valid_filename) );
int size = ftp.GetFileSize(valid_filename);
CPPUNIT_ASSERT( size != -1 );
}
SECTION("Pwd")
{
CPPUNIT_ASSERT_EQUAL( "/", ftp.Pwd() );
CPPUNIT_ASSERT( ftp.ChDir(directory) );
CPPUNIT_ASSERT_EQUAL( directory, ftp.Pwd() );
}
SECTION("Misc")
{
CPPUNIT_ASSERT( ftp.SendCommand(wxT("STAT")) == '2' );
CPPUNIT_ASSERT( ftp.SendCommand(wxT("HELP SITE")) == '2' );
}
SECTION("Upload")
{
if ( user.empty() )
{
WARN("Skipping upload test when using anonymous FTP.");
return;
}
// upload a file
static const wxChar *file1 = wxT("test1");
wxOutputStream *out = 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( ftp.SendCommand(wxString(wxT("STAT ")) + file1) == '2' );
CPPUNIT_ASSERT( ftp.GetLastResult() == "11" );
}
}
|