1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.ldap.server.tools.schema;
18  
19  
20  import java.io.InputStream;
21  import java.util.Map;
22  
23  import junit.framework.TestCase;
24  
25  import org.apache.ldap.common.schema.ObjectClassTypeEnum;
26  
27  
28  /***
29   * Tests the OpenLDAP schema parser.
30   *
31   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32   * @version $Rev: 157997 $
33   */
34  public class OpenLdapSchemaParserTest extends TestCase
35  {
36      private OpenLdapSchemaParser parser;
37  
38  
39      protected void setUp() throws Exception
40      {
41          super.setUp();
42  
43          parser = new OpenLdapSchemaParser();
44          parser.setParserMonitor( new ConsoleParserMonitor() );
45      }
46  
47  
48      protected void tearDown() throws Exception
49      {
50          super.tearDown();
51          parser = null;
52      }
53  
54  
55      public void testSimpleAttributeTypeNoLength() throws Exception
56      {
57          String attributeTypeData = "attributetype ( 2.5.4.14 NAME 'searchGuide'\n" +
58              "        DESC 'RFC2256: search guide, obsoleted by enhancedSearchGuide'\n" +
59              "        SYNTAX 1.3.6.1.4.1.1466.115.121.1.25 )";
60  
61          parser.parse( attributeTypeData );
62          Map attributeTypes = parser.getAttributeTypes();
63          AttributeTypeLiteral type = ( AttributeTypeLiteral ) attributeTypes.get( "2.5.4.14" );
64  
65          assertNotNull( type );
66          assertEquals( "2.5.4.14", type.getOid() );
67          assertEquals( "searchGuide", type.getNames()[0] );
68          assertEquals( "RFC2256: search guide, obsoleted by enhancedSearchGuide", type.getDescription() );
69          assertEquals( "1.3.6.1.4.1.1466.115.121.1.25", type.getSyntax() );
70      }
71  
72  
73      public void testSimpleAttributeTypeParse() throws Exception
74      {
75          String attributeTypeData = "# adding a comment  \n" +
76              "attributetype ( 2.5.4.2 NAME 'knowledgeInformation'\n" +
77              "        DESC 'RFC2256: knowledge information'\n" +
78              "        EQUALITY caseIgnoreMatch\n" +
79              "        SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{32768} )";
80          parser.parse( attributeTypeData );
81          Map attributeTypes = parser.getAttributeTypes();
82          AttributeTypeLiteral type = ( AttributeTypeLiteral ) attributeTypes.get( "2.5.4.2" );
83  
84          assertNotNull( type );
85          assertEquals( "2.5.4.2", type.getOid() );
86          assertEquals( "knowledgeInformation", type.getNames()[0] );
87          assertEquals( "RFC2256: knowledge information", type.getDescription() );
88          assertEquals( "1.3.6.1.4.1.1466.115.121.1.15", type.getSyntax() );
89          assertEquals( 32768, type.getLength() );
90      }
91  
92  
93      public void testAttributeTypeParseWithDescQuotes() throws Exception
94      {
95          String attributeTypeData = "# adding a comment  \n" +
96              "attributetype ( 2.5.4.2 NAME 'knowledgeInformation'\n" +
97              "        DESC 'RFC2256: \"knowledge\" information'\n" +
98              "        EQUALITY caseIgnoreMatch\n" +
99              "        SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{32768} )";
100         parser.parse( attributeTypeData );
101         Map attributeTypes = parser.getAttributeTypes();
102         AttributeTypeLiteral type = ( AttributeTypeLiteral ) attributeTypes.get( "2.5.4.2" );
103 
104         assertNotNull( type );
105         assertEquals( "2.5.4.2", type.getOid() );
106         assertEquals( "knowledgeInformation", type.getNames()[0] );
107         assertEquals( "RFC2256: //\"knowledge//\" information", type.getDescription() );
108         assertEquals( "1.3.6.1.4.1.1466.115.121.1.15", type.getSyntax() );
109         assertEquals( 32768, type.getLength() );
110     }
111 
112 
113     public void testComplexAttributeTypeParse() throws Exception
114     {
115         String attributeTypeData = "# adding a comment  \n" +
116             "attributetype ( 2.5.4.2 NAME ( 'knowledgeInformation' 'asdf' ) \n" +
117             "        DESC 'RFC2256: knowledge information'\n" +
118             "        EQUALITY caseIgnoreMatch\n" +
119             "        SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{32768} )";
120         parser.parse( attributeTypeData );
121         Map attributeTypes = parser.getAttributeTypes();
122         AttributeTypeLiteral type = ( AttributeTypeLiteral ) attributeTypes.get( "2.5.4.2" );
123 
124         assertNotNull( type );
125         assertEquals( "2.5.4.2", type.getOid() );
126         assertEquals( "knowledgeInformation", type.getNames()[0] );
127         assertEquals( "RFC2256: knowledge information", type.getDescription() );
128         assertEquals( "1.3.6.1.4.1.1466.115.121.1.15", type.getSyntax() );
129         assertEquals( 32768, type.getLength() );
130     }
131 
132 
133     public void testObjectClassParse() throws Exception
134     {
135         String objectClassData = "objectclass ( 2.5.6.6 NAME 'person'\n" +
136             "        DESC 'RFC2256: a person'\n" +
137             "        SUP top STRUCTURAL\n" +
138             "        MUST ( sn $ cn )\n" +
139             "        MAY ( userPassword $ telephoneNumber $ seeAlso $ description ) )";
140         parser.parse( objectClassData );
141         Map objectClasses = parser.getObjectClassTypes();
142         ObjectClassLiteral objectClass = ( ObjectClassLiteral ) objectClasses.get( "2.5.6.6" );
143 
144         assertNotNull( objectClass );
145         assertEquals( "2.5.6.6", objectClass.getOid() );
146         assertEquals( "person", objectClass.getNames()[0] );
147         assertEquals( "RFC2256: a person", objectClass.getDescription() );
148         assertEquals( ObjectClassTypeEnum.STRUCTURAL, objectClass.getClassType() );
149         assertEquals( "sn", objectClass.getMust()[0] );
150         assertEquals( "cn", objectClass.getMust()[1] );
151         assertEquals( "userPassword", objectClass.getMay()[0] );
152         assertEquals( "telephoneNumber", objectClass.getMay()[1] );
153         assertEquals( "seeAlso", objectClass.getMay()[2] );
154         assertEquals( "description", objectClass.getMay()[3] );
155     }
156 
157 
158     public void testObjectClassMultipleNames() throws Exception
159     {
160         String objectClassData = "objectclass ( 0.9.2342.19200300.100.4.4\n" +
161             "\tNAME ( 'pilotPerson' 'newPilotPerson' )\n" +
162             "\tSUP person STRUCTURAL\n" +
163             "\tMAY ( userid $ textEncodedORAddress $ rfc822Mailbox $\n" +
164             "\t\tfavouriteDrink $ roomNumber $ userClass $\n" +
165             "\t\thomeTelephoneNumber $ homePostalAddress $ secretary $\n" +
166             "\t\tpersonalTitle $ preferredDeliveryMethod $ businessCategory $\n" +
167             "\t\tjanetMailbox $ otherMailbox $ mobileTelephoneNumber $\n" +
168             "\t\tpagerTelephoneNumber $ organizationalStatus $\n" +
169             "\t\tmailPreferenceOption $ personalSignature )\n" +
170             "\t)";
171         parser.parse( objectClassData );
172         Map objectClasses = parser.getObjectClassTypes();
173         ObjectClassLiteral objectClass = ( ObjectClassLiteral )
174             objectClasses.get( "0.9.2342.19200300.100.4.4" );
175 
176         assertNotNull( objectClass );
177         assertEquals( "0.9.2342.19200300.100.4.4", objectClass.getOid() );
178         assertEquals( "pilotPerson", objectClass.getNames()[0] );
179         assertEquals( "newPilotPerson", objectClass.getNames()[1] );
180         assertEquals( ObjectClassTypeEnum.STRUCTURAL, objectClass.getClassType() );
181         assertEquals( "person", objectClass.getSuperiors()[0] );
182 
183         assertEquals( "userid", objectClass.getMay()[0] );
184         assertEquals( "textEncodedORAddress", objectClass.getMay()[1] );
185         assertEquals( "rfc822Mailbox", objectClass.getMay()[2] );
186         assertEquals( "favouriteDrink", objectClass.getMay()[3] );
187         assertEquals( "roomNumber", objectClass.getMay()[4] );
188         assertEquals( "userClass", objectClass.getMay()[5] );
189         assertEquals( "homeTelephoneNumber", objectClass.getMay()[6] );
190         assertEquals( "homePostalAddress", objectClass.getMay()[7] );
191         assertEquals( "secretary", objectClass.getMay()[8] );
192         assertEquals( "personalTitle", objectClass.getMay()[9] );
193         assertEquals( "preferredDeliveryMethod", objectClass.getMay()[10] );
194         assertEquals( "businessCategory", objectClass.getMay()[11] );
195         assertEquals( "janetMailbox", objectClass.getMay()[12] );
196         assertEquals( "otherMailbox", objectClass.getMay()[13] );
197         assertEquals( "mobileTelephoneNumber", objectClass.getMay()[14] );
198         assertEquals( "pagerTelephoneNumber", objectClass.getMay()[15] );
199         assertEquals( "organizationalStatus", objectClass.getMay()[16] );
200         assertEquals( "mailPreferenceOption", objectClass.getMay()[17] );
201         assertEquals( "personalSignature", objectClass.getMay()[18] );
202 
203     }
204 
205 
206     public void testAutoFsSchemaFile() throws Exception
207     {
208         InputStream in = getClass().getResourceAsStream( "autofs.schema" );
209         parser.parse( in );
210     }
211 
212 
213     public void testCoreSchemaFile() throws Exception
214     {
215         InputStream in = getClass().getResourceAsStream( "core.schema" );
216         parser.parse( in );
217     }
218 
219 
220     public void testCorbaSchemaFile() throws Exception
221     {
222         InputStream in = getClass().getResourceAsStream( "corba.schema" );
223         parser.parse( in );
224     }
225 
226 
227     public void testCosineSchemaFile() throws Exception
228     {
229         InputStream in = getClass().getResourceAsStream( "cosine.schema" );
230         parser.parse( in );
231     }
232 
233 
234     public void testInetOrgPersonSchemaFile() throws Exception
235     {
236         InputStream in = getClass().getResourceAsStream( "inetorgperson.schema" );
237         parser.parse( in );
238     }
239 
240 
241     public void testJavaSchemaFile() throws Exception
242     {
243         InputStream in = getClass().getResourceAsStream( "java.schema" );
244         parser.parse( in );
245     }
246 
247 
248     public void testMiscSchemaFile() throws Exception
249     {
250         InputStream in = getClass().getResourceAsStream( "misc.schema" );
251         parser.parse( in );
252     }
253 
254 
255     public void testNisSchemaFile() throws Exception
256     {
257         InputStream in = getClass().getResourceAsStream( "nis.schema" );
258         parser.parse( in );
259     }
260 }