]> git.saurik.com Git - wxWidgets.git/blame - tests/uris/ftp.cpp
Use wxCOMPtr throughout wxWebViewIE to simplify the code and reduce the chance of...
[wxWidgets.git] / tests / uris / ftp.cpp
CommitLineData
ec0e0939
FM
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
48class FTPTestCase : public CppUnit::TestCase
49{
50public:
51 FTPTestCase() {}
52
53 virtual void setUp();
54 virtual void tearDown();
55
56private:
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
20ba398d
FM
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 );
ec0e0939 81
e3778b4d 82// also include in its own registry so that these tests can be run alone
ec0e0939
FM
83CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FTPTestCase, "FTPTestCase" );
84
85
86void FTPTestCase::setUp()
87{
88 wxSocketBase::Initialize();
89
90 // wxFTP cannot be a static variable as its ctor needs to access
91 // wxWidgets internals after it has been initialized
92 m_ftp = new wxFTP;
93
94#ifndef FTP_ANONYMOUS
95 m_ftp->SetUser(user);
96 m_ftp->SetPassword(password);
97#endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
98}
99
100void FTPTestCase::tearDown()
101{
102 delete m_ftp;
103
104 wxSocketBase::Shutdown();
105}
106
107void FTPTestCase::List()
108{
109 CPPUNIT_ASSERT( m_ftp->Connect(hostname) );
110
111 // test CWD
112 CPPUNIT_ASSERT( m_ftp->ChDir(directory) );
113
114 // test NLIST and LIST
115 wxArrayString files;
116 CPPUNIT_ASSERT( m_ftp->GetFilesList(files) );
117 CPPUNIT_ASSERT( m_ftp->GetDirList(files) );
118
119 CPPUNIT_ASSERT( m_ftp->ChDir(wxT("..")) );
120}
121
122void FTPTestCase::Download()
123{
124 CPPUNIT_ASSERT( m_ftp->Connect(hostname) );
125 CPPUNIT_ASSERT( m_ftp->ChDir(directory) );
126
127 // test RETR
128 wxInputStream *in1 = m_ftp->GetInputStream(invalid_filename);
129 CPPUNIT_ASSERT( in1 == NULL );
130 delete in1;
131
132 wxInputStream *in2 = m_ftp->GetInputStream(valid_filename);
133 CPPUNIT_ASSERT( in2 != NULL );
134
135 size_t size = in2->GetSize();
136 wxChar *data = new wxChar[size];
137 CPPUNIT_ASSERT( in2->Read(data, size).GetLastError() == wxSTREAM_NO_ERROR );
138
139 delete [] data;
140 delete in2;
141}
142
143void FTPTestCase::FileSize()
144{
145 CPPUNIT_ASSERT( m_ftp->Connect(hostname) );
146
147 CPPUNIT_ASSERT( m_ftp->ChDir(directory) );
148
149 CPPUNIT_ASSERT( m_ftp->FileExists(valid_filename) );
150
151 int size = m_ftp->GetFileSize(valid_filename);
152 CPPUNIT_ASSERT( size != -1 );
153}
154
155void FTPTestCase::Misc()
156{
157 CPPUNIT_ASSERT( m_ftp->Connect(hostname) );
158
159 CPPUNIT_ASSERT( m_ftp->SendCommand(wxT("STAT")) == '2' );
160 CPPUNIT_ASSERT( m_ftp->SendCommand(wxT("HELP SITE")) == '2' );
161}
162
163#ifndef FTP_ANONYMOUS
164void FTPTestCase::Upload()
165{
166 CPPUNIT_ASSERT( m_ftp->Connect(hostname) );
167
168 // upload a file
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 );
173 delete out;
174
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" );
178}
179#endif
180
181