001/* 002 * ModeShape (http://www.modeshape.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.modeshape.common.text; 017 018import static org.hamcrest.core.Is.is; 019import static org.hamcrest.core.IsNull.notNullValue; 020import static org.junit.Assert.assertThat; 021import java.util.HashSet; 022import java.util.Set; 023import org.modeshape.common.util.SecureHash.Algorithm; 024import org.junit.Before; 025import org.junit.Test; 026 027/** 028 * 029 */ 030public class SecureHashTextEncoderTest { 031 032 private SecureHashTextEncoder encoder = new SecureHashTextEncoder(Algorithm.SHA_1); 033 private SecureHashTextEncoder shortEncoder = new SecureHashTextEncoder(Algorithm.SHA_1, 4); 034 private SecureHashTextEncoder md5Encoder = new SecureHashTextEncoder(Algorithm.MD5); 035 private Set<String> alreadyEncoded = new HashSet<String>(); 036 private Set<String> alreadyEncodedShort = new HashSet<String>(); 037 private Set<String> alreadyEncodedMd5 = new HashSet<String>(); 038 039 @Before 040 public void beforeEach() { 041 042 } 043 044 protected void checkEncoding( String input ) { 045 assertThat(alreadyEncoded.add(checkEncoding(encoder, input)), is(true)); 046 assertThat(alreadyEncodedShort.add(checkEncoding(shortEncoder, input)), is(true)); 047 assertThat(alreadyEncodedMd5.add(checkEncoding(md5Encoder, input)), is(true)); 048 } 049 050 protected String checkEncoding( SecureHashTextEncoder encoder, 051 String input ) { 052 String output = encoder.encode(input); 053 assertThat(output, is(notNullValue())); 054 assertThat(output.length() <= encoder.getMaxLength(), is(true)); 055 return output; 056 } 057 058 @Test 059 public void shouldEncodeAlphabeticCharacters() { 060 checkEncoding("abcdefghijklmnopqrstuvwxyz"); 061 checkEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); 062 } 063 064 @Test 065 public void shouldEncodeNumericCharacters() { 066 checkEncoding("0123456789"); 067 } 068 069 @Test 070 public void shouldEncodePunctuationCharacters() { 071 checkEncoding("~`!@#$%^&()-_+={}\\;\"'<,>.?"); 072 } 073 074 @Test 075 public void shouldEncodeUrlsAndHaveNoDuplicates() { 076 checkEncoding("http://www.jboss.org"); 077 checkEncoding("http://www.jboss.org/"); 078 checkEncoding("http://www.modeshape.org"); 079 checkEncoding("http://www.modeshape.org/1.0"); 080 checkEncoding("http://www.modeshape.org/internal/1.0"); 081 checkEncoding("http://www.jcp.org/jcr/1.0"); 082 checkEncoding("http://www.jcp.org/jcr/nt/1.0"); 083 checkEncoding("http://www.jcp.org/jcr/mix/1.0"); 084 checkEncoding("http://www.jcp.org/jcr/sv/1.0"); 085 checkEncoding("http://www.acme.com/this/is/a/really/long/url/this/is/a/really/long/url/this/is/a/really/long/url/this/is/a/really/long/url/21?x=1&z=2"); 086 checkEncoding("http://www.acme.com/this/is/a/really/long/url/this/is/a/really/long/url/this/is/a/really/long/url/this/is/a/really/long/url/21?x=1&z=3"); 087 // System.out.println(alreadyEncoded); 088 // System.out.println(alreadyEncodedShort); 089 // System.out.println(alreadyEncodedMd5); 090 } 091}