-- 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, and real uses an installed luaossl and adds the one -- check only a genuine MD5 can pass. 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://www.gravatar.com/avatar/" local alt_text = "Gravatar" if script:find("libravatar", 1, true) then base_url = "https://seccdn.libravatar.org/avatar/" alt_text = "Libravatar" end if mode == "stub" then h.stub_digest() end h.load(script) local function render(email, name) return h.run({ email, "commit" }, { name }) 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") if mode == "real" then out = render("test@example.com", "x") h.equals("a known address hashes to its published md5", hash_of(out), "55502f40dc8b7c769880b10874abc9d0") end h.finish()