Skip to content

Commit e77a95c

Browse files
committed
[refactor] Cleanup up SecurityManager XQuery Module Test code
1 parent 6cd5844 commit e77a95c

File tree

7 files changed

+465
-388
lines changed

7 files changed

+465
-388
lines changed

exist-core/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,7 @@
724724
<include>src/test/java/org/exist/xquery/functions/fn/ParsingFunctionsTest.java</include>
725725
<include>src/test/java/org/exist/xquery/functions/fn/transform/ConvertTest.java</include>
726726
<include>src/test/java/org/exist/xquery/functions/fn/transform/FunTransformITTest.java</include>
727+
<include>src/test/java/org/exist/xquery/functions/securitymanager/SecurityManagerTestUtil.java</include>
727728
<include>src/main/java/org/exist/xquery/functions/system/FunctionAvailable.java</include>
728729
<include>src/test/java/org/exist/xquery/functions/xmldb/XMLDBStoreTest.java</include>
729730
<include>src/test/java/org/exist/xquery/functions/xquery3/SerializeTest.java</include>
@@ -1095,6 +1096,11 @@
10951096
<include>src/main/java/org/exist/xquery/functions/fn/transform/Transform.java</include>
10961097
<include>src/main/java/org/exist/xquery/functions/fn/transform/TreeUtils.java</include>
10971098
<include>src/main/java/org/exist/xquery/functions/integer/WordPicture.java</include>
1099+
<include>src/test/java/org/exist/xquery/functions/securitymanager/GetPermissionsTest.java</include>
1100+
<include>src/test/java/org/exist/xquery/functions/securitymanager/GroupManagementFunctionRemoveGroupTest.java</include>
1101+
<include>src/test/java/org/exist/xquery/functions/securitymanager/GroupMembershipFunctionRemoveGroupMemberTest.java</include>
1102+
<include>src/test/java/org/exist/xquery/functions/securitymanager/PermissionsFunctionChmodTest.java</include>
1103+
<include>src/test/java/org/exist/xquery/functions/securitymanager/PermissionsFunctionChownTest.java</include>
10981104
<include>src/main/java/org/exist/xquery/functions/system/GetUptime.java</include>
10991105
<include>src/main/java/org/exist/xquery/functions/system/Shutdown.java</include>
11001106
<include>src/main/java/org/exist/xquery/functions/system/SystemModule.java</include>
@@ -1615,6 +1621,12 @@
16151621
<exclude>src/main/java/org/exist/xquery/functions/fn/transform/TreeUtils.java</exclude>
16161622
<exclude>src/main/java/org/exist/xquery/functions/integer/WordPicture.java</exclude>
16171623
<exclude>src/main/java/org/exist/xquery/functions/map/MapType.java</exclude>
1624+
<exclude>src/test/java/org/exist/xquery/functions/securitymanager/GetPermissionsTest.java</exclude>
1625+
<exclude>src/test/java/org/exist/xquery/functions/securitymanager/GroupManagementFunctionRemoveGroupTest.java</exclude>
1626+
<exclude>src/test/java/org/exist/xquery/functions/securitymanager/GroupMembershipFunctionRemoveGroupMemberTest.java</exclude>
1627+
<exclude>src/test/java/org/exist/xquery/functions/securitymanager/PermissionsFunctionChmodTest.java</exclude>
1628+
<exclude>src/test/java/org/exist/xquery/functions/securitymanager/PermissionsFunctionChownTest.java</exclude>
1629+
<exclude>src/test/java/org/exist/xquery/functions/securitymanager/SecurityManagerTestUtil.java</exclude>
16181630
<exclude>src/test/java/org/exist/xquery/functions/session/AbstractSessionTest.java</exclude>
16191631
<exclude>src/test/java/org/exist/xquery/functions/session/AttributeTest.java</exclude>
16201632
<exclude>src/main/java/org/exist/xquery/functions/system/FunctionAvailable.java</exclude>

exist-core/src/test/java/org/exist/xquery/functions/securitymanager/GetPermissionsTest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -23,7 +47,6 @@
2347

2448
import org.exist.EXistException;
2549
import org.exist.dom.memtree.ElementImpl;
26-
import org.exist.dom.memtree.NodeImpl;
2750
import org.exist.security.PermissionDeniedException;
2851
import org.exist.security.internal.SecurityManagerImpl;
2952
import org.exist.storage.BrokerPool;

exist-core/src/test/java/org/exist/xquery/functions/securitymanager/GroupManagementFunctionRemoveGroupTest.java

