1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: test_ftpurl.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_ucb.hxx"
34 #include <com/sun/star/ucb/OpenMode.hpp>
36 #include "ftploaderthread.hxx"
37 #include "ftphandleprovider.hxx"
38 #include "debughelper.hxx"
39 #include <rtl/memory.h>
43 if(number_of_errors) \
44 fprintf(stderr,"errors in %s: %d\n",name,number_of_errors); \
45 return number_of_errors
48 namespace ccsu
= com::sun::star::ucb
;
54 rtl::OUString username
;
55 rtl::OUString password
;
56 rtl::OUString account
;
60 class FTPHandleProviderI
61 : public ftp::FTPHandleProvider
{
66 : p(new ftp::FTPLoaderThread
) {
69 ~FTPHandleProviderI() {
73 virtual CURL
* handle() {
77 bool forHost(const rtl::OUString
& host
,
78 const rtl::OUString
& port
,
79 const rtl::OUString
& username
,
80 rtl::OUString
& password
,
81 rtl::OUString
& account
)
83 for(unsigned int i
= 0; i
< m_ServerInfo
.size(); ++i
)
84 if(host
== m_ServerInfo
[i
].host
&&
85 port
== m_ServerInfo
[i
].port
&&
86 username
== m_ServerInfo
[i
].username
) {
87 password
= m_ServerInfo
[i
].password
;
88 account
= m_ServerInfo
[i
].account
;
95 virtual bool setHost(const rtl::OUString
& host
,
96 const rtl::OUString
& port
,
97 const rtl::OUString
& username
,
98 const rtl::OUString
& password
,
99 const rtl::OUString
& account
)
104 inf
.username
= username
;
105 inf
.password
= password
;
106 inf
.account
= account
;
109 for(unsigned int i
= 0; i
< m_ServerInfo
.size(); ++i
)
110 if(host
== m_ServerInfo
[i
].host
&&
111 port
== m_ServerInfo
[i
].port
) {
112 m_ServerInfo
[i
] = inf
;
117 m_ServerInfo
.push_back(inf
);
126 std::vector
<ServerInfo
> m_ServerInfo
;
127 ftp::FTPLoaderThread
*p
;
132 // Here are some test for the parsing of an url.
136 ftp::FTPURL url(rtl::OUString::createFromAscii(ascii),&prov); \
137 if(!url.username().equalsAscii(n)) {\
138 ++number_of_errors; \
139 err_msg("wrong username: ",url.username()); \
142 int test_ftpurl(void) {
143 const char* name
= "test_ftpurl";
144 int number_of_errors
= 0;
146 FTPHandleProviderI prov
;
149 ascii
= "ftp://abi:psswd@host/eins/../drei", n
= "abi", p
= "psswd";
152 ascii
= "ftp://:psswd@host:22/eins/../drei", n
= "anonymous", p
= "psswd";
155 ascii
= "ftp://host/bla/../../test/", n
= "anonymous", p
= "";
162 int test_ftplist(void) {
163 int number_of_errors
= 0;
164 const char* name
= "test_ftplist";
166 FTPHandleProviderI provider
;
169 rtl::OUString::createFromAscii(
170 "ftp://abi:psswd@abi-1/dir"),
173 std::vector
<ftp::FTPDirentry
> vec
=
174 url
.list(com::sun::star::ucb::OpenMode::ALL
);
179 if(!(vec
[0].m_aName
.equalsAscii("dir1") &&
180 vec
[1].m_aName
.equalsAscii("dir2") &&
181 vec
[2].m_aName
.equalsAscii("file1")))
190 ftp::FTPURL url(rtl::OUString::createFromAscii(ascii),&prov); \
191 urlStr = url.parent(); \
192 if(!urlStr.equalsAscii(expect)) \
193 ++number_of_errors; \
197 int test_ftpparent(void) {
198 int number_of_errors
= 0;
199 const char* name
= "test_ftpparent";
200 FTPHandleProviderI prov
;
202 rtl::OUString urlStr
;
205 ascii
= "ftp://abi:psswd@abi-1/file";
206 expect
= "ftp://abi:psswd@abi-1/";
209 ascii
= "ftp://abi:psswd@abi-1/dir/../file";
210 expect
= "ftp://abi:psswd@abi-1/";
213 ascii
= "ftp://abi:psswd@abi-1/..";
214 expect
= "ftp://abi:psswd@abi-1/../..";
217 ascii
= "ftp://abi:psswd@abi-1/../../dir";
218 expect
= "ftp://abi:psswd@abi-1/../..";
221 ascii
= "ftp://abi:psswd@abi-1/";
222 expect
= "ftp://abi:psswd@abi-1/..";
229 int test_ftpproperties(void) {
230 int number_of_errors
= 0;
231 const char* name
= "test_ftpproperties";
232 FTPHandleProviderI provider
;
235 rtl::OUString::createFromAscii(
236 "ftp://abi:psswd@abi-1/file"),
239 ftp::FTPDirentry
ade(url
.direntry());
241 if(!(ade
.m_aName
.equalsAscii("file") &&
249 int test_ftpopen(void)
251 int number_of_errors
= 0;
252 const char* name
= "test_ftpopen";
254 FTPHandleProviderI provider
;
256 rtl::OUString::createFromAscii(
257 "ftp://abi:psswd@abi-1/file"),
260 FILE* file
= url
.open();
263 const int bffsz
= 256;
265 char *dest
= (char*) malloc(sizeof(char));
268 rtl_zeroMemory((void*)buff
,bffsz
);
269 fread(buff
,bffsz
-1,1,file
);
271 ndest
= strlen(dest
);
272 dest
= (char*)realloc(dest
,ndest
+ nbuf
+ 1);
273 strncat(dest
,buff
,nbuf
);
274 } while(nbuf
== bffsz
-1);
277 const char* expected
= "You are now looking at the filecontent.\n";
278 if(strcmp(expected
,dest
))