On 01/30/2012 10:13 AM, Philipp Hahn wrote:
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.)
v2:
- Use VIR_REALLOC_N() instead of realloc()
- Use memcpy() for replacement
- Skip shrinking buffer
- Add missing update of token_end
- simplify code after the changes above
- 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");
Nice. If we ever need to generalize this into the common testutils.c
for sharing with other tests, then we should probably also make that a
formal unit test at that time.
TEST("<AAAA", "AA", "BBB",
"<BBBBBB");
alarm(1); /* no infinit loop */
s/infinit/infinite/
+ 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 shrinkt the buffer here... */
s/shrinkt/shrink/
ACK and pushed.
--
Eric Blake eblake(a)redhat.com +1-919-301-3266
Libvirt virtualization library
http://libvirt.org