aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Hahn <hahn@univention.de>2012-01-30 18:13:08 +0100
committerEric Blake <eblake@redhat.com>2012-02-01 17:02:45 -0700
commit22ec60001e9a41fbd529922551313970224aad94 (patch)
tree234520ec8ff752898e340ab5b89c118ef76942ec /tests/networkxml2argvtest.c
parentnetwork: fix testsuite regression (diff)
downloadlibvirt-22ec60001e9a41fbd529922551313970224aad94.tar.gz
libvirt-22ec60001e9a41fbd529922551313970224aad94.tar.bz2
libvirt-22ec60001e9a41fbd529922551313970224aad94.zip
tests: dynamically replace dnsmasq path
The path to the dnsmasq binary can be configured while in the test data the path is hard-coded to /usr/bin/. This break the test suite if a the binary is located in a different location, like /usr/local/sbin/. Replace the hard coded path in the test data by a token, which is dynamically replaced in networkxml2argvtest with the configured path after the test data has been loaded. (Another option would have been to modify configure.ac to generate the test data during configure, but I do not know of an easy way do trick configure into mass-generate those test files without listing every single one, which I consider less flexible.) - unit-test the unit-test: #include <assert.h> #define TEST(in,token,rep,out) { char *buf = strdup(in); assert(!replaceTokens(&buf, token, rep) && !strcmp(buf, out)); free(buf); } TEST("", "AA", "B", ""); TEST("A", "AA", "B", "A"); TEST("AA", "AA", "B", "B"); TEST("AAA", "AA", "B", "BA"); TEST("AA", "AA", "BB", "BB"); TEST("AA", "AA", "BBB", "BBB"); TEST("<AA", "AA", "B", "<B"); TEST("<AA", "AA", "BB", "<BB"); TEST("<AA", "AA", "BBB", "<BBB"); TEST("AA>", "AA", "B", "B>"); TEST("AA>", "AA", "BB", "BB>"); TEST("AA>", "AA", "BBB", "BBB>"); TEST("<AA>", "AA", "B", "<B>"); TEST("<AA>", "AA", "BB", "<BB>"); TEST("<AA>", "AA", "BBB", "<BBB>"); TEST("<AA|AA>", "AA", "B", "<B|B>"); TEST("<AA|AA>", "AA", "BB", "<BB|BB>"); TEST("<AA|AA>", "AA", "BBB", "<BBB|BBB>"); TEST("<AAAA>", "AA", "B", "<BB>"); TEST("<AAAA>", "AA", "BB", "<BBBB>"); TEST("<AAAA>", "AA", "BBB", "<BBBBBB>"); TEST("AAAA>", "AA", "B", "BB>"); TEST("AAAA>", "AA", "BB", "BBBB>"); TEST("AAAA>", "AA", "BBB", "BBBBBB>"); TEST("<AAAA", "AA", "B", "<BB"); TEST("<AAAA", "AA", "BB", "<BBBB"); TEST("<AAAA", "AA", "BBB", "<BBBBBB"); alarm(1); /* no infinite loop */ TEST("A", "A", "A", "A"); TEST("AA", "A", "A", "AA"); alarm(0); Signed-off-by: Philipp Hahn <hahn@univention.de>
Diffstat (limited to 'tests/networkxml2argvtest.c')
-rw-r--r--tests/networkxml2argvtest.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/tests/networkxml2argvtest.c b/tests/networkxml2argvtest.c
index 2dd9b7f14..5c6e29d2e 100644
--- a/tests/networkxml2argvtest.c
+++ b/tests/networkxml2argvtest.c
@@ -15,6 +15,36 @@
#include "memory.h"
#include "network/bridge_driver.h"
+/* Replace all occurrences of @token in @buf by @replacement and adjust size of
+ * @buf accordingly. Returns 0 on success and -1 on out-of-memory errors. */
+static int replaceTokens(char **buf, const char *token, const char *replacement) {
+ char *token_start, *token_end;
+ size_t buf_len, rest_len;
+ const size_t token_len = strlen(token);
+ const size_t replacement_len = strlen(replacement);
+ const int diff = replacement_len - token_len;
+
+ buf_len = rest_len = strlen(*buf) + 1;
+ token_end = *buf;
+ for (;;) {
+ token_start = strstr(token_end, token);
+ if (token_start == NULL)
+ break;
+ rest_len -= token_start + token_len - token_end;
+ token_end = token_start + token_len;
+ buf_len += diff;
+ if (diff > 0)
+ if (VIR_REALLOC_N(*buf, buf_len) < 0)
+ return -1;
+ if (diff != 0)
+ memmove(token_end + diff, token_end, rest_len);
+ memcpy(token_start, replacement, replacement_len);
+ token_end += diff;
+ }
+ /* if diff < 0, we could shrink the buffer here... */
+ return 0;
+}
+
static int testCompareXMLToArgvFiles(const char *inxml, const char *outargv) {
char *inXmlData = NULL;
char *outArgvData = NULL;
@@ -32,6 +62,9 @@ static int testCompareXMLToArgvFiles(const char *inxml, const char *outargv) {
if (virtTestLoadFile(outargv, &outArgvData) < 0)
goto fail;
+ if (replaceTokens(&outArgvData, "@DNSMASQ@", DNSMASQ))
+ goto fail;
+
if (!(dev = virNetworkDefParseString(inXmlData)))
goto fail;