1/**
2 * base64unittest.cpp
3 *
4 * Copyright (C) 2004-2006 Brad Hards <bradh@frogmouth.net>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <QtCrypto>
29#include <QtTest/QtTest>
30
31#ifdef QT_STATICPLUGIN
32#include "import_plugins.h"
33#endif
34
35class Base64UnitTest : public QObject
36{
37 Q_OBJECT
38
39private Q_SLOTS:
40 void initTestCase();
41 void cleanupTestCase();
42 void test1_data();
43 void test1();
44 void test2_data();
45 void test2();
46
47private:
48 QCA::Initializer *m_init;
49};
50
51void Base64UnitTest::initTestCase()
52{
53 m_init = new QCA::Initializer;
54}
55
56void Base64UnitTest::cleanupTestCase()
57{
58 delete m_init;
59}
60
61void Base64UnitTest::test1_data()
62{
63 QTest::addColumn<QString>(name: "raw");
64 QTest::addColumn<QString>(name: "encoded");
65
66 // these are from the Botan test suite. Note that these are hex encoded!
67 QTest::newRow(dataTag: "31") << QStringLiteral("31") << QStringLiteral("4d513d3d");
68 QTest::newRow(dataTag: "235c91") << QStringLiteral("235c91") << QStringLiteral("49317952");
69 QTest::newRow(dataTag: "414") << QStringLiteral("4142634452313236") << QStringLiteral("51554a6a524649784d6a593d");
70 QTest::newRow(dataTag: "241") << QStringLiteral("241bb300a3989a620659")
71 << QStringLiteral("4a42757a414b4f596d6d494757513d3d");
72 QTest::newRow(dataTag: "313") << QStringLiteral("31323537374343666671333435337836")
73 << QStringLiteral("4d5449314e7a644451325a6d63544d304e544e344e673d3d");
74 QTest::newRow(dataTag: "60e") << QStringLiteral("60e8e5ebb1a5eac95a01ec7f8796b2dce471")
75 << QStringLiteral("594f6a6c3637476c36736c614165782f68356179334f5278");
76 QTest::newRow(dataTag: "3134") << QStringLiteral("31346d354f33313333372c31274d754e7354307050346231333a29")
77 << QStringLiteral("4d5452744e55387a4d544d7a4e7977784a303131546e4e554d4842514e4749784d7a6f70");
78}
79
80void Base64UnitTest::test2_data()
81{
82 QTest::addColumn<QString>(name: "raw");
83 QTest::addColumn<QString>(name: "encoded");
84
85 // these are from Python 2.3's tests for base64
86 QTest::newRow(dataTag: "www.python.org") << QStringLiteral("www.python.org") << QStringLiteral("d3d3LnB5dGhvbi5vcmc=");
87 QTest::newRow(dataTag: "a") << QStringLiteral("a") << QStringLiteral("YQ==");
88 QTest::newRow(dataTag: "ab") << QStringLiteral("ab") << QStringLiteral("YWI=");
89 QTest::newRow(dataTag: "abc") << QStringLiteral("abc") << QStringLiteral("YWJj");
90 QTest::newRow(dataTag: "empty") << QString(QLatin1String("")) << QString(QLatin1String(""));
91 QTest::newRow(dataTag: "a-Z") << QStringLiteral(
92 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#0^&*();:<>,. []{}")
93 << QStringLiteral(
94 "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NTY3ODkhQC"
95 "MwXiYqKCk7Ojw+LC4gW117fQ==");
96
97 // these are generated by Python 2.3. I removed the trailing newline
98 QTest::newRow(dataTag: "31") << QStringLiteral("31") << QStringLiteral("MzE=");
99 QTest::newRow(dataTag: "QCA_2.0") << QStringLiteral("QCA_2.0") << QStringLiteral("UUNBXzIuMA==");
100 QTest::newRow(dataTag: "j-0") << QStringLiteral("jh/*-*/*-/4983589230") << QStringLiteral("amgvKi0qLyotLzQ5ODM1ODkyMzA=");
101}
102
103void Base64UnitTest::test1()
104{
105 QCA::Base64 base64Object;
106
107 QFETCH(QString, raw);
108 QFETCH(QString, encoded);
109
110 QCOMPARE(QCA::arrayToHex(base64Object.encode(QCA::hexToArray(raw)).toByteArray()), encoded);
111 QCOMPARE(QCA::arrayToHex(base64Object.decode(QCA::hexToArray(encoded)).toByteArray()), raw);
112}
113
114void Base64UnitTest::test2()
115{
116 QCA::Base64 base64Object;
117
118 QFETCH(QString, raw);
119 QFETCH(QString, encoded);
120
121 QCOMPARE(base64Object.encodeString(raw), encoded);
122 QCOMPARE(base64Object.decodeString(encoded), raw);
123
124 QCOMPARE(QCA::arrayToBase64(raw.toUtf8()), encoded);
125 QCOMPARE(QLatin1String(QCA::base64ToArray(encoded)), raw);
126}
127
128QTEST_MAIN(Base64UnitTest)
129
130#include "base64unittest.moc"
131

source code of qca/unittest/base64unittest/base64unittest.cpp