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
145            protected void engineNextBytes(byte[] arg) {
146            }
147
148            protected byte[] engineGenerateSeed(int arg) {
149                return null;
150            }
151        };
152
153        SecureRandom sr = new SecureRandom(spi, null) {
154        };
155
156        assertEquals("unknown", sr.getAlgorithm());
157    }
158
159    //Regression Test for HARMONY-3552.
160    public void test_nextJ() throws Exception {
161        MySecureRandom mySecureRandom = new MySecureRandom(
162                new MySecureRandomSpi(), null);
163        int numBits = 29;
164        int random = mySecureRandom.getNext(numBits);
165        assertEquals(numBits, Integer.bitCount(random));
166
167        numBits = 0;
168        random = mySecureRandom.getNext(numBits);
169        assertEquals(numBits, Integer.bitCount(random));
170
171        numBits = 40;
172        random = mySecureRandom.getNext(numBits);
173        assertEquals(32, Integer.bitCount(random));
174
175        numBits = -1;
176        random = mySecureRandom.getNext(numBits);
177        assertEquals(0, Integer.bitCount(random));
178    }
179
180    class MySecureRandom extends SecureRandom {
181        private static final long serialVersionUID = 1L;
182
183        public MySecureRandom(SecureRandomSpi secureRandomSpi, Provider provider) {
184            super(secureRandomSpi, provider);
185        }
186
187        public int getNext(int numBits) {
188            return super.next(numBits);
189        }
190    }
191
192    class MySecureRandomSpi extends SecureRandomSpi {
193        private static final long serialVersionUID = 1L;
194
195        @Override
196        protected byte[] engineGenerateSeed(int arg0) {
197            return null;
198        }
199
200        @Override
201        protected void engineNextBytes(byte[] bytes) {
202            for (int i = 0; i < bytes.length; i++) {
203                bytes[i] = (byte) 0xFF;
204            }
205        }
206
207        @Override
208        protected void engineSetSeed(byte[] arg0) {
209            return;
210        }
211    }
212}
213