Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / ucb / source / ucp / ftp / test_ftpurl.cxx
blobbb9fa020e86503bba486369fce13e42390829b2b
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 #define TESTEVAL \
29 if(number_of_errors) \
30 fprintf(stderr,"errors in %s: %d\n",name,number_of_errors); \
31 return number_of_errors
34 struct ServerInfo {
35 rtl::OUString host;
36 rtl::OUString port;
37 rtl::OUString username;
38 rtl::OUString password;
39 rtl::OUString account;
43 class FTPHandleProviderI
44 : public ftp::FTPHandleProvider {
46 public:
48 FTPHandleProviderI()
49 : p(new ftp::FTPLoaderThread) {
52 ~FTPHandleProviderI() {
53 delete p;
56 virtual CURL* handle() {
57 return p->handle();
60 bool forHost(const rtl::OUString& host,
61 const rtl::OUString& port,
62 const rtl::OUString& username,
63 rtl::OUString& password,
64 rtl::OUString& account)
66 for(unsigned int i = 0; i < m_ServerInfo.size(); ++i)
67 if(host == m_ServerInfo[i].host &&
68 port == m_ServerInfo[i].port &&
69 username == m_ServerInfo[i].username ) {
70 password = m_ServerInfo[i].password;
71 account = m_ServerInfo[i].account;
72 return true;
75 return false;
78 virtual bool setHost(const rtl::OUString& host,
79 const rtl::OUString& port,
80 const rtl::OUString& username,
81 const rtl::OUString& password,
82 const rtl::OUString& account)
84 ServerInfo inf;
85 inf.host = host;
86 inf.port = port;
87 inf.username = username;
88 inf.password = password;
89 inf.account = account;
91 bool present(false);
92 for(unsigned int i = 0; i < m_ServerInfo.size(); ++i)
93 if(host == m_ServerInfo[i].host &&
94 port == m_ServerInfo[i].port) {
95 m_ServerInfo[i] = inf;
96 present = true;
99 if(!present)
100 m_ServerInfo.push_back(inf);
102 return !present;
107 private:
109 std::vector<ServerInfo> m_ServerInfo;
110 ftp::FTPLoaderThread *p;
115 // Here are some test for the parsing of an url.
117 #define TESTURL \
119 ftp::FTPURL url(rtl::OUString::createFromAscii(ascii),&prov); \
120 if(!url.username().equalsAscii(n)) {\
121 ++number_of_errors; \
122 err_msg("wrong username: ",url.username()); \
125 int test_ftpurl(void) {
126 const char* name = "test_ftpurl";
127 int number_of_errors = 0;
129 FTPHandleProviderI prov;
130 char* ascii,*n,*p;
132 ascii = "ftp://abi:psswd@host/eins/../drei", n = "abi", p = "psswd";
133 TESTURL;
135 ascii = "ftp://:psswd@host:22/eins/../drei", n = "anonymous", p = "psswd";
136 TESTURL;
138 ascii = "ftp://host/bla/../../test/", n = "anonymous", p = "";
139 TESTURL;
141 TESTEVAL;
145 int test_ftplist(void) {
146 int number_of_errors = 0;
147 const char* name = "test_ftplist";
149 FTPHandleProviderI provider;
151 ftp::FTPURL url(
152 rtl::OUString( "ftp://abi:psswd@abi-1/dir"),
153 &provider);
155 std::vector<ftp::FTPDirentry> vec =
156 url.list(com::sun::star::ucb::OpenMode::ALL);
158 if(vec.size() != 3)
159 ++number_of_errors;
161 if(!(vec[0].m_aName == "dir1" && vec[1].m_aName == "dir2" && vec[2].m_aName == "file1" ))
162 ++number_of_errors;
164 TESTEVAL;
168 #define TESTPARENT \
170 ftp::FTPURL url(rtl::OUString::createFromAscii(ascii),&prov); \
171 urlStr = url.parent(); \
172 if(!urlStr.equalsAscii(expect)) \
173 ++number_of_errors; \
177 int test_ftpparent(void) {
178 int number_of_errors = 0;
179 const char* name = "test_ftpparent";
180 FTPHandleProviderI prov;
182 rtl::OUString urlStr;
183 char *ascii,*expect;
185 ascii = "ftp://abi:psswd@abi-1/file";
186 expect = "ftp://abi:psswd@abi-1/";
187 TESTPARENT;
189 ascii = "ftp://abi:psswd@abi-1/dir/../file";
190 expect = "ftp://abi:psswd@abi-1/";
191 TESTPARENT;
193 ascii = "ftp://abi:psswd@abi-1/..";
194 expect = "ftp://abi:psswd@abi-1/../..";
195 TESTPARENT;
197 ascii = "ftp://abi:psswd@abi-1/../../dir";
198 expect = "ftp://abi:psswd@abi-1/../..";
199 TESTPARENT;
201 ascii = "ftp://abi:psswd@abi-1/";
202 expect = "ftp://abi:psswd@abi-1/..";
203 TESTPARENT;
205 TESTEVAL;
209 int test_ftpproperties(void) {
210 int number_of_errors = 0;
211 const char* name = "test_ftpproperties";
212 FTPHandleProviderI provider;
214 ftp::FTPURL url(
215 rtl::OUString( "ftp://abi:psswd@abi-1/file"),
216 &provider);
218 ftp::FTPDirentry ade(url.direntry());
220 if(!(ade.m_aName == "file" && ade.isFile()))
221 ++number_of_errors;
223 TESTEVAL;
227 int test_ftpopen(void)
229 int number_of_errors = 0;
230 const char* name = "test_ftpopen";
232 FTPHandleProviderI provider;
233 ftp::FTPURL url(
234 rtl::OUString( "ftp://abi:psswd@abi-1/file"),
235 &provider);
237 FILE* file = url.open();
238 if(file) {
239 int nbuf,ndest;
240 const int bffsz = 256;
241 char buff[bffsz];
242 char *dest = (char*) malloc(sizeof(char));
243 dest[0] = 0;
244 do {
245 memset((void*)buff, 0, bffsz);
246 fread(buff,bffsz-1,1,file);
247 nbuf = strlen(buff);
248 ndest = strlen(dest);
249 dest = (char*)realloc(dest,ndest + nbuf + 1);
250 strncat(dest,buff,nbuf);
251 } while(nbuf == bffsz-1);
252 fclose(file);
254 const char* expected = "You are now looking at the filecontent.\n";
255 if(strcmp(expected,dest))
256 ++number_of_errors;
257 free(dest);
258 } else
259 ++number_of_errors;
261 TESTEVAL;
265 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */