2006-10-19 06:00:57 +00:00
/**
* Copyright ( C ) 2006 Brad Hards < bradh @ frogmouth . net >
*
* Redistribution and use in source and binary forms , with or without
* modification , are permitted provided that the following conditions
* are met :
*
* 1. Redistributions of source code must retain the above copyright
* notice , this list of conditions and the following disclaimer .
* 2. Redistributions in binary form must reproduce the above copyright
* notice , this list of conditions and the following disclaimer in the
* documentation and / or other materials provided with the distribution .
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ` ` AS IS ' ' AND ANY EXPRESS OR
* IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED .
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT , INDIRECT ,
* INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES ( INCLUDING , BUT
* NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE ,
* DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT
* ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
*/
# include <QtCrypto>
# include <QtTest/QtTest>
2014-01-02 03:50:18 +06:00
# ifdef QT_STATICPLUGIN
# include "import_plugins.h"
# endif
2006-10-19 06:00:57 +00:00
class KeyBundleTest : public QObject
{
Q_OBJECT
2020-01-22 19:31:28 +01:00
private Q_SLOTS :
2006-10-19 06:00:57 +00:00
void initTestCase ( ) ;
void cleanupTestCase ( ) ;
void nullBundle ( ) ;
void fromFile ( ) ;
void names ( ) ;
void certChain ( ) ;
void privKey ( ) ;
void createBundle ( ) ;
private :
QCA : : Initializer * m_init ;
} ;
void KeyBundleTest : : initTestCase ( )
{
m_init = new QCA : : Initializer ;
}
void KeyBundleTest : : cleanupTestCase ( )
{
QCA : : unloadAllPlugins ( ) ;
delete m_init ;
}
void KeyBundleTest : : nullBundle ( )
{
QCA : : KeyBundle nullBundle ;
QVERIFY ( nullBundle . isNull ( ) ) ;
QCOMPARE ( nullBundle . name ( ) , QString ( ) ) ;
QVERIFY ( nullBundle . certificateChain ( ) . isEmpty ( ) ) ;
QVERIFY ( nullBundle . privateKey ( ) . isNull ( ) ) ;
2020-01-30 15:09:31 +01:00
QCA : : KeyBundle nullCopy = nullBundle ; // NOLINT(performance-unnecessary-copy-initialization) This is copied on purpose to check the assignment operator
2006-10-19 06:00:57 +00:00
QVERIFY ( nullCopy . isNull ( ) ) ;
QCOMPARE ( nullCopy . name ( ) , QString ( ) ) ;
QVERIFY ( nullCopy . certificateChain ( ) . isEmpty ( ) ) ;
QVERIFY ( nullCopy . privateKey ( ) . isNull ( ) ) ;
2020-01-30 15:09:31 +01:00
QCA : : KeyBundle nullAssigned ( nullCopy ) ; // NOLINT(performance-unnecessary-copy-initialization) This is copied on purpose to check the copy constructor
2006-10-19 06:00:57 +00:00
QVERIFY ( nullAssigned . isNull ( ) ) ;
QCOMPARE ( nullAssigned . name ( ) , QString ( ) ) ;
QVERIFY ( nullAssigned . certificateChain ( ) . isEmpty ( ) ) ;
QVERIFY ( nullAssigned . privateKey ( ) . isNull ( ) ) ;
}
void KeyBundleTest : : fromFile ( )
{
2006-10-19 09:51:21 +00:00
if ( QCA : : isSupported ( " pkcs12 " ) ) {
// "start" is the passphrase, but you wouldn't normally
// code it in like this
2020-02-10 00:29:23 +01:00
QCA : : KeyBundle userBundle ( QStringLiteral ( " user2good.p12 " ) , " start " ) ;
2006-10-19 09:51:21 +00:00
QCOMPARE ( userBundle . isNull ( ) , false ) ;
QCOMPARE ( userBundle . name ( ) , QString ( ) ) ;
QCOMPARE ( userBundle . certificateChain ( ) . isEmpty ( ) , false ) ;
QCOMPARE ( userBundle . privateKey ( ) . isNull ( ) , false ) ;
2007-04-18 11:21:04 +00:00
2020-01-30 15:09:31 +01:00
QCA : : KeyBundle userBundleCopy = userBundle ; // NOLINT(performance-unnecessary-copy-initialization) This is copied on purpose to check the assignment operator
2006-10-19 09:51:21 +00:00
QCOMPARE ( userBundleCopy . isNull ( ) , false ) ;
QCOMPARE ( userBundleCopy . name ( ) , QString ( ) ) ;
QCOMPARE ( userBundleCopy . certificateChain ( ) . isEmpty ( ) , false ) ;
QCOMPARE ( userBundleCopy . privateKey ( ) . isNull ( ) , false ) ;
2007-04-18 11:21:04 +00:00
2020-01-30 15:09:31 +01:00
QCA : : KeyBundle userBundleAssign ( userBundleCopy ) ; // NOLINT(performance-unnecessary-copy-initialization) This is copied on purpose to check the copy constructor
2006-10-19 09:51:21 +00:00
QCOMPARE ( userBundleAssign . isNull ( ) , false ) ;
QCOMPARE ( userBundleAssign . name ( ) , QString ( ) ) ;
QCOMPARE ( userBundleAssign . certificateChain ( ) . isEmpty ( ) , false ) ;
QCOMPARE ( userBundleAssign . privateKey ( ) . isNull ( ) , false ) ;
}
2006-10-19 06:00:57 +00:00
}
void KeyBundleTest : : names ( )
{
2006-10-19 09:51:21 +00:00
if ( QCA : : isSupported ( " pkcs12 " ) ) {
2020-02-10 00:29:23 +01:00
QCA : : KeyBundle serverBundle ( QStringLiteral ( " servergood2.p12 " ) , " start " ) ;
2006-10-19 09:51:21 +00:00
QCOMPARE ( serverBundle . isNull ( ) , false ) ;
QCOMPARE ( serverBundle . name ( ) , QString ( ) ) ;
2020-02-10 00:29:23 +01:00
serverBundle . setName ( QStringLiteral ( " Some Server Bundle " ) ) ;
QCOMPARE ( serverBundle . name ( ) , QStringLiteral ( " Some Server Bundle " ) ) ;
2006-10-19 09:51:21 +00:00
}
2006-10-19 06:00:57 +00:00
}
void KeyBundleTest : : certChain ( )
{
2006-10-19 09:51:21 +00:00
if ( QCA : : isSupported ( " pkcs12 " ) ) {
2020-02-10 00:29:23 +01:00
QCA : : KeyBundle serverBundle ( QStringLiteral ( " servergood2.p12 " ) , " start " ) ;
2006-10-19 09:51:21 +00:00
QCOMPARE ( serverBundle . isNull ( ) , false ) ;
QCOMPARE ( serverBundle . certificateChain ( ) . size ( ) , 1 ) ;
}
2006-10-19 06:00:57 +00:00
}
void KeyBundleTest : : privKey ( )
{
2006-10-19 09:51:21 +00:00
if ( QCA : : isSupported ( " pkcs12 " ) ) {
2020-02-10 00:29:23 +01:00
QCA : : KeyBundle serverBundle ( QStringLiteral ( " servergood2.p12 " ) , " start " ) ;
2006-10-19 09:51:21 +00:00
QCOMPARE ( serverBundle . isNull ( ) , false ) ;
QCOMPARE ( serverBundle . privateKey ( ) . isNull ( ) , false ) ;
}
2006-10-19 06:00:57 +00:00
}
void KeyBundleTest : : createBundle ( )
{
2020-06-22 21:54:12 +02:00
QScopedPointer < QCA : : KeyBundle > newBundle ( new QCA : : KeyBundle ) ;
2006-10-19 06:00:57 +00:00
QVERIFY ( newBundle - > isNull ( ) ) ;
2007-04-18 11:21:04 +00:00
if ( ! QCA : : isSupported ( " certificate " ) )
return ;
2020-02-10 00:29:23 +01:00
QCA : : Certificate ca ( QStringLiteral ( " RootCA2cert.pem " ) ) ;
2006-10-19 06:00:57 +00:00
QCOMPARE ( ca . isNull ( ) , false ) ;
2020-02-10 00:29:23 +01:00
QCA : : Certificate primary ( QStringLiteral ( " user2goodcert.pem " ) ) ;
2006-10-19 06:00:57 +00:00
QCOMPARE ( primary . isNull ( ) , false ) ;
2020-02-10 00:29:23 +01:00
QCA : : PrivateKey key ( QStringLiteral ( " user2goodkey.pem " ) ) ;
2006-10-19 06:00:57 +00:00
QCOMPARE ( key . isNull ( ) , false ) ;
QCA : : CertificateChain chain ( primary ) ;
chain . append ( ca ) ;
2007-04-18 11:21:04 +00:00
2006-10-19 06:00:57 +00:00
newBundle - > setCertificateChainAndKey ( chain , key ) ;
2020-02-10 00:29:23 +01:00
newBundle - > setName ( QStringLiteral ( " My New Key Bundle " ) ) ;
2006-10-19 06:00:57 +00:00
QCOMPARE ( newBundle - > certificateChain ( ) , chain ) ;
QCOMPARE ( newBundle - > privateKey ( ) , key ) ;
2020-02-10 00:29:23 +01:00
QCOMPARE ( newBundle - > name ( ) , QStringLiteral ( " My New Key Bundle " ) ) ;
2006-10-19 06:00:57 +00:00
// Try round tripping the bundle
foreach ( const QCA : : Provider * thisProvider , QCA : : providers ( ) ) {
QString provider = thisProvider - > name ( ) ;
2006-10-19 09:51:21 +00:00
if ( QCA : : isSupported ( " pkcs12 " , provider ) ) {
2006-10-19 06:00:57 +00:00
qDebug ( ) < < " Testing " < < provider ;
QByteArray bundleArray = newBundle - > toArray ( " reel secrut " , provider ) ;
QCOMPARE ( bundleArray . isNull ( ) , false ) ;
QCA : : ConvertResult res ;
QCA : : KeyBundle bundleFromArray = QCA : : KeyBundle : : fromArray ( bundleArray , " reel secrut " , & res , provider ) ;
QCOMPARE ( res , QCA : : ConvertGood ) ;
QCOMPARE ( bundleFromArray . isNull ( ) , false ) ;
2020-02-10 00:29:23 +01:00
QCOMPARE ( bundleFromArray . name ( ) , QStringLiteral ( " My New Key Bundle " ) ) ;
2006-10-19 06:00:57 +00:00
QCOMPARE ( bundleFromArray . certificateChain ( ) , chain ) ;
QCOMPARE ( bundleFromArray . privateKey ( ) , key ) ;
QTemporaryFile tempFile ;
QVERIFY ( tempFile . open ( ) ) ;
bool result = newBundle - > toFile ( tempFile . fileName ( ) , " file passphrase " , provider ) ;
QVERIFY ( result ) ;
QCA : : KeyBundle bundleFromFile = QCA : : KeyBundle : : fromFile ( tempFile . fileName ( ) , " file passphrase " , & res , provider ) ;
QCOMPARE ( res , QCA : : ConvertGood ) ;
QCOMPARE ( bundleFromFile . isNull ( ) , false ) ;
2020-02-10 00:29:23 +01:00
QCOMPARE ( bundleFromFile . name ( ) , QStringLiteral ( " My New Key Bundle " ) ) ;
2006-10-19 06:00:57 +00:00
QCOMPARE ( bundleFromFile . certificateChain ( ) , chain ) ;
QCOMPARE ( bundleFromFile . privateKey ( ) , key ) ;
}
}
2007-04-18 11:21:04 +00:00
}
2006-10-19 06:00:57 +00:00
QTEST_MAIN ( KeyBundleTest )
# include "keybundle.moc"