|
| 1 | +/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 | + |
| 3 | +#include <linux/in.h> |
| 4 | +#include <sys/socket.h> |
| 5 | +#include <stdio.h> |
| 6 | + |
| 7 | +#include "macro.h" |
| 8 | +#include "parse-socket-bind-item.h" |
| 9 | + |
| 10 | +static void test_valid_item( |
| 11 | + const char *str, |
| 12 | + int expected_af, |
| 13 | + int expected_ip_protocol, |
| 14 | + uint16_t expected_nr_ports, |
| 15 | + uint16_t expected_port_min) { |
| 16 | + uint16_t nr_ports, port_min; |
| 17 | + int af, ip_protocol; |
| 18 | + |
| 19 | + assert_se(parse_socket_bind_item(str, &af, &ip_protocol, &nr_ports, &port_min) >= 0); |
| 20 | + assert_se(af == expected_af); |
| 21 | + assert_se(ip_protocol == expected_ip_protocol); |
| 22 | + assert_se(nr_ports == expected_nr_ports); |
| 23 | + assert_se(port_min == expected_port_min); |
| 24 | + |
| 25 | + log_info("%s: \"%s\" ok", __func__, str); |
| 26 | +} |
| 27 | + |
| 28 | +static void test_invalid_item(const char *str) { |
| 29 | + uint16_t nr_ports, port_min; |
| 30 | + int af, ip_protocol; |
| 31 | + |
| 32 | + assert_se(parse_socket_bind_item(str, &af, &ip_protocol, &nr_ports, &port_min) == -EINVAL); |
| 33 | + |
| 34 | + log_info("%s: \"%s\" ok", __func__, str); |
| 35 | +} |
| 36 | + |
| 37 | +int main(int argc, char *argv[]) { |
| 38 | + test_valid_item("any", AF_UNSPEC, 0, 0, 0); |
| 39 | + test_valid_item("ipv4", AF_INET, 0, 0, 0); |
| 40 | + test_valid_item("ipv6", AF_INET6, 0, 0, 0); |
| 41 | + test_valid_item("ipv4:any", AF_INET, 0, 0, 0); |
| 42 | + test_valid_item("ipv6:any", AF_INET6, 0, 0, 0); |
| 43 | + test_valid_item("tcp", AF_UNSPEC, IPPROTO_TCP, 0, 0); |
| 44 | + test_valid_item("udp", AF_UNSPEC, IPPROTO_UDP, 0, 0); |
| 45 | + test_valid_item("tcp:any", AF_UNSPEC, IPPROTO_TCP, 0, 0); |
| 46 | + test_valid_item("udp:any", AF_UNSPEC, IPPROTO_UDP, 0, 0); |
| 47 | + test_valid_item("6666", AF_UNSPEC, 0, 1, 6666); |
| 48 | + test_valid_item("6666-6667", AF_UNSPEC, 0, 2, 6666); |
| 49 | + test_valid_item("65535", AF_UNSPEC, 0, 1, 65535); |
| 50 | + test_valid_item("1-65535", AF_UNSPEC, 0, 65535, 1); |
| 51 | + test_valid_item("ipv4:tcp", AF_INET, IPPROTO_TCP, 0, 0); |
| 52 | + test_valid_item("ipv4:udp", AF_INET, IPPROTO_UDP, 0, 0); |
| 53 | + test_valid_item("ipv6:tcp", AF_INET6, IPPROTO_TCP, 0, 0); |
| 54 | + test_valid_item("ipv6:udp", AF_INET6, IPPROTO_UDP, 0, 0); |
| 55 | + test_valid_item("ipv4:6666", AF_INET, 0, 1, 6666); |
| 56 | + test_valid_item("ipv6:6666", AF_INET6, 0, 1, 6666); |
| 57 | + test_valid_item("tcp:6666", AF_UNSPEC, IPPROTO_TCP, 1, 6666); |
| 58 | + test_valid_item("udp:6666", AF_UNSPEC, IPPROTO_UDP, 1, 6666); |
| 59 | + test_valid_item("ipv4:tcp:6666", AF_INET, IPPROTO_TCP, 1, 6666); |
| 60 | + test_valid_item("ipv6:tcp:6666", AF_INET6, IPPROTO_TCP, 1, 6666); |
| 61 | + test_valid_item("ipv6:udp:6666-6667", AF_INET6, IPPROTO_UDP, 2, 6666); |
| 62 | + test_valid_item("ipv6:tcp:any", AF_INET6, IPPROTO_TCP, 0, 0); |
| 63 | + |
| 64 | + test_invalid_item(""); |
| 65 | + test_invalid_item(":"); |
| 66 | + test_invalid_item("::"); |
| 67 | + test_invalid_item("any:"); |
| 68 | + test_invalid_item("meh"); |
| 69 | + test_invalid_item("zupa:meh"); |
| 70 | + test_invalid_item("zupa:meh:eh"); |
| 71 | + test_invalid_item("ip"); |
| 72 | + test_invalid_item("dccp"); |
| 73 | + test_invalid_item("ipv6meh"); |
| 74 | + test_invalid_item("ipv6::"); |
| 75 | + test_invalid_item("ipv6:ipv6"); |
| 76 | + test_invalid_item("ipv6:icmp"); |
| 77 | + test_invalid_item("ipv6:tcp:0"); |
| 78 | + test_invalid_item("65536"); |
| 79 | + test_invalid_item("0-65535"); |
| 80 | + test_invalid_item("ipv6:tcp:6666-6665"); |
| 81 | + test_invalid_item("ipv6:tcp:6666-100000"); |
| 82 | + test_invalid_item("ipv6::6666"); |
| 83 | + test_invalid_item("ipv6:tcp:any:"); |
| 84 | + test_invalid_item("ipv6:tcp:any:ipv6"); |
| 85 | + test_invalid_item("ipv6:tcp:6666:zupa"); |
| 86 | + test_invalid_item("ipv6:tcp:6666:any"); |
| 87 | + test_invalid_item("ipv6:tcp:6666 zupa"); |
| 88 | + test_invalid_item("ipv6:tcp:6666: zupa"); |
| 89 | + test_invalid_item("ipv6:tcp:6666\n zupa"); |
| 90 | + return 0; |
| 91 | +} |
0 commit comments