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"
using namespace OCC ;
2018-07-05 15:24:44 +03:00
using namespace OCC : : Utility ;
2015-05-15 16:34:17 +03:00
2015-11-23 14:09:25 +03:00
class TestChecksumValidator : public QObject
2015-05-15 16:34:17 +03:00
{
Q_OBJECT
private :
2018-07-05 15:24:44 +03:00
QTemporaryDir _root ;
2015-05-15 16:34:17 +03:00
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 ) {
2020-08-14 16:40:14 +03:00
QCOMPARE ( _expectedError , errMsg ) ;
2015-05-15 16:34:17 +03:00
_errorSeen = true ;
}
2018-07-05 15:24:44 +03:00
static QByteArray shellSum ( const QByteArray & cmd , const QString & file )
{
QProcess md5 ;
QStringList args ;
args . append ( file ) ;
md5 . start ( cmd , args ) ;
QByteArray sumShell ;
qDebug ( ) < < " File: " < < file ;
if ( md5 . waitForFinished ( ) ) {
sumShell = md5 . readAll ( ) ;
sumShell = sumShell . left ( sumShell . indexOf ( ' ' ) ) ;
}
return sumShell ;
}
2015-05-15 16:34:17 +03:00
private slots :
void initTestCase ( ) {
2018-07-05 15:24:44 +03:00
_testfile = _root . path ( ) + " /csFile " ;
2015-05-15 16:34:17 +03:00
Utility : : writeRandomFile ( _testfile ) ;
}
2018-07-05 15:24:44 +03:00
void testMd5Calc ( )
{
QString file ( _root . path ( ) + " /file_a.bin " ) ;
QVERIFY ( writeRandomFile ( file ) ) ;
QFileInfo fi ( file ) ;
QVERIFY ( fi . exists ( ) ) ;
2020-11-26 19:12:11 +03:00
QFile fileDevice ( file ) ;
fileDevice . open ( QIODevice : : ReadOnly ) ;
QByteArray sum = calcMd5 ( & fileDevice ) ;
fileDevice . close ( ) ;
2018-07-05 15:24:44 +03:00
QByteArray sSum = shellSum ( " md5sum " , file ) ;
if ( sSum . isEmpty ( ) )
QSKIP ( " Couldn't execute md5sum to calculate checksum, executable missing? " , SkipSingle ) ;
QVERIFY ( ! sum . isEmpty ( ) ) ;
QCOMPARE ( sSum , sum ) ;
}
void testSha1Calc ( )
{
QString file ( _root . path ( ) + " /file_b.bin " ) ;
writeRandomFile ( file ) ;
QFileInfo fi ( file ) ;
QVERIFY ( fi . exists ( ) ) ;
2020-11-26 19:12:11 +03:00
QFile fileDevice ( file ) ;
fileDevice . open ( QIODevice : : ReadOnly ) ;
QByteArray sum = calcSha1 ( & fileDevice ) ;
fileDevice . close ( ) ;
2018-07-05 15:24:44 +03:00
QByteArray sSum = shellSum ( " sha1sum " , file ) ;
if ( sSum . isEmpty ( ) )
QSKIP ( " Couldn't execute sha1sum to calculate checksum, executable missing? " , SkipSingle ) ;
QVERIFY ( ! sum . isEmpty ( ) ) ;
QCOMPARE ( sSum , sum ) ;
}
2015-05-15 16:34:17 +03:00
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
2020-11-26 19:12:11 +03:00
auto file = new QFile ( _testfile , vali ) ;
file - > open ( QIODevice : : ReadOnly ) ;
_expected = calcAdler32 ( file ) ;
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
2020-11-26 19:12:11 +03:00
auto file = new QFile ( _testfile , vali ) ;
file - > open ( QIODevice : : ReadOnly ) ;
_expected = calcMd5 ( file ) ;
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 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
2020-11-26 19:12:11 +03:00
auto file = new QFile ( _testfile , vali ) ;
file - > open ( QIODevice : : ReadOnly ) ;
_expected = calcSha1 ( file ) ;
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
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 ) ) ) ;
2020-11-26 19:12:11 +03:00
auto file = new QFile ( _testfile , vali ) ;
file - > open ( QIODevice : : ReadOnly ) ;
_expected = calcAdler32 ( file ) ;
QByteArray adler = checkSumAdlerC ;
adler . append ( " : " ) ;
adler . append ( _expected ) ;
file - > seek ( 0 ) ;
_successDown = false ;
2019-08-07 12:14:30 +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
2020-08-14 16:40:14 +03:00
_expectedError = QStringLiteral ( " The downloaded file does not match the checksum, it will be resumed. '543345' != '%1' " ) . arg ( QString : : fromUtf8 ( _expected ) ) ;
2015-05-15 16:34:17 +03:00
_errorSeen = false ;
2020-11-26 19:12:11 +03:00
file - > seek ( 0 ) ;
2019-08-07 12:14:30 +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 ;
2020-11-26 19:12:11 +03:00
file - > seek ( 0 ) ;
2019-08-07 12:14:30 +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"