TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

362 lines
18 KiB

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