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>
|
|
|
|
|
2015-11-23 14:09:25 +03:00
|
|
|
#include "checksums.h"
|
2015-05-15 16:34:17 +03:00
|
|
|
#include "networkjobs.h"
|
|
|
|
#include "utility.h"
|
|
|
|
#include "filesystem.h"
|
|
|
|
#include "propagatorjobs.h"
|
|
|
|
|
2015-06-25 15:36:22 +03:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
|
|
|
|
// poor man QTRY_VERIFY when Qt5 is not available.
|
|
|
|
#define QTRY_VERIFY(Cond) QTest::qWait(1000); QVERIFY(Cond)
|
|
|
|
#endif
|
|
|
|
|
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() {
|
|
|
|
qDebug() << Q_FUNC_INFO;
|
|
|
|
_root = QDir::tempPath() + "/" + "test_" + QString::number(qrand());
|
|
|
|
QDir rootDir(_root);
|
|
|
|
|
|
|
|
rootDir.mkpath(_root );
|
|
|
|
_testfile = _root+"/csFile";
|
|
|
|
Utility::writeRandomFile( _testfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
void testUploadChecksummingAdler() {
|
|
|
|
|
2015-10-14 16:03:40 +03:00
|
|
|
ComputeChecksum *vali = new ComputeChecksum(this);
|
|
|
|
_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;
|
2015-05-15 16:34:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void testUploadChecksummingMd5() {
|
|
|
|
|
2015-10-14 16:03:40 +03:00
|
|
|
ComputeChecksum *vali = new ComputeChecksum(this);
|
|
|
|
_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() {
|
|
|
|
|
2015-10-14 16:03:40 +03:00
|
|
|
ComputeChecksum *vali = new ComputeChecksum(this);
|
|
|
|
_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() {
|
|
|
|
|
|
|
|
QByteArray adler = checkSumAdlerC;
|
|
|
|
adler.append(":");
|
|
|
|
adler.append(FileSystem::calcAdler32( _testfile ));
|
|
|
|
_successDown = false;
|
|
|
|
|
2015-10-14 16:03:40 +03:00
|
|
|
ValidateChecksumHeader *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;
|
2015-05-15 16:34:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void cleanupTestCase() {
|
|
|
|
}
|
|
|
|
};
|
2016-03-30 18:58:15 +03:00
|
|
|
|
|
|
|
QTEST_MAIN(TestChecksumValidator)
|
|
|
|
#include "testchecksumvalidator.moc"
|