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
18package org.apache.harmony.security.tests.java.security;
19
20import java.security.NoSuchAlgorithmException;
21import java.security.NoSuchProviderException;
22import java.security.Provider;
23import java.security.SecureRandom;
24import java.security.SecureRandomSpi;
25import java.security.Security;
26
27public class SecureRandom2Test extends junit.framework.TestCase {
28
29	private static final byte[] SEED_BYTES = { (byte) 33, (byte) 15, (byte) -3,
30			(byte) 22, (byte) 77, (byte) -16, (byte) -33, (byte) 56 };
31
32	private static final int SEED_SIZE = 539;
33
34	private static final long SEED_VALUE = 5335486759L;
35
36	/**
37	 * @tests java.security.SecureRandom#SecureRandom()
38	 */
39	public void test_Constructor() {
40		// Test for method java.security.SecureRandom()
41		new SecureRandom();
42	}
43
44	/**
45	 * @tests java.security.SecureRandom#SecureRandom(byte[])
46	 */
47	public void test_Constructor$B() {
48		// Test for method java.security.SecureRandom(byte [])
49		new SecureRandom(SEED_BYTES);
50	}
51
52	/**
53	 * @tests java.security.SecureRandom#generateSeed(int)
54	 */
55	public void test_generateSeedI() {
56		// Test for method byte [] java.security.SecureRandom.generateSeed(int)
57		byte[] seed = new SecureRandom().generateSeed(SEED_SIZE);
58		assertEquals("seed has incorrect size", SEED_SIZE, seed.length);
59	}
60
61	/**
62	 * @tests java.security.SecureRandom#getInstance(java.lang.String)
63	 */
64	public void test_getInstanceLjava_lang_String() {
65		// Test for method java.security.SecureRandom
66		// java.security.SecureRandom.getInstance(java.lang.String)
67		try {
68			SecureRandom.getInstance("SHA1PRNG");
69		} catch (NoSuchAlgorithmException e) {
70			fail("getInstance did not find a SHA1PRNG algorithm");
71		}
72	}
73
74	/**
75	 * @tests java.security.SecureRandom#getInstance(java.lang.String,
76	 *        java.lang.String)
77	 */
78	public void test_getInstanceLjava_lang_StringLjava_lang_String() {
79		// Test for method java.security.SecureRandom
80		// java.security.SecureRandom.getInstance(java.lang.String,
81		// java.lang.String)
82		try {
83			Provider[] providers = Security
84					.getProviders("SecureRandom.SHA1PRNG");
85			if (providers != null) {
86				for (int i = 0; i < providers.length; i++) {
87					SecureRandom
88							.getInstance("SHA1PRNG", providers[i].getName());
89				}// end for
90			} else {
91				fail("No providers support SHA1PRNG");
92			}
93		} catch (NoSuchAlgorithmException e) {
94			fail("getInstance did not find a SHA1PRNG algorithm");
95		} catch (NoSuchProviderException e) {
96			fail("getInstance did not find the provider for SHA1PRNG");
97		}
98	}
99
100	/**
101	 * @tests java.security.SecureRandom#getSeed(int)
102	 */
103	public void test_getSeedI() {
104		// Test for method byte [] java.security.SecureRandom.getSeed(int)
105		byte[] seed = SecureRandom.getSeed(SEED_SIZE);
106		assertEquals("seed has incorrect size", SEED_SIZE, seed.length);
107	}
108
109	/**
110	 * @tests java.security.SecureRandom#nextBytes(byte[])
111	 */
112	public void test_nextBytes$B() {
113		// Test for method void java.security.SecureRandom.nextBytes(byte [])
114		byte[] bytes = new byte[313];
115		new SecureRandom().nextBytes(bytes);
116	}
117
118	/**
119	 * @tests java.security.SecureRandom#setSeed(byte[])
120	 */
121	public void test_setSeed$B() {
122		// Test for method void java.security.SecureRandom.setSeed(byte [])
123		new SecureRandom().setSeed(SEED_BYTES);
124	}
125
126	/**
127	 * @tests java.security.SecureRandom#setSeed(long)
128	 */
129	public void test_setSeedJ() {
130		// Test for method void java.security.SecureRandom.setSeed(long)
131		new SecureRandom().setSeed(SEED_VALUE);
132	}
133
134	/**
135     * @tests java.security.SecureRandom#getAlgorithm()
136     */
137    public void test_getAlgorithm() {
138        // Regression for HARMONY-750
139
140        SecureRandomSpi spi = new SecureRandomSpi() {
141
142            protected void engineSetSeed(byte[] arg) {}
143
144            protected void engineNextBytes(byte[] arg) {}
145
146            protected byte[] engineGenerateSeed(int arg) {
147                return null;
148            }
149        };
150
151        SecureRandom sr = new SecureRandom(spi, null) {};
152
153        assertEquals("unknown", sr.getAlgorithm());
154    }
155
156    //Regression Test for HARMONY-3552.
157    public void test_nextJ() throws Exception {
158        MySecureRandom mySecureRandom = new MySecureRandom(
159                new MySecureRandomSpi(), null);
160        int numBits = 29;
161        int random = mySecureRandom.getNext(numBits);
162        assertEquals(numBits, Integer.bitCount(random));
163
164        numBits = 0;
165        random = mySecureRandom.getNext(numBits);
166        assertEquals(numBits, Integer.bitCount(random));
167
168        numBits = 40;
169        random = mySecureRandom.getNext(numBits);
170        assertEquals(32, Integer.bitCount(random));
171
172        numBits = -1;
173        random = mySecureRandom.getNext(numBits);
174        assertEquals(0, Integer.bitCount(random));
175    }
176
177    class MySecureRandom extends SecureRandom {
178        private static final long serialVersionUID = 1L;
179
180        public MySecureRandom(SecureRandomSpi secureRandomSpi, Provider provider) {
181            super(secureRandomSpi, provider);
182        }
183
184        public int getNext(int numBits) {
185            return super.next(numBits);
186        }
187    }
188
189    class MySecureRandomSpi extends SecureRandomSpi {
190        private static final long serialVersionUID = 1L;
191
192        @Override
193        protected byte[] engineGenerateSeed(int arg0) {
194            return null;
195        }
196
197        @Override
198        protected void engineNextBytes(byte[] bytes) {
199            for (int i = 0; i < bytes.length; i++) {
200                bytes[i] = (byte) 0xFF;
201            }
202        }
203
204        @Override
205        protected void engineSetSeed(byte[] arg0) {
206            return;
207        }
208    }
209}
210