]> git.sesse.net Git - ffmpeg/blob - libavutil/tests/base64.c
Merge commit '65074791e8f8397600aacc9801efdd17777eb6e3'
[ffmpeg] / libavutil / tests / base64.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 // LCOV_EXCL_START
20 #include <stdint.h>
21 #include <stdio.h>
22
23 #include "libavutil/common.h"
24 #include "libavutil/base64.h"
25 #include "libavutil/timer.h"
26
27 #define MAX_DATA_SIZE    1024
28 #define MAX_ENCODED_SIZE 2048
29
30 static int test_encode_decode(const uint8_t *data, unsigned int data_size,
31                               const char *encoded_ref)
32 {
33     char  encoded[MAX_ENCODED_SIZE];
34     uint8_t data2[MAX_DATA_SIZE];
35     int data2_size, max_data2_size = MAX_DATA_SIZE;
36
37     if (!av_base64_encode(encoded, MAX_ENCODED_SIZE, data, data_size)) {
38         printf("Failed: cannot encode the input data\n");
39         return 1;
40     }
41     if (encoded_ref && strcmp(encoded, encoded_ref)) {
42         printf("Failed: encoded string differs from reference\n"
43                "Encoded:\n%s\nReference:\n%s\n", encoded, encoded_ref);
44         return 1;
45     }
46
47     if ((data2_size = av_base64_decode(data2, encoded, max_data2_size)) != data_size) {
48         printf("Failed: cannot decode the encoded string\n"
49                "Encoded:\n%s\n", encoded);
50         return 1;
51     }
52     if ((data2_size = av_base64_decode(data2, encoded, data_size)) != data_size) {
53         printf("Failed: cannot decode with minimal buffer\n"
54                "Encoded:\n%s\n", encoded);
55         return 1;
56     }
57     if (memcmp(data2, data, data_size)) {
58         printf("Failed: encoded/decoded data differs from original data\n");
59         return 1;
60     }
61     if (av_base64_decode(NULL, encoded, 0) != 0) {
62         printf("Failed: decode to NULL buffer\n");
63         return 1;
64     }
65     if (strlen(encoded)) {
66         char *end = strchr(encoded, '=');
67         if (!end)
68             end = encoded + strlen(encoded) - 1;
69         *end = '%';
70         if (av_base64_decode(NULL, encoded, 0) >= 0) {
71             printf("Failed: error detection\n");
72             return 1;
73         }
74     }
75
76     printf("Passed!\n");
77     return 0;
78 }
79
80 int main(int argc, char ** argv)
81 {
82     int i, error_count = 0;
83     struct test {
84         const uint8_t *data;
85         const char *encoded_ref;
86     } tests[] = {
87         { "",        ""},
88         { "1",       "MQ=="},
89         { "22",      "MjI="},
90         { "333",     "MzMz"},
91         { "4444",    "NDQ0NA=="},
92         { "55555",   "NTU1NTU="},
93         { "666666",  "NjY2NjY2"},
94         { "abc:def", "YWJjOmRlZg=="},
95     };
96     char in[1024], out[2048];
97
98     printf("Encoding/decoding tests\n");
99     for (i = 0; i < FF_ARRAY_ELEMS(tests); i++)
100         error_count += test_encode_decode(tests[i].data, strlen(tests[i].data), tests[i].encoded_ref);
101
102     if (argc>1 && !strcmp(argv[1], "-t")) {
103         memset(in, 123, sizeof(in));
104         for(i=0; i<10000; i++){
105             START_TIMER
106             av_base64_encode(out, sizeof(out), in, sizeof(in));
107             STOP_TIMER("encode")
108         }
109         for(i=0; i<10000; i++){
110             START_TIMER
111             av_base64_decode(in, out, sizeof(in));
112             STOP_TIMER("decode")
113         }
114
115         for(i=0; i<10000; i++){
116             START_TIMER
117             av_base64_decode(NULL, out, 0);
118             STOP_TIMER("syntax check")
119         }
120     }
121
122     if (error_count)
123         printf("Error Count: %d.\n", error_count);
124
125     return !!error_count;
126 }
127
128 // LCOV_EXCL_STOP