tests: Use the new DO_TEST_CAPS_*() macros
[libvirt/ericb.git] / tests / qemucapsprobemock.c
blobf3f17f2116257cb61160603af584849433ff836a
1 /*
2 * Copyright (C) 2016 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
19 #include <config.h>
20 #include <dlfcn.h>
22 #include "internal.h"
23 #include "viralloc.h"
24 #include "virjson.h"
25 #include "qemu/qemu_monitor.h"
26 #include "qemu/qemu_monitor_json.h"
28 #define REAL_SYM(realFunc) \
29 do { \
30 if (!realFunc && !(realFunc = dlsym(RTLD_NEXT, __FUNCTION__))) { \
31 fprintf(stderr, "Cannot find real '%s' symbol\n", \
32 __FUNCTION__); \
33 abort(); \
34 } \
35 } while (0)
37 static bool first = true;
39 static void
40 printLineSkipEmpty(const char *line,
41 FILE *fp)
43 const char *p;
45 for (p = line; *p; p++) {
46 if (p[0] == '\n' && p[1] == '\n')
47 continue;
49 fputc(*p, fp);
54 static int (*realQemuMonitorSend)(qemuMonitorPtr mon,
55 qemuMonitorMessagePtr msg);
57 int
58 qemuMonitorSend(qemuMonitorPtr mon,
59 qemuMonitorMessagePtr msg)
61 char *reformatted;
63 REAL_SYM(realQemuMonitorSend);
65 if (!(reformatted = virJSONStringReformat(msg->txBuffer, true))) {
66 fprintf(stderr, "Failed to reformat command string '%s'\n", msg->txBuffer);
67 abort();
70 if (first)
71 first = false;
72 else
73 printLineSkipEmpty("\n", stdout);
75 printLineSkipEmpty(reformatted, stdout);
76 VIR_FREE(reformatted);
78 return realQemuMonitorSend(mon, msg);
82 static int (*realQemuMonitorJSONIOProcessLine)(qemuMonitorPtr mon,
83 const char *line,
84 qemuMonitorMessagePtr msg);
86 int
87 qemuMonitorJSONIOProcessLine(qemuMonitorPtr mon,
88 const char *line,
89 qemuMonitorMessagePtr msg)
91 virJSONValuePtr value = NULL;
92 char *json = NULL;
93 int ret;
95 REAL_SYM(realQemuMonitorJSONIOProcessLine);
97 ret = realQemuMonitorJSONIOProcessLine(mon, line, msg);
99 if (ret == 0) {
100 if (!(value = virJSONValueFromString(line)) ||
101 !(json = virJSONValueToString(value, true))) {
102 fprintf(stderr, "Failed to reformat reply string '%s'\n", line);
103 abort();
106 /* Ignore QMP greeting */
107 if (virJSONValueObjectHasKey(value, "QMP"))
108 goto cleanup;
110 if (first)
111 first = false;
112 else
113 printLineSkipEmpty("\n", stdout);
115 printLineSkipEmpty(json, stdout);
118 cleanup:
119 VIR_FREE(json);
120 virJSONValueFree(value);
121 return ret;