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

Stefano Sabatini stefano.sabatini-lala
Wed Jan 28 23:02:32 CET 2009


On date Wednesday 2009-01-28 20:58:32 +0100, Michael Niedermayer encoded:
> On Wed, Jan 28, 2009 at 08:44:09PM +0100, Stefano Sabatini wrote:
[...]
> >             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);
> 
> duplicate encode/decode test

New round:
-------------------------------8<-------------------------------
#ifdef TEST
#include "log.h"
#include "mem.h"
#include "lfg.h"

#undef printf

int main(void)
{
    int error_count = 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="},
        { "666666",  6, "NjY2NjY2"},
        { "abc:def", 7, "YWJjOmRlZg=="},
        { NULL}
    };

    printf("Encoding tests on constant data\n");
    for (t = tests; t->data; t++) {
        char encoded[1024];

        printf("Encoding '%s'... ", t->data);
        if (!av_base64_encode(encoded, sizeof(encoded), t->data, t->data_size)) {
            printf("failed: cannot encode the input data\n");
            error_count++;
        } else {
            printf("encoded as '%s'... ", encoded);
            if (strcmp(encoded, t->encoded)) {
                printf("failed: '%s' != '%s'\n", encoded, t->encoded);
                error_count++;
            }
            else
                printf("passed!\n");
        }
    }

    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++) {
            uint8_t data[1024];
            int data_size = av_lfg_get(&lfg) % sizeof(data);
            char encoded[2048];
            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");
                error_count++;
            } else {
                int size = data_size + 10;     // try without 10 as well
                uint8_t *data2 = av_malloc(size);
                if (!data2) {
                    printf("failed: cannot allocate data\n");
                    error_count++;
                } else {
                    int data2_size = av_base64_decode(data2, encoded, size);
                    if (data2_size < 0) {
                        printf("failed: cannot decode the encoded string '%s'\n", encoded);
                        error_count++;
                    } 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");
                            error_count++;
                        } else {
                            printf("passed!\n");
                        }
                    }
                    av_free(data2);
                }
            }
        }
    }

    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("Decoding invalid encoded string '%s'... ", encoded[i]);
            if (av_base64_decode(data, encoded[i], sizeof(data)) >= 0) {
                printf("failed: data considered valid!\n");
                error_count++;
            }
            else
                printf("passed: data considered invalid!\n");
        }
    }

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

Regards.
-- 
FFmpeg = Fiendish Fundamental Mystic Puritan Earthshaking Game




More information about the ffmpeg-devel mailing list