OpenLdapAclParser { public synchronized AclItem parse( String s ) throws ParseException { try { LexerSharedInputState state = new LexerSharedInputState( new CharBuffer( new StringReader( s ) ) ); this.lexer.setInputState( state ); this.parser.getInputState().reset(); parser.parse(); return parser.getAclItem(); } catch ( TokenStreamException e ) { throw new ParseException( "Unable to read ACL: " + e.getMessage(), -1 ); } catch ( RecognitionException e ) { throw new ParseException( "Unable to read ACL: " + e.getMessage() + " - [Line:" + e.getLine() + " - Column:" + e.getColumn() + "]", e.getColumn() ); } } OpenLdapAclParser(); synchronized AclItem parse( String s );  }
@Test( expected = ParseException.class ) public void testEmpty() throws Exception { System.out.println( "\n--> testEmpty" ); OpenLdapAclParser parser = new OpenLdapAclParser(); parser.parse( "" ); fail(); }
@Test public void testImpliedWhatStarWithAccess() throws Exception { System.out.println( "\n--> testImpliedWhatStarWithAccess" ); OpenLdapAclParser parser = new OpenLdapAclParser(); AclItem aclItem = parser.parse( "access to by *" ); assertNotNull( aclItem ); AclWhatClause whatClause = aclItem.getWhatClause(); assertNotNull( whatClause ); assertTrue( whatClause instanceof AclWhatClauseStar ); System.out.println( "<-- ACL:" + aclItem ); }
@Test public void testWhatStarWithAccess() throws Exception { System.out.println( "\n--> testWhatStarWithAccess" ); OpenLdapAclParser parser = new OpenLdapAclParser(); AclItem aclItem = parser.parse( "access to * by *" ); assertNotNull( aclItem ); AclWhatClause whatClause = aclItem.getWhatClause(); assertNotNull( whatClause ); assertTrue( whatClause instanceof AclWhatClauseStar ); System.out.println( "<-- ACL:" + aclItem ); }
@Test public void testWhatStarWithoutAccess() throws Exception { System.out.println( "\n--> testWhatStarWithoutAccess" ); OpenLdapAclParser parser = new OpenLdapAclParser(); AclItem aclItem = parser.parse( " to * by *" ); assertNotNull( aclItem ); AclWhatClause whatClause = aclItem.getWhatClause(); assertNotNull( whatClause ); assertTrue( whatClause instanceof AclWhatClauseStar ); System.out.println( "<-- ACL:" + aclItem ); }
@Test public void testWhatStarWithSpaces() throws Exception { System.out.println( "\n--> testWhatStarWithSpaces" ); OpenLdapAclParser parser = new OpenLdapAclParser(); AclItem aclItem = parser.parse( " access to * by * " ); assertNotNull( aclItem ); AclWhatClause whatClause = aclItem.getWhatClause(); assertNotNull( whatClause ); assertTrue( whatClause instanceof AclWhatClauseStar ); System.out.println( "<-- ACL:" + aclItem ); }
@Test( expected = ParseException.class ) public void testWhatTwoStars() throws Exception { System.out.println( "\n--> testWhatTwoStars" ); OpenLdapAclParser parser = new OpenLdapAclParser(); parser.parse( "to * * by *" ); fail(); }
@Test public void testWhatDnValidDnWithQuote() throws Exception { System.out.println( "\n--> testWhatDnValidDnWithQuote" ); String dnPattern = "dc=example,dc=com"; OpenLdapAclParser parser = new OpenLdapAclParser(); AclItem aclItem = parser.parse( "access to dn=\"" + dnPattern + "\" by *" ); assertNotNull( aclItem ); AclWhatClause whatClause = aclItem.getWhatClause(); assertNotNull( whatClause ); assertTrue( whatClause instanceof AclWhatClauseDn ); AclWhatClauseDn whatClauseDn = (AclWhatClauseDn)whatClause; assertEquals( dnPattern, whatClauseDn.getPattern() ); System.out.println( "<-- ACL:" + aclItem ); }
@Test( expected = ParseException.class ) public void testWhatDnStar() throws Exception { System.out.println( "\n--> testWhatDnStar" ); OpenLdapAclParser parser = new OpenLdapAclParser(); parser.parse( "access to dn=* by *" ); fail(); }
@Test( expected=ParseException.class) public void testWhatDnValidDnNoQuote() throws Exception { System.out.println( "\n--> testWhatDnValidDnNoQuote" ); String dnPattern = "dc=example,dc=com"; OpenLdapAclParser parser = new OpenLdapAclParser(); parser.parse( "access to dn=" + dnPattern + "\n by *" ); fail(); }
@Ignore @Test ( expected=ParseException.class) public void testWhatDnBadDn() throws Exception { System.out.println( "\n--> testWhatDnBadDn" ); String dnPattern = "Blah"; OpenLdapAclParser parser = new OpenLdapAclParser(); parser.parse( "access to dn=\"" + dnPattern + "\"\n by *" ); fail(); }
@Test public void testWhatDnBasicDnExact() throws Exception { System.out.println( "\n--> testWhatDnBasicDnExact" ); String dnPattern = "dc=example,dc=com"; OpenLdapAclParser parser = new OpenLdapAclParser(); AclItem aclItem = parser.parse( "access to dn.exact=\"" + dnPattern + "\" by *" ); assertNotNull( aclItem ); AclWhatClause whatClause = aclItem.getWhatClause(); assertNotNull( whatClause ); assertTrue( whatClause instanceof AclWhatClauseDn ); AclWhatClauseDn whatClauseDn = (AclWhatClauseDn) whatClause; assertEquals( dnPattern, whatClauseDn.getPattern() ); assertEquals( AclWhatClauseDnTypeEnum.EXACT, whatClauseDn.getType() ); System.out.println( "<-- ACL:" + aclItem ); }
@Test ( expected=ParseException.class) public void testWhatDnBasicDnExactNoQuote() throws Exception { System.out.println( "\n--> testWhatDnBasicDnExactNoQuote" ); String dnPattern = "dc=example,dc=com"; OpenLdapAclParser parser = new OpenLdapAclParser(); parser.parse( "access to dn.exact=" + dnPattern + " by *" ); fail(); }
@Test ( expected=ParseException.class) public void testWhatDnBasicDnExactBadDn() throws Exception { System.out.println( "\n--> testWhatDnBasicDnExactBadDn" ); String dnPattern = "example"; OpenLdapAclParser parser = new OpenLdapAclParser(); parser.parse( "access to dn.exact=" + dnPattern + " by *" ); fail(); }
@Test public void testWhatDnBasicDnRegex() throws Exception { System.out.println( "\n--> testWhatDnBasicDnRegex" ); String dnPattern = "dc=*"; OpenLdapAclParser parser = new OpenLdapAclParser(); AclItem aclItem = parser.parse( "access to dn.regex=\"" + dnPattern + "\" by *" ); assertNotNull( aclItem ); AclWhatClause whatClause = aclItem.getWhatClause(); assertNotNull( whatClause ); assertTrue( whatClause instanceof AclWhatClauseDn ); AclWhatClauseDn whatClauseDn = (AclWhatClauseDn) whatClause; assertEquals( dnPattern, whatClauseDn.getPattern() ); assertEquals( AclWhatClauseDnTypeEnum.REGEX, whatClauseDn.getType() ); System.out.println( "<-- ACL:" + aclItem ); }
@Test( expected=ParseException.class) public void testWhatDnBasicDnRegexNoQuote() throws Exception { System.out.println( "\n--> testWhatDnBasicDnRegexNoQuote" ); String dnPattern = "dc=*,dc=com"; OpenLdapAclParser parser = new OpenLdapAclParser(); AclItem aclItem = parser.parse( "access to dn.regex=" + dnPattern + " by *" ); fail(); }
@Test public void testWhatDnScopeOneQuotedDn() throws Exception { System.out.println( "\n--> testWhatDnScopeOneQuotedDn" ); String dnPattern = "dc=example,dc=com"; OpenLdapAclParser parser = new OpenLdapAclParser(); AclItem aclItem = parser.parse( "access to dn.one=\"" + dnPattern + "\" by *" ); assertNotNull( aclItem ); AclWhatClause whatClause = aclItem.getWhatClause(); assertNotNull( whatClause ); assertTrue( whatClause instanceof AclWhatClauseDn ); AclWhatClauseDn whatClauseDn = (AclWhatClauseDn) whatClause; assertEquals( dnPattern, whatClauseDn.getPattern() ); assertEquals( AclWhatClauseDnTypeEnum.ONE, whatClauseDn.getType() ); System.out.println( "<-- ACL:" + aclItem ); }
@Test public void testWhatDnScopeOneLevelQuotedDn() throws Exception { System.out.println( "\n--> testWhatDnScopeOneLevelQuotedDn" ); String dnPattern = "dc=example,dc=com"; OpenLdapAclParser parser = new OpenLdapAclParser(); AclItem aclItem = parser.parse( "access to dn.onelevel=\"" + dnPattern + "\" by *" ); assertNotNull( aclItem ); AclWhatClause whatClause = aclItem.getWhatClause(); assertNotNull( whatClause ); assertTrue( whatClause instanceof AclWhatClauseDn ); AclWhatClauseDn whatClauseDn = (AclWhatClauseDn) whatClause; assertEquals( dnPattern, whatClauseDn.getPattern() ); assertEquals( AclWhatClauseDnTypeEnum.ONE_LEVEL, whatClauseDn.getType() ); System.out.println( "<-- ACL:" + aclItem ); }
@Test public void testWhatDnScopeBaseQuotedDn() throws Exception { System.out.println( "\n--> testWhatDnScopeBaseQuotedDn" ); String dnPattern = "dc=example,dc=com"; OpenLdapAclParser parser = new OpenLdapAclParser(); AclItem aclItem = parser.parse( "access to dn.base=\"" + dnPattern + "\" by *" ); assertNotNull( aclItem ); AclWhatClause whatClause = aclItem.getWhatClause(); assertNotNull( whatClause ); assertTrue( whatClause instanceof AclWhatClauseDn ); AclWhatClauseDn whatClauseDn = (AclWhatClauseDn) whatClause; assertEquals( dnPattern, whatClauseDn.getPattern() ); assertEquals( AclWhatClauseDnTypeEnum.BASE, whatClauseDn.getType() ); System.out.println( "<-- ACL:" + aclItem ); }
@Test public void testWhatDnScopeBaseobjectQuotedDn() throws Exception { System.out.println( "\n--> testWhatDnScopeBaseobjectQuotedDn" ); String dnPattern = "dc=example,dc=com"; OpenLdapAclParser parser = new OpenLdapAclParser(); AclItem aclItem = parser.parse( "access to dn.baseobject=\"" + dnPattern + "\" by *" ); assertNotNull( aclItem ); AclWhatClause whatClause = aclItem.getWhatClause(); assertNotNull( whatClause ); assertTrue( whatClause instanceof AclWhatClauseDn ); AclWhatClauseDn whatClauseDn = (AclWhatClauseDn) whatClause; assertEquals( dnPattern, whatClauseDn.getPattern() ); assertEquals( AclWhatClauseDnTypeEnum.BASE_OBJECT, whatClauseDn.getType() ); System.out.println( "<-- ACL:" + aclItem ); }
@Test public void testWhatDnScopeSubQuotedDn() throws Exception { System.out.println( "\n--> testWhatDnScopeSubQuotedDn" ); String dnPattern = "dc=example,dc=com"; OpenLdapAclParser parser = new OpenLdapAclParser(); AclItem aclItem = parser.parse( "access to dn.sub=\"" + dnPattern + "\" by *" ); assertNotNull( aclItem ); AclWhatClause whatClause = aclItem.getWhatClause(); assertNotNull( whatClause ); assertTrue( whatClause instanceof AclWhatClauseDn ); AclWhatClauseDn whatClauseDn = (AclWhatClauseDn) whatClause; assertEquals( dnPattern, whatClauseDn.getPattern() ); assertEquals( AclWhatClauseDnTypeEnum.SUB, whatClauseDn.getType() ); System.out.println( "<-- ACL:" + aclItem ); }
@Test public void testWhatDnScopeSubtreeQuotedDn() throws Exception { System.out.println( "\n--> testWhatDnScopeSubtreeQuotedDn" ); String dnPattern = "dc=example,dc=com"; OpenLdapAclParser parser = new OpenLdapAclParser(); AclItem aclItem = parser.parse( "access to dn.subtree=\"" + dnPattern + "\" by *" ); assertNotNull( aclItem ); AclWhatClause whatClause = aclItem.getWhatClause(); assertNotNull( whatClause ); assertTrue( whatClause instanceof AclWhatClauseDn ); AclWhatClauseDn whatClauseDn = (AclWhatClauseDn) whatClause; assertEquals( dnPattern, whatClauseDn.getPattern() ); assertEquals( AclWhatClauseDnTypeEnum.SUBTREE, whatClauseDn.getType() ); System.out.println( "<-- ACL:" + aclItem ); }
@Test public void testWhatDnScopeChildrenQuotedDn() throws Exception { System.out.println( "\n--> testWhatDnScopeChildrenQuotedDn" ); String dnPattern = "dc=example,dc=com"; OpenLdapAclParser parser = new OpenLdapAclParser(); AclItem aclItem = parser.parse( "access to dn.children=\"" + dnPattern + "\" by *" ); assertNotNull( aclItem ); AclWhatClause whatClause = aclItem.getWhatClause(); assertNotNull( whatClause ); assertTrue( whatClause instanceof AclWhatClauseDn ); AclWhatClauseDn whatClauseDn = (AclWhatClauseDn) whatClause; assertEquals( dnPattern, whatClauseDn.getPattern() ); assertEquals( AclWhatClauseDnTypeEnum.CHILDREN, whatClauseDn.getType() ); System.out.println( "<-- ACL:" + aclItem ); }
@Test public void testWhatDnScopeOneEmptyDn() throws Exception { System.out.println( "\n--> testWhatDnScopeOneEmptyDn" ); OpenLdapAclParser parser = new OpenLdapAclParser(); AclItem aclItem = parser.parse( "access to dn.one=\"\" by *" ); assertNotNull( aclItem ); AclWhatClause whatClause = aclItem.getWhatClause(); assertNotNull( whatClause ); assertTrue( whatClause instanceof AclWhatClauseDn ); AclWhatClauseDn whatClauseDn = (AclWhatClauseDn) whatClause; assertEquals( "", whatClauseDn.getPattern() ); assertEquals( AclWhatClauseDnTypeEnum.ONE, whatClauseDn.getType() ); System.out.println( "<-- ACL:" + aclItem ); }
@Test( expected=ParseException.class) public void testWhatDnScopeOneNoQuotedDn() throws Exception { System.out.println( "\n--> testWhatDnScopeOneNoQuotedDn" ); String dnPattern = "dc=example,dc=com"; OpenLdapAclParser parser = new OpenLdapAclParser(); parser.parse( "access to dn.one=" + dnPattern + " by *" ); }
@Test( expected=ParseException.class) public void testWhatDnScopeOneNoDn() throws Exception { System.out.println( "\n--> testWhatDnScopeOneNoDn" ); OpenLdapAclParser parser = new OpenLdapAclParser(); parser.parse( "access to dn.one= by *" ); }
@Test( expected=ParseException.class) public void testWhatDnScopeOneStar() throws Exception { System.out.println( "\n--> testWhatDnScopeOneStar" ); OpenLdapAclParser parser = new OpenLdapAclParser(); parser.parse( "access to dn.one=* by *" ); }
@Test public void testWhatFilterSimple() throws Exception { System.out.println( "\n--> testWhatFilter" ); String filter = "(objectclass=*)"; OpenLdapAclParser parser = new OpenLdapAclParser(); AclItem aclItem = parser.parse( "access to filter=" + filter + " by *" ); assertNotNull( aclItem ); AclWhatClause whatClause = aclItem.getWhatClause(); assertNotNull( whatClause ); assertTrue( whatClause instanceof AclWhatClauseFilter ); AclWhatClauseFilter whatClauseFilter = (AclWhatClauseFilter) whatClause; assertEquals( filter, whatClauseFilter.getFilter() ); System.out.println( "<-- ACL:" + aclItem ); }
@Test public void testWhatFilterComplex() throws Exception { System.out.println( "\n--> testWhatFilterComplex" ); String filter = "(&(objectclass=*)(cn=test)(!(sn=test)))"; OpenLdapAclParser parser = new OpenLdapAclParser(); AclItem aclItem = parser.parse( "access to filter=" + filter + " by *" ); assertNotNull( aclItem ); AclWhatClause whatClause = aclItem.getWhatClause(); assertNotNull( whatClause ); assertTrue( whatClause instanceof AclWhatClauseFilter ); AclWhatClauseFilter whatClauseFilter = (AclWhatClauseFilter) whatClause; assertEquals( filter, whatClauseFilter.getFilter() ); System.out.println( "<-- ACL:" + aclItem ); }
@Test( expected=ParseException.class) public void testWhatFilterWrongSimple() throws Exception { System.out.println( "\n--> testWhatFilterWrongSimple" ); String filter = "(objectclass=*"; OpenLdapAclParser parser = new OpenLdapAclParser(); parser.parse( "access to filter=" + filter + " by *" ); fail(); }