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 java.security;
022:
023: import org.apache.harmony.security.tests.support.RandomImpl;
024:
025: import junit.framework.TestCase;
026:
027: /**
028: * Tests for <code>SecureRandom</code> constructor and methods
029: *
030: */
031: public class SecureRandomTest extends TestCase {
032:
033: /**
034: * SRProvider
035: */
036: Provider p;
037:
038: /*
039: * @see TestCase#setUp()
040: */
041: protected void setUp() throws Exception {
042: super .setUp();
043: p = new SRProvider();
044: Security.insertProviderAt(p, 1);
045: }
046:
047: /*
048: * @see TestCase#tearDown()
049: */
050: protected void tearDown() throws Exception {
051: super .tearDown();
052: Security.removeProvider(p.getName());
053: }
054:
055: public final void testNext() {
056: SecureRandom sr = new SecureRandom();
057: if (sr.next(1) != 1 || sr.next(2) != 3 || sr.next(3) != 7) {
058: fail("next failed");
059: }
060: }
061:
062: /*
063: * Class under test for void setSeed(long)
064: */
065: public final void testSetSeedlong() {
066: SecureRandom sr = new SecureRandom();
067: sr.setSeed(12345);
068: if (!RandomImpl.runEngineSetSeed) {
069: fail("setSeed failed");
070: }
071: }
072:
073: public final void testNextBytes() {
074: byte[] b = new byte[5];
075: SecureRandom sr = new SecureRandom();
076: sr.nextBytes(b);
077: for (int i = 0; i < b.length; i++) {
078: if (b[i] != (byte) (i + 0xF1)) {
079: fail("nextBytes failed");
080: }
081: }
082: }
083:
084: /*
085: * Class under test for void SecureRandom()
086: */
087: public final void testSecureRandom() {
088: SecureRandom sr = new SecureRandom();
089: if (!sr.getAlgorithm().equals("someRandom")
090: || sr.getProvider() != p) {
091: fail("incorrect SecureRandom implementation" + p.getName());
092: }
093: }
094:
095: /*
096: * Class under test for void SecureRandom(byte[])
097: */
098: public final void testSecureRandombyteArray() {
099: byte[] b = { 1, 2, 3 };
100: new SecureRandom(b);
101: if (!RandomImpl.runEngineSetSeed) {
102: fail("No setSeed");
103: }
104: }
105:
106: /*
107: * Class under test for SecureRandom getInstance(String)
108: */
109: public final void testGetInstanceString() {
110: SecureRandom sr = null;
111: try {
112: sr = SecureRandom.getInstance("someRandom");
113: } catch (NoSuchAlgorithmException e) {
114: fail(e.toString());
115: }
116: if (sr.getProvider() != p
117: || !"someRandom".equals(sr.getAlgorithm())) {
118: fail("getInstance failed");
119: }
120: }
121:
122: /*
123: * Class under test for SecureRandom getInstance(String, String)
124: */
125: public final void testGetInstanceStringString() throws Exception {
126: SecureRandom sr = SecureRandom.getInstance("someRandom",
127: "SRProvider");
128: if (sr.getProvider() != p
129: || !"someRandom".equals(sr.getAlgorithm())) {
130: fail("getInstance failed");
131: }
132: }
133:
134: /*
135: * Class under test for SecureRandom getInstance(String, Provider)
136: */
137: public final void testGetInstanceStringProvider() throws Exception {
138: Provider p = new SRProvider();
139: SecureRandom sr = SecureRandom.getInstance("someRandom", p);
140: if (sr.getProvider() != p
141: || !"someRandom".equals(sr.getAlgorithm())) {
142: fail("getInstance failed");
143: }
144: }
145:
146: /*
147: * Class under test for void setSeed(byte[])
148: */
149: public final void testSetSeedbyteArray() {
150: byte[] b = { 1, 2, 3 };
151: SecureRandom sr = new SecureRandom();
152: sr.setSeed(b);
153: if (!RandomImpl.runEngineSetSeed) {
154: fail("setSeed failed");
155: }
156: }
157:
158: public final void testGetSeed() {
159: byte[] b = SecureRandom.getSeed(4);
160: if (b.length != 4) {
161: fail("getSeed failed");
162: }
163: }
164:
165: public final void testGenerateSeed() {
166: SecureRandom sr = new SecureRandom();
167: byte[] b = sr.generateSeed(4);
168: for (int i = 0; i < b.length; i++) {
169: if (b[i] != (byte) i) {
170: fail("generateSeed failed");
171: }
172: }
173: }
174:
175: class SRProvider extends Provider {
176: public SRProvider() {
177: super ("SRProvider", 1.0, "SRProvider for testing");
178: put("SecureRandom.someRandom",
179: "org.apache.harmony.security.tests.support.RandomImpl");
180: }
181: }
182: }
|