Add comprehensive test coverage for virSocketAddrFormatWithPrefix()
to verify its behavior by adding macros DO_TEST_PARSE_AND_FORMAT_WITH_PREFIX
and DO_TEST_PARSE_AND_CHECK_FORMAT_WITH_PREFIX and the respective
testing function testFormatWithPrefixHelper. This commit also adds
some error handling for the unsupported AF_UNIX family.
Signed-off-by: Julio Faracco <jcfaracco@gmail.com>
---
tests/sockettest.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 63 insertions(+)
diff --git a/tests/sockettest.c b/tests/sockettest.c
index 5cb8a9fb72..9e185cc234 100644
--- a/tests/sockettest.c
+++ b/tests/sockettest.c
@@ -55,6 +55,22 @@ static int testFormat(virSocketAddr *addr, const char *addrstr, bool pass)
}
}
+static int testFormatWithPrefix(virSocketAddr *addr, const char *addrstr,
+ unsigned int prefix, bool pass)
+{
+ g_autofree char *newaddrstr = NULL;
+
+ newaddrstr = virSocketAddrFormatWithPrefix(addr, prefix);
+ if (!newaddrstr)
+ return pass ? -1 : 0;
+
+ if (virTestCompareToString(newaddrstr, addrstr) < 0) {
+ return pass ? -1 : 0;
+ } else {
+ return pass ? 0 : -1;
+ }
+}
+
struct testParseData {
virSocketAddr *addr;
const char *addrstr;
@@ -78,6 +94,19 @@ static int testFormatHelper(const void *opaque)
return testFormat(data->addr, data->addrstr, data->pass);
}
+struct testFormatWithPrefixData {
+ virSocketAddr *addr;
+ const char *addrstr;
+ unsigned int prefix;
+ bool pass;
+};
+static int testFormatWithPrefixHelper(const void *opaque)
+{
+ const struct testFormatWithPrefixData *data = opaque;
+ return testFormatWithPrefix(data->addr, data->addrstr, data->prefix,
+ data->pass);
+}
+
static int
testRange(const char *saddrstr, const char *eaddrstr,
@@ -293,6 +322,32 @@ mymain(void)
ret = -1; \
} while (0)
+#define DO_TEST_PARSE_AND_FORMAT_WITH_PREFIX(addrstr, family, prefix, pass) \
+ do { \
+ virSocketAddr addr = { 0 }; \
+ struct testParseData data = { &addr, addrstr, family, pass }; \
+ struct testFormatWithPrefixData data2 = { &addr, addrstr, prefix, pass }; \
+ if (virTestRun("Test parse " addrstr " family " #family, \
+ testParseHelper, &data) < 0) \
+ ret = -1; \
+ if (virTestRun("Test format " addrstr " family " #family " with prefix /" #prefix, \
+ testFormatWithPrefixHelper, &data2) < 0) \
+ ret = -1; \
+ } while (0)
+
+#define DO_TEST_PARSE_AND_CHECK_FORMAT_WITH_PREFIX(addrstr, addrformated, family, prefix, pass) \
+ do { \
+ virSocketAddr addr = { 0 }; \
+ struct testParseData data = { &addr, addrstr, family, pass }; \
+ struct testFormatWithPrefixData data2 = { &addr, addrformated, prefix, pass }; \
+ if (virTestRun("Test parse " addrstr " family " #family, \
+ testParseHelper, &data) < 0) \
+ ret = -1; \
+ if (virTestRun("Test format " addrstr " family " #family " with prefix /" #prefix, \
+ testFormatWithPrefixHelper, &data2) < 0) \
+ ret = -1; \
+ } while (0)
+
#define DO_TEST_RANGE(saddr, eaddr, netaddr, prefix, size, pass) \
do { \
struct testRangeData data \
@@ -376,6 +431,14 @@ mymain(void)
DO_TEST_PARSE_AND_FORMAT("::fffe:0:0", AF_UNSPEC, true);
DO_TEST_PARSE_AND_FORMAT("::ffff:10.1.2.3", AF_UNSPEC, true);
+ DO_TEST_PARSE_AND_CHECK_FORMAT_WITH_PREFIX("1.2.3.4", "1.2.3.0/24", AF_INET, 24, true);
+ DO_TEST_PARSE_AND_CHECK_FORMAT_WITH_PREFIX("10.1.1.12", "10.0.0.0/8", AF_INET, 8, true);
+ DO_TEST_PARSE_AND_CHECK_FORMAT_WITH_PREFIX("192.168.1.124", "192.168.0.0/16", AF_INET, 16, true);
+ DO_TEST_PARSE_AND_CHECK_FORMAT_WITH_PREFIX("2001:db8:dead:beef:1::", "2001:db8:dead:beef::/64", AF_INET6, 64, true);
+
+ DO_TEST_PARSE_AND_FORMAT_WITH_PREFIX("1.2.3.4", AF_UNIX, 24, false);
+ DO_TEST_PARSE_AND_FORMAT_WITH_PREFIX("2001:db8:dead:beef:1::", AF_UNIX, 64, false);
+
/* tests that specify a network that should contain the range */
DO_TEST_RANGE("192.168.122.1", "192.168.122.1", "192.168.122.1", 24, 1, true);
DO_TEST_RANGE("192.168.122.1", "192.168.122.20", "192.168.122.22", 24, 20, true);
--
2.52.0
On a Monday in 2026, Julio Faracco wrote:
>Add comprehensive test coverage for virSocketAddrFormatWithPrefix()
Calling it comprehensive is quite an exaggeration, since it has no
negative tests for IP addresses.
>to verify its behavior by adding macros DO_TEST_PARSE_AND_FORMAT_WITH_PREFIX
>and DO_TEST_PARSE_AND_CHECK_FORMAT_WITH_PREFIX and the respective
No need to name the long macros here, the reader can see them in the
commit itself.
>testing function testFormatWithPrefixHelper.
The function name is self-evident.
>This commit also adds
>some error handling for the unsupported AF_UNIX family.
>
>Signed-off-by: Julio Faracco <jcfaracco@gmail.com>
>---
> tests/sockettest.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 63 insertions(+)
>
>diff --git a/tests/sockettest.c b/tests/sockettest.c
>index 5cb8a9fb72..9e185cc234 100644
>--- a/tests/sockettest.c
>+++ b/tests/sockettest.c
>@@ -55,6 +55,22 @@ static int testFormat(virSocketAddr *addr, const char *addrstr, bool pass)
> }
> }
>
>+static int testFormatWithPrefix(virSocketAddr *addr, const char *addrstr,
>+ unsigned int prefix, bool pass)
There's a TAB in the indentation here that makes the syntax check fail.
>+{
>+ g_autofree char *newaddrstr = NULL;
>+
>+ newaddrstr = virSocketAddrFormatWithPrefix(addr, prefix);
>+ if (!newaddrstr)
>+ return pass ? -1 : 0;
>+
>+ if (virTestCompareToString(newaddrstr, addrstr) < 0) {
>+ return pass ? -1 : 0;
>+ } else {
>+ return pass ? 0 : -1;
This needs at least a VIR_TEST_DEBUG here, since virTestCompareToString
does not log anything in case of success
Jano
>+ }
>+}
>+
> struct testParseData {
> virSocketAddr *addr;
> const char *addrstr;
Em qua., 21 de jan. de 2026 às 12:57, Ján Tomko <jtomko@redhat.com> escreveu:
>
> On a Monday in 2026, Julio Faracco wrote:
> >Add comprehensive test coverage for virSocketAddrFormatWithPrefix()
>
> Calling it comprehensive is quite an exaggeration, since it has no
> negative tests for IP addresses.
>
> >to verify its behavior by adding macros DO_TEST_PARSE_AND_FORMAT_WITH_PREFIX
> >and DO_TEST_PARSE_AND_CHECK_FORMAT_WITH_PREFIX and the respective
>
> No need to name the long macros here, the reader can see them in the
> commit itself.
>
> >testing function testFormatWithPrefixHelper.
>
> The function name is self-evident.
>
> >This commit also adds
> >some error handling for the unsupported AF_UNIX family.
> >
> >Signed-off-by: Julio Faracco <jcfaracco@gmail.com>
> >---
> > tests/sockettest.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++
> > 1 file changed, 63 insertions(+)
> >
> >diff --git a/tests/sockettest.c b/tests/sockettest.c
> >index 5cb8a9fb72..9e185cc234 100644
> >--- a/tests/sockettest.c
> >+++ b/tests/sockettest.c
> >@@ -55,6 +55,22 @@ static int testFormat(virSocketAddr *addr, const char *addrstr, bool pass)
> > }
> > }
> >
> >+static int testFormatWithPrefix(virSocketAddr *addr, const char *addrstr,
> >+ unsigned int prefix, bool pass)
>
> There's a TAB in the indentation here that makes the syntax check fail.
>
> >+{
> >+ g_autofree char *newaddrstr = NULL;
> >+
> >+ newaddrstr = virSocketAddrFormatWithPrefix(addr, prefix);
> >+ if (!newaddrstr)
> >+ return pass ? -1 : 0;
> >+
> >+ if (virTestCompareToString(newaddrstr, addrstr) < 0) {
> >+ return pass ? -1 : 0;
> >+ } else {
> >+ return pass ? 0 : -1;
>
> This needs at least a VIR_TEST_DEBUG here, since virTestCompareToString
> does not log anything in case of success
I think I will include VIR_TEST_DEBUG for both cases (success and failure).
Sometimes you are expecting to test a failure as the argument @pass suggests.
Let me create a third patch, because testFormat() has a similar behavior.
>
> Jano
>
> >+ }
> >+}
> >+
> > struct testParseData {
> > virSocketAddr *addr;
> > const char *addrstr;
--
Julio
© 2016 - 2026 Red Hat, Inc.