• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Boris V. Kuznetsov
20 * @version $Revision$
21 */
22 
23 package tests.java.security;
24 
25 import java.security.NoSuchAlgorithmException;
26 import java.security.NoSuchProviderException;
27 import java.security.Provider;
28 import java.security.SecureRandom;
29 import java.security.Security;
30 import junit.framework.TestCase;
31 import org.apache.harmony.security.tests.support.RandomImpl;
32 
33 /**
34  * Tests for <code>SecureRandom</code> constructor and methods
35  *
36  */
37 public class SecureRandomTest extends TestCase {
38 
39     /**
40      * SRProvider
41      */
42     Provider p;
43 
44     /*
45      * @see TestCase#setUp()
46      */
setUp()47     protected void setUp() throws Exception {
48         super.setUp();
49         p = new SRProvider();
50         Security.insertProviderAt(p, 1);
51     }
52 
53     /*
54      * @see TestCase#tearDown()
55      */
tearDown()56     protected void tearDown() throws Exception {
57         super.tearDown();
58         Security.removeProvider(p.getName());
59     }
60 
testNext()61     public final void testNext() {
62         MySecureRandom sr = new MySecureRandom();
63         if (sr.nextElement(1) != 1 || sr.nextElement(2) != 3 || sr.nextElement(3) != 7) {
64             fail("next failed");
65         }
66     }
67 
68     /*
69      * Class under test for void setSeed(long)
70      */
testSetSeedlong()71     public final void testSetSeedlong() {
72         SecureRandom sr = new SecureRandom();
73         sr.setSeed(12345);
74         if (!RandomImpl.runEngineSetSeed) {
75             fail("setSeed failed");
76         }
77     }
78 
testNextBytes()79     public final void testNextBytes() {
80         byte[] b = new byte[5];
81         SecureRandom sr = new SecureRandom();
82         sr.nextBytes(b);
83         for (int i = 0; i < b.length; i++) {
84             if (b[i] != (byte)(i + 0xF1)) {
85                 fail("nextBytes failed");
86             }
87         }
88 
89         try {
90             sr.nextBytes(null);
91             fail("expected exception");
92         } catch (Exception e) {
93             // ok
94         }
95     }
96 
97     /*
98      * Class under test for void SecureRandom()
99      */
testSecureRandom()100     public final void testSecureRandom() {
101         SecureRandom sr = new SecureRandom();
102         if (!sr.getAlgorithm().equals("someRandom")  ||
103                 sr.getProvider()!= p) {
104             fail("incorrect SecureRandom implementation" + p.getName());
105         }
106     }
107 
108     /*
109      * Class under test for void SecureRandom(byte[])
110      */
testSecureRandombyteArray()111     public final void testSecureRandombyteArray() {
112         byte[] b = {1,2,3};
113         new SecureRandom(b);
114 
115         if (!RandomImpl.runEngineSetSeed) {
116             fail("No setSeed");
117         }
118 
119 
120     }
121 
122     /*
123      * Class under test for SecureRandom getInstance(String)
124      */
testGetInstanceString()125     public final void testGetInstanceString() {
126         SecureRandom sr = null;
127         try {
128             sr = SecureRandom.getInstance("someRandom");
129         } catch (NoSuchAlgorithmException e) {
130             fail(e.toString());
131         }
132         if (sr.getProvider() != p || !"someRandom".equals(sr.getAlgorithm())) {
133             fail("getInstance failed");
134         }
135     }
136 
137     /*
138      * Class under test for SecureRandom getInstance(String, String)
139      */
testGetInstanceStringString()140     public final void testGetInstanceStringString() throws Exception {
141         SecureRandom sr = SecureRandom.getInstance("someRandom", "SRProvider");
142         if (sr.getProvider() != p || !"someRandom".equals(sr.getAlgorithm())) {
143             fail("getInstance failed");
144         }
145 
146         try {
147             SecureRandom r = SecureRandom.getInstance("anotherRandom", "SRProvider");
148             fail("expected NoSuchAlgorithmException");
149         } catch (NoSuchAlgorithmException e) {
150             // ok
151         } catch (NoSuchProviderException e) {
152             fail("unexpected: " + e);
153         } catch (IllegalArgumentException e) {
154             fail("unexpected: " + e);
155         } catch (NullPointerException e) {
156             fail("unexpected: " + e);
157         }
158 
159         try {
160             SecureRandom r = SecureRandom.getInstance("someRandom", "UnknownProvider");
161             fail("expected NoSuchProviderException");
162         } catch (NoSuchProviderException e) {
163             // ok
164         } catch (NoSuchAlgorithmException e) {
165             fail("unexpected: " + e);
166         } catch (IllegalArgumentException e) {
167             fail("unexpected: " + e);
168         } catch (NullPointerException e) {
169             fail("unexpected: " + e);
170         }
171 
172         try {
173             SecureRandom r = SecureRandom.getInstance("someRandom", (String)null);
174             fail("expected IllegalArgumentException");
175         } catch (NoSuchProviderException e) {
176             fail("unexpected: " + e);
177         } catch (NoSuchAlgorithmException e) {
178             fail("unexpected: " + e);
179         } catch (IllegalArgumentException e) {
180             // ok
181         } catch (NullPointerException e) {
182             fail("unexpected: " + e);
183         }
184 
185         try {
186             SecureRandom r = SecureRandom.getInstance(null, "SRProvider");
187             fail("expected NullPointerException");
188         } catch (NoSuchProviderException e) {
189             fail("unexpected: " + e);
190         } catch (NoSuchAlgorithmException e) {
191             fail("unexpected: " + e);
192         } catch (IllegalArgumentException e) {
193             fail("unexpected: " + e);
194         } catch (NullPointerException e) {
195             // ok
196         }
197     }
198 
199     /*
200      * Class under test for SecureRandom getInstance(String, Provider)
201      */
testGetInstanceStringProvider()202     public final void testGetInstanceStringProvider() throws Exception {
203         Provider p = new SRProvider();
204         SecureRandom sr = SecureRandom.getInstance("someRandom", p);
205         if (sr.getProvider() != p || !"someRandom".equals(sr.getAlgorithm())) {
206             fail("getInstance failed");
207         }
208 
209         try {
210             SecureRandom r = SecureRandom.getInstance("unknownRandom", p);
211             fail("expected NoSuchAlgorithmException");
212         } catch (NoSuchAlgorithmException e) {
213             // ok
214         } catch (IllegalArgumentException e) {
215             fail("unexpected: " + e);
216         } catch (NullPointerException e) {
217             fail("unexpected: " + e);
218         }
219 
220 
221         try {
222             SecureRandom r = SecureRandom.getInstance(null, p);
223             fail("expected NullPointerException");
224         } catch (NoSuchAlgorithmException e) {
225             fail("unexpected: " + e);
226         } catch (IllegalArgumentException e) {
227             fail("unexpected: " + e);
228         } catch (NullPointerException e) {
229             // ok
230         }
231 
232         try {
233             SecureRandom r = SecureRandom.getInstance("anyRandom", (Provider)null);
234             fail("expected IllegalArgumentException");
235         } catch (NoSuchAlgorithmException e) {
236             fail("unexpected: " + e);
237         } catch (IllegalArgumentException e) {
238             // ok
239         } catch (NullPointerException e) {
240             fail("unexpected: " + e);
241         }
242 
243 
244     }
245 
246     /*
247      * Class under test for void setSeed(byte[])
248      */
testSetSeedbyteArray()249     public final void testSetSeedbyteArray() {
250         byte[] b = {1,2,3};
251         SecureRandom sr = new SecureRandom();
252         sr.setSeed(b);
253         if (!RandomImpl.runEngineSetSeed) {
254             fail("setSeed failed");
255         }
256 
257     }
258 
testGetSeed()259     public final void testGetSeed() {
260         byte[] b = SecureRandom.getSeed(4);
261         if( b.length != 4) {
262             fail("getSeed failed");
263         }
264     }
265 
testGenerateSeed()266     public final void testGenerateSeed() {
267         SecureRandom sr = new SecureRandom();
268         byte[] b = sr.generateSeed(4);
269         for (int i = 0; i < b.length; i++) {
270             if (b[i] != (byte)i) {
271                 fail("generateSeed failed");
272             }
273         }
274     }
275 
276 
277 
278     public class SRProvider extends Provider {
SRProvider()279         public SRProvider() {
280             super("SRProvider", 1.0, "SRProvider for testing");
281             put("SecureRandom.someRandom",
282                     "org.apache.harmony.security.tests.support.RandomImpl");
283         }
284     }
285 
286     class MySecureRandom extends SecureRandom {
MySecureRandom()287         public MySecureRandom(){
288             super();
289         }
290 
nextElement(int numBits)291         public int nextElement(int numBits) {
292             return super.next(numBits);
293         }
294     }
295 }
296