networkxml2xmltest.c 4.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#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"
#include "testutilsqemu.h"
15
#include "virstring.h"
16

17 18
#define VIR_FROM_THIS VIR_FROM_NONE

19 20 21 22 23 24 25
typedef enum {
    TEST_COMPARE_NET_XML2XML_RESULT_SUCCESS,
    TEST_COMPARE_NET_XML2XML_RESULT_FAIL_PARSE,
    TEST_COMPARE_NET_XML2XML_RESULT_FAIL_FORMAT,
    TEST_COMPARE_NET_XML2XML_RESULT_FAIL_COMPARE,
} testCompareNetXML2XMLResult;

26
static int
27
testCompareXMLToXMLFiles(const char *inxml, const char *outxml,
28 29
                         unsigned int flags,
                         testCompareNetXML2XMLResult expectResult)
30
{
31
    char *actual = NULL;
32 33
    int ret;
    testCompareNetXML2XMLResult result = TEST_COMPARE_DOM_XML2XML_RESULT_SUCCESS;
34 35
    virNetworkDefPtr dev = NULL;

36 37 38 39 40 41
    if (!(dev = virNetworkDefParseFile(inxml))) {
        result = TEST_COMPARE_NET_XML2XML_RESULT_FAIL_PARSE;
        goto cleanup;
    }
    if (expectResult == TEST_COMPARE_NET_XML2XML_RESULT_FAIL_PARSE)
        goto cleanup;
42

43 44 45 46 47 48
    if (!(actual = virNetworkDefFormat(dev, flags))) {
        result = TEST_COMPARE_NET_XML2XML_RESULT_FAIL_FORMAT;
        goto cleanup;
    }
    if (expectResult == TEST_COMPARE_NET_XML2XML_RESULT_FAIL_FORMAT)
        goto cleanup;
49

50 51 52 53 54 55
    if (virtTestCompareToFile(actual, outxml) < 0) {
        result = TEST_COMPARE_NET_XML2XML_RESULT_FAIL_COMPARE;
        goto cleanup;
    }
    if (expectResult == TEST_COMPARE_NET_XML2XML_RESULT_FAIL_COMPARE)
        goto cleanup;
56

57 58 59 60 61 62 63 64 65 66 67 68 69
 cleanup:
    if (result == expectResult) {
        ret = 0;
        if (expectResult != TEST_COMPARE_NET_XML2XML_RESULT_SUCCESS) {
            VIR_TEST_DEBUG("Got expected failure code=%d msg=%s",
                           result, virGetLastErrorMessage());
        }
    } else {
        ret = -1;
        VIR_TEST_DEBUG("Expected result code=%d but received code=%d",
                       expectResult, result);
    }
    virResetLastError();
70

71
    VIR_FREE(actual);
72 73 74 75
    virNetworkDefFree(dev);
    return ret;
}

76 77 78
struct testInfo {
    const char *name;
    unsigned int flags;
79
    testCompareNetXML2XMLResult expectResult;
80 81
};

82 83 84
static int
testCompareXMLToXMLHelper(const void *data)
{
85
    const struct testInfo *info = data;
86 87 88 89 90
    int result = -1;
    char *inxml = NULL;
    char *outxml = NULL;

    if (virAsprintf(&inxml, "%s/networkxml2xmlin/%s.xml",
91
                    abs_srcdir, info->name) < 0 ||
92
        virAsprintf(&outxml, "%s/networkxml2xmlout/%s.xml",
93
                    abs_srcdir, info->name) < 0) {
94 95 96
        goto cleanup;
    }

97 98
    result = testCompareXMLToXMLFiles(inxml, outxml, info->flags,
                                      info->expectResult);
99

100
 cleanup:
101 102
    VIR_FREE(inxml);
    VIR_FREE(outxml);
103 104 105

    return result;
}
106 107

static int
E
Eric Blake 已提交
108
mymain(void)
109 110 111
{
    int ret = 0;

112
#define DO_TEST_FULL(name, flags, expectResult)                         \
113
    do {                                                                \
114
        const struct testInfo info = {name, flags, expectResult};       \
115
        if (virtTestRun("Network XML-2-XML " name,                      \
116
                        testCompareXMLToXMLHelper, &info) < 0) \
117 118
            ret = -1;                                                   \
    } while (0)
119 120 121 122 123 124
#define DO_TEST(name) \
    DO_TEST_FULL(name, 0, TEST_COMPARE_NET_XML2XML_RESULT_SUCCESS)
#define DO_TEST_FLAGS(name, flags) \
    DO_TEST_FULL(name, flags, TEST_COMPARE_NET_XML2XML_RESULT_SUCCESS)
#define DO_TEST_PARSE_ERROR(name) \
    DO_TEST_FULL(name, 0, TEST_COMPARE_NET_XML2XML_RESULT_FAIL_PARSE)
125

126
    DO_TEST("dhcp6host-routed-network");
127
    DO_TEST("empty-allow-ipv6");
128 129 130 131
    DO_TEST("isolated-network");
    DO_TEST("routed-network");
    DO_TEST("nat-network");
    DO_TEST("netboot-network");
132
    DO_TEST("netboot-proxy-network");
133
    DO_TEST("nat-network-dns-txt-record");
134
    DO_TEST("nat-network-dns-srv-record");
J
Ján Tomko 已提交
135
    DO_TEST("nat-network-dns-srv-records");
136
    DO_TEST("nat-network-dns-srv-record-minimal");
137
    DO_TEST("nat-network-dns-hosts");
138
    DO_TEST("nat-network-dns-forward-plain");
139
    DO_TEST("nat-network-dns-forwarders");
140
    DO_TEST("nat-network-forward-nat-address");
141
    DO_TEST("nat-network-forward-nat-no-address");
142 143 144 145
    DO_TEST("8021Qbh-net");
    DO_TEST("direct-net");
    DO_TEST("host-bridge-net");
    DO_TEST("vepa-net");
146
    DO_TEST("bandwidth-network");
147
    DO_TEST("openvswitch-net");
148
    DO_TEST_FLAGS("passthrough-pf", VIR_NETWORK_XML_INACTIVE);
149
    DO_TEST("hostdev");
150
    DO_TEST_FLAGS("hostdev-pf", VIR_NETWORK_XML_INACTIVE);
151
    DO_TEST("passthrough-address-crash");
152 153
    DO_TEST("nat-network-explicit-flood");
    DO_TEST("host-bridge-no-flood");
154 155
    DO_TEST_PARSE_ERROR("hostdev-duplicate");
    DO_TEST_PARSE_ERROR("passthrough-duplicate");
156

157
    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
158 159 160
}

VIRT_TEST_MAIN(mymain)