1561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes/*
2561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  Licensed to the Apache Software Foundation (ASF) under one or more
3561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  contributor license agreements.  See the NOTICE file distributed with
4561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  this work for additional information regarding copyright ownership.
5561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  The ASF licenses this file to You under the Apache License, Version 2.0
6561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  (the "License"); you may not use this file except in compliance with
7561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  the License.  You may obtain a copy of the License at
8561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *
9561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *     http://www.apache.org/licenses/LICENSE-2.0
10561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *
11561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  Unless required by applicable law or agreed to in writing, software
12561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  distributed under the License is distributed on an "AS IS" BASIS,
13561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  See the License for the specific language governing permissions and
15561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  limitations under the License.
16561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes */
17561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
18561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes/**
19561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes* @author Vladimir N. Molotkov
20561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes*/
21561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
22561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughespackage org.apache.harmony.security.tests.java.security;
23561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
24561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport junit.framework.TestCase;
25561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
26561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes/**
27561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *
28561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *
29561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes */
30561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughespublic class KeyRepTest extends TestCase {
31561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//
32561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//    private static final Set<String> keyFactoryAlgorithm;
33561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//    static {
34561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        keyFactoryAlgorithm = Security.getAlgorithms("KeyFactory");
35561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//    }
36561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//
37561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public final void testKeyRep01() {
38561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        KeyRep kr = new KeyRep(KeyRep.Type.SECRET, "", "", new byte[] {});
39561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        kr = new KeyRep(KeyRep.Type.PUBLIC, "", "", new byte[] {});
40561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        kr = new KeyRep(KeyRep.Type.PRIVATE, "", "", new byte[] {});
41561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
42561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//
43561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//    public final void testKeyRep02() {
44561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        try {
45561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            KeyRep kr = new KeyRep(null, "", "", new byte[] {});
46561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            fail("NullPointerException has not been thrown (type)");
47561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        } catch (NullPointerException ok) {
48561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            System.out.println(getName() + ": " + ok);
49561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        }
50561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        try {
51561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            KeyRep kr = new KeyRep(KeyRep.Type.SECRET, null, "", new byte[] {});
52561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            fail("NullPointerException has not been thrown (alg)");
53561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        } catch (NullPointerException ok) {
54561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            System.out.println(getName() + ": " + ok);
55561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        }
56561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        try {
57561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            KeyRep kr = new KeyRep(KeyRep.Type.PRIVATE, "", null, new byte[] {});
58561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            fail("NullPointerException has not been thrown (format)");
59561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        } catch (NullPointerException ok) {
60561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            System.out.println(getName() + ": " + ok);
61561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        }
62561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        try {
63561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            KeyRep kr = new KeyRep(KeyRep.Type.PUBLIC, "", "", null);
64561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            fail("NullPointerException has not been thrown (encoding)");
65561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        } catch (NullPointerException ok) {
66561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            System.out.println(getName() + ": " + ok);
67561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        }
68561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//    }
69561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//
70561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//    public final void testReadResolve01() throws ObjectStreamException {
71561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        KeyRep kr = new KeyRep(KeyRep.Type.SECRET, "", "", new byte[] {});
72561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        try {
73561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            kr.readResolve();
74561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            fail("NotSerializableException has not been thrown (no format)");
75561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        } catch (NotSerializableException ok) {
76561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            System.out.println(getName() + ": " + ok);
77561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        }
78561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//
79561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        kr = new KeyRep(KeyRep.Type.SECRET, "", "X.509", new byte[] {});
80561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        try {
81561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            kr.readResolve();
82561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            fail("NotSerializableException has not been thrown (unacceptable format)");
83561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        } catch (NotSerializableException ok) {
84561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            System.out.println(getName() + ": " + ok);
85561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        }
86561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//
87561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        kr = new KeyRep(KeyRep.Type.SECRET, "", "RAW", new byte[] {});
88561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        try {
89561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            kr.readResolve();
90561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            fail("NotSerializableException has not been thrown (empty key)");
91561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        } catch (NotSerializableException ok) {
92561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            System.out.println(getName() + ": " + ok);
93561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        }
94561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//    }
95561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//
96561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//    public final void testReadResolve02() throws ObjectStreamException {
97561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        KeyRep kr = new KeyRep(KeyRep.Type.PUBLIC, "", "", new byte[] {});
98561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        try {
99561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            kr.readResolve();
100561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            fail("NotSerializableException has not been thrown (no format)");
101561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        } catch (NotSerializableException ok) {
102561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            System.out.println(getName() + ": " + ok);
103561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        }
104561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//
105561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        kr = new KeyRep(KeyRep.Type.PUBLIC, "", "RAW", new byte[] {});
106561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        try {
107561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            kr.readResolve();
108561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            fail("NotSerializableException has not been thrown (unacceptable format)");
109561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        } catch (NotSerializableException ok) {
110561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            System.out.println(getName() + ": " + ok);
111561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        }
112561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//
113561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        kr = new KeyRep(KeyRep.Type.PUBLIC, "bla-bla", "X.509", new byte[] {});
114561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        try {
115561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            kr.readResolve();
116561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            fail("NotSerializableException has not been thrown (unknown KeyFactory algorithm)");
117561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        } catch (NotSerializableException ok) {
118561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            System.out.println(getName() + ": " + ok);
119561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        }
120561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//    }
121561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//
122561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//    public final void testReadResolve03() throws ObjectStreamException {
123561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        KeyRep kr = new KeyRep(KeyRep.Type.PRIVATE, "", "", new byte[] {});
124561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        try {
125561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            kr.readResolve();
126561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            fail("NotSerializableException has not been thrown (no format)");
127561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        } catch (NotSerializableException ok) {
128561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            System.out.println(getName() + ": " + ok);
129561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        }
130561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//
131561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        kr = new KeyRep(KeyRep.Type.PRIVATE, "", "RAW", new byte[] {});
132561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        try {
133561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            kr.readResolve();
134561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            fail("NotSerializableException has not been thrown (unacceptable format)");
135561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        } catch (NotSerializableException ok) {
136561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            System.out.println(getName() + ": " + ok);
137561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        }
138561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//
139561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        kr = new KeyRep(KeyRep.Type.PRIVATE, "bla-bla", "PKCS#8", new byte[] {});
140561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        try {
141561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            kr.readResolve();
142561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            fail("NotSerializableException has not been thrown (unknown KeyFactory algorithm)");
143561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        } catch (NotSerializableException ok) {
144561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            System.out.println(getName() + ": " + ok);
145561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        }
146561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//    }
147561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//
148561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//    public final void testReadResolve04() throws ObjectStreamException {
149561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        if (keyFactoryAlgorithm.isEmpty()) {
150561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            System.err.println(getName() + ": skipped - no KeyFactory algorithms available");
151561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            return;
152561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        } else {
153561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            System.out.println(getName() + ": available algorithms - " +
154561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//                    keyFactoryAlgorithm);
155561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        }
156561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        for (Iterator<String> i = keyFactoryAlgorithm.iterator(); i.hasNext();) {
157561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            KeyRep kr = new KeyRep(KeyRep.Type.PUBLIC,
158561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//                    i.next(), "X.509", new byte[] {1,2,3});
159561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            try {
160561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//                kr.readResolve();
161561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//                fail("NotSerializableException has not been thrown (no format)");
162561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            } catch (NotSerializableException ok) {
163561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//                System.out.println(getName() + ": " + ok);
164561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            }
165561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        }
166561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//    }
167561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//
168561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//    public final void testReadResolve05() throws ObjectStreamException {
169561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        if (keyFactoryAlgorithm.isEmpty()) {
170561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            System.err.println(getName() + ": skipped - no KeyFactory algorithms available");
171561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            return;
172561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        } else {
173561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            System.out.println(getName() + ": available algorithms - " +
174561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//                    keyFactoryAlgorithm);
175561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        }
176561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        for (Iterator<String> i = keyFactoryAlgorithm.iterator(); i.hasNext();) {
177561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            KeyRep kr = new KeyRep(KeyRep.Type.PRIVATE,
178561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//                    i.next(), "PKCS#8", new byte[] {1,2,3});
179561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            try {
180561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//                kr.readResolve();
181561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//                fail("NotSerializableException has not been thrown (no format)");
182561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            } catch (NotSerializableException ok) {
183561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//                System.out.println(getName() + ": " + ok);
184561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//            }
185561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//        }
186561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes//    }
187561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes}
188