1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18/**
19 * @author Vladimir N. Molotkov
20 * @version $Revision$
21 */
22
23package org.apache.harmony.security.tests.java.security;
24
25import java.io.NotSerializableException;
26import java.io.ObjectStreamException;
27import java.security.KeyRep;
28import java.security.Security;
29import java.util.Set;
30import junit.framework.TestCase;
31
32public class KeyRepTest extends TestCase {
33
34    private static final Set<String> keyFactoryAlgorithms = Security.getAlgorithms("KeyFactory");
35    static {
36        assertFalse(keyFactoryAlgorithms.isEmpty());
37    }
38
39    public final void testKeyRep01() {
40        assertNotNull(new KeyRep(KeyRep.Type.SECRET, "", "", new byte[] {}));
41        assertNotNull(new KeyRep(KeyRep.Type.PUBLIC, "", "", new byte[] {}));
42        assertNotNull(new KeyRep(KeyRep.Type.PRIVATE, "", "", new byte[] {}));
43    }
44
45    public final void testKeyRep02() {
46        try {
47            new KeyRep(null, "", "", new byte[] {});
48            fail("NullPointerException has not been thrown (type)");
49        } catch (NullPointerException expected) {
50        }
51        try {
52            new KeyRep(KeyRep.Type.SECRET, null, "", new byte[] {});
53            fail("NullPointerException has not been thrown (alg)");
54        } catch (NullPointerException expected) {
55        }
56        try {
57            new KeyRep(KeyRep.Type.PRIVATE, "", null, new byte[] {});
58            fail("NullPointerException has not been thrown (format)");
59        } catch (NullPointerException expected) {
60        }
61        try {
62            new KeyRep(KeyRep.Type.PUBLIC, "", "", null);
63            fail("NullPointerException has not been thrown (encoding)");
64        } catch (NullPointerException expected) {
65        }
66    }
67
68    public final void testReadResolve01() throws Exception {
69        KeyRepChild kr = new KeyRepChild(KeyRep.Type.SECRET, "", "", new byte[] {});
70        try {
71            kr.readResolve();
72            fail("NotSerializableException has not been thrown (no format)");
73        } catch (NotSerializableException expected) {
74        }
75
76        kr = new KeyRepChild(KeyRep.Type.SECRET, "", "X.509", new byte[] {});
77        try {
78            kr.readResolve();
79            fail("NotSerializableException has not been thrown (unacceptable format)");
80        } catch (NotSerializableException expected) {
81        }
82
83        kr = new KeyRepChild(KeyRep.Type.SECRET, "", "RAW", new byte[] {});
84        try {
85            kr.readResolve();
86            fail("NotSerializableException has not been thrown (empty key)");
87        } catch (NotSerializableException expected) {
88        }
89    }
90
91    public final void testReadResolve02() throws Exception {
92        KeyRepChild kr = new KeyRepChild(KeyRep.Type.PUBLIC, "", "", new byte[] {});
93        try {
94            kr.readResolve();
95            fail("NotSerializableException has not been thrown (no format)");
96        } catch (NotSerializableException expected) {
97        }
98
99        kr = new KeyRepChild(KeyRep.Type.PUBLIC, "", "RAW", new byte[] {});
100        try {
101            kr.readResolve();
102            fail("NotSerializableException has not been thrown (unacceptable format)");
103        } catch (NotSerializableException expected) {
104        }
105
106        kr = new KeyRepChild(KeyRep.Type.PUBLIC, "bla-bla", "X.509", new byte[] {});
107        try {
108            kr.readResolve();
109            fail("NotSerializableException has not been thrown (unknown KeyFactory algorithm)");
110        } catch (NotSerializableException expected) {
111        }
112    }
113
114    public final void testReadResolve03() throws Exception {
115        KeyRepChild kr = new KeyRepChild(KeyRep.Type.PRIVATE, "", "", new byte[] {});
116        try {
117            kr.readResolve();
118            fail("NotSerializableException has not been thrown (no format)");
119        } catch (NotSerializableException expected) {
120        }
121
122        kr = new KeyRepChild(KeyRep.Type.PRIVATE, "", "RAW", new byte[] {});
123        try {
124            kr.readResolve();
125            fail("NotSerializableException has not been thrown (unacceptable format)");
126        } catch (NotSerializableException expected) {
127        }
128
129        kr = new KeyRepChild(KeyRep.Type.PRIVATE, "bla-bla", "PKCS#8", new byte[] {});
130        try {
131            kr.readResolve();
132            fail("NotSerializableException has not been thrown (unknown KeyFactory algorithm)");
133        } catch (NotSerializableException expected) {
134        }
135    }
136
137    public final void testReadResolve04() throws Exception {
138        for (String algorithm : keyFactoryAlgorithms) {
139            KeyRepChild kr = new KeyRepChild(KeyRep.Type.PUBLIC, algorithm, "X.509",
140                                             new byte[] { 1, 2, 3 });
141            try {
142                kr.readResolve();
143                fail("NotSerializableException has not been thrown (no format) " + algorithm);
144            } catch (NotSerializableException expected) {
145            }
146        }
147    }
148
149    public final void testReadResolve05() throws Exception {
150        for (String algorithm : keyFactoryAlgorithms) {
151            KeyRepChild kr = new KeyRepChild(KeyRep.Type.PRIVATE, algorithm, "PKCS#8",
152                                             new byte[] { 1, 2, 3 });
153            try {
154                kr.readResolve();
155                fail("NotSerializableException has not been thrown (no format) " + algorithm);
156            } catch (NotSerializableException expected) {
157            }
158        }
159    }
160
161    class KeyRepChild extends KeyRep {
162        public KeyRepChild(KeyRep.Type type, String algorithm, String format, byte[] encoded) {
163            super(type, algorithm, format, encoded);
164        }
165
166        // Overriden to make public for testing
167        @Override public Object readResolve() throws ObjectStreamException {
168            return super.readResolve();
169        }
170    }
171}
172