Skip to content

Commit e843227

Browse files
committed
[refactor] Cleanup up SecurityManager XQuery Module Test code
1 parent a33e34e commit e843227

File tree

7 files changed

+471
-387
lines changed

7 files changed

+471
-387
lines changed

exist-core/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,7 @@
773773
<include>src/test/java/org/exist/xquery/functions/fn/ParsingFunctionsTest.java</include>
774774
<include>src/test/java/org/exist/xquery/functions/fn/transform/ConvertTest.java</include>
775775
<include>src/test/java/org/exist/xquery/functions/fn/transform/FunTransformITTest.java</include>
776+
<include>src/test/java/org/exist/xquery/functions/securitymanager/SecurityManagerTestUtil.java</include>
776777
<include>src/main/java/org/exist/xquery/functions/system/FunctionAvailable.java</include>
777778
<include>src/test/java/org/exist/xquery/functions/xmldb/XMLDBStoreTest.java</include>
778779
<include>src/test/java/org/exist/xquery/functions/xquery3/SerializeTest.java</include>
@@ -1147,6 +1148,11 @@
11471148
<include>src/main/java/org/exist/xquery/functions/fn/transform/Transform.java</include>
11481149
<include>src/main/java/org/exist/xquery/functions/fn/transform/TreeUtils.java</include>
11491150
<include>src/main/java/org/exist/xquery/functions/integer/WordPicture.java</include>
1151+
<include>src/test/java/org/exist/xquery/functions/securitymanager/GetPermissionsTest.java</include>
1152+
<include>src/test/java/org/exist/xquery/functions/securitymanager/GroupManagementFunctionRemoveGroupTest.java</include>
1153+
<include>src/test/java/org/exist/xquery/functions/securitymanager/GroupMembershipFunctionRemoveGroupMemberTest.java</include>
1154+
<include>src/test/java/org/exist/xquery/functions/securitymanager/PermissionsFunctionChmodTest.java</include>
1155+
<include>src/test/java/org/exist/xquery/functions/securitymanager/PermissionsFunctionChownTest.java</include>
11501156
<include>src/main/java/org/exist/xquery/functions/system/GetUptime.java</include>
11511157
<include>src/main/java/org/exist/xquery/functions/system/Shutdown.java</include>
11521158
<include>src/main/java/org/exist/xquery/functions/system/SystemModule.java</include>
@@ -1682,6 +1688,12 @@
16821688
<exclude>src/main/java/org/exist/xquery/functions/fn/transform/TreeUtils.java</exclude>
16831689
<exclude>src/main/java/org/exist/xquery/functions/integer/WordPicture.java</exclude>
16841690
<exclude>src/main/java/org/exist/xquery/functions/map/MapType.java</exclude>
1691+
<exclude>src/test/java/org/exist/xquery/functions/securitymanager/GetPermissionsTest.java</exclude>
1692+
<exclude>src/test/java/org/exist/xquery/functions/securitymanager/GroupManagementFunctionRemoveGroupTest.java</exclude>
1693+
<exclude>src/test/java/org/exist/xquery/functions/securitymanager/GroupMembershipFunctionRemoveGroupMemberTest.java</exclude>
1694+
<exclude>src/test/java/org/exist/xquery/functions/securitymanager/PermissionsFunctionChmodTest.java</exclude>
1695+
<exclude>src/test/java/org/exist/xquery/functions/securitymanager/PermissionsFunctionChownTest.java</exclude>
1696+
<exclude>src/test/java/org/exist/xquery/functions/securitymanager/SecurityManagerTestUtil.java</exclude>
16851697
<exclude>src/test/java/org/exist/xquery/functions/session/AbstractSessionTest.java</exclude>
16861698
<exclude>src/test/java/org/exist/xquery/functions/session/AttributeTest.java</exclude>
16871699
<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 & 0 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
*

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

