@TestTargetNew(
      level = TestLevel.COMPLETE,
      notes = "",
      method = "getClassName",
      args = {})
  public void testGetClassName() {
    Provider p = new MyProvider();
    Provider.Service s1 = new Provider.Service(p, "type", "algorithm", "className", null, null);
    assertTrue(s1.getClassName().equals("className"));

    Provider.Service s2 =
        new Provider.Service(
            p, "SecureRandom", "algorithm", "tests.java.security.support.RandomImpl", null, null);
    assertTrue(s2.getClassName().equals("tests.java.security.support.RandomImpl"));
  }
  @TestTargetNew(
      level = TestLevel.COMPLETE,
      notes = "",
      method = "Service",
      args = {
        java.security.Provider.class,
        java.lang.String.class,
        java.lang.String.class,
        java.lang.String.class,
        java.util.List.class,
        java.util.Map.class
      })
  public void testService() {
    Provider p = new MyProvider();
    try {
      new Provider.Service(null, "type", "algorithm", "className", null, null);
      fail("provider is null: No expected NullPointerException");
    } catch (NullPointerException e) {
    }
    try {
      new Provider.Service(p, null, "algorithm", "className", null, null);
      fail("type is null: No expected NullPointerException");
    } catch (NullPointerException e) {
    }
    try {
      new Provider.Service(p, "type", null, "className", null, null);
      fail("algorithm is null: No expected NullPointerException");
    } catch (NullPointerException e) {
    }
    try {
      new Provider.Service(p, "type", "algorithm", null, null, null);
      fail("className is null: No expected NullPointerException");
    } catch (NullPointerException e) {
    }

    Provider.Service s = new Provider.Service(p, "type", "algorithm", "className", null, null);

    if (!s.getType().equals("type")) {
      fail("getType() failed");
    }
    if (!s.getAlgorithm().equals("algorithm")) {
      fail("getAlgorithm() failed");
    }
    if (s.getProvider() != p) {
      fail("getProvider() failed");
    }
    if (!s.getClassName().equals("className")) {
      fail("getClassName() failed");
    }
    if (!s.supportsParameter(new Object())) {
      fail("supportsParameter() failed");
    }
  }
Beispiel #3
0
  /**
   * Makes sure all all expected implementations (but not aliases) and that there are no extras,
   * according to what we expect from StandardNames
   */
  public void test_Provider_getServices() throws Exception {

    // build set of expected algorithms
    Map<String, Set<String>> remaining =
        new HashMap<String, Set<String>>(StandardNames.PROVIDER_ALGORITHMS);
    for (Entry<String, Set<String>> entry : remaining.entrySet()) {
      entry.setValue(new HashSet<String>(entry.getValue()));
    }

    List<String> extra = new ArrayList();
    List<String> missing = new ArrayList();

    Provider[] providers = Security.getProviders();
    for (Provider provider : providers) {
      String providerName = provider.getName();
      // ignore BouncyCastle provider if it is installed on the RI
      if (StandardNames.IS_RI && providerName.equals("BC")) {
        continue;
      }
      Set<Provider.Service> services = provider.getServices();
      assertNotNull(services);
      assertFalse(services.isEmpty());

      for (Provider.Service service : services) {
        String type = service.getType();
        String algorithm = service.getAlgorithm().toUpperCase();
        String className = service.getClassName();
        if (false) {
          System.out.println(providerName + " " + type + " " + algorithm + " " + className);
        }

        // remove from remaining, assert unknown if missing
        Set<String> algorithms = remaining.get(type);
        if (algorithms == null || !algorithms.remove(algorithm)) {
          // seems to be missing, but sometimes the same
          // algorithm is available from multiple providers
          // (e.g. KeyFactory RSA is available from
          // SunRsaSign and SunJSSE), so double check in
          // original source before giving error
          if (!(StandardNames.PROVIDER_ALGORITHMS.containsKey(type)
              && StandardNames.PROVIDER_ALGORITHMS.get(type).contains(algorithm))) {
            extra.add("Unknown " + type + " " + algorithm + " " + providerName + "\n");
          }
        }
        if (algorithms != null && algorithms.isEmpty()) {
          remaining.remove(type);
        }

        // make sure class exists and can be initialized
        try {
          assertNotNull(Class.forName(className, true, provider.getClass().getClassLoader()));
        } catch (ClassNotFoundException e) {
          // Sun forgot their own class
          if (!className.equals("sun.security.pkcs11.P11MAC")) {
            missing.add(className);
          }
        }
      }
    }

    // assert that we don't have any extra in the implementation
    Collections.sort(extra); // sort so that its grouped by type
    assertEquals("Extra algorithms", Collections.EMPTY_LIST, extra);

    // assert that we don't have any missing in the implementation
    assertEquals("Missing algorithms", Collections.EMPTY_MAP, remaining);

    // assert that we don't have any missing classes
    Collections.sort(missing); // sort it for readability
    assertEquals("Missing classes", Collections.EMPTY_LIST, missing);
  }