Lines changed: 28 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -21,23 +45,19 @@
2145
*/
2246
package org.exist.xquery.functions.securitymanager;
2347

24-
import com.evolvedbinary.j8fu.function.Runnable3E;
2548
import org.exist.EXistException;
2649
import org.exist.security.*;
2750
import org.exist.security.SecurityManager;
28-
import org.exist.security.internal.aider.GroupAider;
29-
import org.exist.security.internal.aider.UserAider;
3051
import org.exist.storage.BrokerPool;
3152
import org.exist.storage.DBBroker;
3253
import org.exist.storage.txn.Txn;
3354
import org.exist.test.ExistEmbeddedServer;
3455
import org.exist.xquery.XPathException;
35-
import org.exist.xquery.XQuery;
36-
import org.exist.xquery.value.Sequence;
3756
import org.junit.*;
3857

3958
import java.util.Optional;
4059

60+
import static org.exist.xquery.functions.securitymanager.SecurityManagerTestUtil.*;
4161
import static org.junit.Assert.*;
4262

4363
public class GroupManagementFunctionRemoveGroupTest {
@@ -56,21 +76,21 @@ public class GroupManagementFunctionRemoveGroupTest {
5676
@Test(expected = PermissionDeniedException.class)
5777
public void cannotDeleteDbaGroup() throws XPathException, PermissionDeniedException, EXistException {
5878
extractPermissionDenied(() -> {
59-
xqueryRemoveGroup(SecurityManager.DBA_GROUP);
79+
xqueryRemoveGroup(existWebServer.getBrokerPool(), SecurityManager.DBA_GROUP);
6080
});
6181
}
6282

6383
@Test(expected = PermissionDeniedException.class)
6484
public void cannotDeleteGuestGroup() throws XPathException, PermissionDeniedException, EXistException {
6585
extractPermissionDenied(() -> {
66-
xqueryRemoveGroup(SecurityManager.GUEST_GROUP);
86+
xqueryRemoveGroup(existWebServer.getBrokerPool(), SecurityManager.GUEST_GROUP);
6787
});
6888
}
6989

7090
@Test(expected = PermissionDeniedException.class)
7191
public void cannotDeleteUnknownGroup() throws XPathException, PermissionDeniedException, EXistException {
7292
extractPermissionDenied(() -> {
73-
xqueryRemoveGroup(SecurityManager.UNKNOWN_GROUP);
93+
xqueryRemoveGroup(existWebServer.getBrokerPool(), SecurityManager.UNKNOWN_GROUP);
7494
});
7595
}
7696

@@ -264,69 +284,4 @@ public void deleteUsersSharingPersonalPrimaryGroup() throws PermissionDeniedExce
264284
transaction.commit();
265285
}
266286
}
267-
268-
private static Account createUser(final DBBroker broker, final SecurityManager sm, final String username, final String password) throws PermissionDeniedException, EXistException {
269-
Group userGroup = new GroupAider(username);
270-
sm.addGroup(broker, userGroup);
271-
final Account user = new UserAider(username);
272-
user.setPassword(password);
273-
user.setPrimaryGroup(userGroup);
274-
sm.addAccount(user);
275-
276-
userGroup = sm.getGroup(username);
277-
userGroup.addManager(sm.getAccount(username));
278-
sm.updateGroup(userGroup);
279-
280-
return user;
281-
}
282-
283-
private static Group createGroup(final DBBroker broker, final SecurityManager sm, final String groupName) throws PermissionDeniedException, EXistException {
284-
final Group otherGroup = new GroupAider(groupName);
285-
return sm.addGroup(broker, otherGroup);
286-
}
287-
288-
private static void addUserToGroup(final SecurityManager sm, final Account user, final Group group) throws PermissionDeniedException, EXistException {
289-
user.addGroup(group.getName());
290-
sm.updateAccount(user);
291-
}
292-
293-
private static void setPrimaryGroup(final SecurityManager sm, final Account user, final Group group) throws PermissionDeniedException, EXistException {
294-
user.setPrimaryGroup(group);
295-
sm.updateAccount(user);
296-
}
297-
298-
private static void removeUser(final SecurityManager sm, final String username) throws PermissionDeniedException, EXistException {
299-
sm.deleteAccount(username);
300-
removeGroup(sm, username);
301-
}
302-
303-
private static void removeGroup(final SecurityManager sm, final String groupname) throws PermissionDeniedException, EXistException {
304-
sm.deleteGroup(groupname);
305-
}
306-
307-
private Sequence xqueryRemoveGroup(final String groupname) throws EXistException, PermissionDeniedException, XPathException {
308-
final BrokerPool pool = existWebServer.getBrokerPool();
309-
310-
final String query =
311-
"import module namespace sm = 'http://exist-db.org/xquery/securitymanager';\n" +
312-
"sm:remove-group('" + groupname + "')";
313-
314-
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
315-
final XQuery xquery = existWebServer.getBrokerPool().getXQueryService();
316-
final Sequence result = xquery.execute(broker, query, null);
317-
return result;
318-
}
319-
}
320-
321-
private static void extractPermissionDenied(final Runnable3E<XPathException, PermissionDeniedException, EXistException> runnable) throws XPathException, PermissionDeniedException, EXistException {
322-
try {
323-
runnable.run();
324-
} catch (final XPathException e) {
325-
if (e.getCause() != null && e.getCause() instanceof PermissionDeniedException) {
326-
throw (PermissionDeniedException)e.getCause();
327-
} else {
328-
throw e;
329-
}
330-
}
331-
}
332287
}
Lines changed: 33 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -21,26 +45,22 @@
2145
*/
2246
package org.exist.xquery.functions.securitymanager;
2347

