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"
2023-05-12 14:50:31 +03:00
# include "common/checksumcalculator.h"
# include "common/checksumconsts.h"
2017-08-16 09:36:52 +03:00
# include "common/utility.h"
2015-05-15 16:34:17 +03:00
# include "filesystem.h"
2024-04-30 11:04:32 +03:00
# include "logger.h"
2015-05-15 16:34:17 +03:00
# 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 ;
2021-12-20 13:45:14 +03:00
ValidateChecksumHeader : : FailureReason _expectedFailureReason = ValidateChecksumHeader : : FailureReason : : Success ;
2015-05-15 16:34:17 +03:00
QByteArray _expected ;
2015-10-14 16:03:40 +03:00
QByteArray _expectedType ;
2023-02-03 20:38:52 +03:00
bool _successDown = false ;
bool _errorSeen = false ;
2015-05-15 16:34:17 +03:00
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 ;
}
2021-12-20 15:59:08 +03:00
void slotDownError ( const QString & errMsg , const QByteArray & calculatedChecksumType ,
2021-12-23 13:07:02 +03:00
const QByteArray & calculatedChecksum , const ValidateChecksumHeader : : FailureReason reason )
2021-07-27 15:02:12 +03:00
{
2021-12-20 15:59:08 +03:00
Q_UNUSED ( calculatedChecksumType ) ;
Q_UNUSED ( calculatedChecksum ) ;
2021-12-20 12:13:48 +03:00
QCOMPARE ( _expectedError , errMsg ) ;
2021-12-20 13:45:14 +03:00
QCOMPARE ( _expectedFailureReason , reason ) ;
2021-12-20 12:13:48 +03:00
_errorSeen = true ;
2015-05-15 16:34:17 +03:00
}
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 ;
}
2024-04-30 11:04:32 +03:00
private slots :
void initTestCase ( )
{
OCC : : Logger : : instance ( ) - > setLogFlush ( true ) ;
OCC : : Logger : : instance ( ) - > setLogDebug ( true ) ;
QStandardPaths : : setTestModeEnabled ( true ) ;
2015-05-15 16:34:17 +03:00
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
2023-10-27 21:32:40 +03:00
ChecksumCalculator checksumCalculator ( file , OCC : : checkSumMD5C ) ;
2023-05-12 14:50:31 +03:00
const auto sum = checksumCalculator . calculate ( ) ;
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
2023-10-27 21:32:40 +03:00
ChecksumCalculator checksumCalculator ( file , OCC : : checkSumSHA1C ) ;
2023-05-12 14:50:31 +03:00
const auto sum = checksumCalculator . calculate ( ) ;
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
2023-02-17 19:06:35 +03:00
connect ( vali , & ComputeChecksum : : done , this , & TestChecksumValidator : : slotUpValidated ) ;
2015-05-15 16:34:17 +03:00
2023-10-27 21:32:40 +03:00
ChecksumCalculator checksumCalculator ( _testfile , OCC : : checkSumAdlerC ) ;
2023-05-12 14:50:31 +03:00
_expected = checksumCalculator . calculate ( ) ;
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 ;
2023-02-17 19:06:35 +03:00
connect ( vali , & ComputeChecksum : : done , & loop , & QEventLoop : : 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 ) ;
2023-02-17 19:06:35 +03:00
connect ( vali , & ComputeChecksum : : done , this , & TestChecksumValidator : : slotUpValidated ) ;
2015-05-15 16:34:17 +03:00
2023-10-27 21:32:40 +03:00
ChecksumCalculator checksumCalculator ( _testfile , OCC : : checkSumMD5C ) ;
2023-05-12 14:50:31 +03:00
_expected = checksumCalculator . calculate ( ) ;
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 ;
2023-02-17 19:06:35 +03:00
connect ( vali , & ComputeChecksum : : done , & loop , & QEventLoop : : 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 ) ;
2023-02-17 19:06:35 +03:00
connect ( vali , & ComputeChecksum : : done , this , & TestChecksumValidator : : slotUpValidated ) ;
2015-05-15 16:34:17 +03:00
2023-10-27 21:32:40 +03:00
ChecksumCalculator checksumCalculator ( _testfile , OCC : : checkSumSHA1C ) ;
2023-05-12 14:50:31 +03:00
_expected = checksumCalculator . calculate ( ) ;
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 ;
2023-02-17 19:06:35 +03:00
connect ( vali , & ComputeChecksum : : done , & loop , & QEventLoop : : 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 ) ;
2021-06-03 17:10:26 +03:00
connect ( vali , & ValidateChecksumHeader : : validated , this , & TestChecksumValidator : : slotDownValidated ) ;
connect ( vali , & ValidateChecksumHeader : : validationFailed , this , & TestChecksumValidator : : slotDownError ) ;
2020-11-26 19:12:11 +03:00
2023-10-27 21:32:40 +03:00
ChecksumCalculator checksumCalculator ( _testfile , OCC : : checkSumAdlerC ) ;
2023-05-12 14:50:31 +03:00
_expected = checksumCalculator . calculate ( ) ;
2020-11-26 19:12:11 +03:00
QByteArray adler = checkSumAdlerC ;
adler . append ( " : " ) ;
adler . append ( _expected ) ;
_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
2021-08-02 14:49:24 +03:00
_expectedError = QStringLiteral ( " The downloaded file does not match the checksum, it will be resumed. \" 543345 \" != \" %1 \" " ) . arg ( QString : : fromUtf8 ( _expected ) ) ;
2021-12-20 13:45:14 +03:00
_expectedFailureReason = ValidateChecksumHeader : : FailureReason : : ChecksumMismatch ;
2015-05-15 16:34:17 +03:00
_errorSeen = false ;
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
2021-08-02 14:49:24 +03:00
_expectedError = QLatin1String ( " The checksum header contained an unknown checksum type \" Klaas32 \" " ) ;
2021-12-20 13:45:14 +03:00
_expectedFailureReason = ValidateChecksumHeader : : FailureReason : : ChecksumTypeUnknown ;
2015-05-15 16:34:17 +03:00
_errorSeen = false ;
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"