diff options
author | John Levon <john.levon@nutanix.com> | 2021-03-25 16:54:53 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-25 16:54:53 +0000 |
commit | 710a0081aa206b319161fac9bbafce14f5045ee8 (patch) | |
tree | fbf91231220be75cd05279c0492f90cf1d5ab669 /lib/dma.c | |
parent | 4f4c378978f0e22ec5b803496e2971adca6a3f8f (diff) | |
download | libvfio-user-710a0081aa206b319161fac9bbafce14f5045ee8.zip libvfio-user-710a0081aa206b319161fac9bbafce14f5045ee8.tar.gz libvfio-user-710a0081aa206b319161fac9bbafce14f5045ee8.tar.bz2 |
re-work unit test mocking (#400)
Instead of trying to use the linker's --wrap, which just led to more problems
when we want to call the real function, we'll add two defines, MOCK_DEFINE() and
MOCK_DECLARE(), that behave differently when building the unit tests, such that
all wrapped functions are picked up from test/mocks.c instead, regardless of
compilation unit.
Signed-off-by: John Levon <john.levon@nutanix.com>
Reviewed-by: Thanos Makatos <thanos.makatos@nutanix.com>
Diffstat (limited to 'lib/dma.c')
-rw-r--r-- | lib/dma.c | 30 |
1 files changed, 11 insertions, 19 deletions
@@ -92,8 +92,8 @@ dma_controller_create(vfu_ctx_t *vfu_ctx, int max_regions) } void -_dma_controller_do_remove_region(dma_controller_t *dma, - dma_memory_region_t *region) +MOCK_DEFINE(_dma_controller_do_remove_region)(dma_controller_t *dma, + dma_memory_region_t *region) { int err; @@ -113,13 +113,6 @@ _dma_controller_do_remove_region(dma_controller_t *dma, } } } -UNIT_TEST_SYMBOL(_dma_controller_do_remove_region); -/* - * FIXME super ugly, but without this functions within the same compilation - * unit don't call the wrapped version, making unit testing impossible. - * Ideally we'd like the UNIT_TEST_SYMBOL macro to solve this. - */ -#define _dma_controller_do_remove_region __wrap__dma_controller_do_remove_region /* * FIXME no longer used. Also, it doesn't work for addresses that span two @@ -144,9 +137,10 @@ dma_controller_region_valid(dma_controller_t *dma, dma_addr_t dma_addr, /* FIXME not thread safe */ int -dma_controller_remove_region(dma_controller_t *dma, - dma_addr_t dma_addr, size_t size, - vfu_unmap_dma_cb_t *unmap_dma, void *data) +MOCK_DEFINE(dma_controller_remove_region)(dma_controller_t *dma, + dma_addr_t dma_addr, size_t size, + vfu_unmap_dma_cb_t *unmap_dma, + void *data) { int idx; dma_memory_region_t *region; @@ -183,7 +177,6 @@ dma_controller_remove_region(dma_controller_t *dma, } return -ENOENT; } -UNIT_TEST_SYMBOL(dma_controller_remove_region); static inline void dma_controller_remove_regions(dma_controller_t *dma) @@ -214,9 +207,9 @@ dma_controller_destroy(dma_controller_t *dma) } int -dma_controller_add_region(dma_controller_t *dma, - dma_addr_t dma_addr, size_t size, - int fd, off_t offset, uint32_t prot) +MOCK_DEFINE(dma_controller_add_region)(dma_controller_t *dma, + dma_addr_t dma_addr, size_t size, + int fd, off_t offset, uint32_t prot) { int idx; dma_memory_region_t *region; @@ -320,7 +313,6 @@ dma_controller_add_region(dma_controller_t *dma, err: return -idx - 1; } -UNIT_TEST_SYMBOL(dma_controller_add_region); static inline void mmap_round(size_t *offset, size_t *size, size_t page_size) @@ -331,7 +323,8 @@ mmap_round(size_t *offset, size_t *size, size_t page_size) } void * -dma_map_region(dma_memory_region_t *region, int prot, size_t offset, size_t len) +MOCK_DEFINE(dma_map_region)(dma_memory_region_t *region, int prot, + size_t offset, size_t len) { size_t mmap_offset, mmap_size = len; char *mmap_base; @@ -357,7 +350,6 @@ dma_map_region(dma_memory_region_t *region, int prot, size_t offset, size_t len) return mmap_base + (offset - mmap_offset); } -UNIT_TEST_SYMBOL(dma_map_region); int dma_unmap_region(dma_memory_region_t *region, void *virt_addr, size_t len) |