24-
import com.evolvedbinary.j8fu.function.Runnable3E;
2548
import org.exist.EXistException;
2649
import org.exist.TestUtils;
2750
import org.exist.security.*;
2851
import org.exist.security.SecurityManager;
29-
import org.exist.security.internal.aider.GroupAider;
30-
import org.exist.security.internal.aider.UserAider;
3152
import org.exist.storage.BrokerPool;
3253
import org.exist.storage.DBBroker;
3354
import org.exist.storage.txn.Txn;
3455
import org.exist.test.ExistEmbeddedServer;
3556
import org.exist.xquery.XPathException;
36-
import org.exist.xquery.XQuery;
37-
import org.exist.xquery.value.Sequence;
3857
import org.junit.Before;
3958
import org.junit.Rule;
4059
import org.junit.Test;
4160

4261
import java.util.Optional;
4362

63+
import static org.exist.xquery.functions.securitymanager.SecurityManagerTestUtil.*;
4464
import static org.junit.Assert.*;
4565

4666
public class GroupMembershipFunctionRemoveGroupMemberTest {
@@ -59,9 +79,9 @@ public void cannotRemoveAllGroupsFromUserAsOwner() throws XPathException, Permis
5979
final BrokerPool pool = existWebServer.getBrokerPool();
6080
final Subject owner = pool.getSecurityManager().authenticate(USER1_NAME, USER1_NAME);
6181
extractPermissionDenied(() -> {
62-
xqueryRemoveUserFromGroup(USER1_NAME, OTHER_GROUP2_NAME, Optional.of(owner));
63-
xqueryRemoveUserFromGroup(USER1_NAME, OTHER_GROUP1_NAME, Optional.of(owner));
64-
xqueryRemoveUserFromGroup(USER1_NAME, USER1_NAME, Optional.of(owner));
82+
xqueryRemoveUserFromGroup(pool, USER1_NAME, OTHER_GROUP2_NAME, Optional.of(owner));
83+
xqueryRemoveUserFromGroup(pool, USER1_NAME, OTHER_GROUP1_NAME, Optional.of(owner));
84+
xqueryRemoveUserFromGroup(pool, USER1_NAME, USER1_NAME, Optional.of(owner));
6585
});
6686
}
6787

@@ -70,9 +90,9 @@ public void cannotRemoveAllGroupsFromUserAsDBA() throws XPathException, Permissi
7090
final BrokerPool pool = existWebServer.getBrokerPool();
7191
final Subject admin = pool.getSecurityManager().authenticate(TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD);
7292
extractPermissionDenied(() -> {
73-
xqueryRemoveUserFromGroup(USER1_NAME, OTHER_GROUP2_NAME, Optional.of(admin));
74-
xqueryRemoveUserFromGroup(USER1_NAME, OTHER_GROUP1_NAME, Optional.of(admin));
75-
xqueryRemoveUserFromGroup(USER1_NAME, USER1_NAME, Optional.of(admin));
93+
xqueryRemoveUserFromGroup(pool, USER1_NAME, OTHER_GROUP2_NAME, Optional.of(admin));
94+
xqueryRemoveUserFromGroup(pool, USER1_NAME, OTHER_GROUP1_NAME, Optional.of(admin));
95+
xqueryRemoveUserFromGroup(pool, USER1_NAME, USER1_NAME, Optional.of(admin));
7696
});
7797
}
7898

