[FFmpeg-devel] [PATCH] remove unused and broken test program in libavutil/base64.c

Stefano Sabatini stefano.sabatini-lala
Wed Jan 28 00:02:34 CET 2009


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

int main(void)
{
    int numerr = 0;

    struct test {
        void *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'... ", (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) {
                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) != 0) {
            printf("failed: data differs\n");
            numerr++;
        } else {
            printf("decoded to '%s'\n", (char *)t->data);
        }
    }
    printf("\n");

    printf("Encoding/decoding tests on random data\n");
    {
        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);
            int i;

            for (i = 0; i < data_size; i++)
                data[i] = rand() % 255;

            printf("Test %d: data size %d bytes... ", test_count, data_size);
            if (av_base64_encode(encoded, sizeof(encoded), data, data_size)) {
                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("invalid input data sequence: '%s'\n", encoded);
                    else if (data2_size != data_size) {
                        printf("decoded/encoded size mismatch (%d != %d)\n", data2_size, data_size);
                    } else {
                        if (memcmp(data2, data, data_size)) {
                            printf("failed: data differs!\n");
                        } else {
                            printf("passed!\n");
                        }
                    }
                    av_free(data2);
                }
            }
            else
                printf("impossible to encode the input data\n");
        }
    }
    printf("\n");

    printf("Decoding tests on constant invalid data\n");
    {
        uint8_t data[32];
        int i;
        char *encoded[] = { "M", "M=M=", "MQ===" };

        for (i=0; i < FF_ARRAY_ELEMS(encoded); i++) {
            printf("Checking for validity of invalid encoded sequence '%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");
        }
    }
    printf("\n");

    return numerr;
}
#endif
-----8<-----8<------------------------------------------------------

Regards.
-- 
FFmpeg = Fostering and Faboulous Majestic Purposeless Earthshaking Guide




More information about the ffmpeg-devel mailing list