qemu: monitor: Add test validating that 'eventHandlers' are properly sorted

The monitor code uses 'bsearch' to look up the event handler so the
event names must be properly listed. Until now only a comment reminded
us to do it. Add a test to verify that it is actually sorted properly.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa
2026-02-12 16:45:34 +01:00
parent 6f372d526f
commit 25922c1fcb
3 changed files with 42 additions and 0 deletions
+21
View File
@@ -143,6 +143,27 @@ qemuMonitorEventCompare(const void *key, const void *elt)
}
/**
* qemuMonitorJSONValidateEventHandlers:
*
* Used by 'qemumonitorjsontest' to validate that the 'eventHandlers' array
* is properly sorted to use 'bsearch'.
*/
char *
qemuMonitorJSONValidateEventHandlers(void)
{
size_t i;
for (i = 1; i < G_N_ELEMENTS(eventHandlers); i++) {
if (strcmp(eventHandlers[i-1].type, eventHandlers[i].type) > -1)
return g_strdup_printf("mis-ordered 'eventHandlers': '%s', '%s'",
eventHandlers[i-1].type, eventHandlers[i].type);
}
return NULL;
}
static int
qemuMonitorJSONIOProcessEvent(qemuMonitor *mon,
virJSONValue *obj)
+3
View File
@@ -27,6 +27,9 @@
#include "cpu/cpu.h"
#include "util/virgic.h"
char *
qemuMonitorJSONValidateEventHandlers(void);
int
qemuMonitorJSONIOProcessLine(qemuMonitor *mon,
const char *line,
+18
View File
@@ -2825,6 +2825,20 @@ testQemuMonitorJSONGetGuestCPU(const void *opaque)
}
static int
testEventHandlersOrdering(const void *opaque G_GNUC_UNUSED)
{
g_autofree char *errmsg = NULL;
if ((errmsg = qemuMonitorJSONValidateEventHandlers())) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", errmsg);
return -1;
}
return 0;
}
static int
mymain(void)
{
@@ -2848,6 +2862,10 @@ mymain(void)
qapiData.schema = qapischema_x86_64;
if (virTestRun("'eventHandlers' ordering check", testEventHandlersOrdering,
NULL) < 0)
ret = -1;
#define DO_TEST(name) \
do { \
testGenericData data = { driver.xmlopt, qapiData.schema }; \