1 /*
2 * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the License); you may
5 * not use this file except in compliance with the License.
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 */
9
10
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <gmssl/base64.h>
16 #include <gmssl/error.h>
17
18
test_base64(void)19 static int test_base64(void)
20 {
21 uint8_t bin1[50];
22 uint8_t bin2[100];
23 uint8_t bin3[200];
24 uint8_t buf1[8000] = {0};
25 uint8_t buf2[8000] = {0};
26
27 BASE64_CTX ctx;
28 uint8_t *p;
29 int len;
30
31 memset(bin1, 0x01, sizeof(bin1));
32 memset(bin2, 0xA5, sizeof(bin2));
33 memset(bin3, 0xff, sizeof(bin3));
34
35
36 p = buf1;
37 base64_encode_init(&ctx);
38 base64_encode_update(&ctx, bin1, sizeof(bin1), p, &len); p += len;
39 base64_encode_update(&ctx, bin2, sizeof(bin2), p, &len); p += len;
40 base64_encode_update(&ctx, bin3, sizeof(bin3), p, &len); p += len;
41 base64_encode_finish(&ctx, p, &len); p += len;
42 len = (int)(p - buf1);
43
44 p = buf2;
45 base64_decode_init(&ctx);
46 base64_decode_update(&ctx, buf1, len, p, &len); p += len;
47 base64_decode_finish(&ctx, p, &len); p += len;
48 len = (int)(p - buf2);
49
50 printf("base64 test ");
51 if (len != sizeof(bin1) + sizeof(bin2) + sizeof(bin3)
52 || memcmp(buf2, bin1, sizeof(bin1)) != 0
53 || memcmp(buf2 + sizeof(bin1), bin2, sizeof(bin2)) != 0
54 || memcmp(buf2 + sizeof(bin1) + sizeof(bin2), bin3, sizeof(bin3)) != 0) {
55 printf("failed\n");
56 return -1;
57 } else {
58 printf("ok\n");
59 }
60
61 return 1;
62 }
63
main(void)64 int main(void)
65 {
66 if (test_base64() != 1) goto err;
67 printf("%s all tests passed\n", __FILE__);
68 return 0;
69 err:
70 error_print();
71 return -1;
72 }
73