testchecksumvalidator.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. using namespace OCC::Utility;
  17. class TestChecksumValidator : public QObject
  18. {
  19. Q_OBJECT
  20. private:
  21. QTemporaryDir _root;
  22. QString _testfile;
  23. QString _expectedError;
  24. ValidateChecksumHeader::FailureReason _expectedFailureReason = ValidateChecksumHeader::FailureReason::Success;
  25. QByteArray _expected;
  26. QByteArray _expectedType;
  27. bool _successDown;
  28. bool _errorSeen;
  29. public slots:
  30. void slotUpValidated(const QByteArray& type, const QByteArray& checksum) {
  31. qDebug() << "Checksum: " << checksum;
  32. QVERIFY(_expected == checksum );
  33. QVERIFY(_expectedType == type );
  34. }
  35. void slotDownValidated() {
  36. _successDown = true;
  37. }
  38. void slotDownError(const QString &errMsg, const QByteArray &calculatedChecksumType,
  39. const QByteArray &calculatedChecksum, const ValidateChecksumHeader::FailureReason reason)
  40. {
  41. Q_UNUSED(calculatedChecksumType);
  42. Q_UNUSED(calculatedChecksum);
  43. QCOMPARE(_expectedError, errMsg);
  44. QCOMPARE(_expectedFailureReason, reason);
  45. _errorSeen = true;
  46. }
  47. static QByteArray shellSum( const QByteArray& cmd, const QString& file )
  48. {
  49. QProcess md5;
  50. QStringList args;
  51. args.append(file);
  52. md5.start(cmd, args);
  53. QByteArray sumShell;
  54. qDebug() << "File: "<< file;
  55. if( md5.waitForFinished() ) {
  56. sumShell = md5.readAll();
  57. sumShell = sumShell.left( sumShell.indexOf(' '));
  58. }
  59. return sumShell;
  60. }
  61. private slots:
  62. void initTestCase() {
  63. _testfile = _root.path()+"/csFile";
  64. Utility::writeRandomFile( _testfile);
  65. }
  66. void testMd5Calc()
  67. {
  68. QString file( _root.path() + "/file_a.bin");
  69. QVERIFY(writeRandomFile(file));
  70. QFileInfo fi(file);
  71. QVERIFY(fi.exists());
  72. QFile fileDevice(file);
  73. fileDevice.open(QIODevice::ReadOnly);
  74. QByteArray sum = calcMd5(&fileDevice);
  75. fileDevice.close();
  76. QByteArray sSum = shellSum("md5sum", file);
  77. if (sSum.isEmpty())
  78. QSKIP("Couldn't execute md5sum to calculate checksum, executable missing?", SkipSingle);
  79. QVERIFY(!sum.isEmpty());
  80. QCOMPARE(sSum, sum);
  81. }
  82. void testSha1Calc()
  83. {
  84. QString file( _root.path() + "/file_b.bin");
  85. writeRandomFile(file);
  86. QFileInfo fi(file);
  87. QVERIFY(fi.exists());
  88. QFile fileDevice(file);
  89. fileDevice.open(QIODevice::ReadOnly);
  90. QByteArray sum = calcSha1(&fileDevice);
  91. fileDevice.close();
  92. QByteArray sSum = shellSum("sha1sum", file);
  93. if (sSum.isEmpty())
  94. QSKIP("Couldn't execute sha1sum to calculate checksum, executable missing?", SkipSingle);
  95. QVERIFY(!sum.isEmpty());
  96. QCOMPARE(sSum, sum);
  97. }
  98. void testUploadChecksummingAdler() {
  99. #ifndef ZLIB_FOUND
  100. QSKIP("ZLIB not found.", SkipSingle);
  101. #else
  102. auto *vali = new ComputeChecksum(this);
  103. _expectedType = "Adler32";
  104. vali->setChecksumType(_expectedType);
  105. connect(vali, SIGNAL(done(QByteArray,QByteArray)), SLOT(slotUpValidated(QByteArray,QByteArray)));
  106. auto file = new QFile(_testfile, vali);
  107. file->open(QIODevice::ReadOnly);
  108. _expected = calcAdler32(file);
  109. qDebug() << "XX Expected Checksum: " << _expected;
  110. vali->start(_testfile);
  111. QEventLoop loop;
  112. connect(vali, SIGNAL(done(QByteArray,QByteArray)), &loop, SLOT(quit()), Qt::QueuedConnection);
  113. loop.exec();
  114. delete vali;
  115. #endif
  116. }
  117. void testUploadChecksummingMd5() {
  118. auto *vali = new ComputeChecksum(this);
  119. _expectedType = OCC::checkSumMD5C;
  120. vali->setChecksumType(_expectedType);
  121. connect(vali, SIGNAL(done(QByteArray,QByteArray)), this, SLOT(slotUpValidated(QByteArray,QByteArray)));
  122. auto file = new QFile(_testfile, vali);
  123. file->open(QIODevice::ReadOnly);
  124. _expected = calcMd5(file);
  125. vali->start(_testfile);
  126. QEventLoop loop;
  127. connect(vali, SIGNAL(done(QByteArray,QByteArray)), &loop, SLOT(quit()), Qt::QueuedConnection);
  128. loop.exec();
  129. delete vali;
  130. }
  131. void testUploadChecksummingSha1() {
  132. auto *vali = new ComputeChecksum(this);
  133. _expectedType = OCC::checkSumSHA1C;
  134. vali->setChecksumType(_expectedType);
  135. connect(vali, SIGNAL(done(QByteArray,QByteArray)), this, SLOT(slotUpValidated(QByteArray,QByteArray)));
  136. auto file = new QFile(_testfile, vali);
  137. file->open(QIODevice::ReadOnly);
  138. _expected = calcSha1(file);
  139. vali->start(_testfile);
  140. QEventLoop loop;
  141. connect(vali, SIGNAL(done(QByteArray,QByteArray)), &loop, SLOT(quit()), Qt::QueuedConnection);
  142. loop.exec();
  143. delete vali;
  144. }
  145. void testDownloadChecksummingAdler() {
  146. #ifndef ZLIB_FOUND
  147. QSKIP("ZLIB not found.", SkipSingle);
  148. #else
  149. auto *vali = new ValidateChecksumHeader(this);
  150. connect(vali, &ValidateChecksumHeader::validated, this, &TestChecksumValidator::slotDownValidated);
  151. connect(vali, &ValidateChecksumHeader::validationFailed, this, &TestChecksumValidator::slotDownError);
  152. auto file = new QFile(_testfile, vali);
  153. file->open(QIODevice::ReadOnly);
  154. _expected = calcAdler32(file);
  155. QByteArray adler = checkSumAdlerC;
  156. adler.append(":");
  157. adler.append(_expected);
  158. file->seek(0);
  159. _successDown = false;
  160. vali->start(_testfile, adler);
  161. QTRY_VERIFY(_successDown);
  162. _expectedError = QStringLiteral("The downloaded file does not match the checksum, it will be resumed. \"543345\" != \"%1\"").arg(QString::fromUtf8(_expected));
  163. _expectedFailureReason = ValidateChecksumHeader::FailureReason::ChecksumMismatch;
  164. _errorSeen = false;
  165. file->seek(0);
  166. vali->start(_testfile, "Adler32:543345");
  167. QTRY_VERIFY(_errorSeen);
  168. _expectedError = QLatin1String("The checksum header contained an unknown checksum type \"Klaas32\"");
  169. _expectedFailureReason = ValidateChecksumHeader::FailureReason::ChecksumTypeUnknown;
  170. _errorSeen = false;
  171. file->seek(0);
  172. vali->start(_testfile, "Klaas32:543345");
  173. QTRY_VERIFY(_errorSeen);
  174. delete vali;
  175. #endif
  176. }
  177. void cleanupTestCase() {
  178. }
  179. };
  180. QTEST_GUILESS_MAIN(TestChecksumValidator)
  181. #include "testchecksumvalidator.moc"