예제 #1
0
static char* testEncodeDecode(void)
{
    // 2 bytes == 4 bytes out
    char ibuf[2];
    char obuf[5];
    char rbuf[4];
    char msg[100]; // for test messages output
    msg[0] = 0; // make msg an empty string
    unsigned int i,j;
    size_t d;

    for (i = 0; i < 256; ++i) {
        for (j = 0; j < 256; ++j) {
            // comment this out.. it really slows down the test
            sprintf(msg, "(i,j) = (%d,%d):", i,j);
            ibuf[0] = (char)((unsigned char) i);
            ibuf[1] = (char)((unsigned char) j);

            memset(obuf, 0, sizeof(obuf));
            d = modp_b16_encode(obuf, ibuf, (size_t)2);
            mu_assert_int_equals_msg(msg, 4, d);
            d = modp_b16_decode(rbuf, obuf, d);
            mu_assert_int_equals_msg(msg, 2, d);
            mu_assert_int_equals_msg(msg, ibuf[0], rbuf[0]);
            mu_assert_int_equals_msg(msg, ibuf[1], rbuf[1]);
        }
    }

    return 0;
}
예제 #2
0
static char* testDecodeErrors(void)
{
    int i;
    size_t y;
    char out[1000];
    char decode[5];
    char msg[100];

    /* negative length */
    memset(decode, 1, sizeof(decode));
    y = modp_b64_decode(out, decode, (size_t)-1);
    mu_assert_int_equals(-1, y);

    /* test bad input -  all combinations */
    char goodchar = 'A';
    char badchar = '~';
    for (i = 1; i < 16; ++i) {
        decode[0] = (char)(((i & 0x01) == 0) ? goodchar : badchar);
        decode[1] = (char)(((i & 0x02) == 0) ? goodchar : badchar);
        decode[2] = (char)(((i & 0x04) == 0) ? goodchar : badchar);
        decode[3] = (char)(((i & 0x08) == 0) ? goodchar : badchar);
        decode[4] = '\0';

        sprintf(msg, "i = %d, %s", i, decode);
        y = modp_b64_decode(out, decode, (size_t)4);
        mu_assert_int_equals_msg(msg, (size_t)-1, y);
    }


    /*  test just 1-4 padchars */
    for (i = 0; i < 4; ++i) {
        decode[i] = '=';
        decode[i+1] = '\0';
        y = modp_b64_decode(out, decode, (size_t)(i+1));
        sprintf(msg, "i=%d, b64=%s", i, decode);
        mu_assert_int_equals_msg(msg, (size_t)-1, y);
    }

    /* Test good+3 pad chars (should be impossible) */
    decode[0] = 'A';
    decode[1] = '=';
    decode[2] = '=';
    decode[3] = '=';
    y = modp_b64_decode(out, decode, (size_t)4);
    mu_assert_int_equals(-1, y);

    return 0;
}
예제 #3
0
/**
 * Test all 17M 3 bytes inputs to encoder, decode
 * and make sure it's equal.
 */
static char* testEncodeDecode(void)
{
    char ibuf[4];
    char obuf[5];
    char rbuf[4];
    char msg[100];
    msg[0] = 0; /* make msg an empty string */
    unsigned int i, j, k;
    size_t d = 0;
    for (i = 0; i < 256; ++i) {
        for (j = 0; j < 256; ++j) {
            for (k = 0; k < 256; ++k) {
                /* comment this out.. it really slows down the test */
                /* sprintf(msg, "(i,j,k) = (%d,%d,%d):", i,j,k); */
                ibuf[0] = (char)((unsigned char)i);
                ibuf[1] = (char)((unsigned char)j);
                ibuf[2] = (char)((unsigned char)k);
                ibuf[3] = 0;

                memset(obuf, 1, sizeof(obuf));
                d = modp_b64_encode(obuf, ibuf, (size_t)3);
                mu_assert_int_equals_msg(msg, 4, d);
                mu_assert_int_equals_msg(msg, 0, obuf[4]);

                memset(rbuf, 1, sizeof(rbuf));
                d = modp_b64_decode(rbuf, obuf, (size_t)4);
                mu_assert_int_equals_msg(msg, 3, d);
                mu_assert_int_equals_msg(msg, ibuf[0], rbuf[0]);
                mu_assert_int_equals_msg(msg, ibuf[1], rbuf[1]);
                mu_assert_int_equals_msg(msg, ibuf[2], rbuf[2]);
                mu_assert_int_equals_msg(msg, 1, rbuf[3]);
            }
        }
    }
    return 0;
}
예제 #4
0
/**
 * Test 1-6 bytes input and decode
 *
 */
