testchecksumvalidator.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * This software is in the public domain, furnished "as is", without technical
  3. * support, and with no warranty, express or implied, as to its usefulness for
  4. * any purpose.
  5. *
  6. */
  7. #include <QtTest>
  8. #include <QDir>
  9. #include <QString>
  10. #include "common/checksums.h"
  11. #include "networkjobs.h"
  12. #include "common/utility.h"
  13. #include "filesystem.h"
  14. #include "propagatorjobs.h"
  15. using namespace OCC;
  16. class TestChecksumValidator : public QObject
  17. {
  18. Q_OBJECT
  19. private:
  20. QString _root;
  21. QString _testfile;
  22. QString _expectedError;
  23. QByteArray _expected;
  24. QByteArray _expectedType;
  25. bool _successDown;
  26. bool _errorSeen;
  27. public slots:
  28. void slotUpValidated(const QByteArray& type, const QByteArray& checksum) {
  29. qDebug() << "Checksum: " << checksum;
  30. QVERIFY(_expected == checksum );
  31. QVERIFY(_expectedType == type );
  32. }
  33. void slotDownValidated() {
  34. _successDown = true;
  35. }
  36. void slotDownError( const QString& errMsg ) {
  37. QVERIFY(_expectedError == errMsg );
  38. _errorSeen = true;
  39. }
  40. private slots:
  41. void initTestCase() {
  42. _root = QDir::tempPath() + "/" + "test_" + QString::number(qrand());
  43. QDir rootDir(_root);
  44. rootDir.mkpath(_root );
  45. _testfile = _root+"/csFile";
  46. Utility::writeRandomFile( _testfile);
  47. }
  48. void testUploadChecksummingAdler() {
  49. #ifndef ZLIB_FOUND
  50. QSKIP("ZLIB not found.", SkipSingle);
  51. #else
  52. ComputeChecksum *vali = new ComputeChecksum(this);
  53. _expectedType = "Adler32";
  54. vali->setChecksumType(_expectedType);
  55. connect(vali, SIGNAL(done(QByteArray,QByteArray)), SLOT(slotUpValidated(QByteArray,QByteArray)));
  56. _expected = FileSystem::calcAdler32( _testfile );
  57. qDebug() << "XX Expected Checksum: " << _expected;
  58. vali->start(_testfile);
  59. QEventLoop loop;
  60. connect(vali, SIGNAL(done(QByteArray,QByteArray)), &loop, SLOT(quit()), Qt::QueuedConnection);
  61. loop.exec();
  62. delete vali;
  63. #endif
  64. }
  65. void testUploadChecksummingMd5() {
  66. ComputeChecksum *vali = new ComputeChecksum(this);
  67. _expectedType = OCC::checkSumMD5C;
  68. vali->setChecksumType(_expectedType);
  69. connect(vali, SIGNAL(done(QByteArray,QByteArray)), this, SLOT(slotUpValidated(QByteArray,QByteArray)));
  70. _expected = FileSystem::calcMd5( _testfile );
  71. vali->start(_testfile);
  72. QEventLoop loop;
  73. connect(vali, SIGNAL(done(QByteArray,QByteArray)), &loop, SLOT(quit()), Qt::QueuedConnection);
  74. loop.exec();
  75. delete vali;
  76. }
  77. void testUploadChecksummingSha1() {
  78. ComputeChecksum *vali = new ComputeChecksum(this);
  79. _expectedType = OCC::checkSumSHA1C;
  80. vali->setChecksumType(_expectedType);
  81. connect(vali, SIGNAL(done(QByteArray,QByteArray)), this, SLOT(slotUpValidated(QByteArray,QByteArray)));
  82. _expected = FileSystem::calcSha1( _testfile );
  83. vali->start(_testfile);
  84. QEventLoop loop;
  85. connect(vali, SIGNAL(done(QByteArray,QByteArray)), &loop, SLOT(quit()), Qt::QueuedConnection);
  86. loop.exec();
  87. delete vali;
  88. }
  89. void testDownloadChecksummingAdler() {
  90. #ifndef ZLIB_FOUND
  91. QSKIP("ZLIB not found.", SkipSingle);
  92. #else
  93. QByteArray adler = checkSumAdlerC;
  94. adler.append(":");
  95. adler.append(FileSystem::calcAdler32( _testfile ));
  96. _successDown = false;
  97. ValidateChecksumHeader *vali = new ValidateChecksumHeader(this);
  98. connect(vali, SIGNAL(validated(QByteArray,QByteArray)), this, SLOT(slotDownValidated()));
  99. connect(vali, SIGNAL(validationFailed(QString)), this, SLOT(slotDownError(QString)));
  100. vali->start(_testfile, adler);
  101. QTRY_VERIFY(_successDown);
  102. _expectedError = QLatin1String("The downloaded file does not match the checksum, it will be resumed.");
  103. _errorSeen = false;
  104. vali->start(_testfile, "Adler32:543345");
  105. QTRY_VERIFY(_errorSeen);
  106. _expectedError = QLatin1String("The checksum header contained an unknown checksum type 'Klaas32'");
  107. _errorSeen = false;
  108. vali->start(_testfile, "Klaas32:543345");
  109. QTRY_VERIFY(_errorSeen);
  110. delete vali;
  111. #endif
  112. }
  113. void cleanupTestCase() {
  114. }
  115. };
  116. QTEST_GUILESS_MAIN(TestChecksumValidator)
  117. #include "testchecksumvalidator.moc"