2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
5506
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
* @test
|
|
26 |
* @bug 4915392
|
|
27 |
* @summary test that the getAlgorithm() method works correctly
|
|
28 |
* @author Andreas Sterbenz
|
|
29 |
*/
|
|
30 |
|
|
31 |
import java.io.*;
|
|
32 |
|
|
33 |
import java.security.*;
|
|
34 |
|
|
35 |
public class GetAlgorithm {
|
|
36 |
|
|
37 |
private final static String BASE = System.getProperty("test.src", ".");
|
|
38 |
|
|
39 |
public static void main(String[] args) throws Exception {
|
|
40 |
SecureRandom sr;
|
|
41 |
|
|
42 |
sr = new SecureRandom();
|
|
43 |
if (sr.getAlgorithm().equals("unknown")) {
|
|
44 |
throw new Exception("Unknown: " + sr.getAlgorithm());
|
|
45 |
}
|
|
46 |
|
|
47 |
sr = SecureRandom.getInstance("SHA1PRNG");
|
|
48 |
check("SHA1PRNG", sr);
|
|
49 |
|
|
50 |
// OutputStream out = new FileOutputStream("sha1prng.bin");
|
|
51 |
// ObjectOutputStream oout = new ObjectOutputStream(out);
|
|
52 |
// sr.nextInt();
|
|
53 |
// oout.writeObject(sr);
|
|
54 |
// oout.flush();
|
|
55 |
// oout.close();
|
|
56 |
|
|
57 |
sr = new MySecureRandom();
|
|
58 |
check("unknown", sr);
|
|
59 |
|
|
60 |
InputStream in = new FileInputStream(new File(BASE, "sha1prng-old.bin"));
|
|
61 |
ObjectInputStream oin = new ObjectInputStream(in);
|
|
62 |
sr = (SecureRandom)oin.readObject();
|
|
63 |
oin.close();
|
|
64 |
check("unknown", sr);
|
|
65 |
|
|
66 |
in = new FileInputStream(new File(BASE, "sha1prng-new.bin"));
|
|
67 |
oin = new ObjectInputStream(in);
|
|
68 |
sr = (SecureRandom)oin.readObject();
|
|
69 |
oin.close();
|
|
70 |
check("SHA1PRNG", sr);
|
|
71 |
|
|
72 |
System.out.println("All tests passed");
|
|
73 |
}
|
|
74 |
|
|
75 |
private static void check(String s1, SecureRandom sr) throws Exception {
|
|
76 |
String s2 = sr.getAlgorithm();
|
|
77 |
if (s1.equals(s2) == false) {
|
|
78 |
throw new Exception("Expected " + s1 + ", got " + s2);
|
|
79 |
}
|
|
80 |
}
|
|
81 |
|
|
82 |
private static class MySecureRandom extends SecureRandom {
|
|
83 |
|
|
84 |
}
|
|
85 |
|
|
86 |
}
|