File: testnextcloudpropagator.cpp

package info (click to toggle)
nextcloud-desktop 4.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 40,740 kB
  • sloc: cpp: 119,301; objc: 752; python: 606; ansic: 389; sh: 377; makefile: 44; javascript: 32; xml: 6
file content (338 lines) | stat: -rw-r--r-- 14,196 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
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
/*
 * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2016 ownCloud GmbH
 * SPDX-License-Identifier: CC0-1.0
 *
 * This software is in the public domain, furnished "as is", without technical
 * support, and with no warranty, express or implied, as to its usefulness for
 * any purpose.
 */

#include <QtTest>
#include <QDebug>

#include "accessmanager.h"
#include "creds/webflowcredentials.h"
#include "propagatedownload.h"
#include "owncloudpropagator_p.h"
#include "syncenginetestutils.h"

using namespace Qt::StringLiterals;

#ifdef HAVE_QHTTPSERVER
#include <QHttpServer>
#include <QTcpServer>
#endif

using namespace OCC;
namespace OCC {
QString OWNCLOUDSYNC_EXPORT createDownloadTmpFileName(const QString &previous);
}

class TestNextcloudPropagator : public QObject
{
    Q_OBJECT

private slots:
    void initTestCase()
    {
        OCC::Logger::instance()->setLogFlush(true);
        OCC::Logger::instance()->setLogDebug(true);

        QStandardPaths::setTestModeEnabled(true);
    }

    void testUpdateErrorFromSession()
    {
        //OwncloudPropagator propagator(nullptr, QLatin1String("test1"), QLatin1String("test2"), new ProgressDatabase);
        QVERIFY( true );
    }

    void testTmpDownloadFileNameGeneration()
    {
        QString fn;
        // without dir
        for (int i = 1; i <= 1000; i++) {
            fn+="F";
            QString tmpFileName = createDownloadTmpFileName(fn);
            if (tmpFileName.contains('/')) {
                tmpFileName = tmpFileName.mid(tmpFileName.lastIndexOf('/')+1);
            }
            QVERIFY( tmpFileName.length() > 0);
            QVERIFY( tmpFileName.length() <= 254);
        }
        // with absolute dir
        fn = "/Users/guruz/ownCloud/rocks/GPL";
        for (int i = 1; i < 1000; i++) {
            fn+="F";
            QString tmpFileName = createDownloadTmpFileName(fn);
            if (tmpFileName.contains('/')) {
                tmpFileName = tmpFileName.mid(tmpFileName.lastIndexOf('/')+1);
            }
            QVERIFY( tmpFileName.length() > 0);
            QVERIFY( tmpFileName.length() <= 254);
        }
        // with relative dir
        fn = "rocks/GPL";
        for (int i = 1; i < 1000; i++) {
            fn+="F";
            QString tmpFileName = createDownloadTmpFileName(fn);
            if (tmpFileName.contains('/')) {
                tmpFileName = tmpFileName.mid(tmpFileName.lastIndexOf('/')+1);
            }
            QVERIFY( tmpFileName.length() > 0);
            QVERIFY( tmpFileName.length() <= 254);
        }
    }

    void testParseEtag()
    {
        using Test = QPair<const char*, const char*>;
        QList<Test> tests;
        tests.append(Test("\"abcd\"", "abcd"));
        tests.append(Test("\"\"", ""));
        tests.append(Test("\"fii\"-gzip", "fii"));
        tests.append(Test("W/\"foo\"", "foo"));

        for (const auto &test : tests) {
            QCOMPARE(parseEtag(test.first), QByteArray(test.second));
        }
    }

