001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.filter.ssl;
021
022import java.io.File;
023import java.io.FileOutputStream;
024import java.io.InputStream;
025import java.io.OutputStream;
026import java.security.KeyStore;
027
028import org.junit.Test;
029
030/**
031 * Tests {@link KeyStoreFactory}.
032 *
033 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
034 */
035public class KeyStoreFactoryTest {
036    @Test
037    public void testCreateInstanceFromResource() throws Exception {
038        // Test using default for now.
039        KeyStoreFactory factory = new KeyStoreFactory();
040        factory.setDataUrl(getClass().getResource("keystore.cert"));
041        factory.setPassword("boguspw");
042
043        KeyStore ks = factory.newInstance();
044
045        ks.getCertificate("bogus");
046        ks.getKey("bogus", "boguspw".toCharArray());
047    }
048
049    @Test
050    public void testCreateInstanceFromFile() throws Exception {
051        // Copy the keystore from the class path to a temporary file.
052        File file = File.createTempFile("keystoretest ", null);
053        file.deleteOnExit();
054        InputStream in = getClass().getResourceAsStream("keystore.cert");
055        OutputStream out = new FileOutputStream(file);
056        int b;
057
058        while ((b = in.read()) != -1) {
059            out.write(b);
060        }
061
062        in.close();
063        out.close();
064
065        // Test using default for now.
066        KeyStoreFactory factory = new KeyStoreFactory();
067        factory.setDataFile(file);
068        factory.setPassword("boguspw");
069
070        KeyStore ks = factory.newInstance();
071
072        ks.getCertificate("bogus");
073        ks.getKey("bogus", "boguspw".toCharArray());
074    }
075}