001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020 package org.apache.directory.server.core.entry; 021 import org.apache.directory.server.i18n.I18n; 022 import org.apache.directory.shared.ldap.entry.BinaryValue; 023 import org.apache.directory.shared.ldap.entry.StringValue; 024 import org.apache.directory.shared.ldap.entry.Value; 025 import org.apache.directory.shared.ldap.exception.LdapException; 026 import org.apache.directory.shared.ldap.schema.AttributeType; 027 import org.apache.directory.shared.ldap.schema.LdapComparator; 028 import org.apache.directory.shared.ldap.schema.LdapSyntax; 029 import org.apache.directory.shared.ldap.schema.MatchingRule; 030 import org.apache.directory.shared.ldap.schema.Normalizer; 031 import org.apache.directory.shared.ldap.schema.SyntaxChecker; 032 import org.apache.directory.shared.ldap.schema.comparators.ByteArrayComparator; 033 import org.apache.directory.shared.ldap.schema.normalizers.DeepTrimToLowerNormalizer; 034 import org.apache.directory.shared.ldap.util.StringTools; 035 036 /** 037 * Some common declaration used by the serverEntry tests. 038 * 039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 040 * @version $Rev$, $Date$ 041 */ 042 public class TestServerEntryUtils 043 { 044 /** 045 * A local Syntax class for tests 046 */ 047 static class AT extends AttributeType 048 { 049 private static final long serialVersionUID = 0L; 050 051 protected AT( String oid ) 052 { 053 super( oid ); 054 } 055 } 056 057 public static MatchingRule matchingRuleFactory( String oid ) 058 { 059 MatchingRule matchingRule = new MatchingRule( oid ); 060 061 return matchingRule; 062 } 063 /** 064 * A local MatchingRule class for tests 065 */ 066 static class MR extends MatchingRule 067 { 068 private static final long serialVersionUID = 0L; 069 070 protected MR( String oid ) 071 { 072 super( oid ); 073 } 074 } 075 076 077 /** 078 * A local Syntax class used for the tests 079 */ 080 public static LdapSyntax syntaxFactory( String oid, boolean humanReadable ) 081 { 082 LdapSyntax ldapSyntax = new LdapSyntax( oid ); 083 084 ldapSyntax.setHumanReadable( humanReadable ); 085 086 return ldapSyntax; 087 } 088 static class S extends LdapSyntax 089 { 090 private static final long serialVersionUID = 0L; 091 092 public S( String oid, boolean humanReadible ) 093 { 094 super( oid, "", humanReadible ); 095 } 096 } 097 098 /* no protection*/ static AttributeType getCaseIgnoringAttributeNoNumbersType() 099 { 100 AttributeType attributeType = new AttributeType( "1.1.3.1" ); 101 LdapSyntax syntax = new LdapSyntax( "1.1.1.1", "", true ); 102 103 syntax.setSyntaxChecker( new SyntaxChecker( "1.1.2.1" ) 104 { 105 private static final long serialVersionUID = 0L; 106 107 public boolean isValidSyntax( Object value ) 108 { 109 if ( !( value instanceof String ) ) 110 { 111 return false; 112 } 113 114 String strval = ( String ) value; 115 116 for ( char c:strval.toCharArray() ) 117 { 118 if ( Character.isDigit( c ) ) 119 { 120 return false; 121 } 122 } 123 return true; 124 } 125 } ); 126 127 MatchingRule matchingRule = new MatchingRule( "1.1.2.1" ); 128 matchingRule.setSyntax( syntax ); 129 130 131 matchingRule.setLdapComparator( new LdapComparator<String>( matchingRule.getOid() ) 132 { 133 private static final long serialVersionUID = 0L; 134 135 public int compare( String o1, String o2 ) 136 { 137 return ( o1 == null ? 138 ( o2 == null ? 0 : -1 ) : 139 ( o2 == null ? 1 : o1.compareTo( o2 ) ) ); 140 } 141 142 int getValue( String val ) 143 { 144 if ( val.equals( "LOW" ) ) 145 { 146 return 0; 147 } 148 else if ( val.equals( "MEDIUM" ) ) 149 { 150 return 1; 151 } 152 else if ( val.equals( "HIGH" ) ) 153 { 154 return 2; 155 } 156 157 throw new IllegalArgumentException( I18n.err( I18n.ERR_472 ) ); 158 } 159 } ); 160 161 Normalizer normalizer = new Normalizer( "1.1.1" ) 162 { 163 private static final long serialVersionUID = 0L; 164 165 public Value<?> normalize( Value<?> value ) throws LdapException 166 { 167 if ( !value.isBinary() ) 168 { 169 return new StringValue( value.getString().toLowerCase() ); 170 } 171 172 throw new IllegalStateException( I18n.err( I18n.ERR_473 ) ); 173 } 174 175 176 public String normalize( String value ) throws LdapException 177 { 178 return value.toLowerCase(); 179 } 180 }; 181 182 matchingRule.setNormalizer( normalizer ); 183 184 attributeType.setEquality( matchingRule ); 185 attributeType.setSyntax( syntax ); 186 187 return attributeType; 188 } 189 190 191 /* no protection*/ static AttributeType getIA5StringAttributeType() 192 { 193 AttributeType attributeType = new AttributeType( "1.1" ); 194 attributeType.addName( "1.1" ); 195 LdapSyntax syntax = new LdapSyntax( "1.1.1", "", true ); 196 197 syntax.setSyntaxChecker( new SyntaxChecker( "1.1.2" ) 198 { 199 private static final long serialVersionUID = 0L; 200 201 public boolean isValidSyntax( Object value ) 202 { 203 return ((String)value == null) || (((String)value).length() < 7) ; 204 } 205 } ); 206 207 MatchingRule matchingRule = new MatchingRule( "1.1.2" ); 208 matchingRule.setSyntax( syntax ); 209 210 211 matchingRule.setLdapComparator( new LdapComparator<String>( matchingRule.getOid() ) 212 { 213 private static final long serialVersionUID = 0L; 214 215 public int compare( String o1, String o2 ) 216 { 217 return ( ( o1 == null ) ? 218 ( o2 == null ? 0 : -1 ) : 219 ( o2 == null ? 1 : o1.compareTo( o2 ) ) ); 220 } 221 } ); 222 223 matchingRule.setNormalizer( new DeepTrimToLowerNormalizer( matchingRule.getOid() ) ); 224 225 attributeType.setEquality( matchingRule ); 226 attributeType.setSyntax( syntax ); 227 228 return attributeType; 229 } 230 231 232 /* No protection */ static AttributeType getBytesAttributeType() 233 { 234 AttributeType attributeType = new AttributeType( "1.2" ); 235 LdapSyntax syntax = new LdapSyntax( "1.2.1", "", true ); 236 237 syntax.setSyntaxChecker( new SyntaxChecker( "1.2.1" ) 238 { 239 private static final long serialVersionUID = 0L; 240 241 public boolean isValidSyntax( Object value ) 242 { 243 return ( value == null ) || ( ((byte[])value).length < 5 ); 244 } 245 } ); 246 247 MatchingRule matchingRule = new MatchingRule( "1.2.2" ); 248 matchingRule.setSyntax( syntax ); 249 250 matchingRule.setLdapComparator( new ByteArrayComparator( "1.2.2" ) ); 251 252 matchingRule.setNormalizer( new Normalizer( "1.1.1" ) 253 { 254 // The serial UID 255 private static final long serialVersionUID = 1L; 256 257 public Value<?> normalize( Value<?> value ) throws LdapException 258 { 259 if ( value.isBinary() ) 260 { 261 byte[] val = value.getBytes(); 262 263 // each byte will be changed to be > 0, and spaces will be trimmed 264 byte[] newVal = new byte[ val.length ]; 265 266 int i = 0; 267 268 for ( byte b:val ) 269 { 270 newVal[i++] = (byte)(b & 0x007F); 271 } 272 273 return new BinaryValue( StringTools.trim( newVal ) ); 274 } 275 276 throw new IllegalStateException( I18n.err( I18n.ERR_474 ) ); 277 } 278 279 public String normalize( String value ) throws LdapException 280 { 281 throw new IllegalStateException( I18n.err( I18n.ERR_474 ) ); 282 } 283 } ); 284 285 attributeType.setEquality( matchingRule ); 286 attributeType.setSyntax( syntax ); 287 288 return attributeType; 289 } 290 }