diff options
author | Guido Günther <agx@sigxcpu.org> | 2014-05-04 13:35:32 +0200 |
---|---|---|
committer | Guido Günther <agx@sigxcpu.org> | 2014-05-04 13:35:32 +0200 |
commit | b38846c74874f77464e63fcbc0da39cbe73c5670 (patch) | |
tree | a3165ac8394f40f873b6d0b809616a34348bac11 /tests | |
parent | 1190eb8d003c872dcb203f9e3dade0c92f14cc9c (diff) |
New upstream version 1.2.4
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 12 | ||||
-rw-r--r-- | tests/Makefile.in | 22 | ||||
-rw-r--r-- | tests/networkxml2firewalltest.c | 3 | ||||
-rw-r--r-- | tests/nwfilterebiptablestest.c | 64 | ||||
-rw-r--r-- | tests/qemuxml2argvtest.c | 4 | ||||
-rw-r--r-- | tests/virfirewalltest.c | 58 | ||||
-rw-r--r-- | tests/virstringtest.c | 200 |
7 files changed, 300 insertions, 63 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index 4a5e14bd7..5ef89408b 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -782,7 +782,13 @@ commandtest_LDADD = $(LDADDS) commandhelper_SOURCES = \ commandhelper.c -commandhelper_LDADD = $(LDADDS) +commandhelper_LDADD = \ + $(WARN_CFLAGS) \ + $(NO_INDIRECT_LDFLAGS) \ + $(PROBES_O) \ + ../src/libvirt_util.la \ + $(GNULIB_LIBS) + commandhelper_LDFLAGS = -static virnetdevbandwidthtest_SOURCES = \ @@ -957,7 +963,7 @@ virmockdbus_la_LDFLAGS = -module -avoid-version \ virsystemdtest_SOURCES = \ virsystemdtest.c testutils.h testutils.c virsystemdtest_CFLAGS = $(AM_CFLAGS) $(DBUS_CFLAGS) -virsystemdtest_LDADD = $(LDADDS) +virsystemdtest_LDADD = $(LDADDS) $(DBUS_LIBS) else ! WITH_DBUS EXTRA_DIST += virdbustest.c virmockdbus.c virsystemdtest.c @@ -1041,7 +1047,7 @@ virfiletest_LDADD = $(LDADDS) virfirewalltest_SOURCES = \ virfirewalltest.c testutils.h testutils.c -virfirewalltest_LDADD = $(LDADDS) +virfirewalltest_LDADD = $(LDADDS) $(DBUS_LIBS) virfirewalltest_CFLAGS = $(AM_CFLAGS) $(DBUS_CFLAGS) jsontest_SOURCES = \ diff --git a/tests/Makefile.in b/tests/Makefile.in index 9b2a5540f..f32373d23 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -619,7 +619,9 @@ bhyvexml2xmltest_OBJECTS = $(am_bhyvexml2xmltest_OBJECTS) @WITH_BHYVE_TRUE@ $(am__DEPENDENCIES_3) am_commandhelper_OBJECTS = commandhelper.$(OBJEXT) commandhelper_OBJECTS = $(am_commandhelper_OBJECTS) -commandhelper_DEPENDENCIES = $(am__DEPENDENCIES_2) +commandhelper_DEPENDENCIES = $(am__DEPENDENCIES_1) \ + $(am__DEPENDENCIES_1) $(PROBES_O) ../src/libvirt_util.la \ + $(GNULIB_LIBS) commandhelper_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ $(commandhelper_LDFLAGS) $(LDFLAGS) -o $@ @@ -987,7 +989,8 @@ am_virfirewalltest_OBJECTS = \ virfirewalltest-virfirewalltest.$(OBJEXT) \ virfirewalltest-testutils.$(OBJEXT) virfirewalltest_OBJECTS = $(am_virfirewalltest_OBJECTS) -virfirewalltest_DEPENDENCIES = $(am__DEPENDENCIES_2) +virfirewalltest_DEPENDENCIES = $(am__DEPENDENCIES_2) \ + $(am__DEPENDENCIES_1) virfirewalltest_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC \ $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=link $(CCLD) \ $(virfirewalltest_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) $(LDFLAGS) \ @@ -1102,7 +1105,8 @@ am__virsystemdtest_SOURCES_DIST = virsystemdtest.c testutils.h \ @WITH_DBUS_TRUE@ virsystemdtest-virsystemdtest.$(OBJEXT) \ @WITH_DBUS_TRUE@ virsystemdtest-testutils.$(OBJEXT) virsystemdtest_OBJECTS = $(am_virsystemdtest_OBJECTS) -@WITH_DBUS_TRUE@virsystemdtest_DEPENDENCIES = $(am__DEPENDENCIES_2) +@WITH_DBUS_TRUE@virsystemdtest_DEPENDENCIES = $(am__DEPENDENCIES_2) \ +@WITH_DBUS_TRUE@ $(am__DEPENDENCIES_1) virsystemdtest_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC \ $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=link $(CCLD) \ $(virsystemdtest_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o \ @@ -3415,7 +3419,13 @@ commandtest_LDADD = $(LDADDS) commandhelper_SOURCES = \ commandhelper.c -commandhelper_LDADD = $(LDADDS) +commandhelper_LDADD = \ + $(WARN_CFLAGS) \ + $(NO_INDIRECT_LDFLAGS) \ + $(PROBES_O) \ + ../src/libvirt_util.la \ + $(GNULIB_LIBS) + commandhelper_LDFLAGS = -static virnetdevbandwidthtest_SOURCES = \ virnetdevbandwidthtest.c testutils.h testutils.c @@ -3572,7 +3582,7 @@ virpcimock_la_LDFLAGS = -module -avoid-version \ @WITH_DBUS_TRUE@ virsystemdtest.c testutils.h testutils.c @WITH_DBUS_TRUE@virsystemdtest_CFLAGS = $(AM_CFLAGS) $(DBUS_CFLAGS) -@WITH_DBUS_TRUE@virsystemdtest_LDADD = $(LDADDS) +@WITH_DBUS_TRUE@virsystemdtest_LDADD = $(LDADDS) $(DBUS_LIBS) viruritest_SOURCES = \ viruritest.c testutils.h testutils.c @@ -3641,7 +3651,7 @@ virfiletest_LDADD = $(LDADDS) virfirewalltest_SOURCES = \ virfirewalltest.c testutils.h testutils.c -virfirewalltest_LDADD = $(LDADDS) +virfirewalltest_LDADD = $(LDADDS) $(DBUS_LIBS) virfirewalltest_CFLAGS = $(AM_CFLAGS) $(DBUS_CFLAGS) jsontest_SOURCES = \ jsontest.c testutils.h testutils.c diff --git a/tests/networkxml2firewalltest.c b/tests/networkxml2firewalltest.c index 9255e0149..1225c443d 100644 --- a/tests/networkxml2firewalltest.c +++ b/tests/networkxml2firewalltest.c @@ -21,9 +21,10 @@ #include <config.h> +#include "testutils.h" + #if defined (__linux__) -# include "testutils.h" # include "network/bridge_driver_platform.h" # include "virbuffer.h" diff --git a/tests/nwfilterebiptablestest.c b/tests/nwfilterebiptablestest.c index df939d5fe..e04bc21e5 100644 --- a/tests/nwfilterebiptablestest.c +++ b/tests/nwfilterebiptablestest.c @@ -33,11 +33,43 @@ #define VIR_FROM_THIS VIR_FROM_NONE + +#define VIR_NWFILTER_NEW_RULES_TEARDOWN \ + "iptables -D libvirt-out -m physdev --physdev-is-bridged --physdev-out vnet0 -g FP-vnet0\n" \ + "iptables -D libvirt-out -m physdev --physdev-out vnet0 -g FP-vnet0\n" \ + "iptables -D libvirt-in -m physdev --physdev-in vnet0 -g FJ-vnet0\n" \ + "iptables -D libvirt-host-in -m physdev --physdev-in vnet0 -g HJ-vnet0\n" \ + "iptables -F FP-vnet0\n" \ + "iptables -X FP-vnet0\n" \ + "iptables -F FJ-vnet0\n" \ + "iptables -X FJ-vnet0\n" \ + "iptables -F HJ-vnet0\n" \ + "iptables -X HJ-vnet0\n" \ + "ip6tables -D libvirt-out -m physdev --physdev-is-bridged --physdev-out vnet0 -g FP-vnet0\n" \ + "ip6tables -D libvirt-out -m physdev --physdev-out vnet0 -g FP-vnet0\n" \ + "ip6tables -D libvirt-in -m physdev --physdev-in vnet0 -g FJ-vnet0\n" \ + "ip6tables -D libvirt-host-in -m physdev --physdev-in vnet0 -g HJ-vnet0\n" \ + "ip6tables -F FP-vnet0\n" \ + "ip6tables -X FP-vnet0\n" \ + "ip6tables -F FJ-vnet0\n" \ + "ip6tables -X FJ-vnet0\n" \ + "ip6tables -F HJ-vnet0\n" \ + "ip6tables -X HJ-vnet0\n" \ + "ebtables -t nat -D PREROUTING -i vnet0 -j libvirt-J-vnet0\n" \ + "ebtables -t nat -D POSTROUTING -o vnet0 -j libvirt-P-vnet0\n" \ + "ebtables -t nat -L libvirt-J-vnet0\n" \ + "ebtables -t nat -L libvirt-P-vnet0\n" \ + "ebtables -t nat -F libvirt-J-vnet0\n" \ + "ebtables -t nat -X libvirt-J-vnet0\n" \ + "ebtables -t nat -F libvirt-P-vnet0\n" \ + "ebtables -t nat -X libvirt-P-vnet0\n" + static int testNWFilterEBIPTablesAllTeardown(const void *opaque ATTRIBUTE_UNUSED) { virBuffer buf = VIR_BUFFER_INITIALIZER; const char *expected = + VIR_NWFILTER_NEW_RULES_TEARDOWN "iptables -D libvirt-out -m physdev --physdev-is-bridged --physdev-out vnet0 -g FO-vnet0\n" "iptables -D libvirt-out -m physdev --physdev-out vnet0 -g FO-vnet0\n" "iptables -D libvirt-in -m physdev --physdev-in vnet0 -g FI-vnet0\n" @@ -221,34 +253,7 @@ testNWFilterEBIPTablesTearNewRules(const void *opaque ATTRIBUTE_UNUSED) { virBuffer buf = VIR_BUFFER_INITIALIZER; const char *expected = - "iptables -D libvirt-out -m physdev --physdev-is-bridged --physdev-out vnet0 -g FP-vnet0\n" - "iptables -D libvirt-out -m physdev --physdev-out vnet0 -g FP-vnet0\n" - "iptables -D libvirt-in -m physdev --physdev-in vnet0 -g FJ-vnet0\n" - "iptables -D libvirt-host-in -m physdev --physdev-in vnet0 -g HJ-vnet0\n" - "iptables -F FP-vnet0\n" - "iptables -X FP-vnet0\n" - "iptables -F FJ-vnet0\n" - "iptables -X FJ-vnet0\n" - "iptables -F HJ-vnet0\n" - "iptables -X HJ-vnet0\n" - "ip6tables -D libvirt-out -m physdev --physdev-is-bridged --physdev-out vnet0 -g FP-vnet0\n" - "ip6tables -D libvirt-out -m physdev --physdev-out vnet0 -g FP-vnet0\n" - "ip6tables -D libvirt-in -m physdev --physdev-in vnet0 -g FJ-vnet0\n" - "ip6tables -D libvirt-host-in -m physdev --physdev-in vnet0 -g HJ-vnet0\n" - "ip6tables -F FP-vnet0\n" - "ip6tables -X FP-vnet0\n" - "ip6tables -F FJ-vnet0\n" - "ip6tables -X FJ-vnet0\n" - "ip6tables -F HJ-vnet0\n" - "ip6tables -X HJ-vnet0\n" - "ebtables -t nat -D PREROUTING -i vnet0 -j libvirt-J-vnet0\n" - "ebtables -t nat -D POSTROUTING -o vnet0 -j libvirt-P-vnet0\n" - "ebtables -t nat -L libvirt-J-vnet0\n" - "ebtables -t nat -L libvirt-P-vnet0\n" - "ebtables -t nat -F libvirt-J-vnet0\n" - "ebtables -t nat -X libvirt-J-vnet0\n" - "ebtables -t nat -F libvirt-P-vnet0\n" - "ebtables -t nat -X libvirt-P-vnet0\n"; + VIR_NWFILTER_NEW_RULES_TEARDOWN; char *actual = NULL; int ret = -1; @@ -282,6 +287,7 @@ testNWFilterEBIPTablesApplyBasicRules(const void *opaque ATTRIBUTE_UNUSED) { virBuffer buf = VIR_BUFFER_INITIALIZER; const char *expected = + VIR_NWFILTER_NEW_RULES_TEARDOWN "iptables -D libvirt-out -m physdev --physdev-is-bridged --physdev-out vnet0 -g FO-vnet0\n" "iptables -D libvirt-out -m physdev --physdev-out vnet0 -g FO-vnet0\n" "iptables -D libvirt-in -m physdev --physdev-in vnet0 -g FI-vnet0\n" @@ -353,6 +359,7 @@ testNWFilterEBIPTablesApplyDHCPOnlyRules(const void *opaque ATTRIBUTE_UNUSED) { virBuffer buf = VIR_BUFFER_INITIALIZER; const char *expected = + VIR_NWFILTER_NEW_RULES_TEARDOWN "iptables -D libvirt-out -m physdev --physdev-is-bridged --physdev-out vnet0 -g FO-vnet0\n" "iptables -D libvirt-out -m physdev --physdev-out vnet0 -g FO-vnet0\n" "iptables -D libvirt-in -m physdev --physdev-in vnet0 -g FI-vnet0\n" @@ -443,6 +450,7 @@ testNWFilterEBIPTablesApplyDropAllRules(const void *opaque ATTRIBUTE_UNUSED) { virBuffer buf = VIR_BUFFER_INITIALIZER; const char *expected = + VIR_NWFILTER_NEW_RULES_TEARDOWN "iptables -D libvirt-out -m physdev --physdev-is-bridged --physdev-out vnet0 -g FO-vnet0\n" "iptables -D libvirt-out -m physdev --physdev-out vnet0 -g FO-vnet0\n" "iptables -D libvirt-in -m physdev --physdev-in vnet0 -g FI-vnet0\n" diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c index a1ef2b800..14482fd63 100644 --- a/tests/qemuxml2argvtest.c +++ b/tests/qemuxml2argvtest.c @@ -499,9 +499,11 @@ mymain(void) return EXIT_FAILURE; } - driver.config = virQEMUDriverConfigNew(true); + driver.config = virQEMUDriverConfigNew(false); if (driver.config == NULL) return EXIT_FAILURE; + else + driver.config->privileged = true; VIR_FREE(driver.config->spiceListen); VIR_FREE(driver.config->vncListen); diff --git a/tests/virfirewalltest.c b/tests/virfirewalltest.c index 805fa44b7..ba2e6ad4c 100644 --- a/tests/virfirewalltest.c +++ b/tests/virfirewalltest.c @@ -24,23 +24,26 @@ #define __VIR_COMMAND_PRIV_H_ALLOW__ #include "testutils.h" -#include "virbuffer.h" -#include "vircommandpriv.h" -#include "virfirewallpriv.h" -#include "virmock.h" -#include "virdbuspriv.h" -#define VIR_FROM_THIS VIR_FROM_FIREWALL +#if defined(__linux__) -#if WITH_DBUS -# include <dbus/dbus.h> -#endif +# include "virbuffer.h" +# include "vircommandpriv.h" +# include "virfirewallpriv.h" +# include "virmock.h" +# include "virdbuspriv.h" + +# define VIR_FROM_THIS VIR_FROM_FIREWALL + +# if WITH_DBUS +# include <dbus/dbus.h> +# endif static bool fwDisabled = true; static virBufferPtr fwBuf; static bool fwError; -#define TEST_FILTER_TABLE_LIST \ +# define TEST_FILTER_TABLE_LIST \ "Chain INPUT (policy ACCEPT)\n" \ "target prot opt source destination\n" \ "\n" \ @@ -50,7 +53,7 @@ static bool fwError; "Chain OUTPUT (policy ACCEPT)\n" \ "target prot opt source destination\n" -#define TEST_NAT_TABLE_LIST \ +# define TEST_NAT_TABLE_LIST \ "Chain PREROUTING (policy ACCEPT)\n" \ "target prot opt source destination\n" \ "\n" \ @@ -63,7 +66,7 @@ static bool fwError; "Chain POSTROUTING (policy ACCEPT)\n" \ "target prot opt source destination\n" -#if WITH_DBUS +# if WITH_DBUS VIR_MOCK_IMPL_RET_ARGS(dbus_connection_send_with_reply_and_block, DBusMessage *, DBusConnection *, connection, @@ -186,7 +189,7 @@ VIR_MOCK_IMPL_RET_ARGS(dbus_connection_send_with_reply_and_block, goto cleanup; } -#endif +# endif struct testFirewallData { virFirewallBackend tryBackend; @@ -1126,7 +1129,7 @@ mymain(void) { int ret = 0; -#define RUN_TEST_DIRECT(name, method) \ +# define RUN_TEST_DIRECT(name, method) \ do { \ struct testFirewallData data; \ data.tryBackend = VIR_FIREWALL_BACKEND_AUTOMATIC; \ @@ -1141,8 +1144,8 @@ mymain(void) ret = -1; \ } while (0) -#if WITH_DBUS -# define RUN_TEST_FIREWALLD(name, method) \ +# if WITH_DBUS +# define RUN_TEST_FIREWALLD(name, method) \ do { \ struct testFirewallData data; \ data.tryBackend = VIR_FIREWALL_BACKEND_AUTOMATIC; \ @@ -1157,13 +1160,13 @@ mymain(void) ret = -1; \ } while (0) -# define RUN_TEST(name, method) \ +# define RUN_TEST(name, method) \ RUN_TEST_DIRECT(name, method); \ RUN_TEST_FIREWALLD(name, method) -#else /* ! WITH_DBUS */ -# define RUN_TEST(name, method) \ +# else /* ! WITH_DBUS */ +# define RUN_TEST(name, method) \ RUN_TEST_DIRECT(name, method) -#endif /* ! WITH_DBUS */ +# endif /* ! WITH_DBUS */ RUN_TEST("single group", testFirewallSingleGroup); RUN_TEST("remove rule", testFirewallRemoveRule); @@ -1179,8 +1182,17 @@ mymain(void) return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } -#if WITH_DBUS +# if WITH_DBUS VIRT_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/virmockdbus.so") -#else +# else VIRT_TEST_MAIN(mymain) -#endif +# endif + +#else /* ! defined (__linux__) */ + +int main(void) +{ + return EXIT_AM_SKIP; +} + +#endif /* ! defined(__linux__) */ diff --git a/tests/virstringtest.c b/tests/virstringtest.c index a4ae96601..1e330f959 100644 --- a/tests/virstringtest.c +++ b/tests/virstringtest.c @@ -23,6 +23,8 @@ #include <stdlib.h> #include "testutils.h" +#include "intprops.h" +#include "verify.h" #include "virerror.h" #include "viralloc.h" #include "virfile.h" @@ -286,7 +288,7 @@ struct stringSearchData { }; static int -testStringSearch(const void *opaque ATTRIBUTE_UNUSED) +testStringSearch(const void *opaque) { const struct stringSearchData *data = opaque; char **matches = NULL; @@ -373,6 +375,116 @@ testStringReplace(const void *opaque ATTRIBUTE_UNUSED) } +struct stringToLongData { + const char *str; + const char *suffix; + int si; /* syntax-check doesn't like bare 'i' */ + int si_ret; + unsigned int ui; + int ui_ret; + /* No expected results for long: on 32-bit platforms, it is the + * same as int, on 64-bit platforms it is the same as long long */ + long long ll; + int ll_ret; + unsigned long long ull; + int ull_ret; +}; + +/* This test makes assumptions about our compilation platform that are + * not guaranteed by POSIX. Good luck to you if you are crazy enough + * to try and port libvirt to a platform with 16-bit int. */ +verify(sizeof(int) == 4); +verify(TYPE_TWOS_COMPLEMENT(int)); +verify(sizeof(long) == sizeof(int) || sizeof(long) == sizeof(long long)); +verify(TYPE_TWOS_COMPLEMENT(long)); +verify(sizeof(long long) == 8); +verify(TYPE_TWOS_COMPLEMENT(long long)); + +static int +testStringToLong(const void *opaque) +{ + const struct stringToLongData *data = opaque; + int ret = 0; + char *end; + long l; + unsigned long ul; + bool negative; + + if (data->suffix) + negative = !!memchr(data->str, '-', + strlen(data->str) - strlen(data->suffix)); + else + negative = !!strchr(data->str, '-'); + +#define TEST_ONE(Str, Suff, Type, Fn, Fmt, Exp, Exp_ret) \ + do { \ + Type value = 5; \ + int result; \ + end = (char *) "oops"; \ + result = virStrToLong_ ## Fn(Str, Suff ? &end : NULL, \ + 0, &value); \ + /* On failure, end is modified, value is unchanged */ \ + if (result != (Exp_ret)) { \ + fprintf(stderr, \ + "type " #Fn " returned %d expected %d\n", \ + result, Exp_ret); \ + ret = -1; \ + } \ + if (value != ((Exp_ret) ? 5 : Exp)) { \ + fprintf(stderr, \ + "type " #Fn " value " Fmt " expected " Fmt "\n", \ + value, ((Exp_ret) ? 5 : Exp)); \ + ret = -1; \ + } \ + if (Suff && STRNEQ_NULLABLE(Suff, end)) { \ + fprintf(stderr, \ + "type " #Fn " end '%s' expected '%s'\n", \ + NULLSTR(end), Suff); \ + ret = -1; \ + } \ + } while (0) + + TEST_ONE(data->str, data->suffix, int, i, "%d", + data->si, data->si_ret); + TEST_ONE(data->str, data->suffix, unsigned int, ui, "%u", + data->ui, data->ui_ret); + if (negative) + TEST_ONE(data->str, data->suffix, unsigned int, uip, "%u", 0U, -1); + else + TEST_ONE(data->str, data->suffix, unsigned int, uip, "%u", + data->ui, data->ui_ret); + + /* We hate adding new API with 'long', and prefer 'int' or 'long + * long' instead, since platform-specific results are evil */ + l = (sizeof(int) == sizeof(long)) ? data->si : data->ll; + TEST_ONE(data->str, data->suffix, long, l, "%ld", + l, (sizeof(int) == sizeof(long)) ? data->si_ret : data->ll_ret); + ul = (sizeof(int) == sizeof(long)) ? data->ui : data->ull; + TEST_ONE(data->str, data->suffix, unsigned long, ul, "%lu", + ul, (sizeof(int) == sizeof(long)) ? data->ui_ret : data->ull_ret); + if (negative) + TEST_ONE(data->str, data->suffix, unsigned long, ulp, "%lu", 0UL, -1); + else + TEST_ONE(data->str, data->suffix, unsigned long, ulp, "%lu", ul, + (sizeof(int) == sizeof(long)) ? data->ui_ret : data->ull_ret); + + TEST_ONE(data->str, data->suffix, long long, ll, "%lld", + data->ll, data->ll_ret); + TEST_ONE(data->str, data->suffix, unsigned long long, ull, "%llu", + data->ull, data->ull_ret); + if (negative) + TEST_ONE(data->str, data->suffix, unsigned long long, ullp, "%llu", + 0ULL, -1); + else + TEST_ONE(data->str, data->suffix, unsigned long long, ullp, "%llu", + data->ull, data->ull_ret); + +#undef TEST_ONE + + return ret; +} + + static int mymain(void) { @@ -493,6 +605,92 @@ mymain(void) TEST_REPLACE("fooooofoooo", "foo", "barwizzeek", "barwizzeekooobarwizzeekoo"); TEST_REPLACE("fooooofoooo", "foooo", "foo", "fooofoo"); +#define TEST_STRTOL(str, suff, i, i_ret, u, u_ret, \ + ll, ll_ret, ull, ull_ret) \ + do { \ + struct stringToLongData data = { \ + str, suff, i, i_ret, u, u_ret, ll, ll_ret, ull, ull_ret, \ + }; \ + if (virtTestRun("virStringToLong '" str "'", testStringToLong, \ + &data) < 0) \ + ret = -1; \ + } while (0) + + /* Start simple */ + TEST_STRTOL("0", NULL, 0, 0, 0U, 0, 0LL, 0, 0ULL, 0); + + /* All your base are belong to us */ + TEST_STRTOL("0x0", NULL, 0, 0, 0U, 0, 0LL, 0, 0ULL, 0); + TEST_STRTOL("0XaB", NULL, 171, 0, 171U, 0, 171LL, 0, 171ULL, 0); + TEST_STRTOL("010", NULL, 8, 0, 8U, 0, 8LL, 0, 8ULL, 0); + + /* Suffix handling */ + TEST_STRTOL("42", NULL, 42, 0, 42U, 0, 42LL, 0, 42ULL, 0); + TEST_STRTOL("42", "", 42, 0, 42U, 0, 42LL, 0, 42ULL, 0); + TEST_STRTOL("42.", NULL, 0, -1, 0U, -1, 0LL, -1, 0ULL, -1); + TEST_STRTOL("42.", ".", 42, 0, 42U, 0, 42LL, 0, 42ULL, 0); + + /* Blatant invalid input */ + TEST_STRTOL("", "", 0, -1, 0U, -1, 0LL, -1, 0ULL, -1); + TEST_STRTOL("", NULL, 0, -1, 0U, -1, 0LL, -1, 0ULL, -1); + TEST_STRTOL(" ", " ", 0, -1, 0U, -1, 0LL, -1, 0ULL, -1); + TEST_STRTOL(" ", NULL, 0, -1, 0U, -1, 0LL, -1, 0ULL, -1); + TEST_STRTOL(" -", " -", 0, -1, 0U, -1, 0LL, -1, 0ULL, -1); + TEST_STRTOL(" -", NULL, 0, -1, 0U, -1, 0LL, -1, 0ULL, -1); + TEST_STRTOL("a", "a", 0, -1, 0U, -1, 0LL, -1, 0ULL, -1); + TEST_STRTOL("a", NULL, 0, -1, 0U, -1, 0LL, -1, 0ULL, -1); + + /* Not a hex number, but valid when suffix expected */ + TEST_STRTOL(" 0x", NULL, 0, -1, 0U, -1, 0LL, -1, 0ULL, -1); + TEST_STRTOL(" 0x", "x", 0, 0, 0U, 0, 0LL, 0, 0ULL, 0); + + /* Upper bounds */ + TEST_STRTOL("2147483647", NULL, 2147483647, 0, 2147483647U, 0, + 2147483647LL, 0, 2147483647ULL, 0); + TEST_STRTOL("2147483648", NULL, 0, -1, 2147483648U, 0, + 2147483648LL, 0, 2147483648ULL, 0); + TEST_STRTOL("4294967295", NULL, 0, -1, 4294967295U, 0, + 4294967295LL, 0, 4294967295ULL, 0); + TEST_STRTOL("4294967296", NULL, 0, -1, 0U, -1, + 4294967296LL, 0, 4294967296ULL, 0); + TEST_STRTOL("9223372036854775807", NULL, 0, -1, 0U, -1, + 9223372036854775807LL, 0, 9223372036854775807ULL, 0); + TEST_STRTOL("9223372036854775808", NULL, 0, -1, 0U, -1, + 0LL, -1, 9223372036854775808ULL, 0); + TEST_STRTOL("18446744073709551615", NULL, 0, -1, 0U, -1, + 0LL, -1, 18446744073709551615ULL, 0); + TEST_STRTOL("18446744073709551616", NULL, 0, -1, 0U, -1, + 0LL, -1, 0ULL, -1); + TEST_STRTOL("18446744073709551616", "", 0, -1, 0U, -1, + 0LL, -1, 0ULL, -1); + + /* Negative bounds */ + TEST_STRTOL("-0", NULL, 0, 0, 0U, 0, 0LL, 0, 0ULL, 0); + TEST_STRTOL("-1", "", -1, 0, 4294967295U, 0, + -1LL, 0, 18446744073709551615ULL, 0); + TEST_STRTOL("-2147483647", NULL, -2147483647, 0, 2147483649U, 0, + -2147483647LL, 0, 18446744071562067969ULL, 0); + TEST_STRTOL("-2147483648", NULL, -2147483648, 0, 2147483648U, 0, + -2147483648LL, 0, 18446744071562067968ULL, 0); + TEST_STRTOL("-2147483649", NULL, 0, -1, 2147483647U, 0, + -2147483649LL, 0, 18446744071562067967ULL, 0); + TEST_STRTOL("-4294967295", NULL, 0, -1, 1U, 0, + -4294967295LL, 0, 18446744069414584321ULL, 0); + TEST_STRTOL("-4294967296", NULL, 0, -1, 0U, -1, + -4294967296LL, 0, 18446744069414584320ULL, 0); + TEST_STRTOL("-9223372036854775807", NULL, 0, -1, 0U, -1, + -9223372036854775807LL, 0, 9223372036854775809ULL, 0); + /* Bah, stupid gcc warning about -9223372036854775808LL being an + * unrepresentable integer constant */ + TEST_STRTOL("-9223372036854775808", NULL, 0, -1, 0U, -1, + 0x8000000000000000LL, 0, 9223372036854775808ULL, 0); + TEST_STRTOL("-9223372036854775809", NULL, 0, -1, 0U, -1, + 0LL, -1, 9223372036854775807ULL, 0); + TEST_STRTOL("-18446744073709551615", NULL, 0, -1, 0U, -1, + 0LL, -1, 1ULL, 0); + TEST_STRTOL("-18446744073709551616", NULL, 0, -1, 0U, -1, + 0LL, -1, 0ULL, -1); + return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } |