[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v2 1/9] tools/ocaml: add block checks to the bindings test



The Xenctrl C stubs build OCaml values by hand, and nothing checks that
the blocks they allocate match the types declared in xenctrl.mli. A
mismatch, e.g. a record or variant argument with a different number of
fields, isn't caught at build time.

So, add helpers to test-ocaml-bindings that check the tag and size of a
block, and that a value is one of the constant constructors of a
variant, and check the version record with them. Also, count the
failures, report Xenctrl.Error per test instead of aborting, and exit
with an error if any check failed.

OCaml values don't carry their type at run time, so the expected sizes
are written down by hand and have to be updated together with
xenctrl.mli.

Signed-off-by: Julian Vetter <julian.vetter@xxxxxxxxxx>
---
Changes in v2:
- Split out of the v1 patch
- Open Xenctrl, to remove the Xenctrl. prefixes
---
 tools/ocaml/tests/bindings/main.ml | 41 +++++++++++++++++++++++++++---
 1 file changed, 38 insertions(+), 3 deletions(-)

diff --git a/tools/ocaml/tests/bindings/main.ml 
b/tools/ocaml/tests/bindings/main.ml
index 785837e270..3302a7f054 100644
--- a/tools/ocaml/tests/bindings/main.ml
+++ b/tools/ocaml/tests/bindings/main.ml
@@ -1,19 +1,54 @@
 open Printf
+open Xenctrl
 
-let xch = Xenctrl.interface_open ()
+let xch = interface_open ()
+
+let nr_failures = ref 0
+
+let fail fmt =
+  ksprintf (fun s -> incr nr_failures; printf "  Fail: %s\n%!" s) fmt
+
+(* Check that [v] is a block with the given tag and number of fields. *)
+let check_block name v ~tag ~size =
+  let r = Obj.repr v in
+  if Obj.is_int r then
+    fail "%s: immediate value, expected a block" name
+  else if Obj.tag r <> tag || Obj.size r <> size then
+    fail "%s: tag %d, size %d, expected tag %d, size %d"
+      name (Obj.tag r) (Obj.size r) tag size
+
+let check_record name v ~size = check_block name v ~tag:0 ~size
+
+(* Check that [v] is one of the [nr] constant constructors of a variant. *)
+let check_enum name v ~nr =
+  let r = Obj.repr v in
+  let ok = Obj.is_int r && (Obj.obj r : int) >= 0 && (Obj.obj r : int) < nr in
+  if not ok then
+    fail "%s: not one of the %d constructors" name nr;
+  ok
 
 let test_xenversion () =
-  let ver = Xenctrl.version xch in
+  let ver = version xch in
+  check_record "version" ver ~size:3;
   printf "Xen version: %d.%d%s\n" ver.major ver.minor ver.extra
 
+let run name f =
+  try f () with
+  | Xenctrl.Error e -> fail "%s: Xenctrl.Error %s" name e
+
 
 let () =
   printf "Test Ocaml Bindings\n%!";
 
-  test_xenversion ();
+  run "version" test_xenversion;
 
   printf "Starting GC.compact()\n%!";
   Gc.compact ();
   printf "GC done\n";
 
+  if !nr_failures > 0 then begin
+    printf "%d failures\n" !nr_failures;
+    exit 1
+  end;
+
   ()
-- 
2.53.0



--
Julian Vetter | Vates Hypervisor & Kernel Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.