OpenVPN 3 Core Library
Loading...
Searching...
No Matches
test_hostport.cpp
Go to the documentation of this file.
1#include "test_common.hpp"
2
4
5using namespace openvpn;
6
7static void test(const std::string &str,
8 const std::string &default_port,
9 const bool allow_unix,
10 const std::string &expected_host,
11 const std::string &expected_port,
12 const unsigned int expected_portnum,
13 const bool expected_status)
14{
15 std::string host;
16 std::string port;
17 unsigned int port_save = 0;
18
19 const bool status = HostPort::split_host_port(str, host, port, default_port, allow_unix, &port_save);
20 if (expected_status)
21 {
22 if (!status)
23 OPENVPN_THROW_EXCEPTION("failed to parse " << str << " default_port=" << default_port << " allow_unix=" << allow_unix);
24 if (expected_host != host)
25 OPENVPN_THROW_EXCEPTION("inconsistent host " << str << " expected=" << expected_host << " actual=" << host);
26 if (expected_port != port)
27 OPENVPN_THROW_EXCEPTION("inconsistent port " << str << " expected=" << expected_port << " actual-" << port);
28 if (expected_portnum != port_save)
29 OPENVPN_THROW_EXCEPTION("inconsistent portnum " << str << " expected=" << expected_portnum << " actual=" << port_save);
30 }
31 else
32 {
33 if (status)
34 OPENVPN_THROW_EXCEPTION("parse should have failed: " << str);
35 }
36}
37
38TEST(misc, host_port)
39{
40 test("foo.bar.gov", "", false, "", "", 0, false);
41 test("foo.bar.gov", "1234", false, "foo.bar.gov", "1234", 1234, true);
42 test("foo.bar.gov:5678", "1234", false, "foo.bar.gov", "5678", 5678, true);
43 test("foo.bar.gov:5678", "", false, "foo.bar.gov", "5678", 5678, true);
44 test("[foo.bar.gov]:5678", "555555", false, "foo.bar.gov", "5678", 5678, true);
45 test("[foo.bar.gov]", "1234", false, "foo.bar.gov", "1234", 1234, true);
46 test("[foo.bar.gov]", "123456", false, "", "", 0, false);
47 test("1.2.3.4:5678", "", false, "1.2.3.4", "5678", 5678, true);
48 test("[1.2.3.4]:5678", "", false, "1.2.3.4", "5678", 5678, true);
49 test("[1.2.3.4]", "5678", false, "1.2.3.4", "5678", 5678, true);
50 test("[::0]", "9999", false, "::0", "9999", 9999, true);
51 test("[::0]", "", false, "", "", 0, false);
52 test("[::0]:9999", "", false, "::0", "9999", 9999, true);
53 test("", "", false, "", "", 0, false);
54 test(":", "", false, "", "", 0, false);
55 test("x:", "", false, "", "", 0, false);
56 test(":4", "", false, "", "", 0, false);
57 test("[]:1234", "", false, "", "", 0, false);
58 test("[fe80::1443:76ff:fe2e:1479]", "4040", false, "fe80::1443:76ff:fe2e:1479", "4040", 4040, true);
59 test("[fe80::1443:76ff:fe2e:147a]:8080", "4040", false, "fe80::1443:76ff:fe2e:147a", "8080", 8080, true);
60 test("fe80::1443:76ff:fe2e:1477", "4040", false, "fe80::1443:76ff:fe2e:1477", "4040", 4040, true);
61 test("[foo]", "", false, "", "", 0, false);
62 test("[", "", false, "", "", 0, false);
63 test("]", "", false, "", "", 0, false);
64
65 test("/foo/bar", "unix", false, "", "", 0, false);
66 test("/foo/bar", "unix", true, "/foo/bar", "unix", 0, true);
67 test("/foo/bar:unix", "", true, "/foo/bar", "unix", 0, true);
68}
#define OPENVPN_THROW_EXCEPTION(stuff)
bool split_host_port(const std::string &str, std::string &host, std::string &port, const std::string &default_port, const bool allow_unix, unsigned int *port_save=nullptr)
Definition hostport.hpp:99
proxy_host_port port
os<< "Session Name: "<< tbc-> session_name<< '\n';os<< "Layer: "<< tbc-> layer str()<< '\n'
proxy_host_port host
TEST(misc, host_port)
void test()
Definition test_rc.cpp:80