    void testParseException()
    {
        QNetworkRequest request;
        request.setUrl(QStringLiteral("http://cloud.example.de/"));
        const auto body = QByteArrayLiteral(
            "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
            "<d:error xmlns:d=\"DAV:\" xmlns:s=\"http://sabredav.org/ns\">\n"
            "<s:exception>Sabre\\Exception\\UnsupportedMediaType</s:exception>\n"
            "<s:message>Virus detected!</s:message>\n"
            "</d:error>");
        const auto reply = new FakeErrorReply(QNetworkAccessManager::PutOperation,
                                              request,
                                              this,
                                              415, body);
        const auto exceptionParsed = OCC::getExceptionFromReply(reply);
        // verify parsing succeeded
        QVERIFY(!exceptionParsed.first.isEmpty());
        QVERIFY(!exceptionParsed.second.isEmpty());
        // verify buffer is not changed
        QCOMPARE(reply->readAll().size(), body.size());
    }

#ifdef HAVE_QHTTPSERVER
    void testGETFileJobDecompressionThreshold()
    {
        // generate 50 MiB of easily compressable data, and compress it with
        // the highest possible level to achieve a big enough decompression
        // ratio for Qt's decompression check to kick in
        const QByteArray decompressedContent(50 * 1024 * 1024, 'A');
        // skip the first 4 bytes of compression header to only serve the pure
        // deflate stream
        const auto compressedContent = qCompress(decompressedContent, 9).mid(4);
        // ensure the ratio is greater than 40:1 (default for gzip/deflate as
        // per Qt docs)
        const auto ratio = double(decompressedContent.size()) / (compressedContent.size());
        qInfo() << "Compression ratio is" << ratio;
        QCOMPARE_GT(ratio, 40.0);

        // spin up a temprary test-specific HTTP server that serves the
        // compressed data
        // with a fake QNAM and overrides we would skip the decompression checks
        // done internally in QNetworkReply
        QHttpServer httpServer;
        httpServer.route("/remote.php/dav/files/admin/someTestFile", [&compressedContent](QHttpServerResponder &responder) -> void {
            QHttpHeaders headers;
            headers.append(QHttpHeaders::WellKnownHeader::ContentEncoding, "deflate"_ba);
            headers.append("OC-ETag"_ba, "0123456789abcdef"_ba);
            headers.append("ETag"_ba, "0123456789abcdef"_ba);
            responder.write(compressedContent, headers);
        });

        QTcpServer tcpServer;
        QVERIFY(tcpServer.listen(QHostAddress::LocalHost));
        QVERIFY(httpServer.bind(&tcpServer));
        const QString baseUrl = "http://%1:%2"_L1.arg(tcpServer.serverAddress().toString(), QString::number(tcpServer.serverPort()));
        qInfo() << "Listening on" << baseUrl;

        auto account = OCC::Account::create();
        account->setCredentials(new FakeCredentials{new OCC::AccessManager{this}});
        account->setUrl(baseUrl);

        {
            qInfo() << "Test: with default decompression threshold (20 MiB)";
            QBuffer receivedContent;
            receivedContent.open(QIODevice::ReadWrite);

            auto job = GETFileJob(account, "/someTestFile"_L1, &receivedContent, {}, {}, 0);
            QSignalSpy spy(&job, &GETFileJob::finishedSignal);

            job.start();
            spy.wait(1000);

            QVERIFY(spy.isEmpty()); // the request failed, so the finishedSignal never was emitted
            QVERIFY(job.reply());
            QCOMPARE(job.reply()->error(), QNetworkReply::UnknownContentError);
            // the download was aborted below the configured threshold
            QCOMPARE_LT(receivedContent.size(), job.reply()->request().decompressedSafetyCheckThreshold());
        }

        {
            qInfo() << "Test with decompression threshold set from expected file size (50 MiB + default 20MiB)";
            QBuffer receivedContent;
            receivedContent.open(QIODevice::WriteOnly);

            auto job = GETFileJob(account, "/someTestFile"_L1, &receivedContent, {}, {}, 0);
            QSignalSpy spy(&job, &GETFileJob::finishedSignal);

            job.setDecompressionThresholdBase(decompressedContent.size()); // expect a response of 50MiB
            job.start();
            spy.wait(1000);

            QVERIFY(!spy.isEmpty());
            QVERIFY(job.reply());
            QCOMPARE(job.reply()->error(), QNetworkReply::NoError);
            // the download succeeded
            QCOMPARE(receivedContent.size(), decompressedContent.size());
        }
    }

