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