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"
29 if(number_of_errors) \
30 fprintf(stderr,"errors in %s: %d\n",name,number_of_errors); \
31 return number_of_errors
37 rtl::OUString username
;
38 rtl::OUString password
;
39 rtl::OUString account
;
43 class FTPHandleProviderI
44 : public ftp::FTPHandleProvider
{
49 : p(new ftp::FTPLoaderThread
) {
52 ~FTPHandleProviderI() {
56 virtual CURL
* handle() {
60 bool forHost(const rtl::OUString
& host
,
61 const rtl::OUString
& port
,
62 const rtl::OUString
& username
,
63 rtl::OUString
& password
,
64 rtl::OUString
& account
)
66 for(unsigned int i
= 0; i
< m_ServerInfo
.size(); ++i
)
67 if(host
== m_ServerInfo
[i
].host
&&
68 port
== m_ServerInfo
[i
].port
&&
69 username
== m_ServerInfo
[i
].username
) {
70 password
= m_ServerInfo
[i
].password
;
71 account
= m_ServerInfo
[i
].account
;
78 virtual bool setHost(const rtl::OUString
& host
,
79 const rtl::OUString
& port
,
80 const rtl::OUString
& username
,
81 const rtl::OUString
& password
,
82 const rtl::OUString
& account
)
87 inf
.username
= username
;
88 inf
.password
= password
;
89 inf
.account
= account
;
92 for(unsigned int i
= 0; i
< m_ServerInfo
.size(); ++i
)
93 if(host
== m_ServerInfo
[i
].host
&&
94 port
== m_ServerInfo
[i
].port
) {
95 m_ServerInfo
[i
] = inf
;
100 m_ServerInfo
.push_back(inf
);
109 std::vector
<ServerInfo
> m_ServerInfo
;
110 ftp::FTPLoaderThread
*p
;
115 // Here are some test for the parsing of an url.
119 ftp::FTPURL url(rtl::OUString::createFromAscii(ascii),&prov); \
120 if(!url.username().equalsAscii(n)) {\
121 ++number_of_errors; \
122 err_msg("wrong username: ",url.username()); \
125 int test_ftpurl(void) {
126 const char* name
= "test_ftpurl";
127 int number_of_errors
= 0;
129 FTPHandleProviderI prov
;
132 ascii
= "ftp://abi:psswd@host/eins/../drei", n
= "abi", p
= "psswd";
135 ascii
= "ftp://:psswd@host:22/eins/../drei", n
= "anonymous", p
= "psswd";
138 ascii
= "ftp://host/bla/../../test/", n
= "anonymous", p
= "";
145 int test_ftplist(void) {
146 int number_of_errors
= 0;
147 const char* name
= "test_ftplist";
149 FTPHandleProviderI provider
;
152 rtl::OUString( "ftp://abi:psswd@abi-1/dir"),
155 std::vector
<ftp::FTPDirentry
> vec
=
156 url
.list(com::sun::star::ucb::OpenMode::ALL
);
161 if(!(vec
[0].m_aName
== "dir1" && vec
[1].m_aName
== "dir2" && vec
[2].m_aName
== "file1" ))
170 ftp::FTPURL url(rtl::OUString::createFromAscii(ascii),&prov); \
171 urlStr = url.parent(); \
172 if(!urlStr.equalsAscii(expect)) \
173 ++number_of_errors; \
177 int test_ftpparent(void) {
178 int number_of_errors
= 0;
179 const char* name
= "test_ftpparent";
180 FTPHandleProviderI prov
;
182 rtl::OUString urlStr
;
185 ascii
= "ftp://abi:psswd@abi-1/file";
186 expect
= "ftp://abi:psswd@abi-1/";
189 ascii
= "ftp://abi:psswd@abi-1/dir/../file";
190 expect
= "ftp://abi:psswd@abi-1/";
193 ascii
= "ftp://abi:psswd@abi-1/..";
194 expect
= "ftp://abi:psswd@abi-1/../..";
197 ascii
= "ftp://abi:psswd@abi-1/../../dir";
198 expect
= "ftp://abi:psswd@abi-1/../..";
201 ascii
= "ftp://abi:psswd@abi-1/";
202 expect
= "ftp://abi:psswd@abi-1/..";
209 int test_ftpproperties(void) {
210 int number_of_errors
= 0;
211 const char* name
= "test_ftpproperties";
212 FTPHandleProviderI provider
;
215 rtl::OUString( "ftp://abi:psswd@abi-1/file"),
218 ftp::FTPDirentry
ade(url
.direntry());
220 if(!(ade
.m_aName
== "file" && ade
.isFile()))
227 int test_ftpopen(void)
229 int number_of_errors
= 0;
230 const char* name
= "test_ftpopen";
232 FTPHandleProviderI provider
;
234 rtl::OUString( "ftp://abi:psswd@abi-1/file"),
237 FILE* file
= url
.open();
240 const int bffsz
= 256;
242 char *dest
= (char*) malloc(sizeof(char));
245 memset((void*)buff
, 0, bffsz
);
246 fread(buff
,bffsz
-1,1,file
);
248 ndest
= strlen(dest
);
249 dest
= (char*)realloc(dest
,ndest
+ nbuf
+ 1);
250 strncat(dest
,buff
,nbuf
);
251 } while(nbuf
== bffsz
-1);
254 const char* expected
= "You are now looking at the filecontent.\n";
255 if(strcmp(expected
,dest
))
265 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */