merge the formfield patch from ooo-build
[ooovba.git] / ucb / source / ucp / ftp / test_ftpurl.cxx
blob9d7167e078e49c8283656ec6964a0e548e86cf45
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: test_ftpurl.cxx,v $
10 * $Revision: 1.4 $
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>
35 #include "ftpurl.hxx"
36 #include "ftploaderthread.hxx"
37 #include "ftphandleprovider.hxx"
38 #include "debughelper.hxx"
39 #include <rtl/memory.h>
40 #include <vector>
42 #define TESTEVAL \
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;
51 struct ServerInfo {
52 rtl::OUString host;
53 rtl::OUString port;
54 rtl::OUString username;
55 rtl::OUString password;
56 rtl::OUString account;
60 class FTPHandleProviderI
61 : public ftp::FTPHandleProvider {
63 public:
65 FTPHandleProviderI()
66 : p(new ftp::FTPLoaderThread) {
69 ~FTPHandleProviderI() {
70 delete p;
73 virtual CURL* handle() {
74 return p->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;
89 return true;
92 return false;
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)
101 ServerInfo inf;
102 inf.host = host;
103 inf.port = port;
104 inf.username = username;
105 inf.password = password;
106 inf.account = account;
108 bool present(false);
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;
113 present = true;
116 if(!present)
117 m_ServerInfo.push_back(inf);
119 return !present;
124 private:
126 std::vector<ServerInfo> m_ServerInfo;
127 ftp::FTPLoaderThread *p;
132 // Here are some test for the parsing of an url.
134 #define TESTURL \
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;
147 char* ascii,*n,*p;
149 ascii = "ftp://abi:psswd@host/eins/../drei", n = "abi", p = "psswd";
150 TESTURL;
152 ascii = "ftp://:psswd@host:22/eins/../drei", n = "anonymous", p = "psswd";
153 TESTURL;
155 ascii = "ftp://host/bla/../../test/", n = "anonymous", p = "";
156 TESTURL;
158 TESTEVAL;
162 int test_ftplist(void) {
163 int number_of_errors = 0;
164 const char* name = "test_ftplist";
166 FTPHandleProviderI provider;
168 ftp::FTPURL url(
169 rtl::OUString::createFromAscii(
170 "ftp://abi:psswd@abi-1/dir"),
171 &provider);
173 std::vector<ftp::FTPDirentry> vec =
174 url.list(com::sun::star::ucb::OpenMode::ALL);
176 if(vec.size() != 3)
177 ++number_of_errors;
179 if(!(vec[0].m_aName.equalsAscii("dir1") &&
180 vec[1].m_aName.equalsAscii("dir2") &&
181 vec[2].m_aName.equalsAscii("file1")))
182 ++number_of_errors;
184 TESTEVAL;
188 #define TESTPARENT \
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;
203 char *ascii,*expect;
205 ascii = "ftp://abi:psswd@abi-1/file";
206 expect = "ftp://abi:psswd@abi-1/";
207 TESTPARENT;
209 ascii = "ftp://abi:psswd@abi-1/dir/../file";
210 expect = "ftp://abi:psswd@abi-1/";
211 TESTPARENT;
213 ascii = "ftp://abi:psswd@abi-1/..";
214 expect = "ftp://abi:psswd@abi-1/../..";
215 TESTPARENT;
217 ascii = "ftp://abi:psswd@abi-1/../../dir";
218 expect = "ftp://abi:psswd@abi-1/../..";
219 TESTPARENT;
221 ascii = "ftp://abi:psswd@abi-1/";
222 expect = "ftp://abi:psswd@abi-1/..";
223 TESTPARENT;
225 TESTEVAL;
229 int test_ftpproperties(void) {
230 int number_of_errors = 0;
231 const char* name = "test_ftpproperties";
232 FTPHandleProviderI provider;
234 ftp::FTPURL url(
235 rtl::OUString::createFromAscii(
236 "ftp://abi:psswd@abi-1/file"),
237 &provider);
239 ftp::FTPDirentry ade(url.direntry());
241 if(!(ade.m_aName.equalsAscii("file") &&
242 ade.isFile()))
243 ++number_of_errors;
245 TESTEVAL;
249 int test_ftpopen(void)
251 int number_of_errors = 0;
252 const char* name = "test_ftpopen";
254 FTPHandleProviderI provider;
255 ftp::FTPURL url(
256 rtl::OUString::createFromAscii(
257 "ftp://abi:psswd@abi-1/file"),
258 &provider);
260 FILE* file = url.open();
261 if(file) {
262 int nbuf,ndest;
263 const int bffsz = 256;
264 char buff[bffsz];
265 char *dest = (char*) malloc(sizeof(char));
266 dest[0] = 0;
267 do {
268 rtl_zeroMemory((void*)buff,bffsz);
269 fread(buff,bffsz-1,1,file);
270 nbuf = strlen(buff);
271 ndest = strlen(dest);
272 dest = (char*)realloc(dest,ndest + nbuf + 1);
273 strncat(dest,buff,nbuf);
274 } while(nbuf == bffsz-1);
275 fclose(file);
277 const char* expected = "You are now looking at the filecontent.\n";
278 if(strcmp(expected,dest))
279 ++number_of_errors;
280 free(dest);
281 } else
282 ++number_of_errors;
284 TESTEVAL;