/****************************************************************************
 * Copyright (c) 1998-2009 AOL LLC. 
 * 
 * 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 net.oauth.signature;

import java.io.IOException;
import junit.framework.TestCase;
import net.oauth.OAuthConsumer;
import net.oauth.OAuthServiceProvider;

/**
 * @author zhang
 * @author John Kristian
 */
public class PkcsTest extends TestCase {

    protected static final String PEM_FILE_DIR = "net/oauth/signature/pem/";

    protected OAuthConsumer client;
    protected OAuthConsumer server;
    protected ClassLoader classLoader;

    @Override
    protected void setUp() throws Exception {
        OAuthServiceProvider provider = new OAuthServiceProvider("http://www.google.com/url1",
                "http://www.google.com/url2", "http://www.google.com/url3");
        client = new OAuthConsumer(provider.requestTokenURL, "google", null, provider);
        server = new OAuthConsumer(provider.requestTokenURL, "google", null, provider);
        classLoader = Thread.currentThread().getContextClassLoader();
        if (classLoader == null)
            classLoader = PkcsTest.class.getClassLoader();
    }

    private String readResource(String name) throws IOException {
        return new String(Echo.readAll(classLoader.getResourceAsStream(PEM_FILE_DIR + name)), "UTF-8");
    }

    /**
     * Sign with Private Key PKCS1 PEM file Verify with Certificate X509 PEM
     * file
     * 
     * @throws Exception
     */
    public void testPkcs1() throws Exception {
        client.setProperty(RSA_SHA1.PRIVATE_KEY, readResource("private_pkcs1.pem"));
        server.setProperty(RSA_SHA1.X509_CERTIFICATE, readResource("certificate_x509.pem"));
        RSA_SHA1SignatureTest.signAndVerify(client, server);
    }

    /**
     * Sign with Private Key PKCS8 PEM file Verify with Public Key X509 PEM file
     * 
     * @throws Exception
     */
    public void testPkcs8() throws Exception {
        client.setProperty(RSA_SHA1.PRIVATE_KEY, readResource("private_pkcs8.pem"));
        server.setProperty(RSA_SHA1.PUBLIC_KEY, readResource("public_x509.pem"));
        RSA_SHA1SignatureTest.signAndVerify(client, server);
    }
}

@@ -1080,16 +1080,118 @@
 e/pem/%22;
+%0A    protected static final String%5B%5D PRIVATE_KEY_FILES = %7B %22private_pkcs1.pem%22, %22private_pkcs8.pem%22 %7D;
 %0A%0A    pr
@@ -1896,23 +1896,19 @@
 ing read
-Resourc
+Fil
 e(String
@@ -2064,45 +2064,8 @@
    *
- Sign with Private Key PKCS1 PEM file
  Ver
@@ -2073,17 +2073,19 @@
 fy with 
-C
+a c
 ertifica
@@ -2091,61 +2091,31 @@
 ate 
-X509 PEM%0A     * file%0A     * %0A     * @throws Exception
+from an X.509 PEM file.
 %0A   
@@ -2143,13 +2143,19 @@
 test
-Pkcs1
+Certificate
 () t
@@ -2172,38 +2172,38 @@
 ption %7B%0A        
-client
+server
 .setProperty(RSA
@@ -2212,146 +2212,195 @@
 HA1.
-PRIVATE_KEY, readResource(%22private_pkcs1.pem%22));%0A        server.setProperty(RSA_SHA1.X509_CERTIFICATE, readResource(%22certificate_x509.pem%22
+X509_CERTIFICATE, readFile(%22certificate_x509.pem%22));%0A        for (String privateKeyFile : PRIVATE_KEY_FILES) %7B%0A            client.setProperty(RSA_SHA1.PRIVATE_KEY, readFile(privateKeyFile
 ));%0A
@@ -2399,32 +2399,36 @@
 File));%0A        
+   
+ 
 RSA_SHA1Signatur
@@ -2472,62 +2472,35 @@
     
-%7D%0A%0A
     
-/**
+%7D
 %0A    
- * Sign with Private Key PKCS8 PEM file
+%7D%0A%0A    /**%0A     *
  Ver
@@ -2512,20 +2512,31 @@
 ith 
-P
+a p
 ublic 
-K
+k
 ey 
+from an 
 X
+.
 509 
@@ -2547,41 +2547,9 @@
 file
-%0A     * %0A     * @throws Exception
+.
 %0A   
@@ -2574,20 +2574,24 @@
 id testP
-kcs8
+ublicKey
 () throw
@@ -2604,38 +2604,38 @@
 ption %7B%0A        
-client
+server
 .setProperty(RSA
@@ -2633,38 +2633,37 @@
 perty(RSA_SHA1.P
-RIVATE
+UBLIC
 _KEY, readResour
@@ -2660,54 +2660,110 @@
 read
-Resource(%22private_pkcs8.pem%22));%0A        server
+File(%22public_x509.pem%22));%0A        for (String privateKeyFile : PRIVATE_KEY_FILES) %7B%0A            client
 .set
@@ -2773,37 +2773,38 @@
 perty(RSA_SHA1.P
-UBLIC
+RIVATE
 _KEY, readResour
@@ -2801,38 +2801,35 @@
 read
-Resource(%22public_x509.pem%22));%0A
+File(privateKeyFile));%0A    
     
@@ -2881,20 +2881,30 @@
 t, server);%0A
+        %7D%0A
     %7D%0A%7D%0A

Test all combinations of signing and verifying materials.