aboutsummaryrefslogtreecommitdiff
path: root/tests/base64/driver.cxx
blob: 32d523622d32b6af9ae5642c659279d93bf3e860 (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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// file      : tests/base64/driver.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <string>
#include <vector>
#include <sstream>

#include <libbutl/base64.hxx>

#undef NDEBUG
#include <cassert>

using namespace std;
using namespace butl;

// Test base64 encoding and decoding.
//
static bool
encode (const string& i, const string& o)
{
  // Test encoding.
  //
  istringstream is (i);
  string s (base64_encode (is));
  bool r (s == o && is.eof ());

  if (r)
  {
    is.seekg (0);

    // VC15 seekg() doesn't clear eofbit.
    //
#if defined(_MSC_VER) && _MSC_VER < 1920
    is.clear ();
#endif

    assert (!is.eof ());

    ostringstream os;
    base64_encode (os, is);
    r = os.str () == o && is.eof ();
  }

  if (r)
    r = base64_encode (vector<char> (i.begin (), i.end ())) == o;

  // Test decoding.
  //
  if (r)
  {
    istringstream is (o);
    ostringstream os;
    base64_decode (os, is);
    r = os.str () == i;
  }

  if (r)
  {
    ostringstream os;
    base64_decode (os, o);
    r = os.str () == i;
  }

  if (r)
  {
    vector<char> v (base64_decode (o));
    r = string (v.begin (), v.end ()) == i;
  }

  return r;
}

// Test base64url encoding only (decoding not yet implemented).
//
static bool
encode_url (const string& i, const string& o)
{
  istringstream is (i);
  string s (base64url_encode (is));
  bool r (s == o && is.eof ());

  if (r)
  {
    is.seekg (0);

    // VC15 seekg() doesn't clear eofbit.
    //
#if defined(_MSC_VER) && _MSC_VER < 1920
    is.clear ();
#endif

    assert (!is.eof ());

    ostringstream os;
    base64url_encode (os, is);
    r = os.str () == o && is.eof ();
  }

  if (r)
    r = base64url_encode (vector<char> (i.begin (), i.end ())) == o;

  return r;
}


int
main ()
{
  // base64
  //
  assert (encode ("", ""));
  assert (encode ("B", "Qg=="));
  assert (encode ("BX", "Qlg="));
  assert (encode ("BXz", "Qlh6"));
  assert (encode ("BXzS", "Qlh6Uw=="));
  assert (encode ("BXzS@", "Qlh6U0A="));
  assert (encode ("BXzS@#", "Qlh6U0Aj"));
  assert (encode ("BXzS@#/", "Qlh6U0AjLw=="));

  // base64url: no padding in output.
  //
  assert (encode_url ("", ""));
  assert (encode_url ("B", "Qg"));
  assert (encode_url ("BX", "Qlg"));
  assert (encode_url ("BXz", "Qlh6"));
  assert (encode_url ("BXzS", "Qlh6Uw"));
  assert (encode_url ("BXzS@", "Qlh6U0A"));
  assert (encode_url ("BXzS@#", "Qlh6U0Aj"));
  assert (encode_url ("BXzS@#/", "Qlh6U0AjLw"));

  // Multi-line input.
  //
  const char* s (
"class fdstream_base\n"
"{\n"
"protected:\n"
"  fdstream_base () = default;\n"
"  fdstream_base (int fd): buf_ (fd) {}\n"
"\n"
"protected:\n"
"  fdbuf buf_;\n"
"};\n");

  // base64
  //
  const char* r (
"Y2xhc3MgZmRzdHJlYW1fYmFzZQp7CnByb3RlY3RlZDoKICBmZHN0cmVhbV9iYXNlICgpID0gZGVm\n"
"YXVsdDsKICBmZHN0cmVhbV9iYXNlIChpbnQgZmQpOiBidWZfIChmZCkge30KCnByb3RlY3RlZDoK\n"
"ICBmZGJ1ZiBidWZfOwp9Owo=");

  assert (encode (s, r));

  // base64url: no newlines or padding in output.
  //
  r =
"Y2xhc3MgZmRzdHJlYW1fYmFzZQp7CnByb3RlY3RlZDoKICBmZHN0cmVhbV9iYXNlICgpID0gZGVm"
"YXVsdDsKICBmZHN0cmVhbV9iYXNlIChpbnQgZmQpOiBidWZfIChmZCkge30KCnByb3RlY3RlZDoK"
"ICBmZGJ1ZiBidWZfOwp9Owo";

  assert (encode_url (s, r));

  // Test 63rd and 64th characters: `>` maps to `+` or `-`; `?` maps to `/` or
  // `_`.
  //
  assert (encode     (">>>>>>", "Pj4+Pj4+"));
  assert (encode_url (">>>>>>", "Pj4-Pj4-"));
  assert (encode     ("??????", "Pz8/Pz8/"));
  assert (encode_url ("??????", "Pz8_Pz8_"));
}