Importing Handle Service code.
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / common / AQHTest.java
1 package edu.internet2.middleware.shibboleth.common;
2 import java.security.NoSuchAlgorithmException;
3 import java.security.SecureRandom;
4 import java.security.Security;
5
6 import javax.crypto.KeyGenerator;
7 import javax.crypto.SecretKey;
8 import junit.framework.TestCase;
9 import org.bouncycastle.jce.provider.BouncyCastleProvider;
10
11 /**
12  * Exercises the <code>AttributeQueryHandle</code>
13  * 
14  * @author Walter Hoehn wassa&#064;columbia.edu
15  *
16  */
17
18 public class AQHTest extends TestCase {
19         protected SecretKey goodKey;
20         public AQHTest(String name) {
21                 super(name);
22         }
23         public static void main(String args[]) {
24                 junit.textui.TestRunner.run(AQHTest.class);
25         }
26         protected void setUp() {
27                 try {
28                         Security.addProvider(new BouncyCastleProvider());
29                         KeyGenerator gen = KeyGenerator.getInstance("DESede");
30                         gen.init(new SecureRandom());
31                         goodKey = gen.generateKey();
32                 } catch (NoSuchAlgorithmException e) {
33                         fail("Could not generate fixture (secret key)");
34                 }
35         }
36         /**
37          * Tests the basic, creation, serialization, and unmarshalling of the <code>AttributeQueryHandle</code>
38          */
39         public void testAQH() {
40                 try {
41
42                         //Create an AQH
43                         AttributeQueryHandle originalAQH =
44                                 new AttributeQueryHandle("Walter", goodKey, 300000l);
45
46                         //Ensure that a unique id was generated 
47                         assertNotNull(
48                                 "No unique id generated for handle",
49                                 originalAQH.getHandleID());
50                         String cacheHandleID = originalAQH.getHandleID();
51
52                         //Ensure that the principal was set correctly
53                         assertEquals(
54                                 "Principal incorrect",
55                                 "Walter",
56                                 originalAQH.getPrincipal());
57
58                         //Test to see that the handle has not expired   
59                         //Hopefull this doesn't take more than 5 mintues to run :-)
60                         assertTrue(
61                                 "AttributeQueryHandle unexpectedly expired.",
62                                 (!originalAQH.isExpired()));
63
64                         //Create a new AQH from the serialized first AQH
65                         AttributeQueryHandle secondAQH =
66                                 new AttributeQueryHandle(originalAQH.serialize(), goodKey);
67
68                         //Ensure that the principal was set correctly
69                         assertEquals(
70                                 "Principal incorrect",
71                                 "Walter",
72                                 secondAQH.getPrincipal());
73
74                         //Test to see that the handle has not expired   
75                         //Hopefull this doesn't take more than 5 mintues to run :-)
76                         assertTrue(
77                                 "AttributeQueryHandle unexpectedly expired.",
78                                 (!secondAQH.isExpired()));
79
80                         //Make sure that the handle id matches that of the first object
81                         assertEquals(
82                                 "Improper unmarshalling of unique handle id",
83                                 cacheHandleID,
84                                 secondAQH.getHandleID());
85
86                 } catch (HandleException e) {
87                         fail("Failed to create AttributeQueryHandle" + e);
88                 }
89         }
90
91         /**
92          * Ensure that <code>AttributeQueryHandle</code> objects expire correctly
93          */
94
95         public void testExpiration() {
96
97                 try {
98                         AttributeQueryHandle aqh =
99                                 new AttributeQueryHandle("Walter", goodKey, 1l);
100                         Thread.sleep(2);
101                         assertTrue(
102                                 "AttributeQueryHandle failed to expire appropriately",
103                                 aqh.isExpired());
104                 } catch (InterruptedException e) {
105                 } catch (HandleException e) {
106                         fail("Failed to create AttributeQueryHandle" + e);
107                 }
108
109         }
110 }