    void testDirectUrlCredentials()
    {
        // for this one we need two HTTP servers:
        // - one that acts as a host for direct downloads
        // - and another one that's our pretend-Nextcloud server
        //
        // in any case the requests made to the direct download server should
        // never contain the `Authorization` header

        QHttpServer directDownloadServer;
        directDownloadServer.route("/data/directData", [](const QHttpServerRequest &request, QHttpServerResponder &responder) -> void {
            QVERIFY(!request.headers().contains(QHttpHeaders::WellKnownHeader::Authorization));

            QHttpHeaders headers;
            headers.append(QHttpHeaders::WellKnownHeader::ContentType, "text/plain"_ba);
            responder.write("OK directData"_ba, headers);
        });
        directDownloadServer.route("/data/redirectToOtherUrl", [](const QHttpServerRequest &request, QHttpServerResponder &responder) -> void {
            QVERIFY(!request.headers().contains(QHttpHeaders::WellKnownHeader::Authorization));

            QHttpHeaders headers;
            headers.append(QHttpHeaders::WellKnownHeader::Location, "/someOtherPath/redirectTarget"_ba);
            responder.write(""_ba, headers, QHttpServerResponder::StatusCode::Found);
        });
        directDownloadServer.route("/someOtherPath/redirectTarget", [](const QHttpServerRequest &request, QHttpServerResponder &responder) -> void {
            QVERIFY(!request.headers().contains(QHttpHeaders::WellKnownHeader::Authorization));

            QHttpHeaders headers;
            headers.append(QHttpHeaders::WellKnownHeader::ContentType, "text/plain"_ba);
            headers.append("OC-ETag"_ba, "0123456789abcdef"_ba);
            headers.append("ETag"_ba, "0123456789abcdef"_ba);
            responder.write("OK redirectTarget"_ba, headers);
        });

        QTcpServer directDownloadTcpServer;
        QVERIFY(directDownloadTcpServer.listen(QHostAddress::LocalHost));
        QVERIFY(directDownloadServer.bind(&directDownloadTcpServer));
        const QString directDownloadBaseUrl = "http://%1:%2"_L1.arg(directDownloadTcpServer.serverAddress().toString(), QString::number(directDownloadTcpServer.serverPort()));
        qInfo() << "Listening on" << directDownloadBaseUrl;

        QHttpServer nextcloudServer;
        nextcloudServer.route("/remote.php/dav/files/propagatortest/someFile", [](const QHttpServerRequest &request, QHttpServerResponder &responder) -> void {
            QVERIFY(request.headers().contains(QHttpHeaders::WellKnownHeader::Authorization));

            QHttpHeaders headers;
            headers.append(QHttpHeaders::WellKnownHeader::ContentType, "text/plain"_ba);
            headers.append("OC-ETag"_ba, "0123456789abcdef"_ba);
            headers.append("ETag"_ba, "0123456789abcdef"_ba);
            responder.write("OK someFile"_ba, headers);
        });
        nextcloudServer.route("/remote.php/dav/files/propagatortest/redirectedFile", [&directDownloadBaseUrl](const QHttpServerRequest &request, QHttpServerResponder &responder) -> void {
            QVERIFY(request.headers().contains(QHttpHeaders::WellKnownHeader::Authorization));

            QHttpHeaders headers;
            headers.append(QHttpHeaders::WellKnownHeader::ContentType, "text/plain"_ba);
            headers.append(QHttpHeaders::WellKnownHeader::Location, directDownloadBaseUrl + "/someOtherPath/redirectTarget"_L1);
            responder.write(""_ba, headers, QHttpServerResponder::StatusCode::Found);
        });

        QTcpServer nextcloudTcpServer;
        QVERIFY(nextcloudTcpServer.listen(QHostAddress::LocalHost));
        QVERIFY(nextcloudServer.bind(&nextcloudTcpServer));
        const QString nextcloudBaseUrl = "http://%1:%2"_L1.arg(nextcloudTcpServer.serverAddress().toString(), QString::number(nextcloudTcpServer.serverPort()));
        qInfo() << "Listening on" << nextcloudBaseUrl;


        auto account = OCC::Account::create();
        // using WebFlowCredentials here to to make sure it works as expected for real
        account->setCredentials(new WebFlowCredentials(u"propagatortest"_s, u"invalid"_s));
        account->setDavUser(u"propagatortest"_s);
        account->setUrl(nextcloudBaseUrl);


        {
            qInfo() << "Test: direct URL";
            QBuffer receivedContent;
            receivedContent.open(QIODevice::ReadWrite);

            auto job = GETFileJob(account, QUrl{directDownloadBaseUrl + "/data/directData"_L1}, &receivedContent, {}, {}, 0);
            QSignalSpy spy(&job, &GETFileJob::finishedSignal);

            job.start();
            spy.wait(1000);

            // The temporary web servers verify the received headers
            QCOMPARE(receivedContent.data(), "OK directData"_ba);
        }

        {
            qInfo() << "Test: direct URL with redirect";
            QBuffer receivedContent;
            receivedContent.open(QIODevice::ReadWrite);

            auto job = GETFileJob(account, QUrl{directDownloadBaseUrl + "/data/redirectToOtherUrl"_L1}, &receivedContent, {}, {}, 0);
            QSignalSpy spy(&job, &GETFileJob::finishedSignal);

            job.start();
            spy.wait(1000);

            // The temporary web servers verify the received headers
            QCOMPARE(receivedContent.data(), "OK redirectTarget"_ba);
        }

        {
            qInfo() << "Test: standard dav path";
            QBuffer receivedContent;
            receivedContent.open(QIODevice::ReadWrite);

            auto job = GETFileJob(account, "/someFile"_L1, &receivedContent, {}, {}, 0);
            QSignalSpy spy(&job, &GETFileJob::finishedSignal);

            job.start();
            spy.wait(1000);

            // The temporary web servers verify the received headers
            QCOMPARE(receivedContent.data(), "OK someFile"_ba);
        }

        {
            qInfo() << "Test: standard dav path with a redirect to a different origin";
            QBuffer receivedContent;
            receivedContent.open(QIODevice::ReadWrite);

            auto job = GETFileJob(account, "/redirectedFile"_L1, &receivedContent, {}, {}, 0);
            QSignalSpy spy(&job, &GETFileJob::finishedSignal);

            job.start();
            spy.wait(1000);

            // The temporary web servers verify the received headers
            QCOMPARE(receivedContent.data(), "OK redirectTarget"_ba);
        }
    }
#endif
};

QTEST_GUILESS_MAIN(TestNextcloudPropagator)
#include "testnextcloudpropagator.moc"