You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@abdera.apache.org by jm...@apache.org on 2006/09/06 18:28:03 UTC

svn commit: r440769 - in /incubator/abdera/java/trunk/security/src/test: java/org/ java/org/apache/ java/org/apache/abdera/ java/org/apache/abdera/test/ java/org/apache/abdera/test/security/ resources/

Author: jmsnell
Date: Wed Sep  6 09:28:02 2006
New Revision: 440769

URL: http://svn.apache.org/viewvc?view=rev&rev=440769
Log:
Adding security module tests.

 * Test XML digital signature creation, verification, roundtrip
 * Test XML Encryption roundtrip

Added:
    incubator/abdera/java/trunk/security/src/test/java/org/
    incubator/abdera/java/trunk/security/src/test/java/org/apache/
    incubator/abdera/java/trunk/security/src/test/java/org/apache/abdera/
    incubator/abdera/java/trunk/security/src/test/java/org/apache/abdera/test/
    incubator/abdera/java/trunk/security/src/test/java/org/apache/abdera/test/security/
    incubator/abdera/java/trunk/security/src/test/java/org/apache/abdera/test/security/DigitalSignatureTest.java
    incubator/abdera/java/trunk/security/src/test/java/org/apache/abdera/test/security/EncryptionTest.java
    incubator/abdera/java/trunk/security/src/test/java/org/apache/abdera/test/security/TestSuite.java
    incubator/abdera/java/trunk/security/src/test/resources/
    incubator/abdera/java/trunk/security/src/test/resources/key.jks   (with props)
    incubator/abdera/java/trunk/security/src/test/resources/log4j.properties

Added: incubator/abdera/java/trunk/security/src/test/java/org/apache/abdera/test/security/DigitalSignatureTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/security/src/test/java/org/apache/abdera/test/security/DigitalSignatureTest.java?view=auto&rev=440769
==============================================================================
--- incubator/abdera/java/trunk/security/src/test/java/org/apache/abdera/test/security/DigitalSignatureTest.java (added)
+++ incubator/abdera/java/trunk/security/src/test/java/org/apache/abdera/test/security/DigitalSignatureTest.java Wed Sep  6 09:28:02 2006
@@ -0,0 +1,109 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* 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.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.test.security;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.security.KeyStore;
+import java.security.PrivateKey;
+import java.security.cert.X509Certificate;
+
+import javax.xml.namespace.QName;
+
+import junit.framework.TestCase;
+
+import org.apache.abdera.Abdera;
+import org.apache.abdera.factory.Factory;
+import org.apache.abdera.model.Document;
+import org.apache.abdera.model.Entry;
+import org.apache.abdera.security.AbderaSecurity;
+import org.apache.abdera.security.Signature;
+import org.apache.abdera.security.SignatureOptions;
+
+public class DigitalSignatureTest extends TestCase {
+
+  private static final String keystoreFile = "/key.jks";
+  private static final String keystoreType = "JKS";
+  private static final String keystorePass = "testing";
+  private static final String privateKeyAlias = "James";
+  private static final String privateKeyPass = "testing";
+  private static final String certificateAlias = "James";
+  
+  public static void testSignEntry() throws Exception {
+    
+    // Initialize the keystore
+    KeyStore ks = KeyStore.getInstance(keystoreType);
+    assertNotNull(ks);
+    
+    InputStream in = DigitalSignatureTest.class.getResourceAsStream(keystoreFile);
+    assertNotNull(in);
+    
+    ks.load(in, keystorePass.toCharArray());
+    PrivateKey signingKey = 
+      (PrivateKey) ks.getKey(
+        privateKeyAlias,
+        privateKeyPass.toCharArray());
+    X509Certificate cert = 
+      (X509Certificate) ks.getCertificate(
+        certificateAlias);
+    assertNotNull(signingKey);
+    assertNotNull(cert);
+    
+    // Create the entry to sign
+    Abdera abdera = new Abdera();
+    AbderaSecurity absec = new AbderaSecurity(abdera);
+    Factory factory = abdera.getFactory();
+    
+    Entry entry = factory.newEntry();
+    entry.setId("http://example.org/foo/entry");  
+    entry.setUpdated(new java.util.Date());
+    entry.setTitle("This is an entry");
+    entry.setContentAsXhtml("This <b>is</b> <i>markup</i>");
+    entry.addAuthor("James");
+    entry.addLink("http://www.example.org");
+    
+    // Prepare the digital signature options
+    Signature sig = absec.getSignature();
+    SignatureOptions options = sig.getDefaultSignatureOptions();    
+    options.setCertificate(cert);
+    options.setSigningKey(signingKey);
+
+    // Sign the entry
+    entry = sig.sign(entry, options);
+    assertNotNull(
+      entry.getFirstChild(
+        new QName(
+          "http://www.w3.org/2000/09/xmldsig#", 
+          "Signature")));
+      
+    // Check the round trip
+    ByteArrayOutputStream out = new ByteArrayOutputStream();
+    entry.writeTo(out); // do not use the pretty writer, it will break the signature
+    ByteArrayInputStream bais = new ByteArrayInputStream(out.toByteArray());
+    Document<Entry> entry_doc = abdera.getParser().parse(bais);
+    entry = entry_doc.getRoot();
+    assertTrue(sig.verify(entry, null));  // the signature better still be valid
+    
+    entry.setTitle("Change the title");
+    
+    assertFalse(sig.verify(entry, null)); // the signature better be invalid
+    
+  }
+  
+}

