summaryrefslogtreecommitdiff
path: root/src/mongo/transport/proxy_protocol_header_parser_test.cpp
blob: 8de76feb091325618aae6115974d0ea1d66c3932 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/**
 *    Copyright (C) 2021-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    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
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#include "mongo/transport/proxy_protocol_header_parser.h"

#include "mongo/unittest/assert_that.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/shared_buffer.h"

namespace mongo::transport {
namespace {

using namespace unittest::match;
using namespace fmt::literals;

template <typename MSrc, typename MDst>
class ProxiedEndpointsAre : public Matcher {
public:
    explicit ProxiedEndpointsAre(MSrc&& s, MDst&& d) : _src(std::move(s)), _dst(std::move(d)) {}

    std::string describe() const {
        return "ProxiedEndpointsAre({}, {})"_format(_src.describe(), _dst.describe());
    }

    MatchResult match(const ProxiedEndpoints& e) const {
        return StructuredBindingsAre<MSrc, MDst>(_src, _dst).match(e);
    }

private:
    MSrc _src;
    MDst _dst;
};

ParserResults parseAllPrefixes(StringData s) {
    boost::optional<ParserResults> results;
    for (size_t len = 0; len <= s.size(); ++len) {
        StringData sub = s.substr(0, len);
        results = parseProxyProtocolHeader(sub);
        if (len < s.size()) {
            ASSERT_FALSE(results) << "size={}, sub={}"_format(len, sub);
        }
    }
    ASSERT_TRUE(results);
    return *results;
}

void parseStringExpectFailure(StringData s, std::string regex) {
    try {
        parseAllPrefixes(s);
        FAIL("Expected to throw");
    } catch (const DBException& ex) {
        ASSERT_THAT(ex.toStatus(), StatusIs(Eq(ErrorCodes::FailedToParse), ContainsRegex(regex)));
    }
}

boost::optional<ProxiedEndpoints> parseStringExpectSuccess(StringData s) {
    const ParserResults results = parseAllPrefixes(s);
    ASSERT_THAT(results.bytesParsed, Eq(s.size()));

    // Also test that adding garbage to the end doesn't increase the bytesParsed amount.
    const boost::optional<ParserResults> possibleResultsWithGarbage =
        parseProxyProtocolHeader(s + "garbage");
    ASSERT_TRUE(possibleResultsWithGarbage);
    const ParserResults resultsWithGarbage = *possibleResultsWithGarbage;
    ASSERT_THAT(resultsWithGarbage.bytesParsed, Eq(s.size()));
    if (results.endpoints) {
        ASSERT_THAT(*results.endpoints,
                    ProxiedEndpointsAre(Eq(resultsWithGarbage.endpoints->sourceAddress),
                                        Eq(resultsWithGarbage.endpoints->destinationAddress)));
    } else {
        ASSERT_FALSE(resultsWithGarbage.endpoints);
    }

    return resultsWithGarbage.endpoints;
}

TEST(ProxyProtocolHeaderParser, MalformedIpv4Addresses) {
    StringData testCases[] = {"1",
                              "1.1",
                              "1.1.1",
                              "1.1.1.1.1",
                              "1.1.1.1.",
                              ".1.1.1.1",
                              "1234.1.1.1",
                              "1.1234.1.1",
                              "1.1.1234.1",
                              "1.1.1.1234",
                              "1.1.1.a",
                              "1.1.1.256",
                              "256.1.1.1",
                              "1.1..1.1",
                              "-0.1.1.1",
                              "-1.1.1.1",
                              ""};

    for (const auto& testCase : testCases) {
        try {
            proxy_protocol_details::validateIpv4Address(testCase);
            FAIL("Expected to throw");
        } catch (const DBException& ex) {
            ASSERT_THAT(ex.toStatus(),
                        StatusIs(Eq(ErrorCodes::FailedToParse), ContainsRegex("malformed")));
        }
    }
}

TEST(ProxyProtocolHeaderParser, WellFormedIpv4Addresses) {
    StringData testCases[] = {
        "1.1.1.1", "0.0.0.0", "255.255.255.255", "0.255.0.255", "127.0.1.1", "1.12.123.0"};

    for (const auto& testCase : testCases) {
        proxy_protocol_details::validateIpv4Address(testCase);
    }
}

TEST(ProxyProtocolHeaderParser, MalformedIpv6Addresses) {
    StringData testCases[] = {"0000",
                              "0000:0000",
                              "0000:0000:0000",
                              "0000:0000:0000:0000:0000",
                              "0000:0000:0000:0000:0000:0000",
                              "0000:0000:0000:0000:0000:0000:0000",
                              "0000:0000:0000:0000:0000:0000:0000:0000:0000",
                              "0000:0000:0000:0000:0000:0000:0000:",
                              ":0000:0000:0000:0000:0000:0000:0000",
                              "00000:0000:0000:0000:0000:0000:0000:0000",
                              "0000:0000:0000:0000:0000:0000:0000:00000",
                              "0000:-0000:0000:0000:0000:0000:0000:0000",
                              "0000:-000:0000:0000:0000:0000:0000:0000",
                              "000g:0000:0000:0000:0000:0000:0000:0000",
                              "0000:0000:0000:0000:0000:0000:0000:000g",
                              "0000::0000:0000:0000:0000:0000:0000:0000",
                              "0000:0000:0000:0000:0000:0000:0000::0000",
                              "0000:0000:0000:0000:0000:0000:0000:0000::",
                              "::0000:0000:0000:0000:0000:0000:0000:0000",
                              "::0000::",
                              "0000::0000::0000:0000:0000:0000:0000",
                              "0000::0000::0000:0000:0000:0000:0000:0000",
                              "::0000:",
                              ":0000::",
                              ":::",
                              ""};

    for (const auto& testCase : testCases) {
        try {
            proxy_protocol_details::validateIpv6Address(testCase);
            FAIL("Expected to throw");
        } catch (const DBException& ex) {
            ASSERT_THAT(ex.toStatus(),
                        StatusIs(Eq(ErrorCodes::FailedToParse), ContainsRegex("malformed")));
        }
    }
}

TEST(ProxyProtocolHeaderParser, WellFormedIpv6Addresses) {
    StringData testCases[] = {"::",
                              "::0000",
                              "::0000:0000",
                              "::0000:0000:0000",
                              "::0000:0000:0000:0000",
                              "::0000:0000:0000:0000:0000",
                              "::0000:0000:0000:0000:0000:0000",
                              "::0000:0000:0000:0000:0000:0000:0000",
                              "0000:0000:0000:0000:0000:0000:0000::",
                              "0000:0000:0000:0000:0000:0000::",
                              "0000:0000:0000:0000:0000::",
                              "0000:0000:0000:0000::",
                              "0000:0000:0000::",
                              "0000:0000::",
                              "0000::",
                              "0000::0000",
                              "0000::0000:0000",
                              "0000::0000:0000:0000",
                              "0000::0000:0000:0000:0000",
                              "0000::0000:0000:0000:0000:0000",
                              "0000::0000:0000:0000:0000:0000:0000",
                              "0000:0000:0000::0000:0000:0000",
                              "0000:0000:0000::0000:0000",
                              "0000:0000:0000:0000:0000:0000:0000:0000",
                              "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
                              "ffff::",
                              "0123:4567:89ab:cdef::",
                              "::0123:4567:89ab:cdef",
                              "0123:4567::89ab:cdef"};

    for (const auto& testCase : testCases) {
        proxy_protocol_details::validateIpv6Address(testCase);
    }
}

TEST(ProxyProtocolHeaderParser, MalformedV1Headers) {
    std::pair<std::string, std::string> testCases[] = {
        {"PORXY ", "header bytes invalid"},

        {"PROXY " + std::string(200, '1'), "No terminating newline"},

        // Even if there is a terminating newline, it has to happen before the longest possible
        // header length is seen.
        {"PROXY UNKNOWN " + std::string(92, '1') + "\r\n", "No terminating newline"},

        {"PROXY " + std::string(50, '\r') + "1" + "\r\r\n", "address string malformed"},
        {"PROXY TCP4 \r\n", "address string malformed"},
        {"PROXY TCP4 1.1.1.1\r\n", "address string malformed"},
        {"PROXY TCP4 1.1.1.1 1.1.1.1 10\r\n", "address string malformed"},

        {"PROXY TCP4 12800000000 28 10 10\r\n", "malformed IPv4"},
        {"PROXY TCP4 128 28000000000000 10 10\r\n", "malformed IPv4"},
        {"PROXY TCP4 1.1.1.1 notanip 10 300\r\n", "malformed IPv4"},
        {"PROXY TCP4 a:b:c:d 20 10 300\r\n", "malformed IPv4"},
        {"PROXY TCP4 1.1.1.1 1.1.1.1 -10 300\r\n", "Negative"},
        {"PROXY TCP4 1.1.1.1 1.1.1.1 10 -300\r\n", "Negative"},
        {"PROXY TCP4 1.1.1.1 2.2.2.2 notaport 10\r\n", "Did not consume"},
        {"PROXY TCP4 1.1.1.1 2.2.2.2 10 20garbage\r\n", "Did not consume"},

        // Check TCP6
        {"PROXY TCP6 \r\n", "address string malformed"},
        {"PROXY TCP6 ::\r\n", "address string malformed"},
        {"PROXY TCP6 :: :: 10\r\n", "address string malformed"},

        {"PROXY TCP6 1.1.1.1 2.2.2.2 10 10\r\n", "malformed IPv6"},
        {"PROXY TCP6 :: ::000g 10 10\r\n", "malformed IPv6"},
        {"PROXY TCP6 :: :: -10 10\r\n", "Negative"},
        {"PROXY TCP6 :: :: 10 -10\r\n", "Negative"},
        {"PROXY TCP6 :: notanip 10 300\r\n", "malformed IPv6"},
        {"PROXY TCP6 :: :: notaport 10\r\n", "Did not consume"},
        {"PROXY TCP6 :: :: 10 20garbage\r\n", "Did not consume"}};

    for (const auto& testCase : testCases) {
        parseStringExpectFailure(testCase.first, testCase.second);
    }
}

TEST(ProxyProtocolHeaderParser, WellFormedV1Headers) {
    ASSERT_THAT(*parseStringExpectSuccess("PROXY TCP4 1.1.1.1 2.2.2.2 10 300\r\n"),
                ProxiedEndpointsAre(Eq(SockAddr::create("1.1.1.1", 10, AF_INET)),
                                    Eq(SockAddr::create("2.2.2.2", 300, AF_INET))));

    ASSERT_THAT(*parseStringExpectSuccess("PROXY TCP4 0.0.0.128 0.0.1.44 1000 3000\r\n"),
                ProxiedEndpointsAre(Eq(SockAddr::create("0.0.0.128", 1000, AF_INET)),
                                    Eq(SockAddr::create("0.0.1.44", 3000, AF_INET))));

    static constexpr StringData allFs = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"_sd;
    ASSERT_THAT(*parseStringExpectSuccess("PROXY TCP6 {} "
                                          "{} 10000 30000\r\n"_format(allFs, allFs)),
                ProxiedEndpointsAre(Eq(SockAddr::create(allFs, 10000, AF_INET6)),
                                    Eq(SockAddr::create(allFs, 30000, AF_INET6))));

    ASSERT_THAT(*parseStringExpectSuccess("PROXY TCP6 :: {} 1000 3000\r\n"_format(allFs)),
                ProxiedEndpointsAre(Eq(SockAddr::create("::", 1000, AF_INET6)),
                                    Eq(SockAddr::create(allFs, 3000, AF_INET6))));

    ASSERT_THAT(*parseStringExpectSuccess("PROXY TCP6 2001:0db8:: 0064:ff9b::0000 1000 3000\r\n"),
                ProxiedEndpointsAre(Eq(SockAddr::create("2001:db8::", 1000, AF_INET6)),
                                    Eq(SockAddr::create("64:ff9b::0000", 3000, AF_INET6))));

    // The shortest possible V1 header
    ASSERT_FALSE(parseStringExpectSuccess("PROXY UNKNOWN\r\n"));
    ASSERT_FALSE(
        parseStringExpectSuccess("PROXY UNKNOWN 2001:db8:: 64:ff9b::0.0.0.0 1000 3000\r\n"));
    ASSERT_FALSE(parseStringExpectSuccess("PROXY UNKNOWN hot garbage\r\n"));
    // The longest possible V1 header
    ASSERT_FALSE(
        parseStringExpectSuccess("PROXY UNKNOWN {} {} 65535 65535\r\n"_format(allFs, allFs)));
}

struct TestV2Header {
    std::string header;
    std::string versionAndCommand;
    std::string addressFamilyAndProtocol;
    std::string length;
    std::string firstAddr;
    std::string secondAddr;
    std::string metadata;

    std::string toString() const {
        return "{}{}{}{}{}{}{}"_format(header,
                                       versionAndCommand,
                                       addressFamilyAndProtocol,
                                       length,
                                       firstAddr,
                                       secondAddr,
                                       metadata);
    }
};

TEST(ProxyProtocolHeaderParser, MalformedV2Headers) {
    // These strings contain null characters in them, so we need string literals.
    using namespace std::string_literals;

    TestV2Header header;

    // Specify an invalid header.
    header.header = "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x48\x54\x0A"s;
    parseStringExpectFailure(header.toString(), "header bytes invalid");

    // Correct the header but break the version.
    header.header = "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A"s;
    header.versionAndCommand = "\x30";
    parseStringExpectFailure(header.toString(), "Invalid version");
    header.versionAndCommand = "\x22";
    parseStringExpectFailure(header.toString(), "Invalid version");
    // Correct the version/command but break the address family.
    header.versionAndCommand = "\x21";
    header.addressFamilyAndProtocol = "\x40";
    parseStringExpectFailure(header.toString(), "Invalid address");
    header.addressFamilyAndProtocol = "\x23";
    parseStringExpectFailure(header.toString(), "Invalid protocol");

    // TCP4
    // Set the length to 1.
    header.addressFamilyAndProtocol = "\x11";
    header.length = "\x00\x01"s;
    header.firstAddr = std::string(1, '\0');
    parseStringExpectFailure(header.toString(), "too short");
    // Set to the longest non-valid length (11).
    header.length = "\x00\x0B"s;
    header.firstAddr = std::string(6, '\0');
    header.secondAddr = std::string(5, '\0');
    parseStringExpectFailure(header.toString(), "too short");

    // TCP6
    // Set the length to 1.
    header.addressFamilyAndProtocol = "\x21";
    header.length = "\x00\x01"s;
    header.firstAddr = std::string(1, '\0');
    header.secondAddr = "";
    parseStringExpectFailure(header.toString(), "too short");
    // Set to the longest non-valid length (35).
    header.length = "\x00\x23"s;
    header.firstAddr = std::string(18, '\0');
    header.secondAddr = std::string(17, '\0');
    parseStringExpectFailure(header.toString(), "too short");

    // UNIX
    // Set the length to 1.
    header.addressFamilyAndProtocol = "\x31";
    header.length = "\x00\x01"s;
    header.firstAddr = std::string(1, '\0');
    header.secondAddr = "";
    parseStringExpectFailure(header.toString(), "too short");
    // Set to the longest non-valid length (35).
    header.length = "\x00\xD7"s;
    header.firstAddr = std::string(108, '\0');
    header.secondAddr = std::string(107, '\0');
    parseStringExpectFailure(header.toString(), "too short");
}

template <typename SockAddrUn = sockaddr_un>
std::pair<SockAddrUn, SockAddrUn> createTestSockAddrUn(std::string srcPath, std::string dstPath) {
    std::pair<SockAddrUn, SockAddrUn> addrs;
    addrs.first.sun_family = addrs.second.sun_family = AF_UNIX;

    memcpy(addrs.first.sun_path,
           srcPath.c_str(),
           std::min(srcPath.size(), sizeof(SockAddrUn::sun_path)));
    memcpy(addrs.second.sun_path,
           dstPath.c_str(),
           std::min(dstPath.size(), sizeof(SockAddrUn::sun_path)));

    return addrs;
}

std::string createTestUnixPathString(StringData path) {
    std::string out{path};
    out.resize(proxy_protocol_details::kMaxUnixPathLength);
    return out;
}

TEST(ProxyProtocolHeaderParser, WellFormedV2Headers) {
    // These strings contain null characters in them, so we need string literals.
    using namespace std::string_literals;

    TestV2Header header;
    // TCP4
    header.header = "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A"s;
    header.versionAndCommand = "\x21"s;
    header.addressFamilyAndProtocol = "\x12"s;
    header.length = "\x00\x0C"s;
    header.firstAddr = "\x0C\x22\x38\x4e\xac\x10"s;
    header.secondAddr = "\x00\x01\x04\xd2\x1f\x90"s;
    ASSERT_THAT(*parseStringExpectSuccess(header.toString()),
                ProxiedEndpointsAre(Eq(SockAddr::create("12.34.56.78", 1234, AF_INET)),
                                    Eq(SockAddr::create("172.16.0.1", 8080, AF_INET))));

    // We correctly ignore data at the end.
    header.length = "\x00\x0E"s;
    header.metadata = std::string(2, '\0');
    ASSERT_THAT(*parseStringExpectSuccess(header.toString()),
                ProxiedEndpointsAre(Eq(SockAddr::create("12.34.56.78", 1234, AF_INET)),
                                    Eq(SockAddr::create("172.16.0.1", 8080, AF_INET))));

    // If this is a local connection, return nothing.
    header.versionAndCommand = "\x20"s;
    ASSERT_FALSE(parseStringExpectSuccess(header.toString()));

    // TCP6
    header.versionAndCommand = "\x21"s;
    header.addressFamilyAndProtocol = "\x21"s;
    header.length = "\x00\x24"s;
    header.firstAddr = "\x20\x1\xd\xb8\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x64"s;
    header.secondAddr = "\xff\x9b\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x4\xd2\x1f\x90"s;
    header.metadata = "";
    ASSERT_THAT(*parseStringExpectSuccess(header.toString()),
                ProxiedEndpointsAre(Eq(SockAddr::create("2001:db8::", 1234, AF_INET6)),
                                    Eq(SockAddr::create("64:ff9b::0.0.0.0", 8080, AF_INET6))));

    // We correctly ignore data at the end.
    header.length = "\x00\x55"s;
    header.metadata = std::string(49, '\1');
    ASSERT_THAT(*parseStringExpectSuccess(header.toString()),
                ProxiedEndpointsAre(Eq(SockAddr::create("2001:db8::", 1234, AF_INET6)),
                                    Eq(SockAddr::create("64:ff9b::0.0.0.0", 8080, AF_INET6))));

    // If this is a local connection, return nothing.
    header.versionAndCommand = "\x20"s;
    ASSERT_FALSE(parseStringExpectSuccess(header.toString()));

    // UNIX
    header.versionAndCommand = "\x21"s;
    header.addressFamilyAndProtocol = "\x31"s;
    header.length = "\x00\xD8"s;
    const std::string srcPath(sizeof(sockaddr_un::sun_path) / 2, '\1');
    const std::string dstPath(sizeof(sockaddr_un::sun_path) - 1, '\2');
    header.firstAddr = createTestUnixPathString(srcPath);
    header.secondAddr = createTestUnixPathString(dstPath);
    header.metadata = "";
    const auto addrs = createTestSockAddrUn(srcPath, dstPath);
    ASSERT_THAT(*parseStringExpectSuccess(header.toString()),
                ProxiedEndpointsAre(Eq(SockAddr((sockaddr*)&addrs.first, sizeof(sockaddr_un))),
                                    Eq(SockAddr((sockaddr*)&addrs.second, sizeof(sockaddr_un)))));

    // We correctly ignore data at the end.
    header.length = "\x01\x08";
    header.metadata = std::string(48, '\0');
    // Extraneous data at the end is correctly ingested and ignored.
    ASSERT_THAT(*parseStringExpectSuccess(header.toString()),
                ProxiedEndpointsAre(Eq(SockAddr((sockaddr*)&addrs.first, sizeof(sockaddr_un))),
                                    Eq(SockAddr((sockaddr*)&addrs.second, sizeof(sockaddr_un)))));

    // If this is a local connection, return nothing.
    header.versionAndCommand = "\x20"s;
    ASSERT_FALSE(parseStringExpectSuccess(header.toString()));

    // The family is not parsed if the connection is local.
    header.addressFamilyAndProtocol = "\xA2";
    ASSERT_FALSE(parseStringExpectSuccess(header.toString()));
}

struct TestSockAddrUnLinux {
    sa_family_t sun_family;
    char sun_path[108];

    friend bool operator==(const TestSockAddrUnLinux& a, const TestSockAddrUnLinux& b) {
        return a.sun_family == b.sun_family && !memcmp(a.sun_path, b.sun_path, sizeof(sun_path));
    }
};

TEST(ProxyProtocolHeaderParser, LinuxSockAddrUnParsing) {
    // Test the parser against a Linux-like sockaddr_un
    {
        const std::string srcPath(sizeof(TestSockAddrUnLinux::sun_path) / 2, '\1');
        const std::string dstPath(sizeof(TestSockAddrUnLinux::sun_path) - 1, '\2');
        const auto addrs = createTestSockAddrUn<TestSockAddrUnLinux>(srcPath, dstPath);
        ASSERT_THAT(proxy_protocol_details::parseSockAddrUn<TestSockAddrUnLinux>(
                        createTestUnixPathString(srcPath)),
                    Eq(addrs.first));
        ASSERT_THAT(proxy_protocol_details::parseSockAddrUn<TestSockAddrUnLinux>(
                        createTestUnixPathString(dstPath)),
                    Eq(addrs.second));
    }

    {
        const std::string srcPath(sizeof(TestSockAddrUnLinux::sun_path), '\0');
        const std::string dstPath(1, '\0');
        const auto addrs = createTestSockAddrUn<TestSockAddrUnLinux>(srcPath, dstPath);
        ASSERT_THAT(proxy_protocol_details::parseSockAddrUn<TestSockAddrUnLinux>(
                        createTestUnixPathString(srcPath)),
                    Eq(addrs.first));
        ASSERT_THAT(proxy_protocol_details::parseSockAddrUn<TestSockAddrUnLinux>(
                        createTestUnixPathString(dstPath)),
                    Eq(addrs.second));
    }
}

struct TestSockAddrUnMac {
    unsigned char sun_len;
    sa_family_t sun_family;
    char sun_path[104];

    friend bool operator==(const TestSockAddrUnMac& a, const TestSockAddrUnMac& b) {
        return a.sun_family == b.sun_family && a.sun_len == b.sun_len &&
            !memcmp(a.sun_path, b.sun_path, a.sun_len);
    }
};

TEST(ProxyProtocolHeaderParser, MacSockAddrUnParsing) {
    // Test the parser against a Mac-like sockaddr_un
    {
        const std::string srcPath(sizeof(TestSockAddrUnMac::sun_path) / 2, '\1');
        const std::string dstPath(sizeof(TestSockAddrUnMac::sun_path) - 1, '\2');
        const auto addrs = createTestSockAddrUn<TestSockAddrUnMac>(srcPath, dstPath);
        ASSERT_THAT(proxy_protocol_details::parseSockAddrUn<TestSockAddrUnMac>(
                        createTestUnixPathString(srcPath)),
                    Eq(addrs.first));
        ASSERT_THAT(proxy_protocol_details::parseSockAddrUn<TestSockAddrUnMac>(
                        createTestUnixPathString(dstPath)),
                    Eq(addrs.second));
    }

    {
        const std::string srcPath(1, '\1');
        const std::string dstPath = "";
        const auto addrs = createTestSockAddrUn<TestSockAddrUnMac>(srcPath, dstPath);
        ASSERT_THAT(proxy_protocol_details::parseSockAddrUn<TestSockAddrUnMac>(
                        createTestUnixPathString(srcPath)),
                    Eq(addrs.first));
        ASSERT_THAT(proxy_protocol_details::parseSockAddrUn<TestSockAddrUnMac>(
                        createTestUnixPathString(dstPath)),
                    Eq(addrs.second));
    }

    try {
        const std::string srcPath(proxy_protocol_details::kMaxUnixPathLength - 1, '\1');
        const std::string dstPath(proxy_protocol_details::kMaxUnixPathLength - 1, '\2');
        proxy_protocol_details::parseSockAddrUn<TestSockAddrUnMac>(
            createTestUnixPathString(srcPath) + createTestUnixPathString(dstPath));
        FAIL("Expected to throw");
    } catch (const DBException& ex) {
        ASSERT_THAT(ex.toStatus(),
                    StatusIs(Eq(ErrorCodes::FailedToParse), ContainsRegex("longer than system")));
    }

    try {
        const std::string srcPath(sizeof(TestSockAddrUnMac::sun_path), '\1');
        const std::string dstPath(sizeof(TestSockAddrUnMac::sun_path), '\2');
        proxy_protocol_details::parseSockAddrUn<TestSockAddrUnMac>(
            createTestUnixPathString(srcPath) + createTestUnixPathString(dstPath));
        FAIL("Expected to throw");
    } catch (const DBException& ex) {
        ASSERT_THAT(ex.toStatus(),
                    StatusIs(Eq(ErrorCodes::FailedToParse), ContainsRegex("longer than system")));
    }
}

}  // namespace
}  // namespace mongo::transport