1 /* This file is part of the KDE project
2 Copyright (C) 2007 David Faure <faure@kde.org>
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 as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
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.
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.
20 #include "testkioarchive.h"
21 #include <qtest_kde.h>
22 #include <kio/copyjob.h>
23 #include <kio/deletejob.h>
24 #include <kio/netaccess.h>
26 #include <kstandarddirs.h>
29 QTEST_KDEMAIN(TestKioArchive
, NoGUI
)
30 static const char* s_tarFileName
= "karchivetest.tar";
32 static void writeTestFilesToArchive( KArchive
* archive
)
35 ok
= archive
->writeFile( "empty", "weis", "users", "", 0 );
37 ok
= archive
->writeFile( "test1", "weis", "users", "Hallo", 5 );
39 ok
= archive
->writeFile( "mydir/subfile", "dfaure", "users", "Bonjour", 7 );
41 ok
= archive
->writeSymLink( "mydir/symlink", "subfile", "dfaure", "users" );
45 void TestKioArchive::initTestCase()
47 // Make sure we start clean
50 // Taken from KArchiveTest::testCreateTar
51 KTar
tar( s_tarFileName
);
52 bool ok
= tar
.open( QIODevice::WriteOnly
);
54 writeTestFilesToArchive( &tar
);
57 QFileInfo
fileInfo( QFile::encodeName( s_tarFileName
) );
58 QVERIFY( fileInfo
.exists() );
61 void TestKioArchive::testListTar()
64 KIO::ListJob
* job
= KIO::listDir(tarUrl(), KIO::HideProgressInfo
);
65 connect( job
, SIGNAL( entries( KIO::Job
*, const KIO::UDSEntryList
& ) ),
66 SLOT( slotEntries( KIO::Job
*, const KIO::UDSEntryList
& ) ) );
67 bool ok
= KIO::NetAccess::synchronousRun( job
, 0 );
69 kDebug() << "listDir done - entry count=" << m_listResult
.count();
70 QVERIFY( m_listResult
.count() > 1 );
72 kDebug() << m_listResult
;
73 QCOMPARE(m_listResult
.count( "." ), 1); // found it, and only once
74 QCOMPARE(m_listResult
.count("empty"), 1);
75 QCOMPARE(m_listResult
.count("test1"), 1);
76 QCOMPARE(m_listResult
.count("mydir"), 1);
77 QCOMPARE(m_listResult
.count("mydir/subfile"), 0); // not a recursive listing
78 QCOMPARE(m_listResult
.count("mydir/symlink"), 0);
81 void TestKioArchive::testListRecursive()
84 KIO::ListJob
* job
= KIO::listRecursive(tarUrl(), KIO::HideProgressInfo
);
85 connect( job
, SIGNAL( entries( KIO::Job
*, const KIO::UDSEntryList
& ) ),
86 SLOT( slotEntries( KIO::Job
*, const KIO::UDSEntryList
& ) ) );
87 bool ok
= KIO::NetAccess::synchronousRun( job
, 0 );
89 kDebug() << "listDir done - entry count=" << m_listResult
.count();
90 QVERIFY( m_listResult
.count() > 1 );
92 kDebug() << m_listResult
;
93 QCOMPARE(m_listResult
.count( "." ), 1); // found it, and only once
94 QCOMPARE(m_listResult
.count("empty"), 1);
95 QCOMPARE(m_listResult
.count("test1"), 1);
96 QCOMPARE(m_listResult
.count("mydir"), 1);
97 QCOMPARE(m_listResult
.count("mydir/subfile"), 1);
98 QCOMPARE(m_listResult
.count("mydir/symlink"), 1);
101 KUrl
TestKioArchive::tarUrl() const
104 url
.setProtocol("tar");
105 url
.setPath(QDir::currentPath());
106 url
.addPath(s_tarFileName
);
110 void TestKioArchive::slotEntries( KIO::Job
*, const KIO::UDSEntryList
& lst
)
112 for( KIO::UDSEntryList::ConstIterator it
= lst
.begin(); it
!= lst
.end(); ++it
) {
113 const KIO::UDSEntry
& entry (*it
);
114 QString displayName
= entry
.stringValue( KIO::UDSEntry::UDS_NAME
);
115 m_listResult
<< displayName
;
119 QString
TestKioArchive::tmpDir() const
121 // Note that this goes into ~/.kde-unit-test (see qtest_kde.h)
122 // Use saveLocation if locateLocal doesn't work
123 return KStandardDirs::locateLocal("tmp", "test_kio_archive/");
126 void TestKioArchive::cleanupTestCase()
128 KIO::NetAccess::synchronousRun(KIO::del(tmpDir(), KIO::HideProgressInfo
), 0);
131 void TestKioArchive::copyFromTar(const KUrl
& src
, const QString
& destPath
)
134 // Check that src exists
135 KIO::StatJob
* statJob
= KIO::stat(src
, KIO::StatJob::SourceSide
, 0, KIO::HideProgressInfo
);
136 QVERIFY(KIO::NetAccess::synchronousRun(statJob
, 0));
138 KIO::Job
* job
= KIO::copyAs( src
, dest
, KIO::HideProgressInfo
);
139 bool ok
= KIO::NetAccess::synchronousRun( job
, 0 );
141 QVERIFY( QFile::exists( destPath
) );
144 void TestKioArchive::testExtractFileFromTar()
146 const QString destPath
= tmpDir() + "fileFromTar_copied";
148 u
.addPath("mydir/subfile");
149 copyFromTar(u
, destPath
);
150 QVERIFY(QFileInfo(destPath
).isFile());
151 QVERIFY(QFileInfo(destPath
).size() == 7);
154 void TestKioArchive::testExtractSymlinkFromTar()
156 const QString destPath
= tmpDir() + "symlinkFromTar_copied";
158 u
.addPath("mydir/symlink");
159 copyFromTar(u
, destPath
);
160 QVERIFY(QFileInfo(destPath
).isFile());
161 QEXPECT_FAIL("", "See #5601 -- on FTP we want to download the real file, not the symlink...", Continue
);
162 // See comment in 149903
163 // Maybe this is something we can do depending on Class=:local and Class=:internet
164 // (we already know if a protocol is local or remote).
165 // So local->local should copy symlinks, while internet->local and internet->internet should
166 // copy the actual file, I guess?
168 QVERIFY(QFileInfo(destPath
).isSymLink());