keepassxc

Форк
0
/
TestSharing.cpp 
189 строк · 6.4 Кб
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

18
#include "TestSharing.h"
19

20
#include <QTest>
21
#include <QXmlStreamReader>
22

23
#include "crypto/Crypto.h"
24
#include "crypto/Random.h"
25
#include "keeshare/KeeShareSettings.h"
26

27
#include <botan/rsa.h>
28

29
QTEST_GUILESS_MAIN(TestSharing)
30

31
Q_DECLARE_METATYPE(KeeShareSettings::Type)
32
Q_DECLARE_METATYPE(KeeShareSettings::Key)
33
Q_DECLARE_METATYPE(KeeShareSettings::Certificate)
34

35
void TestSharing::initTestCase()
36
{
37
    QVERIFY(Crypto::init());
38
}
39

40
void TestSharing::testNullObjects()
41
{
42
    const QString empty;
43
    QXmlStreamReader reader(empty);
44

45
    const auto nullKey = KeeShareSettings::Key();
46
    QVERIFY(nullKey.isNull());
47
    const auto xmlKey = KeeShareSettings::Key::deserialize(reader);
48
    QVERIFY(xmlKey.isNull());
49

50
    const auto certificate = KeeShareSettings::Certificate();
51
    QVERIFY(certificate.isNull());
52
    const auto xmlCertificate = KeeShareSettings::Certificate::deserialize(reader);
53
    QVERIFY(xmlCertificate.isNull());
54

55
    const auto own = KeeShareSettings::Own();
56
    QVERIFY(own.isNull());
57
    const auto xmlOwn = KeeShareSettings::Own::deserialize(empty);
58
    QVERIFY(xmlOwn.isNull());
59

60
    const auto active = KeeShareSettings::Active();
61
    QVERIFY(active.isNull());
62
    const auto xmlActive = KeeShareSettings::Active::deserialize(empty);
63
    QVERIFY(xmlActive.isNull());
64

65
    const auto reference = KeeShareSettings::Reference();
66
    QVERIFY(reference.isNull());
67
    const auto xmlReference = KeeShareSettings::Reference::deserialize(empty);
68
    QVERIFY(xmlReference.isNull());
69
}
70

71
void TestSharing::testKeySerialization()
72
{
73
    auto key = stubkey();
74
    KeeShareSettings::Key original;
75
    original.key = key;
76

77
    QString buffer;
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);
88

89
    QCOMPARE(restored.key->private_key_bits(), original.key->private_key_bits());
90
    QCOMPARE(restored.key->algo_name(), original.key->algo_name());
91
}
92

93
void TestSharing::testReferenceSerialization()
94
{
95
    QFETCH(QString, password);
96
    QFETCH(QString, path);
97
    QFETCH(QUuid, uuid);
98
    QFETCH(int, type);
99
    KeeShareSettings::Reference original;
100
    original.password = password;
101
    original.path = path;
102
    original.uuid = uuid;
103
    original.type = static_cast<KeeShareSettings::Type>(type);
104

105
    const QString serialized = KeeShareSettings::Reference::serialize(original);
106
    const KeeShareSettings::Reference restored = KeeShareSettings::Reference::deserialize(serialized);
107

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));
112
}
113

114
void TestSharing::testReferenceSerialization_data()
115
{
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);
126
}
127

128
void TestSharing::testSettingsSerialization()
129
{
130
    QFETCH(bool, importing);
131
    QFETCH(bool, exporting);
132
    QFETCH(KeeShareSettings::Certificate, ownCertificate);
133
    QFETCH(KeeShareSettings::Key, ownKey);
134

135
    KeeShareSettings::Own originalOwn;
136
    KeeShareSettings::Active originalActive;
137
    originalActive.in = importing;
138
    originalActive.out = exporting;
139
    originalOwn.certificate = ownCertificate;
140
    originalOwn.key = ownKey;
141

142
    const QString serializedActive = KeeShareSettings::Active::serialize(originalActive);
143
    KeeShareSettings::Active restoredActive = KeeShareSettings::Active::deserialize(serializedActive);
144

145
    const QString serializedOwn = KeeShareSettings::Own::serialize(originalOwn);
146
    KeeShareSettings::Own restoredOwn = KeeShareSettings::Own::deserialize(serializedOwn);
147

148
    QCOMPARE(restoredActive.in, importing);
149
    QCOMPARE(restoredActive.out, exporting);
150
    if (ownCertificate.key) {
151
        QCOMPARE(restoredOwn.certificate, ownCertificate);
152
    }
153
    if (ownKey.key) {
154
        QCOMPARE(restoredOwn.key, ownKey);
155
    }
156
}
157

158
void TestSharing::testSettingsSerialization_data()
159
{
160
    auto sshKey0 = stubkey(0);
161

162
    KeeShareSettings::Certificate certificate0;
163
    certificate0.key = sshKey0;
164
    certificate0.signer = "signer";
165

166
    KeeShareSettings::Key key0;
167
    key0.key = sshKey0;
168

169
    QTest::addColumn<bool>("importing");
170
    QTest::addColumn<bool>("exporting");
171
    QTest::addColumn<KeeShareSettings::Certificate>("ownCertificate");
172
    QTest::addColumn<KeeShareSettings::Key>("ownKey");
173

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;
179
}
180

181
const QSharedPointer<Botan::RSA_PrivateKey> TestSharing::stubkey(int index)
182
{
183
    static QMap<int, QSharedPointer<Botan::RSA_PrivateKey>> keys;
184
    if (!keys.contains(index)) {
185
        keys.insert(index,
186
                    QSharedPointer<Botan::RSA_PrivateKey>(new Botan::RSA_PrivateKey(*randomGen()->getRng(), 2048)));
187
    }
188
    return keys[index];
189
}
190

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.