File: test_base64.c

package info (click to toggle)
xrdp 0.10.5-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 15,104 kB
  • sloc: ansic: 107,426; sh: 5,852; asm: 4,742; cpp: 2,555; makefile: 1,474
file content (400 lines) | stat: -rw-r--r-- 11,889 bytes parent folder | download | duplicates (8)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400

#if defined(HAVE_CONFIG_H)
#include "config_ac.h"
#endif

#include "os_calls.h"
#include "string_calls.h"
#include "base64.h"

#include "test_common.h"
/*
* These are the example test strings in RFC4648(10)
*/
static const char *rfc4648_ex1_text = "";
static const char *rfc4648_ex1_b64 = "";
static const char *rfc4648_ex2_text = "f";
static const char *rfc4648_ex2_b64 = "Zg==";
static const char *rfc4648_ex3_text = "fo";
static const char *rfc4648_ex3_b64 = "Zm8=";
static const char *rfc4648_ex4_text = "foo";
static const char *rfc4648_ex4_b64 = "Zm9v";
static const char *rfc4648_ex5_text = "foob";
static const char *rfc4648_ex5_b64 = "Zm9vYg==";
static const char *rfc4648_ex6_text = "fooba";
static const char *rfc4648_ex6_b64 = "Zm9vYmE=";
static const char *rfc4648_ex7_text = "foobar";
static const char *rfc4648_ex7_b64 = "Zm9vYmFy";

/* Every single valid base64 character, except padding */
static const char *all_b64 =
    "ABCDEFGHIJKL"
    "MNOPQRSTUVWX"
    "YZabcdefghij"
    "klmnopqrstuv"
    "wxyz01234567"
    "89+/";

/* What we should get as binary if we decode this */
static const char all_b64_decoded[] =
{
    '\x00', '\x10', '\x83', '\x10', '\x51', '\x87', '\x20', '\x92', '\x8b',
    '\x30', '\xd3', '\x8f', '\x41', '\x14', '\x93', '\x51', '\x55', '\x97',
    '\x61', '\x96', '\x9b', '\x71', '\xd7', '\x9f', '\x82', '\x18', '\xa3',
    '\x92', '\x59', '\xa7', '\xa2', '\x9a', '\xab', '\xb2', '\xdb', '\xaf',
    '\xc3', '\x1c', '\xb3', '\xd3', '\x5d', '\xb7', '\xe3', '\x9e', '\xbb',
    '\xf3', '\xdf', '\xbf'
};

static void
test_rfc4648_to_b64(const char *plaintext, size_t len, const char *b64)
{
    char buff[256];
    size_t result;

    result = base64_encode(plaintext, len, buff, sizeof(buff));
    ck_assert_int_eq(result, len);
    ck_assert_str_eq(buff, b64);

}

/* Text-only encoder wrapper */
static void
test_rfc4648_to_b64_text(const char *plaintext, const char *b64)
{
    test_rfc4648_to_b64(plaintext, g_strlen(plaintext), b64);
}

/* Text-only decoder wrapper for valid base64 */
static void
test_rfc4648_from_b64_text(const char *b64, const char *text)
{
    char buff[256];
    size_t actual_len;
    int result;

    result = base64_decode(b64, buff, sizeof(buff), &actual_len);
    ck_assert_int_eq(result, 0);
    ck_assert_int_lt(actual_len, sizeof(buff));
    buff[actual_len] = '\0';
    ck_assert_str_eq(buff, text);
}

/******************************************************************************/
START_TEST(test_b64_rfc4648_ex1_to)
{
    test_rfc4648_to_b64_text(rfc4648_ex1_text, rfc4648_ex1_b64);
}
END_TEST

/******************************************************************************/
START_TEST(test_b64_rfc4648_ex1_from)
{
    test_rfc4648_from_b64_text(rfc4648_ex1_b64, rfc4648_ex1_text);
}
END_TEST

/******************************************************************************/
START_TEST(test_b64_rfc4648_ex2_to)
{
    test_rfc4648_to_b64_text(rfc4648_ex2_text, rfc4648_ex2_b64);
}
END_TEST

/******************************************************************************/
START_TEST(test_b64_rfc4648_ex2_from)
{
    test_rfc4648_from_b64_text(rfc4648_ex2_b64, rfc4648_ex2_text);
}
END_TEST

/******************************************************************************/
START_TEST(test_b64_rfc4648_ex3_to)
{
    test_rfc4648_to_b64_text(rfc4648_ex3_text, rfc4648_ex3_b64);
}
END_TEST

/******************************************************************************/
START_TEST(test_b64_rfc4648_ex3_from)
{
    test_rfc4648_from_b64_text(rfc4648_ex3_b64, rfc4648_ex3_text);
}
END_TEST

