StrongSeedReader.java revision 7027:b600d637ef77
172339Sabial /*
272339Sabial * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
372339Sabial * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
472339Sabial *
572339Sabial * This code is free software; you can redistribute it and/or modify it
672339Sabial * under the terms of the GNU General Public License version 2 only, as
772339Sabial * published by the Free Software Foundation.
872339Sabial *
972339Sabial * This code is distributed in the hope that it will be useful, but WITHOUT
1072339Sabial * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1172339Sabial * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1272339Sabial * version 2 for more details (a copy is included in the LICENSE file that
1372339Sabial * accompanied this code).
1472339Sabial *
1572339Sabial * You should have received a copy of the GNU General Public License version
1672339Sabial * 2 along with this work; if not, write to the Free Software Foundation,
1772339Sabial * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1872339Sabial *
1972339Sabial * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2072339Sabial * or visit www.oracle.com if you need additional information or have any
2172339Sabial * questions.
2272339Sabial */
2372339Sabial
2472339Sabial/**
2572339Sabial * @test
2672339Sabial * @bug 6425477
2772339Sabial * @summary Better support for generation of high entropy random numbers
2872339Sabial * @run main/othervm StrongSeedReader
2972339Sabial */
3072339Sabial
3172339Sabialimport java.io.*;
3272339Sabialimport java.net.*;
3372339Sabialimport java.security.SecureRandom;
3472339Sabial
3572339Sabial/**
3672339Sabial * A simple test which takes into account knowledge about the underlying
3772339Sabial * implementation. This may change if the implementations change.
3872339Sabial *
3972339Sabial * Create a new EGD file with known bytes, then set the EGD System property. The
4072339Sabial * data read should be the same as what was written.
4172339Sabial */
4272339Sabialpublic class StrongSeedReader {
4372339Sabial
4472339Sabial    public static void main(String[] args) throws Exception {
4572339Sabial        // Skip Windows, the SHA1PRNG uses CryptGenRandom.
4672339Sabial        if (System.getProperty("os.name", "unknown").startsWith("Windows")) {
4772339Sabial            return;
4872339Sabial        }
4972339Sabial
5072339Sabial        File file = null;
5172339Sabial        try {
5272339Sabial            file = new File(System.getProperty("java.io.tmpdir"),
5372339Sabial                    "StrongSeedReader.tmpdata");
5472339Sabial
5572339Sabial            // write a bunch of 0's to the file.
5672339Sabial            FileOutputStream fos = new FileOutputStream(file);
5772339Sabial            fos.write(new byte[2048]);
5872339Sabial
5972339Sabial            System.setProperty("java.security.egd", file.toURI().toString());
6072339Sabial            testSeed("NativePRNG");
6172339Sabial            testSeed("SHA1PRNG");
6272339Sabial        } finally {
6372339Sabial            if (file != null) {
6472339Sabial                file.delete();
6572339Sabial            }
6672339Sabial        }
6772339Sabial    }
6872339Sabial
6972339Sabial    private static void testSeed(String alg) throws Exception {
7072339Sabial        System.out.println("Testing: " + alg);
7172339Sabial        SecureRandom sr = SecureRandom.getInstance(alg);
7272339Sabial        byte[] ba = sr.generateSeed(20);
7372339Sabial
7472339Sabial        // We should get back a bunch of zeros from the file.
7572339Sabial        for (byte b : ba) {
7672339Sabial            if (b != 0) {
7772339Sabial                throw new Exception("Byte != 0");
7872339Sabial            }
7972339Sabial        }
8072339Sabial    }
8172339Sabial}
8272339Sabial