File: base64unittest.cpp

package info (click to toggle)
qca2 2.3.10-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 5,884 kB
  • sloc: cpp: 59,224; ansic: 814; perl: 133; sh: 89; makefile: 34
file content (130 lines) | stat: -rw-r--r-- 5,106 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
 * base64unittest.cpp
 *
 * Copyright (C)  2004-2006  Brad Hards <bradh@frogmouth.net>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <QTest>
#include <QtCrypto>

#ifdef QT_STATICPLUGIN
#include "import_plugins.h"
#endif

class Base64UnitTest : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void initTestCase();
    void cleanupTestCase();
    void test1_data();
    void test1();
    void test2_data();
    void test2();

private:
    QCA::Initializer *m_init;
};

void Base64UnitTest::initTestCase()
{
    m_init = new QCA::Initializer;
}

void Base64UnitTest::cleanupTestCase()
{
    delete m_init;
}

void Base64UnitTest::test1_data()
{
    QTest::addColumn<QString>("raw");
    QTest::addColumn<QString>("encoded");

    // these are from the Botan test suite. Note that these are hex encoded!
    QTest::newRow("31") << QStringLiteral("31") << QStringLiteral("4d513d3d");
    QTest::newRow("235c91") << QStringLiteral("235c91") << QStringLiteral("49317952");
    QTest::newRow("414") << QStringLiteral("4142634452313236") << QStringLiteral("51554a6a524649784d6a593d");
    QTest::newRow("241") << QStringLiteral("241bb300a3989a620659")
                         << QStringLiteral("4a42757a414b4f596d6d494757513d3d");
    QTest::newRow("313") << QStringLiteral("31323537374343666671333435337836")
                         << QStringLiteral("4d5449314e7a644451325a6d63544d304e544e344e673d3d");
    QTest::newRow("60e") << QStringLiteral("60e8e5ebb1a5eac95a01ec7f8796b2dce471")
                         << QStringLiteral("594f6a6c3637476c36736c614165782f68356179334f5278");
    QTest::newRow("3134") << QStringLiteral("31346d354f33313333372c31274d754e7354307050346231333a29")
                          << QStringLiteral("4d5452744e55387a4d544d7a4e7977784a303131546e4e554d4842514e4749784d7a6f70");
}

void Base64UnitTest::test2_data()
{
    QTest::addColumn<QString>("raw");
    QTest::addColumn<QString>("encoded");

    // these are from Python 2.3's tests for base64
    QTest::newRow("www.python.org") << QStringLiteral("www.python.org") << QStringLiteral("d3d3LnB5dGhvbi5vcmc=");
    QTest::newRow("a") << QStringLiteral("a") << QStringLiteral("YQ==");
    QTest::newRow("ab") << QStringLiteral("ab") << QStringLiteral("YWI=");
    QTest::newRow("abc") << QStringLiteral("abc") << QStringLiteral("YWJj");
    QTest::newRow("empty") << QString(QLatin1String("")) << QString(QLatin1String(""));
    QTest::newRow("a-Z") << QStringLiteral(
                                "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#0^&*();:<>,. []{}")
                         << QStringLiteral(
                                "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NTY3ODkhQC"
                                "MwXiYqKCk7Ojw+LC4gW117fQ==");

    // these are generated by Python 2.3. I removed the trailing newline
    QTest::newRow("31") << QStringLiteral("31") << QStringLiteral("MzE=");
    QTest::newRow("QCA_2.0") << QStringLiteral("QCA_2.0") << QStringLiteral("UUNBXzIuMA==");
    QTest::newRow("j-0") << QStringLiteral("jh/*-*/*-/4983589230") << QStringLiteral("amgvKi0qLyotLzQ5ODM1ODkyMzA=");
}

void Base64UnitTest::test1()
{
    QCA::Base64 base64Object;

    QFETCH(QString, raw);
    QFETCH(QString, encoded);

    QCOMPARE(QCA::arrayToHex(base64Object.encode(QCA::hexToArray(raw)).toByteArray()), encoded);
    QCOMPARE(QCA::arrayToHex(base64Object.decode(QCA::hexToArray(encoded)).toByteArray()), raw);
}

void Base64UnitTest::test2()
{
    QCA::Base64 base64Object;

    QFETCH(QString, raw);
    QFETCH(QString, encoded);

    QCOMPARE(base64Object.encodeString(raw), encoded);
    QCOMPARE(base64Object.decodeString(encoded), raw);

    QCOMPARE(QCA::arrayToBase64(raw.toUtf8()), encoded);
    QCOMPARE(QLatin1String(QCA::base64ToArray(encoded)), raw);
}

QTEST_MAIN(Base64UnitTest)

#include "base64unittest.moc"