nwfilterebiptablestest.c 6.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
/*
 * nwfilterebiptablestest.c: Test {eb,ip,ip6}tables rule generation
 *
 * Copyright (C) 2014 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 */

#include <config.h>

#include "testutils.h"
#include "nwfilter/nwfilter_ebiptables_driver.h"
#include "virbuffer.h"

#define __VIR_FIREWALL_PRIV_H_ALLOW__
#include "virfirewallpriv.h"

#define __VIR_COMMAND_PRIV_H_ALLOW__
#include "vircommandpriv.h"

#define VIR_FROM_THIS VIR_FROM_NONE

static int
testNWFilterEBIPTablesAllTeardown(const void *opaque ATTRIBUTE_UNUSED)
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    const char *expected =
        "iptables -D libvirt-out -m physdev --physdev-is-bridged --physdev-out vnet0 -g FO-vnet0\n"
        "iptables -D libvirt-out -m physdev --physdev-out vnet0 -g FO-vnet0\n"
        "iptables -D libvirt-in -m physdev --physdev-in vnet0 -g FI-vnet0\n"
        "iptables -D libvirt-host-in -m physdev --physdev-in vnet0 -g HI-vnet0\n"
        "iptables -D libvirt-in-post -m physdev --physdev-in vnet0 -j ACCEPT\n"
        "iptables -F FO-vnet0\n"
        "iptables -X FO-vnet0\n"
        "iptables -F FI-vnet0\n"
        "iptables -X FI-vnet0\n"
        "iptables -F HI-vnet0\n"
        "iptables -X HI-vnet0\n"
        "ip6tables -D libvirt-out -m physdev --physdev-is-bridged --physdev-out vnet0 -g FO-vnet0\n"
        "ip6tables -D libvirt-out -m physdev --physdev-out vnet0 -g FO-vnet0\n"
        "ip6tables -D libvirt-in -m physdev --physdev-in vnet0 -g FI-vnet0\n"
        "ip6tables -D libvirt-host-in -m physdev --physdev-in vnet0 -g HI-vnet0\n"
        "ip6tables -D libvirt-in-post -m physdev --physdev-in vnet0 -j ACCEPT\n"
        "ip6tables -F FO-vnet0\n"
        "ip6tables -X FO-vnet0\n"
        "ip6tables -F FI-vnet0\n"
        "ip6tables -X FI-vnet0\n"
        "ip6tables -F HI-vnet0\n"
        "ip6tables -X HI-vnet0\n"
        "ebtables -t nat -D PREROUTING -i vnet0 -j libvirt-I-vnet0\n"
        "ebtables -t nat -D POSTROUTING -o vnet0 -j libvirt-O-vnet0\n"
        "ebtables -t nat -L libvirt-I-vnet0\n"
        "ebtables -t nat -L libvirt-O-vnet0\n"
        "ebtables -t nat -F libvirt-I-vnet0\n"
        "ebtables -t nat -X libvirt-I-vnet0\n"
        "ebtables -t nat -F libvirt-O-vnet0\n"
        "ebtables -t nat -X libvirt-O-vnet0\n";
    char *actual = NULL;
    int ret = -1;

    virCommandSetDryRun(&buf, NULL, NULL);

    if (ebiptables_driver.allTeardown("vnet0") < 0)
        goto cleanup;

    if (virBufferError(&buf))
        goto cleanup;

    actual = virBufferContentAndReset(&buf);
    virtTestClearCommandPath(actual);

    if (STRNEQ_NULLABLE(actual, expected)) {
        virtTestDifference(stderr, actual, expected);
        goto cleanup;
    }

    ret = 0;
 cleanup:
    virCommandSetDryRun(NULL, NULL, NULL);
    virBufferFreeAndReset(&buf);
    VIR_FREE(actual);
    return ret;
}


