From 1d3e32fe0251e40a26a1527308447613e23bf5d3 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Fri, 30 Mar 2018 09:34:34 +0100 Subject: [PATCH] [Test] Improve URL unit tests --- test/lua/unit/url.lua | 209 ++++++++++++++++++++------------------- test/rspamd_test_suite.c | 2 +- 2 files changed, 107 insertions(+), 104 deletions(-) diff --git a/test/lua/unit/url.lua b/test/lua/unit/url.lua index 879386bba..ccde222f8 100644 --- a/test/lua/unit/url.lua +++ b/test/lua/unit/url.lua @@ -15,91 +15,93 @@ context("URL check functions", function() ffi.C.rspamd_url_init(string.format('%s/%s', test_dir, "test_tld.dat")) - test("Extract urls from text", function() - local pool = mpool.create() - local cases = { - {"test.com", {"test.com", nil}}, - {" test.com", {"test.com", nil}}, - {" text", {"test.com", nil}}, - {"test.com. text", {"test.com", nil}}, - {"mailto:A.User@example.com text", {"example.com", "A.User"}}, - {"http://Тест.Рф:18 text", {"тест.рф", nil}}, - {"http://user:password@тест2.РФ:18 text", {"тест2.рф", "user"}}, - {"somebody@example.com", {"example.com", "somebody"}}, - {"https://127.0.0.1/abc text", {"127.0.0.1", nil}}, - {"https://127.0.0.1 text", {"127.0.0.1", nil}}, - {"https://[::1]:1", {"::1", nil}}, - {"https://user:password@[::1]:1", {"::1", nil}}, - {"https://user:password@[::1]", {"::1", nil}}, - {"https://user:password@[::1]/1", {"::1", nil}}, - } + local pool = mpool.create() - for _,c in ipairs(cases) do - local res = url.create(pool, c[1]) + local cases = { + {"test.com", {"test.com", nil}}, + {" test.com", {"test.com", nil}}, + {" text", {"test.com", nil}}, + {"test.com. text", {"test.com", nil}}, + {"mailto:A.User@example.com text", {"example.com", "A.User"}}, + {"http://Тест.Рф:18 text", {"тест.рф", nil}}, + {"http://user:password@тест2.РФ:18 text", {"тест2.рф", "user"}}, + {"somebody@example.com", {"example.com", "somebody"}}, + {"https://127.0.0.1/abc text", {"127.0.0.1", nil}}, + {"https://127.0.0.1 text", {"127.0.0.1", nil}}, + {"https://[::1]:1", {"::1", nil}}, + {"https://user:password@[::1]:1", {"::1", nil}}, + {"https://user:password@[::1]", {"::1", nil}}, + {"https://user:password@[::1]/1", {"::1", nil}}, + } + for i,c in ipairs(cases) do + local res = url.create(pool, c[1]) + + test("Extract urls from text" .. i, function() assert_not_nil(res, "cannot parse " .. c[1]) local t = res:to_table() --local s = logger.slog("%1 -> %2", c[1], t) --print(s) assert_not_nil(t, "cannot convert to table " .. c[1]) - assert_equal(c[2][1], t['host']) + assert_equal(c[2][1], t['host'], + logger.slog('expected host "%s", but got "%s" in url %s => %s', + c[2][1], t['host'], c[1], t)) if c[2][2] then - assert_equal(c[2][2], t['user']) + assert_equal(c[2][1], t['host'], + logger.slog('expected user "%s", but got "%s" in url %s => %s', + c[2][1], t['host'], c[1], t)) end - end - pool:destroy() - end) + end) + end - -- Some cases from https://code.google.com/p/google-url/source/browse/trunk/src/url_canon_unittest.cc - test("Parse urls", function() - local pool = mpool.create() - -- input, parseable, {host, port, user, password, path, query, part} - local cases = { - {"http://%30%78%63%30%2e%30%32%35%30.01", true, { --0xc0.0250.01 - host = '192.168.0.1', - }}, - {"http://www.google.com/foo?bar=baz#", true, { - host = 'www.google.com', path = 'foo', query = 'bar=baz', tld = 'google.com' - }}, - {"http://[www.google.com]/", false}, - {"", false}, - {",test.com text", false}, - {"ht\ttp:@www.google.com:80/;p?#", false}, - {"http://user:pass@/", false}, - {"http://foo:-80/", false}, - {"http:////////user:@google.com:99?foo", true, { - host = 'google.com', user = 'user', port = 99, query = 'foo' - }}, - {"http://%25DOMAIN:foobar@foodomain.com/", true, { - host = 'foodomain.com', user = '%25DOMAIN' - }}, - {"http://0.0xFFFFFF", true, { - host = '0.255.255.255' - }}, - {"http://030052000001", true, { - host = '192.168.0.1' - }}, - {"http://0xc0.052000001", true, { - host = '192.168.0.1' - }}, - {"http://192.168.0.1.", true, { - host = '192.168.0.1' - }}, - {"http://[::eeee:192.168.0.1]", true, { - host = '::eeee:c0a8:1' - }}, - {"http://twitter.com#test", true, { - host = 'twitter.com', fragment = 'test' - }}, - } + cases = { + {"http://%30%78%63%30%2e%30%32%35%30.01", true, { --0xc0.0250.01 + host = '192.168.0.1', + }}, + {"http://www.google.com/foo?bar=baz#", true, { + host = 'www.google.com', path = 'foo', query = 'bar=baz', tld = 'google.com' + }}, + {"http://[www.google.com]/", false}, + {"", false}, + {",test.com text", false}, + {"ht\ttp:@www.google.com:80/;p?#", false}, + {"http://user:pass@/", false}, + {"http://foo:-80/", false}, + {"http:////////user:@google.com:99?foo", true, { + host = 'google.com', user = 'user', port = 99, query = 'foo' + }}, + {"http://%25DOMAIN:foobar@foodomain.com/", true, { + host = 'foodomain.com', user = '%25DOMAIN' + }}, + {"http://0.0xFFFFFF", true, { + host = '0.255.255.255' + }}, + {"http://030052000001", true, { + host = '192.168.0.1' + }}, + {"http://0xc0.052000001", true, { + host = '192.168.0.1' + }}, + {"http://192.168.0.1.", true, { + host = '192.168.0.1' + }}, + {"http://[::eeee:192.168.0.1]", true, { + host = '::eeee:c0a8:1' + }}, + {"http://twitter.com#test", true, { + host = 'twitter.com', fragment = 'test' + }}, + } - for _,c in ipairs(cases) do - local res = url.create(pool, c[1]) + -- Some cases from https://code.google.com/p/google-url/source/browse/trunk/src/url_canon_unittest.cc + for i,c in ipairs(cases) do + local res = url.create(pool, c[1]) + test("Parse urls " .. i, function() if c[2] then assert_not_nil(res, "cannot parse " .. c[1]) @@ -107,7 +109,8 @@ context("URL check functions", function() for k,v in pairs(c[3]) do assert_not_nil(uf[k], k .. ' is missing in url, must be ' .. v) - assert_equal(uf[k], v, 'expected ' .. v .. ' for ' .. k .. ' but got ' .. uf[k] .. ' in url ' .. c[1]) + assert_equal(uf[k], v, logger.slog('expected "%s", for %s, but got "%s" in url %s => %s', + v, k, uf[k], c[1], uf)) end for k,v in pairs(uf) do if k ~= 'url' and k ~= 'protocol' and k ~= 'tld' then @@ -117,42 +120,42 @@ context("URL check functions", function() else assert_nil(res, "should not parse " .. c[1] .. ' parsed to: ' .. tostring(res)) end - end + end) end - ) - test("Normalize paths", function() - local cases = { - {"/././foo", "/foo"}, - {"/a/b/c/./../../g", "/a/g"}, - {"/./.foo", "/.foo"}, - {"/foo/.", "/foo"}, - {"/foo/./", "/foo"}, - {"/foo/bar/..", "/foo"}, - {"/foo/bar/../", "/foo/"}, - {"/foo/..bar", "/foo/..bar"}, - {"/foo/bar/../ton", "/foo/ton"}, - {"/foo/bar/../ton/../../a", "/a"}, - {"/foo/../../..", "/"}, - {"/foo/../../../ton", "/ton"}, - {"////../..", "/"}, - {"./", ""}, - {"/./", "/"}, - {"/./././././././", "/"}, - {"/", "/"}, - {"/a/b", "/a/b"}, - {"/a/b/", "/a/b/"}, - {"..", "/"}, - {"/../", "/"}, - {"../", "/"}, - } - for _,v in ipairs(cases) do + cases = { + {"/././foo", "/foo"}, + {"/a/b/c/./../../g", "/a/g"}, + {"/./.foo", "/.foo"}, + {"/foo/.", "/foo"}, + {"/foo/./", "/foo"}, + {"/foo/bar/..", "/foo"}, + {"/foo/bar/../", "/foo/"}, + {"/foo/..bar", "/foo/..bar"}, + {"/foo/bar/../ton", "/foo/ton"}, + {"/foo/bar/../ton/../../a", "/a"}, + {"/foo/../../..", "/"}, + {"/foo/../../../ton", "/ton"}, + {"////../..", "/"}, + {"./", ""}, + {"/./", "/"}, + {"/./././././././", "/"}, + {"/", "/"}, + {"/a/b", "/a/b"}, + {"/a/b/", "/a/b/"}, + {"..", "/"}, + {"/../", "/"}, + {"../", "/"}, + } + + for i,v in ipairs(cases) do + test("Normalize paths " .. i, function() local buf = ffi.new("uint8_t[?]", #v[1]) local sizbuf = ffi.new("size_t[1]") ffi.copy(buf, v[1], #v[1]) ffi.C.rspamd_http_normalize_path_inplace(buf, #v[1], sizbuf) local res = ffi.string(buf, tonumber(sizbuf[0])) assert_equal(v[2], res, 'expected ' .. v[2] .. ' but got ' .. res .. ' in path ' .. v[1]) - end - end) + end) + end end) diff --git a/test/rspamd_test_suite.c b/test/rspamd_test_suite.c index f3586e4f5..d6fa237a3 100644 --- a/test/rspamd_test_suite.c +++ b/test/rspamd_test_suite.c @@ -19,7 +19,7 @@ main (int argc, char **argv) rspamd_main->cfg = cfg; cfg->cfg_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL); cfg->log_type = RSPAMD_LOG_CONSOLE; - cfg->log_level = G_LOG_LEVEL_INFO; + cfg->log_level = G_LOG_LEVEL_MESSAGE; rspamd_set_logger (cfg, g_quark_from_static_string("rspamd-test"), &rspamd_main->logger, rspamd_main->server_pool); -- 2.39.5