Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
************************************************************************
**** C A N A D I A N A S T R O N O M Y D A T A C E N T R E *****
*
* (c) 2014. (c) 2014.
* National Research Council Conseil national de recherches
* Ottawa, Canada, K1A 0R6 Ottawa, Canada, K1A 0R6
* All rights reserved Tous droits reserves
*
* NRC disclaims any warranties Le CNRC denie toute garantie
* expressed, implied, or statu- enoncee, implicite ou legale,
* tory, of any kind with respect de quelque nature que se soit,
* to the software, including concernant le logiciel, y com-
* without limitation any war- pris sans restriction toute
* ranty of merchantability or garantie de valeur marchande
* fitness for a particular pur- ou de pertinence pour un usage
* pose. NRC shall not be liable particulier. Le CNRC ne
* in any event for any damages, pourra en aucun cas etre tenu
* whether direct or indirect, responsable de tout dommage,
* special or general, consequen- direct ou indirect, particul-
* tial or incidental, arising ier ou general, accessoire ou
* from the use of the software. fortuit, resultant de l'utili-
* sation du logiciel.
*
*
* @author adriand
*
* @version $Revision: $
*
*
**** C A N A D I A N A S T R O N O M Y D A T A C E N T R E *****
************************************************************************
*/
package ca.nrc.cadc.ac.server.ldap;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.security.AccessControlException;
import java.util.Collection;
import java.util.Set;
import javax.security.auth.Subject;
import javax.security.auth.x500.X500Principal;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.BeforeClass;
Jeff Burke
committed
import ca.nrc.cadc.ac.GroupAlreadyExistsException;
Jeff Burke
committed
import ca.nrc.cadc.ac.UserNotFoundException;
import ca.nrc.cadc.util.Log4jInit;
private static final Logger log = Logger.getLogger(LdapGroupDAOTest.class);
Jeff Burke
committed
static String server = "mach275.cadc.dao.nrc.ca";
static int port = 389;
static String adminDN = "uid=webproxy,ou=webproxy,ou=topologymanagement,o=netscaperoot";
static String adminPW = "go4it";
static String usersDN = "ou=Users,ou=ds,dc=canfartest,dc=net";
static String groupsDN = "ou=Groups,ou=ds,dc=canfartest,dc=net";
static String adminGroupsDN = "ou=adminGroups,ou=ds,dc=canfartest,dc=net";
//static String usersDN = "ou=Users,ou=ds,dc=canfar,dc=net";
//static String groupsDN = "ou=Groups,ou=ds,dc=canfar,dc=net";
Jeff Burke
committed
static String daoTestDN1 = "cn=cadcdaotest1,ou=cadc,o=hia,c=ca";
static String daoTestDN2 = "cn=cadcdaotest2,ou=cadc,o=hia,c=ca";
static String unknownDN = "cn=foo,ou=cadc,o=hia,c=ca";
static X500Principal daoTestPrincipal1;
static X500Principal daoTestPrincipal2;
static X500Principal unknownPrincipal;
static X500Principal adminPrincipal;
static User<X500Principal> daoTestUser1;
static User<X500Principal> daoTestUser2;
static User<X500Principal> unknownUser;
static User<X500Principal> adminUser;
static Subject authSubject;
static Subject anonSubject;
static LdapConfig config;
@BeforeClass
public static void setUpBeforeClass()
throws Exception
{
Log4jInit.setLevel("ca.nrc.cadc.ac", Level.DEBUG);
Jeff Burke
committed
daoTestPrincipal1 = new X500Principal(daoTestDN1);
daoTestPrincipal2 = new X500Principal(daoTestDN2);
unknownPrincipal = new X500Principal(unknownDN);
adminPrincipal = new X500Principal(adminDN);
Jeff Burke
committed
daoTestUser1 = new User<X500Principal>(daoTestPrincipal1);
daoTestUser2 = new User<X500Principal>(daoTestPrincipal2);
unknownUser = new User<X500Principal>(unknownPrincipal);
adminUser = new User<X500Principal>(adminPrincipal);
authSubject = new Subject();
authSubject.getPrincipals().add(daoTestUser1.getUserID());
anonSubject = new Subject();
anonSubject.getPrincipals().add(unknownUser.getUserID());
config = new LdapConfig(server, port, adminDN, adminPW, usersDN, groupsDN, adminGroupsDN);
LdapGroupDAO<X500Principal> getGroupDAO()
{
return new LdapGroupDAO<X500Principal>(config,
new LdapUserDAO<X500Principal>(config));
}
Jeff Burke
committed
return "CadcDaoTestGroup-" + System.currentTimeMillis();
public void testOneGroup() throws Exception
{
// do everything as owner
Jeff Burke
committed
Subject.doAs(authSubject, new PrivilegedExceptionAction<Object>()
{
public Object run() throws Exception
{
try
{
Jeff Burke
committed
Group expectGroup = new Group(getGroupID(), daoTestUser1);
Group actualGroup = getGroupDAO().addGroup(expectGroup);
Jeff Burke
committed
Group otherGroup = new Group(getGroupID(), daoTestUser1);
otherGroup = getGroupDAO().addGroup(otherGroup);
// modify group fields
// description
expectGroup.description = "Happy testing";
actualGroup = getGroupDAO().modifyGroup(expectGroup);
assertGroupsEqual(expectGroup, actualGroup);
expectGroup.description = null;
actualGroup = getGroupDAO().modifyGroup(expectGroup);
assertGroupsEqual(expectGroup, actualGroup);
Jeff Burke
committed
expectGroup.getUserMembers().add(daoTestUser2);
actualGroup = getGroupDAO().modifyGroup(expectGroup);
assertGroupsEqual(expectGroup, actualGroup);
Jeff Burke
committed
expectGroup.getUserMembers().remove(daoTestUser2);
actualGroup = getGroupDAO().modifyGroup(expectGroup);
assertGroupsEqual(expectGroup, actualGroup);
// groupMembers
expectGroup.getGroupMembers().add(otherGroup);
actualGroup = getGroupDAO().modifyGroup(expectGroup);
assertGroupsEqual(expectGroup, actualGroup);
expectGroup.getGroupMembers().remove(otherGroup);
actualGroup = getGroupDAO().modifyGroup(expectGroup);
assertGroupsEqual(expectGroup, actualGroup);
Jeff Burke
committed
expectGroup.description = "Happy testing";
expectGroup.getUserMembers().add(daoTestUser2);
expectGroup.getGroupMembers().add(otherGroup);
getGroupDAO().deleteGroup(expectGroup.getID());
try
{
getGroupDAO().getGroup(expectGroup.getID());
fail("get on deleted group should throw exception");
}
catch (GroupNotFoundException ignore) {}
// reactivate the group
actualGroup = getGroupDAO().addGroup(expectGroup);
assertTrue(actualGroup instanceof ActivatedGroup);
// get the activated group
actualGroup = getGroupDAO().getGroup(expectGroup.getID());
Jeff Burke
committed
// delete the group
getGroupDAO().deleteGroup(expectGroup.getID());
throw new Exception("Problems", e);
}
}
});
}
Jeff Burke
committed
// TODO: add test passing in groupID
@Test
public void testSearchOwnerGroups() throws Exception
{
// do everything as owner
Jeff Burke
committed
Subject.doAs(authSubject, new PrivilegedExceptionAction<Object>()
{
public Object run() throws Exception
{
try
{
Jeff Burke
committed
Group testGroup = new Group(getGroupID(), daoTestUser1);
testGroup = getGroupDAO().addGroup(testGroup);
getGroupDAO().getGroups(daoTestUser1.getUserID(),
Jeff Burke
committed
boolean found = false;
for (Group group : groups)
{
Jeff Burke
committed
if (!group.getOwner().equals(daoTestUser1))
{
fail("returned group with wrong owner");
}
Jeff Burke
committed
if (group.getID().equals(group.getID()))
{
found = true;
}
}
if (!found)
{
Jeff Burke
committed
fail("Group for owner not found");
Jeff Burke
committed
getGroupDAO().deleteGroup(testGroup.getID());
}
catch (Exception e)
{
throw new Exception("Problems", e);
}
return null;
}
});
}
Jeff Burke
committed
// TODO: add test passing in groupID
// @Test
public void testSearchMemberGroups() throws Exception
{
// do everything as owner
Jeff Burke
committed
Subject.doAs(authSubject, new PrivilegedExceptionAction<Object>()
{
public Object run() throws Exception
{
try
Jeff Burke
committed
{
Group expectedGroup = new Group("CadcDaoTestGroup1");
Jeff Burke
committed
getGroupDAO().getGroups(daoTestUser2.getUserID(),
Jeff Burke
committed
log.debug("# groups found: " + groups.size());
boolean found = false;
for (Group group : groups)
{
log.debug("found test group: " + group.getID());
Set<Group> members = group.getGroupMembers();
Jeff Burke
committed
log.debug("#test group members: " + members.size());
for (Group member : members)
{
if (member.equals(expectedGroup))
Jeff Burke
committed
{
Jeff Burke
committed
}
}
}
if (!found)
{
fail("Group member not found");
}
}
catch (Exception e)
{
throw new Exception("Problems", e);
}
return null;
}
});
}
Jeff Burke
committed
// TODO: add test passing in groupID
// @Test
public void testSearchRWGroups() throws Exception
{
// do everything as owner
Jeff Burke
committed
Subject.doAs(authSubject, new PrivilegedExceptionAction<Object>()
{
public Object run() throws Exception
{
try
Jeff Burke
committed
{
Group expectedGroup = new Group("CadcDaoTestGroup1");
Jeff Burke
committed
getGroupDAO().getGroups(daoTestUser2.getUserID(),
Role.RW, null);
System.out.println("# groups found: " + groups.size());
Jeff Burke
committed
boolean found = false;
for (Group group : groups)
{
System.out.println("found group: " + group.getID());
// get the group to get the owner
// (not returned for RW groups)
group = getGroupDAO().getGroup(group.getID());
if (!group.getOwner().equals(daoTestUser2))
{
fail("returned group with wrong owner");
}
if (group.equals(expectedGroup))
Jeff Burke
committed
{
found = true;
}
}
if (!found)
{
fail("");
}
}
catch (Exception e)
{
throw new Exception("Problems", e);
}
return null;
}
});
}
Jeff Burke
committed
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
@Test
public void testAddGroupExceptions() throws Exception
{
Subject.doAs(anonSubject, new PrivilegedExceptionAction<Object>()
{
public Object run() throws Exception
{
try
{
getGroupDAO().addGroup(new Group(getGroupID(), daoTestUser1));
fail("addGroup with anonymous access should throw " +
"AccessControlException");
}
catch (AccessControlException ignore) {}
return null;
}
});
Subject.doAs(authSubject, new PrivilegedExceptionAction<Object>()
{
public Object run() throws Exception
{
try
{
getGroupDAO().addGroup(new Group("foo", unknownUser));
fail("addGroup with unknown user should throw " +
"AccessControlException");
Jeff Burke
committed
}
catch (AccessControlException ignore) {}
Jeff Burke
committed
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
Group group = getGroupDAO().addGroup(new Group(getGroupID(),
daoTestUser1));
try
{
getGroupDAO().addGroup(group);
fail("addGroup with existing group should throw " +
"GroupAlreadyExistsException");
}
catch (GroupAlreadyExistsException ignore) {}
getGroupDAO().deleteGroup(group.getID());
return null;
}
});
}
@Test
public void testGetGroupExceptions() throws Exception
{
final String groupID = getGroupID();
Subject.doAs(authSubject, new PrivilegedExceptionAction<Object>()
{
public Object run() throws Exception
{
try
{
getGroupDAO().getGroup(groupID);
fail("getGroup with unknown group should throw " +
"GroupNotFoundException");
}
catch (GroupNotFoundException ignore) {}
getGroupDAO().addGroup(new Group(groupID, daoTestUser1));
return null;
}
});
Subject.doAs(anonSubject, new PrivilegedExceptionAction<Object>()
{
public Object run() throws Exception
{
try
{
getGroupDAO().getGroup(groupID);
fail("getGroup with anonymous access should throw " +
"AccessControlException");
}
catch (AccessControlException ignore) {}
return null;
}
});
Subject.doAs(authSubject, new PrivilegedExceptionAction<Object>()
{
public Object run() throws Exception
{
getGroupDAO().deleteGroup(groupID);
return null;
}
});
}
@Test
public void testModifyGroupExceptions() throws Exception
{
final String groupID = getGroupID();
Subject.doAs(authSubject, new PrivilegedExceptionAction<Object>()
{
public Object run() throws Exception
{
getGroupDAO().addGroup(new Group(groupID, daoTestUser1));
// try
// {
// getGroupDAO().modifyGroup(new Group(groupID, unknownUser));
// fail("modifyGroup with unknown user should throw " +
// "UserNotFoundException");
// }
// catch (UserNotFoundException ignore) {}
try
{
getGroupDAO().modifyGroup(new Group("foo", daoTestUser1));
fail("modifyGroup with unknown user should throw " +
"GroupNotFoundException");
}
catch (GroupNotFoundException ignore) {}
return null;
}
});
Subject.doAs(anonSubject, new PrivilegedExceptionAction<Object>()
{
public Object run() throws Exception
{
try
{
getGroupDAO().getGroup(groupID);
fail("getGroup with anonymous access should throw " +
"AccessControlException");
}
catch (AccessControlException ignore) {}
return null;
}
});
Subject.doAs(authSubject, new PrivilegedExceptionAction<Object>()
{
public Object run() throws Exception
{
getGroupDAO().deleteGroup(groupID);
return null;
}
});
}
@Test
public void testDeleteGroupExceptions() throws Exception
{
final String groupID = getGroupID();
Subject.doAs(authSubject, new PrivilegedExceptionAction<Object>()
{
public Object run() throws Exception
{
try
{
getGroupDAO().deleteGroup(groupID);
fail("deleteGroup with unknown group should throw " +
"GroupNotFoundException");
}
catch (GroupNotFoundException ignore) {}
getGroupDAO().addGroup(new Group(groupID, daoTestUser1));
return null;
}
});
Subject.doAs(anonSubject, new PrivilegedExceptionAction<Object>()
{
public Object run() throws Exception
{
try
{
getGroupDAO().deleteGroup(groupID);
fail("deleteGroup with anonymous access should throw " +
"AccessControlException");
}
catch (AccessControlException ignore) {}
return null;
}
});
Subject.doAs(authSubject, new PrivilegedExceptionAction<Object>()
{
public Object run() throws Exception
{
getGroupDAO().deleteGroup(groupID);
return null;
}
});
}
@Test
public void testSearchGroupsExceptions() throws Exception
{
final String groupID = getGroupID();
Subject.doAs(authSubject, new PrivilegedExceptionAction<Object>()
{
public Object run() throws Exception
{
getGroupDAO().addGroup(new Group(groupID, daoTestUser1));
try
{
getGroupDAO().getGroups(unknownPrincipal, Role.OWNER,
Jeff Burke
committed
groupID);
fail("searchGroups with unknown user should throw " +
"UserNotFoundException");
}
catch (UserNotFoundException ignore) {}
try
{
getGroupDAO().getGroups(daoTestPrincipal1, Role.OWNER,
Jeff Burke
committed
"foo");
fail("searchGroups with unknown user should throw " +
"GroupNotFoundException");
}
catch (GroupNotFoundException ignore) {}
return null;
}
});
Subject.doAs(anonSubject, new PrivilegedExceptionAction<Object>()
{
public Object run() throws Exception
{
try
{
getGroupDAO().getGroups(daoTestPrincipal1, Role.OWNER,
Jeff Burke
committed
groupID);
fail("searchGroups with anonymous access should throw " +
"AccessControlException");
}
catch (AccessControlException ignore) {}
return null;
}
});
Subject.doAs(authSubject, new PrivilegedExceptionAction<Object>()
{
public Object run() throws Exception
{
getGroupDAO().deleteGroup(groupID);
return null;
}
});
}
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
private void assertGroupsEqual(Group gr1, Group gr2)
{
if ((gr1 == null) && (gr2 == null))
{
return;
}
assertEquals(gr1, gr2);
assertEquals(gr1.getID(), gr2.getID());
assertEquals(gr1.description, gr2.description);
assertEquals(gr1.getOwner(), gr2.getOwner());
assertEquals(gr1.getGroupMembers(), gr2.getGroupMembers());
assertEquals(gr1.getGroupMembers().size(), gr2.getGroupMembers()
.size());
for (Group gr : gr1.getGroupMembers())
{
assertTrue(gr2.getGroupMembers().contains(gr));
}
assertEquals(gr1.getUserMembers(), gr2.getUserMembers());
assertEquals(gr1.getUserMembers().size(), gr2.getUserMembers()
.size());
for (User<?> user : gr1.getUserMembers())
{
assertTrue(gr2.getUserMembers().contains(user));
}
assertEquals(gr1.getProperties(), gr2.getProperties());
for (GroupProperty prop : gr1.getProperties())
{
assertTrue(gr2.getProperties().contains(prop));
}
}
Jeff Burke
committed