Added encoding of usernames so that "||" can be handled.
[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 import javax.crypto.KeyGenerator;
8 import javax.crypto.SecretKey;
9 import junit.framework.TestCase;
10 import org.bouncycastle.jce.provider.BouncyCastleProvider;
11 /**
12  * Exercises the <code>AttributeQueryHandle</code>
13  * 
14  * @author Walter Hoehn wassa&#064;columbia.edu
15  *
16  */
17 public class AQHTest extends TestCase {
18         protected SecretKey goodKey;
19         protected String testHs;
20         public AQHTest(String name) {
21                 super(name);
22         }
23
24         public static void main(String args[]) {
25                 junit.textui.TestRunner.run(AQHTest.class);
26         }
27
28         /**
29          * @see TestCase#setUp()
30          */
31
32         protected void setUp() {
33                 try {
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)");
40                 }
41                 testHs = "http://www.test.com/HS";
42         }
43         /**
44          * Tests the basic, creation, serialization, and unmarshalling of the <code>AttributeQueryHandle</code>
45          */
46
47         public void testAQH() {
48                 try {
49                         //Create an AQH
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
69                         assertEquals(
70                                 "Improper unmarshalling of unique handle id",
71                                 cacheHandleID,
72                                 secondAQH.getHandleID());
73                 } catch (HandleException e) {
74                         fail("Failed to create AttributeQueryHandle" + e);
75                 }
76         }
77         /**
78          * Ensure that <code>AttributeQueryHandle</code> objects expire correctly
79          */
80         public void testExpiration() {
81                 try {
82                         AttributeQueryHandle aqh = new AttributeQueryHandle("Walter", goodKey, 1l, testHs);
83                         Thread.sleep(50);
84                         assertTrue("AttributeQueryHandle failed to expire appropriately", aqh.isExpired());
85                 } catch (InterruptedException e) {
86                 } catch (HandleException e) {
87                         fail("Failed to create AttributeQueryHandle" + e);
88                 }
89         }
90         /**
91          * Ensure that all of our UUIDs are not identical
92          */
93         public void testDups() {
94                 try {
95                         AttributeQueryHandle aqh1 = new AttributeQueryHandle("Walter", goodKey, 1l, testHs);
96                         AttributeQueryHandle aqh2 = new AttributeQueryHandle("Walter", goodKey, 1l, testHs);
97                         assertTrue(
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);
102                 }
103         }
104         
105         /**
106          * Make sure that it is works to create an AQH where the username contains
107          * the character that is used for field separation during serialization
108          */
109         
110         public void testSeparator() {
111                 
112                 try {
113                         
114                 String userName = "Test||Test";
115                 
116                 //Create an AQH
117                 AttributeQueryHandle originalAQH =
118                         new AttributeQueryHandle(userName, goodKey, 300000l, testHs);
119                         
120                 //Ensure that the principal was set correctly
121                 assertEquals("Principal incorrect", userName, originalAQH.getPrincipal());
122                 
123                 //Create a new AQH from the serialized first AQH
124                 AttributeQueryHandle secondAQH =
125                         new AttributeQueryHandle(originalAQH.serialize(), goodKey);
126                         
127                 //Ensure that the principal was set correctly
128                 assertEquals("Principal incorrect", userName, secondAQH.getPrincipal());
129                 
130                 } catch (HandleException e) {
131                         fail("Failed to create AttributeQueryHandle" + e);
132                 }
133                 
134                 
135         }
136 }