[libvirt] [PATCH] test: fix IP address range failure test

Laine Stump laine at laine.org
Mon Jun 1 20:09:12 UTC 2015


This was revealed when I made a cut-paste mistake in an upgrade to
virSocketAddrGetRange(), leading to failure to check for the end
address being outside of the defined network, but a negative test case
that should have caught the error instead returned success.

The problem was that testRange in sockettest.c was written so that
when it expected a failure, even an "unexpected success" would be
considered as an "expected failure" because of the way the check in
testRange was done. testRange had this:

 if (gotsize < 0 || gotsize != size) {
     return pass ? -1 : 0;
 } else {
     return pass ? 0 : -1;
 }

but all the tests that expected a failure give "-1" as the expected
size. So in a case where we expect a failure, we would have pass ==
false and size == -1. If virSocketAddrGetRange() was incorrectly
*successful* (returned some positive number), then "gotsize != size"
would be, e.g. "276 != -1", so we would take the if clause and, since
pass == false, we would return 0 (success i.e. expected failure).

The solution is that in the case where we expect failure, we should
just ignore size - virSocketAddrGetRange() must return -1 in order for
us to report "expected failure == success".
---

Here's the email where jferlan points out the typo and asks about the
apparent lack of a proper negative test case:

  https://www.redhat.com/archives/libvir-list/2015-May/msg01166.html

tests/sockettest.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/tests/sockettest.c b/tests/sockettest.c
index 84170d5..292edb6 100644
--- a/tests/sockettest.c
+++ b/tests/sockettest.c
@@ -103,10 +103,12 @@ testRange(const char *saddrstr, const char *eaddrstr,
 
     int gotsize = virSocketAddrGetRange(&saddr, &eaddr, &netaddr, prefix);
     VIR_DEBUG("Size want %d vs got %d", size, gotsize);
-    if (gotsize < 0 || gotsize != size) {
-        return pass ? -1 : 0;
+    if (pass) {
+        /* fail if virSocketAddrGetRange returns failure, or unexpected size */
+        return (gotsize < 0 || gotsize != size) ? -1 : 0;
     } else {
-        return pass ? 0 : -1;
+        /* succeed if virSocketAddrGetRange fails, otherwise fail. */
+        return gotsize < 0 ? 0 : -1;
     }
 }
 
-- 
2.1.0




More information about the libvir-list mailing list