2015-05-15 16:34:17 +03:00
|
|
|
/*
|
|
|
|
* 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 <QDir>
|
|
|
|
#include <QString>
|
|
|
|
|
2017-09-01 19:11:43 +03:00
|
|
|
#include "common/checksums.h"
|
2015-05-15 16:34:17 +03:00
|
|
|
#include "networkjobs.h"
|
2017-08-16 09:36:52 +03:00
|
|
|
#include "common/utility.h"
|
2015-05-15 16:34:17 +03:00
|
|
|
#include "filesystem.h"
|
|
|
|
#include "propagatorjobs.h"
|
|
|
|
|
2015-06-25 15:36:22 +03:00
|
|
|
|
2015-05-15 16:34:17 +03:00
|
|
|
using namespace OCC;
|
|
|
|
|
2015-11-23 14:09:25 +03:00
|
|
|
class TestChecksumValidator : public QObject
|
2015-05-15 16:34:17 +03:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
private:
|
|
|
|
QString _root;
|
|
|
|
QString _testfile;
|
|
|
|
QString _expectedError;
|
|
|
|
QByteArray _expected;
|
2015-10-14 16:03:40 +03:00
|
|
|
QByteArray _expectedType;
|
2015-05-15 16:34:17 +03:00
|
|
|
bool _successDown;
|
|
|
|
bool _errorSeen;
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
|
2015-10-14 16:03:40 +03:00
|
|
|
void slotUpValidated(const QByteArray& type, const QByteArray& checksum) {
|
2015-05-21 16:51:48 +03:00
|
|
|
qDebug() << "Checksum: " << checksum;
|
|
|
|
QVERIFY(_expected == checksum );
|
2015-10-14 16:03:40 +03:00
|
|
|
QVERIFY(_expectedType == type );
|
2015-05-15 16:34:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void slotDownValidated() {
|
|
|
|
_successDown = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void slotDownError( const QString& errMsg ) {
|
|
|
|
QVERIFY(_expectedError == errMsg );
|
|
|
|
_errorSeen = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
|
|
|
|
void initTestCase() {
|
|
|
|
_root = QDir::tempPath() + "/" + "test_" + QString::number(qrand());
|
|
|
|
QDir rootDir(_root);
|
|
|
|
|
|
|
|
rootDir.mkpath(_root );
|
|
|
|
_testfile = _root+"/csFile";
|
|
|
|
Utility::writeRandomFile( _testfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
void testUploadChecksummingAdler() {
|
2017-02-07 21:31:55 +03:00
|
|
|
#ifndef ZLIB_FOUND
|
|
|
|
QSKIP("ZLIB not found.", SkipSingle);
|
|
|
|
#else
|
2020-05-18 21:54:23 +03:00
|
|
|
auto *vali = new ComputeChecksum(this);
|
2015-10-14 16:03:40 +03:00
|
|
|
_expectedType = "Adler32";
|
|
|
|
vali->setChecksumType(_expectedType);
|
2015-05-19 17:57:50 +03:00
|
|
|
|
2015-10-14 16:03:40 +03:00
|
|
|
connect(vali, SIGNAL(done(QByteArray,QByteArray)), SLOT(slotUpValidated(QByteArray,QByteArray)));
|
2015-05-15 16:34:17 +03:00
|
|
|
|
2015-10-14 16:03:40 +03:00
|
|
|
_expected = FileSystem::calcAdler32( _testfile );
|
2015-05-15 16:34:17 +03:00
|
|
|
qDebug() << "XX Expected Checksum: " << _expected;
|
2015-10-14 16:03:40 +03:00
|
|
|
vali->start(_testfile);
|
2015-05-15 16:34:17 +03:00
|
|
|
|
2015-06-25 13:45:09 +03:00
|
|
|
QEventLoop loop;
|
2015-10-14 16:03:40 +03:00
|
|
|
connect(vali, SIGNAL(done(QByteArray,QByteArray)), &loop, SLOT(quit()), Qt::QueuedConnection);
|
2015-06-25 13:45:09 +03:00
|
|
|
loop.exec();
|
2015-05-15 16:34:17 +03:00
|
|
|
|
2015-05-21 16:51:48 +03:00
|
|
|
delete vali;
|
2017-02-07 21:31:55 +03:00
|
|
|
#endif
|
2015-05-15 16:34:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void testUploadChecksummingMd5() {
|
|
|
|
|
2020-05-18 21:54:23 +03:00
|
|
|
auto *vali = new ComputeChecksum(this);
|
2015-10-14 16:03:40 +03:00
|
|
|
_expectedType = OCC::checkSumMD5C;
|
|
|
|
vali->setChecksumType(_expectedType);
|
|
|
|
connect(vali, SIGNAL(done(QByteArray,QByteArray)), this, SLOT(slotUpValidated(QByteArray,QByteArray)));
|
2015-05-15 16:34:17 +03:00
|
|
|
|
2015-10-14 16:03:40 +03:00
|
|
|
_expected = FileSystem::calcMd5( _testfile );
|
|
|
|
vali->start(_testfile);
|
2015-05-15 16:34:17 +03:00
|
|
|
|
2015-06-25 13:45:09 +03:00
|
|
|
QEventLoop loop;
|
2015-10-14 16:03:40 +03:00
|
|
|
connect(vali, SIGNAL(done(QByteArray,QByteArray)), &loop, SLOT(quit()), Qt::QueuedConnection);
|
2015-06-25 13:45:09 +03:00
|
|
|
loop.exec();
|
2015-05-15 16:34:17 +03:00
|
|
|
|
2015-05-21 16:51:48 +03:00
|
|
|
delete vali;
|
2015-05-15 16:34:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void testUploadChecksummingSha1() {
|
|
|
|
|
2020-05-18 21:54:23 +03:00
|
|
|
auto *vali = new ComputeChecksum(this);
|
2015-10-14 16:03:40 +03:00
|
|
|
_expectedType = OCC::checkSumSHA1C;
|
|
|
|
vali->setChecksumType(_expectedType);
|
|
|
|
connect(vali, SIGNAL(done(QByteArray,QByteArray)), this, SLOT(slotUpValidated(QByteArray,QByteArray)));
|
2015-05-15 16:34:17 +03:00
|
|
|
|
2015-10-14 16:03:40 +03:00
|
|
|
_expected = FileSystem::calcSha1( _testfile );
|
2015-05-15 16:34:17 +03:00
|
|
|
|
2015-10-14 16:03:40 +03:00
|
|
|
vali->start(_testfile);
|
2015-05-15 16:34:17 +03:00
|
|
|
|
2015-06-25 13:45:09 +03:00
|
|
|
QEventLoop loop;
|
2015-10-14 16:03:40 +03:00
|
|
|
connect(vali, SIGNAL(done(QByteArray,QByteArray)), &loop, SLOT(quit()), Qt::QueuedConnection);
|
2015-06-25 13:45:09 +03:00
|
|
|
loop.exec();
|
2015-05-15 16:34:17 +03:00
|
|
|
|
2015-05-21 16:51:48 +03:00
|
|
|
delete vali;
|
2015-05-15 16:34:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void testDownloadChecksummingAdler() {
|
2017-02-07 21:31:55 +03:00
|
|
|
#ifndef ZLIB_FOUND
|
|
|
|
QSKIP("ZLIB not found.", SkipSingle);
|
|
|
|
#else
|
2015-05-15 16:34:17 +03:00
|
|
|
QByteArray adler = checkSumAdlerC;
|
|
|
|
adler.append(":");
|
|
|
|
adler.append(FileSystem::calcAdler32( _testfile ));
|
|
|
|
_successDown = false;
|
|
|
|
|
2020-05-18 21:54:23 +03:00
|
|
|
auto *vali = new ValidateChecksumHeader(this);
|
2015-10-28 13:00:03 +03:00
|
|
|
connect(vali, SIGNAL(validated(QByteArray,QByteArray)), this, SLOT(slotDownValidated()));
|
2015-05-15 16:34:17 +03:00
|
|
|
connect(vali, SIGNAL(validationFailed(QString)), this, SLOT(slotDownError(QString)));
|
2015-10-14 16:03:40 +03:00
|
|
|
vali->start(_testfile, adler);
|
2015-05-15 16:34:17 +03:00
|
|
|
|
2015-06-25 13:45:09 +03:00
|
|
|
QTRY_VERIFY(_successDown);
|
2015-05-15 16:34:17 +03:00
|
|
|
|
2015-05-19 17:57:50 +03:00
|
|
|
_expectedError = QLatin1String("The downloaded file does not match the checksum, it will be resumed.");
|
2015-05-15 16:34:17 +03:00
|
|
|
_errorSeen = false;
|
2015-10-14 16:03:40 +03:00
|
|
|
vali->start(_testfile, "Adler32:543345");
|
2015-06-25 13:45:09 +03:00
|
|
|
QTRY_VERIFY(_errorSeen);
|
2015-05-15 16:34:17 +03:00
|
|
|
|
2015-10-14 16:03:40 +03:00
|
|
|
_expectedError = QLatin1String("The checksum header contained an unknown checksum type 'Klaas32'");
|
2015-05-15 16:34:17 +03:00
|
|
|
_errorSeen = false;
|
2015-10-14 16:03:40 +03:00
|
|
|
vali->start(_testfile, "Klaas32:543345");
|
2015-06-25 13:45:09 +03:00
|
|
|
QTRY_VERIFY(_errorSeen);
|
2015-05-15 16:34:17 +03:00
|
|
|
|
2015-05-21 16:51:48 +03:00
|
|
|
delete vali;
|
2017-02-07 21:31:55 +03:00
|
|
|
#endif
|
2015-05-15 16:34:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void cleanupTestCase() {
|
|
|
|
}
|
|
|
|
};
|
2016-03-30 18:58:15 +03:00
|
|
|
|
2016-05-11 02:17:16 +03:00
|
|
|
QTEST_GUILESS_MAIN(TestChecksumValidator)
|
|
|
|
|
2016-03-30 18:58:15 +03:00
|
|
|
#include "testchecksumvalidator.moc"
|