From: Peter Krempa <[email protected]>

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

Signed-off-by: Peter Krempa <[email protected]>
---
 src/qemu/qemu_monitor_json.c | 21 +++++++++++++++++++++
 src/qemu/qemu_monitor_json.h |  3 +++
 tests/qemumonitorjsontest.c  | 18 ++++++++++++++++++
 3 files changed, 42 insertions(+)

diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index 021995f5cc..825508e8f5 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -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)
diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h
index db9160eb68..8f5434d0df 100644
--- a/src/qemu/qemu_monitor_json.h
+++ b/src/qemu/qemu_monitor_json.h
@@ -27,6 +27,9 @@
 #include "cpu/cpu.h"
 #include "util/virgic.h"

+char *
+qemuMonitorJSONValidateEventHandlers(void);
+
 int
 qemuMonitorJSONIOProcessLine(qemuMonitor *mon,
                              const char *line,
diff --git a/tests/qemumonitorjsontest.c b/tests/qemumonitorjsontest.c
index b3aca6a6c3..9c9b3397ad 100644
--- a/tests/qemumonitorjsontest.c
+++ b/tests/qemumonitorjsontest.c
@@ -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 }; \
-- 
2.53.0

Reply via email to