PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/java/org/owasp/esapi/crypto/CipherSpecTest.java

http://owasp-esapi-java.googlecode.com/
Java | 273 lines | 193 code | 38 blank | 42 comment | 22 complexity | bac35b1f65bdf5fbd505ff5c7272a163 MD5 | raw file
Possible License(s): BSD-3-Clause, CC-BY-SA-3.0
  1. package org.owasp.esapi.crypto;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.ObjectInputStream;
  7. import java.io.ObjectOutputStream;
  8. import junit.framework.TestCase;
  9. import junit.framework.TestSuite;
  10. import javax.crypto.Cipher;
  11. import org.junit.After;
  12. import org.junit.Before;
  13. import org.junit.Test;
  14. import org.owasp.esapi.ESAPI;
  15. import org.owasp.esapi.codecs.Hex;
  16. import org.owasp.esapi.crypto.CipherSpec;
  17. /** JUnit test to test CipherSpec class. */
  18. public class CipherSpecTest extends TestCase {
  19. private Cipher dfltAESCipher = null;
  20. private Cipher dfltECBCipher = null; // will be "AES/ECB/NoPadding";
  21. private Cipher dfltOtherCipher = null;
  22. private CipherSpec cipherSpec = null;
  23. private byte[] myIV = null;
  24. @Before public void setUp() throws Exception {
  25. // This will throw ConfigurationException if IV type is not set to
  26. // 'fixed', which it's not. (We have it set to 'random'.)
  27. // myIV = Hex.decode( ESAPI.securityConfiguration().getFixedIV() );
  28. myIV = Hex.decode( "0x000102030405060708090a0b0c0d0e0f" );
  29. dfltAESCipher = Cipher.getInstance("AES");
  30. dfltECBCipher = Cipher.getInstance("AES/ECB/NoPadding");
  31. dfltOtherCipher = Cipher.getInstance("Blowfish/OFB8/PKCS5Padding");
  32. assertTrue( dfltAESCipher != null );
  33. assertTrue( dfltECBCipher != null );
  34. assertTrue( dfltOtherCipher != null );
  35. cipherSpec = new CipherSpec(dfltOtherCipher);
  36. assertTrue( cipherSpec != null );
  37. }
  38. @After public void tearDown() throws Exception {
  39. // none
  40. }
  41. /** Test CipherSpec(String cipherXform, int keySize, int blockSize, final byte[] iv) */
  42. @Test public void testCipherSpecStringIntIntByteArray() {
  43. cipherSpec = new CipherSpec( "AES/CBC/NoPadding", 128, 8, myIV);
  44. assertTrue( cipherSpec != null );
  45. cipherSpec = null;
  46. boolean caughtException = false;
  47. try {
  48. // Invalid cipher xform -- empty
  49. cipherSpec = new CipherSpec( "", 128, 8, myIV);
  50. } catch( Throwable t ) {
  51. caughtException = true;
  52. }
  53. assertTrue( caughtException && (cipherSpec == null) );
  54. caughtException = false;
  55. try {
  56. // Invalid cipher xform -- missing padding scheme
  57. cipherSpec = new CipherSpec("AES/CBC", 128, 8, myIV);
  58. } catch( Throwable t ) {
  59. caughtException = true;
  60. }
  61. assertTrue( caughtException && (cipherSpec == null) );
  62. }
  63. /** CipherSpec(final Cipher cipher, int keySize) */
  64. @Test public void testCipherSpecCipherInt() {
  65. cipherSpec = new CipherSpec(dfltOtherCipher, 112);
  66. assertTrue( cipherSpec != null );
  67. assertTrue( cipherSpec.getCipherAlgorithm().equals("Blowfish"));
  68. assertTrue( cipherSpec.getCipherMode().equals("OFB8"));
  69. cipherSpec = new CipherSpec(dfltAESCipher, 256);
  70. assertTrue( cipherSpec != null );
  71. assertTrue( cipherSpec.getCipherAlgorithm().equals("AES"));
  72. assertTrue( cipherSpec.getCipherMode().equals("ECB") );
  73. assertTrue( cipherSpec.getPaddingScheme().equals("NoPadding") );
  74. // System.out.println("testCipherSpecInt(): " + cipherSpec);
  75. }
  76. /** Test CipherSpec(final byte[] iv) */
  77. @Test public void testCipherSpecByteArray() {
  78. assertTrue( myIV != null );
  79. assertTrue( myIV.length > 0 );
  80. cipherSpec = new CipherSpec(myIV);
  81. assertTrue( cipherSpec.getKeySize() ==
  82. ESAPI.securityConfiguration().getEncryptionKeyLength() );
  83. assertTrue( cipherSpec.getCipherTransformation().equals(
  84. ESAPI.securityConfiguration().getCipherTransformation() ) );
  85. }
  86. /** Test CipherSpec() */
  87. @Test public void testCipherSpec() {
  88. cipherSpec = new CipherSpec( dfltECBCipher );
  89. assertTrue( cipherSpec.getCipherTransformation().equals("AES/ECB/NoPadding") );
  90. assertTrue( cipherSpec.getIV() == null );
  91. cipherSpec = new CipherSpec(dfltOtherCipher);
  92. assertTrue( cipherSpec.getCipherMode().equals("OFB8") );
  93. }
  94. /** Test setCipherTransformation(String cipherXform) */
  95. @Test public void testSetCipherTransformation() {
  96. cipherSpec = new CipherSpec();
  97. cipherSpec.setCipherTransformation("AlgName/Mode/Padding");
  98. cipherSpec.getCipherAlgorithm().equals("AlgName/Mode/Padding");
  99. try {
  100. // Don't use null here as compiling JUnit tests disables assertion
  101. // checking so we get a NullPointerException here instead.
  102. cipherSpec.setCipherTransformation(""); // Throws IllegalArgumentException
  103. } catch (IllegalArgumentException e) {
  104. assertTrue(true); // Doesn't work w/ @Test(expected=IllegalArgumentException.class)
  105. }
  106. }
  107. /** Test getCipherTransformation() */
  108. @Test public void testGetCipherTransformation() {
  109. assertTrue( (new CipherSpec()).getCipherTransformation().equals("AES/CBC/PKCS5Padding") );
  110. }
  111. /** Test setKeySize() */
  112. @Test public void testSetKeySize() {
  113. assertTrue( (new CipherSpec()).setKeySize(56).getKeySize() == 56 );
  114. }
  115. /** Test getKeySize() */
  116. @Test public void testGetKeySize() {
  117. assertTrue( (new CipherSpec()).getKeySize() ==
  118. ESAPI.securityConfiguration().getEncryptionKeyLength() );
  119. }
  120. /** Test setBlockSize() */
  121. @Test public void testSetBlockSize() {
  122. try {
  123. cipherSpec.setBlockSize(0); // Throws AssertionError
  124. } catch (AssertionError e) {
  125. assertTrue(true); // Doesn't work w/ @Test(expected=AssertionError.class)
  126. }
  127. try {
  128. cipherSpec.setBlockSize(-1); // Throws AssertionError
  129. } catch (AssertionError e) {
  130. assertTrue(true); // Doesn't work w/ @Test(expected=AssertionError.class)
  131. }
  132. assertTrue( cipherSpec.setBlockSize(4).getBlockSize() == 4 );
  133. }
  134. /** Test getBlockSize() */
  135. @Test public void testGetBlockSize() {
  136. assertTrue( cipherSpec.getBlockSize() == 8 );
  137. }
  138. /** Test getCipherAlgorithm() */
  139. @Test public void testGetCipherAlgorithm() {
  140. assertTrue( cipherSpec.getCipherAlgorithm().equals("Blowfish") );
  141. }
  142. /** Test getCipherMode */
  143. @Test public void testGetCipherMode() {
  144. assertTrue( cipherSpec.getCipherMode().equals("OFB8") );
  145. }
  146. /** Test getPaddingScheme() */
  147. @Test public void testGetPaddingScheme() {
  148. assertTrue( cipherSpec.getPaddingScheme().equals("PKCS5Padding") );
  149. }
  150. /** Test setIV() */
  151. @Test public void testSetIV() {
  152. try {
  153. // Test that ECB mode allows a null IV
  154. cipherSpec = new CipherSpec(dfltECBCipher);
  155. cipherSpec.setIV(null);
  156. assertTrue(true);
  157. } catch ( AssertionError e) {
  158. assertFalse("Test failed; unexpected exception", false);
  159. }
  160. try {
  161. // Test that CBC mode does allows a null IV
  162. cipherSpec = new CipherSpec(dfltAESCipher);
  163. cipherSpec.setIV(null);
  164. assertFalse("Test failed; Expected exception not thrown", false);
  165. } catch ( AssertionError e) {
  166. assertTrue(true);
  167. }
  168. }
  169. /** Test requiresIV() */
  170. @Test public void testRequiresIV() {
  171. assertTrue( (new CipherSpec(dfltECBCipher)).requiresIV() == false );
  172. cipherSpec = new CipherSpec(dfltAESCipher);
  173. assertTrue( cipherSpec.getCipherMode().equals("ECB") );
  174. assertTrue( cipherSpec.requiresIV() == false );
  175. assertTrue( new CipherSpec(dfltOtherCipher).requiresIV() );
  176. }
  177. /** Test serialization */
  178. @Test public void testSerialization() {
  179. String filename = "cipherspec.ser";
  180. File serializedFile = new File(filename);
  181. boolean success = false;
  182. try {
  183. // Delete any old serialized file. If it fails, it's not
  184. // a big deal. If we can't overwrite it later, we'll get
  185. // an IOException.
  186. //
  187. // NOTE: FindBugs complains we are not checking return value here.
  188. // Guess what? We don't care!!!
  189. serializedFile.delete();
  190. cipherSpec = new CipherSpec( "AES/CBC/NoPadding", 128, 8, myIV);
  191. FileOutputStream fos = new FileOutputStream(filename);
  192. ObjectOutputStream out = new ObjectOutputStream(fos);
  193. out.writeObject(cipherSpec);
  194. out.close();
  195. fos.close();
  196. FileInputStream fis = new FileInputStream(filename);
  197. ObjectInputStream in = new ObjectInputStream(fis);
  198. CipherSpec restoredCipherSpec = (CipherSpec)in.readObject();
  199. in.close();
  200. fis.close();
  201. // check that cipherSpec and restoredCipherSpec are equal. Just
  202. // compare them via their string representations.
  203. assertEquals("Serialized restored CipherSpec differs from saved CipherSpec",
  204. cipherSpec.toString(), restoredCipherSpec.toString() );
  205. success = true;
  206. } catch(IOException ex) {
  207. ex.printStackTrace(System.err);
  208. fail("testSerialization(): Unexpected IOException: " + ex);
  209. } catch(ClassNotFoundException ex) {
  210. ex.printStackTrace(System.err);
  211. fail("testSerialization(): Unexpected ClassNotFoundException: " + ex);
  212. } finally {
  213. // If test succeeds, remove the file. If it fails, leave it behind
  214. // for further analysis.
  215. if ( success && serializedFile.exists() ) {
  216. boolean deleted = serializedFile.delete();
  217. if ( !deleted ) {
  218. try {
  219. System.err.println("Unable to delete file: " + serializedFile.getCanonicalPath() );
  220. } catch (IOException e) {
  221. ; // Ignore
  222. }
  223. }
  224. }
  225. }
  226. }
  227. /**
  228. * Run all the test cases in this suite.
  229. * This is to allow running from {@code org.owasp.esapi.AllTests}.
  230. */
  231. public static junit.framework.Test suite() {
  232. TestSuite suite = new TestSuite(CipherSpecTest.class);
  233. return suite;
  234. }
  235. }