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
|
local test_directory = arg[0]:match("^(.*)/") or "."
local h = dofile(test_directory .. "/harness.lua")
local script = arg[1]
local mode = arg[2] or "stub"
local base_url = "https://gravatar.com/avatar/"
if script:find("libravatar", 1, true) then
base_url = "https://seccdn.libravatar.org/avatar/"
end
if mode == "stub" then
h.stub_digest()
elseif mode == "missing" then
h.preload_failure("openssl.digest")
end
h.load(script)
local function render(email, name)
return h.run({ email, "commit" }, { name })
end
if mode == "missing" then
local out, ret = render("<author@example.com>", "A U Thor")
h.equals("without luaossl the name stands alone", out, "A U Thor")
h.equals("and close still answers zero", ret, 0)
h.finish()
end
local function hash_of(output)
local prefix = base_url:gsub("([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
return output:match("src='" .. prefix .. "(%x+)%?")
end
local out, ret = render("<author@example.com>", "A U Thor")
h.contains("an icon is drawn for an address", out, "<img src='" .. base_url)
h.contains("the size lands in the query", out, "?s=13&d=retro'")
h.contains("a dense screen gets twice the size", out, "?s=26&d=retro 2x'")
h.contains("the icon is marked decorative", out, "alt='' width=")
h.contains("width matches the size", out, "width='13'")
h.contains("height matches the size", out, "height='13'")
h.contains("the fetch is deferred", out, "loading='lazy' decoding='async'")
h.contains("the name follows the icon", out, "> A U Thor")
h.equals("close answers zero", ret, 0)
h.check("the hash is hex", hash_of(out) ~= nil)
local plain = hash_of(render("author@example.com", "x"))
local wrapped = hash_of(render("<author@example.com>", "x"))
local shouting = hash_of(render(" <AUTHOR@Example.COM> ", "x"))
h.check("angle brackets do not change the hash", plain == wrapped)
h.check("case and padding do not change the hash", plain == shouting)
local other = hash_of(render("other@example.com", "x"))
h.check("a different address hashes differently", plain ~= other)
out = render(nil, "A U Thor")
h.equals("no address draws no icon", out, "A U Thor")
out = render("", "A U Thor")
h.equals("an empty address draws no icon", out, "A U Thor")
out = render("<>", "A U Thor")
h.equals("empty angle brackets draw no icon", out, "A U Thor")
out = h.run({ "<author@example.com>", "commit" }, { "A U ", "Thor" })
h.contains("the name is buffered across writes", out, "> A U Thor")
local started = os.clock()
render("<a" .. string.rep(" ", 40000) .. "b>", "x")
h.check("a long run of inner spaces is trimmed quickly", os.clock() - started < 0.5)
started = os.clock()
render(string.rep("<", 40000), "x")
h.check("a long run of open brackets is unwrapped quickly", os.clock() - started < 0.5)
h.check("the outermost brackets are the ones taken off",
hash_of(render("x <author@example.com> y", "x")) == plain)
if mode == "real" then
out = render("test@example.com", "x")
h.equals("a known address hashes to its sha256", hash_of(out),
"973dfe463ec85785f5f95af5ba3906eedb2d931c24e69824a89ea65dba4e813b")
end
h.finish()
|