Bump for 3.6-28
[LibreOffice.git] / ucb / source / ucp / ftp / test_ftpurl.cxx
blob3e2a5c22a2dd563e503bcf218f46f6935fd455cd
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>
31 #include "ftpurl.hxx"
32 #include "ftploaderthread.hxx"
33 #include "ftphandleprovider.hxx"
34 #include "debughelper.hxx"
35 #include <rtl/memory.h>
36 #include <vector>
38 #define TESTEVAL \
39 if(number_of_errors) \
40 fprintf(stderr,"errors in %s: %d\n",name,number_of_errors); \
41 return number_of_errors
44 struct ServerInfo {
45 rtl::OUString host;
46 rtl::OUString port;
47 rtl::OUString username;
48 rtl::OUString password;
49 rtl::OUString account;
53 class FTPHandleProviderI
54 : public ftp::FTPHandleProvider {
56 public:
58 FTPHandleProviderI()
59 : p(new ftp::FTPLoaderThread) {
62 ~FTPHandleProviderI() {
63 delete p;
66 virtual CURL* handle() {
67 return p->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;
82 return true;
85 return false;
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)
94 ServerInfo inf;
95 inf.host = host;
96 inf.port = port;
97 inf.username = username;
98 inf.password = password;
99 inf.account = account;
101 bool present(false);
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;
106 present = true;
109 if(!present)
110 m_ServerInfo.push_back(inf);
112 return !present;
117 private:
119 std::vector<ServerInfo> m_ServerInfo;
120 ftp::FTPLoaderThread *p;
125 // Here are some test for the parsing of an url.
127 #define TESTURL \
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;
140 char* ascii,*n,*p;
142 ascii = "ftp://abi:psswd@host/eins/../drei", n = "abi", p = "psswd";
143 TESTURL;
145 ascii = "ftp://:psswd@host:22/eins/../drei", n = "anonymous", p = "psswd";
146 TESTURL;
148 ascii = "ftp://host/bla/../../test/", n = "anonymous", p = "";
149 TESTURL;
151 TESTEVAL;
155 int test_ftplist(void) {
156 int number_of_errors = 0;
157 const char* name = "test_ftplist";
159 FTPHandleProviderI provider;
161 ftp::FTPURL url(
162 rtl::OUString( "ftp://abi:psswd@abi-1/dir"),
163 &provider);
165 std::vector<ftp::FTPDirentry> vec =
166 url.list(com::sun::star::ucb::OpenMode::ALL);
168 if(vec.size() != 3)
169 ++number_of_errors;
171 if(!(vec[0].m_aName == "dir1" && vec[1].m_aName == "dir2" && vec[2].m_aName == "file1" ))
172 ++number_of_errors;
174 TESTEVAL;
178 #define TESTPARENT \
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;
193 char *ascii,*expect;
195 ascii = "ftp://abi:psswd@abi-1/file";
196 expect = "ftp://abi:psswd@abi-1/";
197 TESTPARENT;
199 ascii = "ftp://abi:psswd@abi-1/dir/../file";
200 expect = "ftp://abi:psswd@abi-1/";
201 TESTPARENT;
203 ascii = "ftp://abi:psswd@abi-1/..";
204 expect = "ftp://abi:psswd@abi-1/../..";
205 TESTPARENT;
207 ascii = "ftp://abi:psswd@abi-1/../../dir";
208 expect = "ftp://abi:psswd@abi-1/../..";
209 TESTPARENT;
211 ascii = "ftp://abi:psswd@abi-1/";
212 expect = "ftp://abi:psswd@abi-1/..";
213 TESTPARENT;
215 TESTEVAL;
219 int test_ftpproperties(void) {
220 int number_of_errors = 0;
221 const char* name = "test_ftpproperties";
222 FTPHandleProviderI provider;
224 ftp::FTPURL url(
225 rtl::OUString( "ftp://abi:psswd@abi-1/file"),
226 &provider);
228 ftp::FTPDirentry ade(url.direntry());
230 if(!(ade.m_aName == "file" && ade.isFile()))
231 ++number_of_errors;
233 TESTEVAL;
237 int test_ftpopen(void)
239 int number_of_errors = 0;
240 const char* name = "test_ftpopen";
242 FTPHandleProviderI provider;
243 ftp::FTPURL url(
244 rtl::OUString( "ftp://abi:psswd@abi-1/file"),
245 &provider);
247 FILE* file = url.open();
248 if(file) {
249 int nbuf,ndest;
250 const int bffsz = 256;
251 char buff[bffsz];
252 char *dest = (char*) malloc(sizeof(char));
253 dest[0] = 0;
254 do {
255 rtl_zeroMemory((void*)buff,bffsz);
256 fread(buff,bffsz-1,1,file);
257 nbuf = strlen(buff);
258 ndest = strlen(dest);
259 dest = (char*)realloc(dest,ndest + nbuf + 1);
260 strncat(dest,buff,nbuf);
261 } while(nbuf == bffsz-1);
262 fclose(file);
264 const char* expected = "You are now looking at the filecontent.\n";
265 if(strcmp(expected,dest))
266 ++number_of_errors;
267 free(dest);
268 } else
269 ++number_of_errors;
271 TESTEVAL;
275 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */