testfilesystem.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 "common/utility.h"
  10. using namespace OCC::Utility;
  11. using namespace OCC::FileSystem;
  12. class TestFileSystem : public QObject
  13. {
  14. Q_OBJECT
  15. QTemporaryDir _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 testMd5Calc()
  32. {
  33. QString file( _root.path() + "/file_a.bin");
  34. QVERIFY(writeRandomFile(file));
  35. QFileInfo fi(file);
  36. QVERIFY(fi.exists());
  37. QByteArray sum = calcMd5(file);
  38. QByteArray sSum = shellSum("md5sum", file);
  39. if (sSum.isEmpty())
  40. QSKIP("Couldn't execute md5sum to calculate checksum, executable missing?", SkipSingle);
  41. QVERIFY(!sum.isEmpty());
  42. QCOMPARE(sSum, sum);
  43. }
  44. void testSha1Calc()
  45. {
  46. QString file( _root.path() + "/file_b.bin");
  47. writeRandomFile(file);
  48. QFileInfo fi(file);
  49. QVERIFY(fi.exists());
  50. QByteArray sum = calcSha1(file);
  51. QByteArray sSum = shellSum("sha1sum", file);
  52. if (sSum.isEmpty())
  53. QSKIP("Couldn't execute sha1sum to calculate checksum, executable missing?", SkipSingle);
  54. QVERIFY(!sum.isEmpty());
  55. QCOMPARE(sSum, sum);
  56. }
  57. };
  58. QTEST_APPLESS_MAIN(TestFileSystem)
  59. #include "testfilesystem.moc"