util.encodings: Spell out all IDNA 2008 options ICU has
[prosody.git] / spec / core_storagemanager_spec.lua
blobfd2f8742eb6e3a90aad5bc2bbc3e9345de431c13
1 local unpack = table.unpack or unpack; -- luacheck: ignore 113
2 local server = require "net.server_select";
3 package.loaded["net.server"] = server;
5 local st = require "util.stanza";
7 local function mock_prosody()
8 _G.prosody = {
9 core_post_stanza = function () end;
10 events = require "util.events".new();
11 hosts = {};
12 paths = {
13 data = "./data";
16 end
18 local configs = {
19 memory = {
20 storage = "memory";
22 internal = {
23 storage = "internal";
25 sqlite = {
26 storage = "sql";
27 sql = { driver = "SQLite3", database = "prosody-tests.sqlite" };
29 mysql = {
30 storage = "sql";
31 sql = { driver = "MySQL", database = "prosody", username = "prosody", password = "secret", host = "localhost" };
33 postgres = {
34 storage = "sql";
35 sql = { driver = "PostgreSQL", database = "prosody", username = "prosody", password = "secret", host = "localhost" };
39 local test_host = "storage-unit-tests.invalid";
41 describe("storagemanager", function ()
42 for backend, backend_config in pairs(configs) do
43 local tagged_name = "#"..backend;
44 if backend ~= backend_config.storage then
45 tagged_name = tagged_name.." #"..backend_config.storage;
46 end
47 insulate(tagged_name.." #storage backend", function ()
48 mock_prosody();
50 local config = require "core.configmanager";
51 local sm = require "core.storagemanager";
52 local hm = require "core.hostmanager";
53 local mm = require "core.modulemanager";
55 -- Simple check to ensure insulation is working correctly
56 assert.is_nil(config.get(test_host, "storage"));
58 for k, v in pairs(backend_config) do
59 config.set(test_host, k, v);
60 end
61 assert(hm.activate(test_host, {}));
62 sm.initialize_host(test_host);
63 assert(mm.load(test_host, "storage_"..backend_config.storage));
65 describe("key-value stores", function ()
66 -- These tests rely on being executed in order, disable any order
67 -- randomization for this block
68 randomize(false);
70 local store;
71 it("may be opened", function ()
72 store = assert(sm.open(test_host, "test"));
73 end);
75 local simple_data = { foo = "bar" };
77 it("may set data for a user", function ()
78 assert(store:set("user9999", simple_data));
79 end);
81 it("may get data for a user", function ()
82 assert.same(simple_data, assert(store:get("user9999")));
83 end);
85 it("may remove data for a user", function ()
86 assert(store:set("user9999", nil));
87 local ret, err = store:get("user9999");
88 assert.is_nil(ret);
89 assert.is_nil(err);
90 end);
91 end);
93 describe("archive stores", function ()
94 randomize(false);
96 local archive;
97 it("can be opened", function ()
98 archive = assert(sm.open(test_host, "test-archive", "archive"));
99 end);
101 local test_stanza = st.stanza("test", { xmlns = "urn:example:foo" })
102 :tag("foo"):up()
103 :tag("foo"):up();
104 local test_time = 1539204123;
106 local test_data = {
107 { nil, test_stanza, test_time, "contact@example.com" };
108 { nil, test_stanza, test_time+1, "contact2@example.com" };
109 { nil, test_stanza, test_time+2, "contact2@example.com" };
110 { nil, test_stanza, test_time-1, "contact2@example.com" };
113 it("can be added to", function ()
114 for _, data_item in ipairs(test_data) do
115 local ok = archive:append("user", unpack(data_item, 1, 4));
116 assert.truthy(ok);
118 end);
120 describe("can be queried", function ()
121 it("for all items", function ()
122 local data, err = archive:find("user", {});
123 assert.truthy(data);
124 local count = 0;
125 for id, item, when in data do
126 count = count + 1;
127 assert.truthy(id);
128 assert(st.is_stanza(item));
129 assert.equal("test", item.name);
130 assert.equal("urn:example:foo", item.attr.xmlns);
131 assert.equal(2, #item.tags);
132 assert.equal(test_data[count][3], when);
134 assert.equal(#test_data, count);
135 end);
137 it("by JID", function ()
138 local data, err = archive:find("user", {
139 with = "contact@example.com";
141 assert.truthy(data);
142 local count = 0;
143 for id, item, when in data do
144 count = count + 1;
145 assert.truthy(id);
146 assert(st.is_stanza(item));
147 assert.equal("test", item.name);
148 assert.equal("urn:example:foo", item.attr.xmlns);
149 assert.equal(2, #item.tags);
150 assert.equal(test_time, when);
152 assert.equal(1, count);
153 end);
155 it("by time (end)", function ()
156 local data, err = archive:find("user", {
157 ["end"] = test_time;
159 assert.truthy(data);
160 local count = 0;
161 for id, item, when in data do
162 count = count + 1;
163 assert.truthy(id);
164 assert(st.is_stanza(item));
165 assert.equal("test", item.name);
166 assert.equal("urn:example:foo", item.attr.xmlns);
167 assert.equal(2, #item.tags);
168 assert(test_time >= when);
170 assert.equal(2, count);
171 end);
173 it("by time (start)", function ()
174 local data, err = archive:find("user", {
175 ["start"] = test_time;
177 assert.truthy(data);
178 local count = 0;
179 for id, item, when in data do
180 count = count + 1;
181 assert.truthy(id);
182 assert(st.is_stanza(item));
183 assert.equal("test", item.name);
184 assert.equal("urn:example:foo", item.attr.xmlns);
185 assert.equal(2, #item.tags);
186 assert(test_time <= when);
188 assert.equal(#test_data -1, count);
189 end);
191 it("by time (start+end)", function ()
192 local data, err = archive:find("user", {
193 ["start"] = test_time;
194 ["end"] = test_time+1;
196 assert.truthy(data);
197 local count = 0;
198 for id, item, when in data do
199 count = count + 1;
200 assert.truthy(id);
201 assert(st.is_stanza(item));
202 assert.equal("test", item.name);
203 assert.equal("urn:example:foo", item.attr.xmlns);
204 assert.equal(2, #item.tags);
205 assert(when >= test_time, ("%d >= %d"):format(when, test_time));
206 assert(when <= test_time+1, ("%d <= %d"):format(when, test_time+1));
208 assert.equal(2, count);
209 end);
210 end);
212 it("can selectively delete items", function ()
213 local delete_id;
215 local data = assert(archive:find("user", {}));
216 local count = 0;
217 for id, item, when in data do --luacheck: ignore 213/item 213/when
218 count = count + 1;
219 if count == 2 then
220 delete_id = id;
222 assert.truthy(id);
224 assert.equal(#test_data, count);
227 assert(archive:delete("user", { key = delete_id }));
230 local data = assert(archive:find("user", {}));
231 local count = 0;
232 for id, item, when in data do --luacheck: ignore 213/item 213/when
233 count = count + 1;
234 assert.truthy(id);
235 assert.not_equal(delete_id, id);
237 assert.equal(#test_data-1, count);
239 end);
241 it("can be purged", function ()
242 local ok, err = archive:delete("user");
243 assert.truthy(ok);
244 local data, err = archive:find("user", {
245 with = "contact@example.com";
247 assert.truthy(data);
248 local count = 0;
249 for id, item, when in data do -- luacheck: ignore id item when
250 count = count + 1;
252 assert.equal(0, count);
253 end);
255 it("can truncate the oldest items", function ()
256 local username = "user-truncate";
257 for i = 1, 10 do
258 assert(archive:append(username, nil, test_stanza, i, "contact@example.com"));
260 assert(archive:delete(username, { truncate = 3 }));
263 local data = assert(archive:find(username, {}));
264 local count = 0;
265 for id, item, when in data do --luacheck: ignore 213/when
266 count = count + 1;
267 assert.truthy(id);
268 assert(st.is_stanza(item));
269 assert(when > 7, ("%d > 7"):format(when));
271 assert.equal(3, count);
273 end);
275 it("overwrites existing keys with new data", function ()
276 local prefix = ("a"):rep(50);
277 local username = "user-overwrite";
278 assert(archive:append(username, prefix.."-1", test_stanza, test_time, "contact@example.com"));
279 assert(archive:append(username, prefix.."-2", test_stanza, test_time, "contact@example.com"));
282 local data = assert(archive:find(username, {}));
283 local count = 0;
284 for id, item, when in data do --luacheck: ignore 213/when
285 count = count + 1;
286 assert.truthy(id);
287 assert.equals(("%s-%d"):format(prefix, count), id);
288 assert(st.is_stanza(item));
290 assert.equal(2, count);
293 local new_stanza = st.clone(test_stanza);
294 new_stanza.attr.foo = "bar";
295 assert(archive:append(username, prefix.."-2", new_stanza, test_time+1, "contact2@example.com"));
298 local data = assert(archive:find(username, {}));
299 local count = 0;
300 for id, item, when in data do
301 count = count + 1;
302 assert.truthy(id);
303 assert.equals(("%s-%d"):format(prefix, count), id);
304 assert(st.is_stanza(item));
305 if count == 2 then
306 assert.equals(test_time+1, when);
307 assert.equals("bar", item.attr.foo);
310 assert.equal(2, count);
312 end);
314 it("can contain multiple long unique keys #issue1073", function ()
315 local prefix = ("a"):rep(50);
316 assert(archive:append("user-issue1073", prefix.."-1", test_stanza, test_time, "contact@example.com"));
317 assert(archive:append("user-issue1073", prefix.."-2", test_stanza, test_time, "contact@example.com"));
319 local data = assert(archive:find("user-issue1073", {}));
320 local count = 0;
321 for id, item, when in data do --luacheck: ignore 213/when
322 count = count + 1;
323 assert.truthy(id);
324 assert(st.is_stanza(item));
326 assert.equal(2, count);
327 assert(archive:delete("user-issue1073"));
328 end);
329 end);
330 end);
332 end);