Re: [PATCH] tests: dynamically replace dnsmasq path

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

Cool hack!

> 
> (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.)

Agreed with your reasoning and choice of implementation to do the
replacement at test run rather than configure time.  However,

> +++ b/tests/networkxml2argvtest.c
> @@ -15,6 +15,45 @@
>  #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 old_len, new_len, rest_len;
> +    void *tmp;
> +    const size_t token_len = strlen(token);
> +    const size_t replacement_len = strlen(replacement);
> +    const int diff = replacement_len - token_len;
> +
> +    old_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;
> +        new_len = old_len + diff;
> +        if (diff > 0) {
> +            tmp = realloc((void *)*buf, new_len);

We should not be using realloc in this file, but should be using
VIR_RESIZE_N or similar.

> +            if (tmp == NULL)
> +                return -1;
> +            *buf = tmp;
> +        }
> +        if (diff != 0)
> +            memmove(token_end + diff, token_end, rest_len);

This must be memmove,

> +        memmove(token_start, replacement, replacement_len);

But this would be more efficient as memcpy, since replacement (better
not) overlap with token_start.

> +        if (diff < 0) {
> +            tmp = realloc((void *)*buf, new_len);

No need to downsize the allocation - it's okay to leave junk in the tail
end of the buffer, as long as we have a trailing NUL to stop us in time.

-- 
Eric Blake   eblake@xxxxxxxxxx    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]