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 */ 020package org.apache.directory.server.core.api.entry; 021 022 023import org.apache.directory.api.ldap.model.entry.BinaryValue; 024import org.apache.directory.api.ldap.model.entry.StringValue; 025import org.apache.directory.api.ldap.model.entry.Value; 026import org.apache.directory.api.ldap.model.exception.LdapException; 027import org.apache.directory.api.ldap.model.schema.AttributeType; 028import org.apache.directory.api.ldap.model.schema.LdapComparator; 029import org.apache.directory.api.ldap.model.schema.LdapSyntax; 030import org.apache.directory.api.ldap.model.schema.MatchingRule; 031import org.apache.directory.api.ldap.model.schema.MutableAttributeType; 032import org.apache.directory.api.ldap.model.schema.MutableMatchingRule; 033import org.apache.directory.api.ldap.model.schema.Normalizer; 034import org.apache.directory.api.ldap.model.schema.SyntaxChecker; 035import org.apache.directory.api.ldap.model.schema.comparators.ByteArrayComparator; 036import org.apache.directory.api.ldap.model.schema.normalizers.DeepTrimToLowerNormalizer; 037import org.apache.directory.api.util.Strings; 038import org.apache.directory.server.i18n.I18n; 039 040 041/** 042 * Some common declaration used by the serverEntry tests. 043 * 044 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 045 */ 046public class TestServerEntryUtils 047{ 048 /** 049 * A local Syntax class for tests 050 */ 051 static class AT extends AttributeType 052 { 053 private static final long serialVersionUID = 0L; 054 055 056 protected AT( String oid ) 057 { 058 super( oid ); 059 } 060 } 061 062 063 public static MatchingRule matchingRuleFactory( String oid ) 064 { 065 MatchingRule matchingRule = new MutableMatchingRule( oid ); 066 067 return matchingRule; 068 } 069 070 /** 071 * A local MatchingRule class for tests 072 */ 073 static class MR extends MatchingRule 074 { 075 protected MR( String oid ) 076 { 077 super( oid ); 078 } 079 } 080 081 082 /** 083 * A local Syntax class used for the tests 084 */ 085 public static LdapSyntax syntaxFactory( String oid, boolean humanReadable ) 086 { 087 LdapSyntax ldapSyntax = new LdapSyntax( oid ); 088 089 ldapSyntax.setHumanReadable( humanReadable ); 090 091 return ldapSyntax; 092 } 093 094 static class S extends LdapSyntax 095 { 096 private static final long serialVersionUID = 0L; 097 098 099 public S( String oid, boolean humanReadible ) 100 { 101 super( oid, "", humanReadible ); 102 } 103 } 104 105 106 /* no protection*/ 107 //This will suppress PMD.AvoidUsingHardCodedIP warnings in this class 108 @SuppressWarnings("PMD.AvoidUsingHardCodedIP") 109 static AttributeType getCaseIgnoringAttributeNoNumbersType() 110 { 111 MutableAttributeType attributeType = new MutableAttributeType( "1.1.3.1" ); 112 LdapSyntax syntax = new LdapSyntax( "1.1.1.1", "", true ); 113 114 syntax.setSyntaxChecker( new SyntaxChecker( "1.1.2.1" ) 115 { 116 public boolean isValidSyntax( Object value ) 117 { 118 if ( !( value instanceof String ) ) 119 { 120 return false; 121 } 122 123 String strval = ( String ) value; 124 125 for ( char c : strval.toCharArray() ) 126 { 127 if ( Character.isDigit( c ) ) 128 { 129 return false; 130 } 131 } 132 return true; 133 } 134 } ); 135 136 MutableMatchingRule matchingRule = new MutableMatchingRule( "1.1.2.1" ); 137 matchingRule.setSyntax( syntax ); 138 139 matchingRule.setLdapComparator( new LdapComparator<String>( matchingRule.getOid() ) 140 { 141 public int compare( String o1, String o2 ) 142 { 143 return ( o1 == null ? 144 ( o2 == null ? 0 : -1 ) : 145 ( o2 == null ? 1 : o1.compareTo( o2 ) ) ); 146 } 147 148 149 int getValue( String val ) 150 { 151 if ( val.equals( "LOW" ) ) 152 { 153 return 0; 154 } 155 else if ( val.equals( "MEDIUM" ) ) 156 { 157 return 1; 158 } 159 else if ( val.equals( "HIGH" ) ) 160 { 161 return 2; 162 } 163 164 throw new IllegalArgumentException( I18n.err( I18n.ERR_472 ) ); 165 } 166 } ); 167 168 Normalizer normalizer = new Normalizer( "1.1.1" ) 169 { 170 public Value<?> normalize( Value<?> value ) throws LdapException 171 { 172 if ( value.isHumanReadable() ) 173 { 174 return new StringValue( Strings.toLowerCase( value.getString() ) ); 175 } 176 177 throw new IllegalStateException( I18n.err( I18n.ERR_473 ) ); 178 } 179 180 181 public String normalize( String value ) throws LdapException 182 { 183 return Strings.toLowerCase( value ); 184 } 185 }; 186 187 matchingRule.setNormalizer( normalizer ); 188 189 attributeType.setEquality( matchingRule ); 190 attributeType.setSyntax( syntax ); 191 192 return attributeType; 193 } 194 195 196 /* no protection*/static AttributeType getIA5StringAttributeType() 197 { 198 MutableAttributeType attributeType = new MutableAttributeType( "1.1" ); 199 attributeType.addName( "1.1" ); 200 LdapSyntax syntax = new LdapSyntax( "1.1.1", "", true ); 201 202 syntax.setSyntaxChecker( new SyntaxChecker( "1.1.2" ) 203 { 204 public boolean isValidSyntax( Object value ) 205 { 206 return ( ( String ) value == null ) || ( ( ( String ) value ).length() < 7 ); 207 } 208 } ); 209 210 MutableMatchingRule matchingRule = new MutableMatchingRule( "1.1.2" ); 211 matchingRule.setSyntax( syntax ); 212 213 matchingRule.setLdapComparator( new LdapComparator<String>( matchingRule.getOid() ) 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 MutableAttributeType attributeType = new MutableAttributeType( "1.2" ); 235 LdapSyntax syntax = new LdapSyntax( "1.2.1", "", true ); 236 237 syntax.setSyntaxChecker( new SyntaxChecker( "1.2.1" ) 238 { 239 public boolean isValidSyntax( Object value ) 240 { 241 return ( value == null ) || ( ( ( byte[] ) value ).length < 5 ); 242 } 243 } ); 244 245 MutableMatchingRule matchingRule = new MutableMatchingRule( "1.2.2" ); 246 matchingRule.setSyntax( syntax ); 247 248 matchingRule.setLdapComparator( new ByteArrayComparator( "1.2.2" ) ); 249 250 matchingRule.setNormalizer( new Normalizer( "1.1.1" ) 251 { 252 public Value<?> normalize( Value<?> value ) throws LdapException 253 { 254 if ( !value.isHumanReadable() ) 255 { 256 byte[] val = value.getBytes(); 257 258 // each byte will be changed to be > 0, and spaces will be trimmed 259 byte[] newVal = new byte[val.length]; 260 261 int i = 0; 262 263 for ( byte b : val ) 264 { 265 newVal[i++] = ( byte ) ( b & 0x007F ); 266 } 267 268 return new BinaryValue( Strings.trim( newVal ) ); 269 } 270 271 throw new IllegalStateException( I18n.err( I18n.ERR_474 ) ); 272 } 273 274 275 public String normalize( String value ) throws LdapException 276 { 277 throw new IllegalStateException( I18n.err( I18n.ERR_474 ) ); 278 } 279 } ); 280 281 attributeType.setEquality( matchingRule ); 282 attributeType.setSyntax( syntax ); 283 284 return attributeType; 285 } 286}