1 package com.openedit.users.filesystem; 2 3 import junit.framework.TestCase; 4 5 import com.openedit.users.StringEncrypter; 6 7 public class StringEncrypterTest extends TestCase 8 { 9 10 public void testEncryptionKeyIsDefined() 11 { 12 assertEquals( StringEncrypter.DEFAULT_ENCRYPTION_KEY, 13 "This is a fairly long phrase used to encrypt" ); 14 } 15 16 public void testThrowsErrorOnInvalidKeySpec() throws Exception 17 { 18 String encryptionScheme = "asdf"; 19 String encryptionKey = "123456789012345678901234567890"; 20 21 try 22 { 23 StringEncrypter encrypter = new StringEncrypter( encryptionScheme, 24 encryptionKey ); 25 } 26 catch (IllegalArgumentException e) 27 { 28 assertEquals( "Encryption scheme not supported: asdf", e.getMessage() ); 29 } 30 } 31 32 public void testEncryptsUsingDesEde() throws Exception 33 { 34 String stringToEncrypt = "test"; 35 String encryptionKey = "123456789012345678901234567890"; 36 String encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME; 37 38 StringEncrypter encrypter = new StringEncrypter( encryptionScheme, encryptionKey ); 39 String encryptedString = encrypter.encrypt( stringToEncrypt ); 40 41 assertEquals( "Ni2Bih3nCUU=", encryptedString ); 42 } 43 44 public void testEncryptsUsingDes() throws Exception 45 { 46 String stringToEncrypt = "test"; 47 String encryptionKey = "123456789012345678901234567890"; 48 String encryptionScheme = StringEncrypter.DES_ENCRYPTION_SCHEME; 49 50 StringEncrypter encrypter = new StringEncrypter( encryptionScheme, encryptionKey ); 51 String encryptedString = encrypter.encrypt( stringToEncrypt ); 52 53 assertEquals( "oEtoaxGK9ns=", encryptedString ); 54 } 55 56 public void testEncryptionKeyCanContainLetters() throws Exception 57 { 58 String string = "test"; 59 String encryptionKey = "ASDF asdf 1234 8983 jklasdf J2Jaf8"; 60 String encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME; 61 62 StringEncrypter encrypter = new StringEncrypter( encryptionScheme, encryptionKey ); 63 String encryptedString = encrypter.encrypt( string ); 64 65 assertEquals( "Q+UyPrxdge0=", encryptedString ); 66 } 67 68 public void testDecryptsUsingDesEde() throws Exception 69 { 70 String string = "Ni2Bih3nCUU="; 71 String encryptionKey = "123456789012345678901234567890"; 72 String encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME; 73 74 StringEncrypter encrypter = new StringEncrypter( encryptionScheme, encryptionKey ); 75 String decryptedString = encrypter.decrypt( string ); 76 77 assertEquals( "test", decryptedString ); 78 } 79 80 public void testDecryptsUsingDes() throws Exception 81 { 82 String string = "oEtoaxGK9ns="; 83 String encryptionKey = "123456789012345678901234567890"; 84 String encryptionScheme = StringEncrypter.DES_ENCRYPTION_SCHEME; 85 86 StringEncrypter encrypter = new StringEncrypter( encryptionScheme, encryptionKey ); 87 String decryptedString = encrypter.decrypt( string ); 88 89 assertEquals( "test", decryptedString ); 90 } 91 92 public void testCantInstantiateWithNullEncryptionKey() throws Exception 93 { 94 try 95 { 96 String encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME; 97 String encryptionKey = null; 98 99 StringEncrypter encrypter = new StringEncrypter( encryptionScheme, 100 encryptionKey ); 101 } 102 catch (IllegalArgumentException e) 103 { 104 assertEquals( "encryption key was null", e.getMessage() ); 105 } 106 107 } 108 109 public void testCantInstantiateWithEmptyEncryptionKey() throws Exception 110 { 111 try 112 { 113 String encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME; 114 String encryptionKey = ""; 115 116 StringEncrypter encrypter = new StringEncrypter( encryptionScheme, 117 encryptionKey ); 118 } 119 catch (IllegalArgumentException e) 120 { 121 assertEquals( "encryption key was less than 24 characters", e.getMessage() ); 122 } 123 124 } 125 126 public void testCantDecryptWithNullString() throws Exception 127 { 128 try 129 { 130 String encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME; 131 String encryptionKey = "123456789012345678901234"; 132 133 StringEncrypter encrypter = new StringEncrypter( encryptionScheme, 134 encryptionKey ); 135 encrypter.decrypt( null ); 136 } 137 catch (IllegalArgumentException e) 138 { 139 assertEquals( "encrypted string was null or empty", e.getMessage() ); 140 } 141 142 } 143 144 public void testCantDecryptWithEmptyString() throws Exception 145 { 146 try 147 { 148 String encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME; 149 String encryptionKey = "123456789012345678901234"; 150 151 StringEncrypter encrypter = new StringEncrypter( encryptionScheme, 152 encryptionKey ); 153 encrypter.decrypt( "" ); 154 } 155 catch (IllegalArgumentException e) 156 { 157 assertEquals( "encrypted string was null or empty", e.getMessage() ); 158 } 159 160 } 161 162 public void testCantEncryptWithNullString() throws Exception 163 { 164 try 165 { 166 String encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME; 167 String encryptionKey = "123456789012345678901234"; 168 169 StringEncrypter encrypter = new StringEncrypter( encryptionScheme, 170 encryptionKey ); 171 encrypter.encrypt( null ); 172 } 173 catch (IllegalArgumentException e) 174 { 175 assertEquals( "unencrypted string was null or empty", e.getMessage() ); 176 } 177 178 } 179 180 public void testCantEncryptWithEmptyString() throws Exception 181 { 182 try 183 { 184 String encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME; 185 String encryptionKey = "123456789012345678901234"; 186 187 StringEncrypter encrypter = new StringEncrypter( encryptionScheme, 188 encryptionKey ); 189 encrypter.encrypt( "" ); 190 } 191 catch (IllegalArgumentException e) 192 { 193 assertEquals( "unencrypted string was null or empty", e.getMessage() ); 194 } 195 196 } 197 } | Popular Tags |