/******************************************************************************/
START_TEST(test_b64_rfc4648_ex4_to)
{
    test_rfc4648_to_b64_text(rfc4648_ex4_text, rfc4648_ex4_b64);
}
END_TEST

/******************************************************************************/
START_TEST(test_b64_rfc4648_ex4_from)
{
    test_rfc4648_from_b64_text(rfc4648_ex4_b64, rfc4648_ex4_text);
}
END_TEST

/******************************************************************************/
START_TEST(test_b64_rfc4648_ex5_to)
{
    test_rfc4648_to_b64_text(rfc4648_ex5_text, rfc4648_ex5_b64);
}
END_TEST

/******************************************************************************/
START_TEST(test_b64_rfc4648_ex5_from)
{
    test_rfc4648_from_b64_text(rfc4648_ex5_b64, rfc4648_ex5_text);
}
END_TEST

/******************************************************************************/
START_TEST(test_b64_rfc4648_ex6_to)
{
    test_rfc4648_to_b64_text(rfc4648_ex6_text, rfc4648_ex6_b64);
}
END_TEST

/******************************************************************************/
START_TEST(test_b64_rfc4648_ex6_from)
{
    test_rfc4648_from_b64_text(rfc4648_ex6_b64, rfc4648_ex6_text);
}
END_TEST

/******************************************************************************/
START_TEST(test_b64_rfc4648_ex7_to)
{
    test_rfc4648_to_b64_text(rfc4648_ex7_text, rfc4648_ex7_b64);
}
END_TEST

/******************************************************************************/
START_TEST(test_b64_rfc4648_ex7_from)
{
    test_rfc4648_from_b64_text(rfc4648_ex7_b64, rfc4648_ex7_text);
}
END_TEST

/******************************************************************************/
START_TEST(test_b64_all_valid_from)
{
    char buff[256];
    size_t actual_len;
    int result;
    char *str_result;
    char *str_expected;

    result = base64_decode(all_b64, buff, sizeof(buff), &actual_len);
    ck_assert_int_eq(result, 0);
    ck_assert_int_eq(actual_len, sizeof(all_b64_decoded));
    str_result = bin_to_hex(buff, actual_len);
    str_expected = bin_to_hex(all_b64_decoded, sizeof(all_b64_decoded));
    ck_assert_str_eq(str_result, str_expected);
    free(str_result);
    free(str_expected);
}
END_TEST

/******************************************************************************/
START_TEST(test_b64_all_valid_to)
{
    char buff[256];
    size_t result;

    result = base64_encode(all_b64_decoded, sizeof(all_b64_decoded),
                           buff, sizeof(buff));
    ck_assert_int_eq(result, sizeof(all_b64_decoded));
    ck_assert_str_eq(buff, all_b64);
}
END_TEST

/******************************************************************************/
START_TEST(test_b64_all_invalid)
{
    char buff[256];
    size_t actual_len;
    char valid[256] = {0};
    char encoded[5] = { "T0sh" }; /* Decodes to 'OK!' */
    int result;

    /* Make a note of all the valid b64 characters */
    unsigned int i = 0;
    unsigned char c;
    while ((c = all_b64[i]) != '\0')
    {
        valid[c] = 1;
        ++i;
    }

    /* Check the decoder's working on a simple string...*/
    result = base64_decode(encoded, buff, sizeof(buff), &actual_len);
    ck_assert_int_eq(result, 0);
    ck_assert_int_eq(actual_len, 3);
    buff[actual_len] = '\0';
    ck_assert_str_eq(buff, "OK!");

    /* Now replace the 1st character with all invalid characters in turn,
     * and check they're rejected */
    for (i = 0 ; i < 256; ++i)
    {
        if (i != '\0' && !valid[i]) /* Don't try the string terminator char! */
        {
            encoded[0] = i;
            result = base64_decode(encoded, buff, sizeof(buff), &actual_len);
            if (result == 0)
            {
                ck_abort_msg("Character 0x%02x was not rejected", i);
            }
        }
    }
}
END_TEST

