Move the checksum related routine from FileSystem to checksum, where they belong
authorOlivier Goffart <ogoffart@woboq.com>
Thu, 5 Jul 2018 12:24:44 +0000 (14:24 +0200)
committerKevin Ottens <kevin.ottens@nextcloud.com>
Tue, 15 Dec 2020 09:58:05 +0000 (10:58 +0100)
src/common/checksums.cpp
src/common/checksums.h
src/common/filesystembase.cpp
src/common/filesystembase.h
test/CMakeLists.txt
test/testchecksumvalidator.cpp
test/testfilesystem.cpp [deleted file]

index 3099c24fbfac2db1ee36ccac706e7dd9eafc7b10..a2bfdabc798d3492c41d5df6e12877384d50bf19 100644 (file)
 
 #include <QLoggingCategory>
 #include <qtconcurrentrun.h>
+#include <QCryptographicHash>
+
+#ifdef ZLIB_FOUND
+#include <zlib.h>
+#endif
 
 /** \file checksums.cpp
  *
@@ -80,6 +85,53 @@ namespace OCC {
 
 Q_LOGGING_CATEGORY(lcChecksums, "nextcloud.sync.checksums", QtInfoMsg)
 
+#define BUFSIZE qint64(500 * 1024) // 500 KiB
+
+static QByteArray calcCryptoHash( const QString& filename, QCryptographicHash::Algorithm algo )
+{
+     QFile file(filename);
+     QByteArray arr;
+     QCryptographicHash crypto( algo );
+
+     if (file.open(QIODevice::ReadOnly)) {
+         if (crypto.addData(&file)) {
+             arr = crypto.result().toHex();
+         }
+     }
+     return arr;
+ }
+
+QByteArray calcMd5(const QString &filename)
+{
+    return calcCryptoHash(filename, QCryptographicHash::Md5);
+}
+
+QByteArray calcSha1(const QString &filename)
+{
+    return calcCryptoHash(filename, QCryptographicHash::Sha1);
+}
+
+#ifdef ZLIB_FOUND
+QByteArray calcAdler32(const QString &filename)
+{
+    QFile file(filename);
+    const qint64 bufSize = qMin(BUFSIZE, file.size() + 1);
+    QByteArray buf(bufSize, Qt::Uninitialized);
+
+    unsigned int adler = adler32(0L, Z_NULL, 0);
+    if (file.open(QIODevice::ReadOnly)) {
+        qint64 size;
+        while (!file.atEnd()) {
+            size = file.read(buf.data(), bufSize);
+            if (size > 0)
+                adler = adler32(adler, (const Bytef *)buf.data(), size);
+        }
+    }
+
+    return QByteArray::number(adler, 16);
+}
+#endif
+
 QByteArray makeChecksumHeader(const QByteArray &checksumType, const QByteArray &checksum)
 {
     if (checksumType.isEmpty() || checksum.isEmpty())
@@ -188,13 +240,13 @@ QByteArray ComputeChecksum::computeNow(const QString &filePath, const QByteArray
     }
 
     if (checksumType == checkSumMD5C) {
-        return FileSystem::calcMd5(filePath);
+        return calcMd5(filePath);
     } else if (checksumType == checkSumSHA1C) {
-        return FileSystem::calcSha1(filePath);
+        return calcSha1(filePath);
     }
 #ifdef ZLIB_FOUND
     else if (checksumType == checkSumAdlerC) {
-        return FileSystem::calcAdler32(filePath);
+        return calcAdler32(filePath);
     }
 #endif
     // for an unknown checksum or no checksum, we're done right now
index 29a5bcf12a2e9d4e0234f6f2c720d4adc40561cc..765e4659b6769bcdff95b0baffc8e076816e3677 100644 (file)
@@ -19,6 +19,7 @@
 #pragma once
 
 #include "ocsynclib.h"
+#include "config.h"
 
 #include <QObject>
 #include <QByteArray>
@@ -61,6 +62,12 @@ OCSYNC_EXPORT bool uploadChecksumEnabled();
 /// Checks OWNCLOUD_CONTENT_CHECKSUM_TYPE (default: SHA1)
 OCSYNC_EXPORT QByteArray contentChecksumType();
 
+// Exported functions for the tests.
+QByteArray OCSYNC_EXPORT calcMd5(const QString &fileName);
+QByteArray OCSYNC_EXPORT calcSha1(const QString &fileName);
+#ifdef ZLIB_FOUND
+QByteArray OCSYNC_EXPORT calcAdler32(const QString &fileName);
+#endif
 
 /**
  * Computes the checksum of a file.
index 464c2edc69bd9bbfb61f65e84f38d9b7773a5a29..491a74e3abf4c20beee88f21494b600a49d2781f 100644 (file)
 #include <QDir>
 #include <QUrl>
 #include <QFile>
-#include <QCryptographicHash>
 #include <QCoreApplication>
 
 #include <sys/stat.h>
 #include <sys/types.h>
 
-#ifdef ZLIB_FOUND
-#include <zlib.h>
-#endif
-
 #ifdef Q_OS_WIN
 #include <windows.h>
 #include <windef.h>
@@ -361,53 +356,6 @@ QString FileSystem::fileSystemForPath(const QString &path)
 }
 #endif
 
-#define BUFSIZE qint64(500 * 1024) // 500 KiB
-
-static QByteArray readToCrypto( const QString& filename, QCryptographicHash::Algorithm algo )
- {
-     QFile file(filename);
-     QByteArray arr;
-     QCryptographicHash crypto( algo );
-
-     if (file.open(QIODevice::ReadOnly)) {
-         if (crypto.addData(&file)) {
-             arr = crypto.result().toHex();
-         }
-     }
-     return arr;
- }
-
-QByteArray FileSystem::calcMd5(const QString &filename)
-{
-    return readToCrypto(filename, QCryptographicHash::Md5);
-}
-
-QByteArray FileSystem::calcSha1(const QString &filename)
-{
-    return readToCrypto(filename, QCryptographicHash::Sha1);
-}
-
-#ifdef ZLIB_FOUND
-QByteArray FileSystem::calcAdler32(const QString &filename)
-{
-    QFile file(filename);
-    const qint64 bufSize = qMin(BUFSIZE, file.size() + 1);
-    QByteArray buf(bufSize, Qt::Uninitialized);
-
-    unsigned int adler = adler32(0L, Z_NULL, 0);
-    if (file.open(QIODevice::ReadOnly)) {
-        qint64 size = 0;
-        while (!file.atEnd()) {
-            size = file.read(buf.data(), bufSize);
-            if (size > 0)
-                adler = adler32(adler, (const Bytef *)buf.data(), size);
-        }
-    }
-
-    return QByteArray::number(adler, 16);
-}
-#endif
-
 bool FileSystem::remove(const QString &fileName, QString *errorString)
 {
 #ifdef Q_OS_WIN
index c4a19056efe317a07060ecfb76a1cff0662387c1..4c7b8e99913f4c0c2d16bafcc4f6ff760eedca63 100644 (file)
@@ -130,12 +130,6 @@ namespace FileSystem {
     QString fileSystemForPath(const QString &path);
 #endif
 
-    QByteArray OCSYNC_EXPORT calcMd5(const QString &fileName);
-    QByteArray OCSYNC_EXPORT calcSha1(const QString &fileName);
-#ifdef ZLIB_FOUND
-    QByteArray OCSYNC_EXPORT calcAdler32(const QString &fileName);
-#endif
-
     /**
      * Returns true when a file is locked. (Windows only)
      */
