001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.security.tests.java.security;
019:
020: import java.security.NoSuchAlgorithmException;
021: import java.security.NoSuchProviderException;
022: import java.security.Provider;
023: import java.security.SecureRandom;
024: import java.security.SecureRandomSpi;
025: import java.security.Security;
026:
027: public class SecureRandom2Test extends junit.framework.TestCase {
028:
029: private static final byte[] SEED_BYTES = { (byte) 33, (byte) 15,
030: (byte) -3, (byte) 22, (byte) 77, (byte) -16, (byte) -33,
031: (byte) 56 };
032:
033: private static final int SEED_SIZE = 539;
034:
035: private static final long SEED_VALUE = 5335486759L;
036:
037: /**
038: * @tests java.security.SecureRandom#SecureRandom()
039: */
040: public void test_Constructor() {
041: // Test for method java.security.SecureRandom()
042: new SecureRandom();
043: }
044:
045: /**
046: * @tests java.security.SecureRandom#SecureRandom(byte[])
047: */
048: public void test_Constructor$B() {
049: // Test for method java.security.SecureRandom(byte [])
050: new SecureRandom(SEED_BYTES);
051: }
052:
053: /**
054: * @tests java.security.SecureRandom#generateSeed(int)
055: */
056: public void test_generateSeedI() {
057: // Test for method byte [] java.security.SecureRandom.generateSeed(int)
058: byte[] seed = new SecureRandom().generateSeed(SEED_SIZE);
059: assertEquals("seed has incorrect size", SEED_SIZE, seed.length);
060: }
061:
062: /**
063: * @tests java.security.SecureRandom#getInstance(java.lang.String)
064: */
065: public void test_getInstanceLjava_lang_String() {
066: // Test for method java.security.SecureRandom
067: // java.security.SecureRandom.getInstance(java.lang.String)
068: try {
069: SecureRandom.getInstance("SHA1PRNG");
070: } catch (NoSuchAlgorithmException e) {
071: fail("getInstance did not find a SHA1PRNG algorithm");
072: }
073: }
074:
075: /**
076: * @tests java.security.SecureRandom#getInstance(java.lang.String,
077: * java.lang.String)
078: */
079: public void test_getInstanceLjava_lang_StringLjava_lang_String() {
080: // Test for method java.security.SecureRandom
081: // java.security.SecureRandom.getInstance(java.lang.String,
082: // java.lang.String)
083: try {
084: Provider[] providers = Security
085: .getProviders("SecureRandom.SHA1PRNG");
086: if (providers != null) {
087: for (int i = 0; i < providers.length; i++) {
088: SecureRandom.getInstance("SHA1PRNG", providers[i]
089: .getName());
090: }// end for
091: } else {
092: fail("No providers support SHA1PRNG");
093: }
094: } catch (NoSuchAlgorithmException e) {
095: fail("getInstance did not find a SHA1PRNG algorithm");
096: } catch (NoSuchProviderException e) {
097: fail("getInstance did not find the provider for SHA1PRNG");
098: }
099: }
100:
101: /**
102: * @tests java.security.SecureRandom#getSeed(int)
103: */
104: public void test_getSeedI() {
105: // Test for method byte [] java.security.SecureRandom.getSeed(int)
106: byte[] seed = SecureRandom.getSeed(SEED_SIZE);
107: assertEquals("seed has incorrect size", SEED_SIZE, seed.length);
108: }
109:
110: /**
111: * @tests java.security.SecureRandom#nextBytes(byte[])
112: */
113: public void test_nextBytes$B() {
114: // Test for method void java.security.SecureRandom.nextBytes(byte [])
115: byte[] bytes = new byte[313];
116: new SecureRandom().nextBytes(bytes);
117: }
118:
119: /**
120: * @tests java.security.SecureRandom#setSeed(byte[])
121: */
122: public void test_setSeed$B() {
123: // Test for method void java.security.SecureRandom.setSeed(byte [])
124: new SecureRandom().setSeed(SEED_BYTES);
125: }
126:
127: /**
128: * @tests java.security.SecureRandom#setSeed(long)
129: */
130: public void test_setSeedJ() {
131: // Test for method void java.security.SecureRandom.setSeed(long)
132: new SecureRandom().setSeed(SEED_VALUE);
133: }
134:
135: /**
136: * @tests java.security.SecureRandom#getAlgorithm()
137: */
138: public void test_getAlgorithm() {
139: // Regression for HARMONY-750
140:
141: SecureRandomSpi spi = new SecureRandomSpi() {
142:
143: protected void engineSetSeed(byte[] arg) {
144: }
145:
146: protected void engineNextBytes(byte[] arg) {
147: }
148:
149: protected byte[] engineGenerateSeed(int arg) {
150: return null;
151: }
152: };
153:
154: SecureRandom sr = new SecureRandom(spi, null) {
155: };
156:
157: assertEquals("unknown", sr.getAlgorithm());
158: }
159:
160: //Regression Test for HARMONY-3552.
161: public void test_nextJ() throws Exception {
162: MySecureRandom mySecureRandom = new MySecureRandom(
163: new MySecureRandomSpi(), null);
164: int numBits = 29;
165: int random = mySecureRandom.getNext(numBits);
166: assertEquals(numBits, Integer.bitCount(random));
167:
168: numBits = 0;
169: random = mySecureRandom.getNext(numBits);
170: assertEquals(numBits, Integer.bitCount(random));
171:
172: numBits = 40;
173: random = mySecureRandom.getNext(numBits);
174: assertEquals(32, Integer.bitCount(random));
175:
176: numBits = -1;
177: random = mySecureRandom.getNext(numBits);
178: assertEquals(0, Integer.bitCount(random));
179: }
180:
181: class MySecureRandom extends SecureRandom {
182: private static final long serialVersionUID = 1L;
183:
184: public MySecureRandom(SecureRandomSpi secureRandomSpi,
185: Provider provider) {
186: super (secureRandomSpi, provider);
187: }
188:
189: public int getNext(int numBits) {
190: return super .next(numBits);
191: }
192: }
193:
194: class MySecureRandomSpi extends SecureRandomSpi {
195: private static final long serialVersionUID = 1L;
196:
197: @Override
198: protected byte[] engineGenerateSeed(int arg0) {
199: return null;
200: }
201:
202: @Override
203: protected void engineNextBytes(byte[] bytes) {
204: for (int i = 0; i < bytes.length; i++) {
205: bytes[i] = (byte) 0xFF;
206: }
207: }
208:
209: @Override
210: protected void engineSetSeed(byte[] arg0) {
211: return;
212: }
213: }
214: }
|