add more spacing
[personal-kdebase.git] / runtime / kurifilter-plugins / tests / kurifiltertest.cpp
blobbb4223122164c187681391923273aa7ea49ddc38
1 /*
2 * Copyright (C) 2002, 2003 David Faure <faure@kde.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License version 2 as published by the Free Software Foundation;
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Library General Public License for more details.
13 * You should have received a copy of the GNU Library General Public License
14 * along with this library; see the file COPYING.LIB. If not, write to
15 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 * Boston, MA 02110-1301, USA.
19 #include "qtest_kde.h"
21 #include <kurifilter.h>
22 #include <kaboutdata.h>
23 #include <kdebug.h>
24 #include <kstandarddirs.h>
25 #include <ksharedconfig.h>
26 #include <klocale.h>
27 #include <kio/netaccess.h>
28 #include <kdefakes.h>
29 #include <kconfiggroup.h>
31 #include <QtCore/QDir>
32 #include <QtCore/QRegExp>
34 #include <iostream>
35 #include "kurifiltertest.h"
37 QTEST_KDEMAIN( KUriFilterTest, NoGUI )
39 static const char * const s_uritypes[] = { "NetProtocol", "LOCAL_FILE", "LOCAL_DIR", "EXECUTABLE", "HELP", "SHELL", "BLOCKED", "ERROR", "UNKNOWN" };
40 #define NO_FILTERING -2
42 static void filter( const char* u, const char * expectedResult = 0, int expectedUriType = -1, const QStringList& list = QStringList(), const char * abs_path = 0, bool checkForExecutables = true )
44 QString a = QString::fromUtf8( u );
45 KUriFilterData * filterData = new KUriFilterData;
46 filterData->setData( a );
47 filterData->setCheckForExecutables( checkForExecutables );
49 if( abs_path )
51 filterData->setAbsolutePath( QLatin1String( abs_path ) );
52 kDebug() << "Filtering: " << a << " with abs_path=" << abs_path;
54 else
55 kDebug() << "Filtering: " << a;
57 if (KUriFilter::self()->filterUri(*filterData, list))
59 // Copied from minicli...
60 QString cmd;
61 KUrl uri = filterData->uri();
63 if ( uri.isLocalFile() && !uri.hasRef() && uri.query().isEmpty() )
64 cmd = uri.path();
65 else
66 cmd = uri.url();
68 switch( filterData->uriType() )
70 case KUriFilterData::LocalFile:
71 case KUriFilterData::LocalDir:
72 case KUriFilterData::Help:
73 kDebug() << "*** Result: Local Resource => '"
74 << filterData->uri().url() << "'" << endl;
75 break;
76 case KUriFilterData::NetProtocol:
77 kDebug() << "*** Result: Network Resource => '"
78 << filterData->uri().url() << "'" << endl;
79 break;
80 case KUriFilterData::Shell:
81 case KUriFilterData::Executable:
82 if( filterData->hasArgsAndOptions() )
83 cmd += filterData->argsAndOptions();
84 kDebug() << "*** Result: Executable/Shell => '" << cmd << "'";
85 break;
86 case KUriFilterData::Error:
87 kDebug() << "*** Result: Encountered error. See reason below.";
88 break;
89 default:
90 kDebug() << "*** Result: Unknown or invalid resource.";
93 if ( expectedUriType != -1 && expectedUriType != filterData->uriType() )
95 QCOMPARE( s_uritypes[filterData->uriType()],
96 s_uritypes[expectedUriType] );
97 kError() << " Got URI type " << s_uritypes[filterData->uriType()]
98 << " expected " << s_uritypes[expectedUriType] << endl;
101 if ( expectedResult )
103 // Hack for other locales than english, normalize google hosts to google.com
104 cmd = cmd.replace( QRegExp( "www\\.google\\.[^/]*/" ), "www.google.com/" );
105 QString expected = QString::fromUtf8( expectedResult );
106 QCOMPARE( cmd, expected );
109 else
111 if ( expectedUriType == NO_FILTERING )
112 kDebug() << "*** No filtering required.";
113 else
115 kDebug() << "*** Could not be filtered.";
116 if( expectedUriType != filterData->uriType() )
118 QCOMPARE( s_uritypes[filterData->uriType()],
119 s_uritypes[expectedUriType] );
124 delete filterData;
125 kDebug() << "-----";
128 static void testLocalFile( const QString& filename )
130 QFile tmpFile( filename ); // Yeah, I know, security risk blah blah. This is a test prog!
132 if ( tmpFile.open( QIODevice::ReadWrite ) )
134 QByteArray fname = QFile::encodeName( tmpFile.fileName() );
135 filter(fname, fname, KUriFilterData::LocalFile);
136 tmpFile.close();
137 tmpFile.remove();
139 else
140 kDebug() << "Couldn't create " << tmpFile.fileName() << ", skipping test";
143 static char s_delimiter = ':'; // the alternative is ' '
145 KUriFilterTest::KUriFilterTest()
147 minicliFilters << "kshorturifilter" << "kurisearchfilter" << "localdomainurifilter";
148 qtdir = getenv("QTDIR");
149 home = getenv("HOME");
150 kdehome = getenv("KDEHOME");
153 void KUriFilterTest::init()
155 kDebug() ;
156 setenv( "KDE_FORK_SLAVES", "yes", true ); // simpler, for the final cleanup
158 // Allow testing of the search engine using both delimiters...
159 const char* envDelimiter = ::getenv( "KURIFILTERTEST_DELIMITER" );
160 if ( envDelimiter )
161 s_delimiter = envDelimiter[0];
163 // Many tests check the "default search engine" feature.
164 // There is no default search engine by default (since it was annoying when making typos),
165 // so the user has to set it up, which we do here.
167 KConfigGroup cfg( KSharedConfig::openConfig( "kuriikwsfilterrc", KConfig::SimpleConfig ), "General" );
168 cfg.writeEntry( "DefaultSearchEngine", "google" );
169 cfg.writeEntry( "Verbose", true );
170 cfg.writeEntry( "KeywordDelimiter", QString(s_delimiter) );
171 cfg.sync();
174 // Enable verbosity for debugging
176 KSharedConfig::openConfig("kshorturifilterrc", KConfig::SimpleConfig )->group(QString()).writeEntry( "Verbose", true );
179 KStandardDirs::makeDir( kdehome+"/urifilter" );
182 void KUriFilterTest::tests()
184 // URI that should require no filtering
185 filter( "http://www.kde.org", "http://www.kde.org", KUriFilterData::NetProtocol );
186 filter( "http://www.kde.org/developer//index.html", "http://www.kde.org/developer//index.html", KUriFilterData::NetProtocol );
187 // URL with reference
188 filter( "http://www.kde.org/index.html#q8", "http://www.kde.org/index.html#q8", KUriFilterData::NetProtocol );
189 // local file with reference
190 filter( "file:/etc/passwd#q8", "file:///etc/passwd#q8", KUriFilterData::LocalFile );
191 filter( "file:///etc/passwd#q8", "file:///etc/passwd#q8", KUriFilterData::LocalFile );
192 filter( "/etc/passwd#q8", "file:///etc/passwd#q8", KUriFilterData::LocalFile );
193 // local file with query (can be used by javascript)
194 filter( "file:/etc/passwd?foo=bar", "file:///etc/passwd?foo=bar", KUriFilterData::LocalFile );
195 testLocalFile( "/tmp/kurifiltertest?foo" ); // local file with ? in the name (#58990)
196 testLocalFile( "/tmp/kurlfiltertest#foo" ); // local file with '#' in the name
197 testLocalFile( "/tmp/kurlfiltertest#foo?bar" ); // local file with both
198 testLocalFile( "/tmp/kurlfiltertest?foo#bar" ); // local file with both, the other way round
200 // hostnames are lowercased by KUrl
201 filter( "http://www.myDomain.commyPort/ViewObjectRes//Default:name=hello",
202 "http://www.mydomain.commyport/ViewObjectRes//Default:name=hello", KUriFilterData::NetProtocol);
203 filter( "ftp://ftp.kde.org", "ftp://ftp.kde.org", KUriFilterData::NetProtocol );
204 filter( "ftp://username@ftp.kde.org:500", "ftp://username@ftp.kde.org:500", KUriFilterData::NetProtocol );
206 // ShortURI/LocalDomain filter tests. NOTE: any of these tests can fail
207 // if you have specified your own patterns in kshorturifilterrc. For
208 // examples, see $KDEDIR/share/config/kshorturifilterrc .
209 filter( "linuxtoday.com", "http://linuxtoday.com", KUriFilterData::NetProtocol );
210 filter( "LINUXTODAY.COM", "http://linuxtoday.com", KUriFilterData::NetProtocol );
211 filter( "kde.org", "http://kde.org", KUriFilterData::NetProtocol );
212 filter( "ftp.kde.org", "ftp://ftp.kde.org", KUriFilterData::NetProtocol );
213 filter( "ftp.kde.org:21", "ftp://ftp.kde.org:21", KUriFilterData::NetProtocol );
214 filter( "cr.yp.to", "http://cr.yp.to", KUriFilterData::NetProtocol );
215 filter( "user@192.168.1.0:3128", "http://user@192.168.1.0:3128", KUriFilterData::NetProtocol );
216 filter( "127.0.0.1", "http://127.0.0.1", KUriFilterData::NetProtocol );
217 filter( "127.0.0.1:3128", "http://127.0.0.1:3128", KUriFilterData::NetProtocol );
218 filter( "foo@bar.com", "mailto:foo@bar.com", KUriFilterData::NetProtocol );
219 filter( "firstname.lastname@x.foo.bar", "mailto:firstname.lastname@x.foo.bar", KUriFilterData::NetProtocol );
220 filter( "www.123.foo", "http://www.123.foo", KUriFilterData::NetProtocol );
221 filter( "user@www.123.foo:3128", "http://user@www.123.foo:3128", KUriFilterData::NetProtocol );
223 // Exotic IPv4 address formats...
224 filter( "127.1", "http://127.1", KUriFilterData::NetProtocol );
225 filter( "127.0.1", "http://127.0.1", KUriFilterData::NetProtocol );
227 // Local domain filter - If you uncomment these test, make sure you
228 // you adjust it based on the localhost entry in your /etc/hosts file.
229 // filter( "localhost:3128", "http://localhost.localdomain:3128", KUriFilterData::NetProtocol );
230 // filter( "localhost", "http://localhost.localdomain", KUriFilterData::NetProtocol );
231 // filter( "localhost/~blah", "http://localhost.localdomain/~blah", KUriFilterData::NetProtocol );
233 filter( "/", "/", KUriFilterData::LocalDir );
234 filter( "/", "/", KUriFilterData::LocalDir, QStringList( "kshorturifilter" ) );
235 filter( "~/.bashrc", QDir::homePath().toLocal8Bit()+"/.bashrc", KUriFilterData::LocalFile, QStringList( "kshorturifilter" ) );
236 filter( "~", QDir::homePath().toLocal8Bit(), KUriFilterData::LocalDir, QStringList( "kshorturifilter" ), "/tmp" );
237 filter( "~foobar", 0, KUriFilterData::Error, QStringList( "kshorturifilter" ) );
238 filter( "user@host.domain", "mailto:user@host.domain", KUriFilterData::NetProtocol ); // new in KDE-3.2
240 // Windows style SMB (UNC) URL. Should be converted into the valid smb format...
241 filter( "\\\\mainserver\\share\\file", "smb://mainserver/share/file" , KUriFilterData::NetProtocol );
243 // KDE3: was not be filtered at all. All valid protocols of this form were be ignored.
244 // KDE4: parsed as "network protocol", seems fine to me (DF)
245 filter( "ftp:" , "ftp:", KUriFilterData::NetProtocol );
246 filter( "http:" , "http:", KUriFilterData::NetProtocol );
248 // The default search engine is set to 'Google'
249 filter( "gg:", "http://www.google.com/search?q=gg%3A&ie=UTF-8&oe=UTF-8", KUriFilterData::NetProtocol );
250 filter( "KDE", "http://www.google.com/search?q=KDE&ie=UTF-8&oe=UTF-8", KUriFilterData::NetProtocol );
251 filter( "FTP", "http://www.google.com/search?q=FTP&ie=UTF-8&oe=UTF-8", KUriFilterData::NetProtocol );
253 // Typing 'cp' or any other valid unix command in konq's location bar should result in
254 // a search using the default search engine
255 // 'ls' is a bit of a special case though, due to the toplevel domain called 'ls'
256 filter( "cp", "http://www.google.com/search?q=cp&ie=UTF-8&oe=UTF-8", KUriFilterData::NetProtocol,
257 QStringList(), 0, false /* don't check for executables, see konq_misc.cc */ );
259 // Executable tests - No IKWS in minicli
260 filter( "cp", "cp", KUriFilterData::Executable, minicliFilters );
261 filter( "kfmclient", "kfmclient", KUriFilterData::Executable, minicliFilters );
262 filter( "xwininfo", "xwininfo", KUriFilterData::Executable, minicliFilters );
263 filter( "KDE", "KDE", NO_FILTERING, minicliFilters );
264 filter( "I/dont/exist", "I/dont/exist", NO_FILTERING, minicliFilters ); //krazy:exclude=spelling
265 filter( "/I/dont/exist", 0, KUriFilterData::Error, minicliFilters ); //krazy:exclude=spelling
266 filter( "/I/dont/exist#a", 0, KUriFilterData::Error, minicliFilters ); //krazy:exclude=spelling
267 filter( "kfmclient --help", "kfmclient --help", KUriFilterData::Executable, minicliFilters ); // the args are in argsAndOptions()
268 filter( "/usr/bin/gs", "/usr/bin/gs", KUriFilterData::Executable, minicliFilters );
269 filter( "/usr/bin/gs -q -option arg1", "/usr/bin/gs -q -option arg1", KUriFilterData::Executable, minicliFilters ); // the args are in argsAndOptions()
271 // ENVIRONMENT variable
272 setenv( "SOMEVAR", "/somevar", 0 );
273 setenv( "ETC", "/etc", 0 );
275 filter( "$SOMEVAR/kdelibs/kio", 0, KUriFilterData::Error ); // note: this dir doesn't exist...
276 filter( "$ETC/passwd", "/etc/passwd", KUriFilterData::LocalFile );
277 QString qtdocPath = qtdir+"/doc/html/functions.html";
278 if (QFile::exists(qtdocPath)) {
279 QString expectedUrl = KUrl(qtdocPath).url()+"#s";
280 filter( "$QTDIR/doc/html/functions.html#s", expectedUrl.toUtf8(), KUriFilterData::LocalFile );
282 filter( "http://www.kde.org/$USER", "http://www.kde.org/$USER", KUriFilterData::NetProtocol ); // no expansion
284 filter( "$KDEHOME/share", kdehome+"/share", KUriFilterData::LocalDir );
285 KStandardDirs::makeDir( kdehome+"/urifilter/a+plus" );
286 filter( "$KDEHOME/urifilter/a+plus", kdehome+"/urifilter/a+plus", KUriFilterData::LocalDir );
288 // BR 27788
289 KStandardDirs::makeDir( kdehome+"/share/Dir With Space" );
290 filter( "$KDEHOME/share/Dir With Space", kdehome+"/share/Dir With Space", KUriFilterData::LocalDir );
292 // support for name filters (BR 93825)
293 filter( "$KDEHOME/*.txt", kdehome+"/*.txt", KUriFilterData::LocalDir );
294 filter( "$KDEHOME/[a-b]*.txt", kdehome+"/[a-b]*.txt", KUriFilterData::LocalDir );
295 filter( "$KDEHOME/a?c.txt", kdehome+"/a?c.txt", KUriFilterData::LocalDir );
296 filter( "$KDEHOME/?c.txt", kdehome+"/?c.txt", KUriFilterData::LocalDir );
297 // but let's check that a directory with * in the name still works
298 KStandardDirs::makeDir( kdehome+"/share/Dir*With*Stars" );
299 filter( "$KDEHOME/share/Dir*With*Stars", kdehome+"/share/Dir*With*Stars", KUriFilterData::LocalDir );
300 KStandardDirs::makeDir( kdehome+"/share/Dir?QuestionMark" );
301 filter( "$KDEHOME/share/Dir?QuestionMark", kdehome+"/share/Dir?QuestionMark", KUriFilterData::LocalDir );
302 KStandardDirs::makeDir( kdehome+"/share/Dir[Bracket" );
303 filter( "$KDEHOME/share/Dir[Bracket", kdehome+"/share/Dir[Bracket", KUriFilterData::LocalDir );
305 filter( "$HOME/$KDEDIR/kdebase/kcontrol/ebrowsing", 0, KUriFilterData::Error );
306 filter( "$1/$2/$3", "http://www.google.com/search?q=%241%2F%242%2F%243&ie=UTF-8&oe=UTF-8", KUriFilterData::NetProtocol ); // can be used as bogus or valid test. Currently triggers default search, i.e. google
307 filter( "$$$$", "http://www.google.com/search?q=%24%24%24%24&ie=UTF-8&oe=UTF-8", KUriFilterData::NetProtocol ); // worst case scenarios.
309 if (!qtdir.isEmpty()) {
310 filter( "$QTDIR", qtdir, KUriFilterData::LocalDir, QStringList( "kshorturifilter" ) ); //use specific filter.
312 filter( "$HOME", home, KUriFilterData::LocalDir, QStringList( "kshorturifilter" ) ); //use specific filter.
315 QString sc;
316 filter( sc.sprintf("gg%cfoo bar",s_delimiter).toUtf8(), "http://www.google.com/search?q=foo+bar&ie=UTF-8&oe=UTF-8", KUriFilterData::NetProtocol );
317 filter( sc.sprintf("bug%c55798", s_delimiter).toUtf8(), "http://bugs.kde.org/show_bug.cgi?id=55798", KUriFilterData::NetProtocol );
319 filter( sc.sprintf("gg%cC++", s_delimiter).toUtf8(), "http://www.google.com/search?q=C%2B%2B&ie=UTF-8&oe=UTF-8", KUriFilterData::NetProtocol );
320 filter( sc.sprintf("ya%cfoo bar was here", s_delimiter).toUtf8(), 0, -1 ); // this triggers default search, i.e. google
321 filter( sc.sprintf("gg%cwww.kde.org", s_delimiter).toUtf8(), "http://www.google.com/search?q=www.kde.org&ie=UTF-8&oe=UTF-8", KUriFilterData::NetProtocol );
322 filter( sc.sprintf("av%c+rock +sample", s_delimiter).toUtf8(), "http://www.altavista.com/cgi-bin/query?pg=q&kl=XX&stype=stext&q=%2Brock+%2Bsample", KUriFilterData::NetProtocol );
323 filter( QString::fromUtf8("gg%1é").arg(s_delimiter).toUtf8() /*eaccent in utf8*/, "http://www.google.com/search?q=%C3%A9&ie=UTF-8&oe=UTF-8", KUriFilterData::NetProtocol );
324 filter( QString::fromUtf8("gg%1прйвет").arg(s_delimiter).toUtf8() /* 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::NetProtocol );
326 // Absolute Path tests for kshorturifilter
327 filter( "./", kdehome+"/share", KUriFilterData::LocalDir, QStringList( "kshorturifilter" ), kdehome+"/share/" ); // cleanPath removes the trailing slash
328 filter( "../", kdehome, KUriFilterData::LocalDir, QStringList( "kshorturifilter" ), kdehome+"/share" );
329 filter( "config", kdehome+"/share/config", KUriFilterData::LocalDir, QStringList( "kshorturifilter" ), kdehome+"/share" );
332 #include "kurifiltertest.moc"