diff options
author | Felix Geyer <debfx@fobos.de> | 2010-09-11 21:49:30 +0400 |
---|---|---|
committer | Felix Geyer <debfx@fobos.de> | 2010-09-11 21:49:30 +0400 |
commit | 6a2034fa248385d1d1637d4c3444045e50e0c5f5 (patch) | |
tree | 7526ece1e918ea4daa1de072bab37e8d99489980 /tests | |
parent | f5dd24fdbecb94f07398bacbe8fd905b5c83f806 (diff) |
Add crypto classes and tests. Link to libgcrypt.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/CMakeLists.txt | 16 | ||||
-rw-r--r-- | tests/TestCryptoHash.cpp | 59 | ||||
-rw-r--r-- | tests/TestSymmetricCipher.cpp | 52 |
3 files changed, 123 insertions, 4 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 55523bdaa..04fe54b4d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -63,14 +63,22 @@ macro (ADD_UNIT_TEST _test_NAME) endmacro (ADD_UNIT_TEST) +# TODO automocify? + add_unit_test( testgroup TestGroup.cpp ) -target_link_libraries( testgroup keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} ) +target_link_libraries( testgroup keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} ${LIBGCRYPT_LIBS} ) add_unit_test( testkeepass2xmlreader TestKeePass2XmlReader.cpp ) -target_link_libraries( testkeepass2xmlreader keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} ) +target_link_libraries( testkeepass2xmlreader keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} ${LIBGCRYPT_LIBS} ) add_unit_test( testgroupmodel TestGroupModel.cpp modeltest.cpp ) -target_link_libraries( testgroupmodel keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} ) +target_link_libraries( testgroupmodel keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} ${LIBGCRYPT_LIBS} ) add_unit_test( testentrymodel TestEntryModel.cpp modeltest.cpp ) -target_link_libraries( testentrymodel keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} ) +target_link_libraries( testentrymodel keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} ${LIBGCRYPT_LIBS} ) + +add_unit_test( testcryptohash TestCryptoHash.cpp ) +target_link_libraries( testcryptohash keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} ${LIBGCRYPT_LIBS} ) + +add_unit_test( testsymmetriccipher TestSymmetricCipher.cpp ) +target_link_libraries( testsymmetriccipher keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTTEST_LIBRARY} ${LIBGCRYPT_LIBS} ) diff --git a/tests/TestCryptoHash.cpp b/tests/TestCryptoHash.cpp new file mode 100644 index 000000000..e81d82313 --- /dev/null +++ b/tests/TestCryptoHash.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2010 Felix Geyer <debfx@fobos.de> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <QtTest/QTest> + +#include "crypto/Crypto.h" +#include "crypto/CryptoHash.h" + +class TestCryptoHash : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void initTestCase(); + void test(); +}; + +void TestCryptoHash::initTestCase() +{ + Crypto::init(); +} + +void TestCryptoHash::test() +{ + // TODO move somewhere else + QVERIFY(Crypto::selfTest()); + + CryptoHash cryptoHash1(CryptoHash::Sha256); + QCOMPARE(QString(cryptoHash1.result().toHex()), + QString("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")); + + QByteArray source2 = QString("KeePassX").toAscii(); + QString result2 = CryptoHash::hash(source2, CryptoHash::Sha256).toHex(); + QCOMPARE(result2, QString("0b56e5f65263e747af4a833bd7dd7ad26a64d7a4de7c68e52364893dca0766b4")); + + CryptoHash cryptoHash3(CryptoHash::Sha256); + cryptoHash3.addData(QString("KeePa").toAscii()); + cryptoHash3.addData(QString("ssX").toAscii()); + QCOMPARE(QString(cryptoHash3.result().toHex()), + QString("0b56e5f65263e747af4a833bd7dd7ad26a64d7a4de7c68e52364893dca0766b4")); +} + +QTEST_MAIN(TestCryptoHash); + +#include "TestCryptoHash.moc" diff --git a/tests/TestSymmetricCipher.cpp b/tests/TestSymmetricCipher.cpp new file mode 100644 index 000000000..ed45c9dbb --- /dev/null +++ b/tests/TestSymmetricCipher.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2010 Felix Geyer <debfx@fobos.de> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <QtTest/QTest> + +#include "crypto/Crypto.h" +#include "crypto/SymmetricCipher.h" + +class TestSymmetricCipher : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void initTestCase(); + void testAes256CbcEncryption(); +}; + +void TestSymmetricCipher::initTestCase() +{ + Crypto::init(); +} + +void TestSymmetricCipher::testAes256CbcEncryption() +{ + // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf + + QByteArray key = QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4"); + QByteArray iv = QByteArray::fromHex("000102030405060708090a0b0c0d0e0f"); + QByteArray plain = QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172a"); + + SymmetricCipher encrypt(SymmetricCipher::Aes256, SymmetricCipher::Cbc, SymmetricCipher::Encrypt, key, iv); + QCOMPARE(QString(encrypt.process(plain).toHex()), + QString("f58c4c04d6e5f1ba779eabfb5f7bfbd6")); +} + +QTEST_MAIN(TestSymmetricCipher); + +#include "TestSymmetricCipher.moc" |