summaryrefslogtreecommitdiffstats
blob: 272514f693a01c61e3d007747854efb737296abf (plain)
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
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <QA/Checker/IO.h>

#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>

#include <Swiften/Base/ByteArray.h>
#include <Swiften/StringCodecs/Base64.h>

using namespace Swift;

class Base64Test : public CppUnit::TestFixture {
        CPPUNIT_TEST_SUITE(Base64Test);
        CPPUNIT_TEST(testEncodeDecodeAllChars);
        CPPUNIT_TEST(testEncodeDecodeOneBytePadding);
        CPPUNIT_TEST(testEncodeDecodeTwoBytesPadding);
        CPPUNIT_TEST(testEncode_NoData);
        CPPUNIT_TEST(testDecode_NoData);
        CPPUNIT_TEST_SUITE_END();

    public:
        void testEncodeDecodeAllChars() {
            ByteArray input;
            for (unsigned char i = 0; i < 255; ++i) {
                input.push_back(i);
            }
            std::string result(Base64::encode(input));

            CPPUNIT_ASSERT_EQUAL(std::string("AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+"), result);
            CPPUNIT_ASSERT_EQUAL(input, Base64::decode(result));
        }

        void testEncodeDecodeOneBytePadding() {
            ByteArray input = createByteArray("ABCDE", 5);

            std::string result = Base64::encode(input);

            CPPUNIT_ASSERT_EQUAL(std::string("QUJDREU="), result);
            CPPUNIT_ASSERT_EQUAL(input, Base64::decode(result));
        }

        void testEncodeDecodeTwoBytesPadding() {
            ByteArray input = createByteArray("ABCD", 4);

            std::string result = Base64::encode(input);

            CPPUNIT_ASSERT_EQUAL(std::string("QUJDRA=="), result);
            CPPUNIT_ASSERT_EQUAL(input, Base64::decode(result));
        }

        void testEncode_NoData() {
            std::string result(Base64::encode(ByteArray()));
            CPPUNIT_ASSERT_EQUAL(std::string(""), result);
        }

        void testDecode_NoData() {
            ByteArray result(Base64::decode(""));
            CPPUNIT_ASSERT_EQUAL(ByteArray(), result);
        }
};

CPPUNIT_TEST_SUITE_REGISTRATION(Base64Test);