testclientsideencryption.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. This software is in the public domain, furnished "as is", without technical
  3. support, and with no warranty, express or implied, as to its usefulness for
  4. any purpose.
  5. */
  6. #include <QtTest>
  7. #include "clientsideencryption.h"
  8. using namespace OCC;
  9. class TestClientSideEncryption : public QObject
  10. {
  11. Q_OBJECT
  12. private slots:
  13. void shouldEncryptPrivateKeys()
  14. {
  15. // GIVEN
  16. const auto encryptionKey = QByteArrayLiteral("foo");
  17. const auto privateKey = QByteArrayLiteral("bar");
  18. const auto originalSalt = QByteArrayLiteral("baz");
  19. // WHEN
  20. const auto cipher = EncryptionHelper::encryptPrivateKey(encryptionKey, privateKey, originalSalt);
  21. // THEN
  22. const auto parts = cipher.split('|');
  23. QCOMPARE(parts.size(), 3);
  24. const auto encryptedKey = QByteArray::fromBase64(parts[0]);
  25. const auto iv = QByteArray::fromBase64(parts[1]);
  26. const auto salt = QByteArray::fromBase64(parts[2]);
  27. // We're not here to check the merits of the encryption but at least make sure it's been
  28. // somewhat ciphered
  29. QVERIFY(!encryptedKey.isEmpty());
  30. QVERIFY(encryptedKey != privateKey);
  31. QVERIFY(!iv.isEmpty());
  32. QCOMPARE(salt, originalSalt);
  33. }
  34. void shouldDecryptPrivateKeys()
  35. {
  36. // GIVEN
  37. const auto encryptionKey = QByteArrayLiteral("foo");
  38. const auto originalPrivateKey = QByteArrayLiteral("bar");
  39. const auto originalSalt = QByteArrayLiteral("baz");
  40. const auto cipher = EncryptionHelper::encryptPrivateKey(encryptionKey, originalPrivateKey, originalSalt);
  41. // WHEN (note the salt is not passed, so had to extract by hand)
  42. const auto privateKey = EncryptionHelper::decryptPrivateKey(encryptionKey, cipher.left(cipher.lastIndexOf('|')));
  43. // THEN
  44. QCOMPARE(privateKey, originalPrivateKey);
  45. }
  46. void shouldSymmetricEncryptStrings()
  47. {
  48. // GIVEN
  49. const auto encryptionKey = QByteArrayLiteral("foo");
  50. const auto data = QByteArrayLiteral("bar");
  51. // WHEN
  52. const auto cipher = EncryptionHelper::encryptStringSymmetric(encryptionKey, data);
  53. // THEN
  54. const auto parts = cipher.split('|');
  55. QCOMPARE(parts.size(), 2);
  56. const auto encryptedData = QByteArray::fromBase64(parts[0]);
  57. const auto iv = QByteArray::fromBase64(parts[1]);
  58. // We're not here to check the merits of the encryption but at least make sure it's been
  59. // somewhat ciphered
  60. QVERIFY(!encryptedData.isEmpty());
  61. QVERIFY(encryptedData != data);
  62. QVERIFY(!iv.isEmpty());
  63. }
  64. void shouldSymmetricDecryptStrings()
  65. {
  66. // GIVEN
  67. const auto encryptionKey = QByteArrayLiteral("foo");
  68. const auto originalData = QByteArrayLiteral("bar");
  69. const auto cipher = EncryptionHelper::encryptStringSymmetric(encryptionKey, originalData);
  70. // WHEN (not it is still in base64 when returned)
  71. const auto data = QByteArray::fromBase64(EncryptionHelper::decryptStringSymmetric(encryptionKey, cipher));
  72. // THEN
  73. QCOMPARE(data, originalData);
  74. }
  75. };
  76. QTEST_APPLESS_MAIN(TestClientSideEncryption)
  77. #include "testclientsideencryption.moc"