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
const std = @import("std");
const zat = @import("zat");
const hegel = @import("hegel");
const SimIo = @import("sim_io.zig").SimIo;
const Rule = enum { advance, retry, cancel, proof, assertion };
var tally: hegel.Tally(Rule) = .{};
fn payload(a: std.mem.Allocator, token: []const u8) !std.json.Parsed(std.json.Value) {
var parts = std.mem.splitScalar(u8, token, '.');
_ = parts.next();
const decoded = try zat.jwt.base64UrlDecode(a, parts.next().?);
return std.json.parseFromSlice(std.json.Value, a, decoded, .{ .allocate = .alloc_always });
}
fn property(_: void, case: *hegel.TestCase) !void {
var arena: std.heap.ArenaAllocator = .init(std.heap.page_allocator);
defer arena.deinit();
const a = arena.allocator();
var sim = SimIo.init(try case.int(u32, 0, std.math.maxInt(u32)), try case.int(i64, 0, 4_000_000_000));
var model_ns = sim.now_ns;
var secret: [32]u8 = @splat(0);
secret[31] = 1;
const key = try zat.Keypair.fromSecretKey(.p256, secret);
var machine = try case.machine(Rule, &.{ 3, 5, 2, 1, 1 }, 25);
defer machine.deinit();
tally.case();
while (try machine.next()) |rule| {
tally.step(rule);
case.note("{s} at {d} ns", .{ @tagName(rule), model_ns });
switch (rule) {
.advance => {
const delta = try case.int(i64, -1_000_000, 1_000_000);
sim.now_ns += @as(i96, delta) * std.time.ns_per_ms;
model_ns += @as(i96, delta) * std.time.ns_per_ms;
},
.retry, .cancel => {
const base = try case.int(u32, 0, 60_000);
const cap = try case.int(u32, 0, 120_000);
const attempt = try case.int(u8, 0, 255);
const policy: zat.XrpcClient.RetryPolicy = .{ .base_delay_ms = base, .max_delay_ms = cap, .jitter_percent = 0 };
var expected: u64 = base;
for (0..@min(attempt, 16)) |_| expected *= 2;
expected = @min(expected, cap);
const retry_after: ?u64 = if (try case.boolean()) try case.int(u32, 0, 300) else null;
const headers: ?zat.HttpTransport.RateLimitHeaders = if (retry_after) |seconds| blk: {
expected = @min(seconds * 1000, cap);
break :blk .{ .retry_after = seconds };
} else null;
sim.cancel_sleep = rule == .cancel;
if (policy.sleepBeforeRetry(sim.io(), attempt, headers)) |_| {
if (sim.cancel_sleep and expected != 0) return case.fail("retry swallowed cancellation");
model_ns += @as(i96, expected) * std.time.ns_per_ms;
} else |err| {
if (err != error.Canceled or !sim.cancel_sleep or expected == 0) return case.fail("retry failed without cancellation");
case.event("retry canceled before virtual time advances");
}
sim.cancel_sleep = false;
case.event("retry timing compared with model");
},
.proof => {
const nonces = [_][]const u8{ "nonce", "", "quote\"slash\\", "line\nfeed" };
const nonce = nonces[try case.index(nonces.len)];
const token = try zat.oauth.createDpopProof(a, sim.io(), &key, "GET", "https://pds.example/path", nonce, null);
const parsed = payload(a, token) catch return case.fail("DPoP payload is invalid JSON");
const object = parsed.value.object;
if (!std.mem.eql(u8, object.get("nonce").?.string, nonce)) return case.fail("DPoP changes nonce");
if (object.get("iat").?.integer != @divFloor(model_ns, std.time.ns_per_s)) return case.fail("DPoP ignores virtual clock");
case.event("DPoP generated under virtual clock and entropy");
},
.assertion => {
const token = try zat.oauth.createClientAssertion(a, sim.io(), &key, "https://client.example", "https://issuer.example/token");
const parsed = try payload(a, token);
const object = parsed.value.object;
const now = @divFloor(model_ns, std.time.ns_per_s);
if (object.get("iat").?.integer != now or object.get("exp").?.integer != now + 120) return case.fail("client assertion lifetime differs from clock model");
case.event("client assertion lifetime checked");
},
}
if (sim.now_ns != model_ns) return case.fail("retry sleep disagrees with virtual clock model");
}
tally.finish(sim.sleeps);
}
pub fn main() !void {
defer tally.print();
try hegel.run(.{ .key = "runtime" }, {}, property);
}