Return-Path: Delivered-To: apmail-incubator-harmony-commits-archive@www.apache.org Received: (qmail 61468 invoked from network); 30 May 2006 11:36:21 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 30 May 2006 11:36:21 -0000 Received: (qmail 1349 invoked by uid 500); 30 May 2006 11:36:20 -0000 Delivered-To: apmail-incubator-harmony-commits-archive@incubator.apache.org Received: (qmail 1296 invoked by uid 500); 30 May 2006 11:36:19 -0000 Mailing-List: contact harmony-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: harmony-dev@incubator.apache.org Delivered-To: mailing list harmony-commits@incubator.apache.org Received: (qmail 1285 invoked by uid 99); 30 May 2006 11:36:19 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 30 May 2006 04:36:19 -0700 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: local policy) Received: from [140.211.166.113] (HELO eris.apache.org) (140.211.166.113) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 30 May 2006 04:36:13 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id 436E11A9846; Tue, 30 May 2006 04:35:53 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r410243 [3/7] - in /incubator/harmony/enhanced/classlib/trunk/modules/crypto: make/common/ src/main/java/javax/crypto/ src/main/java/javax/crypto/spec/ src/main/java/org/apache/harmony/crypto/internal/ src/test/api/java.injected/javax/crypt... Date: Tue, 30 May 2006 11:35:46 -0000 To: harmony-commits@incubator.apache.org From: mloenko@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20060530113553.436E11A9846@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Modified: incubator/harmony/enhanced/classlib/trunk/modules/crypto/src/test/api/java.injected/javax/crypto/SealedObjectTest.java URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/crypto/src/test/api/java.injected/javax/crypto/SealedObjectTest.java?rev=410243&r1=410242&r2=410243&view=diff ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/crypto/src/test/api/java.injected/javax/crypto/SealedObjectTest.java (original) +++ incubator/harmony/enhanced/classlib/trunk/modules/crypto/src/test/api/java.injected/javax/crypto/SealedObjectTest.java Tue May 30 04:35:44 2006 @@ -55,57 +55,36 @@ * deserialized, the content od deserialized object equals to the * content of initial object. */ - public void testReadObject() { - try { - String secret = "secret string"; - SealedObject so = new SealedObject(secret, new NullCipher()); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream(bos); - oos.writeObject(so); - - ObjectInputStream ois = - new ObjectInputStream( - new ByteArrayInputStream(bos.toByteArray())); + public void testReadObject() throws Exception { + String secret = "secret string"; + SealedObject so = new SealedObject(secret, new NullCipher()); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(bos); + oos.writeObject(so); - SealedObject so_des = (SealedObject) ois.readObject(); - assertEquals("The secret content of deserialized object " + ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream( + bos.toByteArray())); + + SealedObject so_des = (SealedObject) ois.readObject(); + assertEquals("The secret content of deserialized object " + "should be equal to the secret content of initial object", secret, so_des.getObject(new NullCipher())); - assertEquals("The value returned by getAlgorithm() method of " + assertEquals("The value returned by getAlgorithm() method of " + "deserialized object should be equal to the value returned " - + "by getAlgorithm() method of initial object", - so.getAlgorithm(), so_des.getAlgorithm()); - } catch (BadPaddingException e) { - e.printStackTrace(); - fail("Unexpected BadPaddingException was thrown."); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - fail("Unexpected ClassNotFoundException was thrown."); - } catch (IllegalBlockSizeException e) { - e.printStackTrace(); - fail("Unexpected IllegalBlockSizeException was thrown."); - } catch (IOException e) { - e.printStackTrace(); - fail("Unexpected IOException was thrown."); - } + + "by getAlgorithm() method of initial object", so + .getAlgorithm(), so_des.getAlgorithm()); } /** - * SealedObject(Serializable object, Cipher c) method testing. - * Tests if the NullPointerException is thrown in the case of null cipher. + * SealedObject(Serializable object, Cipher c) method testing. Tests if the + * NullPointerException is thrown in the case of null cipher. */ - public void testSealedObject1() { + public void testSealedObject1() throws Exception { String secret = "secret string"; try { new SealedObject(secret, null); fail("NullPointerException should be thrown in the case " + "of null cipher."); - } catch (IllegalBlockSizeException e) { - e.printStackTrace(); - fail("Unexpected IllegalBlockSizeException was thrown."); - } catch (IOException e) { - e.printStackTrace(); - fail("Unexpected IOException was thrown."); } catch (NullPointerException e) { } } @@ -114,7 +93,7 @@ * SealedObject(SealedObject so) method testing. Tests if the * NullPointerException is thrown in the case of null SealedObject. */ - public void testSealedObject2() { + public void testSealedObject2() throws Exception { try { new SealedObject(null); fail("NullPointerException should be thrown in the case " @@ -122,306 +101,133 @@ } catch (NullPointerException e) { } - try { - String secret = "secret string"; - Cipher cipher = new NullCipher(); - SealedObject so1 = new SealedObject(secret, cipher); - SealedObject so2 = new SealedObject(so1); - - assertEquals("The secret content of the object should equals " - + "to the secret content of initial object.", - secret, so2.getObject(cipher)); - assertEquals("The algorithm which was used to seal the object " - + "should be the same as the algorithm used to seal the " - + "initial object", so1.getAlgorithm(), so2.getAlgorithm()); - } catch (BadPaddingException e) { - e.printStackTrace(); - fail("Unexpected BadPaddingException was thrown."); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - fail("Unexpected ClassNotFoundException was thrown."); - } catch (NullPointerException e) { - e.printStackTrace(); - fail("Unexpected NullPointerException was thrown."); - } catch (IllegalBlockSizeException e) { - e.printStackTrace(); - fail("Unexpected IllegalBlockSizeException was thrown."); - } catch (IOException e) { - e.printStackTrace(); - fail("Unexpected IOException was thrown."); - } + String secret = "secret string"; + Cipher cipher = new NullCipher(); + SealedObject so1 = new SealedObject(secret, cipher); + SealedObject so2 = new SealedObject(so1); + + assertEquals("The secret content of the object should equals " + + "to the secret content of initial object.", secret, so2 + .getObject(cipher)); + assertEquals("The algorithm which was used to seal the object " + + "should be the same as the algorithm used to seal the " + + "initial object", so1.getAlgorithm(), so2.getAlgorithm()); } /** - * getAlgorithm() method testing. Tests if the returned value equals - * to the corresponding value of Cipher object. + * getAlgorithm() method testing. Tests if the returned value equals to the + * corresponding value of Cipher object. */ - public void testGetAlgorithm() { - try { - String secret = "secret string"; - String algorithm = "DES"; - KeyGenerator kg = KeyGenerator.getInstance(algorithm); - Key key = kg.generateKey(); - - Cipher cipher = Cipher.getInstance(algorithm); - cipher.init(Cipher.ENCRYPT_MODE, key); - SealedObject so = new SealedObject(secret, cipher); - - assertEquals("The algorithm name should be the same as used " - + "in cipher.", algorithm, so.getAlgorithm()); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - fail("Unexpected NoSuchAlgorithmException was thrown."); - } catch (NoSuchPaddingException e) { - e.printStackTrace(); - fail("Unexpected NoSuchPaddingException was thrown."); - } catch (InvalidKeyException e) { - e.printStackTrace(); - fail("Unexpected InvalidKeyException was thrown."); - } catch (IllegalBlockSizeException e) { - e.printStackTrace(); - fail("Unexpected IllegalBlockSizeException was thrown."); - } catch (IOException e) { - e.printStackTrace(); - fail("Unexpected IOException was thrown."); - } catch (NullPointerException e) { - e.printStackTrace(); - fail("Unexpected NullPointerException was thrown."); - } + public void testGetAlgorithm() throws Exception { + String secret = "secret string"; + String algorithm = "DES"; + KeyGenerator kg = KeyGenerator.getInstance(algorithm); + Key key = kg.generateKey(); + + Cipher cipher = Cipher.getInstance(algorithm); + cipher.init(Cipher.ENCRYPT_MODE, key); + SealedObject so = new SealedObject(secret, cipher); + + assertEquals("The algorithm name should be the same as used " + + "in cipher.", algorithm, so.getAlgorithm()); } /** - * getObject(Key key) method testing. Tests if the object sealed - * with encryption algorithm and specified parameters can be - * retrieved by specifying the cryptographic key. + * getObject(Key key) method testing. Tests if the object sealed with + * encryption algorithm and specified parameters can be retrieved by + * specifying the cryptographic key. */ - public void testGetObject1() { - try { - KeyGenerator kg = KeyGenerator.getInstance("DES"); - Key key = kg.generateKey(); + public void testGetObject1() throws Exception { + KeyGenerator kg = KeyGenerator.getInstance("DES"); + Key key = kg.generateKey(); - IvParameterSpec ips = - new IvParameterSpec(new byte[] {1, 2, 3, 4, 5, 6, 7, 8}); + IvParameterSpec ips = new IvParameterSpec(new byte[] { 1, 2, 3, 4, 5, + 6, 7, 8 }); - Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); - cipher.init(Cipher.ENCRYPT_MODE, key, ips); + Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); + cipher.init(Cipher.ENCRYPT_MODE, key, ips); - String secret = "secret string"; - SealedObject so = new SealedObject(secret, cipher); - - assertEquals("The returned object does not equals to the " - + "original object.", - secret, so.getObject(key)); - - assertTrue("The encodedParams field of SealedObject object " - + "should contain the encoded algorithm parameters.", - Arrays.equals(so.encodedParams, - cipher.getParameters().getEncoded())); - - } catch (InvalidAlgorithmParameterException e) { - e.printStackTrace(); - fail("Unexpected InvalidAlgorithmParameterException was thrown."); - } catch (InvalidKeyException e) { - e.printStackTrace(); - fail("Unexpected InvalidKeyException was thrown."); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - fail("Unexpected ClassNotFoundException was thrown."); - } catch (IllegalBlockSizeException e) { - e.printStackTrace(); - fail("Unexpected IllegalBlockSizeException was thrown."); - } catch (IOException e) { - e.printStackTrace(); - fail("Unexpected IOException was thrown."); - } catch (NoSuchPaddingException e) { - e.printStackTrace(); - fail("Unexpected NoSuchPaddingException was thrown."); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - fail("Unexpected NoSuchAlgorithmException was thrown."); - } + String secret = "secret string"; + SealedObject so = new SealedObject(secret, cipher); + + assertEquals("The returned object does not equals to the " + + "original object.", secret, so.getObject(key)); + + assertTrue("The encodedParams field of SealedObject object " + + "should contain the encoded algorithm parameters.", Arrays + .equals(so.encodedParams, cipher.getParameters().getEncoded())); } /** - * getObject(Cipher c) method testing. Tests if the proper exception - * is thrown in the case of incorrect input parameters and if the - * object sealed with encryption algorithm and specified parameters - * can be retrieved by specifying the initialized Cipher object. + * getObject(Cipher c) method testing. Tests if the proper exception is + * thrown in the case of incorrect input parameters and if the object sealed + * with encryption algorithm and specified parameters can be retrieved by + * specifying the initialized Cipher object. */ - public void testGetObject2() { + public void testGetObject2() throws Exception { try { - new SealedObject("secret string", - new NullCipher()).getObject((Cipher) null); + new SealedObject("secret string", new NullCipher()) + .getObject((Cipher) null); fail("NullPointerException should be thrown in the case of " + "null cipher."); - } catch (IOException e) { - e.printStackTrace(); - fail("Unexpected IOException was thrown."); - } catch (IllegalBlockSizeException e) { - e.printStackTrace(); - fail("Unexpected IllegalBlockSizeException was thrown."); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - fail("Unexpected ClassNotFoundException was thrown."); - } catch (BadPaddingException e) { - e.printStackTrace(); - fail("Unexpected BadPaddingException was thrown."); } catch (NullPointerException e) { } - try { - KeyGenerator kg = KeyGenerator.getInstance("DES"); - Key key = kg.generateKey(); + KeyGenerator kg = KeyGenerator.getInstance("DES"); + Key key = kg.generateKey(); - IvParameterSpec ips = - new IvParameterSpec(new byte[] {1, 2, 3, 4, 5, 6, 7, 8}); + IvParameterSpec ips = new IvParameterSpec(new byte[] { 1, 2, 3, 4, 5, + 6, 7, 8 }); - Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); - cipher.init(Cipher.ENCRYPT_MODE, key, ips); + Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); + cipher.init(Cipher.ENCRYPT_MODE, key, ips); - String secret = "secret string"; - SealedObject so = new SealedObject(secret, cipher); - - cipher.init(Cipher.DECRYPT_MODE, key, ips); - assertEquals("The returned object does not equals to the " - + "original object.", - secret, so.getObject(cipher)); - - } catch (InvalidAlgorithmParameterException e) { - e.printStackTrace(); - fail("Unexpected InvalidAlgorithmParameterException was thrown."); - } catch (BadPaddingException e) { - e.printStackTrace(); - fail("Unexpected BadPaddingException was thrown."); - } catch (InvalidKeyException e) { - e.printStackTrace(); - fail("Unexpected InvalidKeyException was thrown."); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - fail("Unexpected ClassNotFoundException was thrown."); - } catch (IllegalBlockSizeException e) { - e.printStackTrace(); - fail("Unexpected IllegalBlockSizeException was thrown."); - } catch (IOException e) { - e.printStackTrace(); - fail("Unexpected IOException was thrown."); - } catch (NoSuchPaddingException e) { - e.printStackTrace(); - fail("Unexpected NoSuchPaddingException was thrown."); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - fail("Unexpected NoSuchAlgorithmException was thrown."); - } + String secret = "secret string"; + SealedObject so = new SealedObject(secret, cipher); + + cipher.init(Cipher.DECRYPT_MODE, key, ips); + assertEquals("The returned object does not equals to the " + + "original object.", secret, so.getObject(cipher)); } /** - * getObject(Key key, String provider) method testing. Tests if the - * proper exception is thrown in the case of incorrect input parameters - * and if the object sealed with encryption algorithm can be - * retrieved by specifying the cryptographic key and provider name. + * getObject(Key key, String provider) method testing. Tests if the proper + * exception is thrown in the case of incorrect input parameters and if the + * object sealed with encryption algorithm can be retrieved by specifying + * the cryptographic key and provider name. */ - public void testGetObject3() { + public void testGetObject3() throws Exception { try { - new SealedObject("secret string", - new NullCipher()).getObject( - new SecretKeySpec(new byte[] {0, 0, 0}, "algorithm"), - null); + new SealedObject("secret string", new NullCipher()).getObject( + new SecretKeySpec(new byte[] { 0, 0, 0 }, "algorithm"), + null); fail("IllegalArgumentException should be thrown in the case of " + "null provider."); - } catch (IOException e) { - e.printStackTrace(); - fail("Unexpected IOException was thrown."); - } catch (IllegalBlockSizeException e) { - e.printStackTrace(); - fail("Unexpected IllegalBlockSizeException was thrown."); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - fail("Unexpected ClassNotFoundException was thrown."); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - fail("Unexpected NoSuchAlgorithmException was thrown."); - } catch (NoSuchProviderException e) { - e.printStackTrace(); - fail("Unexpected NoSuchProviderException was thrown."); - } catch (InvalidKeyException e) { - e.printStackTrace(); - fail("Unexpected InvalidKeyException was thrown."); } catch (IllegalArgumentException e) { } try { - new SealedObject("secret string", - new NullCipher()).getObject( - new SecretKeySpec(new byte[] {0, 0, 0}, "algorithm"), - ""); + new SealedObject("secret string", new NullCipher()).getObject( + new SecretKeySpec(new byte[] { 0, 0, 0 }, "algorithm"), ""); fail("IllegalArgumentException should be thrown in the case of " + "empty provider."); - } catch (IOException e) { - e.printStackTrace(); - fail("Unexpected IOException was thrown."); - } catch (IllegalBlockSizeException e) { - e.printStackTrace(); - fail("Unexpected IllegalBlockSizeException was thrown."); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - fail("Unexpected ClassNotFoundException was thrown."); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - fail("Unexpected NoSuchAlgorithmException was thrown."); - } catch (NoSuchProviderException e) { - e.printStackTrace(); - fail("Unexpected NoSuchProviderException was thrown."); - } catch (InvalidKeyException e) { - e.printStackTrace(); - fail("Unexpected InvalidKeyException was thrown."); } catch (IllegalArgumentException e) { } - try { - KeyGenerator kg = KeyGenerator.getInstance("DES"); - Key key = kg.generateKey(); + KeyGenerator kg = KeyGenerator.getInstance("DES"); + Key key = kg.generateKey(); - Cipher cipher = Cipher.getInstance("DES"); - String provider = cipher.getProvider().getName(); - cipher.init(Cipher.ENCRYPT_MODE, key); - - String secret = "secret string"; - SealedObject so = new SealedObject(secret, cipher); - - cipher.init(Cipher.DECRYPT_MODE, key); - assertEquals("The returned object does not equals to the " - + "original object.", - secret, so.getObject(key, provider)); - } catch (NoSuchProviderException e) { - e.printStackTrace(); - fail("Unexpected NoSuchProviderException was thrown."); - } catch (InvalidKeyException e) { - e.printStackTrace(); - fail("Unexpected InvalidKeyException was thrown."); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - fail("Unexpected ClassNotFoundException was thrown."); - } catch (IllegalBlockSizeException e) { - e.printStackTrace(); - fail("Unexpected IllegalBlockSizeException was thrown."); - } catch (IOException e) { - e.printStackTrace(); - fail("Unexpected IOException was thrown."); - } catch (NoSuchPaddingException e) { - e.printStackTrace(); - fail("Unexpected NoSuchPaddingException was thrown."); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - fail("Unexpected NoSuchAlgorithmException was thrown."); - } - } + Cipher cipher = Cipher.getInstance("DES"); + String provider = cipher.getProvider().getName(); + cipher.init(Cipher.ENCRYPT_MODE, key); - public static Test suite() { - return new TestSuite(SealedObjectTest.class); - } + String secret = "secret string"; + SealedObject so = new SealedObject(secret, cipher); - public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + cipher.init(Cipher.DECRYPT_MODE, key); + assertEquals("The returned object does not equals to the " + + "original object.", secret, so.getObject(key, provider)); } + } Modified: incubator/harmony/enhanced/classlib/trunk/modules/crypto/src/test/api/java.injected/javax/crypto/spec/PSourceTest.java URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/crypto/src/test/api/java.injected/javax/crypto/spec/PSourceTest.java?rev=410243&r1=410242&r2=410243&view=diff ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/crypto/src/test/api/java.injected/javax/crypto/spec/PSourceTest.java (original) +++ incubator/harmony/enhanced/classlib/trunk/modules/crypto/src/test/api/java.injected/javax/crypto/spec/PSourceTest.java Tue May 30 04:35:44 2006 @@ -1,113 +1,113 @@ -/* - * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** -* @author Alexander Y. Kleymenov -* @version $Revision$ -*/ - -package javax.crypto.spec; - -import java.util.Arrays; -import javax.crypto.spec.PSource; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - */ - -public class PSourceTest extends TestCase { - - /** - * PSpecified(byte[] p) method testing. Tests that NullPointerException - * is thrown in the case of null p array. Also it checks the value of - * DEFAULT field, and that input p array is copied to protect against - * subsequent modification. - */ - public void testPSpecified() { - try { - new PSource.PSpecified(null); - fail("NullPointerException should be thrown in the case of " - + "null p array."); - } catch (NullPointerException e) { - } - - assertEquals("The PSource.PSpecified DEFAULT value should be byte[0]", - 0, PSource.PSpecified.DEFAULT.getValue().length); - - byte[] p = new byte[] {1, 2, 3, 4, 5}; - PSource.PSpecified ps = new PSource.PSpecified(p); - p[0] ++; - assertFalse("The change of p specified in the constructor " - + "should not cause the change of internal array.", - p[0] == ps.getValue()[0]); - } - - /** - * getValue() method testing. Tests that returned array is equal to the - * array specified in the constructor. Checks that modification - * of returned array does not affect the internal array. - */ - public void testGetValue() { - byte[] p = new byte[] {1, 2, 3, 4, 5}; - - PSource.PSpecified ps = new PSource.PSpecified(p); - byte[] result = ps.getValue(); - if (! Arrays.equals(p, result)) { - fail("The returned array does not equal to the specified " - + "in the constructor."); - } - result[0] ++; - assertFalse("The change of returned by getValue() array " - + "should not cause the change of internal array.", - result[0] == ps.getValue()[0]); - } - - /** - * PSource(String pSrcName) method testing. Tests that returned value is - * equal to the value specified in the constructor. - */ - public void testPSource() { - try { - new PSource(null); - fail("NullPointerException should be thrown in the case of " - + "null pSrcName."); - } catch (NullPointerException e) { - } - } - - /** - * getAlgorithm() method testing. Tests that returned value is - * equal to the value specified in the constructor. - */ - public void testGetAlgorithm() { - String pSrcName = "pSrcName"; - PSource ps = new PSource(pSrcName); - assertTrue("The returned value is not equal to the value specified " - + "in constructor", pSrcName.equals(ps.getAlgorithm())); - } - - public static Test suite() { - return new TestSuite(PSourceTest.class); - } - - public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); - } -} - +/* + * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** +* @author Alexander Y. Kleymenov +* @version $Revision$ +*/ + +package javax.crypto.spec; + +import java.util.Arrays; +import javax.crypto.spec.PSource; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + */ + +public class PSourceTest extends TestCase { + + /** + * PSpecified(byte[] p) method testing. Tests that NullPointerException + * is thrown in the case of null p array. Also it checks the value of + * DEFAULT field, and that input p array is copied to protect against + * subsequent modification. + */ + public void testPSpecified() { + try { + new PSource.PSpecified(null); + fail("NullPointerException should be thrown in the case of " + + "null p array."); + } catch (NullPointerException e) { + } + + assertEquals("The PSource.PSpecified DEFAULT value should be byte[0]", + 0, PSource.PSpecified.DEFAULT.getValue().length); + + byte[] p = new byte[] {1, 2, 3, 4, 5}; + PSource.PSpecified ps = new PSource.PSpecified(p); + p[0] ++; + assertFalse("The change of p specified in the constructor " + + "should not cause the change of internal array.", + p[0] == ps.getValue()[0]); + } + + /** + * getValue() method testing. Tests that returned array is equal to the + * array specified in the constructor. Checks that modification + * of returned array does not affect the internal array. + */ + public void testGetValue() { + byte[] p = new byte[] {1, 2, 3, 4, 5}; + + PSource.PSpecified ps = new PSource.PSpecified(p); + byte[] result = ps.getValue(); + if (! Arrays.equals(p, result)) { + fail("The returned array does not equal to the specified " + + "in the constructor."); + } + result[0] ++; + assertFalse("The change of returned by getValue() array " + + "should not cause the change of internal array.", + result[0] == ps.getValue()[0]); + } + + /** + * PSource(String pSrcName) method testing. Tests that returned value is + * equal to the value specified in the constructor. + */ + public void testPSource() { + try { + new PSource(null); + fail("NullPointerException should be thrown in the case of " + + "null pSrcName."); + } catch (NullPointerException e) { + } + } + + /** + * getAlgorithm() method testing. Tests that returned value is + * equal to the value specified in the constructor. + */ + public void testGetAlgorithm() { + String pSrcName = "pSrcName"; + PSource ps = new PSource(pSrcName); + assertTrue("The returned value is not equal to the value specified " + + "in constructor", pSrcName.equals(ps.getAlgorithm())); + } + + public static Test suite() { + return new TestSuite(PSourceTest.class); + } + + public static void main(String[] args) { + junit.textui.TestRunner.run(suite()); + } +} + Modified: incubator/harmony/enhanced/classlib/trunk/modules/crypto/src/test/api/java/org/apache/harmony/crypto/tests/javax/crypto/CipherTest.java URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/crypto/src/test/api/java/org/apache/harmony/crypto/tests/javax/crypto/CipherTest.java?rev=410243&r1=410242&r2=410243&view=diff ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/crypto/src/test/api/java/org/apache/harmony/crypto/tests/javax/crypto/CipherTest.java (original) +++ incubator/harmony/enhanced/classlib/trunk/modules/crypto/src/test/api/java/org/apache/harmony/crypto/tests/javax/crypto/CipherTest.java Tue May 30 04:35:44 2006 @@ -1,474 +1,437 @@ -/* Copyright 2005 The Apache Software Foundation or its licensors, as applicable - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.harmony.crypto.tests.javax.crypto; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.security.AlgorithmParameters; -import java.security.Key; -import java.security.NoSuchAlgorithmException; -import java.security.NoSuchProviderException; -import java.security.Provider; -import java.security.SecureRandom; -import java.security.Security; -import java.security.spec.AlgorithmParameterSpec; -import java.util.Arrays; - -import javax.crypto.Cipher; -import javax.crypto.KeyGenerator; -import javax.crypto.SecretKeyFactory; -import javax.crypto.spec.DESedeKeySpec; -import javax.crypto.spec.IvParameterSpec; - -import tests.support.resource.Support_Resources; - -public class CipherTest extends junit.framework.TestCase { - - /** - * @tests javax.crypto.Cipher#getInstance(java.lang.String) - */ - 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, - * java.lang.String) - */ - public void test_getInstanceLjava_lang_StringLjava_lang_String() - 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].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 - try { - Cipher.getInstance("DES", (String) null); - fail("Should have thrown an IllegalArgumentException"); - } catch (IllegalArgumentException e) { - // Expected - } - - // Exception case - try { - Cipher.getInstance("DES", "IHaveNotBeenConfigured"); - fail("Should have thrown an NoSuchProviderException"); - } catch (NoSuchProviderException e) { - // Expected - } - } - - /** - * @tests javax.crypto.Cipher#getInstance(java.lang.String, - * java.security.Provider) - */ - 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() 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() 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() 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() 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() throws Exception { - /* - * 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; - - 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() 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; - - 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() 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() - 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() - throws Exception { - - final String algorithm = "DESede"; - final int keyLen = 168; - - 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() - throws Exception { - final String algorithm = "DESede"; - final int keyLen = 168; - - 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, sr); - - byte[] cipherIV = cipher.getIV(); - - assertTrue("IVs differ", Arrays.equals(cipherIV, iv)); - } - - /** - * @tests javax.crypto.Cipher#update(byte[], int, int) - */ - public void test_update$BII() throws Exception { - for (int index = 1; index < 4; index++) { - Cipher c = Cipher.getInstance("DESEDE/CBC/PKCS5Padding"); - - byte[] keyMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" - + index + ".key"); - DESedeKeySpec keySpec = new DESedeKeySpec(keyMaterial); - SecretKeyFactory skf = SecretKeyFactory.getInstance("DESEDE"); - Key k = skf.generateSecret(keySpec); - - byte[] ivMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" + index - + ".iv"); - IvParameterSpec iv = new IvParameterSpec(ivMaterial); - - c.init(Cipher.DECRYPT_MODE, k, iv); - - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - byte[] input = new byte[256]; - String resPath = "hyts_" + "des-ede3-cbc.test" + index - + ".ciphertext"; - InputStream is = Support_Resources.getResourceStream(resPath); - - int bytesRead = is.read(input, 0, 256); - while (bytesRead > 0) { - byte[] output = c.update(input, 0, bytesRead); - if (output != null) { - baos.write(output); - } - bytesRead = is.read(input, 0, 256); - } - - byte[] output = c.doFinal(); - if (output != null) { - baos.write(output); - } - - byte[] decipheredCipherText = baos.toByteArray(); - is.close(); - - byte[] plaintextBytes = loadBytes("hyts_" + "des-ede3-cbc.test" - + index + ".plaintext"); - assertTrue("Operation produced incorrect results", Arrays.equals( - plaintextBytes, decipheredCipherText)); - }// end for - } - - /** - * @tests javax.crypto.Cipher#doFinal() - */ - public void test_doFinal() throws Exception { - for (int index = 1; index < 4; index++) { - Cipher c = Cipher.getInstance("DESEDE/CBC/PKCS5Padding"); - - byte[] keyMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" - + index + ".key"); - DESedeKeySpec keySpec = new DESedeKeySpec(keyMaterial); - SecretKeyFactory skf = SecretKeyFactory.getInstance("DESEDE"); - Key k = skf.generateSecret(keySpec); - - byte[] ivMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" + index - + ".iv"); - IvParameterSpec iv = new IvParameterSpec(ivMaterial); - - c.init(Cipher.ENCRYPT_MODE, k, iv); - - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - byte[] input = new byte[256]; - String resPath = "hyts_" + "des-ede3-cbc.test" + index - + ".plaintext"; - InputStream is = Support_Resources.getResourceStream(resPath); - - int bytesRead = is.read(input, 0, 256); - while (bytesRead > 0) { - byte[] output = c.update(input, 0, bytesRead); - if (output != null) { - baos.write(output); - } - bytesRead = is.read(input, 0, 256); - } - byte[] output = c.doFinal(); - if (output != null) { - baos.write(output); - } - byte[] encryptedPlaintext = baos.toByteArray(); - is.close(); - - byte[] cipherText = loadBytes("hyts_" + "des-ede3-cbc.test" + index - + ".ciphertext"); - assertTrue("Operation produced incorrect results", Arrays.equals( - encryptedPlaintext, cipherText)); - }// end for - } - - private byte[] loadBytes(String resPath) { - try { - InputStream is = Support_Resources.getResourceStream(resPath); - - ByteArrayOutputStream out = new ByteArrayOutputStream(); - byte[] buff = new byte[1024]; - int readlen; - while ((readlen = is.read(buff)) > 0) { - out.write(buff, 0, readlen); - } - is.close(); - return out.toByteArray(); - } catch (IOException e) { - return null; - } - } -} +/* Copyright 2005 The Apache Software Foundation or its licensors, as applicable + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.crypto.tests.javax.crypto; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.security.AlgorithmParameters; +import java.security.Key; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.Provider; +import java.security.SecureRandom; +import java.security.Security; +import java.security.spec.AlgorithmParameterSpec; +import java.util.Arrays; + +import javax.crypto.Cipher; +import javax.crypto.KeyGenerator; +import javax.crypto.SecretKeyFactory; +import javax.crypto.ShortBufferException; +import javax.crypto.spec.DESedeKeySpec; +import javax.crypto.spec.IvParameterSpec; + +import tests.support.resource.Support_Resources; + +public class CipherTest extends junit.framework.TestCase { + + static Key cipherKey; + static final String algorithm = "DESede"; + static final int keyLen = 168; + + static { + try { + KeyGenerator kg = KeyGenerator.getInstance(algorithm); + kg.init(keyLen, new SecureRandom()); + cipherKey = kg.generateKey(); + } catch (Exception e) { + fail("No key " + e); + } + } + + + /** + * @tests javax.crypto.Cipher#getInstance(java.lang.String) + */ + 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, + * java.lang.String) + */ + public void test_getInstanceLjava_lang_StringLjava_lang_String() + 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].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 + try { + Cipher.getInstance("DES", (String) null); + fail("Should have thrown an IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // Expected + } + + // Exception case + try { + Cipher.getInstance("DES", "IHaveNotBeenConfigured"); + fail("Should have thrown an NoSuchProviderException"); + } catch (NoSuchProviderException e) { + // Expected + } + } + + /** + * @tests javax.crypto.Cipher#getInstance(java.lang.String, + * java.security.Provider) + */ + 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() 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() 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() 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() throws Exception { + + SecureRandom sr = new SecureRandom(); + Cipher 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#init(int, java.security.Key) + */ + public void test_initILjava_security_Key() throws Exception { + Cipher 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() + throws Exception { + SecureRandom sr = new SecureRandom(); + Cipher 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() + throws Exception { + SecureRandom sr = new SecureRandom(); + Cipher cipher = null; + + byte[] iv = null; + AlgorithmParameterSpec ivAVP = null; + + 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() + throws Exception { + SecureRandom sr = new SecureRandom(); + Cipher cipher = null; + + byte[] iv = null; + AlgorithmParameterSpec ivAVP = null; + + 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) + */ + public void test_update$BII() throws Exception { + for (int index = 1; index < 4; index++) { + Cipher c = Cipher.getInstance("DESEDE/CBC/PKCS5Padding"); + + byte[] keyMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" + + index + ".key"); + DESedeKeySpec keySpec = new DESedeKeySpec(keyMaterial); + SecretKeyFactory skf = SecretKeyFactory.getInstance("DESEDE"); + Key k = skf.generateSecret(keySpec); + + byte[] ivMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" + index + + ".iv"); + IvParameterSpec iv = new IvParameterSpec(ivMaterial); + + c.init(Cipher.DECRYPT_MODE, k, iv); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + byte[] input = new byte[256]; + String resPath = "hyts_" + "des-ede3-cbc.test" + index + + ".ciphertext"; + InputStream is = Support_Resources.getResourceStream(resPath); + + int bytesRead = is.read(input, 0, 256); + while (bytesRead > 0) { + byte[] output = c.update(input, 0, bytesRead); + if (output != null) { + baos.write(output); + } + bytesRead = is.read(input, 0, 256); + } + + byte[] output = c.doFinal(); + if (output != null) { + baos.write(output); + } + + byte[] decipheredCipherText = baos.toByteArray(); + is.close(); + + byte[] plaintextBytes = loadBytes("hyts_" + "des-ede3-cbc.test" + + index + ".plaintext"); + assertTrue("Operation produced incorrect results", Arrays.equals( + plaintextBytes, decipheredCipherText)); + }// end for + } + + /** + * @tests javax.crypto.Cipher#doFinal() + */ + public void test_doFinal() throws Exception { + for (int index = 1; index < 4; index++) { + Cipher c = Cipher.getInstance("DESEDE/CBC/PKCS5Padding"); + + byte[] keyMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" + + index + ".key"); + DESedeKeySpec keySpec = new DESedeKeySpec(keyMaterial); + SecretKeyFactory skf = SecretKeyFactory.getInstance("DESEDE"); + Key k = skf.generateSecret(keySpec); + + byte[] ivMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" + index + + ".iv"); + IvParameterSpec iv = new IvParameterSpec(ivMaterial); + + c.init(Cipher.ENCRYPT_MODE, k, iv); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + byte[] input = new byte[256]; + String resPath = "hyts_" + "des-ede3-cbc.test" + index + + ".plaintext"; + InputStream is = Support_Resources.getResourceStream(resPath); + + int bytesRead = is.read(input, 0, 256); + while (bytesRead > 0) { + byte[] output = c.update(input, 0, bytesRead); + if (output != null) { + baos.write(output); + } + bytesRead = is.read(input, 0, 256); + } + byte[] output = c.doFinal(); + if (output != null) { + baos.write(output); + } + byte[] encryptedPlaintext = baos.toByteArray(); + is.close(); + + byte[] cipherText = loadBytes("hyts_" + "des-ede3-cbc.test" + index + + ".ciphertext"); + assertTrue("Operation produced incorrect results", Arrays.equals( + encryptedPlaintext, cipherText)); + }// end for + } + + private byte[] loadBytes(String resPath) { + try { + InputStream is = Support_Resources.getResourceStream(resPath); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + byte[] buff = new byte[1024]; + int readlen; + while ((readlen = is.read(buff)) > 0) { + out.write(buff, 0, readlen); + } + is.close(); + return out.toByteArray(); + } catch (IOException e) { + return null; + } + } + + public void testGetParameters() throws Exception { + Cipher c = Cipher.getInstance("DES"); + assertNull(c.getParameters()); + } + + /* + * Class under test for int update(byte[], int, int, byte[], int) + */ + public void testUpdatebyteArrayintintbyteArrayint() throws Exception { + Cipher c = Cipher.getInstance("DESede"); + c.init(Cipher.ENCRYPT_MODE, cipherKey); + byte[] b = {1,2,3,4,5,6,7,8,9,10}; + byte[] b1 = new byte[6]; + try { + c.update(b, 0, 10, b1, 5); + fail("No expected ShortBufferException"); + } catch (ShortBufferException e) { + } + } + + /* + * Class under test for int doFinal(byte[], int, int, byte[], int) + */ + public void testDoFinalbyteArrayintintbyteArrayint() throws Exception { + Cipher c = Cipher.getInstance("DESede"); + c.init(Cipher.ENCRYPT_MODE, cipherKey); + byte[] b = {1,2,3,4,5,6,7,8,9,10}; + byte[] b1 = new byte[6]; + // FIXME Failed on BC provider + // try { + // c.doFinal(b, 3, 6, b1, 5); + // fail("No expected ShortBufferException"); + // } catch (ShortBufferException e) { + // } + } + + public void testGetMaxAllowedKeyLength() throws NoSuchAlgorithmException { + try { + Cipher.getMaxAllowedKeyLength(null); + fail("No expected NullPointerException"); + } catch (NullPointerException e) { + } + try { + Cipher.getMaxAllowedKeyLength("//CBC/PKCS5Paddin"); + fail("No expected NoSuchAlgorithmException"); + } catch (NoSuchAlgorithmException e) { + } + try { + Cipher.getMaxAllowedKeyLength("/DES/CBC/PKCS5Paddin/1"); + fail("No expected NoSuchAlgorithmException"); + } catch (NoSuchAlgorithmException e) { + } + } + + public void testGetMaxAllowedParameterSpec() + throws NoSuchAlgorithmException { + try { + Cipher.getMaxAllowedParameterSpec(null); + fail("No expected NullPointerException"); + } catch (NullPointerException e) { + } + try { + Cipher.getMaxAllowedParameterSpec("/DES//PKCS5Paddin"); + fail("No expected NoSuchAlgorithmException"); + } catch (NoSuchAlgorithmException e) { + } + try { + Cipher.getMaxAllowedParameterSpec("/DES/CBC/ /1"); + fail("No expected NoSuchAlgorithmException"); + } catch (NoSuchAlgorithmException e) { + } + } + +}