diff options
Diffstat (limited to 'test/unit-tests.c')
-rw-r--r-- | test/unit-tests.c | 285 |
1 files changed, 3 insertions, 282 deletions
diff --git a/test/unit-tests.c b/test/unit-tests.c index 0fd4fe7..bf99c60 100644 --- a/test/unit-tests.c +++ b/test/unit-tests.c @@ -398,269 +398,9 @@ typedef struct { int conn_fd; } tran_sock_t; -static void -test_migration_state_transitions(void **state UNUSED) -{ - bool (*f)(uint32_t, uint32_t) = vfio_migr_state_transition_is_valid; - uint32_t i, j; - - /* from stopped (000b): all transitions are invalid except to running */ - assert_true(f(0, 0)); - assert_true(f(0, 1)); - for (i = 2; i < 8; i++) { - assert_false(f(0, i)); - } - - /* from running (001b) */ - assert_true(f(1, 0)); - assert_true(f(1, 1)); - assert_true(f(1, 2)); - assert_true(f(1, 3)); - assert_true(f(1, 4)); - assert_false(f(1, 5)); - assert_true(f(1, 6)); - assert_false(f(1, 5)); - - /* from stop-and-copy (010b) */ - assert_true(f(2, 0)); - assert_true(f(2, 1)); - assert_true(f(2, 2)); - assert_false(f(2, 3)); - assert_false(f(2, 4)); - assert_false(f(2, 5)); - assert_true(f(2, 6)); - assert_false(f(2, 7)); - - /* from pre-copy (011b) */ - assert_true(f(3, 0)); - assert_true(f(3, 1)); - assert_true(f(3, 2)); - assert_false(f(3, 3)); - assert_false(f(3, 4)); - assert_false(f(3, 5)); - assert_true(f(3, 6)); - assert_false(f(3, 7)); - - /* from resuming (100b) */ - assert_false(f(4, 0)); - assert_true(f(4, 1)); - assert_false(f(4, 2)); - assert_false(f(4, 3)); - assert_true(f(4, 4)); - assert_false(f(4, 5)); - assert_true(f(4, 6)); - assert_false(f(4, 7)); - - /* - * Transitioning to any other state from the remaining 3 states - * (101b - invalid, 110b - error, 111b - invalid) is invalid. - * Transitioning from the error state to the stopped state is possible but - * that requires a device reset, so we don't consider it a valid state - * transition. - */ - for (i = 5; i < 8; i++) { - for (j = 0; j < 8; j++) { - assert_false(f(i, j)); - } - } -} - -static struct test_setup_migr_reg_dat { - vfu_ctx_t *v; - size_t rs; /* migration registers size */ - size_t ds; /* migration data size */ - size_t s; /* migration region size*/ - const vfu_migration_callbacks_t c; -} migr_reg_data = { - .c = { - .version = VFU_MIGR_CALLBACKS_VERS, - .transition = (void *)0x1, - .get_pending_bytes = (void *)0x2, - .prepare_data = (void *)0x3, - .read_data = (void *)0x4, - .write_data = (void *)0x5, - .data_written = (void *)0x6 - } -}; - -static int -setup_test_setup_migration_region(void **state) -{ - struct test_setup_migr_reg_dat *p = &migr_reg_data; - p->v = vfu_create_ctx(VFU_TRANS_SOCK, "test", 0, NULL, - VFU_DEV_TYPE_PCI); - if (p->v == NULL) { - return -1; - } - p->rs = ROUND_UP(sizeof(struct vfio_user_migration_info), - sysconf(_SC_PAGE_SIZE)); - p->ds = sysconf(_SC_PAGE_SIZE); - p->s = p->rs + p->ds; - *state = p; - return setup(state); -} - -static vfu_ctx_t * -get_vfu_ctx(void **state) -{ - return (*((struct test_setup_migr_reg_dat **)(state)))->v; -} - -static int -teardown_test_setup_migration_region(void **state) -{ - struct test_setup_migr_reg_dat *p = *state; - vfu_destroy_ctx(p->v); - return 0; -} +// TODO: migration state transition tests -static void -test_setup_migration_region_size_ok(void **state) -{ - vfu_ctx_t *v = get_vfu_ctx(state); - int r = vfu_setup_region(v, VFU_PCI_DEV_MIGR_REGION_IDX, - vfu_get_migr_register_area_size(), NULL, - VFU_REGION_FLAG_READ | VFU_REGION_FLAG_WRITE, NULL, 0, -1, 0); - assert_int_equal(0, r); -} - -static void -test_setup_migration_region_sparsely_mappable_valid(void **state) -{ - struct test_setup_migr_reg_dat *p = *state; - struct iovec mmap_areas[] = { - [0] = { - .iov_base = (void *)p->rs, - .iov_len = p->ds - } - }; - int r = vfu_setup_region(p->v, VFU_PCI_DEV_MIGR_REGION_IDX, p->s, NULL, - VFU_REGION_FLAG_READ | VFU_REGION_FLAG_WRITE, mmap_areas, 1, - 0xdeadbeef, 0); - assert_int_equal(0, r); -} - -static void -test_setup_migration_callbacks_without_migration_region(void **state) -{ - struct test_setup_migr_reg_dat *p = *state; - assert_int_equal(-1, vfu_setup_device_migration_callbacks(p->v, &p->c, 0)); - assert_int_equal(EINVAL, errno); -} - -static void -test_setup_migration_callbacks_bad_data_offset(void **state) -{ - struct test_setup_migr_reg_dat *p = *state; - int r = vfu_setup_region(p->v, VFU_PCI_DEV_MIGR_REGION_IDX, p->s, NULL, - VFU_REGION_FLAG_READ | VFU_REGION_FLAG_WRITE, NULL, 0, -1, 0); - assert_int_equal(0, r); - r = vfu_setup_device_migration_callbacks(p->v, &p->c, - vfu_get_migr_register_area_size() - 1); - assert_int_equal(-1, r); -} - -static void -test_setup_migration_callbacks(void **state) -{ - struct test_setup_migr_reg_dat *p = *state; - int r = vfu_setup_region(p->v, VFU_PCI_DEV_MIGR_REGION_IDX, p->s, NULL, - VFU_REGION_FLAG_READ | VFU_REGION_FLAG_WRITE, NULL, 0, -1, 0); - assert_int_equal(0, r); - r = vfu_setup_device_migration_callbacks(p->v, &p->c, - vfu_get_migr_register_area_size()); - assert_int_equal(0, r); - assert_non_null(p->v->migration); - /* FIXME can't validate p->v->migration because it's a private strcut, need to move it out of lib/migration.c */ -} - -static void -test_device_is_stopped_and_copying(UNUSED void **state) -{ - assert_false(device_is_stopped_and_copying(vfu_ctx.migration)); - assert_false(device_is_stopped(vfu_ctx.migration)); - - size_t i; - struct migration migration; - vfu_ctx.migration = &migration; - for (i = 0; i < ARRAY_SIZE(migr_states); i++) { - if (migr_states[i].name == NULL) { - continue; - } - migration.info.device_state = i; - bool r = device_is_stopped_and_copying(vfu_ctx.migration); - if (i == VFIO_DEVICE_STATE_V1_SAVING) { - assert_true(r); - } else { - assert_false(r); - } - r = device_is_stopped(vfu_ctx.migration); - if (i == VFIO_DEVICE_STATE_V1_STOP) { - assert_true(r); - } else { - assert_false(r); - } - } - vfu_ctx.migration = NULL; -} - -static void -test_cmd_allowed_when_stopped_and_copying(UNUSED void **state) -{ - size_t i; - - for (i = 0; i < VFIO_USER_MAX; i++) { - bool r = cmd_allowed_when_stopped_and_copying(i); - if (i == VFIO_USER_REGION_READ || i == VFIO_USER_REGION_WRITE || - i == VFIO_USER_DIRTY_PAGES) { - assert_true(r); - } else { - assert_false(r); - } - } -} - -static void -test_should_exec_command(UNUSED void **state) -{ - struct migration migration = { { 0 } }; - - vfu_ctx.migration = &migration; - - patch("device_is_stopped_and_copying"); - patch("cmd_allowed_when_stopped_and_copying"); - patch("device_is_stopped"); - - /* TEST stopped and copying, command allowed */ - will_return(device_is_stopped_and_copying, true); - expect_value(device_is_stopped_and_copying, migration, &migration); - will_return(cmd_allowed_when_stopped_and_copying, true); - expect_value(cmd_allowed_when_stopped_and_copying, cmd, 0xbeef); - assert_true(should_exec_command(&vfu_ctx, 0xbeef)); - - /* TEST stopped and copying, command not allowed */ - will_return(device_is_stopped_and_copying, true); - expect_any(device_is_stopped_and_copying, migration); - will_return(cmd_allowed_when_stopped_and_copying, false); - expect_any(cmd_allowed_when_stopped_and_copying, cmd); - assert_false(should_exec_command(&vfu_ctx, 0xbeef)); - - /* TEST stopped */ - will_return(device_is_stopped_and_copying, false); - expect_any(device_is_stopped_and_copying, migration); - will_return(device_is_stopped, true); - expect_value(device_is_stopped, migration, &migration); - will_return(cmd_allowed_when_stopped_and_copying, false); - expect_value(cmd_allowed_when_stopped_and_copying, cmd, 0xbeef); - assert_false(should_exec_command(&vfu_ctx, 0xbeef)); - - /* TEST none of the above */ - will_return(device_is_stopped_and_copying, false); - expect_any(device_is_stopped_and_copying, migration); - will_return(device_is_stopped, false); - expect_any(device_is_stopped, migration); - assert_true(should_exec_command(&vfu_ctx, 0xbeef)); -} +// TODO: test migration functions int main(void) @@ -674,26 +414,7 @@ main(void) cmocka_unit_test_setup(test_dma_controller_remove_region_mapped, setup), cmocka_unit_test_setup(test_dma_controller_remove_region_unmapped, setup), cmocka_unit_test_setup(test_dma_addr_to_sgl, setup), - cmocka_unit_test_setup(test_vfu_setup_device_dma, setup), - cmocka_unit_test_setup(test_migration_state_transitions, setup), - cmocka_unit_test_setup_teardown(test_setup_migration_region_size_ok, - setup_test_setup_migration_region, - teardown_test_setup_migration_region), - cmocka_unit_test_setup_teardown(test_setup_migration_region_sparsely_mappable_valid, - setup_test_setup_migration_region, - teardown_test_setup_migration_region), - cmocka_unit_test_setup_teardown(test_setup_migration_callbacks_without_migration_region, - setup_test_setup_migration_region, - teardown_test_setup_migration_region), - cmocka_unit_test_setup_teardown(test_setup_migration_callbacks_bad_data_offset, - setup_test_setup_migration_region, - teardown_test_setup_migration_region), - cmocka_unit_test_setup_teardown(test_setup_migration_callbacks, - setup_test_setup_migration_region, - teardown_test_setup_migration_region), - cmocka_unit_test_setup(test_device_is_stopped_and_copying, setup), - cmocka_unit_test_setup(test_cmd_allowed_when_stopped_and_copying, setup), - cmocka_unit_test_setup(test_should_exec_command, setup), + cmocka_unit_test_setup(test_vfu_setup_device_dma, setup) }; return cmocka_run_group_tests(tests, NULL, NULL); |