mirror of
https://github.com/QuasarApp/qca.git
synced 2025-04-29 04:54:31 +00:00
to put the keys/certs back in. With that, it stops asserting. Then it just fails to compare the messages. If you encrypt the same data, with the same certs, twice, you don't get the same result each time. So I added the decrypt part, including re-use, and checked that if you encrypt the same data twice, then each decrypt produces the same result, and that result is exactly what you started with. The test now passes. svn path=/trunk/kdesupport/qca/; revision=529009
138 lines
4.1 KiB
C++
138 lines
4.1 KiB
C++
/**
|
|
* 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 "cms.h"
|
|
|
|
#include <QtCrypto>
|
|
|
|
void CMSut::initTestCase()
|
|
{
|
|
m_init = new QCA::Initializer;
|
|
#include "../fixpaths.include"
|
|
}
|
|
|
|
void CMSut::cleanupTestCase()
|
|
{
|
|
delete m_init;
|
|
}
|
|
|
|
void CMSut::xcrypt_data()
|
|
{
|
|
QTest::addColumn<QByteArray>("testText");
|
|
|
|
QTest::newRow("empty") << QByteArray("");
|
|
QTest::newRow("0") << QByteArray("0");
|
|
QTest::newRow("07") << QByteArray("07899847jkjjfasjaJKJLJkljklj&kjlj;/**-+.01");
|
|
QTest::newRow("dubious") << QByteArray("~!#**$#&&%^@#^&()");
|
|
}
|
|
|
|
void CMSut::xcrypt()
|
|
{
|
|
QStringList providersToTest;
|
|
providersToTest.append("qca-openssl");
|
|
|
|
foreach(const QString provider, providersToTest) {
|
|
if( !QCA::isSupported( "cert", provider ) )
|
|
QWARN( QString( "Certificate not supported for "+provider).toLocal8Bit() );
|
|
else if( !QCA::isSupported( "cms", provider ) )
|
|
QWARN( QString( "CMS not supported for "+provider).toLocal8Bit() );
|
|
else {
|
|
QCA::Certificate pubCert( "User.pem" );
|
|
QCOMPARE( pubCert.isNull(), false );
|
|
|
|
QCA::SecureMessageKey secMsgKey;
|
|
QCA::CertificateChain chain;
|
|
chain += pubCert;
|
|
secMsgKey.setX509CertificateChain( chain );
|
|
|
|
QCA::CMS cms;
|
|
QCA::SecureMessage msg(&cms);
|
|
QCOMPARE( msg.canClearsign(), false );
|
|
QCOMPARE( msg.canSignAndEncrypt(), false );
|
|
|
|
msg.setRecipient(secMsgKey);
|
|
|
|
QFETCH( QByteArray, testText );
|
|
|
|
msg.startEncrypt();
|
|
msg.update(testText);
|
|
msg.end();
|
|
|
|
msg.waitForFinished(-1);
|
|
|
|
QByteArray encryptedResult1 = msg.read();
|
|
QCOMPARE( encryptedResult1.isEmpty(), false );
|
|
|
|
msg.reset();
|
|
msg.setRecipient(secMsgKey);
|
|
msg.startEncrypt();
|
|
msg.update( testText );
|
|
msg.end();
|
|
|
|
msg.waitForFinished(-1);
|
|
QVERIFY( msg.success() );
|
|
|
|
QByteArray encryptedResult2 = msg.read();
|
|
QCOMPARE( encryptedResult2.isEmpty(), false );
|
|
|
|
QCA::ConvertResult res;
|
|
QSecureArray passPhrase = "start";
|
|
QCA::PrivateKey privKey = QCA::PrivateKey::fromPEMFile( "Userkey.pem", passPhrase, &res );
|
|
QCOMPARE( res, QCA::ConvertGood );
|
|
|
|
secMsgKey.setX509PrivateKey( privKey );
|
|
QCA::SecureMessageKeyList privKeyList;
|
|
privKeyList += secMsgKey;
|
|
QCA::CMS cms2;
|
|
cms2.setPrivateKeys( privKeyList );
|
|
|
|
QCA::SecureMessage msg2( &cms2 );
|
|
|
|
msg2.startDecrypt();
|
|
msg2.update( encryptedResult1 );
|
|
msg2.end();
|
|
msg2.waitForFinished(-1);
|
|
QVERIFY( msg2.success() );
|
|
QByteArray decryptedResult1 = msg2.read();
|
|
QCOMPARE( decryptedResult1, testText );
|
|
|
|
msg2.reset();
|
|
msg2.startDecrypt();
|
|
msg2.update( encryptedResult1 );
|
|
msg2.end();
|
|
msg2.waitForFinished(-1);
|
|
QVERIFY( msg2.success() );
|
|
QByteArray decryptedResult2 = msg2.read();
|
|
|
|
QCOMPARE( decryptedResult1, decryptedResult2 );
|
|
}
|
|
}
|
|
};
|
|
|
|
|
|
QTEST_MAIN(CMSut)
|
|
|
|
|