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 ; 11 12 17 public class AUTHTest extends TestCase{ 18 Properties properties = new Properties (); 19 20 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 failException){ 41 fail(failException.getMessage()); 42 } 43 } 44 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 failException){ 65 fail(failException.getMessage()); 66 } 67 } 68 69 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 failException){ 94 fail(failException.getMessage()); 95 } 96 } 97 98 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 failException){ 123 fail(failException.getMessage()); 124 } 125 } 126 127 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 failException){ 152 fail(failException.getMessage()); 153 } 154 } 155 156 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 failException){ 181 fail(failException.getMessage()); 182 } 183 } 184 185 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 failException){ 204 fail(failException.getMessage()); 205 } 206 } 207 208 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 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 [] args) { 243 junit.textui.TestRunner.run(suite()); 244 } 245 246 } 247 | Popular Tags |