[FFmpeg-devel] [PATCH] remove unused and broken test program in libavutil/base64.c
Stefano Sabatini
stefano.sabatini-lala
Wed Jan 28 20:44:09 CET 2009
On date Wednesday 2009-01-28 18:15:14 +0100, Michael Niedermayer encoded:
> On Wed, Jan 28, 2009 at 12:02:34AM +0100, Stefano Sabatini wrote:
> > On date Tuesday 2009-01-27 03:01:01 +0100, Michael Niedermayer encoded:
> > > On Mon, Jan 26, 2009 at 11:28:18PM +0100, Stefano Sabatini wrote:
> > [...]
> > > > > > It turned out in a semi-complete rewrite of the test program, anyway I
> > > > > > don't feel like to send a patch and discuss every single change, if
> > > > > > it's possible to simply replace the new code I'll happily do it.
> > >
> > > you know likely (after actually thinking about it at least)
> > > that diffing old code against rewritten codeis not that usefull
> > > for the purpose of reading/reviewing
> >
> > Here it is:
> >
> > -----8<-----8<------------------------------------------------------
> > #ifdef TEST
> > #include "log.h"
> > #include "mem.h"
> >
> > #undef printf
>
> > #undef srand
> > #undef rand
>
> is there a problem with our random number generators?
>
>
> >
> > int main(void)
> > {
> > int numerr = 0;
> >
> > struct test {
>
> > void *data;
>
> looks like const char
OK to use const uint8_t to stress the fact that it is generic data
rather than a string?
> > int data_size;
> > const char *encoded;
> > } *t, tests[] = {
> > { "", 0, ""},
> > { "1", 1, "MQ=="},
> > { "22", 2, "MjI="},
> > { "333", 3, "MzMz"},
> > { "4444", 4, "NDQ0NA=="},
> > { "55555", 5, "NTU1NTU="},
> > { "abc:def", 7, "YWJjOmRlZg=="},
> > { NULL}
> > };
> >
> > printf("Encoding/decoding tests on constant data\n");
> > for (t = tests; t->data; t++) {
> > char encoded[1024];
> > uint8_t data[1024];
> > int data_size;
> >
> > printf("Encoding '%s'... ", (char *)t->data);
> > if (av_base64_encode(encoded, sizeof(encoded), t->data, t->data_size)) {
> > printf("encoded to '%s'\n", encoded);
>
> > if (strcmp(encoded, t->encoded) != 0) {
>
> superflous != 0
>
>
> [...]
> > printf("\n");
> >
> > printf("Encoding/decoding tests on random data\n");
>
> can be merged
>
>
> > {
> > int test_count;
> > srand(123141); // time(NULL));
> > for (test_count = 0; test_count < 100; test_count++) {
> > int data_size = rand() % 1024;
> > char *encoded[2048];
>
> > uint8_t *data = (uint8_t *)av_malloc(data_size);
>
> useless cast
New version:
---------------------------------8<-------------------------------------------
#ifdef TEST
#include "log.h"
#include "mem.h"
#include "lfg.h"
#undef printf
int main(void)
{
int numerr = 0;
struct test {
const uint8_t *data;
int data_size;
const char *encoded;
} *t, tests[] = {
{ "", 0, ""},
{ "1", 1, "MQ=="},
{ "22", 2, "MjI="},
{ "333", 3, "MzMz"},
{ "4444", 4, "NDQ0NA=="},
{ "55555", 5, "NTU1NTU="},
{ "abc:def", 7, "YWJjOmRlZg=="},
{ NULL}
};
printf("Encoding/decoding tests on constant data\n");
for (t = tests; t->data; t++) {
char encoded[1024];
uint8_t data[1024];
int data_size;
printf("Encoding '%s'... ", t->data);
if (av_base64_encode(encoded, sizeof(encoded), t->data, t->data_size)) {
printf("encoded to '%s'\n", encoded);
if (strcmp(encoded, t->encoded)) {
printf("failed: '%s' != '%s'\n", encoded, t->encoded);
numerr++;
}
}
printf("Decoding '%s'... ", t->encoded);
data_size = av_base64_decode(data, t->encoded, sizeof(data));
if (data_size != t->data_size) {
printf("failed: len %d != %d\n", data_size, t->data_size);
numerr++;
} else if (memcmp(data, t->data, t->data_size)) {
printf("failed: data differs\n");
numerr++;
} else {
printf("decoded to '%s'\n", t->data);
}
}
printf("\nEncoding/decoding tests on random data\n");
{
int test_count;
AVLFG lfg;
av_lfg_init(&lfg, 123456);
for (test_count = 0; test_count < 100; test_count++) {
int data_size = av_lfg_get(&lfg) % 1024;
char encoded[2048];
uint8_t *data = av_malloc(data_size);
int i;
for (i = 0; i < data_size; i++)
data[i] = av_lfg_get(&lfg) % 255;
printf("Test %d: data size %d bytes... ", test_count, data_size);
if (!av_base64_encode(encoded, sizeof(encoded), data, data_size)) {
printf("failed: cannot encode the input data\n");
numerr++;
} else {
int size = data_size + 10; // try without 10 as well
uint8_t *data2 = av_malloc(size);
if (data2) {
int data2_size = av_base64_decode(data2, encoded, size);
if (data2_size < 0) {
printf("failed: cannot decode the encoded string '%s'\n", encoded);
numerr++;
} else if (data2_size != data_size) {
printf("failed: decoded/encoded size mismatch (%d != %d)\n", data2_size, data_size);
} else {
if (memcmp(data2, data, data_size)) {
printf("failed: data differs!\n");
numerr++;
} else {
printf("passed!\n");
}
}
av_free(data2);
}
}
av_free(data);
}
}
printf("\nDecoding tests on constant invalid data\n");
{
uint8_t data[32];
int i;
const char *encoded[] = { "M", "M=M=", "MQ===" };
for (i=0; i < FF_ARRAY_ELEMS(encoded); i++) {
printf("Checking for validity of invalid encoded string '%s'... ", encoded[i]);
if (av_base64_decode(data, encoded[i], sizeof(data)) >= 0) {
printf("failed: data considered valid!\n");
numerr++;
}
else
printf("passed: data considered invalid\n");
}
}
return numerr;
}
#endif
---------------------------------8<-------------------------------------------
Regards.
--
FFmpeg = Funny and Foolish Maxi Peaceful Enigmatic God
More information about the ffmpeg-devel
mailing list