AusweisApp2/test/qt/securestorage/test_TlsConfiguration.cpp

163 lines
4.7 KiB
C++
Raw Normal View History

2017-07-03 09:33:28 +02:00
/*!
2017-12-20 14:54:05 +01:00
* \brief Unit tests for \ref TlsConfiguration
2017-07-03 09:33:28 +02:00
*
2018-03-28 15:10:51 +02:00
* \copyright Copyright (c) 2016-2018 Governikus GmbH & Co. KG, Germany
2017-07-03 09:33:28 +02:00
*/
2017-12-20 14:54:05 +01:00
#include "TlsConfiguration.h"
2017-07-03 09:33:28 +02:00
#include <QtCore>
#include <QtTest>
using namespace governikus;
2017-12-20 14:54:05 +01:00
class test_TlsConfiguration
2017-07-03 09:33:28 +02:00
: public QObject
{
Q_OBJECT
2017-12-20 14:54:05 +01:00
TlsConfiguration mTlsConfiguration;
2017-07-03 09:33:28 +02:00
private Q_SLOTS:
void initTestCase()
{
2017-12-20 14:54:05 +01:00
mTlsConfiguration = TlsConfiguration();
2017-07-03 09:33:28 +02:00
}
void testDefaults()
{
QByteArray config("{}");
2017-12-20 14:54:05 +01:00
mTlsConfiguration.load(QJsonDocument::fromJson(config).object());
2017-07-03 09:33:28 +02:00
2017-12-20 14:54:05 +01:00
QCOMPARE(mTlsConfiguration.getProtocolVersion(), QSsl::SecureProtocols);
QCOMPARE(mTlsConfiguration.getCiphers().size(), 0);
QCOMPARE(mTlsConfiguration.getEllipticCurves().size(), 0);
QCOMPARE(mTlsConfiguration.getSignatureAlgorithms().size(), 0);
2017-07-03 09:33:28 +02:00
}
void testLoadprotocolVersion()
{
QByteArray config("{"
" \"protocolVersion\": \"TlsV1_0OrLater\""
"}");
2017-12-20 14:54:05 +01:00
mTlsConfiguration.load(QJsonDocument::fromJson(config).object());
2017-07-03 09:33:28 +02:00
2017-12-20 14:54:05 +01:00
QCOMPARE(mTlsConfiguration.getProtocolVersion(), QSsl::TlsV1_0OrLater);
2017-07-03 09:33:28 +02:00
}
void testLoadCiphers()
{
QByteArray config("{"
" \"ciphers\": [\"ECDHE-ECDSA-AES256-GCM-SHA384\",\"DHE-RSA-AES256-SHA256\"]"
"}");
2017-12-20 14:54:05 +01:00
mTlsConfiguration.load(QJsonDocument::fromJson(config).object());
2017-07-03 09:33:28 +02:00
2017-12-20 14:54:05 +01:00
QCOMPARE(mTlsConfiguration.getCiphers().size(), 2);
QCOMPARE(mTlsConfiguration.getCiphers()[0], QSslCipher("ECDHE-ECDSA-AES256-GCM-SHA384"));
QCOMPARE(mTlsConfiguration.getCiphers()[1], QSslCipher("DHE-RSA-AES256-SHA256"));
2017-07-03 09:33:28 +02:00
}
void testLoadEllipticCurves()
{
QByteArray config("{"
" \"ellipticCurves\": [\"brainpoolP512r1\", \"brainpoolP384r1\"]"
"}");
2017-12-20 14:54:05 +01:00
mTlsConfiguration.load(QJsonDocument::fromJson(config).object());
2017-07-03 09:33:28 +02:00
2017-12-20 14:54:05 +01:00
QCOMPARE(mTlsConfiguration.getEllipticCurves().size(), 2);
QCOMPARE(mTlsConfiguration.getEllipticCurves()[0], QSslEllipticCurve::fromLongName("brainpoolP512r1"));
QCOMPARE(mTlsConfiguration.getEllipticCurves()[1], QSslEllipticCurve::fromLongName("brainpoolP384r1"));
2017-07-03 09:33:28 +02:00
}
void testLoadSignatureAlgorithms()
{
2017-12-20 14:54:05 +01:00
#ifndef GOVERNIKUS_QT
QSKIP("SignatureAlgorithms not supported");
#endif
2017-07-03 09:33:28 +02:00
QByteArray config("{"
" \"signatureAlgorithms\": [\"Rsa+Sha512\", \"Dsa+Sha384\", \"Ec+Sha256\"]"
"}");
2017-12-20 14:54:05 +01:00
mTlsConfiguration.load(QJsonDocument::fromJson(config).object());
2017-07-03 09:33:28 +02:00
2017-12-20 14:54:05 +01:00
QCOMPARE(mTlsConfiguration.getSignatureAlgorithms().size(), 3);
QCOMPARE(mTlsConfiguration.getSignatureAlgorithms()[0].first, QSsl::Rsa);
QCOMPARE(mTlsConfiguration.getSignatureAlgorithms()[0].second, QCryptographicHash::Sha512);
QCOMPARE(mTlsConfiguration.getSignatureAlgorithms()[1].first, QSsl::Dsa);
QCOMPARE(mTlsConfiguration.getSignatureAlgorithms()[1].second, QCryptographicHash::Sha384);
QCOMPARE(mTlsConfiguration.getSignatureAlgorithms()[2].first, QSsl::Ec);
QCOMPARE(mTlsConfiguration.getSignatureAlgorithms()[2].second, QCryptographicHash::Sha256);
2017-07-03 09:33:28 +02:00
}
void testEquals_data()
{
QTest::addColumn<QByteArray>("config1");
QTest::addColumn<QByteArray>("config2");
QTest::addColumn<bool>("isEqual");
QByteArrayList configs({"",
"{"
" \"protocolVersion\": \"TlsV1_0OrLater\""
"}",
"{"
" \"protocolVersion\": \"TlsV1_0OrLater\","
" \"ciphers\": [\"ECDHE-ECDSA-AES256-GCM-SHA384\",\"DHE-RSA-AES256-SHA256\"]"
"}",
"{"
" \"protocolVersion\": \"TlsV1_0OrLater\","
" \"ciphers\": [\"ECDHE-ECDSA-AES256-GCM-SHA384\",\"DHE-RSA-AES256-SHA256\"],"
" \"ellipticCurves\": [\"brainpoolP512r1\", \"brainpoolP384r1\"]"
"}",
"{"
" \"protocolVersion\": \"TlsV1_0OrLater\","
" \"ciphers\": [\"ECDHE-ECDSA-AES256-GCM-SHA384\",\"DHE-RSA-AES256-SHA256\"],"
" \"ellipticCurves\": [\"brainpoolP512r1\", \"brainpoolP384r1\"],"
2017-12-20 14:54:05 +01:00
#ifdef GOVERNIKUS_QT
2017-07-03 09:33:28 +02:00
" \"signatureAlgorithms\": [\"Rsa+Sha512\", \"Dsa+Sha384\", \"Ec+Sha256\"]"
2017-12-20 14:54:05 +01:00
#endif
2017-07-03 09:33:28 +02:00
"}"});
for (int i = 0; i < configs.size(); ++i)
{
if (i > 0)
{
const auto& name1 = QStringLiteral("config%1 != config%2").arg(i - 1).arg(i).toLatin1();
QTest::newRow(name1.data()) << configs[i - 1] << configs[i] << false;
}
const auto& name2 = QStringLiteral("config%1 == config%1").arg(i).toLatin1();
QTest::newRow(name2.data()) << configs[i] << configs[i] << true;
}
}
void testEquals()
{
QFETCH(QByteArray, config1);
QFETCH(QByteArray, config2);
QFETCH(bool, isEqual);
2017-12-20 14:54:05 +01:00
TlsConfiguration settings1, settings2;
2017-07-03 09:33:28 +02:00
settings1.load(QJsonDocument::fromJson(config1).object());
settings2.load(QJsonDocument::fromJson(config2).object());
QCOMPARE(settings1 == settings2, isEqual);
}
};
2017-12-20 14:54:05 +01:00
QTEST_GUILESS_MAIN(test_TlsConfiguration)
#include "test_TlsConfiguration.moc"