1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
/*
* Copyright (C) 2002, 2003 David Faure <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License version 2 as published by the Free Software Foundation;
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "kurifilter.h"
#include <config.h>
#include <iostream>
#include <stdlib.h>
#include <tdeaboutdata.h>
#include <tdeapplication.h>
#include <kdebug.h>
#include <tdecmdlineargs.h>
#include <kstandarddirs.h>
#include <ksimpleconfig.h>
#include <tqdir.h>
#include <tqregexp.h>
#include <tdeio/netaccess.h>
static const char * const s_uritypes[] = { "NET_PROTOCOL", "LOCAL_FILE", "LOCAL_DIR", "EXECUTABLE", "HELP", "SHELL", "BLOCKED", "ERROR", "UNKNOWN" };
#define NO_FILTERING -2
void filter( const char* u, const char * expectedResult = 0, int expectedUriType = -1, TQStringList list = TQStringList(), const char * abs_path = 0, bool checkForExecutables = true )
{
TQString a = TQString::fromUtf8( u );
KURIFilterData * m_filterData = new KURIFilterData;
m_filterData->setData( a );
m_filterData->setCheckForExecutables( checkForExecutables );
if( abs_path )
{
m_filterData->setAbsolutePath( TQString::fromLatin1( abs_path ) );
kdDebug() << "Filtering: " << a << " with abs_path=" << abs_path << endl;
}
else
kdDebug() << "Filtering: " << a << endl;
if (KURIFilter::self()->filterURI(*m_filterData, list))
{
// Copied from minicli...
TQString cmd;
KURL uri = m_filterData->uri();
if ( uri.isLocalFile() && !uri.hasRef() && uri.query().isEmpty() )
cmd = uri.path();
else
cmd = uri.url();
switch( m_filterData->uriType() )
{
case KURIFilterData::LOCAL_FILE:
case KURIFilterData::LOCAL_DIR:
case KURIFilterData::HELP:
kdDebug() << "*** Result: Local Resource => '"
<< m_filterData->uri().url() << "'" << endl;
break;
case KURIFilterData::NET_PROTOCOL:
kdDebug() << "*** Result: Network Resource => '"
<< m_filterData->uri().url() << "'" << endl;
break;
case KURIFilterData::SHELL:
case KURIFilterData::EXECUTABLE:
if( m_filterData->hasArgsAndOptions() )
cmd += m_filterData->argsAndOptions();
kdDebug() << "*** Result: Executable/Shell => '" << cmd << "'"<< endl;
break;
case KURIFilterData::ERROR:
kdDebug() << "*** Result: Encountered error. See reason below." << endl;
break;
default:
kdDebug() << "*** Result: Unknown or invalid resource." << endl;
}
if ( expectedUriType != -1 && expectedUriType != m_filterData->uriType() )
{
kdError() << " Got URI type " << s_uritypes[m_filterData->uriType()]
<< " expected " << s_uritypes[expectedUriType] << endl;
::exit(1);
}
if ( expectedResult )
{
// Hack for other locales than english, normalize google hosts to google.com
cmd = cmd.replace( TQRegExp( "www\\.google\\.[^/]*/" ), "www.google.com/" );
if ( cmd != TQString::fromLatin1( expectedResult ) )
{
kdError() << " Got " << cmd << " expected " << expectedResult << endl;
::exit(1);
}
}
}
else
{
if ( expectedUriType == NO_FILTERING )
kdDebug() << "*** No filtering required." << endl;
else
{
kdDebug() << "*** Could not be filtered." << endl;
if( expectedUriType != m_filterData->uriType() )
{
kdError() << " Got URI type " << s_uritypes[m_filterData->uriType()]
<< " expected " << s_uritypes[expectedUriType] << endl;
::exit(1);
}
}
}
delete m_filterData;
kdDebug() << "-----" << endl;
}
void testLocalFile( const TQString& filename )
{
TQFile tmpFile( filename ); // Yeah, I know, security risk blah blah. This is a test prog!
if ( tmpFile.open( IO_ReadWrite ) )
{
TQCString fname = TQFile::encodeName( tmpFile.name() );
filter(fname, fname, KURIFilterData::LOCAL_FILE);
tmpFile.close();
tmpFile.remove();
}
else
kdDebug() << "Couldn't create " << tmpFile.name() << ", skipping test" << endl;
}
static const char appName[] = "kurifiltertest";
static const char programName[] = I18N_NOOP("kurifiltertest");
static const char description[] = I18N_NOOP("Unit test for the URI filter plugin framework.");
static const char version[] = "1.5";
static const TDECmdLineOptions options[] =
{
{ "s", I18N_NOOP("Use space as keyword delimeter for web shortcuts"), 0},
TDECmdLineLastOption
};
int main(int argc, char **argv)
{
// Ensure that user configuration doesn't change the results of those tests
// TDEHOME needs to be writable though, for a tdesycoca database
setenv( "TDEHOME", TQFile::encodeName( TQDir::currentDirPath() + "/.tde-kurifiltertest" ), true );
setenv( "TDE_FORK_SLAVES", "yes", true ); // simpler, for the final cleanup
TDEAboutData aboutData(appName, programName, version, description);
TDECmdLineArgs::init(argc, argv, &aboutData);
TDECmdLineArgs::addCmdLineOptions( options );
TDEApplication app; // it _is_ GUI app
app.disableAutoDcopRegistration();
// Allow testing of the search engine using both delimiters...
char delimiter = TDECmdLineArgs::parsedArgs()->isSet("s") ? ' ' : ':';
// Many tests check the "default search engine" feature.
// There is no default search engine by default (since it was annoying when making typos),
// so the user has to set it up, which we do here.
{
KSimpleConfig cfg( "kuriikwsfilterrc" );
cfg.setGroup( "General" );
cfg.writeEntry( "DefaultSearchEngine", "google" );
cfg.writeEntry( "Verbose", true );
cfg.writeEntry( "KeywordDelimiter", delimiter );
cfg.sync();
}
// Enable verbosity for debugging
{
KSimpleConfig cfg( "tdeshorturifilterrc" );
cfg.writeEntry( "Verbose", true );
cfg.sync();
}
TQStringList minicliFilters;
minicliFilters << "tdeshorturifilter" << "kurisearchfilter" << "localdomainurifilter";
// URI that should require no filtering
filter( "http://www.kde.org", "http://www.kde.org", KURIFilterData::NET_PROTOCOL );
filter( "http://www.kde.org/developer//index.html", "http://www.kde.org/developer//index.html", KURIFilterData::NET_PROTOCOL );
// URL with reference
filter( "http://www.kde.org/index.html#q8", "http://www.kde.org/index.html#q8", KURIFilterData::NET_PROTOCOL );
// local file with reference
filter( "file:/etc/passwd#q8", "file:///etc/passwd#q8", KURIFilterData::LOCAL_FILE );
filter( "file:///etc/passwd#q8", "file:///etc/passwd#q8", KURIFilterData::LOCAL_FILE );
filter( "/etc/passwd#q8", "file:///etc/passwd#q8", KURIFilterData::LOCAL_FILE );
// local file with query (can be used by javascript)
filter( "file:/etc/passwd?foo=bar", "file:///etc/passwd?foo=bar", KURIFilterData::LOCAL_FILE );
testLocalFile( "/tmp/kurifiltertest?foo" ); // local file with ? in the name (#58990)
testLocalFile( "/tmp/kurlfiltertest#foo" ); // local file with '#' in the name
testLocalFile( "/tmp/kurlfiltertest#foo?bar" ); // local file with both
testLocalFile( "/tmp/kurlfiltertest?foo#bar" ); // local file with both, the other way round
// hostnames are lowercased by KURL
filter( "http://www.myDomain.commyPort/ViewObjectRes//Default:name=hello",
"http://www.mydomain.commyport/ViewObjectRes//Default:name=hello", KURIFilterData::NET_PROTOCOL);
filter( "ftp://ftp.kde.org", "ftp://ftp.kde.org", KURIFilterData::NET_PROTOCOL );
filter( "ftp://[email protected]:500", "ftp://[email protected]:500", KURIFilterData::NET_PROTOCOL );
// ShortURI/LocalDomain filter tests. NOTE: any of these tests can fail
// if you have specified your own patterns in tdeshorturifilterrc. For
// examples, see $TDEDIR/share/config/tdeshorturifilterrc .
filter( "linuxtoday.com", "http://linuxtoday.com", KURIFilterData::NET_PROTOCOL );
filter( "LINUXTODAY.COM", "http://linuxtoday.com", KURIFilterData::NET_PROTOCOL );
filter( "kde.org", "http://kde.org", KURIFilterData::NET_PROTOCOL );
filter( "ftp.kde.org", "ftp://ftp.kde.org", KURIFilterData::NET_PROTOCOL );
filter( "ftp.kde.org:21", "ftp://ftp.kde.org:21", KURIFilterData::NET_PROTOCOL );
filter( "cr.yp.to", "http://cr.yp.to", KURIFilterData::NET_PROTOCOL );
filter( "[email protected]:3128", "http://[email protected]:3128", KURIFilterData::NET_PROTOCOL );
filter( "127.0.0.1", "http://127.0.0.1", KURIFilterData::NET_PROTOCOL );
filter( "127.0.0.1:3128", "http://127.0.0.1:3128", KURIFilterData::NET_PROTOCOL );
filter( "[email protected]", "mailto:[email protected]", KURIFilterData::NET_PROTOCOL );
filter( "[email protected]", "mailto:[email protected]", KURIFilterData::NET_PROTOCOL );
filter( "www.123.foo", "http://www.123.foo", KURIFilterData::NET_PROTOCOL );
filter( "[email protected]:3128", "http://[email protected]:3128", KURIFilterData::NET_PROTOCOL );
// Exotic IPv4 address formats...
filter( "127.1", "http://127.1", KURIFilterData::NET_PROTOCOL );
filter( "127.0.1", "http://127.0.1", KURIFilterData::NET_PROTOCOL );
// Local domain filter - If you uncomment these test, make sure you
// you adjust it based on the localhost entry in your /etc/hosts file.
// filter( "localhost:3128", "http://localhost.localdomain:3128", KURIFilterData::NET_PROTOCOL );
// filter( "localhost", "http://localhost.localdomain", KURIFilterData::NET_PROTOCOL );
// filter( "localhost/~blah", "http://localhost.localdomain/~blah", KURIFilterData::NET_PROTOCOL );
filter( "/", "/", KURIFilterData::LOCAL_DIR );
filter( "/", "/", KURIFilterData::LOCAL_DIR, "tdeshorturifilter" );
filter( "~/.bashrc", TQDir::homeDirPath().local8Bit()+"/.bashrc", KURIFilterData::LOCAL_FILE, "tdeshorturifilter" );
filter( "~", TQDir::homeDirPath().local8Bit(), KURIFilterData::LOCAL_DIR, "tdeshorturifilter", "/tmp" );
filter( "~foobar", 0, KURIFilterData::ERROR, "tdeshorturifilter" );
filter( "[email protected]", "mailto:[email protected]", KURIFilterData::NET_PROTOCOL ); // new in KDE-3.2
// Windows style SMB (UNC) URL. Should be converted into the valid smb format...
filter( "\\\\mainserver\\share\\file", "smb://mainserver/share/file" , KURIFilterData::NET_PROTOCOL );
// Should not be filtered at all. All valid protocols of this form will be ignored.
filter( "ftp:" , "ftp:", KURIFilterData::UNKNOWN );
filter( "http:" , "http:", KURIFilterData::UNKNOWN );
/*
Automatic searching tests. NOTE: If the Default search engine is set to 'None',
this stuff will fail as the status returned will then be KURIFilterData::UNKNOWN.
*/
filter( "gg:", 0 , KURIFilterData::NET_PROTOCOL );
filter( "KDE", 0 , KURIFilterData::NET_PROTOCOL );
filter( "FTP", 0 , KURIFilterData::NET_PROTOCOL );
// If your default search engine is set to 'Google', you can uncomment the test below.
filter( "gg:", "http://www.google.com/search?q=gg%3A&ie=UTF-8&oe=UTF-8", KURIFilterData::NET_PROTOCOL );
filter( "KDE", "http://www.google.com/search?q=KDE&ie=UTF-8&oe=UTF-8", KURIFilterData::NET_PROTOCOL );
filter( "FTP", "http://www.google.com/search?q=FTP&ie=UTF-8&oe=UTF-8", KURIFilterData::NET_PROTOCOL );
// Typing 'cp' or any other valid unix command in konq's location bar should result in
// a search using the default search engine
// 'ls' is a bit of a special case though, due to the toplevel domain called 'ls'
filter( "cp", "http://www.google.com/search?q=cp&ie=UTF-8&oe=UTF-8", KURIFilterData::NET_PROTOCOL,
TQStringList(), 0, false /* don't check for executables, see konq_misc.cc */ );
// Executable tests - No IKWS in minicli
filter( "cp", "cp", KURIFilterData::EXECUTABLE, minicliFilters );
filter( "kfmclient", "kfmclient", KURIFilterData::EXECUTABLE, minicliFilters );
filter( "xwininfo", "xwininfo", KURIFilterData::EXECUTABLE, minicliFilters );
filter( "KDE", "KDE", NO_FILTERING, minicliFilters );
filter( "I/dont/exist", "I/dont/exist", NO_FILTERING, minicliFilters );
filter( "/I/dont/exist", 0, KURIFilterData::ERROR, minicliFilters );
filter( "/I/dont/exist#a", 0, KURIFilterData::ERROR, minicliFilters );
filter( "kfmclient --help", "kfmclient --help", KURIFilterData::EXECUTABLE, minicliFilters ); // the args are in argsAndOptions()
filter( "/usr/bin/gs", "/usr/bin/gs", KURIFilterData::EXECUTABLE, minicliFilters );
filter( "/usr/bin/gs -q -option arg1", "/usr/bin/gs -q -option arg1", KURIFilterData::EXECUTABLE, minicliFilters ); // the args are in argsAndOptions()
// ENVIRONMENT variable
setenv( "SOMEVAR", "/somevar", 0 );
setenv( "ETC", "/etc", 0 );
TQCString qtdir=getenv("QTDIR");
TQCString home = getenv("HOME");
TQCString tdehome = getenv("TDEHOME");
filter( "$SOMEVAR/tdelibs/tdeio", 0, KURIFilterData::ERROR ); // note: this dir doesn't exist...
filter( "$ETC/passwd", "/etc/passwd", KURIFilterData::LOCAL_FILE );
if( !qtdir.isEmpty() ) {
filter( "$QTDIR/doc/html/functions.html#s", TQCString("file://")+qtdir+"/doc/html/functions.html#s", KURIFilterData::LOCAL_FILE );
}
filter( "http://www.kde.org/$USER", "http://www.kde.org/$USER", KURIFilterData::NET_PROTOCOL ); // no expansion
// Assume the default (~/.trinity) if
if (tdehome.isEmpty())
{
tdehome += "$HOME/.trinity";
setenv("TDEHOME", tdehome.data(), 0);
}
filter( "$TDEHOME/share", tdehome+"/share", KURIFilterData::LOCAL_DIR );
TDEStandardDirs::makeDir( tdehome+"/a+plus" );
filter( "$TDEHOME/a+plus", tdehome+"/a+plus", KURIFilterData::LOCAL_DIR );
// BR 27788
TDEStandardDirs::makeDir( tdehome+"/share/Dir With Space" );
filter( "$TDEHOME/share/Dir With Space", tdehome+"/share/Dir With Space", KURIFilterData::LOCAL_DIR );
// support for name filters (BR 93825)
filter( "$TDEHOME/*.txt", tdehome+"/*.txt", KURIFilterData::LOCAL_DIR );
filter( "$TDEHOME/[a-b]*.txt", tdehome+"/[a-b]*.txt", KURIFilterData::LOCAL_DIR );
filter( "$TDEHOME/a?c.txt", tdehome+"/a?c.txt", KURIFilterData::LOCAL_DIR );
filter( "$TDEHOME/?c.txt", tdehome+"/?c.txt", KURIFilterData::LOCAL_DIR );
// but let's check that a directory with * in the name still works
TDEStandardDirs::makeDir( tdehome+"/share/Dir*With*Stars" );
filter( "$TDEHOME/share/Dir*With*Stars", tdehome+"/share/Dir*With*Stars", KURIFilterData::LOCAL_DIR );
TDEStandardDirs::makeDir( tdehome+"/share/Dir?QuestionMark" );
filter( "$TDEHOME/share/Dir?QuestionMark", tdehome+"/share/Dir?QuestionMark", KURIFilterData::LOCAL_DIR );
TDEStandardDirs::makeDir( tdehome+"/share/Dir[Bracket" );
filter( "$TDEHOME/share/Dir[Bracket", tdehome+"/share/Dir[Bracket", KURIFilterData::LOCAL_DIR );
filter( "$HOME/$TDEDIR/tdebase/kcontrol/ebrowsing", 0, KURIFilterData::ERROR );
filter( "$1/$2/$3", "http://www.google.com/search?q=$1/$2/$3&ie=UTF-8&oe=UTF-8", KURIFilterData::NET_PROTOCOL ); // can be used as bogus or valid test. Currently triggers default search, i.e. google
filter( "$$$$", "http://www.google.com/search?q=$$$$&ie=UTF-8&oe=UTF-8", KURIFilterData::NET_PROTOCOL ); // worst case scenarios.
// Replaced the match testing with a 0 since
// the shortURI filter will return the string
// itself if the requested environment variable
// is not already set.
if( !qtdir.isEmpty() ) {
filter( "$QTDIR", 0, KURIFilterData::LOCAL_DIR, "tdeshorturifilter" ); //use specific filter.
}
filter( "$HOME", home, KURIFilterData::LOCAL_DIR, "tdeshorturifilter" ); //use specific filter.
TQCString sc;
filter( sc.sprintf("gg%cfoo bar",delimiter), "http://www.google.com/search?q=foo+bar&ie=UTF-8&oe=UTF-8", KURIFilterData::NET_PROTOCOL );
filter( sc.sprintf("bug%c55798", delimiter), "http://bugs.pearsoncomputing.net/show_bug.cgi?id=55798", KURIFilterData::NET_PROTOCOL );
filter( sc.sprintf("gg%cC++", delimiter), "http://www.google.com/search?q=C%2B%2B&ie=UTF-8&oe=UTF-8", KURIFilterData::NET_PROTOCOL );
filter( sc.sprintf("ya%cfoo bar was here", delimiter), 0, -1 ); // this triggers default search, i.e. google
filter( sc.sprintf("gg%cwww.kde.org", delimiter), "http://www.google.com/search?q=www.kde.org&ie=UTF-8&oe=UTF-8", KURIFilterData::NET_PROTOCOL );
filter( sc.sprintf("av%c+rock +sample", delimiter), "http://www.altavista.com/cgi-bin/query?pg=q&kl=XX&stype=stext&q=%2Brock+%2Bsample", KURIFilterData::NET_PROTOCOL );
filter( sc.sprintf("gg%cé", delimiter) /*eaccent in utf8*/, "http://www.google.com/search?q=%C3%A9&ie=UTF-8&oe=UTF-8", KURIFilterData::NET_PROTOCOL );
filter( sc.sprintf("gg%cпрйвет", delimiter) /* greetings in russian utf-8*/, "http://www.google.com/search?q=%D0%BF%D1%80%D0%B9%D0%B2%D0%B5%D1%82&ie=UTF-8&oe=UTF-8", KURIFilterData::NET_PROTOCOL );
// Absolute Path tests for tdeshorturifilter
filter( "./", tdehome+"/share", KURIFilterData::LOCAL_DIR, "tdeshorturifilter", tdehome+"/share/" ); // cleanDirPath removes the trailing slash
filter( "../", tdehome, KURIFilterData::LOCAL_DIR, "tdeshorturifilter", tdehome+"/share" );
filter( "config", tdehome+"/share/config", KURIFilterData::LOCAL_DIR, "tdeshorturifilter", tdehome+"/share" );
// Clean up
TDEIO::NetAccess::del( tdehome, 0 );
kdDebug() << "All tests done. Go home..." << endl;
return 0;
}
|