99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
static int
testNWFilterEBIPTablesTearOldRules(const void *opaque ATTRIBUTE_UNUSED)
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    const char *expected =
        "iptables -D libvirt-out -m physdev --physdev-is-bridged --physdev-out vnet0 -g FO-vnet0\n"
        "iptables -D libvirt-out -m physdev --physdev-out vnet0 -g FO-vnet0\n"
        "iptables -D libvirt-in -m physdev --physdev-in vnet0 -g FI-vnet0\n"
        "iptables -D libvirt-host-in -m physdev --physdev-in vnet0 -g HI-vnet0\n"
        "iptables -F FO-vnet0\n"
        "iptables -X FO-vnet0\n"
        "iptables -F FI-vnet0\n"
        "iptables -X FI-vnet0\n"
        "iptables -F HI-vnet0\n"
        "iptables -X HI-vnet0\n"
        "iptables -E FP-vnet0 FO-vnet0\n"
        "iptables -E FJ-vnet0 FI-vnet0\n"
        "iptables -E HJ-vnet0 HI-vnet0\n"
        "ip6tables -D libvirt-out -m physdev --physdev-is-bridged --physdev-out vnet0 -g FO-vnet0\n"
        "ip6tables -D libvirt-out -m physdev --physdev-out vnet0 -g FO-vnet0\n"
        "ip6tables -D libvirt-in -m physdev --physdev-in vnet0 -g FI-vnet0\n"
        "ip6tables -D libvirt-host-in -m physdev --physdev-in vnet0 -g HI-vnet0\n"
        "ip6tables -F FO-vnet0\n"
        "ip6tables -X FO-vnet0\n"
        "ip6tables -F FI-vnet0\n"
        "ip6tables -X FI-vnet0\n"
        "ip6tables -F HI-vnet0\n"
        "ip6tables -X HI-vnet0\n"
        "ip6tables -E FP-vnet0 FO-vnet0\n"
        "ip6tables -E FJ-vnet0 FI-vnet0\n"
        "ip6tables -E HJ-vnet0 HI-vnet0\n"
        "ebtables -t nat -D PREROUTING -i vnet0 -j libvirt-I-vnet0\n"
        "ebtables -t nat -D POSTROUTING -o vnet0 -j libvirt-O-vnet0\n"
        "ebtables -t nat -L libvirt-I-vnet0\n"
        "ebtables -t nat -L libvirt-O-vnet0\n"
        "ebtables -t nat -F libvirt-I-vnet0\n"
        "ebtables -t nat -X libvirt-I-vnet0\n"
        "ebtables -t nat -F libvirt-O-vnet0\n"
        "ebtables -t nat -X libvirt-O-vnet0\n"
        "ebtables -t nat -L libvirt-J-vnet0\n"
        "ebtables -t nat -L libvirt-P-vnet0\n"
        "ebtables -t nat -E libvirt-J-vnet0 libvirt-I-vnet0\n"
        "ebtables -t nat -E libvirt-P-vnet0 libvirt-O-vnet0\n";
    char *actual = NULL;
    int ret = -1;

    virCommandSetDryRun(&buf, NULL, NULL);

    if (ebiptables_driver.tearOldRules("vnet0") < 0)
        goto cleanup;

    if (virBufferError(&buf))
        goto cleanup;

    actual = virBufferContentAndReset(&buf);
    virtTestClearCommandPath(actual);

    if (STRNEQ_NULLABLE(actual, expected)) {
        virtTestDifference(stderr, actual, expected);
        goto cleanup;
    }

    ret = 0;
 cleanup:
    virCommandSetDryRun(NULL, NULL, NULL);
    virBufferFreeAndReset(&buf);
    VIR_FREE(actual);
    return ret;
}


170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
static int
mymain(void)
{
    int ret = 0;

    if (virFirewallSetBackend(VIR_FIREWALL_BACKEND_DIRECT) < 0) {
        ret = -1;
        goto cleanup;
    }

    if (virtTestRun("ebiptablesAllTeardown",
                    testNWFilterEBIPTablesAllTeardown,
                    NULL) < 0)
        ret = -1;

185 186 187 188 189
    if (virtTestRun("ebiptablesTearOldRules",
                    testNWFilterEBIPTablesTearOldRules,
                    NULL) < 0)
        ret = -1;

190 191 192 193 194
 cleanup:
    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

VIRT_TEST_MAIN(mymain)