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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
local test_directory = arg[0]:match("^(.*)/") or "."
local h = dofile(test_directory .. "/harness.lua")
local script = arg[1]
local mode = h.mode("stub", "missing", "real")
local base_url = "https://gravatar.com/avatar/"
if script:match("[^/]*$"):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)
if mode == "missing" then
h.equals("a missing luaossl is named in the error log", h.error_log(),
"[cgit] Unable to load openssl.digest, so no avatar icons are drawn\n")
else
h.equals("with luaossl the error log stays empty", h.error_log(), "")
end
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 prefix = base_url:gsub("([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
local function hash_in(attribute, output)
return output:match(attribute .. "='" .. prefix .. "([0-9a-f]+)%?")
end
local function hash_of(output)
return hash_in("src", output)
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 fetch carries no Referer", out, "referrerpolicy='no-referrer'")
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)
h.equals("the dense image is of the same address", hash_in("srcset", out), hash_of(out))
if mode == "stub" then
h.equals("the hash is of the bare address in two hex digits a byte", hash_of(out),
"8318f94a3e09d48151cb849a783e6895")
end
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 ~= nil and 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", other ~= nil and 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")
out = render("<author@example.com>", "A & B")
h.contains("a name keeps the escaping cgit gave it", out, "> A & B")
h.excludes("and is not escaped again", out, "&amp;")
local function address_only(text)
return (render(text, "x"))
end
h.stays_linear("a long run of inner spaces is trimmed in linear time", address_only,
function(count) return "<a" .. string.rep(" ", count) .. "b>" end, 10000)
h.stays_linear("an address of open brackets is scanned in linear time", address_only,
function(count) return string.rep("<", count) end, 10000)
h.check("text around the brackets is dropped",
hash_of(render("x <author@example.com> y", "x")) == plain)
if mode == "stub" then
h.equals("the outermost brackets are the ones taken off", hash_of(render("<a <b> c>", "x")),
"4c40a2581c18aec944a8caa7000e17fa")
end
local reversed = hash_of(render("a>b<c", "x"))
h.check("a close before the open unwraps nothing", reversed ~= nil and reversed ~= 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()
|