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

Stefano Sabatini stefano.sabatini-lala
Thu Jan 29 00:21:09 CET 2009


On date Wednesday 2009-01-28 23:24:39 +0100, Michael Niedermayer encoded:
> On Wed, Jan 28, 2009 at 11:02:32PM +0100, Stefano Sabatini wrote:
[...]
> the code is not factorized out, its still containing 2 seperate pages
> of code that check equality and print stuff
> 
> you want a function, a single function that decodes, encodes, tests, and
> prints that then is feeded with random and not random data

Round 4:
------------------------------------------8<-------------------------------
#ifdef TEST
#include "mem.h"
#include "lfg.h"

#undef printf

#define SHOW_STUFF(stuff) show_stuff ? (const char *)stuff : "[...]"

int test_encode_decode(const char *data, unsigned int data_size, const char *encoded_ref, int show_stuff)
{
    unsigned int encoded_size = data_size * 4 / 3 + 12;
    char *encoded = 0;
    uint8_t *data2 = 0;
    int size, data2_size, ret = -1;

    printf("Encoding data with size %d bytes '%s'... ", data_size, SHOW_STUFF(data));
    if (!(encoded = av_malloc(encoded_size))) {
        printf("failed: cannot allocate the buffer for the encoded string\n");
        goto the_end;
    }
    if (!av_base64_encode(encoded, encoded_size, data, data_size)) {
        printf("failed: cannot encode the input data\n");
        goto the_end;
    }
    printf("encoded as '%s'... ", SHOW_STUFF(encoded));
    if (encoded_ref && (strlen(encoded) != strlen(encoded_ref) || strcmp(encoded, encoded_ref))) {
        printf("failed: differs from reference '%s'\n", SHOW_STUFF(encoded_ref));
        goto the_end;
    }

    size = data_size + 10;     // try without 10 as well
    if (!(data2 = av_malloc(size))) {
        printf("failed: cannot allocate the buffer for the decoded data\n");
        goto the_end;
    }

    data2_size = av_base64_decode(data2, encoded, size);
    if (data2_size < 0) {
        printf("failed: cannot decode the encoded string '%s'\n", SHOW_STUFF(encoded));
        goto the_end;
    }
    if (data2_size != data_size) {
        printf("failed: decoded/encoded size mismatch (%d != %d)\n", data2_size, data_size);
        goto the_end;
    }
    if (memcmp(data2, data, data_size)) {
        printf("failed: data differs\n");
        goto the_end;
    }
    printf("passed!\n");
    ret = 0;

the_end:
    av_free(data2);
    av_free(encoded);
    return ret;
}

int main(void)
{
    int error_count = 0;

    struct test {
        const uint8_t *data;
        int data_size;
        const char *encoded_ref;
    } *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/decoding tests on constant data with reference\n");
    for (t = tests; t->data; t++)
        if (test_encode_decode(t->data, t->data_size, t->encoded_ref, 1))
            error_count++;

    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) + 1);
            for (int i = 0; i < data_size; i++)
                data[i] = av_lfg_get(&lfg) % 255;

            if (test_encode_decode(data, data_size, NULL, 0))
                error_count++;
        }
    }

    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 = Funny and Fostering Merciful Ponderous Erratic Goblin




More information about the ffmpeg-devel mailing list