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.

kacltest.cpp 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* This file is part of the KDE project
  2. Copyright (C) 2005 Till Adam <adam@kde.org>
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public
  5. License as published by the Free Software Foundation; either
  6. version 2 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public License
  12. along with this library; see the file COPYING.LIB. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  14. Boston, MA 02110-1301, USA.
  15. */
  16. #include "kacltest.h"
  17. #include <config.h>
  18. #include <kacl.h>
  19. #include <tdeapplication.h>
  20. #include <kdebug.h>
  21. #include <tdecmdlineargs.h>
  22. #include <tqfileinfo.h>
  23. #include <tqeventloop.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <assert.h>
  27. #include <tqdir.h>
  28. // The code comes partly from tdebase/tdeioslave/trash/testtrash.cpp
  29. static bool check(const TQString& txt, TQString a, TQString b)
  30. {
  31. if (a.isEmpty())
  32. a = TQString::null;
  33. if (b.isEmpty())
  34. b = TQString::null;
  35. if (a == b) {
  36. kdDebug() << txt << " : checking '" << a << "' against expected value '" << b << "'... " << "ok" << endl;
  37. }
  38. else {
  39. kdDebug() << txt << " : checking '" << a << "' against expected value '" << b << "'... " << "KO !" << endl;
  40. exit(1);
  41. }
  42. return true;
  43. }
  44. template<typename T>
  45. static bool check(const TQString& txt, T a, T b)
  46. {
  47. if (a == b) {
  48. kdDebug() << txt << " : checking '" << a << "' against expected value '" << b << "'... " << "ok" << endl;
  49. }
  50. else {
  51. kdDebug() << txt << " : checking '" << a << "' against expected value '" << b << "'... " << "KO !" << endl;
  52. exit(1);
  53. }
  54. return true;
  55. }
  56. int main(int argc, char *argv[])
  57. {
  58. TDEApplication::disableAutoDcopRegistration();
  59. TDECmdLineArgs::init(argc,argv,"kacltest", 0, 0, 0, 0);
  60. TDEApplication app(/*styles=*/false, /*gui=*/false);
  61. KACLTest test;
  62. test.setup();
  63. test.runAll();
  64. test.cleanup();
  65. kdDebug() << "All tests OK." << endl;
  66. return 0; // success. The exit(1) in check() is what happens in case of failure.
  67. }
  68. #ifdef Q_OS_FREEBSD
  69. static const TQString s_group1 = TQString::fromLatin1("staff");
  70. static const TQString s_group2 = TQString::fromLatin1("guest");
  71. #else
  72. static const TQString s_group1 = TQString::fromLatin1("audio");
  73. static const TQString s_group2 = TQString::fromLatin1("users");
  74. #endif
  75. static const TQString s_testACL = TQString::fromLatin1( "user::rw-\nuser:bin:rwx\ngroup::rw-\nmask::rwx\nother::r--\n" );
  76. static const TQString s_testACL2 = TQString::fromLatin1( "user::rwx\nuser:bin:rwx\ngroup::rw-\n") +
  77. TQString::fromLatin1( "group:" ) + s_group1 + TQString::fromLatin1( ":--x\n" ) +
  78. TQString::fromLatin1( "group:" ) + s_group2 + TQString::fromLatin1( ":r--\n" ) +
  79. TQString::fromLatin1( "mask::r-x\nother::r--\n" );
  80. static const TQString s_testACLEffective = TQString::fromLatin1( "user::rwx\nuser:bin:rwx #effective:r-x\ngroup::rw- #effective:r--\n" ) +
  81. TQString::fromLatin1( "group:" ) + s_group1 + TQString::fromLatin1( ":--x\n" ) +
  82. TQString::fromLatin1( "group:" ) + s_group2 + TQString::fromLatin1( ":r--\n" ) +
  83. TQString::fromLatin1( "mask::r-x\nother::r--\n" );
  84. KACLTest::KACLTest()
  85. :m_acl( s_testACL )
  86. {
  87. }
  88. void KACLTest::setup()
  89. {
  90. }
  91. void KACLTest::runAll()
  92. {
  93. testAsString();
  94. testGetOwnerPermissions();
  95. testGetOwningGroupPermissions();
  96. testGetOthersPermissions();
  97. testGetMaskPermissions();
  98. testGetAllUserPermissions();
  99. testIsExtended();
  100. // from here on we operate with the second test string
  101. testSetACL();
  102. testGetAllGroupsPermissions();
  103. testOperators();
  104. testSettingBasic();
  105. testSettingExtended();
  106. testSettingErrorHandling();
  107. testNewMask();
  108. }
  109. void KACLTest::cleanup()
  110. {
  111. }
  112. void KACLTest::testAsString()
  113. {
  114. check( "asString: ", m_acl.asString(), s_testACL );
  115. }
  116. void KACLTest::testSetACL()
  117. {
  118. m_acl.setACL( s_testACL2 );
  119. check( "setACL: ", m_acl.asString().simplifyWhiteSpace().remove(" "), s_testACLEffective.simplifyWhiteSpace().remove(" ") );
  120. }
  121. void KACLTest::testGetOwnerPermissions()
  122. {
  123. check( "Owner permissions: ", TQString::number( m_acl.ownerPermissions() ), "6" );
  124. }
  125. void KACLTest::testGetOwningGroupPermissions()
  126. {
  127. check( "Owning group permissions: ", TQString::number( m_acl.owningGroupPermissions() ), "6" );
  128. }
  129. void KACLTest::testGetOthersPermissions()
  130. {
  131. check( "Others permissions: ", TQString::number( m_acl.othersPermissions() ), "4" );
  132. }
  133. void KACLTest::testGetMaskPermissions()
  134. {
  135. bool exists = false;
  136. unsigned short mask = m_acl.maskPermissions( exists );
  137. check( "Mask permissions: ", TQString::number( mask ), "7" );
  138. check( "Mask permissions: ", exists, true );
  139. }
  140. void KACLTest::testGetAllUserPermissions()
  141. {
  142. ACLUserPermissionsList list = m_acl.allUserPermissions();
  143. ACLUserPermissionsConstIterator it = list.begin();
  144. TQString name;
  145. unsigned short permissions;
  146. int count = 0;
  147. while ( it != list.end() ) {
  148. name = ( *it ).first;
  149. permissions = ( *it ).second;
  150. ++it;
  151. ++count;
  152. }
  153. check( "All users count: ", TQString::number( count ), "1" );
  154. check( "All users name: ", name, "bin" );
  155. check( "All users permissions: ", TQString::number( permissions ), "7" );
  156. }
  157. void KACLTest::testGetAllGroupsPermissions()
  158. {
  159. ACLGroupPermissionsList list = m_acl.allGroupPermissions();
  160. ACLGroupPermissionsConstIterator it = list.begin();
  161. TQString name;
  162. unsigned short permissions;
  163. int count = 0;
  164. while ( it != list.end() ) {
  165. name = ( *it ).first;
  166. permissions = ( *it ).second;
  167. // setACL sorts them alphabetically ...
  168. if ( count == 0 ) {
  169. check( "All groups name: ", name, s_group1 );
  170. check( "All groups permissions: ", TQString::number( permissions ), "1" );
  171. } else if ( count == 1 ) {
  172. check( "All groups name: ", name, s_group2 );
  173. check( "All groups permissions: ", TQString::number( permissions ), "4" );
  174. }
  175. ++it;
  176. ++count;
  177. }
  178. check( "All users count: ", TQString::number( count ), "2" );
  179. }
  180. void KACLTest::testIsExtended()
  181. {
  182. KACL dukeOfMonmoth( s_testACL );
  183. check( "isExtended on an extended one: ", dukeOfMonmoth.isExtended(), true );
  184. KACL earlOfUpnor( "user::r--\ngroup::r--\nother::r--\n" );
  185. check( "isExtended on a not extended one: ", earlOfUpnor.isExtended(), false );
  186. }
  187. void KACLTest::testOperators()
  188. {
  189. KACL dukeOfMonmoth( s_testACL );
  190. KACL JamesScott( s_testACL );
  191. KACL earlOfUpnor( s_testACL2 );
  192. check( "operator== on different ones: ", dukeOfMonmoth == earlOfUpnor, false );
  193. check( "operator== on identical ones: ", dukeOfMonmoth == JamesScott, true );
  194. check( "operator!= on diffenrent ones: ", dukeOfMonmoth != earlOfUpnor, true );
  195. check( "operator!=: on identical ones: ", dukeOfMonmoth != JamesScott, false );
  196. }
  197. void KACLTest::testSettingBasic()
  198. {
  199. KACL CharlesII( s_testACL );
  200. CharlesII.setOwnerPermissions( 7 ); // clearly
  201. CharlesII.setOwningGroupPermissions( 0 );
  202. CharlesII.setOthersPermissions( 0 );
  203. check( "setOwnerPermissions: ", TQString::number( CharlesII.ownerPermissions() ),"7" );
  204. check( "setOwningGroupPermissions: ", TQString::number( CharlesII.owningGroupPermissions() ),"0" );
  205. check( "setOthersPermissions: ", TQString::number( CharlesII.othersPermissions() ),"0" );
  206. }
  207. void KACLTest::testSettingExtended()
  208. {
  209. KACL CharlesII( s_testACL );
  210. CharlesII.setMaskPermissions( 7 ); // clearly
  211. bool dummy = false;
  212. check( "setMaskPermissions: ", TQString::number( CharlesII.maskPermissions( dummy ) ),"7" );
  213. const TQString expected( "user::rw-\nuser:root:rwx\nuser:bin:r--\ngroup::rw-\nmask::rwx\nother::r--\n" );
  214. ACLUserPermissionsList users;
  215. ACLUserPermissions user = qMakePair( TQString( "root" ), ( unsigned short )7 );
  216. users.append( user );
  217. user = qMakePair( TQString( "bin" ), ( unsigned short )4 );
  218. users.append( user );
  219. CharlesII.setAllUserPermissions( users );
  220. check( "setAllUserPermissions: ", CharlesII.asString(), expected );
  221. CharlesII.setACL( s_testACL ); // reset
  222. // it already has an entry for bin, let's change it
  223. CharlesII.setNamedUserPermissions( TQString("bin"), 4 );
  224. CharlesII.setNamedUserPermissions( TQString( "root" ), 7 );
  225. check( "setNamedUserPermissions: ", CharlesII.asString(), expected );
  226. // groups, all and named
  227. const TQString expected2 = TQString::fromLatin1( "user::rw-\nuser:bin:rwx\ngroup::rw-\ngroup:" ) + s_group1 +
  228. TQString::fromLatin1( ":-wx\ngroup:" ) + s_group2 + TQString::fromLatin1(":r--\nmask::rwx\nother::r--\n" );
  229. CharlesII.setACL( s_testACL ); // reset
  230. ACLGroupPermissionsList groups;
  231. ACLGroupPermissions group = qMakePair( s_group1, ( unsigned short )3 );
  232. groups.append( group );
  233. group = qMakePair( s_group2, ( unsigned short )4 );
  234. groups.append( group );
  235. CharlesII.setAllGroupPermissions( groups );
  236. check( "setAllGroupPermissions: ", CharlesII.asString(), expected2 );
  237. CharlesII.setACL( s_testACL ); // reset
  238. CharlesII.setNamedGroupPermissions( s_group1, 3 );
  239. CharlesII.setNamedGroupPermissions( s_group2, 4 );
  240. check( "setNamedGroupPermissions: ", CharlesII.asString(), expected2 );
  241. }
  242. void KACLTest::testSettingErrorHandling()
  243. {
  244. KACL foo( s_testACL );
  245. bool v = foo.setNamedGroupPermissions( s_group1, 7 ); // existing group
  246. check( "Existing group: ", v, true );
  247. v = foo.setNamedGroupPermissions( "jongel", 7 ); // non-existing group
  248. check( "Non-existing group: ", v, false );
  249. v = foo.setNamedUserPermissions( "bin", 7 ); // existing user
  250. check( "Existing user: ", v, true );
  251. v = foo.setNamedUserPermissions( "jongel", 7 ); // non-existing user
  252. check( "Non-existing user: ", v, false );
  253. }
  254. void KACLTest::testNewMask()
  255. {
  256. KACL CharlesII( "user::rw-\ngroup::rw-\nother::rw\n" );
  257. bool dummy = false;
  258. CharlesII.maskPermissions( dummy );
  259. check( "mask exists: ", dummy, false );
  260. CharlesII.setMaskPermissions( 6 );
  261. check( "new mask set: ", TQString::number( CharlesII.maskPermissions( dummy ) ), "6" );
  262. check( "mask exists now: ", dummy, true );
  263. }