diff options
Diffstat (limited to 'test/unit-tests.c')
-rw-r--r-- | test/unit-tests.c | 335 |
1 files changed, 0 insertions, 335 deletions
diff --git a/test/unit-tests.c b/test/unit-tests.c index 580eb29..36faaf9 100644 --- a/test/unit-tests.c +++ b/test/unit-tests.c @@ -399,319 +399,6 @@ typedef struct { } 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; - - /* from ERROR: all transitions are invalid */ - for (i = 0; i < 8; i++) { - assert_false(f(VFIO_USER_DEVICE_STATE_ERROR, i)); - } - - /* from STOP */ - assert_false(f(VFIO_USER_DEVICE_STATE_STOP, VFIO_USER_DEVICE_STATE_ERROR)); - assert_false(f(VFIO_USER_DEVICE_STATE_STOP, VFIO_USER_DEVICE_STATE_STOP)); - assert_true(f(VFIO_USER_DEVICE_STATE_STOP, VFIO_USER_DEVICE_STATE_RUNNING)); - assert_true(f(VFIO_USER_DEVICE_STATE_STOP, VFIO_USER_DEVICE_STATE_STOP_COPY)); - assert_true(f(VFIO_USER_DEVICE_STATE_STOP, VFIO_USER_DEVICE_STATE_RESUMING)); - assert_false(f(VFIO_USER_DEVICE_STATE_STOP, VFIO_USER_DEVICE_STATE_RUNNING_P2P)); - assert_false(f(VFIO_USER_DEVICE_STATE_STOP, VFIO_USER_DEVICE_STATE_PRE_COPY)); - assert_false(f(VFIO_USER_DEVICE_STATE_STOP, VFIO_USER_DEVICE_STATE_PRE_COPY_P2P)); - - /* from RUNNING */ - assert_false(f(VFIO_USER_DEVICE_STATE_RUNNING, VFIO_USER_DEVICE_STATE_ERROR)); - assert_true(f(VFIO_USER_DEVICE_STATE_RUNNING, VFIO_USER_DEVICE_STATE_STOP)); - assert_false(f(VFIO_USER_DEVICE_STATE_RUNNING, VFIO_USER_DEVICE_STATE_RUNNING)); - assert_false(f(VFIO_USER_DEVICE_STATE_RUNNING, VFIO_USER_DEVICE_STATE_STOP_COPY)); - assert_false(f(VFIO_USER_DEVICE_STATE_RUNNING, VFIO_USER_DEVICE_STATE_RESUMING)); - assert_false(f(VFIO_USER_DEVICE_STATE_RUNNING, VFIO_USER_DEVICE_STATE_RUNNING_P2P)); - assert_true(f(VFIO_USER_DEVICE_STATE_RUNNING, VFIO_USER_DEVICE_STATE_PRE_COPY)); - assert_false(f(VFIO_USER_DEVICE_STATE_RUNNING, VFIO_USER_DEVICE_STATE_PRE_COPY_P2P)); - - /* from STOP_COPY and RESUMING */ - for (i = 3; i < 5; i++) { - assert_false(f(i, VFIO_USER_DEVICE_STATE_ERROR)); - assert_true(f(i, VFIO_USER_DEVICE_STATE_STOP)); - assert_false(f(i, VFIO_USER_DEVICE_STATE_RUNNING)); - assert_false(f(i, VFIO_USER_DEVICE_STATE_STOP_COPY)); - assert_false(f(i, VFIO_USER_DEVICE_STATE_RESUMING)); - assert_false(f(i, VFIO_USER_DEVICE_STATE_RUNNING_P2P)); - assert_false(f(i, VFIO_USER_DEVICE_STATE_PRE_COPY)); - assert_false(f(i, VFIO_USER_DEVICE_STATE_PRE_COPY_P2P)); - } - - /* from RUNNING_P2P: all transitions are invalid */ - for (i = 0; i < 8; i++) { - assert_false(f(VFIO_USER_DEVICE_STATE_RUNNING_P2P, i)); - } - - /* from PRE_COPY */ - assert_false(f(VFIO_USER_DEVICE_STATE_PRE_COPY, VFIO_USER_DEVICE_STATE_ERROR)); - assert_false(f(VFIO_USER_DEVICE_STATE_PRE_COPY, VFIO_USER_DEVICE_STATE_STOP)); - assert_true(f(VFIO_USER_DEVICE_STATE_PRE_COPY, VFIO_USER_DEVICE_STATE_RUNNING)); - assert_true(f(VFIO_USER_DEVICE_STATE_PRE_COPY, VFIO_USER_DEVICE_STATE_STOP_COPY)); - assert_false(f(VFIO_USER_DEVICE_STATE_PRE_COPY, VFIO_USER_DEVICE_STATE_RESUMING)); - assert_false(f(VFIO_USER_DEVICE_STATE_PRE_COPY, VFIO_USER_DEVICE_STATE_RUNNING_P2P)); - assert_false(f(VFIO_USER_DEVICE_STATE_PRE_COPY, VFIO_USER_DEVICE_STATE_PRE_COPY)); - assert_false(f(VFIO_USER_DEVICE_STATE_PRE_COPY, VFIO_USER_DEVICE_STATE_PRE_COPY_P2P)); - - /* from PRE_COPY_P2P: all transitions are invalid */ - for (i = 0; i < 8; i++) { - assert_false(f(VFIO_USER_DEVICE_STATE_PRE_COPY_P2P, i)); - } -} - -static vfu_migr_state_t LAST_STATE = -1; -static void *LAST_WRITE = NULL; -static int transition_callback(vfu_ctx_t *ctx UNUSED, vfu_migr_state_t state) { - LAST_STATE = state; - return 0; -} -static ssize_t read_callback(vfu_ctx_t *ctx UNUSED, void *buf, uint64_t count) -{ - assert(count < 256); - uint8_t *data = buf; - for (uint8_t i = 0; i < count; i++) { - *(data + i) = i; - } - return count; -} -static ssize_t write_callback(vfu_ctx_t *ctx UNUSED, void *buf, uint64_t count) -{ - if (LAST_WRITE != NULL) { - free(LAST_WRITE); - } - - LAST_WRITE = malloc(count); - memcpy(LAST_WRITE, buf, count); - return count; -} - -static struct test_setup_migr_reg_dat { - vfu_ctx_t *v; - const vfu_migration_callbacks_t c; -} migr_reg_data = { - .c = { - .version = VFU_MIGR_CALLBACKS_VERS, - .transition = transition_callback, - .read_data = read_callback, - .write_data = write_callback - } -}; - -static int -setup_test_setup_migration(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; - } - - *state = p; - return setup(state); -} - -static int -teardown_test_setup_migration(void **state) { - struct test_setup_migr_reg_dat *p = *state; - vfu_destroy_ctx(p->v); - - if (LAST_WRITE != NULL) { - free(LAST_WRITE); - LAST_WRITE = NULL; - } - - return 0; -} - -static void -test_setup_migration_callbacks(void **state) -{ - struct test_setup_migr_reg_dat *p = *state; - int r = vfu_setup_device_migration_callbacks(p->v, &p->c); - assert_int_equal(0, r); - assert_non_null(p->v->migration); - assert_int_equal(p->v->migration->state, VFIO_USER_DEVICE_STATE_RUNNING); -} - -static void -test_handle_device_state(void **state) -{ - test_setup_migration_callbacks(state); - - struct test_setup_migr_reg_dat *p = *state; - struct migration *migr = p->v->migration; - - assert(migr->state == VFIO_USER_DEVICE_STATE_RUNNING); - - int r; - - r = handle_device_state(p->v, migr, VFIO_USER_DEVICE_STATE_PRE_COPY, true); - assert_int_equal(0, r); - assert_int_equal(LAST_STATE, VFU_MIGR_STATE_PRE_COPY); - - r = handle_device_state(p->v, migr, VFIO_USER_DEVICE_STATE_STOP_COPY, true); - assert_int_equal(0, r); - assert_int_equal(LAST_STATE, VFU_MIGR_STATE_STOP_AND_COPY); - - r = handle_device_state(p->v, migr, VFIO_USER_DEVICE_STATE_STOP, true); - assert_int_equal(0, r); - assert_int_equal(LAST_STATE, VFU_MIGR_STATE_STOP); - - r = handle_device_state(p->v, migr, VFIO_USER_DEVICE_STATE_RUNNING, true); - assert_int_equal(0, r); - assert_int_equal(LAST_STATE, VFU_MIGR_STATE_RUNNING); -} - -static void -test_handle_mig_data_read(void **state) -{ - test_setup_migration_callbacks(state); - - struct test_setup_migr_reg_dat *p = *state; - struct migration *migr = p->v->migration; - - struct vfio_user_mig_data data = { - .argsz = sizeof(data), - .size = 4 - }; - - vfu_msg_t *m = mkmsg(VFIO_USER_MIG_DATA_READ, &data, sizeof(data)); - - uint8_t expect[4] = {0, 1, 2, 3}; - - p->v->client_max_data_xfer_size = 4; - - ssize_t r; - - migr->state = VFIO_USER_DEVICE_STATE_PRE_COPY; - r = handle_mig_data_read(p->v, m); - assert_int_equal(4, r); - assert_int_equal(0, memcmp(msg.out.iov.iov_base + sizeof(data), &expect, 4)); - free(msg.out.iov.iov_base); - - migr->state = VFIO_USER_DEVICE_STATE_STOP_COPY; - r = handle_mig_data_read(p->v, m); - assert_int_equal(4, r); - assert_int_equal(0, memcmp(msg.out.iov.iov_base + sizeof(data), &expect, 4)); - free(msg.out.iov.iov_base); -} - -static void -test_handle_mig_data_read_too_long(void **state) { - test_setup_migration_callbacks(state); - - struct test_setup_migr_reg_dat *p = *state; - struct migration *migr = p->v->migration; - - struct vfio_user_mig_data data = { - .argsz = sizeof(data), - .size = 4 - }; - - vfu_msg_t *m = mkmsg(VFIO_USER_MIG_DATA_READ, &data, sizeof(data)); - - p->v->client_max_data_xfer_size = 2; - - ssize_t r; - - migr->state = VFIO_USER_DEVICE_STATE_PRE_COPY; - r = handle_mig_data_read(p->v, m); - assert_int_equal(-1, r); -} - -static void -test_handle_mig_data_read_invalid_state(void **state) { - test_setup_migration_callbacks(state); - - struct test_setup_migr_reg_dat *p = *state; - struct migration *migr = p->v->migration; - - struct vfio_user_mig_data data = { - .argsz = sizeof(data), - .size = 4 - }; - - vfu_msg_t *m = mkmsg(VFIO_USER_MIG_DATA_READ, &data, sizeof(data)); - - p->v->client_max_data_xfer_size = 4; - - ssize_t r; - - migr->state = VFIO_USER_DEVICE_STATE_RUNNING; - r = handle_mig_data_read(p->v, m); - assert_int_equal(-1, r); - - migr->state = VFIO_USER_DEVICE_STATE_STOP; - r = handle_mig_data_read(p->v, m); - assert_int_equal(-1, r); -} - -static void -test_handle_mig_data_write(void **state) -{ - test_setup_migration_callbacks(state); - - struct test_setup_migr_reg_dat *p = *state; - struct migration *migr = p->v->migration; - - uint8_t req[12] = {0}; - - struct vfio_user_mig_data *data = (void*)&req; - data->argsz = sizeof(req); - data->size = 4; - - uint8_t *buf = (void*)data + sizeof(*data); - for (int i = 0; i < 4; i++) { - *(buf + i) = i; - } - - vfu_msg_t *m = mkmsg(VFIO_USER_MIG_DATA_READ, &req, sizeof(req)); - - p->v->client_max_data_xfer_size = 4; - - ssize_t r; - - migr->state = VFIO_USER_DEVICE_STATE_RESUMING; - r = handle_mig_data_write(p->v, m); - assert_int_equal(4, r); - assert_int_equal(0, memcmp(LAST_WRITE, buf, 4)); -} - -static void -test_handle_mig_data_write_invalid_state(void **state) -{ - test_setup_migration_callbacks(state); - - struct test_setup_migr_reg_dat *p = *state; - struct migration *migr = p->v->migration; - - uint8_t req[12] = {0}; - - struct vfio_user_mig_data *data = (void*)&req; - data->argsz = sizeof(req); - data->size = 4; - - uint8_t *buf = (void*)data + sizeof(*data); - for (int i = 0; i < 4; i++) { - *(buf + i) = i; - } - - vfu_msg_t *m = mkmsg(VFIO_USER_MIG_DATA_READ, &req, sizeof(req)); - - p->v->client_max_data_xfer_size = 4; - - ssize_t r; - - migr->state = VFIO_USER_DEVICE_STATE_RUNNING; - r = handle_mig_data_write(p->v, m); - assert_int_equal(-1, r); -} - -static void test_device_is_stopped_and_copying(UNUSED void **state) { assert_false(device_is_stopped_and_copying(vfu_ctx.migration)); @@ -811,28 +498,6 @@ main(void) 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_callbacks, - setup_test_setup_migration, - teardown_test_setup_migration), - cmocka_unit_test_setup_teardown(test_handle_device_state, - setup_test_setup_migration, - teardown_test_setup_migration), - cmocka_unit_test_setup_teardown(test_handle_mig_data_read, - setup_test_setup_migration, - teardown_test_setup_migration), - cmocka_unit_test_setup_teardown(test_handle_mig_data_read_too_long, - setup_test_setup_migration, - teardown_test_setup_migration), - cmocka_unit_test_setup_teardown(test_handle_mig_data_read_invalid_state, - setup_test_setup_migration, - teardown_test_setup_migration), - cmocka_unit_test_setup_teardown(test_handle_mig_data_write, - setup_test_setup_migration, - teardown_test_setup_migration), - cmocka_unit_test_setup_teardown(test_handle_mig_data_write_invalid_state, - setup_test_setup_migration, - teardown_test_setup_migration), 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), |