TestSharing.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 or (at your option)
  7. * version 3 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "TestSharing.h"
  18. #include <QTest>
  19. #include <QXmlStreamReader>
  20. #include "crypto/Crypto.h"
  21. #include "crypto/Random.h"
  22. #include "keeshare/KeeShareSettings.h"
  23. #include <botan/rsa.h>
  24. QTEST_GUILESS_MAIN(TestSharing)
  25. Q_DECLARE_METATYPE(KeeShareSettings::Type)
  26. Q_DECLARE_METATYPE(KeeShareSettings::Key)
  27. Q_DECLARE_METATYPE(KeeShareSettings::Certificate)
  28. void TestSharing::initTestCase()
  29. {
  30. QVERIFY(Crypto::init());
  31. }
  32. void TestSharing::testNullObjects()
  33. {
  34. const QString empty;
  35. QXmlStreamReader reader(empty);
  36. const auto nullKey = KeeShareSettings::Key();
  37. QVERIFY(nullKey.isNull());
  38. const auto xmlKey = KeeShareSettings::Key::deserialize(reader);
  39. QVERIFY(xmlKey.isNull());
  40. const auto certificate = KeeShareSettings::Certificate();
  41. QVERIFY(certificate.isNull());
  42. const auto xmlCertificate = KeeShareSettings::Certificate::deserialize(reader);
  43. QVERIFY(xmlCertificate.isNull());
  44. const auto own = KeeShareSettings::Own();
  45. QVERIFY(own.isNull());
  46. const auto xmlOwn = KeeShareSettings::Own::deserialize(empty);
  47. QVERIFY(xmlOwn.isNull());
  48. const auto active = KeeShareSettings::Active();
  49. QVERIFY(active.isNull());
  50. const auto xmlActive = KeeShareSettings::Active::deserialize(empty);
  51. QVERIFY(xmlActive.isNull());
  52. const auto reference = KeeShareSettings::Reference();
  53. QVERIFY(reference.isNull());
  54. const auto xmlReference = KeeShareSettings::Reference::deserialize(empty);
  55. QVERIFY(xmlReference.isNull());
  56. }
  57. void TestSharing::testKeySerialization()
  58. {
  59. auto key = stubkey();
  60. KeeShareSettings::Key original;
  61. original.key = key;
  62. QString buffer;
  63. QXmlStreamWriter writer(&buffer);
  64. writer.writeStartDocument();
  65. writer.writeStartElement("Key");
  66. KeeShareSettings::Key::serialize(writer, original);
  67. writer.writeEndElement();
  68. writer.writeEndDocument();
  69. QXmlStreamReader reader(buffer);
  70. reader.readNextStartElement();
  71. QVERIFY(reader.name() == "Key");
  72. KeeShareSettings::Key restored = KeeShareSettings::Key::deserialize(reader);
  73. QCOMPARE(restored.key->private_key_bits(), original.key->private_key_bits());
  74. QCOMPARE(restored.key->algo_name(), original.key->algo_name());
  75. }
  76. void TestSharing::testReferenceSerialization()
  77. {
  78. QFETCH(QString, password);
  79. QFETCH(QString, path);
  80. QFETCH(QUuid, uuid);
  81. QFETCH(int, type);
  82. KeeShareSettings::Reference original;
  83. original.password = password;
  84. original.path = path;
  85. original.uuid = uuid;
  86. original.type = static_cast<KeeShareSettings::Type>(type);
  87. const QString serialized = KeeShareSettings::Reference::serialize(original);
  88. const KeeShareSettings::Reference restored = KeeShareSettings::Reference::deserialize(serialized);
  89. QCOMPARE(restored.password, original.password);
  90. QCOMPARE(restored.path, original.path);
  91. QCOMPARE(restored.uuid, original.uuid);
  92. QCOMPARE(int(restored.type), int(original.type));
  93. }
  94. void TestSharing::testReferenceSerialization_data()
  95. {
  96. QTest::addColumn<QString>("password");
  97. QTest::addColumn<QString>("path");
  98. QTest::addColumn<QUuid>("uuid");
  99. QTest::addColumn<int>("type");
  100. QTest::newRow("1") << "Password" << "/some/path" << QUuid::createUuid() << int(KeeShareSettings::Inactive);
  101. QTest::newRow("2") << "" << "" << QUuid() << int(KeeShareSettings::SynchronizeWith);
  102. QTest::newRow("3") << "" << "/some/path" << QUuid() << int(KeeShareSettings::ExportTo);
  103. }
  104. void TestSharing::testSettingsSerialization()
  105. {
  106. QFETCH(bool, importing);
  107. QFETCH(bool, exporting);
  108. QFETCH(KeeShareSettings::Certificate, ownCertificate);
  109. QFETCH(KeeShareSettings::Key, ownKey);
  110. KeeShareSettings::Own originalOwn;
  111. KeeShareSettings::Active originalActive;
  112. originalActive.in = importing;
  113. originalActive.out = exporting;
  114. originalOwn.certificate = ownCertificate;
  115. originalOwn.key = ownKey;
  116. const QString serializedActive = KeeShareSettings::Active::serialize(originalActive);
  117. KeeShareSettings::Active restoredActive = KeeShareSettings::Active::deserialize(serializedActive);
  118. const QString serializedOwn = KeeShareSettings::Own::serialize(originalOwn);
  119. KeeShareSettings::Own restoredOwn = KeeShareSettings::Own::deserialize(serializedOwn);
  120. QCOMPARE(restoredActive.in, importing);
  121. QCOMPARE(restoredActive.out, exporting);
  122. if (ownCertificate.key) {
  123. QCOMPARE(restoredOwn.certificate, ownCertificate);
  124. }
  125. if (ownKey.key) {
  126. QCOMPARE(restoredOwn.key, ownKey);
  127. }
  128. }
  129. void TestSharing::testSettingsSerialization_data()
  130. {
  131. auto sshKey0 = stubkey(0);
  132. KeeShareSettings::Certificate certificate0;
  133. certificate0.key = sshKey0;
  134. certificate0.signer = "signer";
  135. KeeShareSettings::Key key0;
  136. key0.key = sshKey0;
  137. QTest::addColumn<bool>("importing");
  138. QTest::addColumn<bool>("exporting");
  139. QTest::addColumn<KeeShareSettings::Certificate>("ownCertificate");
  140. QTest::addColumn<KeeShareSettings::Key>("ownKey");
  141. QTest::newRow("1") << false << false << KeeShareSettings::Certificate() << KeeShareSettings::Key();
  142. QTest::newRow("2") << true << false << KeeShareSettings::Certificate() << KeeShareSettings::Key();
  143. QTest::newRow("3") << true << true << KeeShareSettings::Certificate() << KeeShareSettings::Key();
  144. QTest::newRow("4") << false << true << certificate0 << key0;
  145. QTest::newRow("5") << false << false << certificate0 << key0;
  146. }
  147. const QSharedPointer<Botan::RSA_PrivateKey> TestSharing::stubkey(int index)
  148. {
  149. static QMap<int, QSharedPointer<Botan::RSA_PrivateKey>> keys;
  150. if (!keys.contains(index)) {
  151. keys.insert(index,
  152. QSharedPointer<Botan::RSA_PrivateKey>(new Botan::RSA_PrivateKey(*randomGen()->getRng(), 2048)));
  153. }
  154. return keys[index];
  155. }