File: webdavtest.cpp

package info (click to toggle)
kio 5.116.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,496 kB
  • sloc: cpp: 123,468; xml: 528; ansic: 466; ruby: 60; sh: 21; makefile: 13
file content (242 lines) | stat: -rw-r--r-- 9,064 bytes parent folder | download | duplicates (3)
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
/*
    SPDX-FileCopyrightText: 2019 Ben Gruber <bengruber250@gmail.com>

    SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/

// This test suite is based on those in ftptest.cpp and uses the same test files.

#include <kio/copyjob.h>
#include <kio/storedtransferjob.h>

#include <QBuffer>
#include <QProcess>
#include <QStandardPaths>
#include <QTest>

class WebDAVTest : public QObject
{
    Q_OBJECT
public:
    QUrl url(const QString &path) const
    {
        Q_ASSERT(path.startsWith(QChar('/')));
        QUrl newUrl = m_url;
        newUrl.setPath(path);
        newUrl.setPort(port);
        return newUrl;
    }

    QTemporaryDir m_remoteDir;
    QProcess m_daemonProc;
    QUrl m_url = QUrl("webdav://localhost");
    static const int port = 30000;

private:
    static void runDaemon(QProcess &proc, const QTemporaryDir &remoteDir)
    {
        QVERIFY(remoteDir.isValid());
        const QString exec = QStandardPaths::findExecutable("wsgidav");
        if (exec.isEmpty()) {
            QFAIL("Could not find 'wsgidav' executable in PATH");
        }
        proc.setProgram(exec);
        proc.setArguments(
            {QStringLiteral("--host=0.0.0.0"), QString("--port=%1").arg(port), QString("--root=%1").arg(remoteDir.path()), QStringLiteral("--auth=anonymous")});
        proc.setProcessChannelMode(QProcess::ForwardedErrorChannel);
        proc.start();
        QVERIFY(proc.waitForStarted());
        QCOMPARE(proc.state(), QProcess::Running);
        // Wait for the daemon to print its port. That tells us both where it's listening
        // and also that it is ready to move ahead with testing.
        QVERIFY(QTest::qWaitFor(
            [&]() -> bool {
                const QString out = proc.readAllStandardOutput();
                if (!out.isEmpty()) {
                    qDebug() << "STDERR:" << out;
                }
                if (!out.endsWith("Serving on http://0.0.0.0:30000 ...\n")) {
                    return false;
                }
                return true;
            },
            5000));
    }

private Q_SLOTS:
    void initTestCase()
    {
        // Force the http/webdav worker from our bindir as first choice. This specifically
        // works around the fact that the kioslave executable would load the worker from the system
        // as first choice instead of the one from the build dir.
        qputenv("QT_PLUGIN_PATH", QCoreApplication::applicationDirPath().toUtf8());

        // Start the webdav server.
        runDaemon(m_daemonProc, m_remoteDir);
        // Put a prefix on the stderr and stdout from the server.
        connect(&m_daemonProc, &QProcess::readyReadStandardError, this, [this] {
            qDebug() << "wsgidav STDERR:" << m_daemonProc.readAllStandardError();
        });
        connect(&m_daemonProc, &QProcess::readyReadStandardOutput, this, [this] {
            qDebug() << "wsgidav STDOUT:" << m_daemonProc.readAllStandardOutput();
        });

        QStandardPaths::setTestModeEnabled(true);
    }

    void cleanupTestCase()
    {
        m_daemonProc.terminate();
        m_daemonProc.kill();
        m_daemonProc.waitForFinished();
    }

    void init()
    {
        QCOMPARE(m_daemonProc.state(), QProcess::Running);
    }

    void testGet()
    {
        const QString path("/testGet");
        const auto url = this->url(path);
        const QString remotePath = m_remoteDir.path() + path;

        QByteArray data("testBasicGet");
        QFile file(remotePath);
        QVERIFY(file.open(QFile::WriteOnly));
        file.write(data);
        file.close();

        auto job = KIO::storedGet(url);
        job->setUiDelegate(nullptr);
        QVERIFY2(job->exec(), qUtf8Printable(job->errorString()));
        QCOMPARE(job->data(), data);
    }

    void testCopy()
    {
        const QString path("/testCopy");
        const auto url = this->url(path);
        const QString remotePath = m_remoteDir.path() + path;
        const QString partPath = remotePath + ".part";

        QFile::remove(remotePath);
        QFile::remove(partPath);

        const QString testCopy1 = QFINDTESTDATA("ftp/testCopy1");
        QVERIFY(!testCopy1.isEmpty());
        auto job = KIO::copy({QUrl::fromLocalFile(testCopy1)}, url, KIO::DefaultFlags);
        job->setUiDelegate(nullptr);
        QVERIFY2(job->exec(), qUtf8Printable(job->errorString()));
        QFile file(remotePath);
        QVERIFY(file.open(QFile::ReadOnly));
        QCOMPARE(file.readAll(), QByteArray("part1\n"));
    }

    void testCopyResume()
    {
        const QString path("/testCopy");
        const auto url = this->url(path);
        const QString remotePath = m_remoteDir.path() + path;
        const QString partPath = remotePath + ".part";

        QFile::remove(remotePath);
        QFile::remove(partPath);
        const QString testCopy1 = QFINDTESTDATA("ftp/testCopy1");
        QVERIFY(!testCopy1.isEmpty());
        QVERIFY(QFile::copy(testCopy1, partPath));

        const QString testCopy2 = QFINDTESTDATA("ftp/testCopy2");
        QVERIFY(!testCopy2.isEmpty());
        auto job = KIO::copy({QUrl::fromLocalFile(testCopy2)}, url, KIO::Resume);
        job->setUiDelegate(nullptr);
        QVERIFY2(job->exec(), qUtf8Printable(job->errorString()));
        QFile file(remotePath);
        QVERIFY(file.open(QFile::ReadOnly));
        QCOMPARE(file.readAll(), QByteArray("part1\npart2\n"));
    }

    void testOverwriteCopy()
    {
        const QString path("/testOverwriteCopy");
        const auto url = this->url(path);
        const QString remotePath = m_remoteDir.path() + path;

        qDebug() << (m_remoteDir.path() + path);

        // Create file
        const QString testCopy1 = QFINDTESTDATA("ftp/testCopy1");
        QVERIFY(!testCopy1.isEmpty());
        auto job1 = KIO::copy({QUrl::fromLocalFile(testCopy1)}, url, KIO::DefaultFlags);
        job1->setUiDelegate(nullptr);
        QVERIFY2(job1->exec(), qUtf8Printable(job1->errorString()));
        QFile file(remotePath);
        QVERIFY(file.open(QFile::ReadOnly));
        QCOMPARE(file.readAll(), QByteArray("part1\n"));
        file.close();

        // File already exists, we expect it to be overwritten.
        const QString testOverwriteCopy2 = QFINDTESTDATA("ftp/testOverwriteCopy2");
        QVERIFY(!testOverwriteCopy2.isEmpty());
        auto job2 = KIO::copy({QUrl::fromLocalFile(testOverwriteCopy2)}, url, KIO::Overwrite);
        job2->setUiDelegate(nullptr);
        QVERIFY2(job2->exec(), qUtf8Printable(job2->errorString()));
        QVERIFY(file.open(QFile::ReadOnly));
        QCOMPARE(file.readAll(), QByteArray("testOverwriteCopy2\n"));
    }

    void testOverwriteCopyWithoutFlagFromLocal()
    {
        const QString path("/testOverwriteCopyWithoutFlag");
        const auto url = this->url(path);

        qDebug() << (m_remoteDir.path() + path);
        const QString testOverwriteCopy1 = QFINDTESTDATA("ftp/testOverwriteCopy1");
        QVERIFY(!testOverwriteCopy1.isEmpty());
        QVERIFY(QFile::copy(testOverwriteCopy1, m_remoteDir.path() + path));

        // Without overwrite flag.
        const QString testOverwriteCopy2 = QFINDTESTDATA("ftp/testOverwriteCopy2");
        QVERIFY(!testOverwriteCopy2.isEmpty());
        auto job = KIO::copy({QUrl::fromLocalFile(testOverwriteCopy2)}, url, KIO::DefaultFlags);
        job->setUiDelegate(nullptr);
        QVERIFY2(!job->exec(), qUtf8Printable(job->errorString()));
        QCOMPARE(job->error(), KIO::ERR_FILE_ALREADY_EXIST);
        QFile file(m_remoteDir.path() + path);
        QVERIFY(file.open(QFile::ReadOnly));
        QCOMPARE(file.readAll(), QByteArray("testOverwriteCopy1\n")); // not 2!
    }

    void testOverwriteCopyWithoutFlagFromRemote()
    {
        // This exercises a different code path than testOverwriteCopyWithoutFlagFromLocal
        const QString path("/testOverwriteCopyWithoutFlagRemote");
        const QString dir_path("/dir");
        const auto url = this->url(path);
        const auto dir_url = this->url(dir_path);

        qDebug() << (m_remoteDir.path() + path);
        const auto testOverwriteCopy1 = QFINDTESTDATA("ftp/testOverwriteCopy1");
        QVERIFY(!testOverwriteCopy1.isEmpty());
        QVERIFY(QFile::copy(testOverwriteCopy1, m_remoteDir.path() + path));
        QVERIFY(QDir(m_remoteDir.path()).mkdir("dir"));

        // First copy should work.
        auto job = KIO::copy(url, dir_url, KIO::DefaultFlags);
        job->setUiDelegate(nullptr);
        QVERIFY2(job->exec(), qUtf8Printable(job->errorString()));

        // Without overwrite flag.
        auto job2 = KIO::copy(url, dir_url, KIO::DefaultFlags);
        job2->setUiDelegate(nullptr);
        QVERIFY2(!job2->exec(), qUtf8Printable(job2->errorString()));
        QCOMPARE(job2->error(), KIO::ERR_FILE_ALREADY_EXIST);
        QFile file(m_remoteDir.path() + path);
        QVERIFY(file.open(QFile::ReadOnly));
        QCOMPARE(file.readAll(), QByteArray("testOverwriteCopy1\n")); // not 2!
    }
};

QTEST_MAIN(WebDAVTest)
#include "webdavtest.moc"