]>
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)
6 // Copyright: (c) 2010 wxWidgets team
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
23 #include <wx/protocol/ftp.h>
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!
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";
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 class FTPTestCase
: public CppUnit::TestCase
53 virtual void tearDown();
56 CPPUNIT_TEST_SUITE( FTPTestCase
);
58 CPPUNIT_TEST( Download
);
59 CPPUNIT_TEST( FileSize
);
62 CPPUNIT_TEST( Upload
);
64 CPPUNIT_TEST_SUITE_END();
74 DECLARE_NO_COPY_CLASS(FTPTestCase
)
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 );
81 // also include in its own registry so that these tests can be run alone
82 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FTPTestCase
, "FTPTestCase" );
85 void FTPTestCase::setUp()
87 wxSocketBase::Initialize();
89 // wxFTP cannot be a static variable as its ctor needs to access
90 // wxWidgets internals after it has been initialized
95 m_ftp
->SetPassword(password
);
96 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
99 void FTPTestCase::tearDown()
103 wxSocketBase::Shutdown();
106 void FTPTestCase::List()
108 CPPUNIT_ASSERT( m_ftp
->Connect(hostname
) );
111 CPPUNIT_ASSERT( m_ftp
->ChDir(directory
) );
113 // test NLIST and LIST
115 CPPUNIT_ASSERT( m_ftp
->GetFilesList(files
) );
116 CPPUNIT_ASSERT( m_ftp
->GetDirList(files
) );
118 CPPUNIT_ASSERT( m_ftp
->ChDir(wxT("..")) );
121 void FTPTestCase::Download()
123 CPPUNIT_ASSERT( m_ftp
->Connect(hostname
) );
124 CPPUNIT_ASSERT( m_ftp
->ChDir(directory
) );
127 wxInputStream
*in1
= m_ftp
->GetInputStream(invalid_filename
);
128 CPPUNIT_ASSERT( in1
== NULL
);
131 wxInputStream
*in2
= m_ftp
->GetInputStream(valid_filename
);
132 CPPUNIT_ASSERT( in2
!= NULL
);
134 size_t size
= in2
->GetSize();
135 wxChar
*data
= new wxChar
[size
];
136 CPPUNIT_ASSERT( in2
->Read(data
, size
).GetLastError() == wxSTREAM_NO_ERROR
);
142 void FTPTestCase::FileSize()
144 CPPUNIT_ASSERT( m_ftp
->Connect(hostname
) );
146 CPPUNIT_ASSERT( m_ftp
->ChDir(directory
) );
148 CPPUNIT_ASSERT( m_ftp
->FileExists(valid_filename
) );
150 int size
= m_ftp
->GetFileSize(valid_filename
);
151 CPPUNIT_ASSERT( size
!= -1 );
154 void FTPTestCase::Misc()
156 CPPUNIT_ASSERT( m_ftp
->Connect(hostname
) );
158 CPPUNIT_ASSERT( m_ftp
->SendCommand(wxT("STAT")) == '2' );
159 CPPUNIT_ASSERT( m_ftp
->SendCommand(wxT("HELP SITE")) == '2' );
162 #ifndef FTP_ANONYMOUS
163 void FTPTestCase::Upload()
165 CPPUNIT_ASSERT( m_ftp
->Connect(hostname
) );
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
);
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" );