| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <sys/eventfd.h> |
| 4 |
#include <sys/socket.h> |
| 5 |
|
| 6 |
#include "sd-event.h" |
| 7 |
#include "sd-future.h" |
| 8 |
#include "sd-json.h" |
| 9 |
|
| 10 |
#include "errno-util.h" |
| 11 |
#include "fd-util.h" |
| 12 |
#include "json-stream.h" |
| 13 |
#include "qmp-client.h" |
| 14 |
#include "string-util.h" |
| 15 |
#include "tests.h" |
| 16 |
|
| 17 |
/* Mock QMP server runs as an sd-fiber alongside the client on the same event loop. Its |
| 18 |
* JsonStream uses the suspending json_stream_wait()/json_stream_flush() helpers, so the mock |
| 19 |
* fiber yields whenever it's blocked on I/O and the client makes progress in the meantime. */ |
| 20 |
|
| 21 |
static JsonStreamPhase mock_qmp_phase(void *userdata) { |
| 22 |
return JSON_STREAM_PHASE_READING; |
| 23 |
} |
| 24 |
|
| 25 |
static int mock_qmp_dispatch(void *userdata) { |
| 26 |
return 0; |
| 27 |
} |
| 28 |
|
| 29 |
static void mock_qmp_init(JsonStream *s, int fd) { |
| 30 |
static const JsonStreamParams params = { |
| 31 |
.delimiter = "\r\n", |
| 32 |
.phase = mock_qmp_phase, |
| 33 |
.dispatch = mock_qmp_dispatch, |
| 34 |
}; |
| 35 |
|
| 36 |
ASSERT_OK(json_stream_init(s, ¶ms)); |
| 37 |
ASSERT_OK(json_stream_connect_fd_pair(s, fd, fd)); |
| 38 |
} |
| 39 |
|
| 40 |
static void mock_qmp_recv(JsonStream *s, sd_json_variant **ret) { |
| 41 |
int r; |
| 42 |
|
| 43 |
for (;;) { |
| 44 |
r = ASSERT_OK(json_stream_parse(s, ret)); |
| 45 |
if (r > 0) |
| 46 |
return; |
| 47 |
|
| 48 |
r = ASSERT_OK(json_stream_read(s)); |
| 49 |
if (r > 0) |
| 50 |
continue; |
| 51 |
|
| 52 |
ASSERT_OK(json_stream_wait(s, USEC_INFINITY)); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
static void mock_qmp_send(JsonStream *s, sd_json_variant *v) { |
| 57 |
ASSERT_OK(json_stream_enqueue(s, v)); |
| 58 |
ASSERT_OK(json_stream_flush(s)); |
| 59 |
} |
| 60 |
|
| 61 |
static void mock_qmp_send_greeting(JsonStream *s) { |
| 62 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 63 |
|
| 64 |
ASSERT_OK(sd_json_buildo(&v, |
| 65 |
SD_JSON_BUILD_PAIR("QMP", SD_JSON_BUILD_OBJECT( |
| 66 |
SD_JSON_BUILD_PAIR("version", SD_JSON_BUILD_OBJECT( |
| 67 |
SD_JSON_BUILD_PAIR("qemu", SD_JSON_BUILD_OBJECT( |
| 68 |
SD_JSON_BUILD_PAIR_UNSIGNED("micro", 0), |
| 69 |
SD_JSON_BUILD_PAIR_UNSIGNED("minor", 2), |
| 70 |
SD_JSON_BUILD_PAIR_UNSIGNED("major", 9))))), |
| 71 |
SD_JSON_BUILD_PAIR("capabilities", SD_JSON_BUILD_STRV(STRV_MAKE("oob"))))))); |
| 72 |
mock_qmp_send(s, v); |
| 73 |
} |
| 74 |
|
| 75 |
/* Receive one command, assert it matches `expected_command`, return its id (borrowed from *cmd). */ |
| 76 |
static sd_json_variant* mock_qmp_expect(JsonStream *s, const char *expected_command, sd_json_variant **cmd) { |
| 77 |
mock_qmp_recv(s, cmd); |
| 78 |
sd_json_variant *execute = ASSERT_NOT_NULL(sd_json_variant_by_key(*cmd, "execute")); |
| 79 |
ASSERT_STREQ(sd_json_variant_string(execute), expected_command); |
| 80 |
return ASSERT_NOT_NULL(sd_json_variant_by_key(*cmd, "id")); |
| 81 |
} |
| 82 |
|
| 83 |
/* Send a reply for a previously-received command id. Passing NULL reply_data sends {}. */ |
| 84 |
static void mock_qmp_reply(JsonStream *s, sd_json_variant *id, sd_json_variant *reply_data) { |
| 85 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *empty = NULL, *response = NULL; |
| 86 |
|
| 87 |
if (!reply_data) { |
| 88 |
ASSERT_OK(sd_json_build(&empty, SD_JSON_BUILD_EMPTY_OBJECT)); |
| 89 |
reply_data = empty; |
| 90 |
} |
| 91 |
|
| 92 |
ASSERT_OK(sd_json_buildo(&response, |
| 93 |
SD_JSON_BUILD_PAIR("return", SD_JSON_BUILD_VARIANT(reply_data)), |
| 94 |
SD_JSON_BUILD_PAIR("id", SD_JSON_BUILD_VARIANT(id)))); |
| 95 |
|
| 96 |
mock_qmp_send(s, response); |
| 97 |
} |
| 98 |
|
| 99 |
static void mock_qmp_expect_and_reply(JsonStream *s, const char *expected_command, sd_json_variant *reply_data) { |
| 100 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *cmd = NULL; |
| 101 |
mock_qmp_reply(s, mock_qmp_expect(s, expected_command, &cmd), reply_data); |
| 102 |
} |
| 103 |
|
| 104 |
static void mock_qmp_expect_and_reply_error(JsonStream *s, const char *expected_command, const char *error_desc) { |
| 105 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *cmd = NULL, *response = NULL; |
| 106 |
sd_json_variant *id = mock_qmp_expect(s, expected_command, &cmd); |
| 107 |
|
| 108 |
ASSERT_OK(sd_json_buildo(&response, |
| 109 |
SD_JSON_BUILD_PAIR("error", SD_JSON_BUILD_OBJECT( |
| 110 |
SD_JSON_BUILD_PAIR_STRING("class", "GenericError"), |
| 111 |
SD_JSON_BUILD_PAIR_STRING("desc", error_desc))), |
| 112 |
SD_JSON_BUILD_PAIR("id", SD_JSON_BUILD_VARIANT(id)))); |
| 113 |
|
| 114 |
mock_qmp_send(s, response); |
| 115 |
} |
| 116 |
|
| 117 |
static void mock_qmp_handshake(JsonStream *s) { |
| 118 |
mock_qmp_send_greeting(s); |
| 119 |
mock_qmp_expect_and_reply(s, "qmp_capabilities", NULL); |
| 120 |
} |
| 121 |
|
| 122 |
/* Reply to query-status with a running=true/status="running" payload. */ |
| 123 |
static void mock_qmp_query_status_running(JsonStream *s) { |
| 124 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 125 |
|
| 126 |
ASSERT_OK(sd_json_buildo(&v, |
| 127 |
SD_JSON_BUILD_PAIR_BOOLEAN("running", true), |
| 128 |
SD_JSON_BUILD_PAIR_STRING("status", "running"))); |
| 129 |
mock_qmp_expect_and_reply(s, "query-status", v); |
| 130 |
} |
| 131 |
|
| 132 |
/* Drive a mock+client pair on a single event loop. The client fiber runs as userdata=client, |
| 133 |
* the mock fiber as userdata=fd (the server-side socket). */ |
| 134 |
static void run_qmp_test(sd_fiber_func_t mock_fn, sd_fiber_func_t client_fn) { |
| 135 |
_cleanup_(sd_event_unrefp) sd_event *event = NULL; |
| 136 |
_cleanup_(sd_future_unrefp) sd_future *client_f = NULL, *mock_f = NULL; |
| 137 |
_cleanup_(qmp_client_unrefp) QmpClient *client = NULL; |
| 138 |
_cleanup_close_pair_ int qmp_fds[2] = EBADF_PAIR; |
| 139 |
|
| 140 |
ASSERT_OK(sd_event_new(&event)); |
| 141 |
ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, qmp_fds)); |
| 142 |
|
| 143 |
ASSERT_OK(qmp_client_connect_fd(&client, TAKE_FD(qmp_fds[0]))); |
| 144 |
ASSERT_OK(qmp_client_attach_event(client, event, SD_EVENT_PRIORITY_NORMAL)); |
| 145 |
|
| 146 |
ASSERT_OK(sd_fiber_new(event, "mock", mock_fn, FD_TO_PTR(TAKE_FD(qmp_fds[1])), NULL, &mock_f)); |
| 147 |
ASSERT_OK(sd_fiber_new(event, "client", client_fn, client, NULL, &client_f)); |
| 148 |
|
| 149 |
ASSERT_OK(sd_event_loop(event)); |
| 150 |
ASSERT_OK(sd_future_result(client_f)); |
| 151 |
ASSERT_OK(sd_future_result(mock_f)); |
| 152 |
} |
| 153 |
|
| 154 |
static int mock_qmp_basic_fiber(void *userdata) { |
| 155 |
_cleanup_(json_stream_done) JsonStream s = {}; |
| 156 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *stop_event = NULL; |
| 157 |
|
| 158 |
mock_qmp_init(&s, PTR_TO_FD(userdata)); |
| 159 |
mock_qmp_handshake(&s); |
| 160 |
|
| 161 |
mock_qmp_query_status_running(&s); |
| 162 |
mock_qmp_expect_and_reply(&s, "stop", NULL); |
| 163 |
|
| 164 |
ASSERT_OK(sd_json_buildo(&stop_event, |
| 165 |
SD_JSON_BUILD_PAIR_STRING("event", "STOP"), |
| 166 |
SD_JSON_BUILD_PAIR("timestamp", SD_JSON_BUILD_OBJECT( |
| 167 |
SD_JSON_BUILD_PAIR_UNSIGNED("seconds", 1234), |
| 168 |
SD_JSON_BUILD_PAIR_UNSIGNED("microseconds", 5678))))); |
| 169 |
mock_qmp_send(&s, stop_event); |
| 170 |
|
| 171 |
mock_qmp_expect_and_reply(&s, "cont", NULL); |
| 172 |
return 0; |
| 173 |
} |
| 174 |
|
| 175 |
static int test_event_callback( |
| 176 |
QmpClient *client, |
| 177 |
const char *event, |
| 178 |
sd_json_variant *data, |
| 179 |
void *userdata) { |
| 180 |
|
| 181 |
bool *event_received = ASSERT_PTR(userdata); |
| 182 |
|
| 183 |
/* Ignore the synthetic SHUTDOWN emitted when the mock closes the connection. */ |
| 184 |
if (streq(event, "STOP")) |
| 185 |
*event_received = true; |
| 186 |
|
| 187 |
return 0; |
| 188 |
} |
| 189 |
|
| 190 |
static int qmp_client_basic_fiber(void *userdata) { |
| 191 |
QmpClient *client = ASSERT_NOT_NULL(userdata); |
| 192 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *result = NULL; |
| 193 |
_cleanup_free_ char *error_desc = NULL; |
| 194 |
bool event_received = false; |
| 195 |
|
| 196 |
qmp_client_bind_event(client, test_event_callback, &event_received); |
| 197 |
|
| 198 |
ASSERT_OK_POSITIVE(qmp_client_call(client, "query-status", NULL, &result, &error_desc)); |
| 199 |
ASSERT_NULL(error_desc); |
| 200 |
|
| 201 |
sd_json_variant *running = ASSERT_NOT_NULL(sd_json_variant_by_key(result, "running")); |
| 202 |
ASSERT_TRUE(sd_json_variant_boolean(running)); |
| 203 |
sd_json_variant *status = ASSERT_NOT_NULL(sd_json_variant_by_key(result, "status")); |
| 204 |
ASSERT_STREQ(sd_json_variant_string(status), "running"); |
| 205 |
|
| 206 |
ASSERT_OK_POSITIVE(qmp_client_call(client, "stop", NULL, NULL, NULL)); |
| 207 |
ASSERT_OK_POSITIVE(qmp_client_call(client, "cont", NULL, NULL, NULL)); |
| 208 |
|
| 209 |
ASSERT_TRUE(event_received); |
| 210 |
ASSERT_OK(sd_event_exit(sd_fiber_get_event(), 0)); |
| 211 |
return 0; |
| 212 |
} |
| 213 |
|
| 214 |
TEST(qmp_basic) { |
| 215 |
run_qmp_test(mock_qmp_basic_fiber, qmp_client_basic_fiber); |
| 216 |
} |
| 217 |
|
| 218 |
static int mock_qmp_eof_fiber(void *userdata) { |
| 219 |
_cleanup_(json_stream_done) JsonStream s = {}; |
| 220 |
|
| 221 |
mock_qmp_init(&s, PTR_TO_FD(userdata)); |
| 222 |
mock_qmp_handshake(&s); |
| 223 |
/* Return; _cleanup_ closes the fd → client sees EOF. */ |
| 224 |
return 0; |
| 225 |
} |
| 226 |
|
| 227 |
static int qmp_client_eof_fiber(void *userdata) { |
| 228 |
QmpClient *client = ASSERT_NOT_NULL(userdata); |
| 229 |
int r = qmp_client_call(client, "query-status", NULL, NULL, NULL); |
| 230 |
ASSERT_TRUE(ERRNO_IS_NEG_DISCONNECT(r)); |
| 231 |
ASSERT_OK(sd_event_exit(sd_fiber_get_event(), 0)); |
| 232 |
return 0; |
| 233 |
} |
| 234 |
|
| 235 |
TEST(qmp_eof) { |
| 236 |
run_qmp_test(mock_qmp_eof_fiber, qmp_client_eof_fiber); |
| 237 |
} |
| 238 |
|
| 239 |
static int mock_qmp_call_fiber(void *userdata) { |
| 240 |
_cleanup_(json_stream_done) JsonStream s = {}; |
| 241 |
|
| 242 |
mock_qmp_init(&s, PTR_TO_FD(userdata)); |
| 243 |
mock_qmp_handshake(&s); |
| 244 |
|
| 245 |
mock_qmp_query_status_running(&s); |
| 246 |
mock_qmp_expect_and_reply_error(&s, "stop", "not running"); |
| 247 |
mock_qmp_expect_and_reply_error(&s, "stop", "still not running"); |
| 248 |
return 0; |
| 249 |
} |
| 250 |
|
| 251 |
static int qmp_client_call_fiber(void *userdata) { |
| 252 |
QmpClient *client = ASSERT_NOT_NULL(userdata); |
| 253 |
_cleanup_(sd_future_cancel_wait_unrefp) sd_future *f = NULL; |
| 254 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *result = NULL; |
| 255 |
_cleanup_free_ char *error_desc = NULL; |
| 256 |
|
| 257 |
/* Exercise qmp_client_call_future() + sd_fiber_await() + future_get_qmp_reply() |
| 258 |
* directly — success path. */ |
| 259 |
ASSERT_OK(qmp_client_call_future(client, "query-status", NULL, &f)); |
| 260 |
ASSERT_OK(sd_fiber_await(f)); |
| 261 |
ASSERT_OK(sd_future_result(f)); |
| 262 |
ASSERT_OK(future_get_qmp_reply(f, &result, &error_desc)); |
| 263 |
|
| 264 |
ASSERT_NULL(error_desc); |
| 265 |
sd_json_variant *running = ASSERT_NOT_NULL(sd_json_variant_by_key(result, "running")); |
| 266 |
ASSERT_TRUE(sd_json_variant_boolean(running)); |
| 267 |
sd_json_variant *status = ASSERT_NOT_NULL(sd_json_variant_by_key(result, "status")); |
| 268 |
ASSERT_STREQ(sd_json_variant_string(status), "running"); |
| 269 |
|
| 270 |
/* QMP-level error: future resolves with -EIO, sd_fiber_await() returns -EIO, and |
| 271 |
* future_get_qmp_reply() also returns -EIO (mirroring future_get_bus_reply()) — with the |
| 272 |
* detailed description captured via error_desc on top, and result left NULL. */ |
| 273 |
f = sd_future_unref(f); |
| 274 |
result = sd_json_variant_unref(result); |
| 275 |
error_desc = mfree(error_desc); |
| 276 |
|
| 277 |
ASSERT_OK(qmp_client_call_future(client, "stop", NULL, &f)); |
| 278 |
ASSERT_ERROR(sd_fiber_await(f), EIO); |
| 279 |
ASSERT_ERROR(sd_future_result(f), EIO); |
| 280 |
ASSERT_ERROR(future_get_qmp_reply(f, &result, &error_desc), EIO); |
| 281 |
|
| 282 |
ASSERT_NULL(result); |
| 283 |
ASSERT_STREQ(error_desc, "not running"); |
| 284 |
|
| 285 |
/* qmp_client_call() also surfaces QMP errors as -EIO, regardless of whether the caller |
| 286 |
* passed ret_error_desc. */ |
| 287 |
ASSERT_ERROR(qmp_client_call(client, "stop", NULL, NULL, NULL), EIO); |
| 288 |
ASSERT_OK(sd_event_exit(sd_fiber_get_event(), 0)); |
| 289 |
return 0; |
| 290 |
} |
| 291 |
|
| 292 |
TEST(qmp_call) { |
| 293 |
run_qmp_test(mock_qmp_call_fiber, qmp_client_call_fiber); |
| 294 |
} |
| 295 |
|
| 296 |
static int mock_qmp_call_disconnect_fiber(void *userdata) { |
| 297 |
_cleanup_(json_stream_done) JsonStream s = {}; |
| 298 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *stop_cmd = NULL; |
| 299 |
|
| 300 |
mock_qmp_init(&s, PTR_TO_FD(userdata)); |
| 301 |
mock_qmp_handshake(&s); |
| 302 |
|
| 303 |
/* Consume the stop command but don't reply — cleanup closes the fd and the client |
| 304 |
* sees a disconnect while suspended. */ |
| 305 |
mock_qmp_recv(&s, &stop_cmd); |
| 306 |
return 0; |
| 307 |
} |
| 308 |
|
| 309 |
static int qmp_client_call_disconnect_fiber(void *userdata) { |
| 310 |
QmpClient *client = ASSERT_NOT_NULL(userdata); |
| 311 |
int r = qmp_client_call(client, "stop", NULL, NULL, NULL); |
| 312 |
ASSERT_TRUE(ERRNO_IS_NEG_DISCONNECT(r)); |
| 313 |
ASSERT_OK(sd_event_exit(sd_fiber_get_event(), 0)); |
| 314 |
return 0; |
| 315 |
} |
| 316 |
|
| 317 |
TEST(qmp_call_disconnect) { |
| 318 |
run_qmp_test(mock_qmp_call_disconnect_fiber, qmp_client_call_disconnect_fiber); |
| 319 |
} |
| 320 |
|
| 321 |
static int mock_qmp_fd_fiber(void *userdata) { |
| 322 |
_cleanup_(json_stream_done) JsonStream s = {}; |
| 323 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *cap_cmd = NULL, *addfd_cmd = NULL, |
| 324 |
*addfd_return = NULL; |
| 325 |
|
| 326 |
mock_qmp_init(&s, PTR_TO_FD(userdata)); |
| 327 |
ASSERT_OK(json_stream_set_allow_fd_passing_input(&s, true, true)); |
| 328 |
|
| 329 |
mock_qmp_send_greeting(&s); |
| 330 |
|
| 331 |
/* The fd may ride with either command depending on AF_UNIX coalescing; count across both. */ |
| 332 |
sd_json_variant *cap_id = mock_qmp_expect(&s, "qmp_capabilities", &cap_cmd); |
| 333 |
size_t n_fds_total = json_stream_get_n_input_fds(&s); |
| 334 |
json_stream_close_input_fds(&s); |
| 335 |
mock_qmp_reply(&s, cap_id, NULL); |
| 336 |
|
| 337 |
sd_json_variant *addfd_id = mock_qmp_expect(&s, "add-fd", &addfd_cmd); |
| 338 |
n_fds_total += json_stream_get_n_input_fds(&s); |
| 339 |
json_stream_close_input_fds(&s); |
| 340 |
ASSERT_EQ(n_fds_total, (size_t) 1); |
| 341 |
|
| 342 |
ASSERT_OK(sd_json_buildo(&addfd_return, |
| 343 |
SD_JSON_BUILD_PAIR_UNSIGNED("fdset-id", 0), |
| 344 |
SD_JSON_BUILD_PAIR_UNSIGNED("fd", 42))); |
| 345 |
mock_qmp_reply(&s, addfd_id, addfd_return); |
| 346 |
return 0; |
| 347 |
} |
| 348 |
|
| 349 |
static int qmp_client_invoke_with_fd_fiber(void *userdata) { |
| 350 |
QmpClient *client = ASSERT_NOT_NULL(userdata); |
| 351 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *args = NULL; |
| 352 |
_cleanup_close_ int fd_to_pass = -EBADF; |
| 353 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *result = NULL; |
| 354 |
|
| 355 |
fd_to_pass = ASSERT_OK_ERRNO(eventfd(0, EFD_CLOEXEC)); |
| 356 |
|
| 357 |
ASSERT_OK(sd_json_buildo(&args, SD_JSON_BUILD_PAIR_UNSIGNED("fdset-id", 0))); |
| 358 |
|
| 359 |
ASSERT_OK_POSITIVE(qmp_client_call(client, "add-fd", |
| 360 |
QMP_CLIENT_ARGS_FD(args, TAKE_FD(fd_to_pass)), |
| 361 |
&result, NULL)); |
| 362 |
ASSERT_NOT_NULL(result); |
| 363 |
ASSERT_OK(sd_event_exit(sd_fiber_get_event(), 0)); |
| 364 |
return 0; |
| 365 |
} |
| 366 |
|
| 367 |
TEST(qmp_invoke_with_fd) { |
| 368 |
run_qmp_test(mock_qmp_fd_fiber, qmp_client_invoke_with_fd_fiber); |
| 369 |
} |
| 370 |
|
| 371 |
static int on_dead_peer_reply( |
| 372 |
QmpClient *client, |
| 373 |
sd_json_variant *result, |
| 374 |
const char *error_desc, |
| 375 |
int error, |
| 376 |
void *userdata) { |
| 377 |
|
| 378 |
bool *fired = ASSERT_PTR(userdata); |
| 379 |
|
| 380 |
/* Peer was closed before the write hit the wire; expect a disconnect. */ |
| 381 |
ASSERT_TRUE(ERRNO_IS_NEG_DISCONNECT(error)); |
| 382 |
*fired = true; |
| 383 |
return 0; |
| 384 |
} |
| 385 |
|
| 386 |
/* Verify caller-supplied fds passed through QMP_CLIENT_ARGS_FD() are closed on client teardown |
| 387 |
* even when the peer is already dead: invoke enqueues, the queue item owns the fd, unref closes. */ |
| 388 |
TEST(qmp_client_invoke_failure_closes_fds) { |
| 389 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *args = NULL; |
| 390 |
_cleanup_close_ int fd_to_pass = -EBADF; |
| 391 |
QmpClient *client = NULL; |
| 392 |
_cleanup_close_pair_ int qmp_fds[2] = EBADF_PAIR; |
| 393 |
int saved_fd_value; |
| 394 |
bool callback_fired = false; |
| 395 |
|
| 396 |
ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, qmp_fds)); |
| 397 |
qmp_fds[1] = safe_close(qmp_fds[1]); |
| 398 |
|
| 399 |
fd_to_pass = ASSERT_OK_ERRNO(eventfd(0, EFD_CLOEXEC)); |
| 400 |
saved_fd_value = fd_to_pass; |
| 401 |
|
| 402 |
ASSERT_OK(sd_json_buildo(&args, SD_JSON_BUILD_PAIR_UNSIGNED("fdset-id", 0))); |
| 403 |
ASSERT_OK(qmp_client_connect_fd(&client, TAKE_FD(qmp_fds[0]))); |
| 404 |
|
| 405 |
ASSERT_OK(qmp_client_invoke(client, NULL, "add-fd", |
| 406 |
QMP_CLIENT_ARGS_FD(args, TAKE_FD(fd_to_pass)), |
| 407 |
on_dead_peer_reply, &callback_fired)); |
| 408 |
ASSERT_EQ(fd_to_pass, -EBADF); |
| 409 |
ASSERT_OK_ERRNO(fcntl(saved_fd_value, F_GETFD)); |
| 410 |
|
| 411 |
client = qmp_client_unref(client); |
| 412 |
ASSERT_ERROR_ERRNO(fcntl(saved_fd_value, F_GETFD), EBADF); |
| 413 |
ASSERT_TRUE(callback_fired); |
| 414 |
} |
| 415 |
|
| 416 |
/* Shared mock for the two slot tests: the follow-up stop is what drives the event loop long |
| 417 |
* enough to dispatch the query-status reply. */ |
| 418 |
static int mock_qmp_slot_fiber(void *userdata) { |
| 419 |
_cleanup_(json_stream_done) JsonStream s = {}; |
| 420 |
|
| 421 |
mock_qmp_init(&s, PTR_TO_FD(userdata)); |
| 422 |
mock_qmp_handshake(&s); |
| 423 |
|
| 424 |
mock_qmp_query_status_running(&s); |
| 425 |
mock_qmp_expect_and_reply(&s, "stop", NULL); |
| 426 |
return 0; |
| 427 |
} |
| 428 |
|
| 429 |
static int nop_callback( |
| 430 |
QmpClient *client, |
| 431 |
sd_json_variant *result, |
| 432 |
const char *error_desc, |
| 433 |
int error, |
| 434 |
void *userdata) { |
| 435 |
|
| 436 |
return 0; |
| 437 |
} |
| 438 |
|
| 439 |
/* Tripwire for the cancel test: if it fires, the cancel didn't do its job. */ |
| 440 |
static int tripwire_callback( |
| 441 |
QmpClient *client, |
| 442 |
sd_json_variant *result, |
| 443 |
const char *error_desc, |
| 444 |
int error, |
| 445 |
void *userdata) { |
| 446 |
|
| 447 |
bool *fired = ASSERT_PTR(userdata); |
| 448 |
*fired = true; |
| 449 |
return 0; |
| 450 |
} |
| 451 |
|
| 452 |
static int qmp_client_invoke_slot_lifecycle_fiber(void *userdata) { |
| 453 |
QmpClient *client = ASSERT_NOT_NULL(userdata); |
| 454 |
_cleanup_(qmp_slot_unrefp) QmpSlot *slot = NULL; |
| 455 |
|
| 456 |
ASSERT_OK(qmp_client_invoke(client, &slot, "query-status", NULL, nop_callback, NULL)); |
| 457 |
ASSERT_PTR_EQ(qmp_slot_get_client(slot), client); |
| 458 |
|
| 459 |
/* Drive the loop via a follow-up stop; its suspending call lets both replies dispatch. */ |
| 460 |
ASSERT_OK_POSITIVE(qmp_client_call(client, "stop", NULL, NULL, NULL)); |
| 461 |
|
| 462 |
/* After dispatch the slot is disconnected from the client but still owned by us. */ |
| 463 |
ASSERT_NULL(qmp_slot_get_client(slot)); |
| 464 |
|
| 465 |
/* Explicit out-of-order unref exercises the already-disconnected path in qmp_slot_free(). */ |
| 466 |
slot = qmp_slot_unref(slot); |
| 467 |
ASSERT_OK(sd_event_exit(sd_fiber_get_event(), 0)); |
| 468 |
return 0; |
| 469 |
} |
| 470 |
|
| 471 |
TEST(qmp_invoke_slot_lifecycle) { |
| 472 |
run_qmp_test(mock_qmp_slot_fiber, qmp_client_invoke_slot_lifecycle_fiber); |
| 473 |
} |
| 474 |
|
| 475 |
static int qmp_client_invoke_slot_cancel_fiber(void *userdata) { |
| 476 |
QmpClient *client = ASSERT_NOT_NULL(userdata); |
| 477 |
QmpSlot *slot = NULL; |
| 478 |
bool fired = false; |
| 479 |
|
| 480 |
ASSERT_OK(qmp_client_invoke(client, &slot, "query-status", NULL, tripwire_callback, &fired)); |
| 481 |
|
| 482 |
/* Drop our sole ref → slot disconnects from the client's pending set. The enqueued |
| 483 |
* query-status is still on the wire; its reply lands on an unknown id and is discarded. */ |
| 484 |
slot = qmp_slot_unref(slot); |
| 485 |
|
| 486 |
ASSERT_OK_POSITIVE(qmp_client_call(client, "stop", NULL, NULL, NULL)); |
| 487 |
|
| 488 |
ASSERT_FALSE(fired); |
| 489 |
ASSERT_OK(sd_event_exit(sd_fiber_get_event(), 0)); |
| 490 |
return 0; |
| 491 |
} |
| 492 |
|
| 493 |
TEST(qmp_invoke_slot_cancel) { |
| 494 |
run_qmp_test(mock_qmp_slot_fiber, qmp_client_invoke_slot_cancel_fiber); |
| 495 |
} |
| 496 |
|
| 497 |
TEST(qmp_schema_has_member) { |
| 498 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *schema = NULL; |
| 499 |
|
| 500 |
/* QEMU introspection uses opaque numeric type ids ("0", "1", ...); only member names |
| 501 |
* are the real QAPI strings. Verify we walk all object entries to find members by name. */ |
| 502 |
ASSERT_OK(sd_json_build(&schema, |
| 503 |
SD_JSON_BUILD_ARRAY( |
| 504 |
SD_JSON_BUILD_OBJECT( |
| 505 |
SD_JSON_BUILD_PAIR_STRING("name", "0"), |
| 506 |
SD_JSON_BUILD_PAIR_STRING("meta-type", "object"), |
| 507 |
SD_JSON_BUILD_PAIR("members", SD_JSON_BUILD_ARRAY( |
| 508 |
SD_JSON_BUILD_OBJECT( |
| 509 |
SD_JSON_BUILD_PAIR_STRING("name", "offset"), |
| 510 |
SD_JSON_BUILD_PAIR_STRING("type", "int"))))), |
| 511 |
SD_JSON_BUILD_OBJECT( |
| 512 |
SD_JSON_BUILD_PAIR_STRING("name", "SomeEnum"), |
| 513 |
SD_JSON_BUILD_PAIR_STRING("meta-type", "enum")), |
| 514 |
SD_JSON_BUILD_OBJECT( |
| 515 |
SD_JSON_BUILD_PAIR_STRING("name", "1"), |
| 516 |
SD_JSON_BUILD_PAIR_STRING("meta-type", "object"), |
| 517 |
SD_JSON_BUILD_PAIR("members", SD_JSON_BUILD_ARRAY( |
| 518 |
SD_JSON_BUILD_OBJECT( |
| 519 |
SD_JSON_BUILD_PAIR_STRING("name", "lazy-refcounts"), |
| 520 |
SD_JSON_BUILD_PAIR_STRING("type", "bool")), |
| 521 |
SD_JSON_BUILD_OBJECT( |
| 522 |
SD_JSON_BUILD_PAIR_STRING("name", "discard-no-unref"), |
| 523 |
SD_JSON_BUILD_PAIR_STRING("type", "bool")))))))); |
| 524 |
|
| 525 |
ASSERT_TRUE(qmp_schema_has_member(schema, "discard-no-unref")); |
| 526 |
ASSERT_TRUE(qmp_schema_has_member(schema, "offset")); |
| 527 |
ASSERT_FALSE(qmp_schema_has_member(schema, "definitely-not-a-real-field")); |
| 528 |
ASSERT_FALSE(qmp_schema_has_member(NULL, "discard-no-unref")); |
| 529 |
} |
| 530 |
|
| 531 |
DEFINE_TEST_MAIN(LOG_DEBUG); |