move all non-interactive FTP tests from the console sample to a new CppUnit FTPTestCa...
[wxWidgets.git] / tests / uris / ftp.cpp
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 // RCS-ID: $Id$
7 // Copyright: (c) 2010 wxWidgets team
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #include "wx/wx.h"
22 #endif // WX_PRECOMP
23
24 #include <wx/protocol/ftp.h>
25
26 #define FTP_ANONYMOUS
27
28 #ifdef FTP_ANONYMOUS
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!
35 #else
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";
42 #endif
43
44 // ----------------------------------------------------------------------------
45 // test class
46 // ----------------------------------------------------------------------------
47
48 class FTPTestCase : public CppUnit::TestCase
49 {
50 public:
51 FTPTestCase() {}
52
53 virtual void setUp();
54 virtual void tearDown();
55
56 private:
57 CPPUNIT_TEST_SUITE( FTPTestCase );
58 CPPUNIT_TEST( List );
59 CPPUNIT_TEST( Download );
60 CPPUNIT_TEST( FileSize );
61 CPPUNIT_TEST( Misc );
62 #ifndef FTP_ANONYMOUS
63 CPPUNIT_TEST( Upload );
64 #endif
65 CPPUNIT_TEST_SUITE_END();
66
67 void List();
68 void Download();
69 void FileSize();
70 void Misc();
71 void Upload();
72
73 wxFTP *m_ftp;
74
75 DECLARE_NO_COPY_CLASS(FTPTestCase)
76 };
77
78 // register in the unnamed registry so that these tests are run by default
79 CPPUNIT_TEST_SUITE_REGISTRATION( FTPTestCase );
80
81 // also include in it's own registry so that these tests can be run alone
82 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FTPTestCase, "FTPTestCase" );
83
84
85 void FTPTestCase::setUp()
86 {
87 wxSocketBase::Initialize();
88
89 // wxFTP cannot be a static variable as its ctor needs to access
90 // wxWidgets internals after it has been initialized
91 m_ftp = new wxFTP;
92
93 #ifndef FTP_ANONYMOUS
94 m_ftp->SetUser(user);
95 m_ftp->SetPassword(password);
96 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
97 }
98
99 void FTPTestCase::tearDown()
100 {
101 delete m_ftp;
102
103 wxSocketBase::Shutdown();
104 }
105
106 void FTPTestCase::List()
107 {
108 CPPUNIT_ASSERT( m_ftp->Connect(hostname) );
109
110 // test CWD
111 CPPUNIT_ASSERT( m_ftp->ChDir(directory) );
112
113 // test NLIST and LIST
114 wxArrayString files;
115 CPPUNIT_ASSERT( m_ftp->GetFilesList(files) );
116 CPPUNIT_ASSERT( m_ftp->GetDirList(files) );
117
118 CPPUNIT_ASSERT( m_ftp->ChDir(wxT("..")) );
119 }
120
121 void FTPTestCase::Download()
122 {
123 CPPUNIT_ASSERT( m_ftp->Connect(hostname) );
124 CPPUNIT_ASSERT( m_ftp->ChDir(directory) );
125
126 // test RETR
127 wxInputStream *in1 = m_ftp->GetInputStream(invalid_filename);
128 CPPUNIT_ASSERT( in1 == NULL );
129 delete in1;
130
131 wxInputStream *in2 = m_ftp->GetInputStream(valid_filename);
132 CPPUNIT_ASSERT( in2 != NULL );
133
134 size_t size = in2->GetSize();
135 wxChar *data = new wxChar[size];
136 CPPUNIT_ASSERT( in2->Read(data, size).GetLastError() == wxSTREAM_NO_ERROR );
137
138 delete [] data;
139 delete in2;
140 }
141
142 void FTPTestCase::FileSize()
143 {
144 CPPUNIT_ASSERT( m_ftp->Connect(hostname) );
145
146 CPPUNIT_ASSERT( m_ftp->ChDir(directory) );
147
148 CPPUNIT_ASSERT( m_ftp->FileExists(valid_filename) );
149
150 int size = m_ftp->GetFileSize(valid_filename);
151 CPPUNIT_ASSERT( size != -1 );
152 }
153
154 void FTPTestCase::Misc()
155 {
156 CPPUNIT_ASSERT( m_ftp->Connect(hostname) );
157
158 CPPUNIT_ASSERT( m_ftp->SendCommand(wxT("STAT")) == '2' );
159 CPPUNIT_ASSERT( m_ftp->SendCommand(wxT("HELP SITE")) == '2' );
160 }
161
162 #ifndef FTP_ANONYMOUS
163 void FTPTestCase::Upload()
164 {
165 CPPUNIT_ASSERT( m_ftp->Connect(hostname) );
166
167 // upload a file
168 static const wxChar *file1 = wxT("test1");
169 wxOutputStream *out = m_ftp->GetOutputStream(file1);
170 CPPUNIT_ASSERT( out != NULL );
171 CPPUNIT_ASSERT( out->Write("First hello", 11).GetLastError() == wxSTREAM_NO_ERROR );
172 delete out;
173
174 // send a command to check the remote file
175 CPPUNIT_ASSERT( m_ftp->SendCommand(wxString(wxT("STAT ")) + file1) == '2' );
176 CPPUNIT_ASSERT( m_ftp->GetLastResult() == "11" );
177 }
178 #endif
179
180