-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase64.cpp
More file actions
208 lines (161 loc) · 5.07 KB
/
Copy pathbase64.cpp
File metadata and controls
208 lines (161 loc) · 5.07 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright (C) 2006 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#include <sstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <dlib/base64.h>
#include "tester.h"
namespace
{
using namespace test;
using namespace dlib;
using namespace std;
logger dlog("test.base64");
template <
typename base64
>
void base64_kernel_test (
)
/*!
requires
- base64 is an implementation of base64/base64_kernel_abstract.h
ensures
- runs tests on base64 for compliance with the specs
!*/
{
const unsigned int seed = static_cast<unsigned int>(time(0));
try
{
srand(seed);
base64 test;
const string wiki_normal = "\
Man is distinguished, not only by his reason, but by this singular passion from other \
animals, which is a lust of the mind, that by a perseverance of delight in the continued \
and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.";
const string wiki_encoded = "\
TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0\n\
aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1\n\
c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0\n\
aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdl\n\
LCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=";
string str;
istringstream sin;
ostringstream sout;
sin.str(wiki_encoded);
test.decode(sin,sout);
DLIB_TEST_MSG(sout.str() == wiki_normal,
"sout.str(): " << sout.str() <<
"\nwiki_normal: " << wiki_normal);
sout.str("");
sin.str(wiki_normal);
sin.clear();
test.encode(sin,sout);
string a(sout.str()), b(wiki_encoded);
// we want to strip all the whitespace from a and b now
sin.str(a);
a.clear();
sin >> str;
while (sin)
{
a += str;
sin >> str;
}
sin.clear();
sin.str(b);
b.clear();
sin >> str;
while (sin)
{
b += str;
sin >> str;
}
sin.clear();
DLIB_TEST_MSG(a == b,
"a: \n" << a <<
"\n\nb: \n" << b);
sin.clear();
sin.str("");
sout.str("");
test.encode(sin,sout);
sin.str(sout.str());
sout.str("");
test.decode(sin,sout);
DLIB_TEST(sout.str() == "");
sin.clear();
sin.str("a");
sout.str("");
test.encode(sin,sout);
sin.str(sout.str());
sout.str("");
test.decode(sin,sout);
DLIB_TEST(sout.str() == "a");
sin.clear();
sin.str("da");
sout.str("");
test.encode(sin,sout);
sin.str(sout.str());
sout.str("");
test.decode(sin,sout);
DLIB_TEST(sout.str() == "da");
sin.clear();
sin.str("dav");
sout.str("");
test.encode(sin,sout);
sin.str(sout.str());
sout.str("");
test.decode(sin,sout);
DLIB_TEST(sout.str() == "dav");
sin.clear();
sin.str("davi");
sout.str("");
test.encode(sin,sout);
sin.str(sout.str());
sout.str("");
test.decode(sin,sout);
DLIB_TEST(sout.str() == "davi");
for (int i = 0; i < 1000; ++i)
{
str.clear();
sin.clear();
sout.str("");
sin.str("");
// fill str with random garbage
const int size = rand()%2000;
for (int j = 0; j < size; ++j)
{
unsigned char ch = rand()&0xFF;
str += ch;
}
sin.str(str);
test.encode(sin,sout);
sin.clear();
sin.str(sout.str());
sout.str("");
test.decode(sin,sout);
DLIB_TEST(str == sout.str());
}
}
catch (typename base64::decode_error& e)
{
DLIB_TEST_MSG(false,
"decode_error thrown when it shouldn't have been (" << seed << "):\n "
<< e.info);
}
}
class base64_tester : public tester
{
public:
base64_tester (
) :
tester ("test_base64",
"Runs tests on the base64 component.")
{}
void perform_test (
)
{
print_spinner();
base64_kernel_test<base64>();
}
} a;
}