testfilesystem.cpp 2.1 KB

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