// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! passlib's own test vectors, run against every scheme through the registry. //! //! `vectors/known.zon` is every known-answer table in passlib 1.7.4's test //! suite, and `vectors/generated.zon` is hashes passlib made from random //! passwords and salts. `tools/gen_vectors.py` writes both, reading the //! tables out of passlib's test classes rather than transcribing them. Each //! row says what passlib's own tests say about it: //! //! * `correct`: identified, verifies, and a wrong password does not. //! * `disabled`: identified, and verifies nothing: a locked account. //! * `config`: a settings string with no checksum. Identified, and verifying //! against it is an error, since there is nothing to compare. //! * `malformed`: identified by its prefix, and an error to verify. //! * `unidentified`: not identified, and an error to verify. //! * `own`: another test's sample that belongs to this scheme after all. //! * `foreign`: another scheme's hash, which this one must not claim. //! //! Every failure is printed before the test fails, so one run shows all of //! them. `zig build test -Dvector-scheme=md5_crypt` runs one scheme's rows. const std = @import("std"); const testing = std.testing; const options = @import("vector_options"); const passlib = @import("passlib"); const Kind = enum { correct, disabled, config, malformed, unidentified, own, foreign }; const Vector = struct { scheme: []const u8, kind: Kind, hash: []const u8, password: ?[]const u8 = null, wrong: ?[]const u8 = null, user: ?[]const u8 = null, realm: ?[]const u8 = null, }; const known: []const Vector = @import("vectors/known.zon"); const generated: []const Vector = @import("vectors/generated.zon"); fn selected(v: Vector) bool { const only = options.scheme orelse return true; return std.mem.eql(u8, only, v.scheme); } const Tally = struct { ran: usize = 0, failed: usize = 0, missing: std.StringArrayHashMapUnmanaged(usize) = .empty, fn fail(t: *Tally, v: Vector, comptime fmt: []const u8, args: anytype) void { t.failed += 1; std.debug.print("{s} {t} {s}: " ++ fmt ++ "\n", .{ v.scheme, v.kind, v.hash } ++ args); } }; fn run(t: *Tally, v: Vector) !void { const scheme = passlib.registry.get(v.scheme) orelse { const entry = try t.missing.getOrPutValue(testing.allocator, v.scheme, 0); entry.value_ptr.* += 1; return; }; t.ran += 1; const ctx: passlib.Context = .{ .io = testing.io, .gpa = testing.allocator, .user = v.user, .realm = v.realm }; const identified = scheme.identify(v.hash); switch (v.kind) { .correct, .disabled => { if (!identified) return t.fail(v, "not identified", .{}); const want = v.kind == .correct; const got = scheme.verify(ctx, v.password.?, v.hash) catch |err| return t.fail(v, "verify: {t}", .{err}); if (got != want) return t.fail(v, "verify returned {}", .{got}); if (v.wrong) |wrong| { const bad = scheme.verify(ctx, wrong, v.hash) catch |err| return t.fail(v, "verify of the wrong password: {t}", .{err}); if (bad) return t.fail(v, "the wrong password {s} verified", .{wrong}); } }, .config, .malformed => { if (!identified) return t.fail(v, "not identified", .{}); try expectMalformed(t, v, scheme, ctx); }, .unidentified, .foreign => { if (identified) return t.fail(v, "identified", .{}); try expectMalformed(t, v, scheme, ctx); }, .own => { if (!identified) return t.fail(v, "not identified", .{}); _ = scheme.verify(ctx, "stub", v.hash) catch |err| return t.fail(v, "verify: {t}", .{err}); }, } } fn expectMalformed(t: *Tally, v: Vector, scheme: *const passlib.Scheme, ctx: passlib.Context) !void { if (scheme.verify(ctx, "stub", v.hash)) |got| { t.fail(v, "verify returned {} rather than an error", .{got}); } else |err| switch (err) { error.MalformedHash => {}, else => t.fail(v, "verify: {t} rather than MalformedHash", .{err}), } } fn runAll(vectors: []const Vector) !void { var t: Tally = .{}; defer t.missing.deinit(testing.allocator); for (vectors) |v| if (selected(v)) try run(&t, v); if (t.missing.count() > 0) { std.debug.print("{d} rows skipped for schemes not yet implemented:", .{sumOf(t.missing.values())}); for (t.missing.keys()) |k| std.debug.print(" {s}", .{k}); std.debug.print("\n", .{}); } if (t.failed > 0) { std.debug.print("{d} of {d} rows failed\n", .{ t.failed, t.ran }); return error.TestFailed; } } fn sumOf(xs: []const usize) usize { var n: usize = 0; for (xs) |x| n += x; return n; } test "passlib's known-answer tables" { try runAll(known); } test "hashes passlib generated" { try runAll(generated); }