testfilesystem.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #ifndef MIRALL_TESTFILESYSTEM_H
  7. #define MIRALL_TESTFILESYSTEM_H
  8. #include <QtTest>
  9. #include <QDebug>
  10. #include "filesystem.h"
  11. #include "utility.h"
  12. using namespace OCC::Utility;
  13. using namespace OCC::FileSystem;
  14. class TestFileSystem : public QObject
  15. {
  16. Q_OBJECT
  17. QString _root;
  18. QByteArray shellSum( const QByteArray& cmd, const QString& file )
  19. {
  20. QProcess md5;
  21. QStringList args;
  22. args.append(file);
  23. md5.start(cmd, args);
  24. QByteArray sumShell;
  25. qDebug() << "File: "<< file;
  26. if( md5.waitForFinished() ) {
  27. sumShell = md5.readAll();
  28. sumShell = sumShell.left( sumShell.indexOf(' '));
  29. }
  30. return sumShell;
  31. }
  32. private slots:
  33. void initTestCase() {
  34. qsrand(QTime::currentTime().msec());
  35. QString subdir("test_"+QString::number(qrand()));
  36. _root = QDir::tempPath() + "/" + subdir;
  37. QDir dir("/tmp");
  38. dir.mkdir(subdir);
  39. qDebug() << "creating test directory " << _root;
  40. }
  41. void cleanupTestCase()
  42. {
  43. if( !_root.isEmpty() )
  44. system(QString("rm -rf "+_root).toUtf8());
  45. }
  46. void testMd5Calc()
  47. {
  48. QString file( _root+"/file_a.bin");
  49. writeRandomFile(file);
  50. QFileInfo fi(file);
  51. QVERIFY(fi.exists());
  52. QByteArray sum = calcMd5(file);
  53. QByteArray sSum = shellSum("/usr/bin/md5sum", file);
  54. qDebug() << "calculated" << sum << "versus md5sum:"<< sSum;
  55. QVERIFY(!sSum.isEmpty());
  56. QVERIFY(!sum.isEmpty());
  57. QVERIFY(sSum == sum );
  58. }
  59. void testSha1Calc()
  60. {
  61. QString file( _root+"/file_b.bin");
  62. writeRandomFile(file);
  63. QFileInfo fi(file);
  64. QVERIFY(fi.exists());
  65. QByteArray sum = calcSha1(file);
  66. QByteArray sSum = shellSum("/usr/bin/sha1sum", file);
  67. qDebug() << "calculated" << sum << "versus sha1sum:"<< sSum;
  68. QVERIFY(!sSum.isEmpty());
  69. QVERIFY(!sum.isEmpty());
  70. QVERIFY(sSum == sum );
  71. }
  72. };
  73. #endif