Return-Path: Delivered-To: apmail-incubator-harmony-commits-archive@www.apache.org Received: (qmail 94107 invoked from network); 1 Jun 2006 08:17:51 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 1 Jun 2006 08:17:51 -0000 Received: (qmail 41573 invoked by uid 500); 1 Jun 2006 08:17:48 -0000 Delivered-To: apmail-incubator-harmony-commits-archive@incubator.apache.org Received: (qmail 41421 invoked by uid 500); 1 Jun 2006 08:17:47 -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 41400 invoked by uid 99); 1 Jun 2006 08:17:47 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 01 Jun 2006 01:17:47 -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; Thu, 01 Jun 2006 01:17:32 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id 1EBFE1A9859; Thu, 1 Jun 2006 01:16:49 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r410788 [11/13] - in /incubator/harmony/enhanced/classlib/trunk/modules: auth/make/common/ auth/src/test/java/common/javax/security/auth/ auth/src/test/java/common/javax/security/auth/callback/serialization/ auth/src/test/java/common/javax/... Date: Thu, 01 Jun 2006 08:15:44 -0000 To: harmony-commits@incubator.apache.org From: mloenko@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20060601081649.1EBFE1A9859@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/IdentityStub.java URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/IdentityStub.java?rev=410788&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/IdentityStub.java (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/IdentityStub.java Thu Jun 1 01:15:17 2006 @@ -0,0 +1,76 @@ +/* + * 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 Aleksei Y. Semenov +* @version $Revision$ +*/ + +package org.apache.harmony.security.tests.support; + +import java.security.Identity; +import java.security.IdentityScope; +import java.security.KeyManagementException; +import java.security.PublicKey; + +/** + * Stub for abstract class Identity + * + */ + +public class IdentityStub extends Identity { + + /** + * Default constructor + */ + public IdentityStub() { + super(); + } + + /** + * TODO Put ctor description here + * + * @param name + */ + public IdentityStub(String name) { + super(name); + } + + /** + * TODO Put ctor description here + * + * @param name + * @param scope + * @throws KeyManagementException + */ + public IdentityStub(String name, IdentityScope scope) + throws KeyManagementException { + super(name, scope); + } + + /** + * Auxilary constructor + * @param name + * @param key + * @throws KeyManagementException + */ + + public IdentityStub(String name, PublicKey key) throws KeyManagementException{ + this(name); + setPublicKey(key); + } + +} Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/MDGoldenData.java URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/MDGoldenData.java?rev=410788&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/MDGoldenData.java (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/MDGoldenData.java Thu Jun 1 01:15:17 2006 @@ -0,0 +1,200 @@ +/* + * 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 Vladimir N. Molotkov +* @version $Revision$ +*/ + +package org.apache.harmony.security.tests.support; + +import java.util.HashMap; + +/** + * Golden data for Message Digest related tests.
+ * Incapsulates the following data:
+ * - reference message
+ * - reference message digests calculated using + * BEA JRockit j2sdk1.4.2_04 (http://www.bea.com) + * for various MD algorithms: + * SHA-1, SHA-256, SHA-384, SHA-512, MD-5. + * Standart algorithm names are defined in + * "JavaTM Cryptography Architecture API Specification & Reference" + * + */ +public class MDGoldenData { + // The length of test message + private static final int MY_MESSAGE_LEN = 1024; + // Test message for digest computations + private static final byte[] myMessage = new byte[MY_MESSAGE_LEN]; + // Reference digests for various algorithms calculated + // for myMessage + private static final HashMap goldenData = new HashMap(); + + static { + // fill myMessage + for (int i=0; i + * There are two modes of test run: reference generation mode and + * testing mode . The actual mode is selected via + * "test.mode" system property. The testing mode is + * the default mode.
+ * To turn on the reference generation mode , the test.mode property + * should be set to value "serial.reference". In this mode, no testing + * is performed but golden files are produced, which contain reference + * serialized objects. This mode should be run on a pure + * Implementation classes, which are targeted for compartibility.
+ * The location of golden files (in both modes) is controlled via + * "RESOURCE_DIR" system property. + * + */ +public abstract class SerializationTest extends TestCase { + + /** + * Property name for the testing mode. + */ + public static final String MODE_KEY = "test.mode"; + + + /** + * Testing mode. + */ + public static String mode = System.getProperty(MODE_KEY); + + /** + * Reference files generation mode switch. + */ + public static final String SERIAL_REFERENCE_MODE = "serial.reference"; + + /** + * Key to a system property defining root location of golden files. + */ + public static final String GOLDEN_PATH = "RESOURCE_DIR"; + + private static String outputPath = System.getProperty(GOLDEN_PATH, + "test/common/unit"); + + /** + * Parameterized c-tor inherited from superclass. + */ + public SerializationTest(String name) { + super(name); + } + + /** + * Default c-tor inherited from superclass. + */ + public SerializationTest() { + super(); + } + + /** + * Depending on testing mode, produces golden files or performs testing. + */ + public void runBare() throws Throwable { + + if (mode != null && mode.equals(SERIAL_REFERENCE_MODE)) { + produceGoldenFiles(); + } else { + super.runBare(); + } + } + + /** + * Returns zero value to exclude serialization tests from performance runs. + */ + public long getRepeatCount() { + return 0; + } + + /** + * This is the main working method of this framework. Subclasses must + * override it to provide actual objects for testing. + * + * @return array of objects to be de/serialized in tests. + */ + protected abstract Object[] getData(); + + /** + * Compares deserialized and reference objects. This default implementation + * just asserts equality of the two objects. Should be overriden if a class + * under test does not provide specific equals() method and it's instances + * should to be compared manually. + */ + protected void assertDeserialized(Object reference, Object test) { + assertEquals(reference, test); + } + + /** + * Tests that data objects can be serialized and deserialized without + * exceptions, and that deserialization really produces deeply cloned + * objects. + */ + public void testSelf() throws Throwable { + + SerializableAssert comparator = defineComparator(); + + Object[] data = getData(); + for (int i = 0; i < data.length; i++) { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + putObjectToStream(data[i], bos); + ByteArrayInputStream bis = new ByteArrayInputStream(bos + .toByteArray()); + + comparator.assertDeserialized((Serializable) data[i], + (Serializable) getObjectFromStream(bis)); + } + } + + /** + * Tests that data objects can be deserialized from golden files, to verify + * compartibility with Reference Implementation. + */ + public void testGolden() throws Throwable { + + SerializableAssert comparator = defineComparator(); + + Object[] data = getData(); + for (int i = 0; i < data.length; i++) { + comparator.assertDeserialized((Serializable) data[i], + (Serializable) getObjectFromStream(new FileInputStream( + getDataFile(i)))); + } + } + + /** + * Returns golden file for an object being tested. + * + * @param index array index of tested data (as returned by + * {@link #getData() getData()}) + * @return corresponding golden file + */ + protected File getDataFile(int index) { + String name = this.getClass().getName(); + int dot = name.lastIndexOf("."); + String path = name.substring(0, dot).replace('.', File.separatorChar); + if (outputPath != null && outputPath.length() != 0) { + path = outputPath + File.separator + path; + } + + return new File(path, name.substring(dot + 1) + "." + index + ".dat"); + } + + /** + * Working method for files generation mode. Serializes test objects + * returned by {@link #getData() getData()}to golden files, each object to + * a separate file. + * + * @throws IOException + */ + protected void produceGoldenFiles() throws IOException { + Object[] data = getData(); + for (int i = 0; i < data.length; i++) { + File gf = getDataFile(i); + gf.getParentFile().mkdirs(); + gf.createNewFile(); + putObjectToStream(data[i], new FileOutputStream(gf)); + } + } + + /** + * Serializes specified object to an output stream. + */ + protected void putObjectToStream(Object obj, OutputStream os) + throws IOException { + ObjectOutputStream oos = new ObjectOutputStream(os); + oos.writeObject(obj); + oos.flush(); + oos.close(); + } + + /** + * Deserializes single object from an input stream. + */ + protected Object getObjectFromStream(InputStream is) throws IOException, + ClassNotFoundException { + ObjectInputStream ois = new ObjectInputStream(is); + Object result = ois.readObject(); + ois.close(); + return result; + } + + /** + * Interface to compare (de)serialized objects + */ + public interface SerializableAssert { + void assertDeserialized(Serializable reference, Serializable test); + } + + // default comparator for a class that has equals(Object) method + private final static SerializableAssert DEFAULT_COMPARATOR = new SerializableAssert() { + public void assertDeserialized(Serializable reference, Serializable test) { + TestCase.assertEquals(reference, test); + } + }; + + // for comparing java.lang.Throwable objects + private final static SerializableAssert THROWABLE_COMPARATOR = new SerializableAssert() { + public void assertDeserialized(Serializable reference, Serializable test) { + + Throwable refThr = (Throwable) reference; + Throwable tstThr = (Throwable) test; + + // verify class + TestCase.assertEquals(refThr.getClass(), tstThr.getClass()); + + // verify message + TestCase.assertEquals(refThr.getMessage(), tstThr.getMessage()); + + // verify cause + if (refThr.getCause() == null) { + TestCase.assertNull(tstThr.getCause()); + } else { + TestCase.assertNotNull(tstThr.getCause()); + + refThr = refThr.getCause(); + tstThr = tstThr.getCause(); + + TestCase.assertEquals(refThr.getClass(), tstThr.getClass()); + TestCase.assertEquals(refThr.getMessage(), tstThr.getMessage()); + } + } + }; + + private SerializableAssert defineComparator() throws Exception { + + if (this instanceof SerializableAssert) { + return (SerializableAssert) this; + } + + Object s[] = getData(); + if (s == null || s.length == 0) { + // nothing to compare - OK with default comparator + return DEFAULT_COMPARATOR; + + } + + Method m = s[0].getClass().getMethod("equals", Object.class); + + if (m.getDeclaringClass() != Object.class) { + // one of classes overrides Object.equals(Object) method + // use default comparator + return DEFAULT_COMPARATOR; + } + + // TODO use generics to detect comparator + // instead of 'instanceof' for the first element + if(s[0] instanceof java.lang.Throwable){ + return THROWABLE_COMPARATOR; + } + + // TODO - throw new RuntimeException() if failed to detect comparator + // return stub comparator for a while + final SerializationTest thisTest = this; + return new SerializableAssert() { + public void assertDeserialized(Serializable reference, + Serializable test) { + thisTest.assertDeserialized(reference, test); + } + }; + } +} Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/SignerStub.java URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/SignerStub.java?rev=410788&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/SignerStub.java (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/SignerStub.java Thu Jun 1 01:15:17 2006 @@ -0,0 +1,63 @@ +/* + * 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 Aleksei Y. Semenov +* @version $Revision$ +*/ + +package org.apache.harmony.security.tests.support; + +import java.security.IdentityScope; +import java.security.KeyManagementException; +import java.security.Signer; + +/** + * Stub for abstract class Signer, necessary for testing purposes + * + */ + +public class SignerStub extends Signer { + + /** + * Default constructor + */ + public SignerStub() { + super(); + } + + /** + * Constructor, sets given name + * + * @param name + */ + public SignerStub(String name) { + super(name); + } + + /** + * Constructor, sets given name and scope + * + * @param name + * @param scope + * @throws KeyManagementException + */ + public SignerStub(String name, IdentityScope scope) + throws KeyManagementException { + super(name, scope); + } + +} Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/SpiEngUtils.java URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/SpiEngUtils.java?rev=410788&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/SpiEngUtils.java (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/SpiEngUtils.java Thu Jun 1 01:15:17 2006 @@ -0,0 +1,84 @@ +/* + * 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 Vera Y. Petrashkova +* @version $Revision$ +*/ + +package org.apache.harmony.security.tests.support; + +import java.io.File; +import java.security.Security; +import java.security.Provider; +import java.util.StringTokenizer; + +/** + * Additional class for verification spi-engine classes + * + */ + +public class SpiEngUtils { + + public static final String[] invalidValues = { + "", + "BadAlgorithm", + "Long message Long message Long message Long message Long message Long message Long message Long message Long message Long message Long message Long message Long message" }; + + /** + * Verification: is algorithm supported or not + * + * @param algorithm + * @param service + * @return + */ + public static Provider isSupport(String algorithm, String service) { + try { + Provider[] provs = Security.getProviders(service.concat(".") + .concat(algorithm)); + if (provs == null) { + return null; + } + return (provs.length == 0 ? null : provs[0]); + } catch (Exception e) { + return null; + } + } + + public static String getFileName(String dir, String name) { + String res = dir; + if (res.charAt(res.length() - 1) == '/') { + res = res.substring(0, res.length() - 1); + } + char[] mm = { File.separatorChar }; + String sp = String.copyValueOf(mm); + StringTokenizer st = new StringTokenizer(name, "/"); + while (st.hasMoreElements()) { + res = res.concat(sp).concat((String) st.nextElement()); + } + return res; + } + + public class MyProvider extends Provider { + + public MyProvider(String name, String info, String key, String clName) { + super(name, 1.0, info); + put(key, clName); + } + + } + +} \ No newline at end of file