1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 29 // MARKER(update_precomp.py): autogen include statement, do not remove 30 #include "precompiled_ucb.hxx" 31 #include <com/sun/star/ucb/OpenMode.hpp> 32 #include "ftpurl.hxx" 33 #include "ftploaderthread.hxx" 34 #include "ftphandleprovider.hxx" 35 #include "debughelper.hxx" 36 #include <rtl/memory.h> 37 #include <vector> 38 39 #define TESTEVAL \ 40 if(number_of_errors) \ 41 fprintf(stderr,"errors in %s: %d\n",name,number_of_errors); \ 42 return number_of_errors 43 44 45 namespace ccsu = com::sun::star::ucb; 46 47 48 struct ServerInfo { 49 rtl::OUString host; 50 rtl::OUString port; 51 rtl::OUString username; 52 rtl::OUString password; 53 rtl::OUString account; 54 }; 55 56 57 class FTPHandleProviderI 58 : public ftp::FTPHandleProvider { 59 60 public: 61 62 FTPHandleProviderI() 63 : p(new ftp::FTPLoaderThread) { 64 } 65 66 ~FTPHandleProviderI() { 67 delete p; 68 } 69 70 virtual CURL* handle() { 71 return p->handle(); 72 } 73 74 bool forHost(const rtl::OUString& host, 75 const rtl::OUString& port, 76 const rtl::OUString& username, 77 rtl::OUString& password, 78 rtl::OUString& account) 79 { 80 for(unsigned int i = 0; i < m_ServerInfo.size(); ++i) 81 if(host == m_ServerInfo[i].host && 82 port == m_ServerInfo[i].port && 83 username == m_ServerInfo[i].username ) { 84 password = m_ServerInfo[i].password; 85 account = m_ServerInfo[i].account; 86 return true; 87 } 88 89 return false; 90 } 91 92 virtual bool setHost(const rtl::OUString& host, 93 const rtl::OUString& port, 94 const rtl::OUString& username, 95 const rtl::OUString& password, 96 const rtl::OUString& account) 97 { 98 ServerInfo inf; 99 inf.host = host; 100 inf.port = port; 101 inf.username = username; 102 inf.password = password; 103 inf.account = account; 104 105 bool present(false); 106 for(unsigned int i = 0; i < m_ServerInfo.size(); ++i) 107 if(host == m_ServerInfo[i].host && 108 port == m_ServerInfo[i].port) { 109 m_ServerInfo[i] = inf; 110 present = true; 111 } 112 113 if(!present) 114 m_ServerInfo.push_back(inf); 115 116 return !present; 117 118 } 119 120 121 private: 122 123 std::vector<ServerInfo> m_ServerInfo; 124 ftp::FTPLoaderThread *p; 125 }; 126 127 128 129 // Here are some test for the parsing of an url. 130 131 #define TESTURL \ 132 { \ 133 ftp::FTPURL url(rtl::OUString::createFromAscii(ascii),&prov); \ 134 if(!url.username().equalsAscii(n)) {\ 135 ++number_of_errors; \ 136 err_msg("wrong username: ",url.username()); \ 137 }} 138 139 int test_ftpurl(void) { 140 const char* name = "test_ftpurl"; 141 int number_of_errors = 0; 142 143 FTPHandleProviderI prov; 144 char* ascii,*n,*p; 145 146 ascii = "ftp://abi:psswd@host/eins/../drei", n = "abi", p = "psswd"; 147 TESTURL; 148 149 ascii = "ftp://:psswd@host:22/eins/../drei", n = "anonymous", p = "psswd"; 150 TESTURL; 151 152 ascii = "ftp://host/bla/../../test/", n = "anonymous", p = ""; 153 TESTURL; 154 155 TESTEVAL; 156 } 157 158 159 int test_ftplist(void) { 160 int number_of_errors = 0; 161 const char* name = "test_ftplist"; 162 163 FTPHandleProviderI provider; 164 165 ftp::FTPURL url( 166 rtl::OUString::createFromAscii( 167 "ftp://abi:psswd@abi-1/dir"), 168 &provider); 169 170 std::vector<ftp::FTPDirentry> vec = 171 url.list(com::sun::star::ucb::OpenMode::ALL); 172 173 if(vec.size() != 3) 174 ++number_of_errors; 175 176 if(!(vec[0].m_aName.equalsAscii("dir1") && 177 vec[1].m_aName.equalsAscii("dir2") && 178 vec[2].m_aName.equalsAscii("file1"))) 179 ++number_of_errors; 180 181 TESTEVAL; 182 } 183 184 185 #define TESTPARENT \ 186 { \ 187 ftp::FTPURL url(rtl::OUString::createFromAscii(ascii),&prov); \ 188 urlStr = url.parent(); \ 189 if(!urlStr.equalsAscii(expect)) \ 190 ++number_of_errors; \ 191 } 192 193 194 int test_ftpparent(void) { 195 int number_of_errors = 0; 196 const char* name = "test_ftpparent"; 197 FTPHandleProviderI prov; 198 199 rtl::OUString urlStr; 200 char *ascii,*expect; 201 202 ascii = "ftp://abi:psswd@abi-1/file"; 203 expect = "ftp://abi:psswd@abi-1/"; 204 TESTPARENT; 205 206 ascii = "ftp://abi:psswd@abi-1/dir/../file"; 207 expect = "ftp://abi:psswd@abi-1/"; 208 TESTPARENT; 209 210 ascii = "ftp://abi:psswd@abi-1/.."; 211 expect = "ftp://abi:psswd@abi-1/../.."; 212 TESTPARENT; 213 214 ascii = "ftp://abi:psswd@abi-1/../../dir"; 215 expect = "ftp://abi:psswd@abi-1/../.."; 216 TESTPARENT; 217 218 ascii = "ftp://abi:psswd@abi-1/"; 219 expect = "ftp://abi:psswd@abi-1/.."; 220 TESTPARENT; 221 222 TESTEVAL; 223 } 224 225 226 int test_ftpproperties(void) { 227 int number_of_errors = 0; 228 const char* name = "test_ftpproperties"; 229 FTPHandleProviderI provider; 230 231 ftp::FTPURL url( 232 rtl::OUString::createFromAscii( 233 "ftp://abi:psswd@abi-1/file"), 234 &provider); 235 236 ftp::FTPDirentry ade(url.direntry()); 237 238 if(!(ade.m_aName.equalsAscii("file") && 239 ade.isFile())) 240 ++number_of_errors; 241 242 TESTEVAL; 243 } 244 245 246 int test_ftpopen(void) 247 { 248 int number_of_errors = 0; 249 const char* name = "test_ftpopen"; 250 251 FTPHandleProviderI provider; 252 ftp::FTPURL url( 253 rtl::OUString::createFromAscii( 254 "ftp://abi:psswd@abi-1/file"), 255 &provider); 256 257 FILE* file = url.open(); 258 if(file) { 259 int nbuf,ndest; 260 const int bffsz = 256; 261 char buff[bffsz]; 262 char *dest = (char*) malloc(sizeof(char)); 263 dest[0] = 0; 264 do { 265 rtl_zeroMemory((void*)buff,bffsz); 266 fread(buff,bffsz-1,1,file); 267 nbuf = strlen(buff); 268 ndest = strlen(dest); 269 dest = (char*)realloc(dest,ndest + nbuf + 1); 270 strncat(dest,buff,nbuf); 271 } while(nbuf == bffsz-1); 272 fclose(file); 273 274 const char* expected = "You are now looking at the filecontent.\n"; 275 if(strcmp(expected,dest)) 276 ++number_of_errors; 277 free(dest); 278 } else 279 ++number_of_errors; 280 281 TESTEVAL; 282 } 283 284 285