]>
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 // register in the unnamed registry so that these tests are run by default
79 CPPUNIT_TEST_SUITE_REGISTRATION( FTPTestCase
);
81 // also include in it's 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" );