Move the calloc buffer initialization test to selftest.c

This way it's more in line with the 2.28 version.
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
This commit is contained in:
Andrzej Kurek 2023-04-27 09:30:18 -04:00
parent 2d981f092e
commit 9032711dc7
3 changed files with 41 additions and 21 deletions

View file

@ -73,23 +73,49 @@ static int calloc_self_test(int verbose)
void *empty2 = mbedtls_calloc(0, 1); void *empty2 = mbedtls_calloc(0, 1);
void *buffer1 = mbedtls_calloc(1, 1); void *buffer1 = mbedtls_calloc(1, 1);
void *buffer2 = mbedtls_calloc(1, 1); void *buffer2 = mbedtls_calloc(1, 1);
unsigned int buf_size = 256;
unsigned char *buffer3 = mbedtls_calloc(buf_size, sizeof(unsigned char));
if (empty1 == NULL && empty2 == NULL) { if (empty1 == NULL && empty2 == NULL) {
if (verbose) { if (verbose) {
mbedtls_printf(" CALLOC(0): passed (NULL)\n"); mbedtls_printf(" CALLOC(0,1): passed (NULL)\n");
} }
} else if (empty1 == NULL || empty2 == NULL) { } else if (empty1 == NULL || empty2 == NULL) {
if (verbose) { if (verbose) {
mbedtls_printf(" CALLOC(0): failed (mix of NULL and non-NULL)\n"); mbedtls_printf(" CALLOC(0,1): failed (mix of NULL and non-NULL)\n");
} }
++failures; ++failures;
} else if (empty1 == empty2) { } else if (empty1 == empty2) {
if (verbose) { if (verbose) {
mbedtls_printf(" CALLOC(0): passed (same non-null)\n"); mbedtls_printf(" CALLOC(0,1): passed (same non-null)\n");
} }
} else { } else {
if (verbose) { if (verbose) {
mbedtls_printf(" CALLOC(0): passed (distinct non-null)\n"); mbedtls_printf(" CALLOC(0,1): passed (distinct non-null)\n");
}
}
mbedtls_free(empty1);
mbedtls_free(empty2);
empty1 = mbedtls_calloc(1, 0);
empty2 = mbedtls_calloc(1, 0);
if (empty1 == NULL && empty2 == NULL) {
if (verbose) {
mbedtls_printf(" CALLOC(1,0): passed (NULL)\n");
}
} else if (empty1 == NULL || empty2 == NULL) {
if (verbose) {
mbedtls_printf(" CALLOC(1,0): failed (mix of NULL and non-NULL)\n");
}
++failures;
} else if (empty1 == empty2) {
if (verbose) {
mbedtls_printf(" CALLOC(1,0): passed (same non-null)\n");
}
} else {
if (verbose) {
mbedtls_printf(" CALLOC(1,0): passed (distinct non-null)\n");
} }
} }
@ -122,6 +148,16 @@ static int calloc_self_test(int verbose)
} }
} }
for (unsigned int i = 0; i < buf_size; i++) {
if (buffer3[i] != 0) {
++failures;
if (verbose) {
mbedtls_printf(" CALLOC(%u): failed (memory not initialized to 0)\n", buf_size);
}
break;
}
}
if (verbose) { if (verbose) {
mbedtls_printf("\n"); mbedtls_printf("\n");
} }
@ -129,6 +165,7 @@ static int calloc_self_test(int verbose)
mbedtls_free(empty2); mbedtls_free(empty2);
mbedtls_free(buffer1); mbedtls_free(buffer1);
mbedtls_free(buffer2); mbedtls_free(buffer2);
mbedtls_free(buffer3);
return failures; return failures;
} }
#endif /* MBEDTLS_SELF_TEST */ #endif /* MBEDTLS_SELF_TEST */

View file

@ -4,6 +4,3 @@ time_get_milliseconds:
Time: get seconds Time: get seconds
time_get_seconds: time_get_seconds:
Check mbedtls_calloc zeroization
check_mbedtls_calloc_zeroization:

View file

@ -120,17 +120,3 @@ void time_delay_seconds(int delay_secs)
goto exit; goto exit;
} }
/* END_CASE */ /* END_CASE */
/* BEGIN_CASE */
void check_mbedtls_calloc_zeroization()
{
unsigned int buf_size = 256;
unsigned char *buf;
buf = mbedtls_calloc(buf_size, sizeof(unsigned char));
for (unsigned int i = 0; i < buf_size; i++) {
TEST_EQUAL(buf[i], 0);
}
exit:
mbedtls_free(buf);
}
/* END_CASE */