|
| 1 | +/** @file |
| 2 | +
|
| 3 | + Catch based unit tests for PROXY Protocol |
| 4 | +
|
| 5 | + @section license License |
| 6 | +
|
| 7 | + Licensed to the Apache Software Foundation (ASF) under one |
| 8 | + or more contributor license agreements. See the NOTICE file |
| 9 | + distributed with this work for additional information |
| 10 | + regarding copyright ownership. The ASF licenses this file |
| 11 | + to you under the Apache License, Version 2.0 (the |
| 12 | + "License"); you may not use this file except in compliance |
| 13 | + with the License. You may obtain a copy of the License at |
| 14 | +
|
| 15 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | +
|
| 17 | + Unless required by applicable law or agreed to in writing, software |
| 18 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | + See the License for the specific language governing permissions and |
| 21 | + limitations under the License. |
| 22 | + */ |
| 23 | + |
| 24 | +#define CATCH_CONFIG_MAIN |
| 25 | +#include "catch.hpp" |
| 26 | + |
| 27 | +#include "ProxyProtocol.h" |
| 28 | + |
| 29 | +using namespace std::literals; |
| 30 | + |
| 31 | +TEST_CASE("PROXY Protocol v1 Parser", "[ProxyProtocol][ProxyProtocolv1]") |
| 32 | +{ |
| 33 | + IpEndpoint src_addr; |
| 34 | + IpEndpoint dst_addr; |
| 35 | + |
| 36 | + SECTION("TCP over IPv4") |
| 37 | + { |
| 38 | + ts::TextView raw_data = "PROXY TCP4 192.0.2.1 198.51.100.1 50000 443\r\n"sv; |
| 39 | + |
| 40 | + ProxyProtocol pp_info; |
| 41 | + REQUIRE(proxy_protocol_parse(&pp_info, raw_data) == raw_data.size()); |
| 42 | + |
| 43 | + REQUIRE(ats_ip_pton("192.0.2.1:50000", src_addr) == 0); |
| 44 | + REQUIRE(ats_ip_pton("198.51.100.1:443", dst_addr) == 0); |
| 45 | + |
| 46 | + CHECK(pp_info.version == ProxyProtocolVersion::V1); |
| 47 | + CHECK(pp_info.ip_family == AF_INET); |
| 48 | + CHECK(pp_info.src_addr == src_addr); |
| 49 | + CHECK(pp_info.dst_addr == dst_addr); |
| 50 | + } |
| 51 | + |
| 52 | + SECTION("TCP over IPv6") |
| 53 | + { |
| 54 | + ts::TextView raw_data = "PROXY TCP6 2001:0DB8:0:0:0:0:0:1 2001:0DB8:0:0:0:0:0:2 50000 443\r\n"sv; |
| 55 | + |
| 56 | + ProxyProtocol pp_info; |
| 57 | + REQUIRE(proxy_protocol_parse(&pp_info, raw_data) == raw_data.size()); |
| 58 | + |
| 59 | + REQUIRE(ats_ip_pton("[2001:0DB8:0:0:0:0:0:1]:50000", src_addr) == 0); |
| 60 | + REQUIRE(ats_ip_pton("[2001:0DB8:0:0:0:0:0:2]:443", dst_addr) == 0); |
| 61 | + |
| 62 | + CHECK(pp_info.version == ProxyProtocolVersion::V1); |
| 63 | + CHECK(pp_info.ip_family == AF_INET6); |
| 64 | + CHECK(pp_info.src_addr == src_addr); |
| 65 | + CHECK(pp_info.dst_addr == dst_addr); |
| 66 | + } |
| 67 | + |
| 68 | + SECTION("UNKNOWN connection (short form)") |
| 69 | + { |
| 70 | + ts::TextView raw_data = "PROXY UNKNOWN\r\n"sv; |
| 71 | + |
| 72 | + ProxyProtocol pp_info; |
| 73 | + REQUIRE(proxy_protocol_parse(&pp_info, raw_data) == raw_data.size()); |
| 74 | + |
| 75 | + CHECK(pp_info.version == ProxyProtocolVersion::V1); |
| 76 | + CHECK(pp_info.ip_family == AF_UNSPEC); |
| 77 | + } |
| 78 | + |
| 79 | + SECTION("UNKNOWN connection (worst case)") |
| 80 | + { |
| 81 | + ts::TextView raw_data = |
| 82 | + "PROXY UNKNOWN ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535 65535\r\n"sv; |
| 83 | + |
| 84 | + ProxyProtocol pp_info; |
| 85 | + REQUIRE(proxy_protocol_parse(&pp_info, raw_data) == raw_data.size()); |
| 86 | + |
| 87 | + CHECK(pp_info.version == ProxyProtocolVersion::V1); |
| 88 | + CHECK(pp_info.ip_family == AF_UNSPEC); |
| 89 | + } |
| 90 | + |
| 91 | + SECTION("Malformed Headers") |
| 92 | + { |
| 93 | + ProxyProtocol pp_info; |
| 94 | + |
| 95 | + // lack of some fields |
| 96 | + CHECK(proxy_protocol_parse(&pp_info, "PROXY TCP4"sv) == 0); |
| 97 | + CHECK(proxy_protocol_parse(&pp_info, "PROXY TCP4 192.0.2.1"sv) == 0); |
| 98 | + CHECK(proxy_protocol_parse(&pp_info, "PROXY TCP4 192.0.2.1 198.51.100.1\r\n"sv) == 0); |
| 99 | + CHECK(proxy_protocol_parse(&pp_info, "PROXY TCP4 192.0.2.1 198.51.100.1\r\n"sv) == 0); |
| 100 | + CHECK(proxy_protocol_parse(&pp_info, "PROXY TCP4 192.0.2.1 198.51.100.1 50000 \r\n"sv) == 0); |
| 101 | + |
| 102 | + // invalid preface |
| 103 | + CHECK(proxy_protocol_parse(&pp_info, "PROX TCP4 192.0.2.1 198.51.100.1 50000 443\r\n"sv) == 0); |
| 104 | + CHECK(proxy_protocol_parse(&pp_info, "PROXZ TCP4 192.0.2.1 198.51.100.1 50000 443\r\n"sv) == 0); |
| 105 | + |
| 106 | + // invalid transport protocol & address family |
| 107 | + CHECK(proxy_protocol_parse(&pp_info, "PROXY TCP1 192.0.2.1 198.51.100.1 50000 443\r\n"sv) == 0); |
| 108 | + CHECK(proxy_protocol_parse(&pp_info, "PROXY UDP4 192.0.2.1 198.51.100.1 50000 443\r\n"sv) == 0); |
| 109 | + |
| 110 | + // extra space |
| 111 | + CHECK(proxy_protocol_parse(&pp_info, "PROXY TCP4 192.0.2.1 198.51.100.1 50000 443\r\n"sv) == 0); |
| 112 | + CHECK(proxy_protocol_parse(&pp_info, "PROXY TCP4 192.0.2.1 198.51.100.1 50000 443\r\n"sv) == 0); |
| 113 | + CHECK(proxy_protocol_parse(&pp_info, "PROXY TCP4 192.0.2.1 198.51.100.1 50000 443\r\n"sv) == 0); |
| 114 | + CHECK(proxy_protocol_parse(&pp_info, "PROXY TCP4 192.0.2.1 198.51.100.1 50000 443\r\n"sv) == 0); |
| 115 | + CHECK(proxy_protocol_parse(&pp_info, "PROXY TCP4 192.0.2.1 198.51.100.1 50000 443\r\n"sv) == 0); |
| 116 | + CHECK(proxy_protocol_parse(&pp_info, "PROXY TCP4 192.0.2.1 198.51.100.1 50000 443 \r\n"sv) == 0); |
| 117 | + |
| 118 | + // invalid CRLF |
| 119 | + CHECK(proxy_protocol_parse(&pp_info, "PROXY TCP4 192.0.2.1 198.51.100.1 50000 443"sv) == 0); |
| 120 | + CHECK(proxy_protocol_parse(&pp_info, "PROXY TCP4 192.0.2.1 198.51.100.1 50000 443\n"sv) == 0); |
| 121 | + CHECK(proxy_protocol_parse(&pp_info, "PROXY TCP4 192.0.2.1 198.51.100.1 50000 443\r"sv) == 0); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +TEST_CASE("PROXY Protocol v2 Parser", "[ProxyProtocol][ProxyProtocolv2]") |
| 126 | +{ |
| 127 | + SECTION("TCP over IPv4") |
| 128 | + { |
| 129 | + uint8_t raw_data[] = { |
| 130 | + 0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, ///< sig |
| 131 | + 0x55, 0x49, 0x54, 0x0A, ///< |
| 132 | + 0x02, ///< ver_vmd |
| 133 | + 0x11, ///< fam |
| 134 | + 0x00, 0x0C, ///< len |
| 135 | + 0xC0, 0x00, 0x02, 0x01, ///< src_addr |
| 136 | + 0xC6, 0x33, 0x64, 0x01, ///< dst_addr |
| 137 | + 0xC3, 0x50, ///< src_port |
| 138 | + 0x01, 0xBB, ///< dst_port |
| 139 | + }; |
| 140 | + |
| 141 | + ts::TextView tv(reinterpret_cast<char *>(raw_data), sizeof(raw_data)); |
| 142 | + |
| 143 | + ProxyProtocol pp_info; |
| 144 | + // TODO: add test when implemented. Just checking this doesn't crash for now |
| 145 | + REQUIRE(proxy_protocol_parse(&pp_info, tv) == 0); |
| 146 | + } |
| 147 | +} |
0 commit comments