| 1 | /** |
| 2 | * Copyright (C) 2005, 2006 Brad Hards <bradh@frogmouth.net> |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 16 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 17 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 18 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 19 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 20 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 21 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 23 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include <QTest> |
| 27 | #include <QtCrypto> |
| 28 | |
| 29 | #ifdef QT_STATICPLUGIN |
| 30 | #include "import_plugins.h" |
| 31 | #endif |
| 32 | |
| 33 | class DSAUnitTest : public QObject |
| 34 | { |
| 35 | Q_OBJECT |
| 36 | |
| 37 | private Q_SLOTS: |
| 38 | void initTestCase(); |
| 39 | void cleanupTestCase(); |
| 40 | void testdsa(); |
| 41 | |
| 42 | private: |
| 43 | QCA::Initializer *m_init; |
| 44 | }; |
| 45 | |
| 46 | void DSAUnitTest::initTestCase() |
| 47 | { |
| 48 | m_init = new QCA::Initializer; |
| 49 | } |
| 50 | |
| 51 | void DSAUnitTest::cleanupTestCase() |
| 52 | { |
| 53 | delete m_init; |
| 54 | } |
| 55 | |
| 56 | void DSAUnitTest::testdsa() |
| 57 | { |
| 58 | if (!QCA::isSupported(features: "pkey" ) || !QCA::PKey::supportedTypes().contains(t: QCA::PKey::DSA) || |
| 59 | !QCA::PKey::supportedIOTypes().contains(t: QCA::PKey::DSA)) { |
| 60 | QSKIP("DSA not supported!" ); |
| 61 | } |
| 62 | |
| 63 | if (!QCA::DLGroup::supportedGroupSets().contains(t: QCA::DSA_1024)) { |
| 64 | QSKIP("DSA_1024 discrete logarithm group sets not supported!" ); |
| 65 | } |
| 66 | |
| 67 | QCA::KeyGenerator keygen; |
| 68 | QCOMPARE(keygen.isBusy(), false); |
| 69 | QCOMPARE(keygen.blockingEnabled(), true); |
| 70 | QCA::DLGroup group = keygen.createDLGroup(set: QCA::DSA_1024); |
| 71 | QCOMPARE(group.isNull(), false); |
| 72 | |
| 73 | QCA::PrivateKey dsaKey = keygen.createDSA(domain: group); |
| 74 | QCOMPARE(dsaKey.isNull(), false); |
| 75 | QCOMPARE(dsaKey.isRSA(), false); |
| 76 | QCOMPARE(dsaKey.isDSA(), true); |
| 77 | QCOMPARE(dsaKey.isDH(), false); |
| 78 | QCOMPARE(dsaKey.isPrivate(), true); |
| 79 | QCOMPARE(dsaKey.isPublic(), false); |
| 80 | QCOMPARE(dsaKey.canSign(), true); |
| 81 | QCOMPARE(dsaKey.canDecrypt(), false); |
| 82 | |
| 83 | QCOMPARE(dsaKey.bitSize(), 1024); |
| 84 | QCA::DSAPrivateKey dsaPrivKey = dsaKey.toDSA(); |
| 85 | QCOMPARE(dsaPrivKey.bitSize(), 1024); |
| 86 | |
| 87 | QCA::SecureArray dsaDER = dsaKey.toDER(); |
| 88 | QCOMPARE(dsaDER.isEmpty(), false); |
| 89 | |
| 90 | QString dsaPEM = dsaKey.toPEM(); |
| 91 | QCOMPARE(dsaPEM.isEmpty(), false); |
| 92 | |
| 93 | QCA::ConvertResult checkResult; |
| 94 | QCA::PrivateKey fromPEMkey = QCA::PrivateKey::fromPEM(s: dsaPEM, passphrase: QCA::SecureArray(), result: &checkResult); |
| 95 | QCOMPARE(checkResult, QCA::ConvertGood); |
| 96 | QCOMPARE(fromPEMkey.isNull(), false); |
| 97 | QCOMPARE(fromPEMkey.isRSA(), false); |
| 98 | QCOMPARE(fromPEMkey.isDSA(), true); |
| 99 | QCOMPARE(fromPEMkey.isDH(), false); |
| 100 | QCOMPARE(fromPEMkey.isPrivate(), true); |
| 101 | QCOMPARE(fromPEMkey.isPublic(), false); |
| 102 | QVERIFY(dsaKey == fromPEMkey); |
| 103 | |
| 104 | QCA::PrivateKey fromDERkey = QCA::PrivateKey::fromDER(a: dsaDER, passphrase: QCA::SecureArray(), result: &checkResult); |
| 105 | QCOMPARE(checkResult, QCA::ConvertGood); |
| 106 | QCOMPARE(fromDERkey.isNull(), false); |
| 107 | QCOMPARE(fromDERkey.isRSA(), false); |
| 108 | QCOMPARE(fromDERkey.isDSA(), true); |
| 109 | QCOMPARE(fromDERkey.isDH(), false); |
| 110 | QCOMPARE(fromDERkey.isPrivate(), true); |
| 111 | QCOMPARE(fromDERkey.isPublic(), false); |
| 112 | QVERIFY(dsaKey == fromDERkey); |
| 113 | } |
| 114 | |
| 115 | QTEST_MAIN(DSAUnitTest) |
| 116 | |
| 117 | #include "dsaunittest.moc" |
| 118 | |