File: test_base64.cc

package info (click to toggle)
eckit 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 11,044 kB
  • sloc: cpp: 111,103; ansic: 2,826; yacc: 590; lex: 361; python: 302; sh: 162; makefile: 53
file content (101 lines) | stat: -rw-r--r-- 2,767 bytes parent folder | download | duplicates (5)
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
/*
 * (C) Copyright 1996- ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence Version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 * In applying this licence, ECMWF does not waive the privileges and immunities
 * granted to it by virtue of its status as an intergovernmental organisation nor
 * does it submit to any jurisdiction.
 */

#include <cstring>

#include "eckit/io/Base64.h"
#include "eckit/io/MemoryHandle.h"

#include "eckit/testing/Test.h"

using namespace std;
using namespace eckit;
using namespace eckit::testing;

namespace eckit::test {

//----------------------------------------------------------------------------------------------------------------------

static std::string test(const char* text, const char* expect) {
    char base64ed[10240]   = {};
    char unbase64ed[10240] = {};

    Base64 base64;

    size_t s;
    size_t t;

    std::cout << "Text is [" << text << "]" << std::endl;

    {
        MemoryHandle in(text, strlen(text));
        MemoryHandle out(base64ed, sizeof(base64ed));

        s = base64.encode(in, out);
        std::cout << "Base64 from " << strlen(text) << " to " << s << std::endl;
    }

    std::string v(base64ed, base64ed + s);

    std::cout << "encoded is [" << v << "]" << std::endl;

    EXPECT(v == expect);

    std::cout << "-----------------------" << std::endl;
    {
        MemoryHandle in(base64ed, s);
        MemoryHandle out(unbase64ed, sizeof(unbase64ed));

        t = base64.decode(in, out);
        std::cout << "Unbase64 from " << s << " to " << t << std::endl;
    }

    std::string u(unbase64ed, unbase64ed + t);
    std::cout << "decoded is [" << u << "]" << std::endl;


    std::cout << text << std::endl;
    std::cout << u << std::endl;

    for (size_t i = 0; i < std::min(u.size(), strlen(text)); ++i) {
        std::cout << ((text[i] == u[i]) ? ' ' : '|');
    }
    std::cout << std::endl;

    return u;
}

//----------------------------------------------------------------------------------------------------------------------

CASE("test_eckit_base64_1") {
    {
        const char* pattern = "Man";
        std::string s       = test(pattern, "TWFu");
        EXPECT(s == std::string(pattern));
    }
    {
        const char* pattern = "M";
        std::string s       = test(pattern, "TQ==");
        EXPECT(s == std::string(pattern));
    }
    {
        const char* pattern = "Ma";
        std::string s       = test(pattern, "TWE=");
        EXPECT(s == std::string(pattern));
    }
}

//----------------------------------------------------------------------------------------------------------------------

}  // namespace eckit::test

int main(int argc, char** argv) {
    return run_tests(argc, argv);
}