From 1079962e4c06f88a54e50d997c1b7e84303d30b4 Mon Sep 17 00:00:00 2001 From: Bernhard Schmidt Date: Sat, 15 Aug 2020 21:29:50 +0200 Subject: New upstream version 2.5~beta1 --- tests/unit_tests/openvpn/test_networking.c | 253 +++++++++++++++++++++++++++++ 1 file changed, 253 insertions(+) create mode 100644 tests/unit_tests/openvpn/test_networking.c (limited to 'tests/unit_tests/openvpn/test_networking.c') diff --git a/tests/unit_tests/openvpn/test_networking.c b/tests/unit_tests/openvpn/test_networking.c new file mode 100644 index 0000000..e7c148f --- /dev/null +++ b/tests/unit_tests/openvpn/test_networking.c @@ -0,0 +1,253 @@ +#include "config.h" +#include "syshead.h" +#include "networking.h" + + +static char *iface = "dummy0"; + +static int +net__iface_up(bool up) +{ + printf("CMD: ip link set %s %s\n", iface, up ? "up" : "down"); + + return net_iface_up(NULL, iface, up); +} + +static int +net__iface_mtu_set(int mtu) +{ + printf("CMD: ip link set %s mtu %d\n", iface, mtu); + + return net_iface_mtu_set(NULL, iface, mtu); +} + +static int +net__addr_v4_add(const char *addr_str, int prefixlen) +{ + in_addr_t addr; + int ret; + + ret = inet_pton(AF_INET, addr_str, &addr); + if (ret != 1) + { + return -1; + } + + addr = ntohl(addr); + + printf("CMD: ip addr add %s/%d dev %s\n", addr_str, prefixlen, iface); + + return net_addr_v4_add(NULL, iface, &addr, prefixlen); +} + +static int +net__addr_v6_add(const char *addr_str, int prefixlen) +{ + struct in6_addr addr; + int ret; + + ret = inet_pton(AF_INET6, addr_str, &addr); + if (ret != 1) + { + return -1; + } + + printf("CMD: ip -6 addr add %s/%d dev %s\n", addr_str, prefixlen, iface); + + return net_addr_v6_add(NULL, iface, &addr, prefixlen); +} + +static int +net__route_v4_add(const char *dst_str, int prefixlen, int metric) +{ + in_addr_t dst; + int ret; + + if (!dst_str) + { + return -1; + } + + ret = inet_pton(AF_INET, dst_str, &dst); + if (ret != 1) + { + return -1; + } + + dst = ntohl(dst); + + printf("CMD: ip route add %s/%d dev %s", dst_str, prefixlen, iface); + if (metric > 0) + { + printf(" metric %d", metric); + } + printf("\n"); + + return net_route_v4_add(NULL, &dst, prefixlen, NULL, iface, 0, metric); + +} + +static int +net__route_v4_add_gw(const char *dst_str, int prefixlen, const char *gw_str, + int metric) +{ + in_addr_t dst, gw; + int ret; + + if (!dst_str || !gw_str) + { + return -1; + } + + ret = inet_pton(AF_INET, dst_str, &dst); + if (ret != 1) + { + return -1; + } + + ret = inet_pton(AF_INET, gw_str, &gw); + if (ret != 1) + { + return -1; + } + + dst = ntohl(dst); + gw = ntohl(gw); + + printf("CMD: ip route add %s/%d dev %s via %s", dst_str, prefixlen, iface, + gw_str); + if (metric > 0) + { + printf(" metric %d", metric); + } + printf("\n"); + + return net_route_v4_add(NULL, &dst, prefixlen, &gw, iface, 0, metric); +} + +static int +net__route_v6_add(const char *dst_str, int prefixlen, int metric) +{ + struct in6_addr dst; + int ret; + + if (!dst_str) + { + return -1; + } + + ret = inet_pton(AF_INET6, dst_str, &dst); + if (ret != 1) + { + return -1; + } + + printf("CMD: ip -6 route add %s/%d dev %s", dst_str, prefixlen, iface); + if (metric > 0) + { + printf(" metric %d", metric); + } + printf("\n"); + + return net_route_v6_add(NULL, &dst, prefixlen, NULL, iface, 0, metric); + +} + +static int +net__route_v6_add_gw(const char *dst_str, int prefixlen, const char *gw_str, + int metric) +{ + struct in6_addr dst, gw; + int ret; + + if (!dst_str || !gw_str) + { + return -1; + } + + ret = inet_pton(AF_INET6, dst_str, &dst); + if (ret != 1) + { + return -1; + } + + ret = inet_pton(AF_INET6, gw_str, &gw); + if (ret != 1) + { + return -1; + } + + printf("CMD: ip -6 route add %s/%d dev %s via %s", dst_str, prefixlen, + iface, gw_str); + if (metric > 0) + { + printf(" metric %d", metric); + } + printf("\n"); + + return net_route_v6_add(NULL, &dst, prefixlen, &gw, iface, 0, metric); +} + +static void +usage(char *name) +{ + printf("Usage: %s <0-7>\n", name); +} + +int +main(int argc, char *argv[]) +{ + int test; + + if (argc < 2) + { + usage(argv[0]); + return -1; + } + + /* the t_net script can use this command to perform a dry-run test */ + if (strcmp(argv[1], "test") == 0) + { + return 0; + } + + if (argc > 3) + { + iface = argv[2]; + } + + test = atoi(argv[1]); + switch (test) + { + case 0: + return net__iface_up(true); + + case 1: + return net__iface_mtu_set(1281); + + case 2: + return net__addr_v4_add("10.255.255.1", 24); + + case 3: + return net__addr_v6_add("2001::1", 64); + + case 4: + return net__route_v4_add("11.11.11.0", 24, 0); + + case 5: + return net__route_v4_add_gw("11.11.12.0", 24, "10.255.255.2", 0); + + case 6: + return net__route_v6_add("2001:babe:cafe:babe::", 64, 600); + + case 7: + return net__route_v6_add_gw("2001:cafe:babe::", 48, "2001::2", 600); + + default: + printf("invalid test: %d\n", test); + break; + } + + usage(argv[0]); + return -1; +} -- cgit v1.2.3 From f2b3dda12a731c2e0971cb7889728edaf23f6cb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Mon, 29 Nov 2021 20:46:00 +0100 Subject: New upstream version 2.5.4 --- tests/unit_tests/openvpn/test_networking.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/unit_tests/openvpn/test_networking.c') diff --git a/tests/unit_tests/openvpn/test_networking.c b/tests/unit_tests/openvpn/test_networking.c index e7c148f..9e9744f 100644 --- a/tests/unit_tests/openvpn/test_networking.c +++ b/tests/unit_tests/openvpn/test_networking.c @@ -3,7 +3,7 @@ #include "networking.h" -static char *iface = "dummy0"; +static char *iface = "ovpn-dummy0"; static int net__iface_up(bool up) -- cgit v1.2.3