#include <QLoggingCategory>
#include <qtconcurrentrun.h>
+#include <QCryptographicHash>
+
+#ifdef ZLIB_FOUND
+#include <zlib.h>
+#endif
/** \file checksums.cpp
*
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())
}
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
#pragma once
#include "ocsynclib.h"
+#include "config.h"
#include <QObject>
#include <QByteArray>
/// 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.
#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>
}
#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
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)
*/
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")
#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;
_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);
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);
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;
vali->setChecksumType(_expectedType);
connect(vali, SIGNAL(done(QByteArray,QByteArray)), this, SLOT(slotUpValidated(QByteArray,QByteArray)));
- _expected = FileSystem::calcSha1( _testfile );
+ _expected = calcSha1( _testfile );
vali->start(_testfile);
#else
QByteArray adler = checkSumAdlerC;
adler.append(":");
- adler.append(FileSystem::calcAdler32( _testfile ));
+ adler.append(calcAdler32( _testfile ));
_successDown = false;
auto *vali = new ValidateChecksumHeader(this);
+++ /dev/null
-/*
- 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"