-- Unit checks for the two avatar email filters, run under a standalone Lua -- by t0504-email-avatar.sh with the script path as the first argument. The -- two scripts differ only in the service they point at, so the expected host -- is picked from the script's name. The second argument chooses how the -- openssl.digest dependency is met, stub swaps in the deterministic fake -- from the harness, real uses an installed luaossl and adds the one check -- only a genuine SHA-256 can pass, and missing makes the module fail to -- load. 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("", "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("", "A U Thor") h.contains("an icon is drawn for an address", out, " A U Thor") h.equals("close answers zero", ret, 0) h.check("the hash is hex", hash_of(out) ~= nil) -- The service hashes the trimmed lowercase address, so every spelling of one -- address has to reach it as the same hash. local plain = hash_of(render("author@example.com", "x")) local wrapped = hash_of(render("", "x")) local shouting = hash_of(render(" ", "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({ "", "commit" }, { "A U ", "Thor" }) h.contains("the name is buffered across writes", out, "> A U Thor") -- A commit can carry any bytes in its address, and a log page runs the -- filter once per row, so the trim has to stay linear in a run of spaces. local started = os.clock() render("", "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 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()