1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <com/sun/star/ucb/OpenMode.hpp>
32 #include "ftploaderthread.hxx"
33 #include "ftphandleprovider.hxx"
34 #include "debughelper.hxx"
35 #include <rtl/memory.h>
39 if(number_of_errors) \
40 fprintf(stderr,"errors in %s: %d\n",name,number_of_errors); \
41 return number_of_errors
47 rtl::OUString username
;
48 rtl::OUString password
;
49 rtl::OUString account
;
53 class FTPHandleProviderI
54 : public ftp::FTPHandleProvider
{
59 : p(new ftp::FTPLoaderThread
) {
62 ~FTPHandleProviderI() {
66 virtual CURL
* handle() {
70 bool forHost(const rtl::OUString
& host
,
71 const rtl::OUString
& port
,
72 const rtl::OUString
& username
,
73 rtl::OUString
& password
,
74 rtl::OUString
& account
)
76 for(unsigned int i
= 0; i
< m_ServerInfo
.size(); ++i
)
77 if(host
== m_ServerInfo
[i
].host
&&
78 port
== m_ServerInfo
[i
].port
&&
79 username
== m_ServerInfo
[i
].username
) {
80 password
= m_ServerInfo
[i
].password
;
81 account
= m_ServerInfo
[i
].account
;
88 virtual bool setHost(const rtl::OUString
& host
,
89 const rtl::OUString
& port
,
90 const rtl::OUString
& username
,
91 const rtl::OUString
& password
,
92 const rtl::OUString
& account
)
97 inf
.username
= username
;
98 inf
.password
= password
;
99 inf
.account
= account
;
102 for(unsigned int i
= 0; i
< m_ServerInfo
.size(); ++i
)
103 if(host
== m_ServerInfo
[i
].host
&&
104 port
== m_ServerInfo
[i
].port
) {
105 m_ServerInfo
[i
] = inf
;
110 m_ServerInfo
.push_back(inf
);
119 std::vector
<ServerInfo
> m_ServerInfo
;
120 ftp::FTPLoaderThread
*p
;
125 // Here are some test for the parsing of an url.
129 ftp::FTPURL url(rtl::OUString::createFromAscii(ascii),&prov); \
130 if(!url.username().equalsAscii(n)) {\
131 ++number_of_errors; \
132 err_msg("wrong username: ",url.username()); \
135 int test_ftpurl(void) {
136 const char* name
= "test_ftpurl";
137 int number_of_errors
= 0;
139 FTPHandleProviderI prov
;
142 ascii
= "ftp://abi:psswd@host/eins/../drei", n
= "abi", p
= "psswd";
145 ascii
= "ftp://:psswd@host:22/eins/../drei", n
= "anonymous", p
= "psswd";
148 ascii
= "ftp://host/bla/../../test/", n
= "anonymous", p
= "";
155 int test_ftplist(void) {
156 int number_of_errors
= 0;
157 const char* name
= "test_ftplist";
159 FTPHandleProviderI provider
;
162 rtl::OUString( "ftp://abi:psswd@abi-1/dir"),
165 std::vector
<ftp::FTPDirentry
> vec
=
166 url
.list(com::sun::star::ucb::OpenMode::ALL
);
171 if(!(vec
[0].m_aName
== "dir1" && vec
[1].m_aName
== "dir2" && vec
[2].m_aName
== "file1" ))
180 ftp::FTPURL url(rtl::OUString::createFromAscii(ascii),&prov); \
181 urlStr = url.parent(); \
182 if(!urlStr.equalsAscii(expect)) \
183 ++number_of_errors; \
187 int test_ftpparent(void) {
188 int number_of_errors
= 0;
189 const char* name
= "test_ftpparent";
190 FTPHandleProviderI prov
;
192 rtl::OUString urlStr
;
195 ascii
= "ftp://abi:psswd@abi-1/file";
196 expect
= "ftp://abi:psswd@abi-1/";
199 ascii
= "ftp://abi:psswd@abi-1/dir/../file";
200 expect
= "ftp://abi:psswd@abi-1/";
203 ascii
= "ftp://abi:psswd@abi-1/..";
204 expect
= "ftp://abi:psswd@abi-1/../..";
207 ascii
= "ftp://abi:psswd@abi-1/../../dir";
208 expect
= "ftp://abi:psswd@abi-1/../..";
211 ascii
= "ftp://abi:psswd@abi-1/";
212 expect
= "ftp://abi:psswd@abi-1/..";
219 int test_ftpproperties(void) {
220 int number_of_errors
= 0;
221 const char* name
= "test_ftpproperties";
222 FTPHandleProviderI provider
;
225 rtl::OUString( "ftp://abi:psswd@abi-1/file"),
228 ftp::FTPDirentry
ade(url
.direntry());
230 if(!(ade
.m_aName
== "file" && ade
.isFile()))
237 int test_ftpopen(void)
239 int number_of_errors
= 0;
240 const char* name
= "test_ftpopen";
242 FTPHandleProviderI provider
;
244 rtl::OUString( "ftp://abi:psswd@abi-1/file"),
247 FILE* file
= url
.open();
250 const int bffsz
= 256;
252 char *dest
= (char*) malloc(sizeof(char));
255 rtl_zeroMemory((void*)buff
,bffsz
);
256 fread(buff
,bffsz
-1,1,file
);
258 ndest
= strlen(dest
);
259 dest
= (char*)realloc(dest
,ndest
+ nbuf
+ 1);
260 strncat(dest
,buff
,nbuf
);
261 } while(nbuf
== bffsz
-1);
264 const char* expected
= "You are now looking at the filecontent.\n";
265 if(strcmp(expected
,dest
))
275 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */