2 * networkxml2firewalltest.c: Test iptables rule generation
4 * Copyright (C) 2014 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see
18 * <http://www.gnu.org/licenses/>.
24 #include "testutils.h"
27 #if defined (__linux__)
29 # include "network/bridge_driver_platform.h"
30 # include "virbuffer.h"
32 # define LIBVIRT_VIRFIREWALLPRIV_H_ALLOW
33 # include "virfirewallpriv.h"
35 # define LIBVIRT_VIRCOMMANDPRIV_H_ALLOW
36 # include "vircommandpriv.h"
38 # define VIR_FROM_THIS VIR_FROM_NONE
41 # define RULESTYPE "linux"
43 # error "test case not ported to this platform"
47 testCommandDryRun(const char *const*args ATTRIBUTE_UNUSED
,
48 const char *const*env ATTRIBUTE_UNUSED
,
49 const char *input ATTRIBUTE_UNUSED
,
53 void *opaque ATTRIBUTE_UNUSED
)
56 ignore_value(VIR_STRDUP_QUIET(*output
, ""));
57 ignore_value(VIR_STRDUP_QUIET(*error
, ""));
60 static int testCompareXMLToArgvFiles(const char *xml
,
64 char *expectargv
= NULL
;
65 char *actualargv
= NULL
;
66 virBuffer buf
= VIR_BUFFER_INITIALIZER
;
67 virNetworkDefPtr def
= NULL
;
71 virCommandSetDryRun(&buf
, testCommandDryRun
, NULL
);
73 if (!(def
= virNetworkDefParseFile(xml
, NULL
)))
76 if (networkAddFirewallRules(def
) < 0)
79 if (virBufferError(&buf
))
82 actual
= actualargv
= virBufferContentAndReset(&buf
);
83 virTestClearCommandPath(actualargv
);
84 virCommandSetDryRun(NULL
, NULL
, NULL
);
86 /* The first network to be created populates the
87 * libvirt global chains. We must skip args for
90 if (STRPREFIX(actual
, baseargs
))
91 actual
+= strlen(baseargs
);
93 if (virTestCompareToFile(actual
, cmdline
) < 0)
99 virBufferFreeAndReset(&buf
);
100 VIR_FREE(expectargv
);
101 VIR_FREE(actualargv
);
102 virNetworkDefFree(def
);
108 const char *baseargs
;
113 testCompareXMLToIPTablesHelper(const void *data
)
116 const struct testInfo
*info
= data
;
120 if (virAsprintf(&xml
, "%s/networkxml2firewalldata/%s.xml",
121 abs_srcdir
, info
->name
) < 0 ||
122 virAsprintf(&args
, "%s/networkxml2firewalldata/%s-%s.args",
123 abs_srcdir
, info
->name
, RULESTYPE
) < 0)
126 result
= testCompareXMLToArgvFiles(xml
, args
, info
->baseargs
);
135 hasNetfilterTools(void)
137 return virFileIsExecutable(IPTABLES_PATH
) &&
138 virFileIsExecutable(IP6TABLES_PATH
) &&
139 virFileIsExecutable(EBTABLES_PATH
);
147 VIR_AUTOFREE(char *)basefile
= NULL
;
148 VIR_AUTOFREE(char *)baseargs
= NULL
;
150 # define DO_TEST(name) \
152 struct testInfo info = { \
155 if (virTestRun("Network XML-2-iptables " name, \
156 testCompareXMLToIPTablesHelper, &info) < 0) \
160 virFirewallSetLockOverride(true);
162 if (virFirewallSetBackend(VIR_FIREWALL_BACKEND_DIRECT
) < 0) {
163 if (!hasNetfilterTools()) {
164 fprintf(stderr
, "iptables/ip6tables/ebtables tools not present");
172 if (virAsprintf(&basefile
, "%s/networkxml2firewalldata/base.args",
178 if (virTestLoadFile(basefile
, &baseargs
) < 0) {
183 DO_TEST("nat-default");
185 DO_TEST("nat-many-ips");
186 DO_TEST("nat-no-dhcp");
188 DO_TEST("route-default");
191 return ret
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
194 VIR_TEST_MAIN(mymain
)
196 #else /* ! defined (__linux__) */
203 #endif /* ! defined (__linux__) */