File: testkioarchive.cpp

package info (click to toggle)
kio-extras 4%3A18.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,364 kB
  • sloc: cpp: 25,948; ansic: 4,443; perl: 1,056; xml: 460; python: 28; sh: 20; makefile: 6
file content (176 lines) | stat: -rw-r--r-- 6,069 bytes parent folder | download
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
/*  This file is part of the KDE project
    Copyright (C) 2007 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 "testkioarchive.h"
#include <qtest.h>
#include <kio/copyjob.h>
#include <kio/deletejob.h>
#include <ktar.h>
#include <QDebug>
#include <QStandardPaths>
#include <QFileInfo>
#include <QDir>

QTEST_MAIN(TestKioArchive)
static const char s_tarFileName[] = "karchivetest.tar";

static void writeTestFilesToArchive( KArchive* archive )
{
    bool ok;
    ok = archive->writeFile( "empty", "weis", "users", "", 0 );
    QVERIFY( ok );
    ok = archive->writeFile( "test1", "weis", "users", "Hallo", 5 );
    QVERIFY( ok );
    ok = archive->writeFile( "mydir/subfile", "dfaure", "users", "Bonjour", 7 );
    QVERIFY( ok );
    ok = archive->writeSymLink( "mydir/symlink", "subfile", "dfaure", "users" );
    QVERIFY( ok );
}

void TestKioArchive::initTestCase()
{
    QStandardPaths::setTestModeEnabled(true);

    // Make sure we start clean
    cleanupTestCase();

    QVERIFY(QDir().mkpath(tmpDir()));

    // Taken from KArchiveTest::testCreateTar
    KTar tar( s_tarFileName );
    bool ok = tar.open( QIODevice::WriteOnly );
    QVERIFY( ok );
    writeTestFilesToArchive( &tar );
    ok = tar.close();
    QVERIFY( ok );
    QFileInfo fileInfo( QFile::encodeName( s_tarFileName ) );
    QVERIFY( fileInfo.exists() );
}

void TestKioArchive::testListTar()
{
    m_listResult.clear();
    KIO::ListJob* job = KIO::listDir(tarUrl(), KIO::HideProgressInfo);
    connect(job, &KIO::ListJob::entries, this, &TestKioArchive::slotEntries);
    bool ok = job->exec();
    QVERIFY( ok );
    //qDebug() << "listDir done - entry count=" << m_listResult.count();
    QVERIFY( m_listResult.count() > 1 );

    //qDebug() << m_listResult;
    QCOMPARE(m_listResult.count( "." ), 1); // found it, and only once
    QCOMPARE(m_listResult.count("empty"), 1);
    QCOMPARE(m_listResult.count("test1"), 1);
    QCOMPARE(m_listResult.count("mydir"), 1);
    QCOMPARE(m_listResult.count("mydir/subfile"), 0); // not a recursive listing
    QCOMPARE(m_listResult.count("mydir/symlink"), 0);
}

void TestKioArchive::testListRecursive()
{
    m_listResult.clear();
    KIO::ListJob* job = KIO::listRecursive(tarUrl(), KIO::HideProgressInfo);
    connect(job, &KIO::ListJob::entries, this, &TestKioArchive::slotEntries);
    bool ok = job->exec();
    QVERIFY( ok );
    //qDebug() << "listDir done - entry count=" << m_listResult.count();
    QVERIFY( m_listResult.count() > 1 );

    //qDebug() << m_listResult;
    QCOMPARE(m_listResult.count( "." ), 1); // found it, and only once
    QCOMPARE(m_listResult.count("empty"), 1);
    QCOMPARE(m_listResult.count("test1"), 1);
    QCOMPARE(m_listResult.count("mydir"), 1);
    QCOMPARE(m_listResult.count("mydir/subfile"), 1);
    QCOMPARE(m_listResult.count("mydir/symlink"), 1);
}

QUrl TestKioArchive::tarUrl() const
{
    QUrl url;
    url.setScheme("tar");
    url.setPath(QDir::currentPath());
    url = url.adjusted(QUrl::StripTrailingSlash);
    url.setPath(url.path() + '/' + s_tarFileName);
    return url;
}

void TestKioArchive::slotEntries( KIO::Job*, const KIO::UDSEntryList& lst )
{
    for( KIO::UDSEntryList::ConstIterator it = lst.begin(); it != lst.end(); ++it ) {
        const KIO::UDSEntry& entry (*it);
        QString displayName = entry.stringValue( KIO::UDSEntry::UDS_NAME );
        m_listResult << displayName;
    }
}

QString TestKioArchive::tmpDir() const
{
    return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/test_kio_archive/";
}

void TestKioArchive::cleanupTestCase()
{
    QDir(tmpDir()).removeRecursively();
}

void TestKioArchive::copyFromTar(const QUrl &src, const QString& destPath)
{
    QUrl dest = QUrl::fromLocalFile(destPath);
    qDebug() << src << "->" << dest;
    // Check that src exists
    KIO::StatJob* statJob = KIO::stat(src, KIO::StatJob::SourceSide, 0, KIO::HideProgressInfo);
    bool ok = statJob->exec();
    QVERIFY( ok );

    KIO::Job* job = KIO::copyAs( src, dest, KIO::HideProgressInfo );
    qDebug() << "copyAs" << src << dest;
    ok = job->exec();
    QVERIFY( ok );
    QVERIFY( QFile::exists( destPath ) );
}

void TestKioArchive::testExtractFileFromTar()
{
    const QString destPath = tmpDir() + "fileFromTar_copied";
    QUrl u = tarUrl();
    u = u.adjusted(QUrl::StripTrailingSlash);
    u.setPath(u.path() + '/' + "mydir/subfile");
    copyFromTar(u, destPath);
    QVERIFY(QFileInfo(destPath).isFile());
    QVERIFY(QFileInfo(destPath).size() == 7);
}

void TestKioArchive::testExtractSymlinkFromTar()
{
    const QString destPath = tmpDir() + "symlinkFromTar_copied";
    QUrl u = tarUrl();
    u = u.adjusted(QUrl::StripTrailingSlash);
    u.setPath(u.path() + '/' + "mydir/symlink");
    copyFromTar(u, destPath);
    QVERIFY(QFileInfo(destPath).isFile());
    QEXPECT_FAIL("", "See #5601 -- on FTP we want to download the real file, not the symlink...", Continue);
    // See comment in 149903
    // Maybe this is something we can do depending on Class=:local and Class=:internet
    // (we already know if a protocol is local or remote).
    // So local->local should copy symlinks, while internet->local and internet->internet should
    // copy the actual file, I guess?
    // -> ### TODO
    QVERIFY(QFileInfo(destPath).isSymLink());
}