Added creation of a true UUID to the Attribute Query Handle, updated tests and HS...
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / common / AQHTest.java
1 package edu.internet2.middleware.shibboleth.common;
2 import java.net.MalformedURLException;
3 import java.net.URL;
4 import java.security.NoSuchAlgorithmException;
5 import java.security.SecureRandom;
6 import java.security.Security;
7
8 import javax.crypto.KeyGenerator;
9 import javax.crypto.SecretKey;
10 import junit.framework.TestCase;
11 import org.bouncycastle.jce.provider.BouncyCastleProvider;
12
13 /**
14  * Exercises the <code>AttributeQueryHandle</code>
15  * 
16  * @author Walter Hoehn wassa&#064;columbia.edu
17  *
18  */
19
20 public class AQHTest extends TestCase {
21         protected SecretKey goodKey;
22         protected URL testHs;
23         public AQHTest(String name) {
24                 super(name);
25         }
26         public static void main(String args[]) {
27                 junit.textui.TestRunner.run(AQHTest.class);
28         }
29         protected void setUp() {
30                 try {
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)");
37                 }
38                 
39                 try {
40                         testHs = new URL("http://www.test.com/HS");
41                 } catch (MalformedURLException e) {
42                         fail("Error initializing test Hs URL.");
43                 }
44         }
45         /**
46          * Tests the basic, creation, serialization, and unmarshalling of the <code>AttributeQueryHandle</code>
47          */
48         public void testAQH() {
49                 try {
50
51                         //Create an AQH
52                         AttributeQueryHandle originalAQH =
53                                 new AttributeQueryHandle("Walter", goodKey, 300000l, testHs);
54
55                         //Ensure that a unique id was generated 
56                         assertNotNull(
57                                 "No unique id generated for handle",
58                                 originalAQH.getHandleID());
59                         String cacheHandleID = originalAQH.getHandleID();
60
61                         //Ensure that the principal was set correctly
62                         assertEquals(
63                                 "Principal incorrect",
64                                 "Walter",
65                                 originalAQH.getPrincipal());
66
67                         //Test to see that the handle has not expired   
68                         //Hopefull this doesn't take more than 5 mintues to run :-)
69                         assertTrue(
70                                 "AttributeQueryHandle unexpectedly expired.",
71                                 (!originalAQH.isExpired()));
72
73                         //Create a new AQH from the serialized first AQH
74                         AttributeQueryHandle secondAQH =
75                                 new AttributeQueryHandle(originalAQH.serialize(), goodKey);
76
77                         //Ensure that the principal was set correctly
78                         assertEquals(
79                                 "Principal incorrect",
80                                 "Walter",
81                                 secondAQH.getPrincipal());
82
83                         //Test to see that the handle has not expired   
84                         //Hopefull this doesn't take more than 5 mintues to run :-)
85                         assertTrue(
86                                 "AttributeQueryHandle unexpectedly expired.",
87                                 (!secondAQH.isExpired()));
88
89                         //Make sure that the handle id matches that of the first object
90                         assertEquals(
91                                 "Improper unmarshalling of unique handle id",
92                                 cacheHandleID,
93                                 secondAQH.getHandleID());
94
95                 } catch (HandleException e) {
96                         fail("Failed to create AttributeQueryHandle" + e);
97                 }
98         }
99
100         /**
101          * Ensure that <code>AttributeQueryHandle</code> objects expire correctly
102          */
103
104         public void testExpiration() {
105
106                 try {
107                         AttributeQueryHandle aqh =
108                                 new AttributeQueryHandle("Walter", goodKey, 1l, testHs);
109                         Thread.sleep(2);
110                         assertTrue(
111                                 "AttributeQueryHandle failed to expire appropriately",
112                                 aqh.isExpired());
113                 } catch (InterruptedException e) {
114                 } catch (HandleException e) {
115                         fail("Failed to create AttributeQueryHandle" + e);
116                 }
117
118         }
119         
120         /**
121          * Ensue that all of our UUIDs are not identical
122          */
123         
124         public void testDups() {
125                 
126                 try {
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);
134                 }
135                 
136                 
137         }
138 }