Add testsuite coverage to ensure that we reject out-of-range values
for enum/flag parameters. The fact that we can't provoke it in ocaml
is worth fixing in the next patch.
---
python/t/120-set-non-defaults.py | 12 +++++++
ocaml/tests/test_120_set_non_defaults.ml | 10 ++++++
tests/errors.c | 32 +++++++++++++++++++
.../libnbd_120_set_non_defaults_test.go | 28 ++++++++++++++--
4 files changed, 80 insertions(+), 2 deletions(-)
diff --git a/python/t/120-set-non-defaults.py b/python/t/120-set-non-defaults.py
index c04ede9..fe0bb87 100644
--- a/python/t/120-set-non-defaults.py
+++ b/python/t/120-set-non-defaults.py
@@ -22,11 +22,23 @@ h.set_export_name ("name")
assert h.get_export_name () == "name"
h.set_full_info (True)
assert h.get_full_info () == True
+try:
+ h.set_tls (nbd.TLS_REQUIRE + 1)
+ assert False
+except nbd.Error as e:
+ pass
+assert h.get_tls () == nbd.TLS_DISABLE
if h.supports_tls ():
h.set_tls (nbd.TLS_ALLOW)
assert h.get_tls () == nbd.TLS_ALLOW
h.set_request_structured_replies (False)
assert h.get_request_structured_replies () == False
+try:
+ h.set_handshake_flags (nbd.HANDSHAKE_FLAG_NO_ZEROES * 2)
+ assert False
+except nbd.Error as e:
+ assert h.get_handshake_flags () == (nbd.HANDSHAKE_FLAG_NO_ZEROES |
+ nbd.HANDSHAKE_FLAG_FIXED_NEWSTYLE)
h.set_handshake_flags (0)
assert h.get_handshake_flags () == 0
h.set_opt_mode (True)
diff --git a/ocaml/tests/test_120_set_non_defaults.ml
b/ocaml/tests/test_120_set_non_defaults.ml
index bd41e37..e616291 100644
--- a/ocaml/tests/test_120_set_non_defaults.ml
+++ b/ocaml/tests/test_120_set_non_defaults.ml
@@ -25,6 +25,11 @@ let () =
NBD.set_full_info nbd true;
let info = NBD.get_full_info nbd in
assert (info = true);
+ (* XXX No way to pass out-of-range enum...
+ try NBD.set_tls nbd XXX
+ *)
+ let tls = NBD.get_tls nbd in
+ assert (tls = 0); (* XXX Add REnum, to get NBD.TLS.DISABLE? *)
if NBD.supports_tls nbd then (
NBD.set_tls nbd NBD.TLS.ALLOW;
let tls = NBD.get_tls nbd in
@@ -33,6 +38,11 @@ let () =
NBD.set_request_structured_replies nbd false;
let sr = NBD.get_request_structured_replies nbd in
assert (sr = false);
+ (* XXX No way to pass out-of-range flags...
+ try NBD.set_handshake_flags nbd [ XXX ]
+ *)
+ let flags = NBD.get_handshake_flags nbd in
+ assert (flags = 3); (* XXX Add RFlags, to get NBD.HANDSHAKE_FLAG list? *)
NBD.set_handshake_flags nbd [];
let flags = NBD.get_handshake_flags nbd in
assert (flags = 0); (* XXX Add RFlags *)
diff --git a/tests/errors.c b/tests/errors.c
index 6627ce2..906c7da 100644
--- a/tests/errors.c
+++ b/tests/errors.c
@@ -86,6 +86,7 @@ main (int argc, char *argv[])
*/
const char *cmd[] = { "nbdkit", "-s",
"--exit-with-parent", "sh",
script, NULL };
+ int i;
progname = argv[0];
@@ -131,6 +132,37 @@ main (int argc, char *argv[])
exit (EXIT_FAILURE);
}
+ /* Attempt to set an enum to garbage. */
+ if (nbd_set_tls (nbd, LIBNBD_TLS_REQUIRE + 1) != -1) {
+ fprintf (stderr, "%s: test failed: "
+ "nbd_set_tls did not reject invalid enum\n",
+ argv[0]);
+ exit (EXIT_FAILURE);
+ }
+ if (nbd_get_tls (nbd) != LIBNBD_TLS_DISABLE) {
+ fprintf (stderr, "%s: test failed: "
+ "nbd_get_tls not left at default value\n",
+ argv[0]);
+ exit (EXIT_FAILURE);
+ }
+
+ /* Attempt to set a bitmask with an unknown bit. */
+ i = LIBNBD_HANDSHAKE_FLAG_NO_ZEROES << 1;
+ if (nbd_set_handshake_flags (nbd, i) != -1) {
+ fprintf (stderr, "%s: test failed: "
+ "nbd_set_handshake_flags did not reject invalid bitmask\n",
+ argv[0]);
+ exit (EXIT_FAILURE);
+ }
+ i = LIBNBD_HANDSHAKE_FLAG_FIXED_NEWSTYLE | LIBNBD_HANDSHAKE_FLAG_NO_ZEROES;
+ if (nbd_get_handshake_flags (nbd) != i) {
+ fprintf (stderr, "%s: test failed: "
+ "nbd_get_handshake_flags not left at default value\n",
+ argv[0]);
+ exit (EXIT_FAILURE);
+ }
+
+
/* Issue a connected command when not connected. */
if (nbd_pread (nbd, buf, 1, 0, 0) != -1) {
fprintf (stderr, "%s: test failed: "
diff --git
a/golang/src/libguestfs.org/libnbd/libnbd_120_set_non_defaults_test.go
b/golang/src/libguestfs.org/libnbd/libnbd_120_set_non_defaults_test.go
index 685604e..5d8cce7 100644
---
a/golang/src/libguestfs.org/libnbd/libnbd_120_set_non_defaults_test.go
+++
b/golang/src/libguestfs.org/libnbd/libnbd_120_set_non_defaults_test.go
@@ -51,6 +51,18 @@ func Test120SetNonDefaults(t *testing.T) {
t.Fatalf("unexpected full info state")
}
+ err = h.SetTls(TLS_REQUIRE + 1)
+ if err == nil {
+ t.Fatalf("expect failure for out-of-range enum")
+ }
+ tls, err := h.GetTls()
+ if err != nil {
+ t.Fatalf("could not get tls state: %s", err)
+ }
+ if tls != TLS_DISABLE {
+ t.Fatalf("unexpected tls state")
+ }
+
support, err := h.SupportsTls()
if err != nil {
t.Fatalf("could not check if tls is supported: %s", err)
@@ -60,7 +72,7 @@ func Test120SetNonDefaults(t *testing.T) {
if err != nil {
t.Fatalf("could not set tls state: %s", err)
}
- tls, err := h.GetTls()
+ tls, err = h.GetTls()
if err != nil {
t.Fatalf("could not get tls state: %s", err)
}
@@ -81,11 +93,23 @@ func Test120SetNonDefaults(t *testing.T) {
t.Fatalf("unexpected structured replies state")
}
+ err = h.SetHandshakeFlags(HANDSHAKE_FLAG_NO_ZEROES << 1)
+ if err == nil {
+ t.Fatalf("expect failure for out-of-range flags")
+ }
+ flags, err := h.GetHandshakeFlags()
+ if err != nil {
+ t.Fatalf("could not get handshake flags: %s", err)
+ }
+ if flags != HANDSHAKE_FLAG_FIXED_NEWSTYLE | HANDSHAKE_FLAG_NO_ZEROES {
+ t.Fatalf("unexpected handshake flags")
+ }
+
err = h.SetHandshakeFlags(0)
if err != nil {
t.Fatalf("could not set handshake flags: %s", err)
}
- flags, err := h.GetHandshakeFlags()
+ flags, err = h.GetHandshakeFlags()
if err != nil {
t.Fatalf("could not get handshake flags: %s", err)
}
--
2.28.0