Something went wrong. Try again.
A URI parser and formatter for Zig, modelled on Python's hyperlink package, with LDAP URLs, file: URIs and sdns:// DNS stamps beside it.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Tests for `ldap`, the RFC 4516 URL parser.//!//! The first table is every example from RFC 4516 §4, which between them//! exercise each optional component, the interaction between percent-encoding//! and the `?` delimiters, and the interaction between percent-encoding and//! RFC 4514's own backslash escapes inside a DN.
const std = @import("std");const uri = @import("root.zig");
const Extension = uri.ldap.Extension;const Host = uri.Host;const Scope = uri.ldap.Scope;const Url = uri.ldap.Url;const expect = std.testing.expect;const expectEqual = std.testing.expectEqual;const expectEqualDeep = std.testing.expectEqualDeep;const expectEqualStrings = std.testing.expectEqualStrings;const expectError = std.testing.expectError;
const Case = struct { url: []const u8, /// Expected serialization. Defaults to `url`; set it where canonical /// re-encoding differs from the input. text: ?[]const u8 = null,
scheme: uri.ldap.Scheme = .ldap, host: ?Host = null, port: ?u16 = null, dn: ?[]const u8 = null, attributes: []const []const u8 = &.{}, scope: ?Scope = null, filter: ?[]const u8 = null, extensions: []const Extension = &.{},};
/// RFC 4516 §4.const rfc_cases = [_]Case{ // No host: the server is whichever one the client is already talking to. .{ .url = "ldap:///o=University%20of%20Michigan,c=US", .dn = "o=University of Michigan,c=US", }, .{ .url = "ldap://ldap1.example.net/o=University%20of%20Michigan,c=US", .host = .{ .hostname = "ldap1.example.net" }, .dn = "o=University of Michigan,c=US", }, .{ .url = "ldap://ldap1.example.net/o=University%20of%20Michigan,c=US?postalAddress", .host = .{ .hostname = "ldap1.example.net" }, .dn = "o=University of Michigan,c=US", .attributes = &.{"postalAddress"}, }, // An empty attribute list, kept because a scope follows it. .{ .url = "ldap://ldap1.example.net:6666/o=University%20of%20Michigan,c=US??sub?(cn=Babs%20Jensen)", .host = .{ .hostname = "ldap1.example.net" }, .port = 6666, .dn = "o=University of Michigan,c=US", .scope = .sub, .filter = "(cn=Babs Jensen)", }, // Scheme and scope are both matched without regard to case, and both are // written back in the lowercase the grammar uses. .{ .url = "LDAP://ldap1.example.com/c=GB?objectClass?ONE", .text = "ldap://ldap1.example.com/c=GB?objectClass?one", .host = .{ .hostname = "ldap1.example.com" }, .dn = "c=GB", .attributes = &.{"objectClass"}, .scope = .one, }, // A `?` inside the DN, which has to stay percent-encoded or it would end // the DN. Written back in the uppercase hex RFC 3986 §6.2.2.1 prefers. .{ .url = "ldap://ldap2.example.com/o=Question%3f,c=US?mail", .text = "ldap://ldap2.example.com/o=Question%3F,c=US?mail", .host = .{ .hostname = "ldap2.example.com" }, .dn = "o=Question?,c=US", .attributes = &.{"mail"}, }, // A filter carrying a `)` that RFC 4515 escapes as `\\29` within the // filter language itself. At the URL level `)` is a sub-delim and needs no // encoding, so the canonical form drops the `%29`: the filter is the same // string either way, and re-parses to the same bytes. .{ .url = "ldap://ldap3.example.com/o=Babsco,c=US???(four=1*two=5%29)", .text = "ldap://ldap3.example.com/o=Babsco,c=US???(four=1*two=5))", .host = .{ .hostname = "ldap3.example.com" }, .dn = "o=Babsco,c=US", .filter = "(four=1*two=5))", }, // Two levels of escaping that must not be confused: `%5C2C` is the URL // encoding of `\2C`, which is in turn RFC 4514's escape for a comma // *inside* an attribute value. Decoding the URL leaves the DN escape // alone, so the DN still has two RDNs rather than three. .{ .url = "ldap://ldap.example.com/o=An%20Example%5C2C%20Inc.,c=US", .host = .{ .hostname = "ldap.example.com" }, .dn = "o=An Example\\2C Inc.,c=US", }, // No DN at all, which is not the same text as an empty one even though it // means the same thing. .{ .url = "ldap://ldap.example.net", .host = .{ .hostname = "ldap.example.net" }, }, .{ .url = "ldap://ldap.example.net/", .host = .{ .hostname = "ldap.example.net" }, .dn = "", }, // A trailing `?` with nothing after it says nothing, so it is dropped. .{ .url = "ldap://ldap.example.net/?", .text = "ldap://ldap.example.net/", .host = .{ .hostname = "ldap.example.net" }, .dn = "", }, // An extension whose value is itself a DN, so its commas are encoded. .{ .url = "ldap:///??sub??e-bindname=cn=Manager%2cdc=example%2cdc=com", .text = "ldap:///??sub??e-bindname=cn=Manager%2Cdc=example%2Cdc=com", .dn = "", .scope = .sub, .extensions = &.{.{ .name = "e-bindname", .value = "cn=Manager,dc=example,dc=com" }}, }, .{ .url = "ldap:///??sub??!e-bindname=cn=Manager%2cdc=example%2cdc=com", .text = "ldap:///??sub??!e-bindname=cn=Manager%2Cdc=example%2Cdc=com", .dn = "", .scope = .sub, .extensions = &.{.{ .critical = true, .name = "e-bindname", .value = "cn=Manager,dc=example,dc=com" }}, },};
const extra_cases = [_]Case{ .{ .url = "ldaps://ldap.example.com/dc=example,dc=com", .scheme = .ldaps, .host = .{ .hostname = "ldap.example.com" }, .dn = "dc=example,dc=com", }, // The socket path lives where the host does, so its slashes are encoded. .{ .url = "ldapi://%2Frun%2Fslapd%2Fldapi/", .scheme = .ldapi, .host = .{ .hostname = "/run/slapd/ldapi" }, .dn = "", }, .{ .url = "ldapi:///dc=example,dc=com", .scheme = .ldapi, .dn = "dc=example,dc=com", }, .{ .url = "ldap://[2001:db8::1]:389/dc=example,dc=com?cn,mail,sn?one", .host = .{ .ipv6 = "2001:db8::1" }, .port = 389, .dn = "dc=example,dc=com", .attributes = &.{ "cn", "mail", "sn" }, .scope = .one, }, .{ .url = "ldap://192.0.2.10/dc=example,dc=com", .host = .{ .ipv4 = "192.0.2.10" }, .dn = "dc=example,dc=com", }, // A compound filter. `&`, `(`, `)`, `*` and `=` are all sub-delims and // survive literally; `|` is not in any RFC 3986 character set, so it is // written as `%7C` even though LDAP tooling prints it raw. .{ .url = "ldap://h/dc=x??sub?(&(objectClass=person)(%7C(cn=a*)(cn=b)))", .host = .{ .hostname = "h" }, .dn = "dc=x", .scope = .sub, .filter = "(&(objectClass=person)(|(cn=a*)(cn=b)))", }, // A trailing run of empty sections carries nothing, so it is dropped. .{ .url = "ldap://h/dc=x????", .text = "ldap://h/dc=x", .host = .{ .hostname = "h" }, .dn = "dc=x", }, // An extension written bare, with no `=` at all -- distinct from one with // an empty value, exactly as for a query parameter. .{ .url = "ldap:///????x-no-value", .dn = "", .extensions = &.{.{ .name = "x-no-value" }}, }, .{ .url = "ldap:///????x-empty-value=", .dn = "", .extensions = &.{.{ .name = "x-empty-value", .value = "" }}, }, // Only the first `=` of an extension is a separator. .{ .url = "ldap:///????x-a=b=c", .dn = "", .extensions = &.{.{ .name = "x-a", .value = "b=c" }}, }, // A port with no host: the client knows which server, but not which port. .{ .url = "ldap://:1389/dc=example", .port = 1389, .dn = "dc=example", }, // The `/` before the first `?` left out, which the grammar does not allow // but which is unambiguous. It reads as an empty DN, and the `/` is put // back on the way out. .{ .url = "ldap://h?cn?sub", .text = "ldap://h/?cn?sub", .host = .{ .hostname = "h" }, .dn = "", .attributes = &.{"cn"}, .scope = .sub, }, // Same, with nothing after the `?` at all. .{ .url = "ldap://h?", .text = "ldap://h/", .host = .{ .hostname = "h" }, .dn = "", }, // Found by `fuzz: parsing an LDAP URL round-trips`, and kept because it is // what caught the missing `/` above: nothing readable would have. It also // exercises lenient percent decoding (the `%-` stays a literal `%`) and a // decoded attribute holding a byte that is not ASCII. .{ .url = "ldap://f?)u#=%-!!,*6]3)),b,S*/63),%ce24", .text = "ldap://f/?)u%23=%25-!!,*6%5D3)),b,S*%2F63),%CE24", .host = .{ .hostname = "f" }, .dn = "", .attributes = &.{ ")u#=%-!!", "*6]3))", "b", "S*/63)", "\xCE24" }, }, // RFC 4514 §2.4 writes a binary attribute value as `#` followed by hex, // and an LDAP URL has no fragment for that `#` to start. .{ .url = "ldap://h/1.3.6.1.4.1.1466.0=%2304024869", .host = .{ .hostname = "h" }, .dn = "1.3.6.1.4.1.1466.0=#04024869", },};
fn check(case: Case) !void { const alloc = std.testing.allocator;
const url = Url.parse(alloc, case.url) catch |err| { std.debug.print("parse failed for {s}: {s}\n", .{ case.url, @errorName(err) }); return err; }; defer url.deinit();
errdefer std.debug.print("case: {s}\n", .{case.url});
try expectEqual(case.scheme, url.scheme); try expectEqualDeep(case.host, url.host); try expectEqual(case.port, url.port); try expectEqualDeep(case.dn, url.dn); try expectEqualDeep(case.attributes, url.attributes); try expectEqual(case.scope, url.scope); try expectEqualDeep(case.filter, url.filter); try expectEqualDeep(case.extensions, url.extensions);
const text = try url.toText(alloc); defer alloc.free(text); try expectEqualStrings(case.text orelse case.url, text);
// Writing is a fixed point: the canonical form parses to the same URL and // writes back byte for byte. const again = try Url.parse(alloc, text); defer again.deinit(); try expect(url.eql(again));
const text_again = try again.toText(alloc); defer alloc.free(text_again); try expectEqualStrings(text, text_again);}
test "RFC 4516 examples" { for (rfc_cases) |case| try check(case);}
test "parsing" { for (extra_cases) |case| try check(case);}
test "{f} renders the URL" { const url = try Url.parse(std.testing.allocator, "ldap://h/dc=x??sub?(cn=a)"); defer url.deinit();
var buf: [128]u8 = undefined; try expectEqualStrings("ldap://h/dc=x??sub?(cn=a)", try std.fmt.bufPrint(&buf, "{f}", .{url}));}
// -- defaults --------------------------------------------------------------
test "an omitted component takes its RFC 4516 default" { const url = try Url.parse(std.testing.allocator, "ldap://h"); defer url.deinit();
try expectEqual(@as(?[]const u8, null), url.dn); try expectEqual(@as(?Scope, null), url.scope); try expectEqual(@as(?[]const u8, null), url.filter); try expectEqual(@as(usize, 0), url.attributes.len);
try expectEqualStrings("", url.effectiveDn()); try expectEqual(Scope.base, url.effectiveScope()); try expectEqualStrings("(objectClass=*)", url.effectiveFilter()); try expectEqual(@as(?u16, 389), url.effectivePort());}
test "the default port follows the scheme" { const alloc = std.testing.allocator; const cases = [_]struct { []const u8, ?u16 }{ .{ "ldap://h", 389 }, .{ "ldaps://h", 636 }, .{ "ldap://h:1389", 1389 }, .{ "ldaps://h:1636", 1636 }, // A socket path has no port to default to. .{ "ldapi://%2Frun%2Fslapd.sock", null }, }; for (cases) |case| { const url = try Url.parse(alloc, case[0]); defer url.deinit(); try expectEqual(case[1], url.effectivePort()); }}
// -- extensions ------------------------------------------------------------
test "extensions are looked up by name and checked for criticality" { const alloc = std.testing.allocator;
const plain = try Url.parse(alloc, "ldap:///????x-a=1,X-B=2"); defer plain.deinit(); try expect(!plain.hasCriticalExtension()); // An extension type is an OID or a name, and names are case-insensitive. try expectEqualStrings("2", plain.extension("x-b").?.value.?); try expectEqualStrings("1", plain.extension("X-A").?.value.?); try expectEqual(@as(?Extension, null), plain.extension("x-c"));
const critical = try Url.parse(alloc, "ldap:///????x-a=1,!x-b=2"); defer critical.deinit(); try expect(critical.hasCriticalExtension()); try expect(!critical.extensions[0].critical); try expect(critical.extensions[1].critical);}
// -- errors ----------------------------------------------------------------
test "rejections" { const alloc = std.testing.allocator; const cases = [_]struct { []const u8, anyerror }{ .{ "https://example.com/", error.NotAnLdapUrl }, .{ "ldapx://example.com/", error.NotAnLdapUrl }, .{ "dc=example,dc=com", error.NotAnLdapUrl }, // A relative reference has no scheme, even though it has a `:`. .{ "/a:b", error.NotAnLdapUrl }, .{ "ldap:dc=example", error.MissingAuthority }, .{ "ldap:/dc=example", error.MissingAuthority }, // RFC 4516 has no userinfo; credentials come from the bind. .{ "ldap://cn=admin@h/dc=x", error.UnexpectedUserinfo }, .{ "ldap://cn=admin:secret@h/dc=x", error.UnexpectedUserinfo }, .{ "ldap://h/dc=x??deep", error.InvalidScope }, .{ "ldap://h/dc=x??subtree", error.InvalidScope }, .{ "ldap://h/dc=x?cn,,mail", error.EmptyAttributeDescription }, .{ "ldap://h/dc=x?,cn", error.EmptyAttributeDescription }, .{ "ldap://h/dc=x????a,,b", error.EmptyExtensionType }, .{ "ldap://h/dc=x????!", error.EmptyExtensionType }, .{ "ldap://h/dc=x????=v", error.EmptyExtensionType }, // The grammar has room for four `?`; a fifth has to be encoded. .{ "ldap://h/dc=x?????", error.ExtraQuestionMark }, .{ "ldap://h/dc=x?cn?sub?(cn=a)?x-a=1?more", error.ExtraQuestionMark }, .{ "ldap://[2001:db8::1/dc=x", error.UnterminatedIPv6Literal }, .{ "ldap://[not-an-address]/dc=x", error.InvalidIPv6Literal }, .{ "ldap://h:99999/dc=x", error.InvalidPort }, }; for (cases) |case| { errdefer std.debug.print("case: {s}\n", .{case[0]}); try expectError(case[1], Url.parse(alloc, case[0])); }}
test "strict_percent rejects a stray percent sign" { const alloc = std.testing.allocator;
// Lenient by default, matching `Uri.parse`: a `%` that introduces nothing // stays a literal `%`. const lenient = try Url.parse(alloc, "ldap://h/dc=100%,c=US"); defer lenient.deinit(); try expectEqualStrings("dc=100%,c=US", lenient.dn.?);
try expectError( error.InvalidPercentEncoding, Url.parseOpts(alloc, "ldap://h/dc=100%,c=US", .{ .strict_percent = true }), );}
// -- the reason this is not `Uri` -----------------------------------------
test "a filter survives here and does not survive the generic parser" { const alloc = std.testing.allocator; const text = "ldap://h/dc=x??sub?(&(objectClass=person)(cn=Bob))";
const url = try Url.parse(alloc, text); defer url.deinit(); try expectEqualStrings("(&(objectClass=person)(cn=Bob))", url.filter.?);
// The same text through `Uri`: everything after the first `?` is one // query string, and `&` and `=` are its syntax, so the scope and the // filter come back shredded into two parameters with nothing recoverable // in either of them. const generic = try uri.Uri.parse(alloc, text); defer generic.deinit(); try expectEqual(@as(usize, 2), generic.query.len); try expectEqualStrings("?sub?(", generic.query[0].key); try expectEqualStrings("(objectClass", generic.query[1].key); try expectEqualStrings("person)(cn=Bob))", generic.query[1].value.?);}