networkxml2xmltest.c 4.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
#include <config.h>

#include <unistd.h>

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

#include "internal.h"
#include "testutils.h"
#include "network_conf.h"
#include "testutilsqemu.h"
12
#include "virstring.h"
13

14 15
#define VIR_FROM_THIS VIR_FROM_NONE

16 17 18 19 20 21 22
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;

23
static int
24
testCompareXMLToXMLFiles(const char *inxml, const char *outxml,
25 26
                         unsigned int flags,
                         testCompareNetXML2XMLResult expectResult)
27
{
28
    char *actual = NULL;
29
    int ret;
A
Andrea Bolognani 已提交
30
    testCompareNetXML2XMLResult result = TEST_COMPARE_NET_XML2XML_RESULT_SUCCESS;
31 32
    virNetworkDefPtr dev = NULL;

33 34 35 36 37 38
    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;
39

40 41 42 43 44 45
    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;
46

47
    if (virTestCompareToFile(actual, outxml) < 0) {
48 49 50 51 52
        result = TEST_COMPARE_NET_XML2XML_RESULT_FAIL_COMPARE;
        goto cleanup;
    }
    if (expectResult == TEST_COMPARE_NET_XML2XML_RESULT_FAIL_COMPARE)
        goto cleanup;
53

54 55 56 57 58 59 60 61 62 63 64 65 66
 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();
67

68
    VIR_FREE(actual);
69 70 71 72
    virNetworkDefFree(dev);
    return ret;
}

73 74 75
struct testInfo {
    const char *name;
    unsigned int flags;
76
    testCompareNetXML2XMLResult expectResult;
77 78
};

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

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

94 95
    result = testCompareXMLToXMLFiles(inxml, outxml, info->flags,
                                      info->expectResult);
96

97
 cleanup:
98 99
    VIR_FREE(inxml);
    VIR_FREE(outxml);
100 101 102

    return result;
}
103 104

static int
E
Eric Blake 已提交
105
mymain(void)
106 107 108
{
    int ret = 0;

109 110 111 112 113 114
#define DO_TEST_FULL(name, flags, expectResult) \
    do { \
        const struct testInfo info = {name, flags, expectResult}; \
        if (virTestRun("Network XML-2-XML " name, \
                       testCompareXMLToXMLHelper, &info) < 0) \
            ret = -1; \
115
    } while (0)
116 117 118 119 120 121
#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)
122

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

161
    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
162 163
}

164
VIR_TEST_MAIN(mymain)