bump product version to 4.1.6.2
[LibreOffice.git] / ucb / source / ucp / ftp / test_ftpurl.cxx
blobbbc99cae9849e7580b11fc98fc564a4c44f85638
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
22 #include "ftpurl.hxx"
23 #include "ftploaderthread.hxx"
24 #include "ftphandleprovider.hxx"
25 #include "debughelper.hxx"
26 #include <vector>
28 struct ServerInfo {
29 OUString host;
30 OUString port;
31 OUString username;
32 OUString password;
33 OUString account;
37 class FTPHandleProviderI
38 : public ftp::FTPHandleProvider {
40 public:
42 FTPHandleProviderI()
43 : p(new ftp::FTPLoaderThread) {
46 ~FTPHandleProviderI() {
47 delete p;
50 virtual CURL* handle() {
51 return p->handle();
54 bool forHost(const OUString& host,
55 const OUString& port,
56 const OUString& username,
57 OUString& password,
58 OUString& account)
60 for(unsigned int i = 0; i < m_ServerInfo.size(); ++i)
61 if(host == m_ServerInfo[i].host &&
62 port == m_ServerInfo[i].port &&
63 username == m_ServerInfo[i].username ) {
64 password = m_ServerInfo[i].password;
65 account = m_ServerInfo[i].account;
66 return true;
69 return false;
72 virtual bool setHost(const OUString& host,
73 const OUString& port,
74 const OUString& username,
75 const OUString& password,
76 const OUString& account)
78 ServerInfo inf;
79 inf.host = host;
80 inf.port = port;
81 inf.username = username;
82 inf.password = password;
83 inf.account = account;
85 bool present(false);
86 for(unsigned int i = 0; i < m_ServerInfo.size(); ++i)
87 if(host == m_ServerInfo[i].host &&
88 port == m_ServerInfo[i].port) {
89 m_ServerInfo[i] = inf;
90 present = true;
93 if(!present)
94 m_ServerInfo.push_back(inf);
96 return !present;
101 private:
103 std::vector<ServerInfo> m_ServerInfo;
104 ftp::FTPLoaderThread *p;
109 // Here are some test for the parsing of an url.
111 #define TESTURL \
113 ftp::FTPURL url(OUString::createFromAscii(ascii),&prov); \
114 if(!url.username().equalsAscii(n)) {\
115 ++number_of_errors; \
116 err_msg("wrong username: ",url.username()); \
119 int test_ftpurl(void) {
120 const char* name = "test_ftpurl";
121 int number_of_errors = 0;
123 FTPHandleProviderI prov;
124 char* ascii,*n,*p;
126 ascii = "ftp://abi:psswd@host/eins/../drei", n = "abi", p = "psswd";
127 TESTURL;
129 ascii = "ftp://:psswd@host:22/eins/../drei", n = "anonymous", p = "psswd";
130 TESTURL;
132 ascii = "ftp://host/bla/../../test/", n = "anonymous", p = "";
133 TESTURL;
135 if(number_of_errors)
137 fprintf(stderr,"errors in %s: %d\n",name,number_of_errors);
139 return number_of_errors;
143 int test_ftplist(void) {
144 int number_of_errors = 0;
145 const char* name = "test_ftplist";
147 FTPHandleProviderI provider;
149 ftp::FTPURL url(
150 OUString( "ftp://abi:psswd@abi-1/dir"),
151 &provider);
153 std::vector<ftp::FTPDirentry> vec =
154 url.list(com::sun::star::ucb::OpenMode::ALL);
156 if(vec.size() != 3)
157 ++number_of_errors;
159 if(!(vec[0].m_aName == "dir1" && vec[1].m_aName == "dir2" && vec[2].m_aName == "file1" ))
160 ++number_of_errors;
162 if(number_of_errors)
164 fprintf(stderr,"errors in %s: %d\n",name,number_of_errors);
166 return number_of_errors;
170 #define TESTPARENT \
172 ftp::FTPURL url(OUString::createFromAscii(ascii),&prov); \
173 urlStr = url.parent(); \
174 if(!urlStr.equalsAscii(expect)) \
175 ++number_of_errors; \
179 int test_ftpparent(void) {
180 int number_of_errors = 0;
181 const char* name = "test_ftpparent";
182 FTPHandleProviderI prov;
184 OUString urlStr;
185 char *ascii,*expect;
187 ascii = "ftp://abi:psswd@abi-1/file";
188 expect = "ftp://abi:psswd@abi-1/";
189 TESTPARENT;
191 ascii = "ftp://abi:psswd@abi-1/dir/../file";
192 expect = "ftp://abi:psswd@abi-1/";
193 TESTPARENT;
195 ascii = "ftp://abi:psswd@abi-1/..";
196 expect = "ftp://abi:psswd@abi-1/../..";
197 TESTPARENT;
199 ascii = "ftp://abi:psswd@abi-1/../../dir";
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 if(number_of_errors)
209 fprintf(stderr,"errors in %s: %d\n",name,number_of_errors);
211 return number_of_errors;
215 int test_ftpproperties(void) {
216 int number_of_errors = 0;
217 FTPHandleProviderI provider;
219 ftp::FTPURL url(
220 OUString( "ftp://abi:psswd@abi-1/file"),
221 &provider);
223 ftp::FTPDirentry ade(url.direntry());
225 if(!(ade.m_aName == "file" && ade.isFile()))
226 ++number_of_errors;
228 if(number_of_errors)
230 const char* name = "test_ftpproperties";
231 fprintf(stderr,"errors in %s: %d\n",name,number_of_errors);
233 return number_of_errors;
237 int test_ftpopen(void)
239 int number_of_errors = 0;
241 FTPHandleProviderI provider;
242 ftp::FTPURL url(
243 OUString( "ftp://abi:psswd@abi-1/file"),
244 &provider);
246 FILE* file = url.open();
247 if(file) {
248 int nbuf,ndest;
249 const int bffsz = 256;
250 char buff[bffsz];
251 char *dest = (char*) malloc(sizeof(char));
252 dest[0] = 0;
253 do {
254 memset((void*)buff, 0, bffsz);
255 fread(buff,bffsz-1,1,file);
256 nbuf = strlen(buff);
257 ndest = strlen(dest);
258 dest = (char*)realloc(dest,ndest + nbuf + 1);
259 strncat(dest,buff,nbuf);
260 } while(nbuf == bffsz-1);
261 fclose(file);
263 const char* expected = "You are now looking at the filecontent.\n";
264 if(strcmp(expected,dest))
265 ++number_of_errors;
266 free(dest);
267 } else
268 ++number_of_errors;
270 if(number_of_errors)
272 const char* name = "test_ftpopen";
273 fprintf(stderr,"errors in %s: %d\n",name,number_of_errors);
275 return number_of_errors;
279 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */