Software /
code /
prosody
Annotate
spec/util_ringbuffer_spec.lua @ 12642:9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
We began moving away from simple "is this user an admin?" permission checks
before 0.12, with the introduction of mod_authz_internal and the ability to
dynamically change the roles of individual users.
The approach in 0.12 still had various limitations however, and apart from
the introduction of roles other than "admin" and the ability to pull that info
from storage, not much actually changed.
This new framework shakes things up a lot, though aims to maintain the same
functionality and behaviour on the surface for a default Prosody
configuration. That is, if you don't take advantage of any of the new
features, you shouldn't notice any change.
The biggest change visible to developers is that usermanager.is_admin() (and
the auth provider is_admin() method) have been removed. Gone. Completely.
Permission checks should now be performed using a new module API method:
module:may(action_name, context)
This method accepts an action name, followed by either a JID (string) or
(preferably) a table containing 'origin'/'session' and 'stanza' fields (e.g.
the standard object passed to most events). It will return true if the action
should be permitted, or false/nil otherwise.
Modules should no longer perform permission checks based on the role name.
E.g. a lot of code previously checked if the user's role was prosody:admin
before permitting some action. Since many roles might now exist with similar
permissions, and the permissions of prosody:admin may be redefined
dynamically, it is no longer suitable to use this method for permission
checks. Use module:may().
If you start an action name with ':' (recommended) then the current module's
name will automatically be used as a prefix.
To define a new permission, use the new module API:
module:default_permission(role_name, action_name)
module:default_permissions(role_name, { action_name[, action_name...] })
This grants the specified role permission to execute the named action(s) by
default. This may be overridden via other mechanisms external to your module.
The built-in roles that developers should use are:
- prosody:user (normal user)
- prosody:admin (host admin)
- prosody:operator (global admin)
The new prosody:operator role is intended for server-wide actions (such as
shutting down Prosody).
Finally, all usage of is_admin() in modules has been fixed by this commit.
Some of these changes were trickier than others, but no change is expected to
break existing deployments.
EXCEPT: mod_auth_ldap no longer supports the ldap_admin_filter option. It's
very possible nobody is using this, but if someone is then we can later update
it to pull roles from LDAP somehow.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 15 Jun 2022 12:15:01 +0100 |
parent | 10960:f84e0e2faae2 |
rev | line source |
---|---|
10897
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 local rb = require "util.ringbuffer"; |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 describe("util.ringbuffer", function () |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 describe("#new", function () |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 it("has a constructor", function () |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 assert.Function(rb.new); |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 end); |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 it("can be created", function () |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 assert.truthy(rb.new()); |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 end); |
10898
c6465fb3c839
util.ringbuffer: Prevent creation of zero-size buffer
Kim Alvefur <zash@zash.se>
parents:
10897
diff
changeset
|
10 it("won't create an empty buffer", function () |
c6465fb3c839
util.ringbuffer: Prevent creation of zero-size buffer
Kim Alvefur <zash@zash.se>
parents:
10897
diff
changeset
|
11 assert.has_error(function () |
c6465fb3c839
util.ringbuffer: Prevent creation of zero-size buffer
Kim Alvefur <zash@zash.se>
parents:
10897
diff
changeset
|
12 rb.new(0); |
c6465fb3c839
util.ringbuffer: Prevent creation of zero-size buffer
Kim Alvefur <zash@zash.se>
parents:
10897
diff
changeset
|
13 end); |
c6465fb3c839
util.ringbuffer: Prevent creation of zero-size buffer
Kim Alvefur <zash@zash.se>
parents:
10897
diff
changeset
|
14 end); |
10899
8048255ae61e
util.ringbuffer: Prevent creation of buffer with negative size
Kim Alvefur <zash@zash.se>
parents:
10898
diff
changeset
|
15 it("won't create a negatively sized buffer", function () |
8048255ae61e
util.ringbuffer: Prevent creation of buffer with negative size
Kim Alvefur <zash@zash.se>
parents:
10898
diff
changeset
|
16 assert.has_error(function () |
8048255ae61e
util.ringbuffer: Prevent creation of buffer with negative size
Kim Alvefur <zash@zash.se>
parents:
10898
diff
changeset
|
17 rb.new(-1); |
8048255ae61e
util.ringbuffer: Prevent creation of buffer with negative size
Kim Alvefur <zash@zash.se>
parents:
10898
diff
changeset
|
18 end); |
8048255ae61e
util.ringbuffer: Prevent creation of buffer with negative size
Kim Alvefur <zash@zash.se>
parents:
10898
diff
changeset
|
19 end); |
10897
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 end); |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 describe(":write", function () |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 local b = rb.new(); |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 it("works", function () |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 assert.truthy(b:write("hi")); |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 end); |
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 end); |
10949
8b5b35baf370
util.ringbuffer: Add test for :discard()
Matthew Wild <mwild1@gmail.com>
parents:
10901
diff
changeset
|
27 |
8b5b35baf370
util.ringbuffer: Add test for :discard()
Matthew Wild <mwild1@gmail.com>
parents:
10901
diff
changeset
|
28 describe(":discard", function () |
8b5b35baf370
util.ringbuffer: Add test for :discard()
Matthew Wild <mwild1@gmail.com>
parents:
10901
diff
changeset
|
29 local b = rb.new(); |
8b5b35baf370
util.ringbuffer: Add test for :discard()
Matthew Wild <mwild1@gmail.com>
parents:
10901
diff
changeset
|
30 it("works", function () |
8b5b35baf370
util.ringbuffer: Add test for :discard()
Matthew Wild <mwild1@gmail.com>
parents:
10901
diff
changeset
|
31 assert.truthy(b:write("hello world")); |
8b5b35baf370
util.ringbuffer: Add test for :discard()
Matthew Wild <mwild1@gmail.com>
parents:
10901
diff
changeset
|
32 assert.truthy(b:discard(6)); |
8b5b35baf370
util.ringbuffer: Add test for :discard()
Matthew Wild <mwild1@gmail.com>
parents:
10901
diff
changeset
|
33 assert.equal(5, #b); |
8b5b35baf370
util.ringbuffer: Add test for :discard()
Matthew Wild <mwild1@gmail.com>
parents:
10901
diff
changeset
|
34 assert.equal("world", b:read(5)); |
8b5b35baf370
util.ringbuffer: Add test for :discard()
Matthew Wild <mwild1@gmail.com>
parents:
10901
diff
changeset
|
35 end); |
8b5b35baf370
util.ringbuffer: Add test for :discard()
Matthew Wild <mwild1@gmail.com>
parents:
10901
diff
changeset
|
36 end); |
8b5b35baf370
util.ringbuffer: Add test for :discard()
Matthew Wild <mwild1@gmail.com>
parents:
10901
diff
changeset
|
37 |
10901
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
38 describe(":sub", function () |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
39 -- Helper function to compare buffer:sub() with string:sub() |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
40 local function test_sub(b, x, y) |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
41 local s = b:read(#b, true); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
42 local string_result, buffer_result = s:sub(x, y), b:sub(x, y); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
43 assert.equals(string_result, buffer_result, ("buffer:sub(%d, %s) does not match string:sub()"):format(x, y and ("%d"):format(y) or "nil")); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
44 end |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
45 |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
46 it("works", function () |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
47 local b = rb.new(); |
10954
fc310727adfb
util.ringbuffer: Add some additional asserts to tests
Matthew Wild <mwild1@gmail.com>
parents:
10953
diff
changeset
|
48 assert.truthy(b:write("hello world")); |
10901
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
49 assert.equals("hello", b:sub(1, 5)); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
50 end); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
51 |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
52 it("supports optional end parameter", function () |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
53 local b = rb.new(); |
10954
fc310727adfb
util.ringbuffer: Add some additional asserts to tests
Matthew Wild <mwild1@gmail.com>
parents:
10953
diff
changeset
|
54 assert.truthy(b:write("hello world")); |
10901
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
55 assert.equals("hello world", b:sub(1)); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
56 assert.equals("world", b:sub(-5)); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
57 end); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
58 |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
59 it("is equivalent to string:sub", function () |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
60 local b = rb.new(6); |
10954
fc310727adfb
util.ringbuffer: Add some additional asserts to tests
Matthew Wild <mwild1@gmail.com>
parents:
10953
diff
changeset
|
61 assert.truthy(b:write("foobar")); |
10901
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
62 b:read(3); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
63 b:write("foo"); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
64 for i = -13, 13 do |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
65 for j = -13, 13 do |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
66 test_sub(b, i, j); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
67 end |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
68 end |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
69 end); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
70 end); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
71 |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
72 describe(":byte", function () |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
73 -- Helper function to compare buffer:byte() with string:byte() |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
74 local function test_byte(b, x, y) |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
75 local s = b:read(#b, true); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
76 local string_result, buffer_result = {s:byte(x, y)}, {b:byte(x, y)}; |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
77 assert.same(string_result, buffer_result, ("buffer:byte(%d, %s) does not match string:byte()"):format(x, y and ("%d"):format(y) or "nil")); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
78 end |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
79 |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
80 it("is equivalent to string:byte", function () |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
81 local b = rb.new(6); |
10960
f84e0e2faae2
util.ringbuffer: Fix accidentally committed test change (thanks buildbot)
Matthew Wild <mwild1@gmail.com>
parents:
10954
diff
changeset
|
82 assert.truthy(b:write("foobar")); |
10901
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
83 b:read(3); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
84 b:write("foo"); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
85 test_byte(b, 1); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
86 test_byte(b, 3); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
87 test_byte(b, -1); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
88 test_byte(b, -3); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
89 for i = -13, 13 do |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
90 for j = -13, 13 do |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
91 test_byte(b, i, j); |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
92 end |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
93 end |
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
94 end); |
10953
c3b3ac63f4c3
util.ringbuffer: Ensure unsigned chars are always returned from :byte()
Matthew Wild <mwild1@gmail.com>
parents:
10949
diff
changeset
|
95 |
c3b3ac63f4c3
util.ringbuffer: Ensure unsigned chars are always returned from :byte()
Matthew Wild <mwild1@gmail.com>
parents:
10949
diff
changeset
|
96 it("works with characters > 127", function () |
c3b3ac63f4c3
util.ringbuffer: Ensure unsigned chars are always returned from :byte()
Matthew Wild <mwild1@gmail.com>
parents:
10949
diff
changeset
|
97 local b = rb.new(); |
c3b3ac63f4c3
util.ringbuffer: Ensure unsigned chars are always returned from :byte()
Matthew Wild <mwild1@gmail.com>
parents:
10949
diff
changeset
|
98 b:write(string.char(0, 140)); |
c3b3ac63f4c3
util.ringbuffer: Ensure unsigned chars are always returned from :byte()
Matthew Wild <mwild1@gmail.com>
parents:
10949
diff
changeset
|
99 local r = { b:byte(1, 2) }; |
c3b3ac63f4c3
util.ringbuffer: Ensure unsigned chars are always returned from :byte()
Matthew Wild <mwild1@gmail.com>
parents:
10949
diff
changeset
|
100 assert.same({ 0, 140 }, r); |
c3b3ac63f4c3
util.ringbuffer: Ensure unsigned chars are always returned from :byte()
Matthew Wild <mwild1@gmail.com>
parents:
10949
diff
changeset
|
101 end); |
10901
5e33926f4b43
util.ringbuffer: Add :sub() and :byte() methods equivalent to the string methods
Matthew Wild <mwild1@gmail.com>
parents:
10899
diff
changeset
|
102 end); |
10897
37df1e757f02
util.ringbuffer: Add some initial tests
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 end); |