index f07283c1a7de4e6f5dfa4b903d4e586fd772b3f6..6741e0499edb08048d3e1b75d21e48a698a543de 100644 (file)
@@ -43,7 +43,6 @@ nextcloud_add_test(ChecksumValidator "")
 nextcloud_add_test(ClientSideEncryption "")
 nextcloud_add_test(ExcludedFiles "")
 
-nextcloud_add_test(FileSystem "")
 nextcloud_add_test(Utility "")
 nextcloud_add_test(SyncEngine "syncenginetestutils.h")
 nextcloud_add_test(SyncVirtualFiles "syncenginetestutils.h")
index f24f8977dbb05337b8ba004e5af616884f189be3..c7f181d2cd845dda24211e2b367b6025b39c5da5 100644 (file)
 #include "filesystem.h"
 #include "propagatorjobs.h"
 
-
 using namespace OCC;
+using namespace OCC::Utility;
 
     class TestChecksumValidator : public QObject
     {
         Q_OBJECT
-
     private:
-        QString _root;
+        QTemporaryDir _root;
         QString _testfile;
         QString _expectedError;
         QByteArray     _expected;
@@ -48,17 +47,62 @@ using namespace OCC;
          _errorSeen = true;
     }
 
+    static QByteArray shellSum( const QByteArray& cmd, const QString& file )
+    {
+        QProcess md5;
+        QStringList args;
+        args.append(file);
+        md5.start(cmd, args);
+        QByteArray sumShell;
+        qDebug() << "File: "<< file;
+
+        if( md5.waitForFinished()  ) {
+
+            sumShell = md5.readAll();
+            sumShell = sumShell.left( sumShell.indexOf(' '));
+        }
+        return sumShell;
+    }
+
     private slots:
 
     void initTestCase() {
-        _root = QDir::tempPath() + "/" + "test_" + QString::number(qrand());
-        QDir rootDir(_root);
-
-        rootDir.mkpath(_root );
-        _testfile = _root+"/csFile";
+        _testfile = _root.path()+"/csFile";
         Utility::writeRandomFile( _testfile);
     }
 
