/test/jdk/java/security/SecureRandom/DefaultProvider.java

https://github.com/openjdk/jdk · Java · 94 lines · 55 code · 10 blank · 29 comment · 8 complexity · 4ca35ec0cf1b516875a65c9e2e92fd7c MD5 · raw file

  1. /*
  2. * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
  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. *
  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.
  22. */
  23. import static java.lang.System.out;
  24. import java.security.NoSuchAlgorithmException;
  25. import java.security.SecureRandom;
  26. /**
  27. * @test
  28. * @bug 8048356
  29. * @summary Assert default provider used on all OS for SecureRandom
  30. */
  31. public class DefaultProvider {
  32. private static final String OS_NAME = System.getProperty("os.name");
  33. private static final String WINDOWS = "Windows";
  34. public static void main(String[] args) throws NoSuchAlgorithmException {
  35. out.println("Operating System: " + OS_NAME);
  36. /* Test default provider used with constructor */
  37. out.println("TEST: Default provider with constructor");
  38. SecureRandom secureRandom = new SecureRandom();
  39. String provider = secureRandom.getProvider().getName();
  40. if (!provider.equals("SUN")) {
  41. throw new RuntimeException("Unexpected provider name: "
  42. + provider);
  43. }
  44. out.println("Passed, default provider with constructor: " + provider);
  45. /* Test default provider with getInstance(String algorithm) */
  46. out.println("TEST: SHA1PRNG supported on all platforms by SUN provider");
  47. String algorithm = "SHA1PRNG";
  48. provider = "SUN";
  49. SecureRandom instance = SecureRandom.getInstance(algorithm);
  50. assertInstance(instance, algorithm, provider);
  51. out.println("Passed.");
  52. if (!OS_NAME.startsWith(WINDOWS)) {
  53. out.println("TEST: NativePRNG supported on all platforms"
  54. + "(except Windows), by SUN provider");
  55. algorithm = "NativePRNG";
  56. provider = "SUN";
  57. } else {
  58. out.println(
  59. "TEST: Windows-PRNG supported on windows by SunMSCAPI provider");
  60. algorithm = "Windows-PRNG";
  61. provider = "SunMSCAPI";
  62. }
  63. instance = SecureRandom.getInstance(algorithm);
  64. assertInstance(instance, algorithm, provider);
  65. out.println("Passed.");
  66. }
  67. private static void assertInstance(SecureRandom instance,
  68. String expectedAlgorithm,
  69. String expectedProvider) {
  70. if (instance != null) {
  71. if (!expectedAlgorithm.equalsIgnoreCase(instance.getAlgorithm())) {
  72. throw new RuntimeException("Expected algorithm:"
  73. + expectedAlgorithm + " actual: " + instance.getAlgorithm());
  74. }
  75. if (!expectedProvider.equalsIgnoreCase(instance.getProvider().getName())) {
  76. throw new RuntimeException("Expected provider: "
  77. + expectedProvider + " actual: "
  78. + instance.getProvider().getName());
  79. }
  80. } else {
  81. throw new RuntimeException("Secure instance is not created");
  82. }
  83. }
  84. }