You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by sm...@apache.org on 2006/04/26 11:14:05 UTC

svn commit: r397152 - /incubator/harmony/enhanced/classlib/trunk/modules/crypto/src/test/java/tests/api/javax/crypto/CipherTest.java

Author: smishura
Date: Wed Apr 26 02:13:58 2006
New Revision: 397152

URL: http://svn.apache.org/viewcvs?rev=397152&view=rev
Log:
Utilizing JUnit's assert methods and exception handling

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/crypto/src/test/java/tests/api/javax/crypto/CipherTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/crypto/src/test/java/tests/api/javax/crypto/CipherTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/crypto/src/test/java/tests/api/javax/crypto/CipherTest.java?rev=397152&r1=397151&r2=397152&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/crypto/src/test/java/tests/api/javax/crypto/CipherTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/crypto/src/test/java/tests/api/javax/crypto/CipherTest.java Wed Apr 26 02:13:58 2006
@@ -39,14 +39,10 @@
 	/**
 	 * @tests javax.crypto.Cipher#getInstance(java.lang.String)
 	 */
-	public void test_getInstanceLjava_lang_String() {
-		try {
-			Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
-			assertNotNull("Received a null Cipher instance", cipher);
-		} catch (Exception e) {
-			fail("Could not find cipher");
-		}
-	}
+    public void test_getInstanceLjava_lang_String() throws Exception {
+        Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
+        assertNotNull("Received a null Cipher instance", cipher);
+    }
 
 	/**
 	 * @tests javax.crypto.Cipher#getInstance(java.lang.String,
@@ -54,24 +50,22 @@
 	 */
 	public void test_getInstanceLjava_lang_StringLjava_lang_String()
             throws Exception {
+
         Provider[] providers = Security.getProviders("Cipher.DES");
-        if (providers != null) {
-            for (int i = 0; i < providers.length; i++) {
-                Cipher cipher = Cipher.getInstance("DES", providers[i]
-                        .getName());
-                assertNotNull("Cipher.getInstance() returned a null value",
-                        cipher);
-
-                // Exception case
-                try {
-                    cipher = Cipher.getInstance("DoBeDoBeDo", providers[i]);
-                    fail("Should have thrown an NoSuchAlgorithmException");
-                } catch (NoSuchAlgorithmException e) {
-                    // Expected
-                }
-            }// end for
-        } else {
-            fail("No installed providers support Cipher.DES");
+        
+        assertNotNull("No installed providers support Cipher.DES", providers);
+
+        for (int i = 0; i < providers.length; i++) {
+            Cipher cipher = Cipher.getInstance("DES", providers[i].getName());
+            assertNotNull("Cipher.getInstance() returned a null value", cipher);
+
+            // Exception case
+            try {
+                cipher = Cipher.getInstance("DoBeDoBeDo", providers[i]);
+                fail("Should have thrown an NoSuchAlgorithmException");
+            } catch (NoSuchAlgorithmException e) {
+                // Expected
+            }
         }
 
         // Exception case
@@ -95,371 +89,273 @@
 	 * @tests javax.crypto.Cipher#getInstance(java.lang.String,
 	 *        java.security.Provider)
 	 */
-	public void test_getInstanceLjava_lang_StringLjava_security_Provider() {
-		try {
-			Provider[] providers = Security.getProviders("Cipher.DES");
-			if (providers != null) {
-				for (int i = 0; i < providers.length; i++) {
-					Cipher cipher = Cipher.getInstance("DES", providers[i]);
-					assertNotNull("Cipher.getInstance() returned a null value",
-							cipher);
-				}// end for
-			} else {
-				fail("No installed providers support Cipher.DES");
-			}
-		} catch (Exception e) {
-			fail("Unexpected exception finding cipher : " + e);
-		}
-	}
+    public void test_getInstanceLjava_lang_StringLjava_security_Provider()
+            throws Exception {
+
+        Provider[] providers = Security.getProviders("Cipher.DES");
+
+        assertNotNull("No installed providers support Cipher.DES", providers);
+
+        for (int i = 0; i < providers.length; i++) {
+            Cipher cipher = Cipher.getInstance("DES", providers[i]);
+            assertNotNull("Cipher.getInstance() returned a null value", cipher);
+        }
+    }
 
 	/**
 	 * @tests javax.crypto.Cipher#getProvider()
 	 */
-	public void test_getProvider() {
-		try {
-			Provider[] providers = Security.getProviders("Cipher.AES");
-			if (providers != null) {
-				for (int i = 0; i < providers.length; i++) {
-					Provider provider = providers[i];
-					Cipher cipher = Cipher.getInstance("AES", provider
-							.getName());
-					Provider cipherProvider = cipher.getProvider();
-					assertTrue("Cipher provider is not the same as that "
-							+ "provided as parameter to getInstance()",
-							cipherProvider.equals(provider));
-				}// end for
-			} else {
-				fail("No providers support Cipher.AES");
-			}
-		} catch (Exception e) {
-			fail("Unexpected exception " + e);
-		}
-	}
+    public void test_getProvider() throws Exception {
+
+        Provider[] providers = Security.getProviders("Cipher.AES");
+
+        assertNotNull("No providers support Cipher.AES", providers);
+
+        for (int i = 0; i < providers.length; i++) {
+            Provider provider = providers[i];
+            Cipher cipher = Cipher.getInstance("AES", provider.getName());
+            Provider cipherProvider = cipher.getProvider();
+            assertTrue("Cipher provider is not the same as that "
+                    + "provided as parameter to getInstance()", cipherProvider
+                    .equals(provider));
+        }
+    }
 
 	/**
 	 * @tests javax.crypto.Cipher#getAlgorithm()
 	 */
-	public void test_getAlgorithm() {
-		final String algorithm = "DESede/CBC/PKCS5Padding";
-		try {
-			Cipher cipher = Cipher.getInstance(algorithm);
-			assertTrue("Cipher algorithm does not match", cipher.getAlgorithm()
-					.equals(algorithm));
-		} catch (Exception e) {
-			fail("Unexpected Exception");
-		}
-	}
+    public void test_getAlgorithm() throws Exception {
+        final String algorithm = "DESede/CBC/PKCS5Padding";
+
+        Cipher cipher = Cipher.getInstance(algorithm);
+        assertTrue("Cipher algorithm does not match", cipher.getAlgorithm()
+                .equals(algorithm));
+    }
 
 	/**
 	 * @tests javax.crypto.Cipher#getBlockSize()
 	 */
-	public void test_getBlockSize() {
-		final String algorithm = "DESede/CBC/PKCS5Padding";
-		try {
-			Cipher cipher = Cipher.getInstance(algorithm);
-			assertEquals("Block size does not match", 8, cipher.getBlockSize());
-		} catch (Exception e) {
-			fail("Unexpected Exception");
-		}
-	}
+    public void test_getBlockSize() throws Exception {
+        final String algorithm = "DESede/CBC/PKCS5Padding";
+
+        Cipher cipher = Cipher.getInstance(algorithm);
+        assertEquals("Block size does not match", 8, cipher.getBlockSize());
+    }
 
 	/**
 	 * @tests javax.crypto.Cipher#getOutputSize(int)
 	 */
-	public void test_getOutputSizeI() {
-		final String algorithm = "DESede";
-		final int keyLen = 168;
-
-		Key cipherKey = null;
-		SecureRandom sr = new SecureRandom();
-		Cipher cipher = null;
-
-		try {
-			KeyGenerator kg = null;
-			try {
-				kg = KeyGenerator.getInstance(algorithm);
-				kg.init(keyLen, new SecureRandom());
-			} catch (NoSuchAlgorithmException e) {
-				fail("Caught a NoSuchAlgorithmException : " + e);
-			}
-			cipherKey = kg.generateKey();
-
-			cipher = Cipher.getInstance(algorithm + "/ECB/PKCS5Padding");
-			cipher.init(Cipher.ENCRYPT_MODE, cipherKey, sr);
-		} catch (Exception e) {
-			fail("Setup failed");
-		}
-
-		// A 25-byte input could result in at least 4 8-byte blocks
-		int result = cipher.getOutputSize(25);
-		assertTrue("Output size too small", result > 31);
-
-		// A 8-byte input should result in 2 8-byte blocks
-		result = cipher.getOutputSize(8);
-		assertTrue("Output size too small", result > 15);
-	}
+    public void test_getOutputSizeI() throws Exception {
+        final String algorithm = "DESede";
+        final int keyLen = 168;
+
+        Key cipherKey = null;
+        SecureRandom sr = new SecureRandom();
+        Cipher cipher = null;
+
+        KeyGenerator kg = KeyGenerator.getInstance(algorithm);
+        kg.init(keyLen, new SecureRandom());
+        cipherKey = kg.generateKey();
+
+        cipher = Cipher.getInstance(algorithm + "/ECB/PKCS5Padding");
+        cipher.init(Cipher.ENCRYPT_MODE, cipherKey, sr);
+
+        // A 25-byte input could result in at least 4 8-byte blocks
+        int result = cipher.getOutputSize(25);
+        assertTrue("Output size too small", result > 31);
+
+        // A 8-byte input should result in 2 8-byte blocks
+        result = cipher.getOutputSize(8);
+        assertTrue("Output size too small", result > 15);
+    }
 
 	/**
 	 * @tests javax.crypto.Cipher#getIV()
 	 * @tests javax.crypto.Cipher#init(int, java.security.Key,
 	 *        java.security.AlgorithmParameters)
 	 */
-	public void test_getIV() {
-		/*
-		 * If this test is changed, implement the following:
-		 * test_initILjava_security_KeyLjava_security_AlgorithmParameters()
-		 */
-		final String algorithm = "DESede";
-		final int keyLen = 168;
-
-		Key cipherKey = null;
-		SecureRandom sr = new SecureRandom();
-		Cipher cipher = null;
-
-		byte[] iv = null;
-
-		try {
-			KeyGenerator kg = null;
-			try {
-				kg = KeyGenerator.getInstance(algorithm);
-				kg.init(keyLen, new SecureRandom());
-			} catch (NoSuchAlgorithmException e) {
-				fail("Unexpected NoSuchAlgorithmException : " + e);
-			}
-
-			cipherKey = kg.generateKey();
-
-			iv = new byte[8];
-			sr.nextBytes(iv);
-			AlgorithmParameters ap = AlgorithmParameters.getInstance(algorithm);
-			ap.init(iv, "RAW");
-
-			cipher = Cipher.getInstance(algorithm + "/CBC/PKCS5Padding");
-			cipher.init(Cipher.ENCRYPT_MODE, cipherKey, ap);
-
-		} catch (Exception e) {
-			fail("Setup error");
-		}
+    public void test_getIV() throws Exception {
+        /*
+         * If this test is changed, implement the following:
+         * test_initILjava_security_KeyLjava_security_AlgorithmParameters()
+         */
+        final String algorithm = "DESede";
+        final int keyLen = 168;
 
-		byte[] cipherIV = cipher.getIV();
+        Key cipherKey = null;
+        SecureRandom sr = new SecureRandom();
+        Cipher cipher = null;
 
-		assertTrue("IVs differ", Arrays.equals(cipherIV, iv));
-	}
+        byte[] iv = null;
+
+        KeyGenerator kg = KeyGenerator.getInstance(algorithm);
+        kg.init(keyLen, new SecureRandom());
+
+        cipherKey = kg.generateKey();
+
+        iv = new byte[8];
+        sr.nextBytes(iv);
+        AlgorithmParameters ap = AlgorithmParameters.getInstance(algorithm);
+        ap.init(iv, "RAW");
+
+        cipher = Cipher.getInstance(algorithm + "/CBC/PKCS5Padding");
+        cipher.init(Cipher.ENCRYPT_MODE, cipherKey, ap);
+
+        byte[] cipherIV = cipher.getIV();
+
+        assertTrue("IVs differ", Arrays.equals(cipherIV, iv));
+    }
 
 	/**
 	 * @tests javax.crypto.Cipher#getParameters()
 	 * @tests javax.crypto.Cipher#init(int, java.security.Key,
 	 *        java.security.AlgorithmParameters, java.security.SecureRandom)
 	 */
-	public void test_getParameters() {
+    public void test_getParameters() throws Exception {
 
-		/*
-		 * If this test is changed, implement the following:
-		 * test_initILjava_security_KeyLjava_security_AlgorithmParametersLjava_security_SecureRandom()
-		 */
-		final String algorithm = "DESede";
-		final int keyLen = 168;
-
-		Key cipherKey = null;
-		SecureRandom sr = new SecureRandom();
-		Cipher cipher = null;
-
-		byte[] apEncoding = null;
-
-		byte[] iv = null;
-
-		try {
-			KeyGenerator kg = null;
-			try {
-				kg = KeyGenerator.getInstance(algorithm);
-				kg.init(keyLen, new SecureRandom());
-			} catch (NoSuchAlgorithmException e) {
-				fail("Caught a NoSuchAlgorithmException : " + e);
-			}
-			cipherKey = kg.generateKey();
-
-			iv = new byte[8];
-			sr.nextBytes(iv);
-
-			AlgorithmParameters ap = AlgorithmParameters.getInstance("DESede");
-			ap.init(iv, "RAW");
-			apEncoding = ap.getEncoded("ASN.1");
-
-			cipher = Cipher.getInstance(algorithm + "/CBC/PKCS5Padding");
-			cipher.init(Cipher.ENCRYPT_MODE, cipherKey, ap, sr);
-
-		} catch (Exception e) {
-			fail("Setup error");
-		}
-
-		try {
-			byte[] cipherParmsEnc = cipher.getParameters().getEncoded("ASN.1");
-			assertTrue("Parameters differ", Arrays.equals(apEncoding,
-					cipherParmsEnc));
-		} catch (IOException e) {
-			fail("Parameter encoding problem");
-		}
+        /*
+         * If this test is changed, implement the following:
+         * test_initILjava_security_KeyLjava_security_AlgorithmParametersLjava_security_SecureRandom()
+         */
+        final String algorithm = "DESede";
+        final int keyLen = 168;
 
-	}
+        Key cipherKey = null;
+        SecureRandom sr = new SecureRandom();
+        Cipher cipher = null;
+
+        byte[] apEncoding = null;
+
+        byte[] iv = null;
+
+        KeyGenerator kg = KeyGenerator.getInstance(algorithm);
+        kg.init(keyLen, new SecureRandom());
+        cipherKey = kg.generateKey();
+
+        iv = new byte[8];
+        sr.nextBytes(iv);
+
+        AlgorithmParameters ap = AlgorithmParameters.getInstance("DESede");
+        ap.init(iv, "RAW");
+        apEncoding = ap.getEncoded("ASN.1");
+
+        cipher = Cipher.getInstance(algorithm + "/CBC/PKCS5Padding");
+        cipher.init(Cipher.ENCRYPT_MODE, cipherKey, ap, sr);
+
+        byte[] cipherParmsEnc = cipher.getParameters().getEncoded("ASN.1");
+        assertTrue("Parameters differ", Arrays.equals(apEncoding,
+                cipherParmsEnc));
+    }
 
 	/**
 	 * @tests javax.crypto.Cipher#init(int, java.security.Key)
 	 */
-	public void test_initILjava_security_Key() {
-		final String algorithm = "DESede";
-		final int keyLen = 168;
-
-		Key cipherKey = null;
-		Cipher cipher = null;
-
-		try {
-			KeyGenerator kg = null;
-			try {
-				kg = KeyGenerator.getInstance(algorithm);
-				kg.init(keyLen, new SecureRandom());
-			} catch (NoSuchAlgorithmException e) {
-				fail("Caught a NoSuchAlgorithmException : " + e);
-			}
-			cipherKey = kg.generateKey();
-
-			cipher = Cipher.getInstance(algorithm + "/ECB/PKCS5Padding");
-
-		} catch (Exception e) {
-			fail("Setup error");
-		}
-
-		try {
-			cipher.init(Cipher.ENCRYPT_MODE, cipherKey);
-		} catch (Exception e) {
-			fail("Unexpected exception");
-		}
-	}
+    public void test_initILjava_security_Key() throws Exception {
+        final String algorithm = "DESede";
+        final int keyLen = 168;
+
+        Key cipherKey = null;
+        Cipher cipher = null;
+
+        KeyGenerator kg = KeyGenerator.getInstance(algorithm);
+        kg.init(keyLen, new SecureRandom());
+        cipherKey = kg.generateKey();
+
+        cipher = Cipher.getInstance(algorithm + "/ECB/PKCS5Padding");
+
+        cipher.init(Cipher.ENCRYPT_MODE, cipherKey);
+    }
 
 	/**
 	 * @tests javax.crypto.Cipher#init(int, java.security.Key,
 	 *        java.security.SecureRandom)
 	 */
-	public void test_initILjava_security_KeyLjava_security_SecureRandom() {
-		final String algorithm = "DESede";
-		final int keyLen = 168;
-
-		Key cipherKey = null;
-		SecureRandom sr = new SecureRandom();
-		Cipher cipher = null;
-
-		try {
-			KeyGenerator kg = null;
-			try {
-				kg = KeyGenerator.getInstance(algorithm);
-				kg.init(keyLen, new SecureRandom());
-			} catch (NoSuchAlgorithmException e) {
-				fail("Caught a NoSuchAlgorithmException : " + e);
-			}
-			cipherKey = kg.generateKey();
-
-			cipher = Cipher.getInstance(algorithm + "/ECB/PKCS5Padding");
-
-		} catch (Exception e) {
-			fail("Setup error");
-		}
-
-		try {
-			cipher.init(Cipher.ENCRYPT_MODE, cipherKey, sr);
-		} catch (Exception e) {
-			fail("Unexpected exception : " + e);
-		}
-	}
+    public void test_initILjava_security_KeyLjava_security_SecureRandom()
+            throws Exception {
+
+        final String algorithm = "DESede";
+        final int keyLen = 168;
+
+        Key cipherKey = null;
+        SecureRandom sr = new SecureRandom();
+        Cipher cipher = null;
+
+        KeyGenerator kg = KeyGenerator.getInstance(algorithm);
+        kg.init(keyLen, new SecureRandom());
+        cipherKey = kg.generateKey();
+
+        cipher = Cipher.getInstance(algorithm + "/ECB/PKCS5Padding");
+
+        cipher.init(Cipher.ENCRYPT_MODE, cipherKey, sr);
+    }
 
 	/**
 	 * @tests javax.crypto.Cipher#init(int, java.security.Key,
 	 *        java.security.spec.AlgorithmParameterSpec)
 	 */
-	public void test_initILjava_security_KeyLjava_security_spec_AlgorithmParameterSpec() {
-		final String algorithm = "DESede";
-		final int keyLen = 168;
-
-		Key cipherKey = null;
-		SecureRandom sr = new SecureRandom();
-		Cipher cipher = null;
-
-		byte[] iv = null;
-		AlgorithmParameterSpec ivAVP = null;
-
-		try {
-			KeyGenerator kg = null;
-			try {
-				kg = KeyGenerator.getInstance(algorithm);
-				kg.init(keyLen, new SecureRandom());
-			} catch (NoSuchAlgorithmException e) {
-				fail("Caught a NoSuchAlgorithmException : " + e);
-			}
-			cipherKey = kg.generateKey();
-
-			iv = new byte[8];
-			sr.nextBytes(iv);
-			ivAVP = new IvParameterSpec(iv);
-
-			cipher = Cipher.getInstance(algorithm + "/CBC/PKCS5Padding");
-		} catch (Exception e) {
-			fail("Setup error");
-		}
-
-		try {
-			cipher.init(Cipher.ENCRYPT_MODE, cipherKey, ivAVP);
-		} catch (Exception e) {
-			fail("Unexpected exception : " + e);
-		}
+    public void test_initILjava_security_KeyLjava_security_spec_AlgorithmParameterSpec()
+            throws Exception {
 
-		byte[] cipherIV = cipher.getIV();
+        final String algorithm = "DESede";
+        final int keyLen = 168;
 
-		assertTrue("IVs differ", Arrays.equals(cipherIV, iv));
-	}
+        Key cipherKey = null;
+        SecureRandom sr = new SecureRandom();
+        Cipher cipher = null;
+
+        byte[] iv = null;
+        AlgorithmParameterSpec ivAVP = null;
+
+        KeyGenerator kg = KeyGenerator.getInstance(algorithm);
+        kg.init(keyLen, new SecureRandom());
+        cipherKey = kg.generateKey();
+
+        iv = new byte[8];
+        sr.nextBytes(iv);
+        ivAVP = new IvParameterSpec(iv);
+
+        cipher = Cipher.getInstance(algorithm + "/CBC/PKCS5Padding");
+
+        cipher.init(Cipher.ENCRYPT_MODE, cipherKey, ivAVP);
+
+        byte[] cipherIV = cipher.getIV();
+
+        assertTrue("IVs differ", Arrays.equals(cipherIV, iv));
+    }
 
 	/**
 	 * @tests javax.crypto.Cipher#init(int, java.security.Key,
 	 *        java.security.spec.AlgorithmParameterSpec,
 	 *        java.security.SecureRandom)
 	 */
-	public void test_initILjava_security_KeyLjava_security_spec_AlgorithmParameterSpecLjava_security_SecureRandom() {
-		final String algorithm = "DESede";
-		final int keyLen = 168;
-
-		Key cipherKey = null;
-		SecureRandom sr = new SecureRandom();
-		Cipher cipher = null;
-
-		byte[] iv = null;
-		AlgorithmParameterSpec ivAVP = null;
-
-		try {
-			KeyGenerator kg = null;
-			try {
-				kg = KeyGenerator.getInstance(algorithm);
-				kg.init(keyLen, new SecureRandom());
-			} catch (NoSuchAlgorithmException e) {
-				fail("Caught a NoSuchAlgorithmException : " + e);
-			}
-			cipherKey = kg.generateKey();
-
-			iv = new byte[8];
-			sr.nextBytes(iv);
-			ivAVP = new IvParameterSpec(iv);
-
-			cipher = Cipher.getInstance(algorithm + "/CBC/PKCS5Padding");
-		} catch (Exception e) {
-			fail("Setup error");
-		}
-
-		try {
-			cipher.init(Cipher.ENCRYPT_MODE, cipherKey, ivAVP, sr);
-		} catch (Exception e) {
-			fail("Unexpected exception : " + e);
-		}
+	public void test_initILjava_security_KeyLjava_security_spec_AlgorithmParameterSpecLjava_security_SecureRandom()
+            throws Exception {
+        final String algorithm = "DESede";
+        final int keyLen = 168;
 
-		byte[] cipherIV = cipher.getIV();
+        Key cipherKey = null;
+        SecureRandom sr = new SecureRandom();
+        Cipher cipher = null;
 
-		assertTrue("IVs differ", Arrays.equals(cipherIV, iv));
-	}
+        byte[] iv = null;
+        AlgorithmParameterSpec ivAVP = null;
+
+        KeyGenerator kg = KeyGenerator.getInstance(algorithm);
+        kg.init(keyLen, new SecureRandom());
+        cipherKey = kg.generateKey();
+
+        iv = new byte[8];
+        sr.nextBytes(iv);
+        ivAVP = new IvParameterSpec(iv);
+
+        cipher = Cipher.getInstance(algorithm + "/CBC/PKCS5Padding");
+
+        cipher.init(Cipher.ENCRYPT_MODE, cipherKey, ivAVP, sr);
+
+        byte[] cipherIV = cipher.getIV();
+
+        assertTrue("IVs differ", Arrays.equals(cipherIV, iv));
+    }
 
 	/**
 	 * @tests javax.crypto.Cipher#update(byte[], int, int)