1 package edu.internet2.middleware.shibboleth.common;
2 import java.net.MalformedURLException;
4 import java.security.NoSuchAlgorithmException;
5 import java.security.SecureRandom;
6 import java.security.Security;
8 import javax.crypto.KeyGenerator;
9 import javax.crypto.SecretKey;
10 import junit.framework.TestCase;
11 import org.bouncycastle.jce.provider.BouncyCastleProvider;
14 * Exercises the <code>AttributeQueryHandle</code>
16 * @author Walter Hoehn wassa@columbia.edu
20 public class AQHTest extends TestCase {
21 protected SecretKey goodKey;
23 public AQHTest(String name) {
26 public static void main(String args[]) {
27 junit.textui.TestRunner.run(AQHTest.class);
29 protected void setUp() {
31 Security.addProvider(new BouncyCastleProvider());
32 KeyGenerator gen = KeyGenerator.getInstance("DESede");
33 gen.init(new SecureRandom());
34 goodKey = gen.generateKey();
35 } catch (NoSuchAlgorithmException e) {
36 fail("Could not generate fixture (secret key)");
40 testHs = new URL("http://www.test.com/HS");
41 } catch (MalformedURLException e) {
42 fail("Error initializing test Hs URL.");
46 * Tests the basic, creation, serialization, and unmarshalling of the <code>AttributeQueryHandle</code>
48 public void testAQH() {
52 AttributeQueryHandle originalAQH =
53 new AttributeQueryHandle("Walter", goodKey, 300000l, testHs);
55 //Ensure that a unique id was generated
57 "No unique id generated for handle",
58 originalAQH.getHandleID());
59 String cacheHandleID = originalAQH.getHandleID();
61 //Ensure that the principal was set correctly
63 "Principal incorrect",
65 originalAQH.getPrincipal());
67 //Test to see that the handle has not expired
68 //Hopefull this doesn't take more than 5 mintues to run :-)
70 "AttributeQueryHandle unexpectedly expired.",
71 (!originalAQH.isExpired()));
73 //Create a new AQH from the serialized first AQH
74 AttributeQueryHandle secondAQH =
75 new AttributeQueryHandle(originalAQH.serialize(), goodKey);
77 //Ensure that the principal was set correctly
79 "Principal incorrect",
81 secondAQH.getPrincipal());
83 //Test to see that the handle has not expired
84 //Hopefull this doesn't take more than 5 mintues to run :-)
86 "AttributeQueryHandle unexpectedly expired.",
87 (!secondAQH.isExpired()));
89 //Make sure that the handle id matches that of the first object
91 "Improper unmarshalling of unique handle id",
93 secondAQH.getHandleID());
95 } catch (HandleException e) {
96 fail("Failed to create AttributeQueryHandle" + e);
101 * Ensure that <code>AttributeQueryHandle</code> objects expire correctly
104 public void testExpiration() {
107 AttributeQueryHandle aqh =
108 new AttributeQueryHandle("Walter", goodKey, 1l, testHs);
111 "AttributeQueryHandle failed to expire appropriately",
113 } catch (InterruptedException e) {
114 } catch (HandleException e) {
115 fail("Failed to create AttributeQueryHandle" + e);
121 * Ensue that all of our UUIDs are not identical
124 public void testDups() {
127 AttributeQueryHandle aqh1 =
128 new AttributeQueryHandle("Walter", goodKey, 1l, testHs);
129 AttributeQueryHandle aqh2 =
130 new AttributeQueryHandle("Walter", goodKey, 1l, testHs);
131 assertTrue("Reusing a UUID when creating new AQH", !aqh1.getHandleID().equals(aqh2.getHandleID()));
132 } catch (HandleException e) {
133 fail("Failed to create AttributeQueryHandle" + e);