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.

kruntest.cpp 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (C) 2002 David Faure <faure@kde.org>
  3. * Copyright (C) 2003 Waldo Bastian <bastian@kde.org>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License version 2 as published by the Free Software Foundation;
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public License
  15. * along with this library; see the file COPYING.LIB. If not, write to
  16. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA.
  18. */
  19. // ------ this test works only if your terminal application is set to "x-term" ------
  20. #include "kruntest.h"
  21. #include <tdeapplication.h>
  22. #include <kdebug.h>
  23. #include <kshell.h>
  24. #include <kservice.h>
  25. #include <kde_file.h>
  26. #include <tqpushbutton.h>
  27. #include <tqlayout.h>
  28. #include <tqdir.h>
  29. #include <stdlib.h>
  30. #include <unistd.h>
  31. #include <fcntl.h>
  32. const int MAXKRUNS = 100;
  33. testKRun * myArray[MAXKRUNS];
  34. void testKRun::foundMimeType( const TQString& _type )
  35. {
  36. kdDebug() << "testKRun::foundMimeType " << _type << endl;
  37. kdDebug() << "testKRun::foundMimeType URL=" << m_strURL.url() << endl;
  38. m_bFinished = true;
  39. m_timer.start( 0, true );
  40. return;
  41. }
  42. Receiver::Receiver()
  43. {
  44. TQVBoxLayout *lay = new TQVBoxLayout(this);
  45. lay->setAutoAdd(true);
  46. TQPushButton * h = new TQPushButton( "Press here to terminate", this );
  47. start = new TQPushButton( "Launch KRuns", this );
  48. stop = new TQPushButton( "Stop those KRuns", this );
  49. stop->setEnabled(false);
  50. TQObject::connect( h, TQT_SIGNAL(clicked()), kapp, TQT_SLOT(quit()) );
  51. TQObject::connect( start, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotStart()) );
  52. TQObject::connect( stop, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotStop()) );
  53. adjustSize();
  54. show();
  55. }
  56. void Receiver::slotStop()
  57. {
  58. for (int i = 0 ; i < MAXKRUNS ; i++ )
  59. {
  60. kdDebug() << " deleting krun " << i << endl;
  61. delete myArray[i];
  62. }
  63. start->setEnabled(true);
  64. stop->setEnabled(false);
  65. }
  66. void Receiver::slotStart()
  67. {
  68. for (int i = 0 ; i < MAXKRUNS ; i++ )
  69. {
  70. kdDebug() << "creating testKRun " << i << endl;
  71. myArray[i] = new testKRun( KURL("file:/tmp"), 0, true, false /* no autodelete */ );
  72. }
  73. start->setEnabled(false);
  74. stop->setEnabled(true);
  75. }
  76. void check(TQString txt, TQString a, TQString b)
  77. {
  78. if (a.isEmpty())
  79. a = TQString::null;
  80. if (b.isEmpty())
  81. b = TQString::null;
  82. if (a == b)
  83. kdDebug() << txt << " : '" << a << "' - ok" << endl;
  84. else {
  85. kdDebug() << txt << " : '" << a << "' but expected '" << b << "' - KO!" << endl;
  86. exit(1);
  87. }
  88. }
  89. const char *bt(bool tr) { return tr?"true":"false"; }
  90. void checkBN(TQString a, bool tr, TQString b)
  91. {
  92. check( TQString().sprintf("binaryName('%s', %s)", a.latin1(), bt(tr)), KRun::binaryName(a, tr), b);
  93. }
  94. void checkPDE(const KService &service, const KURL::List &urls, bool hs, bool tf, TQString b)
  95. {
  96. check(
  97. TQString().sprintf("processDesktopExec( "
  98. "service = {\nexec = %s\nterminal = %s, terminalOptions = %s\nsubstituteUid = %s, user = %s },"
  99. "\nURLs = { %s },\nhas_shell = %s, temp_files = %s )",
  100. service.exec().latin1(), bt(service.terminal()), service.terminalOptions().latin1(), bt(service.substituteUid()), service.username().latin1(),
  101. KShell::joinArgs(urls.toStringList()).latin1(), bt(hs), bt(tf)),
  102. KShell::joinArgs(KRun::processDesktopExec(service,urls,hs,tf)), b);
  103. }
  104. int main(int argc, char **argv)
  105. {
  106. TDEApplication app( argc, argv, "kruntest", true /*styles*/, true /* it _has_ a GUI ! */);
  107. // First some non-interactive tests
  108. checkBN( "/usr/bin/ls", true, "ls");
  109. checkBN( "/usr/bin/ls", false, "/usr/bin/ls");
  110. checkBN( "/path/to/wine \"long argument with path\"", true, "wine" );
  111. checkBN( "/path/with/a/sp\\ ace/exe arg1 arg2", true, "exe" );
  112. checkBN( "\"progname\" \"arg1\"", true, "progname" );
  113. checkBN( "'quoted' \"arg1\"", true, "quoted" );
  114. checkBN( " 'leading space' arg1", true, "leading space" );
  115. KURL::List l0;
  116. KURL::List l1; l1 << "file:/tmp";
  117. KURL::List l2; l2 << "http://localhost/foo";
  118. KURL::List l3; l3 << "file:/local/file" << "http://remotehost.org/bar";
  119. static const char
  120. *execs[] = { "Exec=date -u", "Exec=echo $$PWD" },
  121. *terms[] = { "Terminal=false", "Terminal=true\nTerminalOptions=-T \"%f - %c\"" },
  122. *sus[] = { "X-TDE-SubstituteUID=false", "X-TDE-SubstituteUID=true\nX-TDE-Username=sprallo" },
  123. *rslts[] = {
  124. "'date' '-u'", // 0
  125. "'/bin/sh' '-c' 'echo $PWD '", // 1
  126. "'x-term' '-T' ' - just_a_test' '-e' 'date' '-u'", // 2
  127. "'x-term' '-T' ' - just_a_test' '-e' '/bin/sh' '-c' 'echo $PWD '", // 3
  128. "'tdesu' '-u' 'sprallo' '-c' 'date -u '", // 4
  129. "'tdesu' '-u' 'sprallo' '-c' '/bin/sh -c '\\''echo $PWD '\\'''", // 5
  130. "'x-term' '-T' ' - just_a_test' '-e' 'su' 'sprallo' '-c' 'date -u '", // 6
  131. "'x-term' '-T' ' - just_a_test' '-e' 'su' 'sprallo' '-c' '/bin/sh -c '\\''echo $PWD '\\'''", // 7
  132. "'date -u '", // 8
  133. "'echo $PWD '", // 9
  134. "'x-term -T \" - just_a_test\"' '-e' 'date -u '", // a
  135. "'x-term -T \" - just_a_test\"' '-e' '/bin/sh -c '\\''echo $PWD '\\'''", // b
  136. "'tdesu' '-u' 'sprallo' '-c' ''\\''date -u '\\'''", // c
  137. "'tdesu' '-u' 'sprallo' '-c' ''\\''/bin/sh -c '\\''\\'\\'''\\''echo $PWD '\\''\\'\\'''\\'''\\'''", // d
  138. "'x-term -T \" - just_a_test\"' '-e' 'su' 'sprallo' '-c' ''\\''date -u '\\'''", // e
  139. "'x-term -T \" - just_a_test\"' '-e' 'su' 'sprallo' '-c' ''\\''/bin/sh -c '\\''\\'\\'''\\''echo $PWD '\\''\\'\\'''\\'''\\'''", // f
  140. };
  141. for (int hs = 0; hs < 2; hs++)
  142. for (int su = 0; su < 2; su++)
  143. for (int te = 0; te < 2; te++)
  144. for (int ex = 0; ex < 2; ex++) {
  145. int fd = creat("kruntest.desktop", 0666);
  146. FILE *f;
  147. if (fd < 0) abort();
  148. f = KDE_fdopen(fd, "w");
  149. fprintf(f, "[Desktop Entry]\n"
  150. "Type=Application\n"
  151. "Name=just_a_test\n"
  152. "Icon=~/icon.png\n"
  153. "%s\n%s\n%s\n",execs[ex],terms[te],sus[su]);
  154. close(fd);
  155. fclose(f);
  156. KService s(TQDir::currentDirPath() + "/kruntest.desktop");
  157. unlink("kruntest.desktop");
  158. checkPDE( s, l0, hs, false, rslts[ex+te*2+su*4+hs*8]);
  159. }
  160. KService s1("dummy", "kate %U", "app");
  161. checkPDE( s1, l0, false, false, "'kate'");
  162. checkPDE( s1, l1, false, false, "'kate' '/tmp'");
  163. checkPDE( s1, l2, false, false, "'kate' 'http://localhost/foo'");
  164. checkPDE( s1, l3, false, false, "'kate' '/local/file' 'http://remotehost.org/bar'");
  165. KService s2("dummy", "kate %u", "app");
  166. checkPDE( s2, l0, false, false, "'kate'");
  167. checkPDE( s2, l1, false, false, "'kate' '/tmp'");
  168. checkPDE( s2, l2, false, false, "'kate' 'http://localhost/foo'");
  169. checkPDE( s2, l3, false, false, "'kate'");
  170. KService s3("dummy", "kate %F", "app");
  171. checkPDE( s3, l0, false, false, "'kate'");
  172. checkPDE( s3, l1, false, false, "'kate' '/tmp'");
  173. checkPDE( s3, l2, false, false, "'kfmexec' 'kate %F' 'http://localhost/foo'");
  174. checkPDE( s3, l3, false, false, "'kfmexec' 'kate %F' 'file:/local/file' 'http://remotehost.org/bar'");
  175. checkPDE( s3, l1, false, true, "'kfmexec' '--tempfiles' 'kate %F' 'file:/tmp'");
  176. checkPDE( s3, l1, true, true, "''\\''kfmexec'\\'' '\\''--tempfiles'\\'' '\\''kate %F'\\'' '\\''file:/tmp'\\'''");
  177. KService s4("dummy", "sh -c \"kate \"'\\\"'\"%F\"'\\\"'", "app");
  178. checkPDE( s4, l1, false, false, "'kate' '\"/tmp\"'");
  179. Receiver receiver;
  180. app.setMainWidget(&receiver);
  181. return app.exec();
  182. }
  183. #include "kruntest.moc"