Add BER Visitor hooks to test-visitor-serialization

Cc: Michael Tsirkin <m...@redhat.com>
Cc: Stefan Berger <stef...@linux.vnet.ibm.com>
Signed-off-by: Joel Schopp <jsch...@linux.vnet.ibm.com>
---
 tests/Makefile                     |    2 +-
 tests/test-visitor-serialization.c |   72 ++++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+), 1 deletion(-)

diff --git a/tests/Makefile b/tests/Makefile
index 567e36e..578d732 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -124,7 +124,7 @@ tests/test-qmp-output-visitor$(EXESUF): 
tests/test-qmp-output-visitor.o $(test-q
 tests/test-qmp-input-visitor$(EXESUF): tests/test-qmp-input-visitor.o 
$(test-qapi-obj-y) libqemuutil.a libqemustub.a
 tests/test-qmp-input-strict$(EXESUF): tests/test-qmp-input-strict.o 
$(test-qapi-obj-y) libqemuutil.a libqemustub.a
 tests/test-qmp-commands$(EXESUF): tests/test-qmp-commands.o 
tests/test-qmp-marshal.o $(test-qapi-obj-y) qapi-types.o qapi-visit.o 
libqemuutil.a libqemustub.a
-tests/test-visitor-serialization$(EXESUF): tests/test-visitor-serialization.o 
$(test-qapi-obj-y) libqemuutil.a libqemustub.a
+tests/test-visitor-serialization$(EXESUF): tests/test-visitor-serialization.o 
$(test-qapi-obj-y) $(block-obj-y) libqemuutil.a libqemustub.a
 
 tests/test-mul64$(EXESUF): tests/test-mul64.o libqemuutil.a
 
diff --git a/tests/test-visitor-serialization.c 
b/tests/test-visitor-serialization.c
index 3c6b8df..aae7464 100644
--- a/tests/test-visitor-serialization.c
+++ b/tests/test-visitor-serialization.c
@@ -14,6 +14,7 @@
 #include <stdlib.h>
 #include <stdint.h>
 #include <float.h>
+#include <math.h>
 
 #include "qemu-common.h"
 #include "test-qapi-types.h"
@@ -23,6 +24,9 @@
 #include "qapi/qmp-output-visitor.h"
 #include "qapi/string-input-visitor.h"
 #include "qapi/string-output-visitor.h"
+#include "qapi/ber-input-visitor.h"
+#include "qapi/ber-output-visitor.h"
+#include "migration/qemu-file.h"
 
 typedef struct PrimitiveType {
     union {
@@ -701,6 +705,59 @@ static void string_cleanup(void *datap)
     string_input_visitor_cleanup(d->siv);
 }
 
+
+typedef struct BERSerializeData {
+    BEROutputVisitor *sov;
+    QEMUFile *qoutfile;
+    BERInputVisitor *siv;
+    QEMUFile *qinfile;
+} BERSerializeData;
+
+static void ber_serialize(void *native_in, void **datap,
+                          VisitorFunc visit, Error **errp, BERTypePC 
ber_type_pc)
+{
+    BERSerializeData *d = g_malloc0(sizeof(*d));
+
+    d->qoutfile = qemu_bufopen("w", NULL);
+    d->sov = ber_output_visitor_new(d->qoutfile, ber_type_pc);
+    visit(ber_output_get_visitor(d->sov), &native_in, errp);
+    *datap = d;
+}
+
+static void ber_primitive_serialize(void *native_in, void **datap,
+                          VisitorFunc visit, Error **errp)
+{
+    ber_serialize(native_in, datap, visit, errp, BER_TYPE_PRIMITIVE);
+}
+
+static void ber_constructed_serialize(void *native_in, void **datap,
+                                      VisitorFunc visit, Error **errp)
+{
+    ber_serialize(native_in, datap, visit, errp, BER_TYPE_CONSTRUCTED);
+}
+
+static void ber_deserialize(void **native_out, void *datap,
+                               VisitorFunc visit, Error **errp)
+{
+    BERSerializeData *d = datap;
+    const QEMUSizedBuffer *qsb = qemu_buf_get(d->qoutfile);
+    QEMUSizedBuffer *new_qsb = qsb_clone(qsb);
+    g_assert(new_qsb != NULL);
+
+    d->qinfile = qemu_bufopen("r", new_qsb);
+
+    d->siv = ber_input_visitor_new(d->qinfile, ~0);
+    visit(ber_input_get_visitor(d->siv), native_out, errp);
+}
+
+static void ber_cleanup(void *datap)
+{
+    BERSerializeData *d = datap;
+    ber_output_visitor_cleanup(d->sov);
+    ber_input_visitor_cleanup(d->siv);
+}
+
+
 /* visitor registration, test harness */
 
 /* note: to function interchangeably as a serialization mechanism your
@@ -722,6 +779,21 @@ static const SerializeOps visitors[] = {
         .cleanup = string_cleanup,
         .caps = VCAP_PRIMITIVES
     },
+    {
+        .type = "ASN.1 BER primitives",
+        .serialize = ber_primitive_serialize,
+        .deserialize = ber_deserialize,
+        .cleanup = ber_cleanup,
+        .caps = VCAP_PRIMITIVES
+    },
+    {
+        .type = "ASN.1 BER constructed",
+        .serialize = ber_constructed_serialize,
+        .deserialize = ber_deserialize,
+        .cleanup = ber_cleanup,
+        .caps = VCAP_PRIMITIVES | VCAP_LISTS
+    },
+
     { NULL }
 };
 
-- 
1.7.10.4


Reply via email to