From 31c3aee447bba3627a824cfa11bb318ab5fdf523 Mon Sep 17 00:00:00 2001
From: alex-z <blackslayer4@gmail.com>
Date: Tue, 4 Apr 2023 19:33:53 +0200
Subject: [PATCH] Fix unit tests SecureFileDrop

Signed-off-by: alex-z <blackslayer4@gmail.com>
---
 src/libsync/clientsideencryption.cpp     |  4 +++-
 src/libsync/clientsideencryption.h       |  2 ++
 test/CMakeLists.txt                      |  3 +++
 test/e2etestsfakecert.pem                | 22 +++++++++++++++++++
 test/e2etestsfakecertprivatekey.pem      | 28 ++++++++++++++++++++++++
 test/e2etestsfakecertpublickey.pem       |  9 ++++++++
 test/fakefiledrope2eefoldermetadata.json |  2 +-
 test/testsecurefiledrop.cpp              | 23 +++++++++++++++++++
 8 files changed, 91 insertions(+), 2 deletions(-)
 create mode 100644 test/e2etestsfakecert.pem
 create mode 100644 test/e2etestsfakecertprivatekey.pem
 create mode 100644 test/e2etestsfakecertpublickey.pem

diff --git a/src/libsync/clientsideencryption.cpp b/src/libsync/clientsideencryption.cpp
index 4989e373bbd..fe50a3c1c7c 100644
--- a/src/libsync/clientsideencryption.cpp
+++ b/src/libsync/clientsideencryption.cpp
@@ -1580,6 +1580,8 @@ void FolderMetadata::setupExistingMetadata(const QByteArray& metadata)
     const auto metadataKeyChecksum = metaDataDoc.object()["metadata"].toObject()["checksum"].toString().toUtf8();
 
     _fileDrop = metaDataDoc.object().value("filedrop").toObject();
+    // for unit tests
+    _fileDropFromServer = metaDataDoc.object().value("filedrop").toObject();
 
     // Iterate over the document to store the keys. I'm unsure that the keys are in order,
     // perhaps it's better to store a map instead of a vector, perhaps this just doesn't matter.
@@ -1929,7 +1931,7 @@ bool FolderMetadata::moveFromFileDropToFiles()
 
 QJsonObject FolderMetadata::fileDrop() const
 {
-    return _fileDrop;
+    return _fileDropFromServer;
 }
 
 bool EncryptionHelper::fileEncryption(const QByteArray &key, const QByteArray &iv, QFile *input, QFile *output, QByteArray& returnTag)
diff --git a/src/libsync/clientsideencryption.h b/src/libsync/clientsideencryption.h
index 1f9504d6fa9..212502a0a9a 100644
--- a/src/libsync/clientsideencryption.h
+++ b/src/libsync/clientsideencryption.h
@@ -243,6 +243,8 @@ class OWNCLOUDSYNC_EXPORT FolderMetadata {
     RequiredMetadataVersion _requiredMetadataVersion = RequiredMetadataVersion::Version1_2;
     QVector<QPair<QString, QString>> _sharing;
     QJsonObject _fileDrop;
+    // used by unit tests, must get assigned simultaneously with _fileDrop and not erased
+    QJsonObject _fileDropFromServer;
     bool _isMetadataSetup = false;
     bool _encryptedMetadataNeedUpdate = false;
 };
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 0718822dc65..f6d8f57d98e 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -74,6 +74,9 @@ nextcloud_add_test(SecureFileDrop)
 target_link_libraries(SecureFileDropTest PRIVATE Nextcloud::sync)
 configure_file(fake2eelocksucceeded.json "${PROJECT_BINARY_DIR}/bin/fake2eelocksucceeded.json" COPYONLY)
 configure_file(fakefiledrope2eefoldermetadata.json "${PROJECT_BINARY_DIR}/bin/fakefiledrope2eefoldermetadata.json" COPYONLY)
+configure_file(e2etestsfakecert.pem "${PROJECT_BINARY_DIR}/bin/e2etestsfakecert.pem" COPYONLY)
+configure_file(e2etestsfakecertprivatekey.pem "${PROJECT_BINARY_DIR}/bin/e2etestsfakecertprivatekey.pem" COPYONLY)
+configure_file(e2etestsfakecertpublickey.pem "${PROJECT_BINARY_DIR}/bin/e2etestsfakecertpublickey.pem" COPYONLY)
 
 
 if(ADD_E2E_TESTS)
