tests/auto/qsslkey/tst_qsslkey.cpp
changeset 19 fcece45ef507
parent 18 2f34d5167611
child 29 b72c6db6890b
--- a/tests/auto/qsslkey/tst_qsslkey.cpp	Fri Apr 16 15:50:13 2010 +0300
+++ b/tests/auto/qsslkey/tst_qsslkey.cpp	Mon May 03 13:17:34 2010 +0300
@@ -99,6 +99,7 @@
     void toEncryptedPemOrDer_data();
     void toEncryptedPemOrDer();
 
+    void passphraseChecks();
 #endif
 };
 
@@ -371,6 +372,77 @@
     // ### add a test to verify that public keys are _decrypted_ correctly (by the ctor)
 }
 
+void tst_QSslKey::passphraseChecks()
+{
+    {
+        QString fileName(SRCDIR "/rsa-with-passphrase.pem");
+        QFile keyFile(fileName);
+        QVERIFY(keyFile.exists());
+        {
+            if (!keyFile.isOpen())
+                keyFile.open(QIODevice::ReadOnly);
+            else
+                keyFile.reset();
+            QSslKey key(&keyFile,QSsl::Rsa,QSsl::Pem, QSsl::PrivateKey);
+            QVERIFY(key.isNull()); // null passphrase => should not be able to decode key
+        }
+        {
+            if (!keyFile.isOpen())
+                keyFile.open(QIODevice::ReadOnly);
+            else
+                keyFile.reset();
+            QSslKey key(&keyFile,QSsl::Rsa,QSsl::Pem, QSsl::PrivateKey, "");
+            QVERIFY(key.isNull()); // empty passphrase => should not be able to decode key
+        }
+        {
+            if (!keyFile.isOpen())
+                keyFile.open(QIODevice::ReadOnly);
+            else
+                keyFile.reset();
+            QSslKey key(&keyFile,QSsl::Rsa,QSsl::Pem, QSsl::PrivateKey, "WRONG!");
+            QVERIFY(key.isNull()); // wrong passphrase => should not be able to decode key
+        }
+        {
+            if (!keyFile.isOpen())
+                keyFile.open(QIODevice::ReadOnly);
+            else
+                keyFile.reset();
+            QSslKey key(&keyFile,QSsl::Rsa,QSsl::Pem, QSsl::PrivateKey, "123");
+            QVERIFY(!key.isNull()); // correct passphrase
+        }
+    }
+
+    {
+        // be sure and check a key without passphrase too
+        QString fileName(SRCDIR "/rsa-without-passphrase.pem");
+        QFile keyFile(fileName);
+        {
+            if (!keyFile.isOpen())
+                keyFile.open(QIODevice::ReadOnly);
+            else
+                keyFile.reset();
+            QSslKey key(&keyFile,QSsl::Rsa,QSsl::Pem, QSsl::PrivateKey);
+            QVERIFY(!key.isNull()); // null passphrase => should be able to decode key
+        }
+        {
+            if (!keyFile.isOpen())
+                keyFile.open(QIODevice::ReadOnly);
+            else
+                keyFile.reset();
+            QSslKey key(&keyFile,QSsl::Rsa,QSsl::Pem, QSsl::PrivateKey, "");
+            QVERIFY(!key.isNull()); // empty passphrase => should be able to decode key
+        }
+        {
+            if (!keyFile.isOpen())
+                keyFile.open(QIODevice::ReadOnly);
+            else
+                keyFile.reset();
+            QSslKey key(&keyFile,QSsl::Rsa,QSsl::Pem, QSsl::PrivateKey, "xxx");
+            QVERIFY(!key.isNull()); // passphrase given but key is not encrypted anyway => should work
+        }
+    }
+}
+
 #endif
 
 QTEST_MAIN(tst_QSslKey)