]>
git.saurik.com Git - wxWidgets.git/blob - tests/uris/ftp.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/uris/ftp.cpp
3 // Purpose: wxFTP unit test
4 // Author: Francesco Montorsi (extracted from console sample)
7 // Copyright: (c) 2010 wxWidgets team
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
24 #include <wx/protocol/ftp.h>
29 static const char *hostname
= "ftp.wxwidgets.org";
30 static const char *directory
= "/pub/2.8.11";
31 static const char *invalid_filename
= "a_file_which_does_not_exist";
32 static const char *valid_filename
= "MD5SUM";
33 // NOTE: choose a small file or otherwise the FTPTestCase::Download()
34 // function will take (a lot of) time to complete!
36 static const char *hostname
= "localhost";
37 static const char *user
= "guest";
38 static const char *password
= "";
39 static const char *directory
= "/etc";
40 static const char *invalid_filename
= "issue";
41 static const char *valid_filename
= "hosts";
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 class FTPTestCase
: public CppUnit::TestCase
54 virtual void tearDown();
57 CPPUNIT_TEST_SUITE( FTPTestCase
);
59 CPPUNIT_TEST( Download
);
60 CPPUNIT_TEST( FileSize
);
63 CPPUNIT_TEST( Upload
);
65 CPPUNIT_TEST_SUITE_END();
75 DECLARE_NO_COPY_CLASS(FTPTestCase
)
78 // NOTE: we do not run FTPTestCase suite by default because buildslaves typically
79 // do not have FTP connectivity enabled by default...
80 //CPPUNIT_TEST_SUITE_REGISTRATION( FTPTestCase );
82 // also include in it's own registry so that these tests can be run alone
83 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FTPTestCase
, "FTPTestCase" );
86 void FTPTestCase::setUp()
88 wxSocketBase::Initialize();
90 // wxFTP cannot be a static variable as its ctor needs to access
91 // wxWidgets internals after it has been initialized
96 m_ftp
->SetPassword(password
);
97 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
100 void FTPTestCase::tearDown()
104 wxSocketBase::Shutdown();
107 void FTPTestCase::List()
109 CPPUNIT_ASSERT( m_ftp
->Connect(hostname
) );
112 CPPUNIT_ASSERT( m_ftp
->ChDir(directory
) );
114 // test NLIST and LIST
116 CPPUNIT_ASSERT( m_ftp
->GetFilesList(files
) );
117 CPPUNIT_ASSERT( m_ftp
->GetDirList(files
) );
119 CPPUNIT_ASSERT( m_ftp
->ChDir(wxT("..")) );
122 void FTPTestCase::Download()
124 CPPUNIT_ASSERT( m_ftp
->Connect(hostname
) );
125 CPPUNIT_ASSERT( m_ftp
->ChDir(directory
) );
128 wxInputStream
*in1
= m_ftp
->GetInputStream(invalid_filename
);
129 CPPUNIT_ASSERT( in1
== NULL
);
132 wxInputStream
*in2
= m_ftp
->GetInputStream(valid_filename
);
133 CPPUNIT_ASSERT( in2
!= NULL
);
135 size_t size
= in2
->GetSize();
136 wxChar
*data
= new wxChar
[size
];
137 CPPUNIT_ASSERT( in2
->Read(data
, size
).GetLastError() == wxSTREAM_NO_ERROR
);
143 void FTPTestCase::FileSize()
145 CPPUNIT_ASSERT( m_ftp
->Connect(hostname
) );
147 CPPUNIT_ASSERT( m_ftp
->ChDir(directory
) );
149 CPPUNIT_ASSERT( m_ftp
->FileExists(valid_filename
) );
151 int size
= m_ftp
->GetFileSize(valid_filename
);
152 CPPUNIT_ASSERT( size
!= -1 );
155 void FTPTestCase::Misc()
157 CPPUNIT_ASSERT( m_ftp
->Connect(hostname
) );
159 CPPUNIT_ASSERT( m_ftp
->SendCommand(wxT("STAT")) == '2' );
160 CPPUNIT_ASSERT( m_ftp
->SendCommand(wxT("HELP SITE")) == '2' );
163 #ifndef FTP_ANONYMOUS
164 void FTPTestCase::Upload()
166 CPPUNIT_ASSERT( m_ftp
->Connect(hostname
) );
169 static const wxChar
*file1
= wxT("test1");
170 wxOutputStream
*out
= m_ftp
->GetOutputStream(file1
);
171 CPPUNIT_ASSERT( out
!= NULL
);
172 CPPUNIT_ASSERT( out
->Write("First hello", 11).GetLastError() == wxSTREAM_NO_ERROR
);
175 // send a command to check the remote file
176 CPPUNIT_ASSERT( m_ftp
->SendCommand(wxString(wxT("STAT ")) + file1
) == '2' );
177 CPPUNIT_ASSERT( m_ftp
->GetLastResult() == "11" );