1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | |
30 | #include <QtTest/QtTest> |
31 | |
32 | #include <QtNetwork/qtnetworkglobal.h> |
33 | |
34 | #include <qsslcertificate.h> |
35 | #include <qsslerror.h> |
36 | |
37 | #include <QtCore/qmetaobject.h> |
38 | #include <QtCore/qbytearray.h> |
39 | #include <QtCore/qstring.h> |
40 | #include <QtCore/qset.h> |
41 | |
42 | QT_USE_NAMESPACE |
43 | |
44 | const QByteArray certificateBytes = |
45 | "-----BEGIN CERTIFICATE-----\n" |
46 | "MIIEjjCCBDOgAwIBAgIQCQsKtxCf9ik3vIVQ+PMa5TAKBggqhkjOPQQDAjBKMQsw\n" |
47 | "CQYDVQQGEwJVUzEZMBcGA1UEChMQQ2xvdWRmbGFyZSwgSW5jLjEgMB4GA1UEAxMX\n" |
48 | "Q2xvdWRmbGFyZSBJbmMgRUNDIENBLTMwHhcNMjAwODE2M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BBAYKKwYBBAHWeQIEAgSB9QSB8gDwAHYA9lyUL9F3MCIUVBgIMJRWjuNN\n" |
64 | "Exkzv98MLyALzE7xZOMAAAFz90PlSQAABAMARzBFAiAhrrtxdmuxpCy8HAJJ5Qkg\n" |
65 | "WNvlo8nZqfe6pqGUcz0dmwIhAOMqDtd5ZhcfRk96GAJxPm8bH4hDnmqDP/zJG2Mq\n" |
66 | "nFpMAHYAXNxDkv7mq0VEsV6a1FbmEDf71fpH3KFzlLJe5vbHDsoAAAFz90PlewAA\n" |
67 | "BAMARzBFAiB/EkdY10LDdaRcf6eSc/QxucxU+2PI+3pWjh/21A8ZUAIhAK2Qz9Kw\n" |
68 | "onlRNyHpV3E6qyVydkXihj3c3q5UclpURYcmMAoGCCqGSM49BAMCA0kAMEYCIQDz\n" |
69 | "K/lzLb2Rbeg1HErRLLm2HkJUmfOGU2+tbROSTGK8ugIhAKA+MKqaZ8VjPxQ+Ho4v\n" |
70 | "fuwccvZfkU/fg8tAHTOzX23v\n" |
71 | "-----END CERTIFICATE-----" ; |
72 | |
73 | class tst_QSslError : public QObject |
74 | { |
75 | Q_OBJECT |
76 | private slots: |
77 | void constructing(); |
78 | void nonDefaultConstructors(); |
79 | void hash(); |
80 | }; |
81 | |
82 | void tst_QSslError::constructing() |
83 | { |
84 | const QSslError error; |
85 | QCOMPARE(error.error(), QSslError::NoError); |
86 | QCOMPARE(error.errorString(), QStringLiteral("No error" )); |
87 | QVERIFY(error.certificate().isNull()); |
88 | } |
89 | |
90 | void tst_QSslError::nonDefaultConstructors() |
91 | { |
92 | if (!QSslSocket::supportsSsl()) |
93 | QSKIP("This test requires a working TLS library" ); |
94 | |
95 | const auto chain = QSslCertificate::fromData(data: certificateBytes); |
96 | QCOMPARE(chain.size(), 1); |
97 | const auto certificate = chain.at(i: 0); |
98 | QVERIFY(!certificate.isNull()); |
99 | |
100 | const auto visitor = QSslError::staticMetaObject; |
101 | const int nEnums = visitor.enumeratorCount(); |
102 | QMetaEnum errorCodesEnum; |
103 | for (int i = 0; i < nEnums; ++i) { |
104 | const auto metaEnum = visitor.enumerator(index: i); |
105 | if (metaEnum.enumName() == QStringLiteral("SslError" )) { |
106 | errorCodesEnum = metaEnum; |
107 | break; |
108 | } |
109 | } |
110 | |
111 | QCOMPARE(errorCodesEnum.enumName(), QStringLiteral("SslError" )); |
112 | for (int i = 0, e = errorCodesEnum.keyCount(); i < e; ++i) { |
113 | const int value = errorCodesEnum.value(index: i); |
114 | if (value == -1) { |
115 | QVERIFY(i); |
116 | break; |
117 | } |
118 | const auto errorCode = QSslError::SslError(value); |
119 | QSslError error(errorCode); |
120 | |
121 | const auto basicChecks = [](const QSslError &err, QSslError::SslError code) { |
122 | QCOMPARE(err.error(), code); |
123 | const auto errorString = err.errorString(); |
124 | if (code == QSslError::NoError) |
125 | QCOMPARE(errorString, QStringLiteral("No error" )); |
126 | else |
127 | QVERIFY(errorString != QStringLiteral("No error" )); |
128 | }; |
129 | |
130 | basicChecks(error, errorCode); |
131 | |
132 | // ;) |
133 | error = QSslError(errorCode, certificate); |
134 | |
135 | basicChecks(error, errorCode); |
136 | QVERIFY(!error.certificate().isNull()); |
137 | } |
138 | } |
139 | |
140 | void tst_QSslError::hash() |
141 | { |
142 | // mostly a compile-only test, to check that qHash(QSslError) is found |
143 | QSet<QSslError> errors; |
144 | errors << QSslError(); |
145 | QCOMPARE(errors.size(), 1); |
146 | } |
147 | |
148 | QTEST_MAIN(tst_QSslError) |
149 | #include "tst_qsslerror.moc" |
150 | |