START_TEST(test_vrend_host_backed_memory_no_data_leak)
{
   struct iovec iovs[1];
   int niovs = 1;

   struct virgl_context ctx = {0};

   int ret = testvirgl_init_ctx_cmdbuf(&ctx);

   struct virgl_renderer_resource_create_args res;
   res.handle = 0x400;
   res.target = PIPE_BUFFER;
   res.format = VIRGL_FORMAT_R8_UNORM;
   res.nr_samples = 0;
   res.last_level = 0;
   res.array_size = 1;
   res.bind = VIRGL_BIND_CUSTOM;
   res.depth = 1;
   res.width = 32;
   res.height = 1;
   res.flags = 0;

   uint32_t size = 32;
   uint8_t* data = calloc(1, size);
   memset(data, 1, 32);
   iovs[0].iov_base = data;
   iovs[0].iov_len = size;

   struct pipe_box box = {0,0,0, size, 1,1};

   virgl_renderer_resource_create(&res, NULL, 0);
   virgl_renderer_ctx_attach_resource(ctx.ctx_id, res.handle);

   ret = virgl_renderer_transfer_read_iov(res.handle, ctx.ctx_id, 0, 0, 0,
                                          (struct virgl_box *)&box, 0, iovs, niovs);

   ck_assert_int_eq(ret, 0);

   for (int i = 0; i < 32; ++i)
      ck_assert_int_eq(data[i], 0);

   virgl_renderer_ctx_detach_resource(1, res.handle);

   virgl_renderer_resource_unref(res.handle);
   free(data);

}
START_TEST(test_vrend_host_backed_memory_no_data_leak)
{
   struct iovec iovs[1];
   int niovs = 1;

   struct virgl_context ctx = {0};

   int ret = testvirgl_init_ctx_cmdbuf(&ctx);

   struct virgl_renderer_resource_create_args res;
   res.handle = 0x400;
   res.target = PIPE_BUFFER;
   res.format = VIRGL_FORMAT_R8_UNORM;
   res.nr_samples = 0;
   res.last_level = 0;
   res.array_size = 1;
   res.bind = VIRGL_BIND_CUSTOM;
   res.depth = 1;
   res.width = 32;
   res.height = 1;
   res.flags = 0;

   uint32_t size = 32;
   uint8_t* data = calloc(1, size);
   memset(data, 1, 32);
   iovs[0].iov_base = data;
   iovs[0].iov_len = size;

   struct pipe_box box = {0,0,0, size, 1,1};

   virgl_renderer_resource_create(&res, NULL, 0);
   virgl_renderer_ctx_attach_resource(ctx.ctx_id, res.handle);

   ret = virgl_renderer_transfer_read_iov(res.handle, ctx.ctx_id, 0, 0, 0,
                                          (struct virgl_box *)&box, 0, iovs, niovs);

   ck_assert_int_eq(ret, 0);

   for (int i = 0; i < 32; ++i)
      ck_assert_int_eq(data[i], 0);

   virgl_renderer_ctx_detach_resource(1, res.handle);

   virgl_renderer_resource_unref(res.handle);
   free(data);

}