Added: incubator/abdera/java/trunk/security/src/test/java/org/apache/abdera/test/security/EncryptionTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/security/src/test/java/org/apache/abdera/test/security/EncryptionTest.java?view=auto&rev=440769
==============================================================================
--- incubator/abdera/java/trunk/security/src/test/java/org/apache/abdera/test/security/EncryptionTest.java (added)
+++ incubator/abdera/java/trunk/security/src/test/java/org/apache/abdera/test/security/EncryptionTest.java Wed Sep  6 09:28:02 2006
@@ -0,0 +1,93 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* 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.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.test.security;
+
+import javax.crypto.KeyGenerator;
+import javax.crypto.SecretKey;
+import javax.xml.namespace.QName;
+
+import org.apache.abdera.Abdera;
+import org.apache.abdera.factory.Factory;
+import org.apache.abdera.model.Document;
+import org.apache.abdera.model.Entry;
+import org.apache.abdera.security.AbderaSecurity;
+import org.apache.abdera.security.Encryption;
+import org.apache.abdera.security.EncryptionOptions;
+
+import junit.framework.TestCase;
+
+public class EncryptionTest extends TestCase {
+
+  /**
+   * The bouncy castle JCE provider is required to run this test
+   */
+  @SuppressWarnings("unchecked")
+  public static void testEncryption() throws Exception {
+    
+    try {
+      Class.forName("org.bouncycastle.LICENSE");
+    } catch (Exception e) {
+      EncryptionTest.fail("The Bouncy Castle JCE Provider is not available");
+    }
+    
+    // Generate Encryption Key
+    String jceAlgorithmName = "AES";
+    KeyGenerator keyGenerator =
+        KeyGenerator.getInstance(jceAlgorithmName);
+    keyGenerator.init(128);
+    SecretKey key = keyGenerator.generateKey();
+
+    // Create the entry to encrypt
+    Abdera abdera = new Abdera();
+    AbderaSecurity absec = new AbderaSecurity(abdera);
+    Factory factory = abdera.getFactory();
+    
+    Entry entry = factory.newEntry();
+    entry.setId("http://example.org/foo/entry");
+    entry.setUpdated(new java.util.Date());
+    entry.setTitle("This is an entry");
+    entry.setContentAsXhtml("This <b>is</b> <i>markup</i>");
+    entry.addAuthor("James");
+    entry.addLink("http://www.example.org");
+
+    // Prepare the encryption options
+    Encryption enc = absec.getEncryption();
+    EncryptionOptions options = enc.getDefaultEncryptionOptions();
+    options.setDataEncryptionKey(key);
+    
+    // Encrypt the document using the generated key
+    Document enc_doc = enc.encrypt(entry.getDocument(), options);
+    
+    assertEquals(
+      enc_doc.getRoot().getQName(), 
+      new QName(
+        "http://www.w3.org/2001/04/xmlenc#", 
+        "EncryptedData"));
+    
+    // Decrypt the document using the generated key
+    Document<Entry> entry_doc = enc.decrypt(enc_doc, options);
+
+    assertTrue(entry_doc.getRoot() instanceof Entry);
+    
+    assertEquals(
+      entry_doc.getRoot().getId().toString(), 
+      "http://example.org/foo/entry");
+    
+  }
+  
+}

Added: incubator/abdera/java/trunk/security/src/test/java/org/apache/abdera/test/security/TestSuite.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/security/src/test/java/org/apache/abdera/test/security/TestSuite.java?view=auto&rev=440769
==============================================================================
--- incubator/abdera/java/trunk/security/src/test/java/org/apache/abdera/test/security/TestSuite.java (added)
+++ incubator/abdera/java/trunk/security/src/test/java/org/apache/abdera/test/security/TestSuite.java Wed Sep  6 09:28:02 2006
@@ -0,0 +1,31 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* 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.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.test.security;
+
+public class TestSuite extends junit.framework.TestSuite {
+
+  public static void main(String[] args) {
+    junit.textui.TestRunner.run(new TestSuite());
+  }  
+  
+  public TestSuite() {
+    addTestSuite(DigitalSignatureTest.class);
+    addTestSuite(EncryptionTest.class);
+  }
+  
+}

Added: incubator/abdera/java/trunk/security/src/test/resources/key.jks
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/security/src/test/resources/key.jks?view=auto&rev=440769
==============================================================================
Binary file - no diff available.

Propchange: incubator/abdera/java/trunk/security/src/test/resources/key.jks
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: incubator/abdera/java/trunk/security/src/test/resources/log4j.properties
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/security/src/test/resources/log4j.properties?view=auto&rev=440769
==============================================================================
--- incubator/abdera/java/trunk/security/src/test/resources/log4j.properties (added)
+++ incubator/abdera/java/trunk/security/src/test/resources/log4j.properties Wed Sep  6 09:28:02 2006
@@ -0,0 +1,5 @@
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.Target=System.err
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=%c{1}.%M: %m%n
+log4j.rootLogger=warn, stdout
\ No newline at end of file