Update OpenVMS makefile
[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 // Copyright: (c) 2010 wxWidgets team
7 ///////////////////////////////////////////////////////////////////////////////
8
9 // ----------------------------------------------------------------------------
10 // headers
11 // ----------------------------------------------------------------------------
12
13 #include "testprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif // WX_PRECOMP
22
23 #include <wx/protocol/ftp.h>
24
25 #define FTP_ANONYMOUS
26
27 #ifdef FTP_ANONYMOUS
28 static const char *hostname = "ftp.wxwidgets.org";
29 static const char *directory = "/pub/2.8.11";
30 static const char *invalid_filename = "a_file_which_does_not_exist";
31 static const char *valid_filename = "MD5SUM";
32 // NOTE: choose a small file or otherwise the FTPTestCase::Download()
33 // function will take (a lot of) time to complete!
34 #else
35 static const char *hostname = "localhost";
36 static const char *user = "guest";
37 static const char *password = "";
38 static const char *directory = "/etc";
39 static const char *invalid_filename = "issue";
40 static const char *valid_filename = "hosts";
41 #endif
42
43 // ----------------------------------------------------------------------------
44 // test class
45 // ----------------------------------------------------------------------------
46
47 class FTPTestCase : public CppUnit::TestCase
48 {
49 public:
50 FTPTestCase() {}
51
52 virtual void setUp();
53 virtual void tearDown();
54
55 private:
56 CPPUNIT_TEST_SUITE( FTPTestCase );
57 CPPUNIT_TEST( List );
58 CPPUNIT_TEST( Download );
59 CPPUNIT_TEST( FileSize );
60 CPPUNIT_TEST( Misc );
61 #ifndef FTP_ANONYMOUS
62 CPPUNIT_TEST( Upload );
63 #endif
64 CPPUNIT_TEST_SUITE_END();
65
66 void List();
67 void Download();
68 void FileSize();
69 void Misc();
70 void Upload();
71
72 wxFTP *m_ftp;
73
74 DECLARE_NO_COPY_CLASS(FTPTestCase)
75 };
76
77 // NOTE: we do not run FTPTestCase suite by default because buildslaves typically
78 // do not have FTP connectivity enabled by default...
79 //CPPUNIT_TEST_SUITE_REGISTRATION( FTPTestCase );
80
81 // also include in its 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