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;
7 import javax.crypto.KeyGenerator;
8 import javax.crypto.SecretKey;
9 import junit.framework.TestCase;
10 import org.bouncycastle.jce.provider.BouncyCastleProvider;
12 * Exercises the <code>AttributeQueryHandle</code>
14 * @author Walter Hoehn wassa@columbia.edu
17 public class AQHTest extends TestCase {
18 protected SecretKey goodKey;
19 protected String testHs;
20 public AQHTest(String name) {
24 public static void main(String args[]) {
25 junit.textui.TestRunner.run(AQHTest.class);
29 * @see TestCase#setUp()
32 protected void setUp() {
34 Security.addProvider(new BouncyCastleProvider());
35 KeyGenerator gen = KeyGenerator.getInstance("DESede");
36 gen.init(new SecureRandom());
37 goodKey = gen.generateKey();
38 } catch (NoSuchAlgorithmException e) {
39 fail("Could not generate fixture (secret key)");
41 testHs = "http://www.test.com/HS";
44 * Tests the basic, creation, serialization, and unmarshalling of the <code>AttributeQueryHandle</code>
47 public void testAQH() {
50 AttributeQueryHandle originalAQH =
51 new AttributeQueryHandle("Walter", goodKey, 300000l, testHs);
52 //Ensure that a unique id was generated
53 assertNotNull("No unique id generated for handle", originalAQH.getHandleID());
54 String cacheHandleID = originalAQH.getHandleID();
55 //Ensure that the principal was set correctly
56 assertEquals("Principal incorrect", "Walter", originalAQH.getPrincipal());
57 //Test to see that the handle has not expired
58 //Hopefull this doesn't take more than 5 mintues to run :-)
59 assertTrue("AttributeQueryHandle unexpectedly expired.", (!originalAQH.isExpired()));
60 //Create a new AQH from the serialized first AQH
61 AttributeQueryHandle secondAQH =
62 new AttributeQueryHandle(originalAQH.serialize(), goodKey);
63 //Ensure that the principal was set correctly
64 assertEquals("Principal incorrect", "Walter", secondAQH.getPrincipal());
65 //Test to see that the handle has not expired
66 //Hopefull this doesn't take more than 5 mintues to run :-)
67 assertTrue("AttributeQueryHandle unexpectedly expired.", (!secondAQH.isExpired()));
68 //Make sure that the handle id matches that of the first object
70 "Improper unmarshalling of unique handle id",
72 secondAQH.getHandleID());
73 } catch (HandleException e) {
74 fail("Failed to create AttributeQueryHandle" + e);
78 * Ensure that <code>AttributeQueryHandle</code> objects expire correctly
80 public void testExpiration() {
82 AttributeQueryHandle aqh = new AttributeQueryHandle("Walter", goodKey, 1l, testHs);
84 assertTrue("AttributeQueryHandle failed to expire appropriately", aqh.isExpired());
85 } catch (InterruptedException e) {
86 } catch (HandleException e) {
87 fail("Failed to create AttributeQueryHandle" + e);
91 * Ensue that all of our UUIDs are not identical
93 public void testDups() {
95 AttributeQueryHandle aqh1 = new AttributeQueryHandle("Walter", goodKey, 1l, testHs);
96 AttributeQueryHandle aqh2 = new AttributeQueryHandle("Walter", goodKey, 1l, testHs);
98 "Reusing a UUID when creating new AQH",
99 !aqh1.getHandleID().equals(aqh2.getHandleID()));
100 } catch (HandleException e) {
101 fail("Failed to create AttributeQueryHandle" + e);