static char* testPadding(void)
{
    char msg[100];
    const char ibuf[6] = { 1, 1, 1, 1, 1, 1 };
    char obuf[10];
    char rbuf[10];
    size_t d = 0;

    /* 1 in, 4 out */
    memset(obuf, 255, sizeof(obuf));
    d = modp_b64_encode(obuf, ibuf, (size_t)1);
    sprintf(msg, "b64='%s', d=%d", obuf, (int)d);
    mu_assert_int_equals_msg(msg, 4, d);
    mu_assert_int_equals_msg(msg, 0, obuf[4]);
    memset(rbuf, 255, sizeof(rbuf));
    d = modp_b64_decode(rbuf, obuf, d);
    mu_assert_int_equals_msg(msg, 1, d);
    mu_assert_int_equals(1, rbuf[0]);
    mu_assert_int_equals(-1, rbuf[1]);

    /* 2 in, 4 out */
    memset(obuf, 255, sizeof(obuf));
    d = modp_b64_encode(obuf, ibuf, (size_t)2);
    sprintf(msg, "b64='%s', d=%d", obuf, (int)d);
    mu_assert_int_equals_msg(msg, 4, d);
    mu_assert_int_equals_msg(msg, 0, obuf[4]);
    memset(rbuf, 255, sizeof(rbuf));
    d = modp_b64_decode(rbuf, obuf, d);
    mu_assert_int_equals_msg(msg, 2, d);
    mu_assert_int_equals_msg(msg, 1, rbuf[0]);
    mu_assert_int_equals_msg(msg, 1, rbuf[1]);
    mu_assert_int_equals_msg(msg, -1, rbuf[2]);

    /* 3 in, 4 out */
    memset(obuf, 255, sizeof(obuf));
    d = modp_b64_encode(obuf, ibuf, (size_t)3);
    sprintf(msg, "b64='%s', d=%d", obuf, (int)d);
    mu_assert_int_equals_msg(msg, 4, d);
    mu_assert_int_equals_msg(msg, 0, obuf[4]);
    memset(rbuf, 255, sizeof(rbuf));
    d = modp_b64_decode(rbuf, obuf, d);
    mu_assert_int_equals_msg(msg, 3, d);
    mu_assert_int_equals_msg(msg, 1, rbuf[0]);
    mu_assert_int_equals_msg(msg, 1, rbuf[1]);
    mu_assert_int_equals_msg(msg, 1, rbuf[2]);
    mu_assert_int_equals_msg(msg, -1, rbuf[3]);

    /* 4 in, 8 out */
    memset(obuf, 255, sizeof(obuf));
    d = modp_b64_encode(obuf, ibuf, (size_t)4);
    sprintf(msg, "b64='%s', d=%d", obuf, (int)d);
    mu_assert_int_equals_msg(msg, 8, d);
    mu_assert_int_equals_msg(msg, 0, obuf[8]);
    memset(rbuf, 255, sizeof(rbuf));
    d = modp_b64_decode(rbuf, obuf, d);
    mu_assert_int_equals(4, d);
    mu_assert_int_equals(1, rbuf[0]);
    mu_assert_int_equals(1, rbuf[1]);
    mu_assert_int_equals(1, rbuf[2]);
    mu_assert_int_equals(1, rbuf[3]);
    mu_assert_int_equals(-1, rbuf[4]);

    /* 5 in, 8 out */
    memset(obuf, 255, sizeof(obuf));
    d = modp_b64_encode(obuf, ibuf, (size_t)5);
    sprintf(msg, "b64='%s', d=%d", obuf, (int)d);
    mu_assert_int_equals_msg(msg, 8, d);
    mu_assert_int_equals_msg(msg, 0, obuf[8]);
    memset(rbuf, 255, sizeof(rbuf));
    d = modp_b64_decode(rbuf, obuf, d);
    mu_assert_int_equals(5, d);
    mu_assert_int_equals(1, rbuf[0]);
    mu_assert_int_equals(1, rbuf[1]);
    mu_assert_int_equals(1, rbuf[2]);
    mu_assert_int_equals(1, rbuf[3]);
    mu_assert_int_equals(1, rbuf[4]);
    mu_assert_int_equals(-1, rbuf[5]);

    /* 6 in, 8 out */
    memset(obuf, 255, sizeof(obuf));
    d = modp_b64_encode(obuf, ibuf, (size_t)6);
    sprintf(msg, "b64='%s', d=%d", obuf, (int)d);
    mu_assert_int_equals_msg(msg, 8, d);
    mu_assert_int_equals_msg(msg, 0, obuf[8]);
    memset(rbuf, 255, sizeof(rbuf));
    d = modp_b64_decode(rbuf, obuf, d);
    mu_assert_int_equals(6, d);
    mu_assert_int_equals(1, rbuf[0]);
    mu_assert_int_equals(1, rbuf[1]);
    mu_assert_int_equals(1, rbuf[2]);
    mu_assert_int_equals(1, rbuf[3]);
    mu_assert_int_equals(1, rbuf[4]);
    mu_assert_int_equals(1, rbuf[5]);
    mu_assert_int_equals(-1, rbuf[6]);

    return 0;
}