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: /**
019: * @author Boris V. Kuznetsov
020: * @version $Revision$
021: */package org.apache.harmony.security.tests.java.security;
022:
023: import java.security.NoSuchAlgorithmException;
024: import java.security.Provider;
025: import java.security.SecureRandom;
026: import java.security.Security;
027:
028: import junit.framework.TestCase;
029:
030: /**
031: *
032: * Tests for internal Secure Random implementation based on Random
033: *
034: */
035: public class SecureRandom_ImplTest extends TestCase {
036:
037: /**
038: * Registered providers
039: */
040: Provider providers[] = Security.getProviders();
041:
042: /*
043: * @see TestCase#setUp()
044: */
045: protected void setUp() throws Exception {
046: super .setUp();
047: // remove all registered providers
048: for (int i = 0; i < providers.length; i++) {
049: Security.removeProvider(providers[i].getName());
050: }
051: }
052:
053: /*
054: * @see TestCase#tearDown()
055: */
056: protected void tearDown() throws Exception {
057: super .tearDown();
058: // restore all registered providers
059: for (int i = 0; i < providers.length; i++) {
060: Security.addProvider(providers[i]);
061: }
062: }
063:
064: /*
065: * Class under test for void setSeed(long)
066: */
067: public final void testSetSeedlong() {
068: SecureRandom sr = new SecureRandom();
069: sr.setSeed(0);
070: sr.setSeed(-1);
071: sr.setSeed(11111111111L);
072: }
073:
074: public final void testNextBytes() {
075: SecureRandom sr = new SecureRandom();
076: sr.nextBytes(new byte[20]);
077: sr.nextBytes(new byte[1]);
078:
079: //Not specified behavior: throws NullPointerException if bytes is null
080: try {
081: sr.nextBytes(null);
082: } catch (NullPointerException e) {
083: }
084: sr.nextBytes(new byte[5]);
085: }
086:
087: /*
088: * Class under test for SecureRandom getInstance(String)
089: */
090: public final void testGetInstanceString() {
091: try {
092: SecureRandom.getInstance("SHA1PRNG");
093: fail("No expected NoSuchAlgorithmException");
094: } catch (NoSuchAlgorithmException e) {
095: }
096: }
097:
098: public final void testGetProvider() {
099: assertNull("Non null provider", new SecureRandom()
100: .getProvider());
101: }
102:
103: public final void testGetAlgorithm() {
104: //test default implementation
105: SecureRandom sr = new SecureRandom();
106: assertEquals("Incorrect algorithm", "SHA1PRNG", sr
107: .getAlgorithm());
108: assertNull("Incorrect provider", sr.getProvider());
109:
110: //just in case
111: sr.nextBytes(new byte[100]);
112: }
113:
114: /*
115: * Class under test for void setSeed(byte[])
116: */
117: public final void testSetSeedbyteArray() {
118: SecureRandom sr = new SecureRandom();
119:
120: //Not specified behavior: throws NullPointerException if bytes is null
121: try {
122: sr.setSeed(null);
123: } catch (NullPointerException e) {
124: }
125:
126: byte[] seed = { 1, 2, 3, 4 };
127: sr.setSeed(seed);
128:
129: byte[] seed1 = { 1, 2, 3, 4, -2, 100, 9, 111 };
130: sr.setSeed(seed1);
131: }
132:
133: public final void testGetSeed() {
134: byte[] seed = SecureRandom.getSeed(5);
135: new SecureRandom(seed).nextBytes(new byte[20]);
136: }
137:
138: public final void testGenerateSeed() {
139: SecureRandom sr = new SecureRandom();
140: byte[] seed = sr.generateSeed(5);
141: new SecureRandom(seed).nextBytes(new byte[20]);
142: }
143: }
|