@@ -88,11 +108,11 @@ public void setup() throws EXistException, PermissionDeniedException, XPathExcep
88108

89109
final Group otherGroup1 = createGroup(broker, sm, OTHER_GROUP1_NAME);
90110
addUserToGroup(sm, user1, otherGroup1);
91-
addUserAsGroupManager(USER1_NAME, OTHER_GROUP1_NAME);
111+
xqueryAddUserAsGroupManager(pool, USER1_NAME, OTHER_GROUP1_NAME);
92112

93113
final Group otherGroup2 = createGroup(broker, sm, OTHER_GROUP2_NAME);
94114
addUserToGroup(sm, user1, otherGroup2);
95-
addUserAsGroupManager(USER1_NAME, OTHER_GROUP2_NAME);
115+
xqueryAddUserAsGroupManager(pool, USER1_NAME, OTHER_GROUP2_NAME);
96116

97117
transaction.commit();
98118
}
@@ -110,75 +130,4 @@ public void setup() throws EXistException, PermissionDeniedException, XPathExcep
110130
transaction.commit();
111131
}
112132
}
113-
114-
private Sequence xqueryRemoveUserFromGroup(final String username, final String groupname) throws XPathException, PermissionDeniedException, EXistException {
115-
final BrokerPool pool = existWebServer.getBrokerPool();
116-
final Optional<Subject> asUser = Optional.of(pool.getSecurityManager().getSystemSubject());
117-
return xqueryRemoveUserFromGroup(username, groupname, asUser);
118-
}
119-
120-
private Sequence xqueryRemoveUserFromGroup(final String username, final String groupname, final Optional<Subject> asUser) throws EXistException, PermissionDeniedException, XPathException {
121-
final BrokerPool pool = existWebServer.getBrokerPool();
122-
123-
final String query =
124-
"import module namespace sm = 'http://exist-db.org/xquery/securitymanager';\n" +
125-
"sm:remove-group-member('" + groupname + "', '" + username + "')";
126-
127-
try (final DBBroker broker = pool.get(asUser)) {
128-
final XQuery xquery = existWebServer.getBrokerPool().getXQueryService();
129-
final Sequence result = xquery.execute(broker, query, null);
130-
return result;
131-
}
132-
}
133-
134-
private Sequence addUserAsGroupManager(final String username, final String groupname) throws EXistException, PermissionDeniedException, XPathException {
135-
final BrokerPool pool = existWebServer.getBrokerPool();
136-
137-
final String query =
138-
"import module namespace sm = 'http://exist-db.org/xquery/securitymanager';\n" +
139-
"sm:add-group-manager('" + groupname + "', '" + username + "')";
140-
141-
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
142-
final XQuery xquery = existWebServer.getBrokerPool().getXQueryService();
143-
final Sequence result = xquery.execute(broker, query, null);
144-
return result;
145-
}
146-
}
147-
148-
private static Account createUser(final DBBroker broker, final SecurityManager sm, final String username, final String password) throws PermissionDeniedException, EXistException {
149-
Group userGroup = new GroupAider(username);
150-
sm.addGroup(broker, userGroup);
151-
final Account user = new UserAider(username);
152-
user.setPassword(password);
153-
user.setPrimaryGroup(userGroup);
154-
sm.addAccount(user);
155-
156-
userGroup = sm.getGroup(username);
157-
userGroup.addManager(sm.getAccount(username));
158-
sm.updateGroup(userGroup);
159-
160-
return user;
161-
}
162-
163-
private static Group createGroup(final DBBroker broker, final SecurityManager sm, final String groupName) throws PermissionDeniedException, EXistException {
164-
final Group otherGroup = new GroupAider(groupName);
165-
return sm.addGroup(broker, otherGroup);
166-
}
167-
168-
private static void addUserToGroup(final SecurityManager sm, final Account user, final Group group) throws PermissionDeniedException, EXistException {
169-
user.addGroup(group.getName());
170-
sm.updateAccount(user);
171-
}
172-
173-
private static void extractPermissionDenied(final Runnable3E<XPathException, PermissionDeniedException, EXistException> runnable) throws XPathException, PermissionDeniedException, EXistException {
174-
try {
175-
runnable.run();
176-
} catch (final XPathException e) {
177-
if (e.getCause() != null && e.getCause() instanceof PermissionDeniedException) {
178-
throw (PermissionDeniedException)e.getCause();
179-
} else {
180-
throw e;
181-
}
182-
}
183-
}
184133
}

0 commit comments

Comments
 (0)