Lines changed: 34 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 {
@@ -55,17 +75,23 @@ public class GroupManagementFunctionRemoveGroupTest {
5575

5676
@Test(expected = PermissionDeniedException.class)
5777
public void cannotDeleteDbaGroup() throws XPathException, PermissionDeniedException, EXistException {
58-
extractPermissionDenied(() -> xqueryRemoveGroup(SecurityManager.DBA_GROUP));
78+
extractPermissionDenied(() -> {
79+
xqueryRemoveGroup(existWebServer.getBrokerPool(), SecurityManager.DBA_GROUP);
80+
});
5981
}
6082

6183
@Test(expected = PermissionDeniedException.class)
6284
public void cannotDeleteGuestGroup() throws XPathException, PermissionDeniedException, EXistException {
63-
extractPermissionDenied(() -> xqueryRemoveGroup(SecurityManager.GUEST_GROUP));
85+
extractPermissionDenied(() -> {
86+
xqueryRemoveGroup(existWebServer.getBrokerPool(), SecurityManager.GUEST_GROUP);
87+
});
6488
}
6589

6690
@Test(expected = PermissionDeniedException.class)
6791
public void cannotDeleteUnknownGroup() throws XPathException, PermissionDeniedException, EXistException {
68-
extractPermissionDenied(() -> xqueryRemoveGroup(SecurityManager.UNKNOWN_GROUP));
92+
extractPermissionDenied(() -> {
93+
xqueryRemoveGroup(existWebServer.getBrokerPool(), SecurityManager.UNKNOWN_GROUP);
94+
});
6995
}
7096

7197
@Test
@@ -258,69 +284,4 @@ public void deleteUsersSharingPersonalPrimaryGroup() throws PermissionDeniedExce
258284
transaction.commit();
259285
}
260286
}
261-
262-
private static Account createUser(final DBBroker broker, final SecurityManager sm, final String username, final String password) throws PermissionDeniedException, EXistException {
263-
Group userGroup = new GroupAider(username);
264-
sm.addGroup(broker, userGroup);
265-
final Account user = new UserAider(username);
266-
user.setPassword(password);
267-
user.setPrimaryGroup(userGroup);
268-
sm.addAccount(user);
269-
270-
userGroup = sm.getGroup(username);
271-
userGroup.addManager(sm.getAccount(username));
272-
sm.updateGroup(userGroup);
273-
274-
return user;
275-
}
276-
277-
private static Group createGroup(final DBBroker broker, final SecurityManager sm, final String groupName) throws PermissionDeniedException, EXistException {
278-
final Group otherGroup = new GroupAider(groupName);
279-
return sm.addGroup(broker, otherGroup);
280-
}
281-
282-
private static void addUserToGroup(final SecurityManager sm, final Account user, final Group group) throws PermissionDeniedException, EXistException {
283-
user.addGroup(group.getName());
284-
sm.updateAccount(user);
285-
}
286-
287-
private static void setPrimaryGroup(final SecurityManager sm, final Account user, final Group group) throws PermissionDeniedException, EXistException {
288-
user.setPrimaryGroup(group);
289-
sm.updateAccount(user);
290-
}
291-
292-
private static void removeUser(final SecurityManager sm, final String username) throws PermissionDeniedException, EXistException {
293-
sm.deleteAccount(username);
294-
removeGroup(sm, username);
295-
}
296-
297-
private static void removeGroup(final SecurityManager sm, final String groupname) throws PermissionDeniedException, EXistException {
298-
sm.deleteGroup(groupname);
299-
}
300-
301-
private Sequence xqueryRemoveGroup(final String groupname) throws EXistException, PermissionDeniedException, XPathException {
302-
final BrokerPool pool = existWebServer.getBrokerPool();
303-
304-
final String query =
305-
"import module namespace sm = 'http://exist-db.org/xquery/securitymanager';\n" +
306-
"sm:remove-group('" + groupname + "')";
307-
308-
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
309-
final XQuery xquery = existWebServer.getBrokerPool().getXQueryService();
310-
final Sequence result = xquery.execute(broker, query, null);
311-
return result;
312-
}
313-
}
314-
315-
private static void extractPermissionDenied(final Runnable3E<XPathException, PermissionDeniedException, EXistException> runnable) throws XPathException, PermissionDeniedException, EXistException {
316-
try {
317-
runnable.run();
318-
} catch (final XPathException e) {
319-
if (e.getCause() != null && e.getCause() instanceof PermissionDeniedException) {
320-
throw (PermissionDeniedException)e.getCause();
321-
} else {
322-
throw e;
323-
}
324-
}
325-
}
326287
}
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)