Debian Codegraph @

test/lua/unit/base64.lua

raw
1 context("Base64 encoding", function()
2 local ok, ffi = pcall(require, "ffi")
3 if not ok then
4 ffi = require("cffi")
5 end
6 local util = require("rspamd_util")
7 local logger = require "rspamd_logger"
8 ffi.cdef[[
9 void rspamd_cryptobox_init (void);
10 void ottery_rand_bytes(void *buf, size_t n);
11 unsigned ottery_rand_unsigned(void);
12 unsigned char* g_base64_decode (const char *in, size_t *outlen);
13 char * rspamd_encode_base64 (const unsigned char *in, size_t inlen,
14 size_t str_len, size_t *outlen);
15 void g_free(void *ptr);
16 int memcmp(const void *a1, const void *a2, size_t len);
17 double base64_test (bool generic, size_t niters, size_t len, size_t str_len);
18 double rspamd_get_ticks (int);
19 ]]
20
21 ffi.C.rspamd_cryptobox_init()
22
23 local function random_buf(max_size)
24 local l = ffi.C.ottery_rand_unsigned() % max_size + 1
25 local buf = ffi.new("unsigned char[?]", l)
26 ffi.C.ottery_rand_bytes(buf, l)
27
28 return buf, l
29 end
30
31 local function random_safe_buf(max_size)
32 local l = ffi.C.ottery_rand_unsigned() % max_size + 1
33 local buf = ffi.new("unsigned char[?]", l)
34
35 for i = 0,l-1 do
36 buf[i] = ffi.C.ottery_rand_unsigned() % 20 + string.byte('A')
37 end
38
39 buf[l - 1] = 0;
40
41 return buf, l
42 end
43
44 test("Base64 encode test", function()
45 local cases = {
46 {"", ""},
47 {"f", "Zg=="},
48 {"fo", "Zm8="},
49 {"foo", "Zm9v"},
50 {"foob", "Zm9vYg=="},
51 {"fooba", "Zm9vYmE="},
52 {"foobar", "Zm9vYmFy"},
53 }
54
55 local nl = ffi.new("size_t [1]")
56 for _,c in ipairs(cases) do
57 local inp = ffi.new("unsigned char[?]", #c[1] + 1)
58 ffi.copy(inp, c[1])
59 local b = ffi.C.rspamd_encode_base64(inp, #c[1], 0, nl)
60 local s = ffi.string(b)
61 ffi.C.g_free(b)
62 assert_equal(s, c[2], s .. " not equal " .. c[2])
63 end
64 end)
65
66 test("Base64 decode test", function()
67 local cases = {
68 {"", ""},
69 {"f", "Zg=="},
70 {"fo", "Zm8="},
71 {"foo", "Zm9v"},
72 {"foob", "Zm9vYg=="},
73 {"fooba", "Zm9vYmE="},
74 {"foobar", "Zm9vYmFy"},
75 }
76
77 for _,c in ipairs(cases) do
78 local b = tostring(util.decode_base64(c[2]))
79 assert_equal(b, c[1], b .. " not equal " .. c[1])
80 end
81 end)
82
83 test("Base64 line split encode test", function()
84 local text = [[
85 Man is distinguished, not only by his reason, but by this singular passion from
86 other animals, which is a lust of the mind, that by a perseverance of delight
87 in the continued and indefatigable generation of knowledge, exceeds the short
88 vehemence of any carnal pleasure.]]
89 local b64 = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\r\nIHNpbmd1bGFyIHBhc3Npb24gZnJvbQpvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\r\ndGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodAppbiB0aGUgY29udGlu\r\ndWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\r\nZSBzaG9ydAp2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4="
90 local nl = ffi.new("size_t [1]")
91 local inp = ffi.new("unsigned char[?]", #text + 1)
92 ffi.copy(inp, text)
93 local b = ffi.C.rspamd_encode_base64(inp, #text, 76, nl)
94 local cmp = ffi.C.memcmp(b, b64, nl[0])
95 ffi.C.g_free(b)
96 assert_equal(cmp, 0)
97 end)
98
99 if os.getenv("RSPAMD_LUA_EXPENSIVE_TESTS") then
100 test("Base64 fuzz test", function()
101 for i = 1,1000 do
102 local b, l = random_safe_buf(4096)
103 local lim = ffi.C.ottery_rand_unsigned() % 64 + 10
104 local orig = ffi.string(b)
105 local ben = util.encode_base64(orig, lim)
106 local dec = util.decode_base64(ben)
107 assert_equal(orig, tostring(dec), "fuzz test failed for length: " .. #orig)
108 end
109 end)
110 test("Base64 fuzz test (ffi)", function()
111 for i = 1,1000 do
112 local b, l = random_buf(4096)
113 local nl = ffi.new("size_t [1]")
114 local lim = ffi.C.ottery_rand_unsigned() % 64 + 10
115 local ben = ffi.C.rspamd_encode_base64(b, l, lim, nl)
116 local bs = ffi.string(ben)
117 local ol = ffi.new("size_t [1]")
118 local nb = ffi.C.g_base64_decode(ben, ol)
119
120 local cmp = ffi.C.memcmp(b, nb, l)
121 ffi.C.g_free(ben)
122 ffi.C.g_free(nb)
123 assert_equal(cmp, 0, "fuzz test failed for length: " .. tostring(l))
124 end
125 end)
126
127 local speed_iters = 10000
128
129 local function perform_base64_speed_test(chunk, is_reference, line_len)
130 local ticks = ffi.C.base64_test(is_reference, speed_iters, chunk, line_len)
131 local what = 'Optimized'
132 if is_reference then
133 what = 'Reference'
134 end
135 logger.messagex("%s base64 %s chunk (%s line len): %s ticks per iter, %s ticks per byte",
136 what, chunk, line_len,
137 ticks / speed_iters, ticks / speed_iters / chunk)
138
139 return 1
140 end
141 test("Base64 test reference vectors 78", function()
142 local res = perform_base64_speed_test(78, true, 0)
143 assert_not_equal(res, 0)
144 end)
145 test("Base64 test optimized vectors 78", function()
146 local res = perform_base64_speed_test(78, false, 0)
147 assert_not_equal(res, 0)
148 end)
149
150 test("Base64 test reference vectors 512", function()
151 local res = perform_base64_speed_test(512, true, 0)
152 assert_not_equal(res, 0)
153 end)
154 test("Base64 test optimized vectors 512", function()
155 local res = perform_base64_speed_test(512, false, 0)
156 assert_not_equal(res, 0)
157 end)
158 test("Base64 test reference vectors 512 (78 line len)", function()
159 local res = perform_base64_speed_test(512, true, 78)
160 assert_not_equal(res, 0)
161 end)
162 test("Base64 test optimized vectors 512 (78 line len)", function()
163 local res = perform_base64_speed_test(512, false, 78)
164 assert_not_equal(res, 0)
165 end)
166
167 test("Base64 test reference vectors 1K", function()
168 local res = perform_base64_speed_test(1024, true, 0)
169 assert_not_equal(res, 0)
170 end)
171 test("Base64 test optimized vectors 1K", function()
172 local res = perform_base64_speed_test(1024, false, 0)
173 assert_not_equal(res, 0)
174 end)
175 test("Base64 test reference vectors 1K (78 line len)", function()
176 local res = perform_base64_speed_test(1024, true, 78)
177 assert_not_equal(res, 0)
178 end)
179 test("Base64 test optimized vectors 1K (78 line len)", function()
180 local res = perform_base64_speed_test(1024, false, 78)
181 assert_not_equal(res, 0)
182 end)
183
184 test("Base64 test reference vectors 10K", function()
185 local res = perform_base64_speed_test(10 * 1024, true, 0)
186 assert_not_equal(res, 0)
187 end)
188 test("Base64 test optimized vectors 10K", function()
189 local res = perform_base64_speed_test(10 * 1024, false, 0)
190 assert_not_equal(res, 0)
191 end)
192 test("Base64 test reference vectors 10K (78 line len)", function()
193 local res = perform_base64_speed_test(10 * 1024, true, 78)
194 assert_not_equal(res, 0)
195 end)
196 test("Base64 test optimized vectors 10K (78 line len)", function()
197 local res = perform_base64_speed_test(10 * 1024, false, 78)
198 assert_not_equal(res, 0)
199 end)
200 end
201 end)