summaryrefslogtreecommitdiffstats
path: root/kio/tests/jobtest.cpp
blob: 3b7a6c6d3d96d02e7d02f8b14ae07a599099ee3f (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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/* This file is part of the KDE project
   Copyright (C) 2004-2006 David Faure <faure@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 "jobtest.h"

#include <config.h>

#include <kurl.h>
#include <kapplication.h>
#include <klargefile.h>
#include <kio/netaccess.h>
#include <kdebug.h>
#include <kcmdlineargs.h>
#include <kprotocolinfo.h>

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

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <utime.h>

// The code comes partly from tdebase/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;
}

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

    JobTest 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.
}

TQString JobTest::homeTmpDir() const
{
    return TQDir::homeDirPath() + "/.kde/jobtest/";
}

TQString JobTest::otherTmpDir() const
{
    // This one needs to be on another partition
    return "/tmp/jobtest/";
}

KURL JobTest::systemTmpDir() const
{
    return "system:/home/.kde/jobtest-system/";
}

TQString JobTest::realSystemPath() const
{
    return TQDir::homeDirPath() + "/.kde/jobtest-system/";
}

void JobTest::setup()
{
    // Start with a clean base dir
    cleanup();
    TQDir dir; // TT: why not a static method?
    bool ok = dir.mkdir( homeTmpDir() );
    if ( !ok )
        kdFatal() << "Couldn't create " << homeTmpDir() << endl;
    ok = dir.mkdir( otherTmpDir() );
    if ( !ok )
        kdFatal() << "Couldn't create " << otherTmpDir() << endl;
    ok = dir.mkdir( realSystemPath() );
    if ( !ok )
        kdFatal() << "Couldn't create " << realSystemPath() << endl;
}

void JobTest::runAll()
{
    get();
    copyFileToSamePartition();
    copyDirectoryToSamePartition();
    copyDirectoryToExistingDirectory();
    copyFileToOtherPartition();
    copyDirectoryToOtherPartition();
    listRecursive();
    moveFileToSamePartition();
    moveDirectoryToSamePartition();
    moveFileToOtherPartition();
    moveSymlinkToOtherPartition();
    moveDirectoryToOtherPartition();
    moveFileNoPermissions();
    moveDirectoryNoPermissions();

    copyFileToSystem();
}

void JobTest::cleanup()
{
    KIO::NetAccess::del( homeTmpDir(), 0 );
    KIO::NetAccess::del( otherTmpDir(), 0 );
    KIO::NetAccess::del( systemTmpDir(), 0 );
}

static void setTimeStamp( const TQString& path )
{
#ifdef Q_OS_UNIX
    // Put timestamp in the past so that we can check that the
    // copy actually preserves it.
    struct timeval tp;
    gettimeofday( &tp, 0 );
    struct utimbuf utbuf;
    utbuf.actime = tp.tv_sec - 30; // 30 seconds ago
    utbuf.modtime = tp.tv_sec - 60; // 60 second ago
    utime( TQFile::encodeName( path ), &utbuf );
    qDebug( "Time changed for %s", path.latin1() );
#endif
}

static void createTestFile( const TQString& path )
{
    TQFile f( path );
    if ( !f.open( IO_WriteOnly ) )
        kdFatal() << "Can't create " << path << endl;
    f.tqwriteBlock( "Hello world", 11 );
    f.close();
    setTimeStamp( path );
}

static void createTestSymlink( const TQString& path )
{
    // Create symlink if it doesn't exist yet
    KDE_struct_stat buf;
    if ( KDE_lstat( TQFile::encodeName( path ), &buf ) != 0 ) {
        bool ok = symlink( "/IDontExist", TQFile::encodeName( path ) ) == 0; // broken symlink
        if ( !ok )
            kdFatal() << "couldn't create symlink: " << strerror( errno ) << endl;
    }
}

static void createTestDirectory( const TQString& path )
{
    TQDir dir;
    bool ok = dir.mkdir( path );
    if ( !ok && !dir.exists() )
        kdFatal() << "couldn't create " << path << endl;
    createTestFile( path + "/testfile" );
    createTestSymlink( path + "/testlink" );
    setTimeStamp( path );
}

void JobTest::get()
{
    kdDebug() << k_funcinfo << endl;
    const TQString filePath = homeTmpDir() + "fileFromHome";
    createTestFile( filePath );
    KURL u; u.setPath( filePath );
    m_result = -1;
    KIO::StoredTransferJob* job = KIO::storedGet( u );
    connect( job, TQT_SIGNAL( result( KIO::Job* ) ),
            this, TQT_SLOT( slotGetResult( KIO::Job* ) ) );
    kapp->eventLoop()->enterLoop();
    assert( m_result == 0 ); // no error
    assert( m_data.size() == 11 );
    assert( TQCString( m_data ) == "Hello world" );
}

void JobTest::slotGetResult( KIO::Job* job )
{
    m_result = job->error();
    m_data = static_cast<KIO::StoredTransferJob *>(job)->data();
    kapp->eventLoop()->exitLoop();
}

////

void JobTest::copyLocalFile( const TQString& src, const TQString& dest )
{
    KURL u;
    u.setPath( src );
    KURL d;
    d.setPath( dest );

    // copy the file with file_copy
    bool ok = KIO::NetAccess::file_copy( u, d );
    assert( ok );
    assert( TQFile::exists( dest ) );
    assert( TQFile::exists( src ) ); // still there

    {
        // check that the timestamp is the same (#24443)
        // Note: this only works because of copy() in kio_file.
        // The datapump solution ignores mtime, the app has to call FileCopyJob::setModificationTime()
        TQFileInfo srcInfo( src );
        TQFileInfo destInfo( dest );
        assert( srcInfo.lastModified() == destInfo.lastModified() );
    }

    // cleanup and retry with KIO::copy()
    TQFile::remove( dest );
    ok = KIO::NetAccess::dircopy( u, d, 0 );
    assert( ok );
    assert( TQFile::exists( dest ) );
    assert( TQFile::exists( src ) ); // still there
    {
        // check that the timestamp is the same (#24443)
        TQFileInfo srcInfo( src );
        TQFileInfo destInfo( dest );
        assert( srcInfo.lastModified() == destInfo.lastModified() );
    }
}

void JobTest::copyLocalDirectory( const TQString& src, const TQString& _dest, int flags )
{
    assert( TQFileInfo( src ).isDir() );
    assert( TQFileInfo( src + "/testfile" ).isFile() );
    KURL u;
    u.setPath( src );
    TQString dest( _dest );
    KURL d;
    d.setPath( dest );
    if ( flags & AlreadyExists )
        assert( TQFile::exists( dest ) );
    else
        assert( !TQFile::exists( dest ) );

    bool ok = KIO::NetAccess::dircopy( u, d, 0 );
    assert( ok );

    if ( flags & AlreadyExists ) {
        dest += "/" + u.fileName();
        //kdDebug() << "Expecting dest=" << dest << endl;
    }

    assert( TQFile::exists( dest ) );
    assert( TQFileInfo( dest ).isDir() );
    assert( TQFileInfo( dest + "/testfile" ).isFile() );
    assert( TQFile::exists( src ) ); // still there
    {
        // check that the timestamp is the same (#24443)
        TQFileInfo srcInfo( src );
        TQFileInfo destInfo( dest );
        assert( srcInfo.lastModified() == destInfo.lastModified() );
    }
}

void JobTest::copyFileToSamePartition()
{
    kdDebug() << k_funcinfo << endl;
    const TQString filePath = homeTmpDir() + "fileFromHome";
    const TQString dest = homeTmpDir() + "fileFromHome_copied";
    createTestFile( filePath );
    copyLocalFile( filePath, dest );
}

void JobTest::copyDirectoryToSamePartition()
{
    kdDebug() << k_funcinfo << endl;
    const TQString src = homeTmpDir() + "dirFromHome";
    const TQString dest = homeTmpDir() + "dirFromHome_copied";
    createTestDirectory( src );
    copyLocalDirectory( src, dest );
}

void JobTest::copyDirectoryToExistingDirectory()
{
    kdDebug() << k_funcinfo << endl;
    // just the same as copyDirectoryToSamePartition, but it means that
    // this time dest exists.
    const TQString src = homeTmpDir() + "dirFromHome";
    const TQString dest = homeTmpDir() + "dirFromHome_copied";
    createTestDirectory( src );
    copyLocalDirectory( src, dest, AlreadyExists );
}

void JobTest::copyFileToOtherPartition()
{
    kdDebug() << k_funcinfo << endl;
    const TQString filePath = homeTmpDir() + "fileFromHome";
    const TQString dest = otherTmpDir() + "fileFromHome_copied";
    createTestFile( filePath );
    copyLocalFile( filePath, dest );
}

void JobTest::copyDirectoryToOtherPartition()
{
    kdDebug() << k_funcinfo << endl;
    const TQString src = homeTmpDir() + "dirFromHome";
    const TQString dest = otherTmpDir() + "dirFromHome_copied";
    // src is already created by copyDirectoryToSamePartition()
    // so this is just in case someone calls this method only
    if ( !TQFile::exists( src ) )
        createTestDirectory( src );
    copyLocalDirectory( src, dest );
}

void JobTest::moveLocalFile( const TQString& src, const TQString& dest )
{
    assert( TQFile::exists( src ) );
    KURL u;
    u.setPath( src );
    KURL d;
    d.setPath( dest );

    // move the file with file_move
    bool ok = KIO::NetAccess::file_move( u, d );
    assert( ok );
    assert( TQFile::exists( dest ) );
    assert( !TQFile::exists( src ) ); // not there anymore

    // move it back with KIO::move()
    ok = KIO::NetAccess::move( d, u, 0 );
    assert( ok );
    assert( !TQFile::exists( dest ) );
    assert( TQFile::exists( src ) ); // it's back
}

static void moveLocalSymlink( const TQString& src, const TQString& dest )
{
    KDE_struct_stat buf;
    assert ( KDE_lstat( TQFile::encodeName( src ), &buf ) == 0 );
    KURL u;
    u.setPath( src );
    KURL d;
    d.setPath( dest );

    // move the symlink with move, NOT with file_move
    bool ok = KIO::NetAccess::move( u, d );
    if ( !ok )
        kdWarning() << KIO::NetAccess::lastError() << endl;
    assert( ok );
    assert ( KDE_lstat( TQFile::encodeName( dest ), &buf ) == 0 );
    assert( !TQFile::exists( src ) ); // not there anymore

    // move it back with KIO::move()
    ok = KIO::NetAccess::move( d, u, 0 );
    assert( ok );
    assert ( KDE_lstat( TQFile::encodeName( dest ), &buf ) != 0 ); // doesn't exist anymore
    assert ( KDE_lstat( TQFile::encodeName( src ), &buf ) == 0 ); // it's back
}

void JobTest::moveLocalDirectory( const TQString& src, const TQString& dest )
{
    assert( TQFile::exists( src ) );
    assert( TQFileInfo( src ).isDir() );
    assert( TQFileInfo( src + "/testfile" ).isFile() );
    assert( TQFileInfo( src + "/testlink" ).isSymLink() );
    KURL u;
    u.setPath( src );
    KURL d;
    d.setPath( dest );

    bool ok = KIO::NetAccess::move( u, d, 0 );
    assert( ok );
    assert( TQFile::exists( dest ) );
    assert( TQFileInfo( dest ).isDir() );
    assert( TQFileInfo( dest + "/testfile" ).isFile() );
    assert( !TQFile::exists( src ) ); // not there anymore

    assert( TQFileInfo( dest + "/testlink" ).isSymLink() );
}

void JobTest::moveFileToSamePartition()
{
    kdDebug() << k_funcinfo << endl;
    const TQString filePath = homeTmpDir() + "fileFromHome";
    const TQString dest = homeTmpDir() + "fileFromHome_moved";
    createTestFile( filePath );
    moveLocalFile( filePath, dest );
}

void JobTest::moveDirectoryToSamePartition()
{
    kdDebug() << k_funcinfo << endl;
    const TQString src = homeTmpDir() + "dirFromHome";
    const TQString dest = homeTmpDir() + "dirFromHome_moved";
    createTestDirectory( src );
    moveLocalDirectory( src, dest );
}

void JobTest::moveFileToOtherPartition()
{
    kdDebug() << k_funcinfo << endl;
    const TQString filePath = homeTmpDir() + "fileFromHome";
    const TQString dest = otherTmpDir() + "fileFromHome_moved";
    createTestFile( filePath );
    moveLocalFile( filePath, dest );
}

void JobTest::moveSymlinkToOtherPartition()
{
    kdDebug() << k_funcinfo << endl;
    const TQString filePath = homeTmpDir() + "testlink";
    const TQString dest = otherTmpDir() + "testlink_moved";
    createTestSymlink( filePath );
    moveLocalSymlink( filePath, dest );
}

void JobTest::moveDirectoryToOtherPartition()
{
    kdDebug() << k_funcinfo << endl;
    const TQString src = homeTmpDir() + "dirFromHome";
    const TQString dest = otherTmpDir() + "dirFromHome_moved";
    createTestDirectory( src );
    moveLocalDirectory( src, dest );
}

void JobTest::moveFileNoPermissions()
{
    kdDebug() << k_funcinfo << endl;
    const TQString src = "/etc/passwd";
    const TQString dest = homeTmpDir() + "passwd";
    assert( TQFile::exists( src ) );
    assert( TQFileInfo( src ).isFile() );
    KURL u;
    u.setPath( src );
    KURL d;
    d.setPath( dest );

    KIO::CopyJob* job = KIO::move( u, d, 0 );
    job->setInteractive( false ); // no skip dialog, thanks
    TQMap<TQString, TQString> metaData;
    bool ok = KIO::NetAccess::synchronousRun( job, 0, 0, 0, &metaData );
    assert( !ok );
    assert( KIO::NetAccess::lastError() == KIO::ERR_ACCESS_DENIED );
    // OK this is fishy. Just like mv(1), KIO's behavior depends on whether
    // a direct rename(2) was used, or a full copy+del. In the first case
    // there is no destination file created, but in the second case the
    // destination file remains.
    // In fact we assume /home is a separate partition, in this test, so:
    assert( TQFile::exists( dest ) );
    assert( TQFile::exists( src ) );
}

void JobTest::moveDirectoryNoPermissions()
{
    kdDebug() << k_funcinfo << endl;
    const TQString src = "/etc/init.d";
    const TQString dest = homeTmpDir() + "init.d";
    assert( TQFile::exists( src ) );
    assert( TQFileInfo( src ).isDir() );
    KURL u;
    u.setPath( src );
    KURL d;
    d.setPath( dest );

    KIO::CopyJob* job = KIO::move( u, d, 0 );
    job->setInteractive( false ); // no skip dialog, thanks
    TQMap<TQString, TQString> metaData;
    bool ok = KIO::NetAccess::synchronousRun( job, 0, 0, 0, &metaData );
    assert( !ok );
    assert( KIO::NetAccess::lastError() == KIO::ERR_ACCESS_DENIED );
    assert( TQFile::exists( dest ) ); // see moveFileNoPermissions
    assert( TQFile::exists( src ) );
}

void JobTest::listRecursive()
{
    const TQString src = homeTmpDir();
    KURL u;
    u.setPath( src );
    KIO::ListJob* job = KIO::listRecursive( u );
    connect( job, TQT_SIGNAL( entries( KIO::Job*, const KIO::UDSEntryList& ) ),
             TQT_SLOT( slotEntries( KIO::Job*, const KIO::UDSEntryList& ) ) );
    bool ok = KIO::NetAccess::synchronousRun( job, 0 );
    assert( ok );
    m_names.sort();
    check( "listRecursive", m_names.join( "," ), ".,..,"
            "dirFromHome,dirFromHome/testfile,dirFromHome/testlink,dirFromHome_copied,"
            "dirFromHome_copied/dirFromHome,dirFromHome_copied/dirFromHome/testfile,dirFromHome_copied/dirFromHome/testlink,"
            "dirFromHome_copied/testfile,dirFromHome_copied/testlink,"
            "fileFromHome,fileFromHome_copied" );
}

void JobTest::slotEntries( KIO::Job*, const KIO::UDSEntryList& lst )
{
    for( KIO::UDSEntryList::ConstIterator it = lst.begin(); it != lst.end(); ++it ) {
        KIO::UDSEntry::ConstIterator it2 = (*it).begin();
        TQString displayName;
        KURL url;
        for( ; it2 != (*it).end(); it2++ ) {
            switch ((*it2).m_uds) {
            case KIO::UDS_NAME:
                displayName = (*it2).m_str;
                break;
            case KIO::UDS_URL:
                url = (*it2).m_str;
                break;
            }
        }
        m_names.append( displayName );
    }
}

void JobTest::copyFileToSystem()
{
    if ( !KProtocolInfo::isKnownProtocol( TQString::tqfromLatin1( "system" ) ) ) {
        kdDebug() << k_funcinfo << "no kio_system, skipping test" << endl;
        return;
    }

    // First test with support for UDS_LOCAL_PATH
    copyFileToSystem( true );

    TQString dest = realSystemPath() + "fileFromHome_copied";
    TQFile::remove( dest );

    // Then disable support for UDS_LOCAL_PATH, i.e. test what would
    // happen for ftp, smb, http etc.
    copyFileToSystem( false );
}

void JobTest::copyFileToSystem( bool resolve_local_urls )
{
    kdDebug() << k_funcinfo << resolve_local_urls << endl;
    extern KIO_EXPORT bool kio_resolve_local_urls;
    kio_resolve_local_urls = resolve_local_urls;

    const TQString src = homeTmpDir() + "fileFromHome";
    createTestFile( src );
    KURL u;
    u.setPath( src );
    KURL d = systemTmpDir();
    d.addPath( "fileFromHome_copied" );

    kdDebug() << "copying " << u << " to " << d << endl;

    // copy the file with file_copy
    KIO::FileCopyJob* job = KIO::file_copy( u, d );
    connect( job, TQT_SIGNAL(mimetype(KIO::Job*,const TQString&)),
             this, TQT_SLOT(slotMimetype(KIO::Job*,const TQString&)) );
    bool ok = KIO::NetAccess::synchronousRun( job, 0 );
    assert( ok );

    TQString dest = realSystemPath() + "fileFromHome_copied";

    assert( TQFile::exists( dest ) );
    assert( TQFile::exists( src ) ); // still there

    {
        // do NOT check that the timestamp is the same.
        // It can't work with file_copy when it uses the datapump,
        // unless we use setModificationTime in the app code.
    }

    // Check mimetype
    kdDebug() << m_mimetype << endl;
    // There's no mimemagic determination in kio_file in trinity. Fixing this for kde4...
    assert( m_mimetype == "application/octet-stream" );
    //assert( m_mimetype == "text/plain" );

    // cleanup and retry with KIO::copy()
    TQFile::remove( dest );
    ok = KIO::NetAccess::dircopy( u, d, 0 );
    assert( ok );
    assert( TQFile::exists( dest ) );
    assert( TQFile::exists( src ) ); // still there
    {
        // check that the timestamp is the same (#79937)
        TQFileInfo srcInfo( src );
        TQFileInfo destInfo( dest );
        assert( srcInfo.lastModified() == destInfo.lastModified() );
    }

    // restore normal behavior
    kio_resolve_local_urls = true;
}

void JobTest::slotMimetype(KIO::Job* job, const TQString& type)
{
    assert( job );
    m_mimetype = type;
}

#include "jobtest.moc"