summaryrefslogtreecommitdiffstats
path: root/kio/tests/kacltest.cpp
blob: 0f9ac9e7a9ed6f1472ab6f45d3c3b0aca5ce76d3 (plain)
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
/* This file is part of the KDE project
   Copyright (C) 2005 Till Adam <adam@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   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 "kacltest.h"

#include <config.h>

#include <kacl.h>
#include <kapplication.h>
#include <kdebug.h>
#include <kcmdlineargs.h>

#include <tqfileinfo.h>
#include <tqeventloop.h>

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <tqdir.h>

// The code comes partly from kdebase/kioslave/trash/testtrash.cpp

static bool check(const TQString& txt, TQString a, TQString b)
{
    if (a.isEmpty())
        a = TQString::null;
    if (b.isEmpty())
        b = TQString::null;
    if (a == b) {
        kdDebug() << txt << " : checking '" << a << "' against expected value '" << b << "'... " << "ok" << endl;
    }
    else {
        kdDebug() << txt << " : checking '" << a << "' against expected value '" << b << "'... " << "KO !" << endl;
        exit(1);
    }
    return true;
}

template<typename T> 
static bool check(const TQString& txt, T a, T b)
{
    if (a == b) {
        kdDebug() << txt << " : checking '" << a << "' against expected value '" << b << "'... " << "ok" << endl;
    }
    else {
        kdDebug() << txt << " : checking '" << a << "' against expected value '" << b << "'... " << "KO !" << endl;
        exit(1);
    }
    return true;
}

int main(int argc, char *argv[])
{
    KApplication::disableAutoDcopRegistration();
    KCmdLineArgs::init(argc,argv,"kacltest", 0, 0, 0, 0);
    KApplication app;

    KACLTest test;
    test.setup();
    test.runAll();
    test.cleanup();
    kdDebug() << "All tests OK." << endl;
    return 0; // success. The exit(1) in check() is what happens in case of failure.
}

#ifdef Q_OS_FREEBSD
static const TQString s_group1 = TQString::tqfromLatin1("staff");
static const TQString s_group2 = TQString::tqfromLatin1("guest");
#else
static const TQString s_group1 = TQString::tqfromLatin1("audio");
static const TQString s_group2 = TQString::tqfromLatin1("users");
#endif

static const TQString s_testACL = TQString::tqfromLatin1( "user::rw-\nuser:bin:rwx\ngroup::rw-\ntqmask::rwx\nother::r--\n" );
static const TQString s_testACL2 = TQString::tqfromLatin1( "user::rwx\nuser:bin:rwx\ngroup::rw-\n") +
                                  TQString::tqfromLatin1( "group:" ) + s_group1 + TQString::tqfromLatin1( ":--x\n" ) +
                                  TQString::tqfromLatin1( "group:" ) + s_group2 + TQString::tqfromLatin1( ":r--\n" ) +
                                  TQString::tqfromLatin1( "tqmask::r-x\nother::r--\n" );
static const TQString s_testACLEffective = TQString::tqfromLatin1( "user::rwx\nuser:bin:rwx #effective:r-x\ngroup::rw- #effective:r--\n" ) + 
                                          TQString::tqfromLatin1( "group:" ) + s_group1 + TQString::tqfromLatin1( ":--x\n" ) +
                                          TQString::tqfromLatin1( "group:" ) + s_group2 + TQString::tqfromLatin1( ":r--\n" ) +
                                          TQString::tqfromLatin1( "tqmask::r-x\nother::r--\n" );

KACLTest::KACLTest()
:m_acl( s_testACL )
{
}

void KACLTest::setup()
{
}

void KACLTest::runAll()
{
  testAsString();
  testGetOwnerPermissions();
  testGetOwningGroupPermissions();
  testGetOthersPermissions();
  
  testGetMaskPermissions();
  testGetAllUserPermissions();

  testIsExtended();

  // from here on we operate with the second test string
  testSetACL();
  testGetAllGroupsPermissions();

  testOperators();
  testSettingBasic();
  testSettingExtended();
  testSettingErrorHandling();

  testNewMask();
}

void KACLTest::cleanup()
{
}

void KACLTest::testAsString()
{
  check( "asString: ", m_acl.asString(), s_testACL );
}

void KACLTest::testSetACL()
{
  m_acl.setACL( s_testACL2 );
  check( "setACL: ", m_acl.asString().simplifyWhiteSpace().remove(" "), s_testACLEffective.simplifyWhiteSpace().remove(" ") );
}

void KACLTest::testGetOwnerPermissions()
{
  check( "Owner permissions: ", TQString::number( m_acl.ownerPermissions() ), "6" );
}

void KACLTest::testGetOwningGroupPermissions()
{
  check( "Owning group permissions: ", TQString::number( m_acl.owningGroupPermissions() ), "6" );
}

void KACLTest::testGetOthersPermissions()
{
  check( "Others permissions: ", TQString::number( m_acl.othersPermissions() ), "4" );
}

void KACLTest::testGetMaskPermissions()
{
  bool exists = false;
  unsigned short tqmask = m_acl.tqmaskPermissions( exists );
  check( "Mask permissions: ", TQString::number( tqmask ), "7" );
  check( "Mask permissions: ", exists, true );
}

void KACLTest::testGetAllUserPermissions()
{
  ACLUserPermissionsList list = m_acl.allUserPermissions();
  ACLUserPermissionsConstIterator it = list.begin();
  TQString name;
  unsigned short permissions;
  int count = 0;
  while ( it != list.end() ) {
    name = ( *it ).first;
    permissions = ( *it ).second;
    ++it;
    ++count;
  }
  check( "All users count: ", TQString::number( count ), "1" );
  check( "All users name: ", name, "bin" );
  check( "All users permissions: ", TQString::number( permissions ), "7" );
}

void KACLTest::testGetAllGroupsPermissions()
{
  ACLGroupPermissionsList list = m_acl.allGroupPermissions();
  ACLGroupPermissionsConstIterator it = list.begin();
  TQString name;
  unsigned short permissions;
  int count = 0;
  while ( it != list.end() ) {
    name = ( *it ).first;
    permissions = ( *it ).second;
    // setACL sorts them alphabetically ...
    if ( count == 0 ) {
      check( "All groups name: ", name, s_group1 );
      check( "All groups permissions: ", TQString::number( permissions ), "1" );
    } else if ( count == 1 ) {
      check( "All groups name: ", name, s_group2 );
      check( "All groups permissions: ", TQString::number( permissions ), "4" );
    }
    ++it;
    ++count;
  }
  check( "All users count: ", TQString::number( count ), "2" );
}

void KACLTest::testIsExtended()
{
  KACL dukeOfMonmoth( s_testACL );
  check( "isExtended on an extended one: ", dukeOfMonmoth.isExtended(), true );
  KACL earlOfUpnor( "user::r--\ngroup::r--\nother::r--\n" );
  check( "isExtended on a not extended one: ", earlOfUpnor.isExtended(), false );
}

void KACLTest::testOperators()
{
  KACL dukeOfMonmoth( s_testACL );
  KACL JamesScott( s_testACL );
  KACL earlOfUpnor( s_testACL2 );
  check( "operator== on different ones: ", dukeOfMonmoth == earlOfUpnor, false );
  check( "operator== on identical ones: ", dukeOfMonmoth == JamesScott, true );
  check( "operator!= on diffenrent ones: ", dukeOfMonmoth != earlOfUpnor, true );
  check( "operator!=: on identical ones: ", dukeOfMonmoth != JamesScott, false );
}

void KACLTest::testSettingBasic()
{
  KACL CharlesII( s_testACL );
  CharlesII.setOwnerPermissions( 7 ); // clearly
  CharlesII.setOwningGroupPermissions( 0 );
  CharlesII.setOthersPermissions( 0 );
  check( "setOwnerPermissions: ", TQString::number( CharlesII.ownerPermissions() ),"7" );
  check( "setOwningGroupPermissions: ", TQString::number( CharlesII.owningGroupPermissions() ),"0" );
  check( "setOthersPermissions: ", TQString::number( CharlesII.othersPermissions() ),"0" );
}

void KACLTest::testSettingExtended()
{
  KACL CharlesII( s_testACL );
  CharlesII.setMaskPermissions( 7 ); // clearly
  bool dummy = false;
  check( "setMaskPermissions: ", TQString::number( CharlesII.tqmaskPermissions( dummy ) ),"7" );

  const TQString expected( "user::rw-\nuser:root:rwx\nuser:bin:r--\ngroup::rw-\ntqmask::rwx\nother::r--\n" );
  
  ACLUserPermissionsList users;
  ACLUserPermissions user = qMakePair( TQString( "root" ), ( unsigned short )7 );
  users.append( user );
  user = qMakePair( TQString( "bin" ), ( unsigned short )4 );
  users.append( user );
  CharlesII.setAllUserPermissions( users );
  check( "setAllUserPermissions: ", CharlesII.asString(), expected );

  CharlesII.setACL( s_testACL ); // reset
  // it already has an entry for bin, let's change it
  CharlesII.setNamedUserPermissions( TQString("bin"), 4 );
  CharlesII.setNamedUserPermissions( TQString( "root" ), 7 );
  check( "setNamedUserPermissions: ", CharlesII.asString(), expected );

  // groups, all and named
  
  const TQString expected2 = TQString::tqfromLatin1( "user::rw-\nuser:bin:rwx\ngroup::rw-\ngroup:" ) + s_group1 +
                            TQString::tqfromLatin1( ":-wx\ngroup:" ) + s_group2 + TQString::tqfromLatin1(":r--\ntqmask::rwx\nother::r--\n" );
  CharlesII.setACL( s_testACL ); // reset
  ACLGroupPermissionsList groups;
  ACLGroupPermissions group = qMakePair( s_group1, ( unsigned short )3 );
  groups.append( group );
  group = qMakePair( s_group2, ( unsigned short )4 );
  groups.append( group );
  CharlesII.setAllGroupPermissions( groups );
  check( "setAllGroupPermissions: ", CharlesII.asString(), expected2 );

  CharlesII.setACL( s_testACL ); // reset
  CharlesII.setNamedGroupPermissions( s_group1, 3 );
  CharlesII.setNamedGroupPermissions( s_group2, 4 );
  check( "setNamedGroupPermissions: ", CharlesII.asString(), expected2 );
}

void KACLTest::testSettingErrorHandling()
{
  KACL foo( s_testACL );
  bool v = foo.setNamedGroupPermissions( s_group1, 7 ); // existing group
  check( "Existing group: ", v, true );
  v = foo.setNamedGroupPermissions( "jongel", 7 ); // non-existing group
  check( "Non-existing group: ", v, false );

  v = foo.setNamedUserPermissions( "bin", 7 ); // existing user
  check( "Existing user: ", v, true );
  v = foo.setNamedUserPermissions( "jongel", 7 ); // non-existing user
  check( "Non-existing user: ", v, false );
}

void KACLTest::testNewMask()
{
  KACL CharlesII( "user::rw-\ngroup::rw-\nother::rw\n" );
  bool dummy = false;
  CharlesII.tqmaskPermissions( dummy );
  check( "tqmask exists: ", dummy, false );

  CharlesII.setMaskPermissions( 6 );
  check( "new tqmask set: ", TQString::number( CharlesII.tqmaskPermissions( dummy ) ), "6" );
  check( "tqmask exists now: ", dummy, true );
}