1 | /** |
2 | * Copyright (C) 2004-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 <QtCrypto> |
27 | #include <QtTest/QtTest> |
28 | |
29 | #ifdef QT_STATICPLUGIN |
30 | #include "import_plugins.h" |
31 | #endif |
32 | |
33 | class KeyGenUnitTest : public QObject |
34 | { |
35 | Q_OBJECT |
36 | |
37 | private Q_SLOTS: |
38 | void initTestCase(); |
39 | void cleanupTestCase(); |
40 | void testRSA(); |
41 | void testDSA(); |
42 | void testDH(); |
43 | |
44 | private: |
45 | QCA::Initializer *m_init; |
46 | }; |
47 | |
48 | void KeyGenUnitTest::initTestCase() |
49 | { |
50 | m_init = new QCA::Initializer; |
51 | } |
52 | |
53 | void KeyGenUnitTest::cleanupTestCase() |
54 | { |
55 | delete m_init; |
56 | } |
57 | |
58 | void KeyGenUnitTest::testRSA() |
59 | { |
60 | QCA::KeyGenerator keygen; |
61 | QCOMPARE(keygen.isBusy(), false); |
62 | QCOMPARE(keygen.blockingEnabled(), true); |
63 | |
64 | if (!QCA::isSupported(features: "pkey" ) || !QCA::PKey::supportedTypes().contains(t: QCA::PKey::RSA) || |
65 | !QCA::PKey::supportedIOTypes().contains(t: QCA::PKey::RSA)) |
66 | QSKIP("RSA not supported!" ); |
67 | |
68 | QCA::PrivateKey priv1 = keygen.createRSA(bits: 1024, exp: 65537); |
69 | QCA::RSAPrivateKey rsa1 = priv1.toRSA(); |
70 | QCOMPARE(rsa1.isNull(), false); |
71 | QCOMPARE(rsa1.e(), QCA::BigInteger(65537)); |
72 | QCOMPARE(rsa1.bitSize(), 1024); |
73 | |
74 | priv1 = keygen.createRSA(bits: 512, exp: 17); |
75 | rsa1 = priv1.toRSA(); |
76 | QCOMPARE(rsa1.isNull(), false); |
77 | QCOMPARE(rsa1.e(), QCA::BigInteger(17)); |
78 | QCOMPARE(rsa1.bitSize(), 512); |
79 | |
80 | priv1 = keygen.createRSA(bits: 512, exp: 3); |
81 | rsa1 = priv1.toRSA(); |
82 | QCOMPARE(rsa1.isNull(), false); |
83 | QCOMPARE(rsa1.e(), QCA::BigInteger(3)); |
84 | QCOMPARE(rsa1.bitSize(), 512); |
85 | } |
86 | |
87 | void KeyGenUnitTest::testDSA() |
88 | { |
89 | QCA::KeyGenerator keygen; |
90 | QCOMPARE(keygen.isBusy(), false); |
91 | QCOMPARE(keygen.blockingEnabled(), true); |
92 | |
93 | if (!QCA::isSupported(features: "pkey" ) || !QCA::PKey::supportedTypes().contains(t: QCA::PKey::DSA) || |
94 | !QCA::PKey::supportedIOTypes().contains(t: QCA::PKey::DSA)) |
95 | QSKIP("DSA not supported!" ); |
96 | |
97 | QCA::DLGroup group; |
98 | QCA::PrivateKey priv2; |
99 | QCA::DSAPrivateKey dsa1; |
100 | |
101 | if (QCA::DLGroup::supportedGroupSets().contains(t: QCA::DSA_512)) { |
102 | group = keygen.createDLGroup(set: QCA::DSA_512); |
103 | priv2 = keygen.createDSA(domain: group); |
104 | dsa1 = priv2.toDSA(); |
105 | QCOMPARE(dsa1.isNull(), false); |
106 | QCOMPARE(dsa1.bitSize(), 512); |
107 | } |
108 | |
109 | if (QCA::DLGroup::supportedGroupSets().contains(t: QCA::DSA_768)) { |
110 | group = keygen.createDLGroup(set: QCA::DSA_768); |
111 | priv2 = keygen.createDSA(domain: group); |
112 | dsa1 = priv2.toDSA(); |
113 | QCOMPARE(dsa1.isNull(), false); |
114 | QCOMPARE(dsa1.bitSize(), 768); |
115 | } |
116 | |
117 | if (QCA::DLGroup::supportedGroupSets().contains(t: QCA::DSA_1024)) { |
118 | group = keygen.createDLGroup(set: QCA::DSA_1024); |
119 | priv2 = keygen.createDSA(domain: group); |
120 | dsa1 = priv2.toDSA(); |
121 | QCOMPARE(dsa1.isNull(), false); |
122 | QCOMPARE(dsa1.bitSize(), 1024); |
123 | } |
124 | } |
125 | |
126 | void KeyGenUnitTest::testDH() |
127 | { |
128 | QCA::KeyGenerator keygen; |
129 | QCOMPARE(keygen.isBusy(), false); |
130 | QCOMPARE(keygen.blockingEnabled(), true); |
131 | |
132 | if (!QCA::isSupported(features: "pkey" ) || !QCA::PKey::supportedTypes().contains(t: QCA::PKey::DH) || |
133 | !QCA::PKey::supportedIOTypes().contains(t: QCA::PKey::DH)) |
134 | QSKIP("DH not supported!" ); |
135 | |
136 | QCA::DLGroup group = keygen.createDLGroup(set: QCA::IETF_1024); |
137 | QCA::PrivateKey priv3 = keygen.createDH(domain: group); |
138 | QCA::DHPrivateKey dh1 = priv3.toDH(); |
139 | QCOMPARE(dh1.isNull(), false); |
140 | QCOMPARE(dh1.bitSize(), 1024); |
141 | |
142 | group = keygen.createDLGroup(set: QCA::IETF_2048); |
143 | priv3 = keygen.createDH(domain: group); |
144 | dh1 = priv3.toDH(); |
145 | QCOMPARE(dh1.isNull(), false); |
146 | QCOMPARE(dh1.bitSize(), 2048); |
147 | } |
148 | |
149 | QTEST_MAIN(KeyGenUnitTest) |
150 | |
151 | #include "keygenunittest.moc" |
152 | |