networkxml2conftest.c 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <sys/types.h>
#include <fcntl.h>

#include "internal.h"
#include "testutils.h"
#include "network_conf.h"
14
#include "vircommand.h"
15
#include "viralloc.h"
16
#include "network/bridge_driver.h"
17
#include "virstring.h"
18

19 20
#define VIR_FROM_THIS VIR_FROM_NONE

21
static int
22
testCompareXMLToConfFiles(const char *inxml, const char *outconf, dnsmasqCapsPtr caps)
23
{
24
    char *inXmlData = NULL;
25
    char *outConfData = NULL;
26 27 28 29 30 31
    char *actual = NULL;
    int ret = -1;
    virNetworkDefPtr dev = NULL;
    virNetworkObjPtr obj = NULL;
    virCommandPtr cmd = NULL;
    char *pidfile = NULL;
32
    dnsmasqContext *dctx = NULL;
33 34 35 36

    if (virtTestLoadFile(inxml, &inXmlData) < 0)
        goto fail;

37
    if (virtTestLoadFile(outconf, &outConfData) < 0)
38 39
        goto fail;

40 41 42 43 44 45 46
    if (!(dev = virNetworkDefParseString(inXmlData)))
        goto fail;

    if (VIR_ALLOC(obj) < 0)
        goto fail;

    obj->def = dev;
47
    dctx = dnsmasqContextNew(dev->name, "/var/lib/libvirt/dnsmasq");
48

49 50 51
    if (dctx == NULL)
        goto fail;

52 53
    if (networkDnsmasqConfContents(obj, pidfile, &actual,
                        dctx, caps) < 0)
54 55
        goto fail;

56 57
    if (STRNEQ(outConfData, actual)) {
        virtTestDifference(stderr, outConfData, actual);
58 59 60 61 62 63
        goto fail;
    }

    ret = 0;

 fail:
64
    VIR_FREE(inXmlData);
65
    VIR_FREE(outConfData);
66
    VIR_FREE(actual);
67 68 69
    VIR_FREE(pidfile);
    virCommandFree(cmd);
    virNetworkObjFree(obj);
70
    dnsmasqContextFree(dctx);
71 72 73
    return ret;
}

74 75 76 77 78
typedef struct {
    const char *name;
    dnsmasqCapsPtr caps;
} testInfo;

79
static int
80
testCompareXMLToConfHelper(const void *data)
81 82
{
    int result = -1;
83
    const testInfo *info = data;
84 85 86
    char *inxml = NULL;
    char *outxml = NULL;

87
    if (virAsprintf(&inxml, "%s/networkxml2confdata/%s.xml",
88
                    abs_srcdir, info->name) < 0 ||
89
        virAsprintf(&outxml, "%s/networkxml2confdata/%s.conf",
90
                    abs_srcdir, info->name) < 0) {
91 92 93
        goto cleanup;
    }

94
    result = testCompareXMLToConfFiles(inxml, outxml, info->caps);
95

96
 cleanup:
97 98
    VIR_FREE(inxml);
    VIR_FREE(outxml);
99 100 101 102 103 104 105 106

    return result;
}

static int
mymain(void)
{
    int ret = 0;
107 108 109 110
    dnsmasqCapsPtr restricted
        = dnsmasqCapsNewFromBuffer("Dnsmasq version 2.48", DNSMASQ);
    dnsmasqCapsPtr full
        = dnsmasqCapsNewFromBuffer("Dnsmasq version 2.63\n--bind-dynamic", DNSMASQ);
G
Gene Czarcinski 已提交
111 112
    dnsmasqCapsPtr dhcpv6
        = dnsmasqCapsNewFromBuffer("Dnsmasq version 2.64\n--bind-dynamic", DNSMASQ);
113

114 115 116 117 118 119
#define DO_TEST(xname, xcaps)                                        \
    do {                                                             \
        static testInfo info;                                        \
                                                                     \
        info.name = xname;                                           \
        info.caps = xcaps;                                           \
120
        if (virtTestRun("Network XML-2-Conf " xname,                 \
121
                        testCompareXMLToConfHelper, &info) < 0) {    \
122 123 124 125 126 127 128 129 130
            ret = -1;                                                \
        }                                                            \
    } while (0)

    DO_TEST("isolated-network", restricted);
    DO_TEST("netboot-network", restricted);
    DO_TEST("netboot-proxy-network", restricted);
    DO_TEST("nat-network-dns-srv-record-minimal", restricted);
    DO_TEST("routed-network", full);
G
Gene Czarcinski 已提交
131
    DO_TEST("nat-network", dhcpv6);
132 133 134
    DO_TEST("nat-network-dns-txt-record", full);
    DO_TEST("nat-network-dns-srv-record", full);
    DO_TEST("nat-network-dns-hosts", full);
135
    DO_TEST("nat-network-dns-forward-plain", full);
136
    DO_TEST("nat-network-dns-forwarders", full);
137
    DO_TEST("nat-network-dns-local-domain", full);
G
Gene Czarcinski 已提交
138 139 140
    DO_TEST("dhcp6-network", dhcpv6);
    DO_TEST("dhcp6-nat-network", dhcpv6);
    DO_TEST("dhcp6host-routed-network", dhcpv6);
141

142
    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
143 144 145
}

VIRT_TEST_MAIN(mymain)