Commit fa04b7ff authored by Muhammad Usama Anjum's avatar Muhammad Usama Anjum Committed by Shuah Khan

selftests/dmabuf-heap: conform test to TAP format output

Conform the layout, informational and status messages to TAP. No
functional change is intended other than the layout of output messages.
Improve the TAP messages as well.
Reviewed-by: default avatarT.J. Mercier <tjmercier@google.com>
Signed-off-by: default avatarMuhammad Usama Anjum <usama.anjum@collabora.com>
Signed-off-by: default avatarShuah Khan <skhan@linuxfoundation.org>
parent 5549a798
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include <linux/dma-buf.h> #include <linux/dma-buf.h>
#include <linux/dma-heap.h> #include <linux/dma-heap.h>
#include <drm/drm.h> #include <drm/drm.h>
#include "../kselftest.h"
#define DEVPATH "/dev/dma_heap" #define DEVPATH "/dev/dma_heap"
...@@ -90,14 +91,13 @@ static int dmabuf_heap_open(char *name) ...@@ -90,14 +91,13 @@ static int dmabuf_heap_open(char *name)
char buf[256]; char buf[256];
ret = snprintf(buf, 256, "%s/%s", DEVPATH, name); ret = snprintf(buf, 256, "%s/%s", DEVPATH, name);
if (ret < 0) { if (ret < 0)
printf("snprintf failed!\n"); ksft_exit_fail_msg("snprintf failed! %d\n", ret);
return ret;
}
fd = open(buf, O_RDWR); fd = open(buf, O_RDWR);
if (fd < 0) if (fd < 0)
printf("open %s failed!\n", buf); ksft_exit_fail_msg("open %s failed: %s\n", buf, strerror(errno));
return fd; return fd;
} }
...@@ -140,7 +140,7 @@ static int dmabuf_sync(int fd, int start_stop) ...@@ -140,7 +140,7 @@ static int dmabuf_sync(int fd, int start_stop)
#define ONE_MEG (1024 * 1024) #define ONE_MEG (1024 * 1024)
static int test_alloc_and_import(char *heap_name) static void test_alloc_and_import(char *heap_name)
{ {
int heap_fd = -1, dmabuf_fd = -1, importer_fd = -1; int heap_fd = -1, dmabuf_fd = -1, importer_fd = -1;
uint32_t handle = 0; uint32_t handle = 0;
...@@ -148,27 +148,19 @@ static int test_alloc_and_import(char *heap_name) ...@@ -148,27 +148,19 @@ static int test_alloc_and_import(char *heap_name)
int ret; int ret;
heap_fd = dmabuf_heap_open(heap_name); heap_fd = dmabuf_heap_open(heap_name);
if (heap_fd < 0)
return -1;
printf(" Testing allocation and importing: "); ksft_print_msg("Testing allocation and importing:\n");
ret = dmabuf_heap_alloc(heap_fd, ONE_MEG, 0, &dmabuf_fd); ret = dmabuf_heap_alloc(heap_fd, ONE_MEG, 0, &dmabuf_fd);
if (ret) { if (ret) {
printf("FAIL (Allocation Failed!)\n"); ksft_test_result_fail("FAIL (Allocation Failed!) %d\n", ret);
ret = -1; return;
goto out;
} }
/* mmap and write a simple pattern */ /* mmap and write a simple pattern */
p = mmap(NULL, p = mmap(NULL, ONE_MEG, PROT_READ | PROT_WRITE, MAP_SHARED, dmabuf_fd, 0);
ONE_MEG,
PROT_READ | PROT_WRITE,
MAP_SHARED,
dmabuf_fd,
0);
if (p == MAP_FAILED) { if (p == MAP_FAILED) {
printf("FAIL (mmap() failed)\n"); ksft_test_result_fail("FAIL (mmap() failed): %s\n", strerror(errno));
ret = -1; goto close_and_return;
goto out;
} }
dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_START); dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_START);
...@@ -178,71 +170,64 @@ static int test_alloc_and_import(char *heap_name) ...@@ -178,71 +170,64 @@ static int test_alloc_and_import(char *heap_name)
importer_fd = open_vgem(); importer_fd = open_vgem();
if (importer_fd < 0) { if (importer_fd < 0) {
ret = importer_fd; ksft_test_result_skip("Could not open vgem %d\n", importer_fd);
printf("(Could not open vgem - skipping): ");
} else { } else {
ret = import_vgem_fd(importer_fd, dmabuf_fd, &handle); ret = import_vgem_fd(importer_fd, dmabuf_fd, &handle);
if (ret < 0) { ksft_test_result(ret >= 0, "Import buffer %d\n", ret);
printf("FAIL (Failed to import buffer)\n");
goto out;
}
} }
ret = dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_START); ret = dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_START);
if (ret < 0) { if (ret < 0) {
printf("FAIL (DMA_BUF_SYNC_START failed!)\n"); ksft_print_msg("FAIL (DMA_BUF_SYNC_START failed!) %d\n", ret);
goto out; goto out;
} }
memset(p, 0xff, ONE_MEG); memset(p, 0xff, ONE_MEG);
ret = dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_END); ret = dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_END);
if (ret < 0) { if (ret < 0) {
printf("FAIL (DMA_BUF_SYNC_END failed!)\n"); ksft_print_msg("FAIL (DMA_BUF_SYNC_END failed!) %d\n", ret);
goto out; goto out;
} }
close_handle(importer_fd, handle); close_handle(importer_fd, handle);
ret = 0; ksft_test_result_pass("%s dmabuf sync succeeded\n", __func__);
printf(" OK\n"); return;
out: out:
if (p) ksft_test_result_fail("%s dmabuf sync failed\n", __func__);
munmap(p, ONE_MEG); munmap(p, ONE_MEG);
if (importer_fd >= 0) close(importer_fd);
close(importer_fd);
if (dmabuf_fd >= 0)
close(dmabuf_fd);
if (heap_fd >= 0)
close(heap_fd);
return ret; close_and_return:
close(dmabuf_fd);
close(heap_fd);
} }
static int test_alloc_zeroed(char *heap_name, size_t size) static void test_alloc_zeroed(char *heap_name, size_t size)
{ {
int heap_fd = -1, dmabuf_fd[32]; int heap_fd = -1, dmabuf_fd[32];
int i, j, ret; int i, j, k, ret;
void *p = NULL; void *p = NULL;
char *c; char *c;
printf(" Testing alloced %ldk buffers are zeroed: ", size / 1024); ksft_print_msg("Testing alloced %ldk buffers are zeroed:\n", size / 1024);
heap_fd = dmabuf_heap_open(heap_name); heap_fd = dmabuf_heap_open(heap_name);
if (heap_fd < 0)
return -1;
/* Allocate and fill a bunch of buffers */ /* Allocate and fill a bunch of buffers */
for (i = 0; i < 32; i++) { for (i = 0; i < 32; i++) {
ret = dmabuf_heap_alloc(heap_fd, size, 0, &dmabuf_fd[i]); ret = dmabuf_heap_alloc(heap_fd, size, 0, &dmabuf_fd[i]);
if (ret < 0) { if (ret) {
printf("FAIL (Allocation (%i) failed)\n", i); ksft_test_result_fail("FAIL (Allocation (%i) failed) %d\n", i, ret);
goto out; goto close_and_return;
} }
/* mmap and fill with simple pattern */ /* mmap and fill with simple pattern */
p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, dmabuf_fd[i], 0); p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, dmabuf_fd[i], 0);
if (p == MAP_FAILED) { if (p == MAP_FAILED) {
printf("FAIL (mmap() failed!)\n"); ksft_test_result_fail("FAIL (mmap() failed!): %s\n", strerror(errno));
ret = -1; goto close_and_return;
goto out;
} }
dmabuf_sync(dmabuf_fd[i], DMA_BUF_SYNC_START); dmabuf_sync(dmabuf_fd[i], DMA_BUF_SYNC_START);
memset(p, 0xff, size); memset(p, 0xff, size);
dmabuf_sync(dmabuf_fd[i], DMA_BUF_SYNC_END); dmabuf_sync(dmabuf_fd[i], DMA_BUF_SYNC_END);
...@@ -251,48 +236,47 @@ static int test_alloc_zeroed(char *heap_name, size_t size) ...@@ -251,48 +236,47 @@ static int test_alloc_zeroed(char *heap_name, size_t size)
/* close them all */ /* close them all */
for (i = 0; i < 32; i++) for (i = 0; i < 32; i++)
close(dmabuf_fd[i]); close(dmabuf_fd[i]);
ksft_test_result_pass("Allocate and fill a bunch of buffers\n");
/* Allocate and validate all buffers are zeroed */ /* Allocate and validate all buffers are zeroed */
for (i = 0; i < 32; i++) { for (i = 0; i < 32; i++) {
ret = dmabuf_heap_alloc(heap_fd, size, 0, &dmabuf_fd[i]); ret = dmabuf_heap_alloc(heap_fd, size, 0, &dmabuf_fd[i]);
if (ret < 0) { if (ret < 0) {
printf("FAIL (Allocation (%i) failed)\n", i); ksft_test_result_fail("FAIL (Allocation (%i) failed) %d\n", i, ret);
goto out; goto close_and_return;
} }
/* mmap and validate everything is zero */ /* mmap and validate everything is zero */
p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, dmabuf_fd[i], 0); p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, dmabuf_fd[i], 0);
if (p == MAP_FAILED) { if (p == MAP_FAILED) {
printf("FAIL (mmap() failed!)\n"); ksft_test_result_fail("FAIL (mmap() failed!): %s\n", strerror(errno));
ret = -1; goto close_and_return;
goto out;
} }
dmabuf_sync(dmabuf_fd[i], DMA_BUF_SYNC_START); dmabuf_sync(dmabuf_fd[i], DMA_BUF_SYNC_START);
c = (char *)p; c = (char *)p;
for (j = 0; j < size; j++) { for (j = 0; j < size; j++) {
if (c[j] != 0) { if (c[j] != 0) {
printf("FAIL (Allocated buffer not zeroed @ %i)\n", j); ksft_print_msg("FAIL (Allocated buffer not zeroed @ %i)\n", j);
break; dmabuf_sync(dmabuf_fd[i], DMA_BUF_SYNC_END);
munmap(p, size);
goto out;
} }
} }
dmabuf_sync(dmabuf_fd[i], DMA_BUF_SYNC_END); dmabuf_sync(dmabuf_fd[i], DMA_BUF_SYNC_END);
munmap(p, size); munmap(p, size);
} }
/* close them all */
for (i = 0; i < 32; i++)
close(dmabuf_fd[i]);
close(heap_fd);
printf("OK\n");
return 0;
out: out:
while (i > 0) { ksft_test_result(i == 32, "Allocate and validate all buffers are zeroed\n");
close(dmabuf_fd[i]);
i--; close_and_return:
} /* close them all */
for (k = 0; k < i; k++)
close(dmabuf_fd[k]);
close(heap_fd); close(heap_fd);
return ret; return;
} }
/* Test the ioctl version compatibility w/ a smaller structure then expected */ /* Test the ioctl version compatibility w/ a smaller structure then expected */
...@@ -360,126 +344,97 @@ static int dmabuf_heap_alloc_newer(int fd, size_t len, unsigned int flags, ...@@ -360,126 +344,97 @@ static int dmabuf_heap_alloc_newer(int fd, size_t len, unsigned int flags,
return ret; return ret;
} }
static int test_alloc_compat(char *heap_name) static void test_alloc_compat(char *heap_name)
{ {
int heap_fd = -1, dmabuf_fd = -1; int ret, heap_fd = -1, dmabuf_fd = -1;
int ret;
heap_fd = dmabuf_heap_open(heap_name); heap_fd = dmabuf_heap_open(heap_name);
if (heap_fd < 0)
return -1;
printf(" Testing (theoretical)older alloc compat: "); ksft_print_msg("Testing (theoretical) older alloc compat:\n");
ret = dmabuf_heap_alloc_older(heap_fd, ONE_MEG, 0, &dmabuf_fd); ret = dmabuf_heap_alloc_older(heap_fd, ONE_MEG, 0, &dmabuf_fd);
if (ret) { if (dmabuf_fd >= 0)
printf("FAIL (Older compat allocation failed!)\n"); close(dmabuf_fd);
ret = -1; ksft_test_result(!ret, "dmabuf_heap_alloc_older\n");
goto out;
}
close(dmabuf_fd);
printf("OK\n");
printf(" Testing (theoretical)newer alloc compat: "); ksft_print_msg("Testing (theoretical) newer alloc compat:\n");
ret = dmabuf_heap_alloc_newer(heap_fd, ONE_MEG, 0, &dmabuf_fd); ret = dmabuf_heap_alloc_newer(heap_fd, ONE_MEG, 0, &dmabuf_fd);
if (ret) {
printf("FAIL (Newer compat allocation failed!)\n");
ret = -1;
goto out;
}
printf("OK\n");
out:
if (dmabuf_fd >= 0) if (dmabuf_fd >= 0)
close(dmabuf_fd); close(dmabuf_fd);
if (heap_fd >= 0) ksft_test_result(!ret, "dmabuf_heap_alloc_newer\n");
close(heap_fd);
return ret; close(heap_fd);
} }
static int test_alloc_errors(char *heap_name) static void test_alloc_errors(char *heap_name)
{ {
int heap_fd = -1, dmabuf_fd = -1; int heap_fd = -1, dmabuf_fd = -1;
int ret; int ret;
heap_fd = dmabuf_heap_open(heap_name); heap_fd = dmabuf_heap_open(heap_name);
if (heap_fd < 0)
return -1;
printf(" Testing expected error cases: "); ksft_print_msg("Testing expected error cases:\n");
ret = dmabuf_heap_alloc(0, ONE_MEG, 0x111111, &dmabuf_fd); ret = dmabuf_heap_alloc(0, ONE_MEG, 0x111111, &dmabuf_fd);
if (!ret) { ksft_test_result(ret, "Error expected on invalid fd %d\n", ret);
printf("FAIL (Did not see expected error (invalid fd)!)\n");
ret = -1;
goto out;
}
ret = dmabuf_heap_alloc(heap_fd, ONE_MEG, 0x111111, &dmabuf_fd); ret = dmabuf_heap_alloc(heap_fd, ONE_MEG, 0x111111, &dmabuf_fd);
if (!ret) { ksft_test_result(ret, "Error expected on invalid heap flags %d\n", ret);
printf("FAIL (Did not see expected error (invalid heap flags)!)\n");
ret = -1;
goto out;
}
ret = dmabuf_heap_alloc_fdflags(heap_fd, ONE_MEG, ret = dmabuf_heap_alloc_fdflags(heap_fd, ONE_MEG,
~(O_RDWR | O_CLOEXEC), 0, &dmabuf_fd); ~(O_RDWR | O_CLOEXEC), 0, &dmabuf_fd);
if (!ret) { ksft_test_result(ret, "Error expected on invalid heap flags %d\n", ret);
printf("FAIL (Did not see expected error (invalid fd flags)!)\n");
ret = -1;
goto out;
}
printf("OK\n");
ret = 0;
out:
if (dmabuf_fd >= 0) if (dmabuf_fd >= 0)
close(dmabuf_fd); close(dmabuf_fd);
if (heap_fd >= 0) close(heap_fd);
close(heap_fd); }
return ret; static int numer_of_heaps(void)
{
DIR *d = opendir(DEVPATH);
struct dirent *dir;
int heaps = 0;
while ((dir = readdir(d))) {
if (!strncmp(dir->d_name, ".", 2))
continue;
if (!strncmp(dir->d_name, "..", 3))
continue;
heaps++;
}
return heaps;
} }
int main(void) int main(void)
{ {
DIR *d;
struct dirent *dir; struct dirent *dir;
int ret = -1; DIR *d;
ksft_print_header();
d = opendir(DEVPATH); d = opendir(DEVPATH);
if (!d) { if (!d) {
printf("No %s directory?\n", DEVPATH); ksft_print_msg("No %s directory?\n", DEVPATH);
return -1; return KSFT_SKIP;
} }
while ((dir = readdir(d)) != NULL) { ksft_set_plan(11 * numer_of_heaps());
while ((dir = readdir(d))) {
if (!strncmp(dir->d_name, ".", 2)) if (!strncmp(dir->d_name, ".", 2))
continue; continue;
if (!strncmp(dir->d_name, "..", 3)) if (!strncmp(dir->d_name, "..", 3))
continue; continue;
printf("Testing heap: %s\n", dir->d_name); ksft_print_msg("Testing heap: %s\n", dir->d_name);
printf("=======================================\n"); ksft_print_msg("=======================================\n");
ret = test_alloc_and_import(dir->d_name); test_alloc_and_import(dir->d_name);
if (ret) test_alloc_zeroed(dir->d_name, 4 * 1024);
break; test_alloc_zeroed(dir->d_name, ONE_MEG);
test_alloc_compat(dir->d_name);
ret = test_alloc_zeroed(dir->d_name, 4 * 1024); test_alloc_errors(dir->d_name);
if (ret)
break;
ret = test_alloc_zeroed(dir->d_name, ONE_MEG);
if (ret)
break;
ret = test_alloc_compat(dir->d_name);
if (ret)
break;
ret = test_alloc_errors(dir->d_name);
if (ret)
break;
} }
closedir(d); closedir(d);
return ret; ksft_finished();
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment