testclientsideencryption.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. QByteArray convertToOldStorageFormat(const QByteArray &data)
  13. {
  14. return data.split('|').join("fA==");
  15. }
  16. private slots:
  17. void shouldEncryptPrivateKeys()
  18. {
  19. // GIVEN
  20. const auto encryptionKey = QByteArrayLiteral("foo");
  21. const auto privateKey = QByteArrayLiteral("bar");
  22. const auto originalSalt = QByteArrayLiteral("baz");
  23. // WHEN
  24. const auto cipher = EncryptionHelper::encryptPrivateKey(encryptionKey, privateKey, originalSalt);
  25. // THEN
  26. const auto parts = cipher.split('|');
  27. QCOMPARE(parts.size(), 3);
  28. const auto encryptedKey = QByteArray::fromBase64(parts[0]);
  29. const auto iv = QByteArray::fromBase64(parts[1]);
  30. const auto salt = QByteArray::fromBase64(parts[2]);
  31. // We're not here to check the merits of the encryption but at least make sure it's been
  32. // somewhat ciphered
  33. QVERIFY(!encryptedKey.isEmpty());
  34. QVERIFY(encryptedKey != privateKey);
  35. QVERIFY(!iv.isEmpty());
  36. QCOMPARE(salt, originalSalt);
  37. }
  38. void shouldDecryptPrivateKeys()
  39. {
  40. // GIVEN
  41. const auto encryptionKey = QByteArrayLiteral("foo");
  42. const auto originalPrivateKey = QByteArrayLiteral("bar");
  43. const auto originalSalt = QByteArrayLiteral("baz");
  44. const auto cipher = EncryptionHelper::encryptPrivateKey(encryptionKey, originalPrivateKey, originalSalt);
  45. // WHEN
  46. const auto privateKey = EncryptionHelper::decryptPrivateKey(encryptionKey, cipher);
  47. const auto salt = EncryptionHelper::extractPrivateKeySalt(cipher);
  48. // THEN
  49. QCOMPARE(privateKey, originalPrivateKey);
  50. QCOMPARE(salt, originalSalt);
  51. }
  52. void shouldDecryptPrivateKeysInOldStorageFormat()
  53. {
  54. // GIVEN
  55. const auto encryptionKey = QByteArrayLiteral("foo");
  56. const auto originalPrivateKey = QByteArrayLiteral("bar");
  57. const auto originalSalt = QByteArrayLiteral("baz");
  58. const auto cipher = convertToOldStorageFormat(EncryptionHelper::encryptPrivateKey(encryptionKey, originalPrivateKey, originalSalt));
  59. // WHEN
  60. const auto privateKey = EncryptionHelper::decryptPrivateKey(encryptionKey, cipher);
  61. const auto salt = EncryptionHelper::extractPrivateKeySalt(cipher);
  62. // THEN
  63. QCOMPARE(privateKey, originalPrivateKey);
  64. QCOMPARE(salt, originalSalt);
  65. }
  66. void shouldSymmetricEncryptStrings()
  67. {
  68. // GIVEN
  69. const auto encryptionKey = QByteArrayLiteral("foo");
  70. const auto data = QByteArrayLiteral("bar");
  71. // WHEN
  72. const auto cipher = EncryptionHelper::encryptStringSymmetric(encryptionKey, data);
  73. // THEN
  74. const auto parts = cipher.split('|');
  75. QCOMPARE(parts.size(), 2);
  76. const auto encryptedData = QByteArray::fromBase64(parts[0]);
  77. const auto iv = QByteArray::fromBase64(parts[1]);
  78. // We're not here to check the merits of the encryption but at least make sure it's been
  79. // somewhat ciphered
  80. QVERIFY(!encryptedData.isEmpty());
  81. QVERIFY(encryptedData != data);
  82. QVERIFY(!iv.isEmpty());
  83. }
  84. void shouldSymmetricDecryptStrings()
  85. {
  86. // GIVEN
  87. const auto encryptionKey = QByteArrayLiteral("foo");
  88. const auto originalData = QByteArrayLiteral("bar");
  89. const auto cipher = EncryptionHelper::encryptStringSymmetric(encryptionKey, originalData);
  90. // WHEN
  91. const auto data = EncryptionHelper::decryptStringSymmetric(encryptionKey, cipher);
  92. // THEN
  93. QCOMPARE(data, originalData);
  94. }
  95. void shouldSymmetricDecryptStringsInOldStorageFormat()
  96. {
  97. // GIVEN
  98. const auto encryptionKey = QByteArrayLiteral("foo");
  99. const auto originalData = QByteArrayLiteral("bar");
  100. const auto cipher = convertToOldStorageFormat(EncryptionHelper::encryptStringSymmetric(encryptionKey, originalData));
  101. // WHEN
  102. const auto data = EncryptionHelper::decryptStringSymmetric(encryptionKey, cipher);
  103. // THEN
  104. QCOMPARE(data, originalData);
  105. }
  106. };
  107. QTEST_APPLESS_MAIN(TestClientSideEncryption)
  108. #include "testclientsideencryption.moc"