KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > protocols > AUTHTest


1 package org.jgroups.protocols;
2
3 import junit.framework.TestCase;
4 import junit.framework.Test;
5 import junit.framework.TestSuite;
6 import org.jgroups.auth.SimpleToken;
7 import org.jgroups.auth.AuthToken;
8 import org.jgroups.auth.MD5Token;
9
10 import java.util.Properties JavaDoc;
11
12 /**
13  * A set of JUnit tests for the AUTH protocol
14  *
15  * @author Chris Mills
16  */

17 public class AUTHTest extends TestCase{
18     Properties JavaDoc properties = new Properties JavaDoc();
19
20     /**
21      * Creates two SimpleToken objects with identical auth_values and authenticates one against the other
22      *
23      * Test fails if an exception is thrown or authentication fails
24      */

25     public void testSimpleToken(){
26         try{
27             properties.clear();
28
29             properties.put("auth_value", "chris");
30
31             SimpleToken token1 = new SimpleToken();
32             token1.setValue(properties);
33
34             properties.put("auth_value", "chris");
35
36             SimpleToken token2 = new SimpleToken();
37             token2.setValue(properties);
38
39             assertTrue(token1.authenticate(token2, null));
40         }catch(Exception JavaDoc failException){
41             fail(failException.getMessage());
42         }
43     }
44     /**
45      * Creates two SimpleToken objects with different auth_values and authenticates one against the other
46      *
47      * Test fails if an exception is thrown or authentication passes
48      */

49     public void testSimpleTokenMismatch(){
50         try{
51             properties.clear();
52
53             properties.put("auth_value", "chris");
54
55             SimpleToken token1 = new SimpleToken();
56             token1.setValue(properties);
57
58             properties.put("auth_value", "chrismills");
59
60             SimpleToken token2 = new SimpleToken();
61             token2.setValue(properties);
62
63             assertTrue(!token1.authenticate(token2, null));
64         }catch(Exception JavaDoc failException){
65             fail(failException.getMessage());
66         }
67     }
68
69     /**
70      * Creates two MD5Token objects with identical auth_values and authenticates one against the other
71      *
72      * Uses an MD5 hash type
73      *
74      * Test fails if an exception is thrown or authentication fails
75      */

76     public void testMD5Token(){
77         try{
78             properties.clear();
79
80             properties.put("auth_value", "chris");
81             properties.put("token_hash", "MD5");
82
83             MD5Token token1 = new MD5Token();
84             token1.setValue(properties);
85
86             properties.put("auth_value", "chris");
87             properties.put("token_hash", "MD5");
88
89             MD5Token token2 = new MD5Token();
90             token2.setValue(properties);
91
92             assertTrue(token1.authenticate(token2, null));
93         }catch(Exception JavaDoc failException){
94             fail(failException.getMessage());
95         }
96     }
97
98     /**
99      * Creates two MD5Token objects with different auth_values and authenticates one against the other
100      *
101      * Uses an MD5 hash type
102      *
103      * Test fails if an exception is thrown or authentication passes
104      */

105     public void testMD5TokenMismatch(){
106         try{
107             properties.clear();
108
109             properties.put("auth_value", "chris");
110             properties.put("token_hash", "MD5");
111
112             MD5Token token1 = new MD5Token();
113             token1.setValue(properties);
114
115             properties.put("auth_value", "chrismills");
116             properties.put("token_hash", "MD5");
117
118             MD5Token token2 = new MD5Token();
119             token2.setValue(properties);
120
121             assertTrue(!token1.authenticate(token2, null));
122         }catch(Exception JavaDoc failException){
123             fail(failException.getMessage());
124         }
125     }
126
127     /**
128      * Creates two MD5Token objects with identical auth_values and authenticates one against the other
129      *
130      * Uses an SHA hash type
131      *
132      * Test fails if an exception is thrown or authentication fails
133      */

134     public void testSHAToken(){
135         try{
136             properties.clear();
137
138             properties.put("auth_value", "chris");
139             properties.put("token_hash", "SHA");
140
141             MD5Token token1 = new MD5Token();
142             token1.setValue(properties);
143
144             properties.put("auth_value", "chris");
145             properties.put("token_hash", "SHA");
146
147             MD5Token token2 = new MD5Token();
148             token2.setValue(properties);
149
150             assertTrue(token1.authenticate(token2, null));
151         }catch(Exception JavaDoc failException){
152             fail(failException.getMessage());
153         }
154     }
155
156     /**
157      * Creates two MD5Token objects with different auth_values and authenticates one against the other
158      *
159      * Uses an SHA hash type
160      *
161      * Test fails if an exception is thrown or authentication passes
162      */

163     public void testSHATokenMismatch(){
164         try{
165             properties.clear();
166
167             properties.put("auth_value", "chris");
168             properties.put("token_hash", "SHA");
169
170             MD5Token token1 = new MD5Token();
171             token1.setValue(properties);
172
173             properties.put("auth_value", "chrismills");
174             properties.put("token_hash", "SHA");
175
176             MD5Token token2 = new MD5Token();
177             token2.setValue(properties);
178
179             assertTrue(!token1.authenticate(token2, null));
180         }catch(Exception JavaDoc failException){
181             fail(failException.getMessage());
182         }
183     }
184
185     /**
186      * Test to create an AuthHeader object and set and get the Token object
187      *
188      * Fails if an exception is thrown or the set and get don't equal the same object
189      */

190     public void testAuthHeader(){
191         try{
192             properties.clear();
193
194             properties.put("auth_value", "chris");
195
196             SimpleToken token1 = new SimpleToken();
197             token1.setValue(properties);
198
199             AuthHeader header = new AuthHeader();
200             header.setToken(token1);
201
202             assertTrue(token1 == header.getToken());
203         }catch(Exception JavaDoc failException){
204             fail(failException.getMessage());
205         }
206     }
207
208     /**
209      * Test to create an AuthHeader object and set and get the Token object
210      *
211      * Fails if an exception is thrown or the set and get equal the same object
212      */

213     public void testAuthHeaderDifferent(){
214         try{
215             properties.clear();
216
217             properties.put("auth_value", "chris");
218
219             SimpleToken token1 = new SimpleToken();
220             token1.setValue(properties);
221
222             properties.put("auth_value", "chris");
223
224             SimpleToken token2 = new SimpleToken();
225             token2.setValue(properties);
226
227             AuthHeader header = new AuthHeader();
228             header.setToken(token1);
229
230             assertTrue(!(token2 == header.getToken()));
231         }catch(Exception JavaDoc failException){
232             fail(failException.getMessage());
233         }
234     }
235
236
237     public static Test suite() {
238         TestSuite s = new TestSuite(AUTHTest.class);
239         return s;
240     }
241
242     public static void main(String JavaDoc[] args) {
243         junit.textui.TestRunner.run(suite());
244     }
245
246 }
247
Popular Tags