1   /*
2    *   @(#) $Id: RestrictedByFilterTest.java 292107 2005-09-28 03:42:09Z trustin $
3    *
4    *   Copyright 2004 The Apache Software Foundation
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   *
18   */
19  package org.apache.ldap.server.authz.support;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.HashSet;
25  import java.util.Set;
26  
27  import javax.naming.directory.Attribute;
28  import javax.naming.directory.Attributes;
29  import javax.naming.directory.BasicAttribute;
30  import javax.naming.directory.BasicAttributes;
31  
32  import junit.framework.Assert;
33  import junit.framework.TestCase;
34  
35  import org.apache.ldap.common.aci.ACITuple;
36  import org.apache.ldap.common.aci.AuthenticationLevel;
37  import org.apache.ldap.common.aci.ProtectedItem;
38  import org.apache.ldap.common.aci.ProtectedItem.RestrictedByItem;
39  
40  /***
41   * Tests {@link RestrictedByFilter}.
42   *
43   * @author The Apache Directory Project
44   * @version $Rev: 292107 $, $Date: 2005-09-27 23:42:09 -0400 (Tue, 27 Sep 2005) $
45   */
46  public class RestrictedByFilterTest extends TestCase
47  {
48      private static final Collection EMPTY_COLLECTION =
49          Collections.unmodifiableCollection( new ArrayList() );
50      private static final Set EMPTY_SET =
51          Collections.unmodifiableSet( new HashSet() );
52      
53      private static final Collection PROTECTED_ITEMS = new ArrayList();
54      private static final Attributes ENTRY = new BasicAttributes();
55      
56      static
57      {
58          Collection mvcItems = new ArrayList();
59          mvcItems.add( new RestrictedByItem( "choice", "option" ) );
60          PROTECTED_ITEMS.add( new ProtectedItem.RestrictedBy( mvcItems ) );
61          
62          Attribute attr = new BasicAttribute( "option" );
63          attr.add( "1" );
64          attr.add( "2" );
65          
66          ENTRY.put( attr );
67      }
68      
69      public void testWrongScope() throws Exception
70      {
71          RestrictedByFilter filter = new RestrictedByFilter();
72          Collection tuples = new ArrayList();
73          tuples.add( new ACITuple(
74                  EMPTY_COLLECTION, AuthenticationLevel.NONE, EMPTY_COLLECTION,
75                  EMPTY_SET, true, 0 ) );
76          
77          tuples = Collections.unmodifiableCollection( tuples );
78          
79          Assert.assertEquals(
80                  tuples, filter.filter(
81                          tuples, OperationScope.ATTRIBUTE_TYPE, null, null, null,
82                          null, null, null, null, null, null, null ) );
83  
84          Assert.assertEquals(
85                  tuples, filter.filter(
86                          tuples, OperationScope.ENTRY, null, null, null,
87                          null, null, null, null, null, null, null ) );
88      }
89      
90      public void testZeroTuple() throws Exception
91      {
92          RestrictedByFilter filter = new RestrictedByFilter();
93          
94          Assert.assertEquals(
95                  0, filter.filter(
96                          EMPTY_COLLECTION, OperationScope.ATTRIBUTE_TYPE_AND_VALUE,
97                          null, null, null, null, null, null, null, null, null, null ).size() );
98      }
99      
100     public void testDenialTuple() throws Exception
101     {
102         RestrictedByFilter filter = new RestrictedByFilter();
103         Collection tuples = new ArrayList();
104         tuples.add( new ACITuple(
105                 EMPTY_COLLECTION, AuthenticationLevel.NONE, PROTECTED_ITEMS,
106                 EMPTY_SET, false, 0 ) );
107         
108         tuples = Collections.unmodifiableCollection( tuples );
109         
110         Assert.assertEquals(
111                 tuples, filter.filter(
112                         tuples, OperationScope.ATTRIBUTE_TYPE_AND_VALUE, null, null, null,
113                         null, null, null, "testAttr", null, ENTRY, null ) );
114     }
115 
116 
117     public void testGrantTuple() throws Exception
118     {
119         RestrictedByFilter filter = new RestrictedByFilter();
120         Collection tuples = new ArrayList();
121         tuples.add( new ACITuple(
122                 EMPTY_COLLECTION, AuthenticationLevel.NONE, PROTECTED_ITEMS,
123                 EMPTY_SET, true, 0 ) );
124         
125         Assert.assertEquals(
126                 1, filter.filter(
127                         tuples, OperationScope.ATTRIBUTE_TYPE_AND_VALUE, null, null, null,
128                         null, null, null, "choice", "1", ENTRY, null ).size() );
129 
130         Assert.assertEquals(
131                 1, filter.filter(
132                         tuples, OperationScope.ATTRIBUTE_TYPE_AND_VALUE, null, null, null,
133                         null, null, null, "choice", "2", ENTRY, null ).size() );
134 
135         Assert.assertEquals(
136                 0, filter.filter(
137                         tuples, OperationScope.ATTRIBUTE_TYPE_AND_VALUE, null, null, null,
138                         null, null, null, "choice", "3", ENTRY, null ).size() );
139     }
140 }