1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <com/sun/star/ucb/OpenMode.hpp>
23 #include "ftploaderthread.hxx"
24 #include "ftphandleprovider.hxx"
25 #include "debughelper.hxx"
37 class FTPHandleProviderI
38 : public ftp::FTPHandleProvider
{
43 : p(new ftp::FTPLoaderThread
) {
46 ~FTPHandleProviderI() {
50 virtual CURL
* handle() {
54 bool forHost(const OUString
& host
,
56 const OUString
& username
,
60 for(unsigned int i
= 0; i
< m_ServerInfo
.size(); ++i
)
61 if(host
== m_ServerInfo
[i
].host
&&
62 port
== m_ServerInfo
[i
].port
&&
63 username
== m_ServerInfo
[i
].username
) {
64 password
= m_ServerInfo
[i
].password
;
65 account
= m_ServerInfo
[i
].account
;
72 virtual bool setHost(const OUString
& host
,
74 const OUString
& username
,
75 const OUString
& password
,
76 const OUString
& account
)
81 inf
.username
= username
;
82 inf
.password
= password
;
83 inf
.account
= account
;
86 for(unsigned int i
= 0; i
< m_ServerInfo
.size(); ++i
)
87 if(host
== m_ServerInfo
[i
].host
&&
88 port
== m_ServerInfo
[i
].port
) {
89 m_ServerInfo
[i
] = inf
;
94 m_ServerInfo
.push_back(inf
);
103 std::vector
<ServerInfo
> m_ServerInfo
;
104 ftp::FTPLoaderThread
*p
;
109 // Here are some test for the parsing of an url.
113 ftp::FTPURL url(OUString::createFromAscii(ascii),&prov); \
114 if(!url.username().equalsAscii(n)) {\
115 ++number_of_errors; \
116 err_msg("wrong username: ",url.username()); \
119 int test_ftpurl(void) {
120 const char* name
= "test_ftpurl";
121 int number_of_errors
= 0;
123 FTPHandleProviderI prov
;
126 ascii
= "ftp://abi:psswd@host/eins/../drei", n
= "abi", p
= "psswd";
129 ascii
= "ftp://:psswd@host:22/eins/../drei", n
= "anonymous", p
= "psswd";
132 ascii
= "ftp://host/bla/../../test/", n
= "anonymous", p
= "";
137 fprintf(stderr
,"errors in %s: %d\n",name
,number_of_errors
);
139 return number_of_errors
;
143 int test_ftplist(void) {
144 int number_of_errors
= 0;
145 const char* name
= "test_ftplist";
147 FTPHandleProviderI provider
;
150 OUString( "ftp://abi:psswd@abi-1/dir"),
153 std::vector
<ftp::FTPDirentry
> vec
=
154 url
.list(com::sun::star::ucb::OpenMode::ALL
);
159 if(!(vec
[0].m_aName
== "dir1" && vec
[1].m_aName
== "dir2" && vec
[2].m_aName
== "file1" ))
164 fprintf(stderr
,"errors in %s: %d\n",name
,number_of_errors
);
166 return number_of_errors
;
172 ftp::FTPURL url(OUString::createFromAscii(ascii),&prov); \
173 urlStr = url.parent(); \
174 if(!urlStr.equalsAscii(expect)) \
175 ++number_of_errors; \
179 int test_ftpparent(void) {
180 int number_of_errors
= 0;
181 const char* name
= "test_ftpparent";
182 FTPHandleProviderI prov
;
187 ascii
= "ftp://abi:psswd@abi-1/file";
188 expect
= "ftp://abi:psswd@abi-1/";
191 ascii
= "ftp://abi:psswd@abi-1/dir/../file";
192 expect
= "ftp://abi:psswd@abi-1/";
195 ascii
= "ftp://abi:psswd@abi-1/..";
196 expect
= "ftp://abi:psswd@abi-1/../..";
199 ascii
= "ftp://abi:psswd@abi-1/../../dir";
200 expect
= "ftp://abi:psswd@abi-1/../..";
203 ascii
= "ftp://abi:psswd@abi-1/";
204 expect
= "ftp://abi:psswd@abi-1/..";
209 fprintf(stderr
,"errors in %s: %d\n",name
,number_of_errors
);
211 return number_of_errors
;
215 int test_ftpproperties(void) {
216 int number_of_errors
= 0;
217 FTPHandleProviderI provider
;
220 OUString( "ftp://abi:psswd@abi-1/file"),
223 ftp::FTPDirentry
ade(url
.direntry());
225 if(!(ade
.m_aName
== "file" && ade
.isFile()))
230 const char* name
= "test_ftpproperties";
231 fprintf(stderr
,"errors in %s: %d\n",name
,number_of_errors
);
233 return number_of_errors
;
237 int test_ftpopen(void)
239 int number_of_errors
= 0;
241 FTPHandleProviderI provider
;
243 OUString( "ftp://abi:psswd@abi-1/file"),
246 FILE* file
= url
.open();
249 const int bffsz
= 256;
251 char *dest
= (char*) malloc(sizeof(char));
254 memset((void*)buff
, 0, bffsz
);
255 fread(buff
,bffsz
-1,1,file
);
257 ndest
= strlen(dest
);
258 dest
= (char*)realloc(dest
,ndest
+ nbuf
+ 1);
259 strncat(dest
,buff
,nbuf
);
260 } while(nbuf
== bffsz
-1);
263 const char* expected
= "You are now looking at the filecontent.\n";
264 if(strcmp(expected
,dest
))
272 const char* name
= "test_ftpopen";
273 fprintf(stderr
,"errors in %s: %d\n",name
,number_of_errors
);
275 return number_of_errors
;
279 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */