]> git.sesse.net Git - ffmpeg/blob - libavutil/tests/base64.c
Split global .gitignore file into per-directory files
[ffmpeg] / libavutil / tests / base64.c
1 /*
2  * This file is part of Libav.
3  *
4  * Libav is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * Libav is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with Libav; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include <stdint.h>
20 #include <stdio.h>
21
22 #include "libavutil/common.h"
23 #include "libavutil/base64.h"
24
25 #define MAX_DATA_SIZE    1024
26 #define MAX_ENCODED_SIZE 2048
27
28 static int test_encode_decode(const uint8_t *data, unsigned int data_size,
29                               const char *encoded_ref)
30 {
31     char  encoded[MAX_ENCODED_SIZE];
32     uint8_t data2[MAX_DATA_SIZE];
33     int data2_size, max_data2_size = MAX_DATA_SIZE;
34
35     if (!av_base64_encode(encoded, MAX_ENCODED_SIZE, data, data_size)) {
36         printf("Failed: cannot encode the input data\n");
37         return 1;
38     }
39     if (encoded_ref && strcmp(encoded, encoded_ref)) {
40         printf("Failed: encoded string differs from reference\n"
41                "Encoded:\n%s\nReference:\n%s\n", encoded, encoded_ref);
42         return 1;
43     }
44
45     if ((data2_size = av_base64_decode(data2, encoded, max_data2_size)) < 0) {
46         printf("Failed: cannot decode the encoded string\n"
47                "Encoded:\n%s\n", encoded);
48         return 1;
49     }
50     if (memcmp(data2, data, data_size)) {
51         printf("Failed: encoded/decoded data differs from original data\n");
52         return 1;
53     }
54
55     printf("Passed!\n");
56     return 0;
57 }
58
59 int main(void)
60 {
61     int i, error_count = 0;
62     struct test {
63         const uint8_t *data;
64         const char *encoded_ref;
65     } tests[] = {
66         { "",        ""},
67         { "1",       "MQ=="},
68         { "22",      "MjI="},
69         { "333",     "MzMz"},
70         { "4444",    "NDQ0NA=="},
71         { "55555",   "NTU1NTU="},
72         { "666666",  "NjY2NjY2"},
73         { "abc:def", "YWJjOmRlZg=="},
74     };
75
76     printf("Encoding/decoding tests\n");
77     for (i = 0; i < FF_ARRAY_ELEMS(tests); i++)
78         error_count += test_encode_decode(tests[i].data, strlen(tests[i].data), tests[i].encoded_ref);
79
80     if (error_count)
81         printf("Error Count: %d.\n", error_count);
82
83     return !!error_count;
84 }