test/vtophys: refactor the app to use CUnit

The vtophys tests are about to be expanded with extra
cases, for which CU_ASSERT macros will come useful.

Change-Id: Id52e8df5e4bb2a0d842c174e5ac2969cc9dbbaee
Signed-off-by: Darek Stojaczyk <dariusz.stojaczyk@intel.com>
Reviewed-on: https://review.gerrithub.io/c/438448
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Chandler-Test-Pool: SPDK Automated Test System <sys_sgsw@intel.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
This commit is contained in:
Darek Stojaczyk 2018-12-28 13:54:52 +01:00 committed by Jim Harris
parent 641c5b0018
commit daa3b8b2e0

View File

@ -35,86 +35,64 @@
#include "spdk/env.h"
static int
vtophys_negative_test(void)
#include "CUnit/Basic.h"
static void
vtophys_malloc_test(void)
{
void *p = NULL;
int i;
unsigned int size = 1;
int rc = 0;
uint64_t paddr;
/* Verify vtophys doesn't work on regular malloc memory */
for (i = 0; i < 31; i++) {
p = malloc(size);
if (p == NULL) {
continue;
}
if (spdk_vtophys(p) != SPDK_VTOPHYS_ERROR) {
rc = -1;
printf("Err: VA=%p is mapped to a huge_page,\n", p);
free(p);
break;
}
paddr = spdk_vtophys(p);
CU_ASSERT(paddr == SPDK_VTOPHYS_ERROR);
free(p);
size = size << 1;
}
/* Test addresses that are not in the valid x86-64 usermode range */
if (spdk_vtophys((void *)0x0000800000000000ULL) != SPDK_VTOPHYS_ERROR) {
rc = -1;
printf("Err: kernel-mode address incorrectly allowed\n");
}
if (!rc) {
printf("vtophys_negative_test passed\n");
} else {
printf("vtophys_negative_test failed\n");
}
return rc;
paddr = spdk_vtophys((void *)0x0000800000000000ULL);
CU_ASSERT(paddr == SPDK_VTOPHYS_ERROR)
}
static int
vtophys_positive_test(void)
static void
vtophys_spdk_malloc_test(void)
{
void *p = NULL;
int i;
unsigned int size = 1;
int rc = 0;
uint64_t paddr;
/* Test vtophys on memory allocated through SPDK */
for (i = 0; i < 31; i++) {
p = spdk_dma_zmalloc(size, 512, NULL);
if (p == NULL) {
continue;
}
if (spdk_vtophys(p) == SPDK_VTOPHYS_ERROR) {
rc = -1;
printf("Err: VA=%p is not mapped to a huge_page,\n", p);
spdk_dma_free(p);
break;
}
paddr = spdk_vtophys(p);
CU_ASSERT(paddr != SPDK_VTOPHYS_ERROR);
spdk_dma_free(p);
size = size << 1;
}
if (!rc) {
printf("vtophys_positive_test passed\n");
} else {
printf("vtophys_positive_test failed\n");
}
return rc;
}
int
main(int argc, char **argv)
{
int rc;
struct spdk_env_opts opts;
struct spdk_env_opts opts;
CU_pSuite suite = NULL;
unsigned num_failures;
spdk_env_opts_init(&opts);
opts.name = "vtophys";
@ -124,11 +102,27 @@ main(int argc, char **argv)
return 1;
}
rc = vtophys_negative_test();
if (rc < 0) {
return rc;
if (CU_initialize_registry() != CUE_SUCCESS) {
return CU_get_error();
}
rc = vtophys_positive_test();
return rc;
suite = CU_add_suite("components_suite", NULL, NULL);
if (suite == NULL) {
CU_cleanup_registry();
return CU_get_error();
}
if (
CU_add_test(suite, "vtophys_malloc_test", vtophys_malloc_test) == NULL ||
CU_add_test(suite, "vtophys_spdk_malloc_test", vtophys_spdk_malloc_test) == NULL
) {
CU_cleanup_registry();
return CU_get_error();
}
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
num_failures = CU_get_number_of_failures();
CU_cleanup_registry();
return num_failures;
}