001: package org.jgroups.protocols;
002:
003: import junit.framework.TestCase;
004: import junit.framework.Test;
005: import junit.framework.TestSuite;
006: import org.jgroups.auth.SimpleToken;
007: import org.jgroups.auth.AuthToken;
008: import org.jgroups.auth.MD5Token;
009:
010: import java.util.Properties;
011:
012: /**
013: * A set of JUnit tests for the AUTH protocol
014: *
015: * @author Chris Mills
016: */
017: public class AUTHTest extends TestCase {
018: Properties properties = new Properties();
019:
020: /**
021: * Creates two SimpleToken objects with identical auth_values and authenticates one against the other
022: *
023: * Test fails if an exception is thrown or authentication fails
024: */
025: public void testSimpleToken() {
026: try {
027: properties.clear();
028:
029: properties.put("auth_value", "chris");
030:
031: SimpleToken token1 = new SimpleToken();
032: token1.setValue(properties);
033:
034: properties.put("auth_value", "chris");
035:
036: SimpleToken token2 = new SimpleToken();
037: token2.setValue(properties);
038:
039: assertTrue(token1.authenticate(token2, null));
040: } catch (Exception failException) {
041: fail(failException.getMessage());
042: }
043: }
044:
045: /**
046: * Creates two SimpleToken objects with different auth_values and authenticates one against the other
047: *
048: * Test fails if an exception is thrown or authentication passes
049: */
050: public void testSimpleTokenMismatch() {
051: try {
052: properties.clear();
053:
054: properties.put("auth_value", "chris");
055:
056: SimpleToken token1 = new SimpleToken();
057: token1.setValue(properties);
058:
059: properties.put("auth_value", "chrismills");
060:
061: SimpleToken token2 = new SimpleToken();
062: token2.setValue(properties);
063:
064: assertTrue(!token1.authenticate(token2, null));
065: } catch (Exception failException) {
066: fail(failException.getMessage());
067: }
068: }
069:
070: /**
071: * Creates two MD5Token objects with identical auth_values and authenticates one against the other
072: *
073: * Uses an MD5 hash type
074: *
075: * Test fails if an exception is thrown or authentication fails
076: */
077: public void testMD5Token() {
078: try {
079: properties.clear();
080:
081: properties.put("auth_value", "chris");
082: properties.put("token_hash", "MD5");
083:
084: MD5Token token1 = new MD5Token();
085: token1.setValue(properties);
086:
087: properties.put("auth_value", "chris");
088: properties.put("token_hash", "MD5");
089:
090: MD5Token token2 = new MD5Token();
091: token2.setValue(properties);
092:
093: assertTrue(token1.authenticate(token2, null));
094: } catch (Exception failException) {
095: fail(failException.getMessage());
096: }
097: }
098:
099: /**
100: * Creates two MD5Token objects with different auth_values and authenticates one against the other
101: *
102: * Uses an MD5 hash type
103: *
104: * Test fails if an exception is thrown or authentication passes
105: */
106: public void testMD5TokenMismatch() {
107: try {
108: properties.clear();
109:
110: properties.put("auth_value", "chris");
111: properties.put("token_hash", "MD5");
112:
113: MD5Token token1 = new MD5Token();
114: token1.setValue(properties);
115:
116: properties.put("auth_value", "chrismills");
117: properties.put("token_hash", "MD5");
118:
119: MD5Token token2 = new MD5Token();
120: token2.setValue(properties);
121:
122: assertTrue(!token1.authenticate(token2, null));
123: } catch (Exception failException) {
124: fail(failException.getMessage());
125: }
126: }
127:
128: /**
129: * Creates two MD5Token objects with identical auth_values and authenticates one against the other
130: *
131: * Uses an SHA hash type
132: *
133: * Test fails if an exception is thrown or authentication fails
134: */
135: public void testSHAToken() {
136: try {
137: properties.clear();
138:
139: properties.put("auth_value", "chris");
140: properties.put("token_hash", "SHA");
141:
142: MD5Token token1 = new MD5Token();
143: token1.setValue(properties);
144:
145: properties.put("auth_value", "chris");
146: properties.put("token_hash", "SHA");
147:
148: MD5Token token2 = new MD5Token();
149: token2.setValue(properties);
150:
151: assertTrue(token1.authenticate(token2, null));
152: } catch (Exception failException) {
153: fail(failException.getMessage());
154: }
155: }
156:
157: /**
158: * Creates two MD5Token objects with different auth_values and authenticates one against the other
159: *
160: * Uses an SHA hash type
161: *
162: * Test fails if an exception is thrown or authentication passes
163: */
164: public void testSHATokenMismatch() {
165: try {
166: properties.clear();
167:
168: properties.put("auth_value", "chris");
169: properties.put("token_hash", "SHA");
170:
171: MD5Token token1 = new MD5Token();
172: token1.setValue(properties);
173:
174: properties.put("auth_value", "chrismills");
175: properties.put("token_hash", "SHA");
176:
177: MD5Token token2 = new MD5Token();
178: token2.setValue(properties);
179:
180: assertTrue(!token1.authenticate(token2, null));
181: } catch (Exception failException) {
182: fail(failException.getMessage());
183: }
184: }
185:
186: /**
187: * Test to create an AuthHeader object and set and get the Token object
188: *
189: * Fails if an exception is thrown or the set and get don't equal the same object
190: */
191: public void testAuthHeader() {
192: try {
193: properties.clear();
194:
195: properties.put("auth_value", "chris");
196:
197: SimpleToken token1 = new SimpleToken();
198: token1.setValue(properties);
199:
200: AuthHeader header = new AuthHeader();
201: header.setToken(token1);
202:
203: assertTrue(token1 == header.getToken());
204: } catch (Exception failException) {
205: fail(failException.getMessage());
206: }
207: }
208:
209: /**
210: * Test to create an AuthHeader object and set and get the Token object
211: *
212: * Fails if an exception is thrown or the set and get equal the same object
213: */
214: public void testAuthHeaderDifferent() {
215: try {
216: properties.clear();
217:
218: properties.put("auth_value", "chris");
219:
220: SimpleToken token1 = new SimpleToken();
221: token1.setValue(properties);
222:
223: properties.put("auth_value", "chris");
224:
225: SimpleToken token2 = new SimpleToken();
226: token2.setValue(properties);
227:
228: AuthHeader header = new AuthHeader();
229: header.setToken(token1);
230:
231: assertTrue(!(token2 == header.getToken()));
232: } catch (Exception failException) {
233: fail(failException.getMessage());
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[] args) {
243: junit.textui.TestRunner.run(suite());
244: }
245:
246: }
|