diff --git a/test/e2etestsfakecert.pem b/test/e2etestsfakecert.pem
new file mode 100644
index 00000000000..c3ce6c64186
--- /dev/null
+++ b/test/e2etestsfakecert.pem
@@ -0,0 +1,22 @@
+-----BEGIN CERTIFICATE-----
+MIIDnjCCAoagAwIBAgIBADANBgkqhkiG9w0BAQUFADBoMQswCQYDVQQGEwJERTEb
+MBkGA1UECAwSQmFkZW4tV3VlcnR0ZW1i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vYDgTfwTVAzaoU2ZThgvRmEYQ4C1ruDJ1N
+YGMx13SHn8hOV0O1s0xj/DHCkF+0yG9ugztNQeZc1CYV49JWYuLOLY4qsg/RPpH1
+ZrGxt2IZxiSWZH845v9YZzxB
+-----END CERTIFICATE-----
diff --git a/test/e2etestsfakecertprivatekey.pem b/test/e2etestsfakecertprivatekey.pem
new file mode 100644
index 00000000000..db9995bef02
--- /dev/null
+++ b/test/e2etestsfakecertprivatekey.pem
@@ -0,0 +1,28 @@
+-----BEGIN PRIVATE KEY-----
+MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDFUbXE6XY/Fbrk
+hHJP8x1QzhajMukiRXGFVzIyCHZZN9gcU8XEo+MTeKRW0PoAC4CtdjGYDCYCUshU
+5SYIvsN9ywgZqMgfzNVY947TS6k79rGgzVHPYn6oZoQXneeUryQrHxUkHrCJmrX+
+EVq8FGN/saOS5QO9VBPFyCCCJPTeq586RoGVnrt7NPnjFsjyyelkLQNwUPzBKqhI
+qqUFhHJ+iAXNbZ2XtwKSEEZs1Fh1W2KvRj9LOr7JWxbb7+XvOYUniMnmmv9Bo4Pv
+ekqhV1DSS7A4YKg1HGyNE7RR6u6+DVjXQiNwNVFuGJuj/oOBWX6HVgYiIZ4n2PFj
+vHPHbRQhAgMBAAECggEAY6tydZS39gWz8do1I81Z8YcoSCdfO8B4JzJ0WHGN1mMu
+9qAaNcnS2fMGkBPfNEabKQOa6YK5cFMAfmGBZMl3VgNIhC2NaQK+gcIKkxCU24Ux
+FFiiCm9mKwodjxBj+ea+DkoVE7ms0tUjOMLrYjWqTMYeKgUkbm4Zzr3YLeUdC5wh
+1fwtEExwCcx5CrtCuXPS33nnwowXNV5YLZgh7q6vSh57//yozt03B7qRMis52l8l
+HuafOIaLlZZuijM7vVCJUvctbQcNYY/C4kzymCeuJSgTRXUB+xbJHszWhNQnioVv
+xqOU4u4yVteR6I5J1oWN0Z5e5ukMZSH8UVsNfDNZQQKBgQDt27cr8K8plui3g+pN
+SpEMZMw56rH/pnvGvzpxBMrslCP/Alvu9eSEqzuekuhdu6wTaNxjltlO1jzGtZUL
+hUBCyBvkR7DCpRnLyEBjvy9oQWQUYxzprHdh2U1NFFnQhkeKM1AqK9ZZodNlZmBw
+ZuYAu0rY/KIYrB5DStJKJt2cnwKBgQDUXnOYXNnKY4VHBJG3Av2ALHDwpgJE+xa1
+5CGDgmPqsEEWRX7+GiqK+p2Gt22uN1OBRxCpmZwtiTBYjwSRCX34ub7N1PNvpVhQ
+8gXlm01Zrk3+N/ArC5u6uoi+dUmBdei+VpZSzFPqRbKleWDPIz2ifWFKDwqR3tm5
+s8Ttr2bXPwKBgEvywhQXr/Z5pykhx+iSDKqGbOd5VwioYhFd8g0wWJMd4jiEylIM
+1cC+5DSH+AYl7JXt/6EVA6I2qgVQ6ZxaGeC5AJ7tfTVJfmkhMd4np72EU65mz06V
+sTDfL92Sx/LbTXCFXe15PeI018ki3VxGnt5Q719Fm4SBQNB/zZG7wztFAoGBAJRd
+8HBv3hBRexlkjdDIHAbiBX163XcH3JPy758LB3c88Bv5LYGlEjfKYydtGwltyb81
+wORcVwpRIB+eu1gAw6tzSWRP9Er5vzu9+nSxDVcsOXzz46yPDZtlGofFNTdnAtWH
+dLof2jh/vRFyPTo5qqPM6J1u6LmEcaQVfe14b2bNAoGAIf6XKCLnEkfZC//7pFS3
+M5b6DCrEg03MivpXU2PNMGO4rVdfyzjRjL2aFt7GEN7wAiqLGPDB9cJIAQ6KyJYR
+qSZ+DNIkK1671HtcR2ixa0dGmtf1CZJYbT9Z/gOSmrOtuSE/46TbIhd3rwJgs/Vf
+ImL20AomzY9Dw4OWqOjXCR8=
+-----END PRIVATE KEY-----
diff --git a/test/e2etestsfakecertpublickey.pem b/test/e2etestsfakecertpublickey.pem
new file mode 100644
index 00000000000..52bdaf7ff35
--- /dev/null
+++ b/test/e2etestsfakecertpublickey.pem
@@ -0,0 +1,9 @@
+-----BEGIN PUBLIC KEY-----
+MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxVG1xOl2PxW65IRyT/Md
+UM4WozLpIkVxhVcyMgh2WTfYHFPFxKPjE3ikVtD6AAuArXYxmAwmAlLIVOUmCL7D
+fcsIGajIH8zVWPeO00upO/axoM1Rz2J+qGaEF53nlK8kKx8VJB6wiZq1/hFavBRj
+f7GjkuUDvVQTxcgggiT03qufOkaBlZ67ezT54xbI8snpZC0DcFD8wSqoSKqlBYRy
+fogFzW2dl7cCkhBGbNRYdVtir0Y/Szq+yVsW2+/l7zmFJ4jJ5pr/QaOD73pKoVdQ
+0kuwOGCoNRxsjRO0Ueruvg1Y10IjcDVRbhibo/6DgVl+h1YGIiGeJ9jxY7xzx20U
+IQIDAQAB
+-----END PUBLIC KEY-----
diff --git a/test/fakefiledrope2eefoldermetadata.json b/test/fakefiledrope2eefoldermetadata.json
index d6644440680..fcf457ce57a 100644
--- a/test/fakefiledrope2eefoldermetadata.json
+++ b/test/fakefiledrope2eefoldermetadata.json
@@ -1,6 +1,6 @@
 {
 	"ocs": {
-		"data": { "meta-data": "{\"files\":{\"083c2bffea5d4b0f824e2fd5df5369d2\":{\"authenticationTag\":\"LRWVv0vR01WUhrv26kGvOg==\",\"encrypted\":\"dUlVcf+8xaMgIxkWd7YYIsYLIotqD3ZjQtES1VsepKa7+aUYJGdNlPT25+CTQl65mY5ggu9d03hiysiBIUO7BH7klyUY9OQM80kGVE1xuWXQ1aCfgiFruN4h1VSS8S/9jrgBojxncBnsGZjU/NOGZUjA1svdE2hM+O4fywPKUyT09an9t2EbqUGgUl242ezJ|ja9flmYfZAl/MUjF11chaA==\",\"initializationVector\":\"vNfZNAVYVs0eGdB5vbo5KA==\",\"metadataKey\":0}},\"metadata\":{\"metadataKeys\":{\"0\":\"GHKkcNTxsyigJODA45neTO+8Y0NDfB+7mez90EwjW39mZvNnCUBeGO2R6vLzEf5apYjDNsWNS5sHvUZ188OLa9zCDmMm00m8dwfMPEUA0H5Rp9yewUbnM8YRl6vCZWvDa5HLTCdC8UCIKsbvuifAvveQXEO/vafzWrP8IAhG6WsNXZ4qqaUX/0pm84KXvHmStH60xpZpT8U/kKBNdxDeOTdp5T6FglRTnsF9wt9cplPtRHV+BzkC5NgfBqAvLVSP8gckj5iJNQrRM7IQmBPO0AMUv9yU609o7X4WUZ4LwNGqwL6ciCzS83BQ+FCEbf4HyViEWrEq2OLVFgDH7ML18Q==\"},\"version\":1},\"filedrop\":{\"1a1e95ae836b4005bf69e369661e81ba\":{\"encrypted\":\"r7o01Y3dBQbOlhR38ulPW77X0aoGS8riwJmnRp0k0fQgfySy5++GJkaTJqSdcQYvw8stn+hU7j6wwOJMA/aJ3UV5i8H7yPR+RQHiKjrU4L379HU+H1Xuu5KbkhbLoOc7GAxaCpyC0USYI4UCUcmKnSpqAhpHdnpScmyYztA6qnfumclIzgSfE87lCRRFnIp4mKm305hD+4gBuk3WfERewqbgK2Yo08sFhOjR6zULrJgqQBbHc61R7TZb18H26u0fa+mjRmehTSiqhy0dDePip7sr8+a0dCNCEcBG8qKK9xPDYCFmDrAq7iaEpcRoZ4CgUavXOSF+zdunBXXWmKTq6w==\",\"initializationVector\":\"hel7omho/1XsYqov8XCXgA==\",\"authenticationTag\":\"ni6/k9UpFEkS5FYoMPU+/g==\",\"metadataKey\":0}}}" },
+		"data": { "meta-data": "{\"filedrop\": {\"66b0ac65be914238b87482b9e11e0e50\": {\"authenticationTag\": \"0Rwvs5dNxWkWD6BjPw/kxw==\",\"encrypted\": \"IrGTXDmvT3whxM6peDsAU/K/djqIMH1BRYyBgy/rOdJELflHQqy02Vj2FUEw15wG7dPcz2139LnJ/G5y8hwTK/zfYZu6L4sycNhcNIYdL7RgDxic8Yb0EdldxCkwXNs7vzDbYlFQs8TGgj1t74vBH67q/7egtSobaGFnuhWwyRnPa+6lusVlI6JcvbZgUiznl7d15z3YkMVYU8tnkQnrRKUmAGHN6AnmJfpCERB3X1RvkH+g|dzyVeqYF9aLl8b0p2n4kMA==\", \"encryptedInitializationVector\": \"Ob0dvIhuZfYnqoAtaxd+pw==\", \"encryptedKey\": \"obAtUPk3eE/ptellAOttrv8JvvsGEcd5yAeh+W1g+LXjun0ghw89bZdaCUhUpJmf2FzlQYPiNEagiG6rPKm+ElQ0wK54c2V0cnzwLA4Pp+UpJltzskqAgsWL5MBxCKeG5AaYj8p+dMYHurEqpfVZ1IwoJrXGGbqYF/YkIGs5QpQ+GaR/6DcaFeVc6rV9clLj4kBbYi9nvAAjsrxoi1EFo+OAweAfd4yfD8+dFH73tkp2CjyXFt3uYmbhNrzTZ/aO4VlEZaXfzP/Wb46GHDRdUYn5C7xvyQpojtBqd9xTAEYMG63TAxbXvctkDgWcE6LU7bzpuqAGgzirQot/C1WFuQ==\", \"encryptedTag\": \"5IndHzz57X5EZGMczoJ66g==\", \"initializationVector\": \"YAlFrRfWnV0L0XrpXqn/kA==\"}},\"files\": {\"110d434df5a24afba34235a413a4902b\": {\"authenticationTag\": \"6TuODy8ttUhXjyxym9k1ag==\",\"encrypted\": \"HOKIEvyCATTIbQ+e3KjeACbJ8bfFNvcD11oyfLC8JHly0ywn4yWR2aZJSCJsDY0izEpDouwa2MlUvZktmoNY5HJBU4iYkOXeYMqHsBZhSVIudnBynIqapNNsQDOpkN9e9sf/xsp2L0zLZJMRpUVELtoEZras7vbwWZVqOSxCNP28AmOdEEWNTAEVDmARguB0|OqpD66fQPZuK3cPMb6LbBg==\",\"initializationVector\": \"gw0QQXWubC4DJgrhLpIXZQ==\"},\"9aa629d3fb1b46e994f96f6c69edd85b\": {\"authenticationTag\": \"JE6JpxGXWXzMz2mdJRWiaQ==\",\"encrypted\": \"Jic9JJh/NlO8QKot58B1lWvKClOEK4MXWUsZ8bKD1olcgyD0Ro28aQzvIrjILE6oHAAcyihVLVX9o3ZbdqkcHQ7PPnZzmDhemUI8638b8Ps9LdHnelu7emllChd3ek7E8c0yIXsBl1T4Fx6IPdi4T/1rgd7zYK3zIngw+062tgHHMEEv|p8l4VRZfeJDR5BIk3HfL/w==\",\"initializationVector\": \"ao37NlV9f4TRiAbb+NB6JQ==\"},\"9be0b13448794190bca706ef62851048\": {\"authenticationTag\": \"lk3IR4IF68wcSJNyQeE6JA==\",\"encrypted\": \"Ry7SQ8+FMUeTS94ZpAcNCrAWH2XSzELwwmELuF1axf/S/viFZP7TXcVh2h7pFQj5pgKKv+u+QJV35Rdnbh5yZMCqiPqx/pl/mBsQ22aI8m4NmXAercCgyT+yhsmxavuGh594bCS3s9G/4Qa4b6ySS4acI/DGlGVi5gs+Yp0yvxGfAB8+ysjZmoi0AmZ6wGrL5QUe+bZwvvT8wDnRWN1pJzLLN8OcvGb9MJgi2FDfgu1TAQWT1qvjCf3u6p0e6zx519U7IUv7UoL0pRqjWIhZHpt3cnRBZxRfpIa5BQwVvBfg/Izwj7yEag==|liZRcx80V9VMrFTg1UrkIA==\",\"initializationVector\": \"P7F3E+uUCTV9GT3miHbBkA==\"}},\"metadata\": {\"checksum\": \"f46e70826a40ce86b3e6255c57c9e775bc7352d22168d9d015d27e0355e5a1a5\",\"metadataKey\": \"NeM1n7LA0LTV51RVh7Ql/rCgMFw8jm6rEmqE6YmfBrAWr5Lq2QKkMNqIoGkct8NGFcD7SsZnsVKWrDqpI9qY1Jx9jmmp/KdDEEPsFx/AZYAUtVDVHVzeJ0cTaNi4UrDJEr6ChqVtLzlnF4kjNX0fu/htU5i1x0BJ4By/jilwzumBlZZYH0t35l3t79j2RelmCZjFc1kv93RSSuX3hgBnhaiOoQThFvX3VQPQIvVo5fKoNWLTNkL5bIME8qzLwv71qRzZmKXuKJBwNyXHZGcIquwS7zYBymGLExy2TbqGdmjqHbabaI3Lte9Rc6SqO5hTsx2wI9qW7GlLzu8mPN9xeQ==\",\"version\": 1.2}}"},
 		"meta": {
 			"message": "OK",
 			"status": "ok",
diff --git a/test/testsecurefiledrop.cpp b/test/testsecurefiledrop.cpp
index dd0a6ac00ce..d0b7020f939 100644
--- a/test/testsecurefiledrop.cpp
+++ b/test/testsecurefiledrop.cpp
@@ -43,6 +43,29 @@ private slots:
     {
         _fakeFolder.remoteModifier().mkdir(fakeE2eeFolderName);
         _fakeFolder.remoteModifier().insert(fakeE2eeFolderName + QStringLiteral("/") + QStringLiteral("fake_e2ee_file"), 100);
+
+        {
+            QFile e2eTestFakeCert(QStringLiteral("e2etestsfakecert.pem"));
+            if (e2eTestFakeCert.open(QFile::ReadOnly)) {
+                _fakeFolder.syncEngine().account()->e2e()->_certificate = QSslCertificate(e2eTestFakeCert.readAll());
+                e2eTestFakeCert.close();
+            }
+        }
+        {
+            QFile e2etestsfakecertpublickey(QStringLiteral("e2etestsfakecertpublickey.pem"));
+            if (e2etestsfakecertpublickey.open(QFile::ReadOnly)) {
+                _fakeFolder.syncEngine().account()->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)) {
+                _fakeFolder.syncEngine().account()->e2e()->_privateKey = e2etestsfakecertprivatekey.readAll();
+                e2etestsfakecertprivatekey.close();
+            }
+        }
+
         _fakeFolder.setServerOverride([this](QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *device) {
             Q_UNUSED(device);
             QNetworkReply *reply = nullptr;
