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