[PATCH v3 0/2] Include test cases for virSocketAddrFormatWithPrefix
Recently, we removed the masked argument in commit 6f07a8e6 from the respective function. But, in sockettest.c, we don't have any test case to cover this function. This series adds some test cases for virSocketAddrFormatWithPrefix and an extra patch to increase the testFormat debugging info. Julio Faracco (2): tests: Add tests for virSocketAddrFormatWithPrefix tests: Add debug information in testFormat() tests/sockettest.c | 77 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) -- 2.52.0
Add a simple test coverage for virSocketAddrFormatWithPrefix() to verify its behavior. This commit also adds some error handling for the unsupported AF_UNIX family. Signed-off-by: Julio Faracco <jcfaracco@gmail.com> --- tests/sockettest.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/tests/sockettest.c b/tests/sockettest.c index 5cb8a9fb72..b26503a0d4 100644 --- a/tests/sockettest.c +++ b/tests/sockettest.c @@ -55,6 +55,28 @@ 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; + int rc; + + newaddrstr = virSocketAddrFormatWithPrefix(addr, prefix); + if (!newaddrstr) + return pass ? -1 : 0; + + rc = virTestCompareToString(newaddrstr, addrstr); + + VIR_TEST_DEBUG("Addresses %s and %s %s", newaddrstr, addrstr, + rc < 0 ? "doesn't match" : "matches"); + + if (rc < 0) { + return pass ? -1 : 0; + } else { + return pass ? 0 : -1; + } +} + struct testParseData { virSocketAddr *addr; const char *addrstr; @@ -78,6 +100,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 +328,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 +437,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
The testFormat() function should have debug information for the formatted address and what is expected. Since, virTestCompareToString() does not produce any extra debug info, VIR_TEST_DEBUG should be used in this test. Signed-off-by: Julio Faracco <jcfaracco@gmail.com> --- tests/sockettest.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/sockettest.c b/tests/sockettest.c index b26503a0d4..810a49364a 100644 --- a/tests/sockettest.c +++ b/tests/sockettest.c @@ -43,12 +43,18 @@ static int testParse(virSocketAddr *addr, const char *addrstr, int family, bool static int testFormat(virSocketAddr *addr, const char *addrstr, bool pass) { g_autofree char *newaddrstr = NULL; + int rc; newaddrstr = virSocketAddrFormat(addr); if (!newaddrstr) return pass ? -1 : 0; - if (virTestCompareToString(newaddrstr, addrstr) < 0) { + rc = virTestCompareToString(newaddrstr, addrstr); + + VIR_TEST_DEBUG("Addresses %s and %s %s", newaddrstr, addrstr, + rc < 0 ? "doesn't match" : "matches"); + + if (rc < 0) { return pass ? -1 : 0; } else { return pass ? 0 : -1; -- 2.52.0
participants (1)
-
Julio Faracco