2
* Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
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.
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.
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/>.
18
#include "TestSharing.h"
21
#include <QXmlStreamReader>
23
#include "crypto/Crypto.h"
24
#include "crypto/Random.h"
25
#include "keeshare/KeeShareSettings.h"
29
QTEST_GUILESS_MAIN(TestSharing)
31
Q_DECLARE_METATYPE(KeeShareSettings::Type)
32
Q_DECLARE_METATYPE(KeeShareSettings::Key)
33
Q_DECLARE_METATYPE(KeeShareSettings::Certificate)
35
void TestSharing::initTestCase()
37
QVERIFY(Crypto::init());
40
void TestSharing::testNullObjects()
43
QXmlStreamReader reader(empty);
45
const auto nullKey = KeeShareSettings::Key();
46
QVERIFY(nullKey.isNull());
47
const auto xmlKey = KeeShareSettings::Key::deserialize(reader);
48
QVERIFY(xmlKey.isNull());
50
const auto certificate = KeeShareSettings::Certificate();
51
QVERIFY(certificate.isNull());
52
const auto xmlCertificate = KeeShareSettings::Certificate::deserialize(reader);
53
QVERIFY(xmlCertificate.isNull());
55
const auto own = KeeShareSettings::Own();
56
QVERIFY(own.isNull());
57
const auto xmlOwn = KeeShareSettings::Own::deserialize(empty);
58
QVERIFY(xmlOwn.isNull());
60
const auto active = KeeShareSettings::Active();
61
QVERIFY(active.isNull());
62
const auto xmlActive = KeeShareSettings::Active::deserialize(empty);
63
QVERIFY(xmlActive.isNull());
65
const auto reference = KeeShareSettings::Reference();
66
QVERIFY(reference.isNull());
67
const auto xmlReference = KeeShareSettings::Reference::deserialize(empty);
68
QVERIFY(xmlReference.isNull());
71
void TestSharing::testKeySerialization()
74
KeeShareSettings::Key original;
78
QXmlStreamWriter writer(&buffer);
79
writer.writeStartDocument();
80
writer.writeStartElement("Key");
81
KeeShareSettings::Key::serialize(writer, original);
82
writer.writeEndElement();
83
writer.writeEndDocument();
84
QXmlStreamReader reader(buffer);
85
reader.readNextStartElement();
86
QVERIFY(reader.name() == "Key");
87
KeeShareSettings::Key restored = KeeShareSettings::Key::deserialize(reader);
89
QCOMPARE(restored.key->private_key_bits(), original.key->private_key_bits());
90
QCOMPARE(restored.key->algo_name(), original.key->algo_name());
93
void TestSharing::testReferenceSerialization()
95
QFETCH(QString, password);
96
QFETCH(QString, path);
99
KeeShareSettings::Reference original;
100
original.password = password;
101
original.path = path;
102
original.uuid = uuid;
103
original.type = static_cast<KeeShareSettings::Type>(type);
105
const QString serialized = KeeShareSettings::Reference::serialize(original);
106
const KeeShareSettings::Reference restored = KeeShareSettings::Reference::deserialize(serialized);
108
QCOMPARE(restored.password, original.password);
109
QCOMPARE(restored.path, original.path);
110
QCOMPARE(restored.uuid, original.uuid);
111
QCOMPARE(int(restored.type), int(original.type));
114
void TestSharing::testReferenceSerialization_data()
116
QTest::addColumn<QString>("password");
117
QTest::addColumn<QString>("path");
118
QTest::addColumn<QUuid>("uuid");
119
QTest::addColumn<int>("type");
120
QTest::newRow("1") << "Password"
121
<< "/some/path" << QUuid::createUuid() << int(KeeShareSettings::Inactive);
122
QTest::newRow("2") << ""
123
<< "" << QUuid() << int(KeeShareSettings::SynchronizeWith);
124
QTest::newRow("3") << ""
125
<< "/some/path" << QUuid() << int(KeeShareSettings::ExportTo);
128
void TestSharing::testSettingsSerialization()
130
QFETCH(bool, importing);
131
QFETCH(bool, exporting);
132
QFETCH(KeeShareSettings::Certificate, ownCertificate);
133
QFETCH(KeeShareSettings::Key, ownKey);
135
KeeShareSettings::Own originalOwn;
136
KeeShareSettings::Active originalActive;
137
originalActive.in = importing;
138
originalActive.out = exporting;
139
originalOwn.certificate = ownCertificate;
140
originalOwn.key = ownKey;
142
const QString serializedActive = KeeShareSettings::Active::serialize(originalActive);
143
KeeShareSettings::Active restoredActive = KeeShareSettings::Active::deserialize(serializedActive);
145
const QString serializedOwn = KeeShareSettings::Own::serialize(originalOwn);
146
KeeShareSettings::Own restoredOwn = KeeShareSettings::Own::deserialize(serializedOwn);
148
QCOMPARE(restoredActive.in, importing);
149
QCOMPARE(restoredActive.out, exporting);
150
if (ownCertificate.key) {
151
QCOMPARE(restoredOwn.certificate, ownCertificate);
154
QCOMPARE(restoredOwn.key, ownKey);
158
void TestSharing::testSettingsSerialization_data()
160
auto sshKey0 = stubkey(0);
162
KeeShareSettings::Certificate certificate0;
163
certificate0.key = sshKey0;
164
certificate0.signer = "signer";
166
KeeShareSettings::Key key0;
169
QTest::addColumn<bool>("importing");
170
QTest::addColumn<bool>("exporting");
171
QTest::addColumn<KeeShareSettings::Certificate>("ownCertificate");
172
QTest::addColumn<KeeShareSettings::Key>("ownKey");
174
QTest::newRow("1") << false << false << KeeShareSettings::Certificate() << KeeShareSettings::Key();
175
QTest::newRow("2") << true << false << KeeShareSettings::Certificate() << KeeShareSettings::Key();
176
QTest::newRow("3") << true << true << KeeShareSettings::Certificate() << KeeShareSettings::Key();
177
QTest::newRow("4") << false << true << certificate0 << key0;
178
QTest::newRow("5") << false << false << certificate0 << key0;
181
const QSharedPointer<Botan::RSA_PrivateKey> TestSharing::stubkey(int index)
183
static QMap<int, QSharedPointer<Botan::RSA_PrivateKey>> keys;
184
if (!keys.contains(index)) {
186
QSharedPointer<Botan::RSA_PrivateKey>(new Botan::RSA_PrivateKey(*randomGen()->getRng(), 2048)));