// Copyright (C) 2026 Viorel Munteanu // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program 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 General Public License for more details. // // You should have received a copy of the GNU General Public License along // with this program; if not, write to the Free Software Foundation, Inc., // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. // [unit] tests for parse_port_forward_spec() (port_forward.h) -- pure // syntax/range parsing, no networks/processes involved. #include #include "port_forward.h" TEST_CASE("parse_port_forward_spec: bare host:container defaults to tcp, no network", "[unit]") { auto spec = parse_port_forward_spec("8080:80"); REQUIRE(spec.has_value()); CHECK_FALSE(spec->network.has_value()); CHECK(spec->host_port == 8080); CHECK(spec->container_port == 80); CHECK(spec->protocol == PortForwardProtocol::tcp); } TEST_CASE("parse_port_forward_spec: network:host:container", "[unit]") { auto spec = parse_port_forward_spec("mynet:8080:80"); REQUIRE(spec.has_value()); REQUIRE(spec->network.has_value()); CHECK(*spec->network == "mynet"); CHECK(spec->host_port == 8080); CHECK(spec->container_port == 80); } TEST_CASE("parse_port_forward_spec: explicit /tcp suffix", "[unit]") { auto spec = parse_port_forward_spec("8080:80/tcp"); REQUIRE(spec.has_value()); CHECK(spec->container_port == 80); CHECK(spec->protocol == PortForwardProtocol::tcp); } TEST_CASE("parse_port_forward_spec: /udp suffix", "[unit]") { auto spec = parse_port_forward_spec("8080:80/udp"); REQUIRE(spec.has_value()); CHECK(spec->host_port == 8080); CHECK(spec->container_port == 80); CHECK(spec->protocol == PortForwardProtocol::udp); } TEST_CASE("parse_port_forward_spec: network + /udp suffix combined", "[unit]") { auto spec = parse_port_forward_spec("mynet:8080:80/udp"); REQUIRE(spec.has_value()); REQUIRE(spec->network.has_value()); CHECK(*spec->network == "mynet"); CHECK(spec->protocol == PortForwardProtocol::udp); } TEST_CASE("parse_port_forward_spec: rejects an unrecognized protocol suffix", "[unit]") { CHECK_FALSE(parse_port_forward_spec("8080:80/xyz").has_value()); } TEST_CASE("parse_port_forward_spec: rejects out-of-range ports", "[unit]") { CHECK_FALSE(parse_port_forward_spec("0:80").has_value()); CHECK_FALSE(parse_port_forward_spec("8080:0").has_value()); CHECK_FALSE(parse_port_forward_spec("65536:80").has_value()); CHECK_FALSE(parse_port_forward_spec("8080:65536").has_value()); } TEST_CASE("parse_port_forward_spec: rejects non-numeric ports", "[unit]") { CHECK_FALSE(parse_port_forward_spec("abc:80").has_value()); CHECK_FALSE(parse_port_forward_spec("8080:abc").has_value()); CHECK_FALSE(parse_port_forward_spec("80.5:80").has_value()); } TEST_CASE("parse_port_forward_spec: rejects malformed field counts", "[unit]") { CHECK_FALSE(parse_port_forward_spec("8080").has_value()); CHECK_FALSE(parse_port_forward_spec("").has_value()); CHECK_FALSE(parse_port_forward_spec("a:b:c:d").has_value()); }