From: Olivier Goffart Date: Thu, 5 Jul 2018 12:24:44 +0000 (+0200) Subject: Move the checksum related routine from FileSystem to checksum, where they belong X-Git-Tag: archive/raspbian/3.16.7-1_deb13u1+rpi1~1^2~12^2~21^2~468^2~514 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=bf6e4174c881f293e74b83855325e0d33d95ce1b;p=nextcloud-desktop.git Move the checksum related routine from FileSystem to checksum, where they belong --- diff --git a/src/common/checksums.cpp b/src/common/checksums.cpp index 3099c24fb..a2bfdabc7 100644 --- a/src/common/checksums.cpp +++ b/src/common/checksums.cpp @@ -21,6 +21,11 @@ #include #include +#include + +#ifdef ZLIB_FOUND +#include +#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 diff --git a/src/common/checksums.h b/src/common/checksums.h index 29a5bcf12..765e4659b 100644 --- a/src/common/checksums.h +++ b/src/common/checksums.h @@ -19,6 +19,7 @@ #pragma once #include "ocsynclib.h" +#include "config.h" #include #include @@ -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. diff --git a/src/common/filesystembase.cpp b/src/common/filesystembase.cpp index 464c2edc6..491a74e3a 100644 --- a/src/common/filesystembase.cpp +++ b/src/common/filesystembase.cpp @@ -22,16 +22,11 @@ #include #include #include -#include #include #include #include -#ifdef ZLIB_FOUND -#include -#endif - #ifdef Q_OS_WIN #include #include @@ -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 diff --git a/src/common/filesystembase.h b/src/common/filesystembase.h index c4a19056e..4c7b8e999 100644 --- a/src/common/filesystembase.h +++ b/src/common/filesystembase.h @@ -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) */ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f07283c1a..6741e0499 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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") diff --git a/test/testchecksumvalidator.cpp b/test/testchecksumvalidator.cpp index f24f8977d..c7f181d2c 100644 --- a/test/testchecksumvalidator.cpp +++ b/test/testchecksumvalidator.cpp @@ -15,15 +15,14 @@ #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 index 0b8b61246..000000000 --- a/test/testfilesystem.cpp +++ /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 -#include - -#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"