001package org.apache.archiva.redback.keys; 002 003/* 004 * Copyright 2001-2006 The Apache Software Foundation. 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * 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, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019import junit.framework.TestCase; 020import org.junit.Test; 021import org.junit.runner.RunWith; 022import org.springframework.test.context.ContextConfiguration; 023import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 024 025import java.text.SimpleDateFormat; 026import java.util.*; 027 028/** 029 * KeyManagerTestCase 030 * 031 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> 032 * 033 */ 034@RunWith( SpringJUnit4ClassRunner.class ) 035@ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml","classpath*:/spring-context.xml"} ) 036public class KeyManagerTestCase 037 extends TestCase 038{ 039 private KeyManager manager; 040 041 public KeyManager getKeyManager() 042 { 043 return manager; 044 } 045 046 public void setKeyManager( KeyManager manager ) 047 { 048 this.manager = manager; 049 } 050 051 private void assertSameDates( Date expected, Date actual ) 052 { 053 if ( ( expected == null ) && ( actual != null ) ) 054 { 055 fail( "Expected date is null, actual date [" + actual + "]." ); 056 } 057 058 if ( ( expected != null ) && ( actual == null ) ) 059 { 060 fail( "Expected date [" + expected + "], actual date is null." ); 061 } 062 063 SimpleDateFormat format = new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss Z" ); 064 assertEquals( format.format( expected ), format.format( actual ) ); 065 } 066 067 @Test 068 public void testNormal() 069 throws KeyNotFoundException, KeyManagerException 070 { 071 String principal = "foo"; 072 String purpose = "Testing"; 073 074 AuthenticationKey created = getKeyManager().createKey( principal, purpose, 15 ); 075 076 assertNotNull( created ); 077 assertNotNull( created.getKey() ); 078 assertNotNull( created.getDateCreated() ); 079 assertNotNull( created.getDateExpires() ); 080 081 assertEquals( principal, created.getForPrincipal() ); 082 assertEquals( purpose, created.getPurpose() ); 083 084 Date expectedCreated = created.getDateCreated(); 085 Date expectedExpires = created.getDateExpires(); 086 087 String expectedKey = created.getKey(); 088 089 AuthenticationKey found = getKeyManager().findKey( expectedKey ); 090 091 assertEquals( expectedKey, found.getKey() ); 092 assertEquals( principal, found.getForPrincipal() ); 093 assertEquals( purpose, found.getPurpose() ); 094 assertSameDates( expectedCreated, found.getDateCreated() ); 095 assertSameDates( expectedExpires, found.getDateExpires() ); 096 } 097 098 @Test 099 public void testGetAllKeys() 100 throws KeyManagerException 101 { 102 getKeyManager().eraseDatabase(); 103 AuthenticationKey created1 = getKeyManager().createKey( "foo", "Testing", 15 ); 104 AuthenticationKey created2 = getKeyManager().createKey( "bar", "Something", 23 ); 105 106 assertNotNull( created1 ); 107 assertNotNull( created2 ); 108 109 assertEquals( "foo", created1.getForPrincipal() ); 110 assertEquals( "Testing", created1.getPurpose() ); 111 112 assertEquals( "bar", created2.getForPrincipal() ); 113 assertEquals( "Something", created2.getPurpose() ); 114 115 System.out.println("foo key "+created1.getKey()); 116 System.out.println("bar key "+created2.getKey()); 117 List<AuthenticationKey> keys = new ArrayList<>(getKeyManager().getAllKeys()); 118 Collections.sort( keys, new Comparator<AuthenticationKey>() 119 { 120 public int compare( AuthenticationKey key1, AuthenticationKey key2 ) 121 { 122 System.out.println("Compare "+key2.getForPrincipal()+key2.getKey()+" - "+key1.getForPrincipal()+key1.getKey()); 123 return key2.getForPrincipal().compareTo( key1.getForPrincipal() ); 124 } 125 } ); 126 127 AuthenticationKey found = (AuthenticationKey) keys.get( 0 ); 128 assertEquals( created1.getKey(), found.getKey() ); 129 assertEquals( "foo", found.getForPrincipal() ); 130 assertEquals( "Testing", found.getPurpose() ); 131 assertSameDates( created1.getDateCreated(), found.getDateCreated() ); 132 assertSameDates( created1.getDateExpires(), found.getDateExpires() ); 133 134 found = (AuthenticationKey) keys.get( 1 ); 135 assertEquals( created2.getKey(), found.getKey() ); 136 assertEquals( "bar", found.getForPrincipal() ); 137 assertEquals( "Something", found.getPurpose() ); 138 assertSameDates( created2.getDateCreated(), found.getDateCreated() ); 139 assertSameDates( created2.getDateExpires(), found.getDateExpires() ); 140 } 141 142 @Test 143 public void testNotThere() 144 throws KeyManagerException 145 { 146 String principal = "foo"; 147 String purpose = "Testing"; 148 149 AuthenticationKey created = getKeyManager().createKey( principal, purpose, 15 ); 150 151 assertNotNull( created ); 152 assertNotNull( created.getKey() ); 153 assertNotNull( created.getDateCreated() ); 154 assertNotNull( created.getDateExpires() ); 155 156 assertEquals( principal, created.getForPrincipal() ); 157 assertEquals( purpose, created.getPurpose() ); 158 159 try 160 { 161 getKeyManager().findKey( "deadbeefkey" ); 162 fail( "Invalid Key should not have been found." ); 163 } 164 catch ( KeyNotFoundException e ) 165 { 166 // Expected path for this test. 167 } 168 } 169 170 @Test 171 public void testExpired() 172 throws KeyManagerException, InterruptedException 173 { 174 String principal = "foo"; 175 String purpose = "Testing"; 176 177 AuthenticationKey created = getKeyManager().createKey( principal, purpose, 0 ); 178 179 assertNotNull( created ); 180 assertNotNull( created.getKey() ); 181 assertNotNull( created.getDateCreated() ); 182 assertNotNull( created.getDateExpires() ); 183 184 assertEquals( principal, created.getForPrincipal() ); 185 assertEquals( purpose, created.getPurpose() ); 186 187 String expectedKey = created.getKey(); 188 189 try 190 { 191 Thread.sleep( 500 ); // Sleep to let it expire 192 getKeyManager().findKey( expectedKey ); 193 fail( "Expired Key should not have been found." ); 194 } 195 catch ( KeyNotFoundException e ) 196 { 197 // Expected path for this test. 198 } 199 } 200 201 @Test 202 public void testPermanent() 203 throws KeyManagerException 204 { 205 String principal = "foo"; 206 String purpose = "Testing"; 207 208 AuthenticationKey created = getKeyManager().createKey( principal, purpose, -1 ); 209 210 assertNotNull( created ); 211 assertNotNull( created.getKey() ); 212 assertNotNull( created.getDateCreated() ); 213 assertNull( created.getDateExpires() ); 214 215 assertEquals( principal, created.getForPrincipal() ); 216 assertEquals( purpose, created.getPurpose() ); 217 218 Date expectedCreated = created.getDateCreated(); 219 220 String expectedKey = created.getKey(); 221 222 AuthenticationKey found = getKeyManager().findKey( expectedKey ); 223 224 assertEquals( expectedKey, found.getKey() ); 225 assertEquals( principal, found.getForPrincipal() ); 226 assertEquals( purpose, found.getPurpose() ); 227 assertSameDates( expectedCreated, found.getDateCreated() ); 228 assertNull( found.getDateExpires() ); 229 } 230}