File indexing completed on 2025-01-19 04:22:26
0001 /** 0002 * base64unittest.cpp 0003 * 0004 * Copyright (C) 2004-2006 Brad Hards <bradh@frogmouth.net> 0005 * 0006 * Redistribution and use in source and binary forms, with or without 0007 * modification, are permitted provided that the following conditions 0008 * are met: 0009 * 0010 * 1. Redistributions of source code must retain the above copyright 0011 * notice, this list of conditions and the following disclaimer. 0012 * 2. Redistributions in binary form must reproduce the above copyright 0013 * notice, this list of conditions and the following disclaimer in the 0014 * documentation and/or other materials provided with the distribution. 0015 * 0016 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 0017 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 0018 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 0019 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 0020 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 0021 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 0022 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 0023 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 0024 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 0025 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0026 */ 0027 0028 #include <QtCrypto> 0029 #include <QtTest/QtTest> 0030 0031 #ifdef QT_STATICPLUGIN 0032 #include "import_plugins.h" 0033 #endif 0034 0035 class Base64UnitTest : public QObject 0036 { 0037 Q_OBJECT 0038 0039 private Q_SLOTS: 0040 void initTestCase(); 0041 void cleanupTestCase(); 0042 void test1_data(); 0043 void test1(); 0044 void test2_data(); 0045 void test2(); 0046 0047 private: 0048 QCA::Initializer *m_init; 0049 }; 0050 0051 void Base64UnitTest::initTestCase() 0052 { 0053 m_init = new QCA::Initializer; 0054 } 0055 0056 void Base64UnitTest::cleanupTestCase() 0057 { 0058 delete m_init; 0059 } 0060 0061 void Base64UnitTest::test1_data() 0062 { 0063 QTest::addColumn<QString>("raw"); 0064 QTest::addColumn<QString>("encoded"); 0065 0066 // these are from the Botan test suite. Note that these are hex encoded! 0067 QTest::newRow("31") << QStringLiteral("31") << QStringLiteral("4d513d3d"); 0068 QTest::newRow("235c91") << QStringLiteral("235c91") << QStringLiteral("49317952"); 0069 QTest::newRow("414") << QStringLiteral("4142634452313236") << QStringLiteral("51554a6a524649784d6a593d"); 0070 QTest::newRow("241") << QStringLiteral("241bb300a3989a620659") 0071 << QStringLiteral("4a42757a414b4f596d6d494757513d3d"); 0072 QTest::newRow("313") << QStringLiteral("31323537374343666671333435337836") 0073 << QStringLiteral("4d5449314e7a644451325a6d63544d304e544e344e673d3d"); 0074 QTest::newRow("60e") << QStringLiteral("60e8e5ebb1a5eac95a01ec7f8796b2dce471") 0075 << QStringLiteral("594f6a6c3637476c36736c614165782f68356179334f5278"); 0076 QTest::newRow("3134") << QStringLiteral("31346d354f33313333372c31274d754e7354307050346231333a29") 0077 << QStringLiteral("4d5452744e55387a4d544d7a4e7977784a303131546e4e554d4842514e4749784d7a6f70"); 0078 } 0079 0080 void Base64UnitTest::test2_data() 0081 { 0082 QTest::addColumn<QString>("raw"); 0083 QTest::addColumn<QString>("encoded"); 0084 0085 // these are from Python 2.3's tests for base64 0086 QTest::newRow("www.python.org") << QStringLiteral("www.python.org") << QStringLiteral("d3d3LnB5dGhvbi5vcmc="); 0087 QTest::newRow("a") << QStringLiteral("a") << QStringLiteral("YQ=="); 0088 QTest::newRow("ab") << QStringLiteral("ab") << QStringLiteral("YWI="); 0089 QTest::newRow("abc") << QStringLiteral("abc") << QStringLiteral("YWJj"); 0090 QTest::newRow("empty") << QString(QLatin1String("")) << QString(QLatin1String("")); 0091 QTest::newRow("a-Z") << QStringLiteral( 0092 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#0^&*();:<>,. []{}") 0093 << QStringLiteral( 0094 "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NTY3ODkhQC" 0095 "MwXiYqKCk7Ojw+LC4gW117fQ=="); 0096 0097 // these are generated by Python 2.3. I removed the trailing newline 0098 QTest::newRow("31") << QStringLiteral("31") << QStringLiteral("MzE="); 0099 QTest::newRow("QCA_2.0") << QStringLiteral("QCA_2.0") << QStringLiteral("UUNBXzIuMA=="); 0100 QTest::newRow("j-0") << QStringLiteral("jh/*-*/*-/4983589230") << QStringLiteral("amgvKi0qLyotLzQ5ODM1ODkyMzA="); 0101 } 0102 0103 void Base64UnitTest::test1() 0104 { 0105 QCA::Base64 base64Object; 0106 0107 QFETCH(QString, raw); 0108 QFETCH(QString, encoded); 0109 0110 QCOMPARE(QCA::arrayToHex(base64Object.encode(QCA::hexToArray(raw)).toByteArray()), encoded); 0111 QCOMPARE(QCA::arrayToHex(base64Object.decode(QCA::hexToArray(encoded)).toByteArray()), raw); 0112 } 0113 0114 void Base64UnitTest::test2() 0115 { 0116 QCA::Base64 base64Object; 0117 0118 QFETCH(QString, raw); 0119 QFETCH(QString, encoded); 0120 0121 QCOMPARE(base64Object.encodeString(raw), encoded); 0122 QCOMPARE(base64Object.decodeString(encoded), raw); 0123 0124 QCOMPARE(QCA::arrayToBase64(raw.toUtf8()), encoded); 0125 QCOMPARE(QLatin1String(QCA::base64ToArray(encoded)), raw); 0126 } 0127 0128 QTEST_MAIN(Base64UnitTest) 0129 0130 #include "base64unittest.moc"