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
const std = @import("std");
const hegel = @import("hegel");
const percent = @import("zat").percent;
test "percent decoder contract" {
try hegel.run(.{ .key = "zat.percent.v2" }, {}, property);
}
fn property(_: void, case: *hegel.TestCase) !void {
var input: [768]u8 = undefined;
var raw: [256]u8 = undefined;
const minimum: usize = if (try case.boolean()) 128 else 0;
const bytes = try case.bytes(&raw, minimum, raw.len);
if (bytes.len == 0) case.event("empty input");
if (bytes.len >= 128) case.event("128+ decoded bytes");
if (std.mem.indexOfScalar(u8, bytes, 0) != null) case.event("NUL byte");
if (std.mem.indexOfScalar(u8, bytes, '+') != null) case.event("literal plus");
var used: usize = 0;
for (bytes) |byte| {
if (byte == '%' or try case.boolean()) {
const alphabet = if (try case.boolean()) "0123456789ABCDEF" else "0123456789abcdef";
input[used..][0..3].* = .{ '%', alphabet[byte >> 4], alphabet[byte & 15] };
used += 3;
} else {
input[used] = byte;
used += 1;
}
}
try verify(case, input[0..used]);
case.event("structured valid");
const probes = [_][]const u8{ "%", "%2", "%+A", "%-0", "%_A", "%A_", "%GG", "%00", "%ff", "%2523", "+%2B" };
const start = try case.index(probes.len);
for (0..probes.len) |offset| {
const probe = probes[(start + offset) % probes.len];
const position = try case.int(usize, 0, used);
var mutated: [780]u8 = undefined;
@memcpy(mutated[0..position], input[0..position]);
@memcpy(mutated[position..][0..probe.len], probe);
@memcpy(mutated[position + probe.len ..][0 .. used - position], input[position..used]);
try verify(case, mutated[0 .. used + probe.len]);
}
case.event("all targeted probes");
try verify(case, bytes);
case.event("arbitrary bytes");
}
fn oracle(input: []const u8, out: []u8) ?[]const u8 {
var chunks = std.mem.splitScalar(u8, input, '%');
const head = chunks.next().?;
@memcpy(out[0..head.len], head);
var used = head.len;
while (chunks.next()) |chunk| {
if (chunk.len < 2 or !std.ascii.isHex(chunk[0]) or !std.ascii.isHex(chunk[1])) return null;
out[used] = std.fmt.parseInt(u8, chunk[0..2], 16) catch unreachable;
used += 1;
@memcpy(out[used..][0 .. chunk.len - 2], chunk[2..]);
used += chunk.len - 2;
}
return out[0..used];
}
fn verify(case: *hegel.TestCase, input: []const u8) !void {
case.note("input={x}", .{input});
var expected_buf: [780]u8 = undefined;
const expected = oracle(input, &expected_buf);
if (expected) |plain| {
const decoded = percent.decodeAlloc(std.testing.allocator, input) catch |err| {
if (err == error.InvalidPercentEncoding) return case.fail("valid input rejected");
return err;
};
defer std.testing.allocator.free(decoded);
if (!std.mem.eql(u8, plain, decoded)) return case.fail("decoded bytes differ");
if (!percent.decodedEql(input, plain)) return case.fail("equal bytes refused");
var longer: [781]u8 = undefined;
@memcpy(longer[0..plain.len], plain);
longer[plain.len] = 0;
if (percent.decodedEql(input, longer[0 .. plain.len + 1])) return case.fail("longer comparison accepted");
if (plain.len > 0) {
if (percent.decodedEql(input, plain[0 .. plain.len - 1])) return case.fail("shorter comparison accepted");
longer[plain.len / 2] ^= 1;
if (percent.decodedEql(input, longer[0..plain.len])) return case.fail("different comparison accepted");
}
} else {
if (percent.decodeAlloc(std.testing.allocator, input)) |allocated| {
std.testing.allocator.free(allocated);
return case.fail("malformed input accepted");
} else |err| {
if (err != error.InvalidPercentEncoding) return err;
}
if (percent.decodedEql(input, input) or percent.decodedEql(input, "")) return case.fail("malformed comparison accepted");
}
}