+    void testMd5Calc()
+    {
+        QString file( _root.path() + "/file_a.bin");
+        QVERIFY(writeRandomFile(file));
+        QFileInfo fi(file);
+        QVERIFY(fi.exists());
+        QByteArray sum = calcMd5(file);
+
+        QByteArray sSum = shellSum("md5sum", file);
+        if (sSum.isEmpty())
+            QSKIP("Couldn't execute md5sum to calculate checksum, executable missing?", SkipSingle);
+
+        QVERIFY(!sum.isEmpty());
+        QCOMPARE(sSum, sum);
+    }
+
+    void testSha1Calc()
+    {
+        QString file( _root.path() + "/file_b.bin");
+        writeRandomFile(file);
+        QFileInfo fi(file);
+        QVERIFY(fi.exists());
+        QByteArray sum = calcSha1(file);
+
+        QByteArray sSum = shellSum("sha1sum", file);
+        if (sSum.isEmpty())
+            QSKIP("Couldn't execute sha1sum to calculate checksum, executable missing?", SkipSingle);
+
+        QVERIFY(!sum.isEmpty());
+        QCOMPARE(sSum, sum);
+    }
+
     void testUploadChecksummingAdler() {
 #ifndef ZLIB_FOUND
         QSKIP("ZLIB not found.", SkipSingle);
@@ -69,7 +113,7 @@ using namespace OCC;
 
         connect(vali, SIGNAL(done(QByteArray,QByteArray)), SLOT(slotUpValidated(QByteArray,QByteArray)));
 
-        _expected = FileSystem::calcAdler32( _testfile );
+        _expected = calcAdler32( _testfile );
         qDebug() << "XX Expected Checksum: " << _expected;
         vali->start(_testfile);
 
@@ -88,7 +132,7 @@ using namespace OCC;
         vali->setChecksumType(_expectedType);
         connect(vali, SIGNAL(done(QByteArray,QByteArray)), this, SLOT(slotUpValidated(QByteArray,QByteArray)));
 
-        _expected = FileSystem::calcMd5( _testfile );
+        _expected = calcMd5( _testfile );
         vali->start(_testfile);
 
         QEventLoop loop;
@@ -105,7 +149,7 @@ using namespace OCC;
         vali->setChecksumType(_expectedType);
         connect(vali, SIGNAL(done(QByteArray,QByteArray)), this, SLOT(slotUpValidated(QByteArray,QByteArray)));
 
-        _expected = FileSystem::calcSha1( _testfile );
+        _expected = calcSha1( _testfile );
 
         vali->start(_testfile);
 
@@ -122,7 +166,7 @@ using namespace OCC;
 #else
         QByteArray adler =  checkSumAdlerC;
         adler.append(":");
-        adler.append(FileSystem::calcAdler32( _testfile ));
+        adler.append(calcAdler32( _testfile ));
         _successDown = false;
 
         auto *vali = new ValidateChecksumHeader(this);
diff --git a/test/testfilesystem.cpp b/test/testfilesystem.cpp
deleted file mode 100644 (file)
index 0b8b612..0000000
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
-   This software is in the public domain, furnished "as is", without technical
-   support, and with no warranty, express or implied, as to its usefulness for
-   any purpose.
-*/
-
-#include <QtTest>
-#include <QDebug>
-
-#include "filesystem.h"
-#include "common/utility.h"
-
-using namespace OCC::Utility;
-using namespace OCC::FileSystem;
-
-class TestFileSystem : public QObject
-{
-    Q_OBJECT
-
-    QTemporaryDir _root;
-
-
-    QByteArray shellSum( const QByteArray& cmd, const QString& file )
-    {
-       QProcess md5;
-       QStringList args;
-       args.append(file);
-       md5.start(cmd, args);
-       QByteArray sumShell;
-       qDebug() << "File: "<< file;
-
-       if( md5.waitForFinished()  ) {
-
-         sumShell = md5.readAll();
-         sumShell = sumShell.left( sumShell.indexOf(' '));
-       }
-       return sumShell;
-    }
-
-private slots:
-    void testMd5Calc()
-    {
-        QString file( _root.path() + "/file_a.bin");
-        QVERIFY(writeRandomFile(file));
-        QFileInfo fi(file);
-        QVERIFY(fi.exists());
-        QByteArray sum = calcMd5(file);
-
-        QByteArray sSum = shellSum("md5sum", file);
-        if (sSum.isEmpty())
-            QSKIP("Couldn't execute md5sum to calculate checksum, executable missing?", SkipSingle);
-
-        QVERIFY(!sum.isEmpty());
-        QCOMPARE(sSum, sum);
-    }
-
-    void testSha1Calc()
-    {
-        QString file( _root.path() + "/file_b.bin");
-        writeRandomFile(file);
-        QFileInfo fi(file);
-        QVERIFY(fi.exists());
-        QByteArray sum = calcSha1(file);
-
-        QByteArray sSum = shellSum("sha1sum", file);
-        if (sSum.isEmpty())
-            QSKIP("Couldn't execute sha1sum to calculate checksum, executable missing?", SkipSingle);
-
-        QVERIFY(!sum.isEmpty());
-        QCOMPARE(sSum, sum);
-    }
-
-};
-
-QTEST_APPLESS_MAIN(TestFileSystem)
-#include "testfilesystem.moc"