Add tests for the off-by-one bug we just fixed in mpi_cmp_abs().

This commit is contained in:
Dag-Erling Smørgrav 2014-12-30 16:38:47 +00:00 committed by des
parent 1c024928f9
commit 2027ce33e7

View file

@ -686,20 +686,22 @@ t_mpi_large_load_fail(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
static struct t_cmp_case {
const char *desc;
uint8_t a[16];
size_t alen;
uint8_t b[16];
size_t blen;
int cmpabs, cmp;
int a, b, cmp;
} t_cmp_cases[] = {
{
"0 == 0",
{ 0x00, }, 1,
{ 0x00, }, 1,
0, 0,
},
{ "-1 == -1", -1, -1, 0 },
{ "-1 < 0", -1, 0, -1 },
{ "-1 < 1", -1, 1, -1 },
{ "0 > -1", 0, -1, 1 },
{ "0 == 0", 0, 0, 0 },
{ "0 < 1", 0, 1, -1 },
{ "1 > -1", 1, -1, 1 },
{ "1 > 0", 1, 0, 1 },
{ "1 == 1", 1, 1, 0 },
};
/*
* Compare two MPIs
*/
static int
t_mpi_cmp(char **desc CRYB_UNUSED, void *arg)
{
@ -707,9 +709,28 @@ t_mpi_cmp(char **desc CRYB_UNUSED, void *arg)
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO;
int ret = 1;
mpi_load(&a, tc->a, tc->alen);
mpi_load(&b, tc->b, tc->blen);
ret &= t_compare_i(tc->cmpabs, mpi_cmp_abs(&a, &b));
mpi_set(&a, tc->a);
mpi_set(&b, tc->b);
ret &= t_compare_i(tc->cmp, mpi_cmp(&a, &b));
mpi_destroy(&a);
mpi_destroy(&b);
return (ret);
}
/*
* Compare two MPIs with garbage past the msb
*/
static int
t_mpi_cmp_garbage(char **desc CRYB_UNUSED, void *arg)
{
struct t_cmp_case *tc = arg;
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO;
int ret = 1;
mpi_set(&a, tc->a);
a.words[1] = 2;
mpi_set(&b, tc->b);
b.words[1] = tc->cmp < 1 ? 1 : 3;
ret &= t_compare_i(tc->cmp, mpi_cmp(&a, &b));
mpi_destroy(&a);
mpi_destroy(&b);
@ -1405,8 +1426,11 @@ t_prepare(int argc, char *argv[])
t_add_test(t_mpi_large_load_fail, NULL, "large load (failure)");
/* comparison */
for (i = 0; i < sizeof t_cmp_cases / sizeof t_cmp_cases[0]; ++i)
for (i = 0; i < sizeof t_cmp_cases / sizeof t_cmp_cases[0]; ++i) {
t_add_test(t_mpi_cmp, &t_cmp_cases[i], t_cmp_cases[i].desc);
t_add_test(t_mpi_cmp_garbage, &t_cmp_cases[i],
"%s (trailing garbage)", t_cmp_cases[i].desc);
}
/* left / right shift */
for (i = 0; i < sizeof t_lsh_cases / sizeof t_lsh_cases[0]; ++i)