@TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getType", args = {}) public void testGetType() { Provider p = new MyProvider(); Provider.Service s1 = new Provider.Service(p, "type", "algorithm", "className", null, null); assertTrue(s1.getType().equals("type")); Provider.Service s2 = new Provider.Service( p, "SecureRandom", "algorithm", "tests.java.security.support.RandomImpl", null, null); assertTrue(s2.getType().equals("SecureRandom")); }
// NOTE: none (at least for BC 1.47) private static String getSecureRandomAlgorithm(final Provider provider) { for (Provider.Service service : provider.getServices()) { if ("SecureRandom".equals(service.getType())) { return service.getAlgorithm(); } } return null; }
@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"); } }
void initiateProviderKeyStore(Provider provider) throws Exception { String prov = null; for (Provider.Service ps : provider.getServices()) { if (ps.getType().equals("KeyStore")) { prov = ps.getAlgorithm(); } } Security.addProvider(provider); KeyStore ks = KeyStore.getInstance(prov, provider); ks.load( CMD_keystore.found ? new FileInputStream(CMD_keystore.getString()) : null, CMD_keypass.found ? CMD_keypass.getString().toCharArray() : null); wrap.setKeyStore(ks, CMD_keypass.getString()); CMD_keystore.found = false; }
private String[] getAvailableBuiltInDigestAlgorithmNames() { Set<String> algorithmsSet = new HashSet<String>(); Provider[] providers = Security.getProviders(); for (Provider provider : providers) { Set<Provider.Service> services = provider.getServices(); for (Provider.Service service : services) { if (service.getType() == "MessageDigest") { algorithmsSet.add(service.getAlgorithm()); } } } // For some reason my Java implementation gives SHA1 two names, so we will remove one. if (algorithmsSet.contains("SHA") && algorithmsSet.contains("SHA1")) { algorithmsSet.remove("SHA"); } return algorithmsSet.toArray(new String[algorithmsSet.size()]); }
/** * 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); }