tests/auto/qsslkey/tst_qsslkey.cpp
changeset 7 f7bc934e204c
parent 0 1918ee327afb
--- a/tests/auto/qsslkey/tst_qsslkey.cpp	Tue Feb 02 00:43:10 2010 +0200
+++ b/tests/auto/qsslkey/tst_qsslkey.cpp	Wed Mar 31 11:06:36 2010 +0300
@@ -1,6 +1,6 @@
 /****************************************************************************
 **
-** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 ** All rights reserved.
 ** Contact: Nokia Corporation (qt-info@nokia.com)
 **
@@ -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)