Browse code

base64: more thorough decode tests.

Signed-off-by: Reimar Döffinger <Reimar.Doeffinger@gmx.de>

Reimar Döffinger authored on 2012/01/21 07:26:10
Showing 1 changed files
... ...
@@ -136,15 +136,34 @@ static int test_encode_decode(const uint8_t *data, unsigned int data_size,
136 136
         return 1;
137 137
     }
138 138
 
139
-    if ((data2_size = av_base64_decode(data2, encoded, max_data2_size)) < 0) {
139
+    if ((data2_size = av_base64_decode(data2, encoded, max_data2_size)) != data_size) {
140 140
         printf("Failed: cannot decode the encoded string\n"
141 141
                "Encoded:\n%s\n", encoded);
142 142
         return 1;
143 143
     }
144
+    if ((data2_size = av_base64_decode(data2, encoded, data_size)) != data_size) {
145
+        printf("Failed: cannot decode with minimal buffer\n"
146
+               "Encoded:\n%s\n", encoded);
147
+        return 1;
148
+    }
144 149
     if (memcmp(data2, data, data_size)) {
145 150
         printf("Failed: encoded/decoded data differs from original data\n");
146 151
         return 1;
147 152
     }
153
+    if (av_base64_decode(NULL, encoded, 0) != 0) {
154
+        printf("Failed: decode to NULL buffer\n");
155
+        return 1;
156
+    }
157
+    if (strlen(encoded)) {
158
+        char *end = strchr(encoded, '=');
159
+        if (!end)
160
+            end = encoded + strlen(encoded) - 1;
161
+        *end = '%';
162
+        if (av_base64_decode(NULL, encoded, 0) >= 0) {
163
+            printf("Failed: error detection\n");
164
+            return 1;
165
+        }
166
+    }
148 167
 
149 168
     printf("Passed!\n");
150 169
     return 0;