Skip to content

Commit

Permalink
Unit tests. Refactor and remove redundant things.
Browse files Browse the repository at this point in the history
Signed-off-by: alex-z <[email protected]>
  • Loading branch information
allexzander committed Jan 15, 2024
1 parent 1526af7 commit b3249a1
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 85 deletions.
131 changes: 56 additions & 75 deletions test/testclientsideencryptionv2.cpp
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
/*
* 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.
* Copyright (C) by Oleksandr Zolotov <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/

#include "updatee2eefoldermetadatajob.h"
#include "syncengine.h"
#include "syncenginetestutils.h"
#include "testhelper.h"
#include "owncloudpropagator_p.h"
#include "propagatorjobs.h"
#include "clientsideencryption.h"
#include "foldermetadata.h"

#include <QtTest>

namespace
{
constexpr auto fakeE2eeFolderName = "fake_e2eev2_folder";
const QString fakeE2eeFolderPath = QStringLiteral("/") + fakeE2eeFolderName;
};

using namespace OCC;

class TestClientSideEncryptionV2 : public QObject
{
Q_OBJECT

QScopedPointer<FakeQNAM> _fakeQnam;
QSharedPointer<OwncloudPropagator> _propagator;
QScopedPointer<FolderMetadata> _parsedMetadataWithFileDrop;
QScopedPointer<FolderMetadata> _parsedMetadataAfterProcessingFileDrop;

Expand All @@ -45,43 +38,66 @@ class TestClientSideEncryptionV2 : public QObject
private slots:
void initTestCase()
{
QVariantMap capabilities;
capabilities[QStringLiteral("end-to-end-encryption")] = QVariantMap{
QVariantMap fakeCapabilities;
fakeCapabilities[QStringLiteral("end-to-end-encryption")] = QVariantMap{
{QStringLiteral("enabled"), true},
{QStringLiteral("api-version"), "2.0"}
};
const QUrl fakeUrl("http://example.de");

_account = Account::create();
QUrl url("http://example.de");
_fakeQnam.reset(new FakeQNAM({}));
auto *cred = new FakeCredentials{_fakeQnam.data()};
cred->setUserName("test");
_account->setCredentials(cred);
_account->setUrl(url);
_account->setCapabilities(capabilities);
{
_account = Account::create();
_fakeQnam.reset(new FakeQNAM({}));
const auto cred = new FakeCredentials{_fakeQnam.data()};
cred->setUserName("test");
_account->setCredentials(cred);
_account->setUrl(fakeUrl);
_account->setCapabilities(fakeCapabilities);
}
{
// make a second fake account so we can share metadata to it later
_secondAccount = Account::create();
_fakeQnam.reset(new FakeQNAM({}));
const auto credSecond = new FakeCredentials{_fakeQnam.data()};
credSecond->setUserName("sharee");
_secondAccount->setCredentials(credSecond);
_secondAccount->setUrl(fakeUrl);
_secondAccount->setCapabilities(fakeCapabilities);
}

QSslCertificate cert;
QSslKey publicKey;
QByteArray privateKey;

{
QFile e2eTestFakeCert(QStringLiteral("e2etestsfakecert.pem"));
if (e2eTestFakeCert.open(QFile::ReadOnly)) {
_account->e2e()->_certificate = QSslCertificate(e2eTestFakeCert.readAll());
e2eTestFakeCert.close();
}
QVERIFY(e2eTestFakeCert.open(QFile::ReadOnly));
cert = QSslCertificate(e2eTestFakeCert.readAll());
}
{
QFile e2etestsfakecertpublickey(QStringLiteral("e2etestsfakecertpublickey.pem"));
if (e2etestsfakecertpublickey.open(QFile::ReadOnly)) {
_account->e2e()->_publicKey =
QSslKey(e2etestsfakecertpublickey.readAll(), QSsl::KeyAlgorithm::Rsa, QSsl::EncodingFormat::Pem, QSsl::KeyType::PublicKey);
e2etestsfakecertpublickey.close();
}
QVERIFY(e2etestsfakecertpublickey.open(QFile::ReadOnly));
publicKey = QSslKey(e2etestsfakecertpublickey.readAll(), QSsl::KeyAlgorithm::Rsa, QSsl::EncodingFormat::Pem, QSsl::KeyType::PublicKey);
e2etestsfakecertpublickey.close();
}
{
QFile e2etestsfakecertprivatekey(QStringLiteral("e2etestsfakecertprivatekey.pem"));
if (e2etestsfakecertprivatekey.open(QFile::ReadOnly)) {
_account->e2e()->_privateKey = e2etestsfakecertprivatekey.readAll();
e2etestsfakecertprivatekey.close();
}
QVERIFY(e2etestsfakecertprivatekey.open(QFile::ReadOnly));
privateKey = e2etestsfakecertprivatekey.readAll();
}

QVERIFY(cert.isValid());
QVERIFY(!publicKey.isNull());
QVERIFY(!privateKey.isEmpty());

_account->e2e()->_certificate = cert;
_account->e2e()->_publicKey = publicKey;
_account->e2e()->_privateKey = privateKey;

_secondAccount->e2e()->_certificate = cert;
_secondAccount->e2e()->_publicKey = publicKey;
_secondAccount->e2e()->_privateKey = privateKey;

}

void testInitializeNewRootFolderMetadataThenEncryptAndDecrypt()
Expand Down Expand Up @@ -179,41 +195,6 @@ private slots:

void testE2EeFolderMetadataSharing()
{
// make a second fake account so we can share metadata to it later
_secondAccount = Account::create();
QUrl url("http://example.de");
_fakeQnam.reset(new FakeQNAM({}));
auto *cred = new FakeCredentials{_fakeQnam.data()};
cred->setUserName("sharee");
_secondAccount->setCredentials(cred);
_secondAccount->setUrl(url);

QVariantMap capabilities;
capabilities[QStringLiteral("end-to-end-encryption")] = QVariantMap{{QStringLiteral("enabled"), true}, {QStringLiteral("api-version"), "2.0"}};
_secondAccount->setCapabilities(capabilities);
{
QFile e2eTestFakeCert(QStringLiteral("e2etestsfakecert.pem"));
if (e2eTestFakeCert.open(QFile::ReadOnly)) {
_secondAccount->e2e()->_certificate = QSslCertificate(e2eTestFakeCert.readAll());
e2eTestFakeCert.close();
}
}
{
QFile e2etestsfakecertpublickey(QStringLiteral("e2etestsfakecertpublickey.pem"));
if (e2etestsfakecertpublickey.open(QFile::ReadOnly)) {
_secondAccount->e2e()->_publicKey =
QSslKey(e2etestsfakecertpublickey.readAll(), QSsl::KeyAlgorithm::Rsa, QSsl::EncodingFormat::Pem, QSsl::KeyType::PublicKey);
e2etestsfakecertpublickey.close();
}
}
{
QFile e2etestsfakecertprivatekey(QStringLiteral("e2etestsfakecertprivatekey.pem"));
if (e2etestsfakecertprivatekey.open(QFile::ReadOnly)) {
_secondAccount->e2e()->_privateKey = e2etestsfakecertprivatekey.readAll();
e2etestsfakecertprivatekey.close();
}
}

// instantiate empty metadata, add a file, and share with a second user "sharee"
QScopedPointer<FolderMetadata> metadata(new FolderMetadata(_account, FolderMetadata::FolderType::Root));
QSignalSpy metadataSetupCompleteSpy(metadata.data(), &FolderMetadata::setupComplete);
Expand Down
20 changes: 10 additions & 10 deletions test/testsecurefiledrop.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/*
* 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.
* Copyright (C) by Oleksandr Zolotov <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/

#include "updatee2eefoldermetadatajob.h"
#include "syncengine.h"
#include "syncenginetestutils.h"
#include "testhelper.h"
#include "owncloudpropagator_p.h"
#include "propagatorjobs.h"
#include "clientsideencryption.h"
#include "foldermetadata.h"
#include <array>

#include <QtTest>

namespace
Expand Down

0 comments on commit b3249a1

Please sign in to comment.