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

Stefano Sabatini stefano.sabatini-lala
Fri Jan 30 00:46:36 CET 2009


On date Thursday 2009-01-29 03:11:32 +0100, Michael Niedermayer encoded:
> On Thu, Jan 29, 2009 at 12:21:09AM +0100, Stefano Sabatini wrote:
> > 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;
> >     }
> 
> encoded[MAX_SIZE]
> 
> 
> >     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;
> >     }
> 
> its enough to print it when it differs
> whats the strlen() check supposed to do?
> 
> 
> > 
> >     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;
> >     }
> 
> get rid of all malloc() please this is supposed to be a small test program not a big
> bloated thing that supports everything it will never be feeded with
> 
> 
> > 
> >     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;
> >     }
> 
> i assume data_size is not <0 so the 2nd check is enough
> 
> 
> >     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++;
> 
> error_count += func();
> 
> 
> > 
> >     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];
>                            ^^^^
> #define SOME_WISE_NAME_TAT_MEANS_MAX_OF_THE_THING
> no not the one starting with V ;)
> 
> 
> >             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;
> 
> this will just use 0..254
> 
> 
> > 
> >             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");
> >         }
> >     }
> 
> next try to factorize the code
> hint:
> 
> for(counter){
>     if(counter < C1){
>         ret= test_encode_decode(table[counter])
>         if(counter < C0)
>             ret= !ret;
>     }else{
>         for()
>             temp[]= random
>         ret= test_encode_decode(temp)
>     }
>     if(ret)
>         printf("error blah blah this and that happened and we expected that")
> }

I think I addressed all the points.

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

#undef printf

#define MAX_DATA_SIZE    1024
#define MAX_ENCODED_SIZE 2048

#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)
{
    char encoded[MAX_ENCODED_SIZE];
    uint8_t data2[MAX_DATA_SIZE];
    int data2_size, max_data2_size = MAX_DATA_SIZE;

    printf("Encoding data with size %d bytes '%s'... ", data_size, SHOW_STUFF(data));
    if (!av_base64_encode(encoded, MAX_ENCODED_SIZE, data, data_size)) {
        printf("failed: cannot encode the input data\n");
        return 1;
    }
    printf("encoded as '%s'... ", SHOW_STUFF(encoded));
    if (encoded_ref && strcmp(encoded, encoded_ref)) {
        printf("failed: differs from reference '%s'\n", SHOW_STUFF(encoded_ref));
        return 1;
    }
    if ((data2_size = av_base64_decode(data2, encoded, max_data2_size)) < 0) {
        printf("failed: cannot decode the encoded string '%s'\n", SHOW_STUFF(encoded));
        return 1;
    }
    if (memcmp(data2, data, data_size)) {
        printf("failed: data differs\n");
        return 1;
    }
    printf("passed!\n");
    return 0;
}

int main(void)
{
    int error_count = 0;

    printf("Encoding/decoding tests\n");
    {
        struct ref_test {
            const uint8_t *data;
            int data_size;
            const char *encoded_ref;
        } ref_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}
        };
        unsigned int ref_tests_nb = FF_ARRAY_ELEMS(ref_tests);
        AVLFG lfg;

        av_lfg_init(&lfg, 123456);
        for (int i = 0; i < 100 + ref_tests_nb; i++) {
            unsigned int data_size;
            const uint8_t *data;
            uint8_t data_buf[MAX_DATA_SIZE];
            const char *encoded_ref = NULL;

            if (i < ref_tests_nb) {
                data        = ref_tests[i].data;
                data_size   = ref_tests[i].data_size;
                encoded_ref = ref_tests[i].encoded_ref;
            } else {
                data_size = av_lfg_get(&lfg) % (MAX_DATA_SIZE + 1);
                for (int j = 0; j < data_size; j++)
                    data_buf[i] = av_lfg_get(&lfg);
                data = data_buf;
            }

            error_count += test_encode_decode(data, data_size, encoded_ref, !!encoded_ref);
        }
    }

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

        for (int 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<----------------------8<-------------------------

Regards.
-- 
FFmpeg = Fantastic and Fostering Multimedia Pitiful Elected Goblin




More information about the ffmpeg-devel mailing list