/******************************************************************************/
START_TEST(test_b64_small_buffer_encode)
{
    char buff[10 * 4 + 1]; /* Enough space for 10 quanta */

    size_t result;

    result = base64_encode(all_b64_decoded, sizeof(all_b64_decoded),
                           buff, sizeof(buff));
    /* Should have read 10 lots of 24 bits from the input */
    ck_assert_int_eq(result, 10 * 3);
    ck_assert_str_eq(buff, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn");
}
END_TEST

/******************************************************************************/
START_TEST(test_b64_small_buffer_decode)
{
    char buff[10]; /* Enough space for 10 chars */

    size_t actual_len;
    int result;
    char *str_result;
    char *str_expected;

    result = base64_decode(all_b64, buff, sizeof(buff), &actual_len);
    ck_assert_int_eq(result, 0);
    ck_assert_int_eq(actual_len, sizeof(all_b64_decoded));
    str_result = bin_to_hex(buff, sizeof(buff));
    str_expected = bin_to_hex(all_b64_decoded, sizeof(buff));
    ck_assert_str_eq(str_result, str_expected);
    free(str_result);
    free(str_expected);
}
END_TEST

/******************************************************************************/
START_TEST(test_b64_add_pad_one)
{
    /* Check that a missing trailing '=' is added when decoding */
    test_rfc4648_from_b64_text("Zm8", "fo"); /* RFC4648 example 3 */
}
END_TEST

/******************************************************************************/
START_TEST(test_b64_add_pad_two)
{
    /* Check that two missing trailing '=' chars are added when decoding */
    test_rfc4648_from_b64_text("Zg", "f"); /* RFC4648 example 2 */
}
END_TEST

/******************************************************************************/
START_TEST(test_b64_bad_pad)
{
    char buff[16];
    size_t actual_len;

    /* Check all bad quanta with padding chars */
    static const char *bad_pad[] =
    {
        "=AAA",
        "A=AA",
        "AA=A",
        "==AA",
        "=A=A",
        "=AA=",
        "A==A",
        "A=A=",
        "===A",
        "A===",
        NULL
    };
    const char **p;

    for (p = bad_pad ; *p != NULL ; ++p)
    {
        int result = base64_decode(*p, buff, sizeof(buff), &actual_len);
        if (result == 0)
        {
            ck_abort_msg("Padding '%s' was not rejected", *p);
        }
    }
}
END_TEST

/******************************************************************************/
START_TEST(test_b64_concat_pad)
{
    const char *src =
        "VGVzdA=="  /* Test */
        "IA=="      /* <space> */
        "Y29uY2F0ZW5hdGVk" /* concatenated */
        "IA=="      /* <space> */
        "cGFkZGluZw=="; /*padding */
    const char *expected = "Test concatenated padding";
    char buff[64];
    size_t actual_len;
    int result;

    result = base64_decode(src, buff, sizeof(buff), &actual_len);
    ck_assert_int_eq(result, 0);
    ck_assert_int_eq(actual_len, g_strlen(expected));
    buff[actual_len] = '\0';
    ck_assert_str_eq(buff, expected);
}
END_TEST

/******************************************************************************/
Suite *
make_suite_test_base64(void)
{
    Suite *s;
    TCase *tc_b64;

    s = suite_create("base64");

    tc_b64 = tcase_create("base64");
    suite_add_tcase(s, tc_b64);
    tcase_add_test(tc_b64, test_b64_rfc4648_ex1_to);
    tcase_add_test(tc_b64, test_b64_rfc4648_ex1_from);
    tcase_add_test(tc_b64, test_b64_rfc4648_ex2_to);
    tcase_add_test(tc_b64, test_b64_rfc4648_ex2_from);
    tcase_add_test(tc_b64, test_b64_rfc4648_ex3_to);
    tcase_add_test(tc_b64, test_b64_rfc4648_ex3_from);
    tcase_add_test(tc_b64, test_b64_rfc4648_ex4_to);
    tcase_add_test(tc_b64, test_b64_rfc4648_ex4_from);
    tcase_add_test(tc_b64, test_b64_rfc4648_ex5_to);
    tcase_add_test(tc_b64, test_b64_rfc4648_ex5_from);
    tcase_add_test(tc_b64, test_b64_rfc4648_ex6_to);
    tcase_add_test(tc_b64, test_b64_rfc4648_ex6_from);
    tcase_add_test(tc_b64, test_b64_rfc4648_ex7_to);
    tcase_add_test(tc_b64, test_b64_rfc4648_ex7_from);
    tcase_add_test(tc_b64, test_b64_all_valid_from);
    tcase_add_test(tc_b64, test_b64_all_valid_to);
    tcase_add_test(tc_b64, test_b64_all_invalid);
    tcase_add_test(tc_b64, test_b64_small_buffer_encode);
    tcase_add_test(tc_b64, test_b64_small_buffer_decode);
    tcase_add_test(tc_b64, test_b64_add_pad_one);
    tcase_add_test(tc_b64, test_b64_add_pad_two);
    tcase_add_test(tc_b64, test_b64_bad_pad);
    tcase_add_test(tc_b64, test_b64_concat_pad);

    return s;
}